OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/power_monitor/power_monitor.h" | 5 #include "base/power_monitor/power_monitor.h" |
6 #include "base/test/power_monitor_test_base.h" | 6 #include "base/test/power_monitor_test_base.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 namespace base { | 9 namespace base { |
10 | 10 |
11 class PowerMonitorTest : public testing::Test { | 11 class PowerMonitorTest : public testing::Test { |
12 protected: | 12 protected: |
13 PowerMonitorTest() { | 13 PowerMonitorTest() { |
14 power_monitor_source_ = new PowerMonitorTestSource(); | 14 power_monitor_source_ = new PowerMonitorTestSource(); |
15 power_monitor_.reset(new PowerMonitor( | 15 PowerMonitor::Initialize( |
16 scoped_ptr<PowerMonitorSource>(power_monitor_source_))); | 16 scoped_ptr<PowerMonitorSource>(power_monitor_source_)); |
17 } | 17 } |
18 virtual ~PowerMonitorTest() {}; | 18 virtual ~PowerMonitorTest() { |
| 19 PowerMonitor::ShutdownForTesting(); |
| 20 }; |
19 | 21 |
20 PowerMonitorTestSource* source() { return power_monitor_source_; } | 22 PowerMonitorTestSource* source() { return power_monitor_source_; } |
21 PowerMonitor* monitor() { return power_monitor_.get(); } | |
22 | 23 |
23 private: | 24 private: |
24 PowerMonitorTestSource* power_monitor_source_; | 25 PowerMonitorTestSource* power_monitor_source_; |
25 scoped_ptr<PowerMonitor> power_monitor_; | |
26 | 26 |
27 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); | 27 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); |
28 }; | 28 }; |
29 | 29 |
30 // PowerMonitorSource is tightly coupled with the PowerMonitor, so this test | 30 // PowerMonitorSource is tightly coupled with the PowerMonitor, so this test |
31 // Will cover both classes | 31 // Will cover both classes |
32 TEST_F(PowerMonitorTest, PowerNotifications) { | 32 TEST_F(PowerMonitorTest, PowerNotifications) { |
33 const int kObservers = 5; | 33 const int kObservers = 5; |
34 | 34 |
35 PowerMonitorTestObserver observers[kObservers]; | 35 PowerMonitorTestObserver observers[kObservers]; |
36 for (int index = 0; index < kObservers; ++index) | 36 for (int index = 0; index < kObservers; ++index) |
37 monitor()->AddObserver(&observers[index]); | 37 PowerMonitor::AddObserver(&observers[index]); |
38 | 38 |
39 // Sending resume when not suspended should have no effect. | 39 // Sending resume when not suspended should have no effect. |
40 source()->GenerateResumeEvent(); | 40 source()->GenerateResumeEvent(); |
41 EXPECT_EQ(observers[0].resumes(), 0); | 41 EXPECT_EQ(observers[0].resumes(), 0); |
42 | 42 |
43 // Pretend we suspended. | 43 // Pretend we suspended. |
44 source()->GenerateSuspendEvent(); | 44 source()->GenerateSuspendEvent(); |
45 // Ensure all observers were notified of the event | 45 // Ensure all observers were notified of the event |
46 for (int index = 0; index < kObservers; ++index) | 46 for (int index = 0; index < kObservers; ++index) |
47 EXPECT_EQ(observers[index].suspends(), 1); | 47 EXPECT_EQ(observers[index].suspends(), 1); |
(...skipping 23 matching lines...) Expand all Loading... |
71 source()->GeneratePowerStateEvent(false); | 71 source()->GeneratePowerStateEvent(false); |
72 EXPECT_EQ(observers[0].power_state_changes(), 2); | 72 EXPECT_EQ(observers[0].power_state_changes(), 2); |
73 EXPECT_EQ(observers[0].last_power_state(), false); | 73 EXPECT_EQ(observers[0].last_power_state(), false); |
74 | 74 |
75 // Repeated indications the device is off battery power should be suppressed. | 75 // Repeated indications the device is off battery power should be suppressed. |
76 source()->GeneratePowerStateEvent(false); | 76 source()->GeneratePowerStateEvent(false); |
77 EXPECT_EQ(observers[0].power_state_changes(), 2); | 77 EXPECT_EQ(observers[0].power_state_changes(), 2); |
78 } | 78 } |
79 | 79 |
80 } // namespace base | 80 } // namespace base |
OLD | NEW |