Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / device / battery / battery_status_service_unittest.cc
blobcef64fc66e71081e443d404791a3d0c5167bd6e4
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 } // namespace
48 class BatteryStatusServiceTest : public testing::Test {
49 public:
50 BatteryStatusServiceTest()
51 : battery_manager_(nullptr),
52 callback1_invoked_count_(0),
53 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));
65 // We keep a raw pointer to the FakeBatteryManager, which we expect to
66 // remain valid for the lifetime of the BatteryStatusService.
67 scoped_ptr<FakeBatteryManager> battery_manager(
68 new FakeBatteryManager(battery_service_.GetUpdateCallbackForTesting()));
69 battery_manager_ = battery_manager.get();
71 battery_service_.SetBatteryManagerForTesting(battery_manager.Pass());
74 void TearDown() override {
75 base::RunLoop().RunUntilIdle();
78 FakeBatteryManager* battery_manager() {
79 return battery_manager_;
82 scoped_ptr<BatterySubscription> AddCallback(
83 const BatteryStatusService::BatteryUpdateCallback& callback) {
84 return battery_service_.AddCallback(callback);
87 int callback1_invoked_count() const {
88 return callback1_invoked_count_;
91 int callback2_invoked_count() const {
92 return callback2_invoked_count_;
95 const BatteryStatus& battery_status() const {
96 return battery_status_;
99 const BatteryStatusService::BatteryUpdateCallback& callback1() const {
100 return callback1_;
103 const BatteryStatusService::BatteryUpdateCallback& callback2() const {
104 return callback2_;
107 private:
108 void Callback1(const BatteryStatus& status) {
109 callback1_invoked_count_++;
110 battery_status_ = status;
113 void Callback2(const BatteryStatus& status) {
114 callback2_invoked_count_++;
115 battery_status_ = status;
118 base::MessageLoop message_loop_;
119 BatteryStatusService battery_service_;
120 FakeBatteryManager* battery_manager_;
121 BatteryStatusService::BatteryUpdateCallback callback1_;
122 BatteryStatusService::BatteryUpdateCallback callback2_;
123 int callback1_invoked_count_;
124 int callback2_invoked_count_;
125 BatteryStatus battery_status_;
127 DISALLOW_COPY_AND_ASSIGN(BatteryStatusServiceTest);
130 TEST_F(BatteryStatusServiceTest, AddFirstCallback) {
131 scoped_ptr<BatterySubscription> subscription1 = AddCallback(callback1());
132 EXPECT_EQ(1, battery_manager()->start_invoked_count());
133 EXPECT_EQ(0, battery_manager()->stop_invoked_count());
134 subscription1.reset();
135 EXPECT_EQ(1, battery_manager()->start_invoked_count());
136 EXPECT_EQ(1, battery_manager()->stop_invoked_count());
139 TEST_F(BatteryStatusServiceTest, AddCallbackAfterUpdate) {
140 scoped_ptr<BatterySubscription> subscription1 = AddCallback(callback1());
141 BatteryStatus status;
142 battery_manager()->InvokeUpdateCallback(status);
143 base::RunLoop().RunUntilIdle();
144 EXPECT_EQ(1, callback1_invoked_count());
145 EXPECT_EQ(0, callback2_invoked_count());
147 scoped_ptr<BatterySubscription> subscription2 = AddCallback(callback2());
148 EXPECT_EQ(1, callback1_invoked_count());
149 EXPECT_EQ(1, callback2_invoked_count());
152 TEST_F(BatteryStatusServiceTest, TwoCallbacksUpdate) {
153 scoped_ptr<BatterySubscription> subscription1 = AddCallback(callback1());
154 scoped_ptr<BatterySubscription> subscription2 = AddCallback(callback2());
156 BatteryStatus status;
157 status.charging = true;
158 status.charging_time = 100;
159 status.discharging_time = 200;
160 status.level = 0.5;
161 battery_manager()->InvokeUpdateCallback(status);
162 base::RunLoop().RunUntilIdle();
164 EXPECT_EQ(1, callback1_invoked_count());
165 EXPECT_EQ(1, callback2_invoked_count());
166 EXPECT_EQ(status.charging, battery_status().charging);
167 EXPECT_EQ(status.charging_time, battery_status().charging_time);
168 EXPECT_EQ(status.discharging_time, battery_status().discharging_time);
169 EXPECT_EQ(status.level, battery_status().level);
172 TEST_F(BatteryStatusServiceTest, RemoveOneCallback) {
173 scoped_ptr<BatterySubscription> subscription1 = AddCallback(callback1());
174 scoped_ptr<BatterySubscription> subscription2 = AddCallback(callback2());
176 BatteryStatus status;
177 battery_manager()->InvokeUpdateCallback(status);
178 base::RunLoop().RunUntilIdle();
179 EXPECT_EQ(1, callback1_invoked_count());
180 EXPECT_EQ(1, callback2_invoked_count());
182 subscription1.reset();
183 battery_manager()->InvokeUpdateCallback(status);
184 base::RunLoop().RunUntilIdle();
185 EXPECT_EQ(1, callback1_invoked_count());
186 EXPECT_EQ(2, callback2_invoked_count());
189 } // namespace device