Roll leveldb 3f7758:803d69 (v1.17 -> v1.18)
[chromium-blink-merge.git] / device / battery / battery_status_service_unittest.cc
blobd249d570ef1c6ad4c53652c5b2a70770bba708f0
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 "base/bind.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h"
8 #include "device/battery/battery_status_manager.h"
9 #include "device/battery/battery_status_service.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace device {
14 namespace {
16 class FakeBatteryManager : public BatteryStatusManager {
17 public:
18 explicit FakeBatteryManager(
19 const BatteryStatusService::BatteryUpdateCallback& callback)
20 : callback_(callback), start_invoked_count_(0), stop_invoked_count_(0) {}
21 ~FakeBatteryManager() override {}
23 // Methods from Battery Status Manager
24 bool StartListeningBatteryChange() override {
25 start_invoked_count_++;
26 return true;
29 void StopListeningBatteryChange() override { stop_invoked_count_++; }
31 void InvokeUpdateCallback(const BatteryStatus& status) {
32 callback_.Run(status);
35 int start_invoked_count() const { return start_invoked_count_; }
36 int stop_invoked_count() const { return stop_invoked_count_; }
38 private:
39 BatteryStatusService::BatteryUpdateCallback callback_;
40 int start_invoked_count_;
41 int stop_invoked_count_;
43 DISALLOW_COPY_AND_ASSIGN(FakeBatteryManager);
46 class BatteryStatusServiceTest : public testing::Test {
47 public:
48 BatteryStatusServiceTest()
49 : battery_service_(NULL),
50 battery_manager_(NULL),
51 callback1_invoked_count_(0),
52 callback2_invoked_count_(0) {
54 ~BatteryStatusServiceTest() override {}
56 protected:
57 typedef BatteryStatusService::BatteryUpdateSubscription BatterySubscription;
59 void SetUp() override {
60 callback1_ = base::Bind(&BatteryStatusServiceTest::Callback1,
61 base::Unretained(this));
62 callback2_ = base::Bind(&BatteryStatusServiceTest::Callback2,
63 base::Unretained(this));
64 battery_service_ = BatteryStatusService::GetInstance();
66 // We keep a raw pointer to the FakeBatteryManager, which we expect to
67 // remain valid for the lifetime of the BatteryStatusService.
68 scoped_ptr<FakeBatteryManager> battery_manager(new FakeBatteryManager(
69 battery_service_->GetUpdateCallbackForTesting()));
70 battery_manager_ = battery_manager.get();
72 battery_service_->SetBatteryManagerForTesting(battery_manager.Pass());
75 void TearDown() override {
76 base::RunLoop().RunUntilIdle();
77 battery_service_->SetBatteryManagerForTesting(
78 scoped_ptr<BatteryStatusManager>());
81 FakeBatteryManager* battery_manager() {
82 return battery_manager_;
85 scoped_ptr<BatterySubscription> AddCallback(
86 const BatteryStatusService::BatteryUpdateCallback& callback) {
87 return battery_service_->AddCallback(callback);
90 int callback1_invoked_count() const {
91 return callback1_invoked_count_;
94 int callback2_invoked_count() const {
95 return callback2_invoked_count_;
98 const BatteryStatus& battery_status() const {
99 return battery_status_;
102 const BatteryStatusService::BatteryUpdateCallback& callback1() const {
103 return callback1_;
106 const BatteryStatusService::BatteryUpdateCallback& callback2() const {
107 return callback2_;
110 private:
111 void Callback1(const BatteryStatus& status) {
112 callback1_invoked_count_++;
113 battery_status_ = status;
116 void Callback2(const BatteryStatus& status) {
117 callback2_invoked_count_++;
118 battery_status_ = status;
121 base::MessageLoop message_loop_;
122 BatteryStatusService* battery_service_;
123 FakeBatteryManager* battery_manager_;
124 BatteryStatusService::BatteryUpdateCallback callback1_;
125 BatteryStatusService::BatteryUpdateCallback callback2_;
126 int callback1_invoked_count_;
127 int callback2_invoked_count_;
128 BatteryStatus battery_status_;
130 DISALLOW_COPY_AND_ASSIGN(BatteryStatusServiceTest);
133 TEST_F(BatteryStatusServiceTest, AddFirstCallback) {
134 scoped_ptr<BatterySubscription> subscription1 = AddCallback(callback1());
135 EXPECT_EQ(1, battery_manager()->start_invoked_count());
136 EXPECT_EQ(0, battery_manager()->stop_invoked_count());
137 subscription1.reset();
138 EXPECT_EQ(1, battery_manager()->start_invoked_count());
139 EXPECT_EQ(1, battery_manager()->stop_invoked_count());
142 // Fails on Windows. http://crbug.com/429942.
143 #if defined(OS_WIN)
144 #define MAYBE_AddCallbackAfterUpdate DISABLED_AddCallbackAfterUpdate
145 #else
146 #define MAYBE_AddCallbackAfterUpdate AddCallbackAfterUpdate
147 #endif
148 TEST_F(BatteryStatusServiceTest, MAYBE_AddCallbackAfterUpdate) {
149 scoped_ptr<BatterySubscription> subscription1 = AddCallback(callback1());
150 BatteryStatus status;
151 battery_manager()->InvokeUpdateCallback(status);
152 base::RunLoop().RunUntilIdle();
153 EXPECT_EQ(1, callback1_invoked_count());
154 EXPECT_EQ(0, callback2_invoked_count());
156 scoped_ptr<BatterySubscription> subscription2 = AddCallback(callback2());
157 EXPECT_EQ(1, callback1_invoked_count());
158 EXPECT_EQ(1, callback2_invoked_count());
161 // Fails on Windows. http://crbug.com/429942.
162 #if defined(OS_WIN)
163 #define MAYBE_TwoCallbacksUpdate DISABLED_TwoCallbacksUpdate
164 #else
165 #define MAYBE_TwoCallbacksUpdate TwoCallbacksUpdate
166 #endif
167 TEST_F(BatteryStatusServiceTest, MAYBE_TwoCallbacksUpdate) {
168 scoped_ptr<BatterySubscription> subscription1 = AddCallback(callback1());
169 scoped_ptr<BatterySubscription> subscription2 = AddCallback(callback2());
171 BatteryStatus status;
172 status.charging = true;
173 status.charging_time = 100;
174 status.discharging_time = 200;
175 status.level = 0.5;
176 battery_manager()->InvokeUpdateCallback(status);
177 base::RunLoop().RunUntilIdle();
179 EXPECT_EQ(1, callback1_invoked_count());
180 EXPECT_EQ(1, callback2_invoked_count());
181 EXPECT_EQ(status.charging, battery_status().charging);
182 EXPECT_EQ(status.charging_time, battery_status().charging_time);
183 EXPECT_EQ(status.discharging_time, battery_status().discharging_time);
184 EXPECT_EQ(status.level, battery_status().level);
187 // Fails on Windows. http://crbug.com/429942.
188 #if defined(OS_WIN)
189 #define MAYBE_RemoveOneCallback DISABLED_RemoveOneCallback
190 #else
191 #define MAYBE_RemoveOneCallback RemoveOneCallback
192 #endif
193 TEST_F(BatteryStatusServiceTest, MAYBE_RemoveOneCallback) {
194 scoped_ptr<BatterySubscription> subscription1 = AddCallback(callback1());
195 scoped_ptr<BatterySubscription> subscription2 = AddCallback(callback2());
197 BatteryStatus status;
198 battery_manager()->InvokeUpdateCallback(status);
199 base::RunLoop().RunUntilIdle();
200 EXPECT_EQ(1, callback1_invoked_count());
201 EXPECT_EQ(1, callback2_invoked_count());
203 subscription1.reset();
204 battery_manager()->InvokeUpdateCallback(status);
205 base::RunLoop().RunUntilIdle();
206 EXPECT_EQ(1, callback1_invoked_count());
207 EXPECT_EQ(2, callback2_invoked_count());
210 } // namespace
212 } // namespace device