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

Side by Side Diff: app/system_monitor_unittest.cc

Issue 6361002: Move SystemMonitor to src/chrome/common.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 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 | « app/system_monitor_posix.cc ('k') | app/system_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
(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 }
OLDNEW
« no previous file with comments | « app/system_monitor_posix.cc ('k') | app/system_monitor_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698