OLD | NEW |
1 // Copyright (c) 2012 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/system_monitor/system_monitor.h" | 5 #include "base/power_monitor/power_monitor.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/run_loop.h" | |
9 #include "base/test/mock_devices_changed_observer.h" | |
10 #include "testing/gmock/include/gmock/gmock.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
12 | 9 |
13 namespace base { | 10 namespace base { |
14 | 11 |
15 namespace { | 12 class PowerTest : public PowerObserver { |
16 | |
17 class PowerTest : public SystemMonitor::PowerObserver { | |
18 public: | 13 public: |
19 PowerTest() | 14 PowerTest() |
20 : power_state_changes_(0), | 15 : power_state_changes_(0), |
21 suspends_(0), | 16 suspends_(0), |
22 resumes_(0) { | 17 resumes_(0) { |
23 } | 18 } |
24 | 19 |
25 // PowerObserver callbacks. | 20 // PowerObserver callbacks. |
26 virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE { | 21 virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE { |
27 power_state_changes_++; | 22 power_state_changes_++; |
(...skipping 11 matching lines...) Expand all Loading... |
39 int power_state_changes() { return power_state_changes_; } | 34 int power_state_changes() { return power_state_changes_; } |
40 int suspends() { return suspends_; } | 35 int suspends() { return suspends_; } |
41 int resumes() { return resumes_; } | 36 int resumes() { return resumes_; } |
42 | 37 |
43 private: | 38 private: |
44 int power_state_changes_; // Count of OnPowerStateChange notifications. | 39 int power_state_changes_; // Count of OnPowerStateChange notifications. |
45 int suspends_; // Count of OnSuspend notifications. | 40 int suspends_; // Count of OnSuspend notifications. |
46 int resumes_; // Count of OnResume notifications. | 41 int resumes_; // Count of OnResume notifications. |
47 }; | 42 }; |
48 | 43 |
49 class SystemMonitorTest : public testing::Test { | 44 class PowerMonitorTest : public testing::Test { |
50 protected: | 45 protected: |
51 SystemMonitorTest() { | 46 PowerMonitorTest() { |
52 #if defined(OS_MACOSX) | 47 #if defined(OS_MACOSX) |
53 // This needs to happen before SystemMonitor's ctor. | 48 // This needs to happen before PowerMonitor's ctor. |
54 SystemMonitor::AllocateSystemIOPorts(); | 49 PowerMonitor::AllocateSystemIOPorts(); |
55 #endif | 50 #endif |
56 system_monitor_.reset(new SystemMonitor); | 51 power_monitor_.reset(new PowerMonitor); |
57 } | 52 } |
58 virtual ~SystemMonitorTest() {} | 53 |
| 54 void ProcessPowerEvent(PowerMonitor::PowerEvent event_id) { |
| 55 power_monitor_->ProcessPowerEvent(event_id); |
| 56 } |
| 57 |
| 58 virtual ~PowerMonitorTest() {} |
59 | 59 |
60 MessageLoop message_loop_; | 60 MessageLoop message_loop_; |
61 scoped_ptr<SystemMonitor> system_monitor_; | 61 scoped_ptr<PowerMonitor> power_monitor_; |
62 | 62 |
63 DISALLOW_COPY_AND_ASSIGN(SystemMonitorTest); | 63 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); |
64 }; | 64 }; |
65 | 65 |
66 TEST_F(SystemMonitorTest, PowerNotifications) { | 66 TEST_F(PowerMonitorTest, PowerNotifications) { |
67 const int kObservers = 5; | 67 const int kObservers = 5; |
68 | 68 |
69 PowerTest test[kObservers]; | 69 PowerTest test[kObservers]; |
70 for (int index = 0; index < kObservers; ++index) | 70 for (int index = 0; index < kObservers; ++index) |
71 system_monitor_->AddPowerObserver(&test[index]); | 71 power_monitor_->AddObserver(&test[index]); |
72 | 72 |
73 // Send a bunch of power changes. Since the battery power hasn't | 73 // Send a bunch of power changes. Since the battery power hasn't |
74 // actually changed, we shouldn't get notifications. | 74 // actually changed, we shouldn't get notifications. |
75 for (int index = 0; index < 5; index++) { | 75 for (int index = 0; index < 5; index++) { |
76 system_monitor_->ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | 76 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); |
77 EXPECT_EQ(test[0].power_state_changes(), 0); | 77 EXPECT_EQ(test[0].power_state_changes(), 0); |
78 } | 78 } |
79 | 79 |
80 // Sending resume when not suspended should have no effect. | 80 // Sending resume when not suspended should have no effect. |
81 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 81 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); |
82 RunLoop().RunUntilIdle(); | 82 message_loop_.RunUntilIdle(); |
83 EXPECT_EQ(test[0].resumes(), 0); | 83 EXPECT_EQ(test[0].resumes(), 0); |
84 | 84 |
85 // Pretend we suspended. | 85 // Pretend we suspended. |
86 system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | 86 ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); |
87 RunLoop().RunUntilIdle(); | 87 message_loop_.RunUntilIdle(); |
88 EXPECT_EQ(test[0].suspends(), 1); | 88 EXPECT_EQ(test[0].suspends(), 1); |
89 | 89 |
90 // Send a second suspend notification. This should be suppressed. | 90 // Send a second suspend notification. This should be suppressed. |
91 system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | 91 ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); |
92 RunLoop().RunUntilIdle(); | 92 message_loop_.RunUntilIdle(); |
93 EXPECT_EQ(test[0].suspends(), 1); | 93 EXPECT_EQ(test[0].suspends(), 1); |
94 | 94 |
95 // Pretend we were awakened. | 95 // Pretend we were awakened. |
96 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 96 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); |
97 RunLoop().RunUntilIdle(); | 97 message_loop_.RunUntilIdle(); |
98 EXPECT_EQ(test[0].resumes(), 1); | 98 EXPECT_EQ(test[0].resumes(), 1); |
99 | 99 |
100 // Send a duplicate resume notification. This should be suppressed. | 100 // Send a duplicate resume notification. This should be suppressed. |
101 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 101 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); |
102 RunLoop().RunUntilIdle(); | 102 message_loop_.RunUntilIdle(); |
103 EXPECT_EQ(test[0].resumes(), 1); | 103 EXPECT_EQ(test[0].resumes(), 1); |
104 } | 104 } |
105 | 105 |
106 TEST_F(SystemMonitorTest, DeviceChangeNotifications) { | |
107 const int kObservers = 5; | |
108 | |
109 testing::Sequence mock_sequencer[kObservers]; | |
110 MockDevicesChangedObserver observers[kObservers]; | |
111 for (int index = 0; index < kObservers; ++index) { | |
112 system_monitor_->AddDevicesChangedObserver(&observers[index]); | |
113 | |
114 EXPECT_CALL(observers[index], | |
115 OnDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN)) | |
116 .Times(3) | |
117 .InSequence(mock_sequencer[index]); | |
118 } | |
119 | |
120 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); | |
121 RunLoop().RunUntilIdle(); | |
122 | |
123 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); | |
124 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); | |
125 RunLoop().RunUntilIdle(); | |
126 } | |
127 | |
128 } // namespace | |
129 | |
130 } // namespace base | 106 } // namespace base |
OLD | NEW |