OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "battery_status_dispatcher.h" |
| 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/test_utils.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "third_party/WebKit/public/platform/WebBatteryStatusListener.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 class MockBatteryStatusListener : public blink::WebBatteryStatusListener { |
| 17 public: |
| 18 MockBatteryStatusListener() : did_change_battery_status_(false) { } |
| 19 virtual ~MockBatteryStatusListener() { } |
| 20 |
| 21 // blink::WebBatteryStatusListener method. |
| 22 virtual void updateBatteryStatus( |
| 23 const blink::WebBatteryStatus& status) OVERRIDE { |
| 24 status_ = status; |
| 25 did_change_battery_status_ = true; |
| 26 } |
| 27 |
| 28 const blink::WebBatteryStatus& status() const { return status_; } |
| 29 bool did_change_battery_status() const { return did_change_battery_status_; } |
| 30 |
| 31 private: |
| 32 bool did_change_battery_status_; |
| 33 blink::WebBatteryStatus status_; |
| 34 |
| 35 DISALLOW_COPY_AND_ASSIGN(MockBatteryStatusListener); |
| 36 }; |
| 37 |
| 38 class BatteryStatusDispatcherForTesting : public BatteryStatusDispatcher { |
| 39 public: |
| 40 BatteryStatusDispatcherForTesting() |
| 41 : BatteryStatusDispatcher(0), |
| 42 start_invoked_(false), |
| 43 stop_invoked_(false) { } |
| 44 |
| 45 virtual ~BatteryStatusDispatcherForTesting() { } |
| 46 |
| 47 bool start_invoked() const { return start_invoked_; } |
| 48 bool stop_invoked() const { return stop_invoked_; } |
| 49 |
| 50 protected: |
| 51 virtual bool Start() OVERRIDE { |
| 52 start_invoked_ = true; |
| 53 return true; |
| 54 } |
| 55 |
| 56 virtual bool Stop() OVERRIDE { |
| 57 stop_invoked_ = true; |
| 58 return true; |
| 59 } |
| 60 |
| 61 private: |
| 62 bool start_invoked_; |
| 63 bool stop_invoked_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(BatteryStatusDispatcherForTesting); |
| 66 }; |
| 67 |
| 68 TEST(BatteryStatusDispatcherTest, Start) { |
| 69 MockBatteryStatusListener listener; |
| 70 BatteryStatusDispatcherForTesting dispatcher; |
| 71 |
| 72 EXPECT_FALSE(dispatcher.start_invoked()); |
| 73 EXPECT_FALSE(dispatcher.stop_invoked()); |
| 74 |
| 75 dispatcher.SetListener(&listener); |
| 76 EXPECT_TRUE(dispatcher.start_invoked()); |
| 77 |
| 78 dispatcher.SetListener(0); |
| 79 EXPECT_TRUE(dispatcher.stop_invoked()); |
| 80 } |
| 81 |
| 82 TEST(BatteryStatusDispatcherTest, UpdateListener) { |
| 83 MockBatteryStatusListener listener; |
| 84 BatteryStatusDispatcherForTesting dispatcher; |
| 85 |
| 86 blink::WebBatteryStatus status; |
| 87 status.charging = true; |
| 88 status.chargingTime = 100; |
| 89 status.dischargingTime = 200; |
| 90 status.level = 0.5; |
| 91 |
| 92 dispatcher.SetListener(&listener); |
| 93 EXPECT_TRUE(dispatcher.start_invoked()); |
| 94 |
| 95 BatteryStatusMsg_DidChange message(status); |
| 96 dispatcher.OnControlMessageReceived(message); |
| 97 |
| 98 const blink::WebBatteryStatus& received_status = listener.status(); |
| 99 EXPECT_TRUE(listener.did_change_battery_status()); |
| 100 EXPECT_EQ(status.charging, received_status.charging); |
| 101 EXPECT_EQ(status.chargingTime, received_status.chargingTime); |
| 102 EXPECT_EQ(status.dischargingTime, received_status.dischargingTime); |
| 103 EXPECT_EQ(status.level, received_status.level); |
| 104 |
| 105 dispatcher.SetListener(0); |
| 106 EXPECT_TRUE(dispatcher.stop_invoked()); |
| 107 } |
| 108 |
| 109 TEST(BatteryStatusDispatcherTest, NoUpdateWhenNullListener) { |
| 110 MockBatteryStatusListener listener; |
| 111 BatteryStatusDispatcherForTesting dispatcher; |
| 112 |
| 113 dispatcher.SetListener(0); |
| 114 EXPECT_FALSE(dispatcher.start_invoked()); |
| 115 EXPECT_TRUE(dispatcher.stop_invoked()); |
| 116 |
| 117 blink::WebBatteryStatus status; |
| 118 BatteryStatusMsg_DidChange message(status); |
| 119 dispatcher.OnControlMessageReceived(message); |
| 120 EXPECT_FALSE(listener.did_change_battery_status()); |
| 121 } |
| 122 |
| 123 } // namespace content |
OLD | NEW |