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

Side by Side Diff: base/system_monitor/system_monitor_unittest.cc

Issue 10959020: SystemMonitor refactoring: move power state monitor into a separate class called PowerMonitor (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Revert using Singleton pattern for PowerMonitor Created 8 years, 1 month 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/system_monitor/system_monitor.h"
6 6
7 #include "base/file_path.h" 7 #include "base/file_path.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/test/mock_devices_changed_observer.h" 9 #include "base/test/mock_devices_changed_observer.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
11 #include "testing/gmock/include/gmock/gmock.h" 11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace base { 14 namespace base {
15 15
16 namespace { 16 namespace {
17 17
18 class PowerTest : public SystemMonitor::PowerObserver {
19 public:
20 PowerTest()
21 : power_state_changes_(0),
22 suspends_(0),
23 resumes_(0) {
24 }
25
26 // PowerObserver callbacks.
27 virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE {
28 power_state_changes_++;
29 }
30
31 virtual void OnSuspend() OVERRIDE {
32 suspends_++;
33 }
34
35 virtual void OnResume() OVERRIDE {
36 resumes_++;
37 }
38
39 // Test status counts.
40 int power_state_changes() { return power_state_changes_; }
41 int suspends() { return suspends_; }
42 int resumes() { return resumes_; }
43
44 private:
45 int power_state_changes_; // Count of OnPowerStateChange notifications.
46 int suspends_; // Count of OnSuspend notifications.
47 int resumes_; // Count of OnResume notifications.
48 };
49
50 class SystemMonitorTest : public testing::Test { 18 class SystemMonitorTest : public testing::Test {
51 protected: 19 protected:
52 SystemMonitorTest() { 20 SystemMonitorTest() {
53 #if defined(OS_MACOSX)
54 // This needs to happen before SystemMonitor's ctor.
55 SystemMonitor::AllocateSystemIOPorts();
56 #endif
57 system_monitor_.reset(new SystemMonitor); 21 system_monitor_.reset(new SystemMonitor);
58 } 22 }
59 virtual ~SystemMonitorTest() {} 23 virtual ~SystemMonitorTest() {}
60 24
61 MessageLoop message_loop_; 25 MessageLoop message_loop_;
62 scoped_ptr<SystemMonitor> system_monitor_; 26 scoped_ptr<SystemMonitor> system_monitor_;
63 27
64 DISALLOW_COPY_AND_ASSIGN(SystemMonitorTest); 28 DISALLOW_COPY_AND_ASSIGN(SystemMonitorTest);
65 }; 29 };
66 30
67 TEST_F(SystemMonitorTest, PowerNotifications) {
68 const int kObservers = 5;
69
70 PowerTest test[kObservers];
71 for (int index = 0; index < kObservers; ++index)
72 system_monitor_->AddPowerObserver(&test[index]);
73
74 // Send a bunch of power changes. Since the battery power hasn't
75 // actually changed, we shouldn't get notifications.
76 for (int index = 0; index < 5; index++) {
77 system_monitor_->ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
78 EXPECT_EQ(test[0].power_state_changes(), 0);
79 }
80
81 // Sending resume when not suspended should have no effect.
82 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT);
83 message_loop_.RunAllPending();
84 EXPECT_EQ(test[0].resumes(), 0);
85
86 // Pretend we suspended.
87 system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT);
88 message_loop_.RunAllPending();
89 EXPECT_EQ(test[0].suspends(), 1);
90
91 // Send a second suspend notification. This should be suppressed.
92 system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT);
93 message_loop_.RunAllPending();
94 EXPECT_EQ(test[0].suspends(), 1);
95
96 // Pretend we were awakened.
97 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT);
98 message_loop_.RunAllPending();
99 EXPECT_EQ(test[0].resumes(), 1);
100
101 // Send a duplicate resume notification. This should be suppressed.
102 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT);
103 message_loop_.RunAllPending();
104 EXPECT_EQ(test[0].resumes(), 1);
105 }
106
107 TEST_F(SystemMonitorTest, DeviceChangeNotifications) { 31 TEST_F(SystemMonitorTest, DeviceChangeNotifications) {
108 const int kObservers = 5; 32 const int kObservers = 5;
109 const string16 kDeviceName = ASCIIToUTF16("media device"); 33 const string16 kDeviceName = ASCIIToUTF16("media device");
110 const std::string kDeviceId1 = "1"; 34 const std::string kDeviceId1 = "1";
111 const std::string kDeviceId2 = "2"; 35 const std::string kDeviceId2 = "2";
112 36
113 testing::Sequence mock_sequencer[kObservers]; 37 testing::Sequence mock_sequencer[kObservers];
114 MockDevicesChangedObserver observers[kObservers]; 38 MockDevicesChangedObserver observers[kObservers];
115 for (int index = 0; index < kObservers; ++index) { 39 for (int index = 0; index < kObservers; ++index) {
116 system_monitor_->AddDevicesChangedObserver(&observers[index]); 40 system_monitor_->AddDevicesChangedObserver(&observers[index]);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 117
194 system_monitor_->ProcessRemovableStorageDetached(kDeviceId2); 118 system_monitor_->ProcessRemovableStorageDetached(kDeviceId2);
195 message_loop_.RunAllPending(); 119 message_loop_.RunAllPending();
196 devices = system_monitor_->GetAttachedRemovableStorage(); 120 devices = system_monitor_->GetAttachedRemovableStorage();
197 EXPECT_EQ(0U, devices.size()); 121 EXPECT_EQ(0U, devices.size());
198 } 122 }
199 123
200 } // namespace 124 } // namespace
201 125
202 } // namespace base 126 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698