- add sources.
[platform/framework/web/crosswalk.git] / src / base / power_monitor / power_monitor_unittest.cc
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
3 // found in the LICENSE file.
4
5 #include "base/power_monitor/power_monitor.h"
6 #include "base/test/power_monitor_test_base.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace base {
10
11 class PowerMonitorTest : public testing::Test {
12  protected:
13   PowerMonitorTest() {
14     power_monitor_source_ = new PowerMonitorTestSource();
15     power_monitor_.reset(new PowerMonitor(
16         scoped_ptr<PowerMonitorSource>(power_monitor_source_)));
17   }
18   virtual ~PowerMonitorTest() {};
19
20   PowerMonitorTestSource* source() { return power_monitor_source_; }
21   PowerMonitor* monitor() { return power_monitor_.get(); }
22
23  private:
24   PowerMonitorTestSource* power_monitor_source_;
25   scoped_ptr<PowerMonitor> power_monitor_;
26
27   DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest);
28 };
29
30 // PowerMonitorSource is tightly coupled with the PowerMonitor, so this test
31 // Will cover both classes
32 TEST_F(PowerMonitorTest, PowerNotifications) {
33   const int kObservers = 5;
34
35   PowerMonitorTestObserver observers[kObservers];
36   for (int index = 0; index < kObservers; ++index)
37     monitor()->AddObserver(&observers[index]);
38
39   // Sending resume when not suspended should have no effect.
40   source()->GenerateResumeEvent();
41   EXPECT_EQ(observers[0].resumes(), 0);
42
43   // Pretend we suspended.
44   source()->GenerateSuspendEvent();
45   // Ensure all observers were notified of the event
46   for (int index = 0; index < kObservers; ++index)
47     EXPECT_EQ(observers[index].suspends(), 1);
48
49   // Send a second suspend notification.  This should be suppressed.
50   source()->GenerateSuspendEvent();
51   EXPECT_EQ(observers[0].suspends(), 1);
52
53   // Pretend we were awakened.
54   source()->GenerateResumeEvent();
55   EXPECT_EQ(observers[0].resumes(), 1);
56
57   // Send a duplicate resume notification.  This should be suppressed.
58   source()->GenerateResumeEvent();
59   EXPECT_EQ(observers[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);
78 }
79
80 }  // namespace base