Chromium Code Reviews| Index: content/renderer/battery_status/battery_status_dispatcher_unittest.cc |
| diff --git a/content/renderer/battery_status/battery_status_dispatcher_unittest.cc b/content/renderer/battery_status/battery_status_dispatcher_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4cae259a786294ddc3ab1487618605807be9e459 |
| --- /dev/null |
| +++ b/content/renderer/battery_status/battery_status_dispatcher_unittest.cc |
| @@ -0,0 +1,112 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "battery_status_dispatcher.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "content/common/battery_status_messages.h" |
| +#include "content/public/test/test_utils.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/WebKit/public/platform/WebBatteryStatusListener.h" |
| + |
| +namespace content { |
| + |
| +class MockBatteryStatusListener : public blink::WebBatteryStatusListener { |
| + public: |
| + MockBatteryStatusListener() : did_change_battery_status_(false) { } |
| + virtual ~MockBatteryStatusListener() { } |
| + |
| + virtual void updateBatteryStatus( |
| + const blink::WebBatteryStatus& status) OVERRIDE { |
| + status_ = status; |
| + did_change_battery_status_ = true; |
| + } |
| + |
| + bool did_change_battery_status_; |
| + 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.
|
| +}; |
| + |
| +class BatteryStatusDispatcherForTesting : public BatteryStatusDispatcher { |
| + public: |
| + BatteryStatusDispatcherForTesting() |
| + : start_invoked_(false), |
| + stop_invoked_(false) { } |
| + |
| + virtual ~BatteryStatusDispatcherForTesting() { } |
| + |
| + virtual bool Start() OVERRIDE { |
| + start_invoked_ = true; |
| + return true; |
| + } |
| + |
| + virtual bool Stop() OVERRIDE { |
| + stop_invoked_ = true; |
| + return true; |
| + } |
| + |
| + bool start_invoked_; |
|
jochen (gone - plz use gerrit)
2014/04/30 04:54:07
same here
timvolodine
2014/05/01 10:32:39
Done.
|
| + bool stop_invoked_; |
| +}; |
| + |
| +class BatteryStatusDispatcherTest : public testing::Test { |
| + public: |
| + BatteryStatusDispatcherTest() { } |
| + |
| + protected: |
| + virtual void SetUp() OVERRIDE { |
| + listener_.reset(new MockBatteryStatusListener); |
| + dispatcher_.reset(new BatteryStatusDispatcherForTesting); |
| + } |
| + |
| + scoped_ptr<MockBatteryStatusListener> listener_; |
| + 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.
|
| +}; |
| + |
|
jochen (gone - plz use gerrit)
2014/04/30 04:54:07
nit. only one empty line
timvolodine
2014/05/01 10:32:39
Done.
|
| + |
| +TEST_F(BatteryStatusDispatcherTest, Start) { |
| + EXPECT_FALSE(dispatcher_->start_invoked_); |
| + EXPECT_FALSE(dispatcher_->stop_invoked_); |
| + |
| + dispatcher_->SetListener(listener_.get()); |
| + EXPECT_TRUE(dispatcher_->start_invoked_); |
| + |
| + dispatcher_->SetListener(0); |
| + EXPECT_TRUE(dispatcher_->stop_invoked_); |
| +} |
| + |
| +TEST_F(BatteryStatusDispatcherTest, UpdateListener) { |
| + blink::WebBatteryStatus status; |
| + status.charging = true; |
| + status.chargingTime = 100; |
| + status.dischargingTime = 200; |
| + status.level = 0.5; |
| + |
| + dispatcher_->SetListener(listener_.get()); |
| + EXPECT_TRUE(dispatcher_->start_invoked_); |
| + |
| + BatteryStatusMsg_DidChange message(status); |
| + dispatcher_->OnControlMessageReceived(message); |
| + EXPECT_TRUE(listener_->did_change_battery_status_); |
| + EXPECT_EQ(status.charging, listener_->status_.charging); |
| + EXPECT_EQ(status.chargingTime, listener_->status_.chargingTime); |
| + EXPECT_EQ(status.dischargingTime, listener_->status_.dischargingTime); |
| + EXPECT_EQ(status.level, listener_->status_.level); |
| + |
| + dispatcher_->SetListener(0); |
| + EXPECT_TRUE(dispatcher_->stop_invoked_); |
| +} |
| + |
| +TEST_F(BatteryStatusDispatcherTest, NoUpdateWhenNullListener) { |
| + blink::WebBatteryStatus status; |
| + dispatcher_->SetListener(0); |
| + EXPECT_FALSE(dispatcher_->start_invoked_); |
| + EXPECT_TRUE(dispatcher_->stop_invoked_); |
| + |
| + BatteryStatusMsg_DidChange message(status); |
| + dispatcher_->OnControlMessageReceived(message); |
| + EXPECT_FALSE(listener_->did_change_battery_status_); |
| +} |
| + |
| +} // namespace content |