OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 "app/system_monitor.h" | |
6 #include "testing/gtest/include/gtest/gtest.h" | |
7 | |
8 class PowerTest : public SystemMonitor::PowerObserver { | |
9 public: | |
10 PowerTest() | |
11 : battery_(false), | |
12 power_state_changes_(0), | |
13 suspends_(0), | |
14 resumes_(0) { | |
15 } | |
16 | |
17 // PowerObserver callbacks. | |
18 void OnPowerStateChange(bool on_battery_power) { | |
19 power_state_changes_++; | |
20 } | |
21 | |
22 void OnSuspend() { | |
23 suspends_++; | |
24 } | |
25 | |
26 void OnResume() { | |
27 resumes_++; | |
28 } | |
29 | |
30 // Test status counts. | |
31 bool battery() { return battery_; } | |
32 int power_state_changes() { return power_state_changes_; } | |
33 int suspends() { return suspends_; } | |
34 int resumes() { return resumes_; } | |
35 | |
36 private: | |
37 bool battery_; // Do we currently think we're on battery power. | |
38 int power_state_changes_; // Count of OnPowerStateChange notifications. | |
39 int suspends_; // Count of OnSuspend notifications. | |
40 int resumes_; // Count of OnResume notifications. | |
41 }; | |
42 | |
43 TEST(SystemMonitor, PowerNotifications) { | |
44 const int kObservers = 5; | |
45 | |
46 // Initialize a message loop for this to run on. | |
47 MessageLoop loop; | |
48 // Initialize time() since it registers as a SystemMonitor observer. | |
49 base::Time now = base::Time::Now(); | |
50 | |
51 SystemMonitor system_monitor; | |
52 PowerTest test[kObservers]; | |
53 for (int index = 0; index < kObservers; ++index) | |
54 system_monitor.AddObserver(&test[index]); | |
55 | |
56 // Send a bunch of power changes. Since the battery power hasn't | |
57 // actually changed, we shouldn't get notifications. | |
58 for (int index = 0; index < 5; index++) { | |
59 system_monitor.ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | |
60 EXPECT_EQ(test[0].power_state_changes(), 0); | |
61 } | |
62 | |
63 // Sending resume when not suspended should have no effect. | |
64 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | |
65 loop.RunAllPending(); | |
66 EXPECT_EQ(test[0].resumes(), 0); | |
67 | |
68 // Pretend we suspended. | |
69 system_monitor.ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | |
70 loop.RunAllPending(); | |
71 EXPECT_EQ(test[0].suspends(), 1); | |
72 | |
73 // Send a second suspend notification. This should be suppressed. | |
74 system_monitor.ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | |
75 loop.RunAllPending(); | |
76 EXPECT_EQ(test[0].suspends(), 1); | |
77 | |
78 // Pretend we were awakened. | |
79 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | |
80 loop.RunAllPending(); | |
81 EXPECT_EQ(test[0].resumes(), 1); | |
82 | |
83 // Send a duplicate resume notification. This should be suppressed. | |
84 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | |
85 loop.RunAllPending(); | |
86 EXPECT_EQ(test[0].resumes(), 1); | |
87 } | |
OLD | NEW |