1 // Copyright 2015 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 <content/browser/background_sync/background_sync_power_observer.h>
7 #include "base/run_loop.h"
8 #include "base/test/power_monitor_test_base.h"
9 #include "testing/gtest/include/gtest/gtest.h"
15 class BackgroundSyncPowerObserverTest
: public testing::Test
{
17 BackgroundSyncPowerObserverTest() : power_changed_count_(0) {
18 power_monitor_source_
= new base::PowerMonitorTestSource();
19 power_monitor_
.reset(new base::PowerMonitor(
20 scoped_ptr
<base::PowerMonitorSource
>(power_monitor_source_
)));
21 power_observer_
.reset(new BackgroundSyncPowerObserver(
22 base::Bind(&BackgroundSyncPowerObserverTest::OnPowerChanged
,
23 base::Unretained(this))));
26 void SetOnBatteryPower(bool on_battery_power
) {
27 power_monitor_source_
->GeneratePowerStateEvent(on_battery_power
);
30 void OnPowerChanged() { power_changed_count_
++; }
32 // power_monitor_source_ is owned by power_monitor_
33 base::PowerMonitorTestSource
* power_monitor_source_
;
34 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
35 scoped_ptr
<BackgroundSyncPowerObserver
> power_observer_
;
36 int power_changed_count_
;
38 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncPowerObserverTest
);
41 TEST_F(BackgroundSyncPowerObserverTest
, PowerChangeInvokesCallback
) {
42 SetOnBatteryPower(true);
43 power_changed_count_
= 0;
45 SetOnBatteryPower(false);
46 EXPECT_EQ(1, power_changed_count_
);
47 SetOnBatteryPower(true);
48 EXPECT_EQ(2, power_changed_count_
);
49 SetOnBatteryPower(true);
50 EXPECT_EQ(2, power_changed_count_
);
53 TEST_F(BackgroundSyncPowerObserverTest
, PowerSufficientAuto
) {
54 SetOnBatteryPower(false);
55 EXPECT_TRUE(power_observer_
->PowerSufficient(POWER_STATE_AUTO
));
57 SetOnBatteryPower(true);
58 EXPECT_TRUE(power_observer_
->PowerSufficient(POWER_STATE_AUTO
));
61 TEST_F(BackgroundSyncPowerObserverTest
, PowerSufficientAvoidDraining
) {
62 SetOnBatteryPower(false);
63 EXPECT_TRUE(power_observer_
->PowerSufficient(POWER_STATE_AVOID_DRAINING
));
65 SetOnBatteryPower(true);
66 EXPECT_FALSE(power_observer_
->PowerSufficient(POWER_STATE_AVOID_DRAINING
));
71 } // namespace content