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/test/power_monitor_test_base.h"
6 #include "content/browser/power_monitor_message_broadcaster.h"
7 #include "content/common/power_monitor_messages.h"
8 #include "ipc/ipc_sender.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 class PowerMonitorMessageSender
: public IPC::Sender
{
15 PowerMonitorMessageSender()
16 : power_state_changes_(0),
20 virtual ~PowerMonitorMessageSender() {}
22 virtual bool Send(IPC::Message
* msg
) OVERRIDE
{
23 switch (msg
->type()) {
24 case PowerMonitorMsg_Suspend::ID
:
27 case PowerMonitorMsg_Resume::ID
:
30 case PowerMonitorMsg_PowerStateChange::ID
:
31 power_state_changes_
++;
38 // Test status counts.
39 int power_state_changes() { return power_state_changes_
; }
40 int suspends() { return suspends_
; }
41 int resumes() { return resumes_
; }
44 int power_state_changes_
; // Count of OnPowerStateChange notifications.
45 int suspends_
; // Count of OnSuspend notifications.
46 int resumes_
; // Count of OnResume notifications.
49 class PowerMonitorMessageBroadcasterTest
: public testing::Test
{
51 PowerMonitorMessageBroadcasterTest() {
52 power_monitor_source_
= new base::PowerMonitorTestSource();
53 power_monitor_
.reset(new base::PowerMonitor(
54 scoped_ptr
<base::PowerMonitorSource
>(power_monitor_source_
)));
56 virtual ~PowerMonitorMessageBroadcasterTest() {};
58 base::PowerMonitorTestSource
* source() { return power_monitor_source_
; }
59 base::PowerMonitor
* monitor() { return power_monitor_
.get(); }
62 base::PowerMonitorTestSource
* power_monitor_source_
;
63 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
65 DISALLOW_COPY_AND_ASSIGN(PowerMonitorMessageBroadcasterTest
);
68 TEST_F(PowerMonitorMessageBroadcasterTest
, PowerMessageBroadcast
) {
69 PowerMonitorMessageSender sender
;
70 PowerMonitorMessageBroadcaster
broadcaster(&sender
);
72 // Calling Init should invoke a power state change.
74 EXPECT_EQ(sender
.power_state_changes(), 1);
76 // Sending resume when not suspended should have no effect.
77 source()->GenerateResumeEvent();
78 EXPECT_EQ(sender
.resumes(), 0);
80 // Pretend we suspended.
81 source()->GenerateSuspendEvent();
82 EXPECT_EQ(sender
.suspends(), 1);
84 // Send a second suspend notification. This should be suppressed.
85 source()->GenerateSuspendEvent();
86 EXPECT_EQ(sender
.suspends(), 1);
88 // Pretend we were awakened.
89 source()->GenerateResumeEvent();
90 EXPECT_EQ(sender
.resumes(), 1);
92 // Send a duplicate resume notification. This should be suppressed.
93 source()->GenerateResumeEvent();
94 EXPECT_EQ(sender
.resumes(), 1);
96 // Pretend the device has gone on battery power
97 source()->GeneratePowerStateEvent(true);
98 EXPECT_EQ(sender
.power_state_changes(), 2);
100 // Repeated indications the device is on battery power should be suppressed.
101 source()->GeneratePowerStateEvent(true);
102 EXPECT_EQ(sender
.power_state_changes(), 2);
104 // Pretend the device has gone off battery power
105 source()->GeneratePowerStateEvent(false);
106 EXPECT_EQ(sender
.power_state_changes(), 3);
108 // Repeated indications the device is off battery power should be suppressed.
109 source()->GeneratePowerStateEvent(false);
110 EXPECT_EQ(sender
.power_state_changes(), 3);