OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <content/browser/background_sync/background_sync_power_observer.h> | |
6 | |
7 #include "base/macros.h" | |
8 #include "base/run_loop.h" | |
9 #include "base/test/power_monitor_test_base.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace content { | |
13 | |
14 namespace { | |
15 | |
16 class BackgroundSyncPowerObserverTest : public testing::Test { | |
17 protected: | |
18 BackgroundSyncPowerObserverTest() : power_changed_count_(0) { | |
19 power_monitor_source_ = new base::PowerMonitorTestSource(); | |
20 power_monitor_.reset(new base::PowerMonitor( | |
21 scoped_ptr<base::PowerMonitorSource>(power_monitor_source_))); | |
22 power_observer_.reset(new BackgroundSyncPowerObserver( | |
23 base::Bind(&BackgroundSyncPowerObserverTest::OnPowerChanged, | |
24 base::Unretained(this)))); | |
25 } | |
26 | |
27 void SetOnBatteryPower(bool on_battery_power) { | |
28 power_monitor_source_->GeneratePowerStateEvent(on_battery_power); | |
29 } | |
30 | |
31 void OnPowerChanged() { power_changed_count_++; } | |
32 | |
33 // power_monitor_source_ is owned by power_monitor_ | |
34 base::PowerMonitorTestSource* power_monitor_source_; | |
35 scoped_ptr<base::PowerMonitor> power_monitor_; | |
36 scoped_ptr<BackgroundSyncPowerObserver> power_observer_; | |
37 int power_changed_count_; | |
38 | |
39 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncPowerObserverTest); | |
40 }; | |
41 | |
42 TEST_F(BackgroundSyncPowerObserverTest, PowerChangeInvokesCallback) { | |
43 SetOnBatteryPower(true); | |
44 power_changed_count_ = 0; | |
45 | |
46 SetOnBatteryPower(false); | |
47 EXPECT_EQ(1, power_changed_count_); | |
48 SetOnBatteryPower(true); | |
49 EXPECT_EQ(2, power_changed_count_); | |
50 SetOnBatteryPower(true); | |
51 EXPECT_EQ(2, power_changed_count_); | |
52 } | |
53 | |
54 TEST_F(BackgroundSyncPowerObserverTest, PowerSufficientAuto) { | |
55 SetOnBatteryPower(false); | |
56 EXPECT_TRUE(power_observer_->PowerSufficient(POWER_STATE_AUTO)); | |
57 | |
58 SetOnBatteryPower(true); | |
59 EXPECT_TRUE(power_observer_->PowerSufficient(POWER_STATE_AUTO)); | |
60 } | |
61 | |
62 TEST_F(BackgroundSyncPowerObserverTest, PowerSufficientAvoidDraining) { | |
63 SetOnBatteryPower(false); | |
64 EXPECT_TRUE(power_observer_->PowerSufficient(POWER_STATE_AVOID_DRAINING)); | |
65 | |
66 SetOnBatteryPower(true); | |
67 EXPECT_FALSE(power_observer_->PowerSufficient(POWER_STATE_AVOID_DRAINING)); | |
68 } | |
69 | |
70 } // namespace | |
71 | |
72 } // namespace content | |
OLD | NEW |