Reland "Replace Chrome IPC with Mojo IPC for querying BatteryStatus service"
[chromium-blink-merge.git] / device / battery / battery_status_service_unittest.cc
blob2c0bca94c22bb94941bda8944b3ee1e3b8db3f0b
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 virtual ~BatteryStatusServiceTest() { }
56 protected:
57 typedef BatteryStatusService::BatteryUpdateSubscription BatterySubscription;
59 virtual 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 virtual 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 TEST_F(BatteryStatusServiceTest, AddCallbackAfterUpdate) {
143 scoped_ptr<BatterySubscription> subscription1 = AddCallback(callback1());
144 BatteryStatus status;
145 battery_manager()->InvokeUpdateCallback(status);
146 base::RunLoop().RunUntilIdle();
147 EXPECT_EQ(1, callback1_invoked_count());
148 EXPECT_EQ(0, callback2_invoked_count());
150 scoped_ptr<BatterySubscription> subscription2 = AddCallback(callback2());
151 EXPECT_EQ(1, callback1_invoked_count());
152 EXPECT_EQ(1, callback2_invoked_count());
155 TEST_F(BatteryStatusServiceTest, TwoCallbacksUpdate) {
156 scoped_ptr<BatterySubscription> subscription1 = AddCallback(callback1());
157 scoped_ptr<BatterySubscription> subscription2 = AddCallback(callback2());
159 BatteryStatus status;
160 status.charging = true;
161 status.charging_time = 100;
162 status.discharging_time = 200;
163 status.level = 0.5;
164 battery_manager()->InvokeUpdateCallback(status);
165 base::RunLoop().RunUntilIdle();
167 EXPECT_EQ(1, callback1_invoked_count());
168 EXPECT_EQ(1, callback2_invoked_count());
169 EXPECT_EQ(status.charging, battery_status().charging);
170 EXPECT_EQ(status.charging_time, battery_status().charging_time);
171 EXPECT_EQ(status.discharging_time, battery_status().discharging_time);
172 EXPECT_EQ(status.level, battery_status().level);
175 TEST_F(BatteryStatusServiceTest, RemoveOneCallback) {
176 scoped_ptr<BatterySubscription> subscription1 = AddCallback(callback1());
177 scoped_ptr<BatterySubscription> subscription2 = AddCallback(callback2());
179 BatteryStatus status;
180 battery_manager()->InvokeUpdateCallback(status);
181 base::RunLoop().RunUntilIdle();
182 EXPECT_EQ(1, callback1_invoked_count());
183 EXPECT_EQ(1, callback2_invoked_count());
185 subscription1.reset();
186 battery_manager()->InvokeUpdateCallback(status);
187 base::RunLoop().RunUntilIdle();
188 EXPECT_EQ(1, callback1_invoked_count());
189 EXPECT_EQ(2, callback2_invoked_count());
192 } // namespace
194 } // namespace device