summaryrefslogtreecommitdiffstats
path: root/base/power_monitor/power_monitor_unittest.cc
blob: 90f36f0736235800e483cfd61a50ea6f68fe40f4 (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
// Copyright 2013 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/power_monitor/power_monitor.h"

#include "base/message_loop/message_loop.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

class PowerTest : public PowerObserver {
 public:
  PowerTest()
      : 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.
  int power_state_changes() { return power_state_changes_; }
  int suspends() { return suspends_; }
  int resumes() { return resumes_; }

 private:
  int power_state_changes_;  // Count of OnPowerStateChange notifications.
  int suspends_;  // Count of OnSuspend notifications.
  int resumes_;  // Count of OnResume notifications.
};

class PowerMonitorTest : public testing::Test {
 protected:
  PowerMonitorTest() {
#if defined(OS_MACOSX)
    // This needs to happen before PowerMonitor's ctor.
    PowerMonitor::AllocateSystemIOPorts();
#endif
    power_monitor_.reset(new PowerMonitor);
  }

  void ProcessPowerEvent(PowerMonitor::PowerEvent event_id) {
    power_monitor_->ProcessPowerEvent(event_id);
  }

  virtual ~PowerMonitorTest() {}

  MessageLoop message_loop_;
  scoped_ptr<PowerMonitor> power_monitor_;

  DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest);
};

TEST_F(PowerMonitorTest, PowerNotifications) {
  const int kObservers = 5;

  PowerTest test[kObservers];
  for (int index = 0; index < kObservers; ++index)
    power_monitor_->AddObserver(&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++) {
    ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT);
    EXPECT_EQ(test[0].power_state_changes(), 0);
  }

  // Sending resume when not suspended should have no effect.
  ProcessPowerEvent(PowerMonitor::RESUME_EVENT);
  message_loop_.RunUntilIdle();
  EXPECT_EQ(test[0].resumes(), 0);

  // Pretend we suspended.
  ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT);
  message_loop_.RunUntilIdle();
  EXPECT_EQ(test[0].suspends(), 1);

  // Send a second suspend notification.  This should be suppressed.
  ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT);
  message_loop_.RunUntilIdle();
  EXPECT_EQ(test[0].suspends(), 1);

  // Pretend we were awakened.
  ProcessPowerEvent(PowerMonitor::RESUME_EVENT);
  message_loop_.RunUntilIdle();
  EXPECT_EQ(test[0].resumes(), 1);

  // Send a duplicate resume notification.  This should be suppressed.
  ProcessPowerEvent(PowerMonitor::RESUME_EVENT);
  message_loop_.RunUntilIdle();
  EXPECT_EQ(test[0].resumes(), 1);
}

}  // namespace base