summaryrefslogtreecommitdiffstats
path: root/base/system_monitor/system_monitor_unittest.cc
blob: 4475dcb3f9bac8f4f78eaf69b6e37d85f5d4fda6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/file_path.h"
#include "base/test/mock_devices_changed_observer.h"
#include "base/system_monitor/system_monitor.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

class PowerTest : public SystemMonitor::PowerObserver {
 public:
  PowerTest()
      : battery_(false),
        power_state_changes_(0),
        suspends_(0),
        resumes_(0) {
  }

  // PowerObserver callbacks.
  virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE {
    power_state_changes_++;
  }

  virtual void OnSuspend() OVERRIDE {
    suspends_++;
  }

  virtual void OnResume() OVERRIDE {
    resumes_++;
  }

  // Test status counts.
  bool battery() { return battery_; }
  int power_state_changes() { return power_state_changes_; }
  int suspends() { return suspends_; }
  int resumes() { return resumes_; }

 private:
  bool battery_;   // Do we currently think we're on battery power.
  int power_state_changes_;  // Count of OnPowerStateChange notifications.
  int suspends_;  // Count of OnSuspend notifications.
  int resumes_;  // Count of OnResume notifications.
};

TEST(SystemMonitor, PowerNotifications) {
  const int kObservers = 5;

  // Initialize a message loop for this to run on.
  MessageLoop loop;

#if defined(OS_MACOSX)
  SystemMonitor::AllocateSystemIOPorts();
#endif

  SystemMonitor system_monitor;
  PowerTest test[kObservers];
  for (int index = 0; index < kObservers; ++index)
    system_monitor.AddPowerObserver(&test[index]);

  // Send a bunch of power changes.  Since the battery power hasn't
  // actually changed, we shouldn't get notifications.
  for (int index = 0; index < 5; index++) {
    system_monitor.ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
    EXPECT_EQ(test[0].power_state_changes(), 0);
  }

  // Sending resume when not suspended should have no effect.
  system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT);
  loop.RunAllPending();
  EXPECT_EQ(test[0].resumes(), 0);

  // Pretend we suspended.
  system_monitor.ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT);
  loop.RunAllPending();
  EXPECT_EQ(test[0].suspends(), 1);

  // Send a second suspend notification.  This should be suppressed.
  system_monitor.ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT);
  loop.RunAllPending();
  EXPECT_EQ(test[0].suspends(), 1);

  // Pretend we were awakened.
  system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT);
  loop.RunAllPending();
  EXPECT_EQ(test[0].resumes(), 1);

  // Send a duplicate resume notification.  This should be suppressed.
  system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT);
  loop.RunAllPending();
  EXPECT_EQ(test[0].resumes(), 1);
}

TEST(SystemMonitor, DeviceChangeNotifications) {
  const int kObservers = 5;

  // Initialize a message loop for this to run on.
  MessageLoop loop;

#if defined(OS_MACOSX)
  SystemMonitor::AllocateSystemIOPorts();
#endif

  testing::Sequence mock_sequencer[kObservers];
  SystemMonitor system_monitor;
  MockDevicesChangedObserver observers[kObservers];
  for (int index = 0; index < kObservers; ++index) {
    system_monitor.AddDevicesChangedObserver(&observers[index]);

    EXPECT_CALL(observers[index], OnDevicesChanged())
        .Times(3)
        .InSequence(mock_sequencer[index]);
    EXPECT_CALL(observers[index], OnMediaDeviceAttached(1, "media device",
                                                        testing::_))
        .InSequence(mock_sequencer[index]);
    EXPECT_CALL(observers[index], OnMediaDeviceDetached(1))
        .InSequence(mock_sequencer[index]);
    EXPECT_CALL(observers[index], OnMediaDeviceDetached(2))
        .InSequence(mock_sequencer[index]);
  }

  system_monitor.ProcessDevicesChanged();
  loop.RunAllPending();

  system_monitor.ProcessDevicesChanged();
  system_monitor.ProcessDevicesChanged();
  loop.RunAllPending();

  system_monitor.ProcessMediaDeviceAttached(
      1, "media device", FilePath(FILE_PATH_LITERAL("path")));
  loop.RunAllPending();

  system_monitor.ProcessMediaDeviceDetached(1);
  system_monitor.ProcessMediaDeviceDetached(2);
  loop.RunAllPending();
}

TEST(SystemMonitor, PowerRequirements) {
#if defined(OS_WIN)
  MessageLoop loop;
  SystemMonitor system_monitor;
  ASSERT_EQ(0, system_monitor.GetPowerRequirementsCountForTest());

  system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "foo");
  ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest());

  system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar");
  ASSERT_EQ(2, system_monitor.GetPowerRequirementsCountForTest());

  // A second identical request should not increase the request count.
  system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar");
  ASSERT_EQ(2, system_monitor.GetPowerRequirementsCountForTest());

  system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "foo");
  ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest());

  // The request count should not decrease until all identical requests end.
  system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar");
  ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest());

  system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar");
  ASSERT_EQ(0, system_monitor.GetPowerRequirementsCountForTest());
#endif  // defined(OS_WIN)
}

}  // namespace base