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.
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"
11 class PowerMonitorTest
: public testing::Test
{
14 power_monitor_source_
= new PowerMonitorTestSource();
15 power_monitor_
.reset(new PowerMonitor(
16 scoped_ptr
<PowerMonitorSource
>(power_monitor_source_
)));
18 ~PowerMonitorTest() override
{};
20 PowerMonitorTestSource
* source() { return power_monitor_source_
; }
21 PowerMonitor
* monitor() { return power_monitor_
.get(); }
24 PowerMonitorTestSource
* power_monitor_source_
;
25 scoped_ptr
<PowerMonitor
> power_monitor_
;
27 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest
);
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;
35 PowerMonitorTestObserver observers
[kObservers
];
36 for (int index
= 0; index
< kObservers
; ++index
)
37 monitor()->AddObserver(&observers
[index
]);
39 // Sending resume when not suspended should have no effect.
40 source()->GenerateResumeEvent();
41 EXPECT_EQ(observers
[0].resumes(), 0);
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);
49 // Send a second suspend notification. This should be suppressed.
50 source()->GenerateSuspendEvent();
51 EXPECT_EQ(observers
[0].suspends(), 1);
53 // Pretend we were awakened.
54 source()->GenerateResumeEvent();
55 EXPECT_EQ(observers
[0].resumes(), 1);
57 // Send a duplicate resume notification. This should be suppressed.
58 source()->GenerateResumeEvent();
59 EXPECT_EQ(observers
[0].resumes(), 1);
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);
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);
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);
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);