Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: base/power_monitor/power_monitor_unittest.cc

Issue 17074009: Created multi-process-friendly PowerMonitor interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing windows unit test errors Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/power_monitor/power_monitor_test_base.cc ('k') | base/power_monitor/power_monitor_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 6 #include "base/power_monitor/power_monitor_test_base.h"
7 #include "base/message_loop/message_loop.h"
8 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
9 8
10 namespace base { 9 namespace base {
11 10
12 class PowerTest : public PowerObserver {
13 public:
14 PowerTest()
15 : power_state_changes_(0),
16 suspends_(0),
17 resumes_(0) {
18 }
19
20 // PowerObserver callbacks.
21 virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE {
22 power_state_changes_++;
23 }
24
25 virtual void OnSuspend() OVERRIDE {
26 suspends_++;
27 }
28
29 virtual void OnResume() OVERRIDE {
30 resumes_++;
31 }
32
33 // Test status counts.
34 int power_state_changes() { return power_state_changes_; }
35 int suspends() { return suspends_; }
36 int resumes() { return resumes_; }
37
38 private:
39 int power_state_changes_; // Count of OnPowerStateChange notifications.
40 int suspends_; // Count of OnSuspend notifications.
41 int resumes_; // Count of OnResume notifications.
42 };
43
44 class PowerMonitorTest : public testing::Test { 11 class PowerMonitorTest : public testing::Test {
45 protected: 12 protected:
46 PowerMonitorTest() { 13 PowerMonitorTest() {
47 #if defined(OS_MACOSX) 14 power_monitor_source_ = new PowerMonitorTestSource();
48 // This needs to happen before PowerMonitor's ctor. 15 power_monitor_.reset(new PowerMonitor(
49 PowerMonitor::AllocateSystemIOPorts(); 16 scoped_ptr<PowerMonitorSource>(power_monitor_source_)));
50 #endif
51 power_monitor_.reset(new PowerMonitor);
52 } 17 }
18 virtual ~PowerMonitorTest() {};
53 19
54 void ProcessPowerEvent(PowerMonitor::PowerEvent event_id) { 20 PowerMonitorTestSource* source() { return power_monitor_source_; }
55 power_monitor_->ProcessPowerEvent(event_id); 21 PowerMonitor* monitor() { return power_monitor_.get(); }
56 }
57 22
58 virtual ~PowerMonitorTest() {} 23 private:
59 24 PowerMonitorTestSource* power_monitor_source_;
60 MessageLoop message_loop_;
61 scoped_ptr<PowerMonitor> power_monitor_; 25 scoped_ptr<PowerMonitor> power_monitor_;
62 26
63 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); 27 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest);
64 }; 28 };
65 29
30 // PowerMonitorSource is tightly coupled with the PowerMonitor, so this test
31 // Will cover both classes
66 TEST_F(PowerMonitorTest, PowerNotifications) { 32 TEST_F(PowerMonitorTest, PowerNotifications) {
67 const int kObservers = 5; 33 const int kObservers = 5;
68 34
69 PowerTest test[kObservers]; 35 PowerMonitorTestObserver observers[kObservers];
70 for (int index = 0; index < kObservers; ++index) 36 for (int index = 0; index < kObservers; ++index)
71 power_monitor_->AddObserver(&test[index]); 37 monitor()->AddObserver(&observers[index]);
72
73 // Send a bunch of power changes. Since the battery power hasn't
74 // actually changed, we shouldn't get notifications.
75 for (int index = 0; index < 5; index++) {
76 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT);
77 EXPECT_EQ(test[0].power_state_changes(), 0);
78 }
79 38
80 // Sending resume when not suspended should have no effect. 39 // Sending resume when not suspended should have no effect.
81 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); 40 source()->GenerateResumeEvent();
82 message_loop_.RunUntilIdle(); 41 EXPECT_EQ(observers[0].resumes(), 0);
83 EXPECT_EQ(test[0].resumes(), 0);
84 42
85 // Pretend we suspended. 43 // Pretend we suspended.
86 ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); 44 source()->GenerateSuspendEvent();
87 message_loop_.RunUntilIdle(); 45 // Ensure all observers were notified of the event
88 EXPECT_EQ(test[0].suspends(), 1); 46 for (int index = 0; index < kObservers; ++index)
47 EXPECT_EQ(observers[index].suspends(), 1);
89 48
90 // Send a second suspend notification. This should be suppressed. 49 // Send a second suspend notification. This should be suppressed.
91 ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); 50 source()->GenerateSuspendEvent();
92 message_loop_.RunUntilIdle(); 51 EXPECT_EQ(observers[0].suspends(), 1);
93 EXPECT_EQ(test[0].suspends(), 1);
94 52
95 // Pretend we were awakened. 53 // Pretend we were awakened.
96 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); 54 source()->GenerateResumeEvent();
97 message_loop_.RunUntilIdle(); 55 EXPECT_EQ(observers[0].resumes(), 1);
98 EXPECT_EQ(test[0].resumes(), 1);
99 56
100 // Send a duplicate resume notification. This should be suppressed. 57 // Send a duplicate resume notification. This should be suppressed.
101 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); 58 source()->GenerateResumeEvent();
102 message_loop_.RunUntilIdle(); 59 EXPECT_EQ(observers[0].resumes(), 1);
103 EXPECT_EQ(test[0].resumes(), 1); 60
61 // Pretend the device has gone on battery power
62 source()->GeneratePowerStateEvent(true);
63 EXPECT_EQ(observers[0].power_state_changes(), 1);
64 EXPECT_EQ(observers[0].last_power_state(), true);
65
66 // Repeated indications the device is on battery power should be suppressed.
67 source()->GeneratePowerStateEvent(true);
68 EXPECT_EQ(observers[0].power_state_changes(), 1);
69
70 // Pretend the device has gone off battery power
71 source()->GeneratePowerStateEvent(false);
72 EXPECT_EQ(observers[0].power_state_changes(), 2);
73 EXPECT_EQ(observers[0].last_power_state(), false);
74
75 // Repeated indications the device is off battery power should be suppressed.
76 source()->GeneratePowerStateEvent(false);
77 EXPECT_EQ(observers[0].power_state_changes(), 2);
104 } 78 }
105 79
106 } // namespace base 80 } // namespace base
OLDNEW
« no previous file with comments | « base/power_monitor/power_monitor_test_base.cc ('k') | base/power_monitor/power_monitor_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698