| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "battery_status_dispatcher.h" | 5 #include "battery_status_dispatcher.h" |
| 6 | 6 |
| 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" | 7 #include "content/public/test/mock_render_thread.h" |
| 11 #include "content/public/test/test_utils.h" | 8 #include "content/public/test/test_utils.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/WebKit/public/platform/WebBatteryStatusListener.h" | 10 #include "third_party/WebKit/public/platform/WebBatteryStatusListener.h" |
| 14 | 11 |
| 15 namespace content { | 12 namespace content { |
| 16 | 13 |
| 17 class MockBatteryStatusListener : public blink::WebBatteryStatusListener { | 14 class MockBatteryStatusListener : public blink::WebBatteryStatusListener { |
| 18 public: | 15 public: |
| 19 MockBatteryStatusListener() : did_change_battery_status_(false) { } | 16 MockBatteryStatusListener() : did_change_battery_status_(false) {} |
| 20 virtual ~MockBatteryStatusListener() { } | 17 virtual ~MockBatteryStatusListener() {} |
| 21 | 18 |
| 22 // blink::WebBatteryStatusListener method. | 19 // blink::WebBatteryStatusListener method. |
| 23 virtual void updateBatteryStatus( | 20 void updateBatteryStatus(const blink::WebBatteryStatus& status) override { |
| 24 const blink::WebBatteryStatus& status) override { | |
| 25 status_ = status; | 21 status_ = status; |
| 26 did_change_battery_status_ = true; | 22 did_change_battery_status_ = true; |
| 27 } | 23 } |
| 28 | 24 |
| 29 const blink::WebBatteryStatus& status() const { return status_; } | 25 const blink::WebBatteryStatus& status() const { return status_; } |
| 30 bool did_change_battery_status() const { return did_change_battery_status_; } | 26 bool did_change_battery_status() const { return did_change_battery_status_; } |
| 31 | 27 |
| 32 private: | 28 private: |
| 33 bool did_change_battery_status_; | 29 bool did_change_battery_status_; |
| 34 blink::WebBatteryStatus status_; | 30 blink::WebBatteryStatus status_; |
| 35 | 31 |
| 36 DISALLOW_COPY_AND_ASSIGN(MockBatteryStatusListener); | 32 DISALLOW_COPY_AND_ASSIGN(MockBatteryStatusListener); |
| 37 }; | 33 }; |
| 38 | 34 |
| 39 class BatteryStatusDispatcherTest : public testing::Test { | 35 class BatteryStatusDispatcherTest : public testing::Test { |
| 36 public: |
| 37 void UpdateBatteryStatus(const device::BatteryStatus& status) { |
| 38 device::BatteryStatusPtr status_ptr(device::BatteryStatus::New()); |
| 39 *status_ptr = status; |
| 40 dispatcher_->DidChange(status_ptr.Pass()); |
| 41 } |
| 42 |
| 43 const MockBatteryStatusListener& listener() const { |
| 44 return listener_; |
| 45 } |
| 46 |
| 47 protected: |
| 48 void SetUp() override { |
| 49 dispatcher_.reset(new BatteryStatusDispatcher(&listener_)); |
| 50 } |
| 51 |
| 40 private: | 52 private: |
| 41 // We need to create a MockRenderThread so RenderThread::Get() doesn't return | 53 // We need to create a MockRenderThread so RenderThread::Get() doesn't return |
| 42 // null. | 54 // null. |
| 43 MockRenderThread render_thread_; | 55 MockRenderThread render_thread_; |
| 56 MockBatteryStatusListener listener_; |
| 57 scoped_ptr<BatteryStatusDispatcher> dispatcher_; |
| 44 }; | 58 }; |
| 45 | 59 |
| 46 TEST_F(BatteryStatusDispatcherTest, UpdateListener) { | 60 TEST_F(BatteryStatusDispatcherTest, UpdateListener) { |
| 47 MockBatteryStatusListener listener; | 61 device::BatteryStatus status; |
| 48 BatteryStatusDispatcher dispatcher(0); | |
| 49 | |
| 50 blink::WebBatteryStatus status; | |
| 51 status.charging = true; | 62 status.charging = true; |
| 52 status.chargingTime = 100; | 63 status.charging_time = 100; |
| 53 status.dischargingTime = 200; | 64 status.discharging_time = 200; |
| 54 status.level = 0.5; | 65 status.level = 0.5; |
| 55 | 66 |
| 56 dispatcher.Start(&listener); | 67 UpdateBatteryStatus(status); |
| 57 | 68 |
| 58 BatteryStatusMsg_DidChange message(status); | 69 const blink::WebBatteryStatus& received_status = listener().status(); |
| 59 dispatcher.OnControlMessageReceived(message); | 70 EXPECT_TRUE(listener().did_change_battery_status()); |
| 60 | |
| 61 const blink::WebBatteryStatus& received_status = listener.status(); | |
| 62 EXPECT_TRUE(listener.did_change_battery_status()); | |
| 63 EXPECT_EQ(status.charging, received_status.charging); | 71 EXPECT_EQ(status.charging, received_status.charging); |
| 64 EXPECT_EQ(status.chargingTime, received_status.chargingTime); | 72 EXPECT_EQ(status.charging_time, received_status.chargingTime); |
| 65 EXPECT_EQ(status.dischargingTime, received_status.dischargingTime); | 73 EXPECT_EQ(status.discharging_time, received_status.dischargingTime); |
| 66 EXPECT_EQ(status.level, received_status.level); | 74 EXPECT_EQ(status.level, received_status.level); |
| 67 | |
| 68 dispatcher.Stop(); | |
| 69 } | |
| 70 | |
| 71 TEST_F(BatteryStatusDispatcherTest, NoUpdateWhenNullListener) { | |
| 72 MockBatteryStatusListener listener; | |
| 73 BatteryStatusDispatcher dispatcher(0); | |
| 74 | |
| 75 dispatcher.Start(0); | |
| 76 dispatcher.Stop(); | |
| 77 | |
| 78 blink::WebBatteryStatus status; | |
| 79 BatteryStatusMsg_DidChange message(status); | |
| 80 dispatcher.OnControlMessageReceived(message); | |
| 81 EXPECT_FALSE(listener.did_change_battery_status()); | |
| 82 } | 75 } |
| 83 | 76 |
| 84 } // namespace content | 77 } // namespace content |
| OLD | NEW |