Chromium Code Reviews| 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 virtual void updateBatteryStatus( | |
| 22 const blink::WebBatteryStatus& status) OVERRIDE { | |
| 23 status_ = status; | |
| 24 did_change_battery_status_ = true; | |
| 25 } | |
| 26 | |
| 27 bool did_change_battery_status_; | |
| 28 blink::WebBatteryStatus status_; | |
|
jochen (gone - plz use gerrit)
2014/04/30 04:54:07
members should be private and use disallow copy/as
timvolodine
2014/05/01 10:32:39
Done.
| |
| 29 }; | |
| 30 | |
| 31 class BatteryStatusDispatcherForTesting : public BatteryStatusDispatcher { | |
| 32 public: | |
| 33 BatteryStatusDispatcherForTesting() | |
| 34 : start_invoked_(false), | |
| 35 stop_invoked_(false) { } | |
| 36 | |
| 37 virtual ~BatteryStatusDispatcherForTesting() { } | |
| 38 | |
| 39 virtual bool Start() OVERRIDE { | |
| 40 start_invoked_ = true; | |
| 41 return true; | |
| 42 } | |
| 43 | |
| 44 virtual bool Stop() OVERRIDE { | |
| 45 stop_invoked_ = true; | |
| 46 return true; | |
| 47 } | |
| 48 | |
| 49 bool start_invoked_; | |
|
jochen (gone - plz use gerrit)
2014/04/30 04:54:07
same here
timvolodine
2014/05/01 10:32:39
Done.
| |
| 50 bool stop_invoked_; | |
| 51 }; | |
| 52 | |
| 53 class BatteryStatusDispatcherTest : public testing::Test { | |
| 54 public: | |
| 55 BatteryStatusDispatcherTest() { } | |
| 56 | |
| 57 protected: | |
| 58 virtual void SetUp() OVERRIDE { | |
| 59 listener_.reset(new MockBatteryStatusListener); | |
| 60 dispatcher_.reset(new BatteryStatusDispatcherForTesting); | |
| 61 } | |
| 62 | |
| 63 scoped_ptr<MockBatteryStatusListener> listener_; | |
| 64 scoped_ptr<BatteryStatusDispatcherForTesting> dispatcher_; | |
|
jochen (gone - plz use gerrit)
2014/04/30 04:54:07
and here
timvolodine
2014/05/01 10:32:39
Done.
| |
| 65 }; | |
| 66 | |
|
jochen (gone - plz use gerrit)
2014/04/30 04:54:07
nit. only one empty line
timvolodine
2014/05/01 10:32:39
Done.
| |
| 67 | |
| 68 TEST_F(BatteryStatusDispatcherTest, Start) { | |
| 69 EXPECT_FALSE(dispatcher_->start_invoked_); | |
| 70 EXPECT_FALSE(dispatcher_->stop_invoked_); | |
| 71 | |
| 72 dispatcher_->SetListener(listener_.get()); | |
| 73 EXPECT_TRUE(dispatcher_->start_invoked_); | |
| 74 | |
| 75 dispatcher_->SetListener(0); | |
| 76 EXPECT_TRUE(dispatcher_->stop_invoked_); | |
| 77 } | |
| 78 | |
| 79 TEST_F(BatteryStatusDispatcherTest, UpdateListener) { | |
| 80 blink::WebBatteryStatus status; | |
| 81 status.charging = true; | |
| 82 status.chargingTime = 100; | |
| 83 status.dischargingTime = 200; | |
| 84 status.level = 0.5; | |
| 85 | |
| 86 dispatcher_->SetListener(listener_.get()); | |
| 87 EXPECT_TRUE(dispatcher_->start_invoked_); | |
| 88 | |
| 89 BatteryStatusMsg_DidChange message(status); | |
| 90 dispatcher_->OnControlMessageReceived(message); | |
| 91 EXPECT_TRUE(listener_->did_change_battery_status_); | |
| 92 EXPECT_EQ(status.charging, listener_->status_.charging); | |
| 93 EXPECT_EQ(status.chargingTime, listener_->status_.chargingTime); | |
| 94 EXPECT_EQ(status.dischargingTime, listener_->status_.dischargingTime); | |
| 95 EXPECT_EQ(status.level, listener_->status_.level); | |
| 96 | |
| 97 dispatcher_->SetListener(0); | |
| 98 EXPECT_TRUE(dispatcher_->stop_invoked_); | |
| 99 } | |
| 100 | |
| 101 TEST_F(BatteryStatusDispatcherTest, NoUpdateWhenNullListener) { | |
| 102 blink::WebBatteryStatus status; | |
| 103 dispatcher_->SetListener(0); | |
| 104 EXPECT_FALSE(dispatcher_->start_invoked_); | |
| 105 EXPECT_TRUE(dispatcher_->stop_invoked_); | |
| 106 | |
| 107 BatteryStatusMsg_DidChange message(status); | |
| 108 dispatcher_->OnControlMessageReceived(message); | |
| 109 EXPECT_FALSE(listener_->did_change_battery_status_); | |
| 110 } | |
| 111 | |
| 112 } // namespace content | |
| OLD | NEW |