Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / renderer / battery_status / battery_status_dispatcher_unittest.cc
blobdd0bed6ca2388695109f4ee5527d4b6b5fe8c92a
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "battery_status_dispatcher.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "content/common/battery_status_messages.h"
10 #include "content/public/test/mock_render_thread.h"
11 #include "content/public/test/test_utils.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/WebKit/public/platform/WebBatteryStatusListener.h"
15 namespace content {
17 class MockBatteryStatusListener : public blink::WebBatteryStatusListener {
18 public:
19 MockBatteryStatusListener() : did_change_battery_status_(false) { }
20 virtual ~MockBatteryStatusListener() { }
22 // blink::WebBatteryStatusListener method.
23 virtual void updateBatteryStatus(
24 const blink::WebBatteryStatus& status) OVERRIDE {
25 status_ = status;
26 did_change_battery_status_ = true;
29 const blink::WebBatteryStatus& status() const { return status_; }
30 bool did_change_battery_status() const { return did_change_battery_status_; }
32 private:
33 bool did_change_battery_status_;
34 blink::WebBatteryStatus status_;
36 DISALLOW_COPY_AND_ASSIGN(MockBatteryStatusListener);
39 class BatteryStatusDispatcherTest : public testing::Test {
40 private:
41 // We need to create a MockRenderThread so RenderThread::Get() doesn't return
42 // null.
43 MockRenderThread render_thread_;
46 TEST_F(BatteryStatusDispatcherTest, UpdateListener) {
47 MockBatteryStatusListener listener;
48 BatteryStatusDispatcher dispatcher(0);
50 blink::WebBatteryStatus status;
51 status.charging = true;
52 status.chargingTime = 100;
53 status.dischargingTime = 200;
54 status.level = 0.5;
56 dispatcher.Start(&listener);
58 BatteryStatusMsg_DidChange message(status);
59 dispatcher.OnControlMessageReceived(message);
61 const blink::WebBatteryStatus& received_status = listener.status();
62 EXPECT_TRUE(listener.did_change_battery_status());
63 EXPECT_EQ(status.charging, received_status.charging);
64 EXPECT_EQ(status.chargingTime, received_status.chargingTime);
65 EXPECT_EQ(status.dischargingTime, received_status.dischargingTime);
66 EXPECT_EQ(status.level, received_status.level);
68 dispatcher.Stop();
71 TEST_F(BatteryStatusDispatcherTest, NoUpdateWhenNullListener) {
72 MockBatteryStatusListener listener;
73 BatteryStatusDispatcher dispatcher(0);
75 dispatcher.Start(0);
76 dispatcher.Stop();
78 blink::WebBatteryStatus status;
79 BatteryStatusMsg_DidChange message(status);
80 dispatcher.OnControlMessageReceived(message);
81 EXPECT_FALSE(listener.did_change_battery_status());
84 } // namespace content