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/message_loop/message_loop.h"
6 #include "base/test/power_monitor_test_base.h"
7 #include "content/child/power_monitor_broadcast_source.h"
8 #include "content/common/power_monitor_messages.h"
9 #include "ipc/message_filter.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 class PowerMonitorBroadcastSourceTest
: public testing::Test
{
16 PowerMonitorBroadcastSourceTest() {
17 power_monitor_source_
= new PowerMonitorBroadcastSource();
18 power_monitor_
.reset(new base::PowerMonitor(
19 scoped_ptr
<base::PowerMonitorSource
>(power_monitor_source_
)));
21 ~PowerMonitorBroadcastSourceTest() override
{}
23 PowerMonitorBroadcastSource
* source() { return power_monitor_source_
; }
24 base::PowerMonitor
* monitor() { return power_monitor_
.get(); }
26 base::MessageLoop message_loop_
;
29 PowerMonitorBroadcastSource
* power_monitor_source_
;
30 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
32 DISALLOW_COPY_AND_ASSIGN(PowerMonitorBroadcastSourceTest
);
35 TEST_F(PowerMonitorBroadcastSourceTest
, PowerMessageReceiveBroadcast
) {
36 IPC::MessageFilter
* message_filter
= source()->GetMessageFilter();
38 base::PowerMonitorTestObserver observer
;
39 monitor()->AddObserver(&observer
);
41 PowerMonitorMsg_Suspend suspend_msg
;
42 PowerMonitorMsg_Resume resume_msg
;
44 // Sending resume when not suspended should have no effect.
45 message_filter
->OnMessageReceived(resume_msg
);
46 message_loop_
.RunUntilIdle();
47 EXPECT_EQ(observer
.resumes(), 0);
49 // Pretend we suspended.
50 message_filter
->OnMessageReceived(suspend_msg
);
51 message_loop_
.RunUntilIdle();
52 EXPECT_EQ(observer
.suspends(), 1);
54 // Send a second suspend notification. This should be suppressed.
55 message_filter
->OnMessageReceived(suspend_msg
);
56 message_loop_
.RunUntilIdle();
57 EXPECT_EQ(observer
.suspends(), 1);
59 // Pretend we were awakened.
60 message_filter
->OnMessageReceived(resume_msg
);
61 message_loop_
.RunUntilIdle();
62 EXPECT_EQ(observer
.resumes(), 1);
64 // Send a duplicate resume notification. This should be suppressed.
65 message_filter
->OnMessageReceived(resume_msg
);
66 message_loop_
.RunUntilIdle();
67 EXPECT_EQ(observer
.resumes(), 1);
69 PowerMonitorMsg_PowerStateChange
on_battery_msg(true);
70 PowerMonitorMsg_PowerStateChange
off_battery_msg(false);
72 // Pretend the device has gone on battery power
73 message_filter
->OnMessageReceived(on_battery_msg
);
74 message_loop_
.RunUntilIdle();
75 EXPECT_EQ(observer
.power_state_changes(), 1);
76 EXPECT_EQ(observer
.last_power_state(), true);
78 // Repeated indications the device is on battery power should be suppressed.
79 message_filter
->OnMessageReceived(on_battery_msg
);
80 message_loop_
.RunUntilIdle();
81 EXPECT_EQ(observer
.power_state_changes(), 1);
83 // Pretend the device has gone off battery power
84 message_filter
->OnMessageReceived(off_battery_msg
);
85 message_loop_
.RunUntilIdle();
86 EXPECT_EQ(observer
.power_state_changes(), 2);
87 EXPECT_EQ(observer
.last_power_state(), false);
89 // Repeated indications the device is off battery power should be suppressed.
90 message_filter
->OnMessageReceived(off_battery_msg
);
91 message_loop_
.RunUntilIdle();
92 EXPECT_EQ(observer
.power_state_changes(), 2);