OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/macros.h" | 5 #include "base/macros.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/test/power_monitor_test_base.h" | 8 #include "base/test/power_monitor_test_base.h" |
9 #include "content/child/power_monitor_broadcast_source.h" | 9 #include "content/child/power_monitor_broadcast_source.h" |
10 #include "content/common/power_monitor_messages.h" | |
11 #include "ipc/message_filter.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
13 | 11 |
14 namespace content { | 12 namespace content { |
15 | 13 |
16 class PowerMonitorBroadcastSourceTest : public testing::Test { | 14 class PowerMonitorBroadcastSourceTest : public testing::Test { |
17 protected: | 15 protected: |
18 PowerMonitorBroadcastSourceTest() { | 16 PowerMonitorBroadcastSourceTest() { |
19 power_monitor_source_ = new PowerMonitorBroadcastSource(); | 17 power_monitor_source_ = new PowerMonitorBroadcastSource(); |
20 power_monitor_.reset(new base::PowerMonitor( | 18 power_monitor_.reset(new base::PowerMonitor( |
21 std::unique_ptr<base::PowerMonitorSource>(power_monitor_source_))); | 19 std::unique_ptr<base::PowerMonitorSource>(power_monitor_source_))); |
22 } | 20 } |
23 ~PowerMonitorBroadcastSourceTest() override {} | 21 ~PowerMonitorBroadcastSourceTest() override {} |
24 | 22 |
25 PowerMonitorBroadcastSource* source() { return power_monitor_source_; } | 23 PowerMonitorBroadcastSource* source() { return power_monitor_source_; } |
26 base::PowerMonitor* monitor() { return power_monitor_.get(); } | 24 base::PowerMonitor* monitor() { return power_monitor_.get(); } |
27 | 25 |
28 base::MessageLoop message_loop_; | 26 base::MessageLoop message_loop_; |
29 | 27 |
30 private: | 28 private: |
31 PowerMonitorBroadcastSource* power_monitor_source_; | 29 PowerMonitorBroadcastSource* power_monitor_source_; |
32 std::unique_ptr<base::PowerMonitor> power_monitor_; | 30 std::unique_ptr<base::PowerMonitor> power_monitor_; |
33 | 31 |
34 DISALLOW_COPY_AND_ASSIGN(PowerMonitorBroadcastSourceTest); | 32 DISALLOW_COPY_AND_ASSIGN(PowerMonitorBroadcastSourceTest); |
35 }; | 33 }; |
36 | 34 |
37 TEST_F(PowerMonitorBroadcastSourceTest, PowerMessageReceiveBroadcast) { | 35 TEST_F(PowerMonitorBroadcastSourceTest, PowerMessageReceiveBroadcast) { |
38 IPC::MessageFilter* message_filter = source()->GetMessageFilter(); | |
39 | 36 |
40 base::PowerMonitorTestObserver observer; | 37 base::PowerMonitorTestObserver observer; |
41 monitor()->AddObserver(&observer); | 38 monitor()->AddObserver(&observer); |
42 | 39 |
43 PowerMonitorMsg_Suspend suspend_msg; | |
44 PowerMonitorMsg_Resume resume_msg; | |
45 | |
46 // Sending resume when not suspended should have no effect. | 40 // Sending resume when not suspended should have no effect. |
47 message_filter->OnMessageReceived(resume_msg); | 41 source()->Resume(); |
48 base::RunLoop().RunUntilIdle(); | 42 base::RunLoop().RunUntilIdle(); |
49 EXPECT_EQ(observer.resumes(), 0); | 43 EXPECT_EQ(observer.resumes(), 0); |
50 | 44 |
51 // Pretend we suspended. | 45 // Pretend we suspended. |
52 message_filter->OnMessageReceived(suspend_msg); | 46 source()->Suspend(); |
53 base::RunLoop().RunUntilIdle(); | 47 base::RunLoop().RunUntilIdle(); |
54 EXPECT_EQ(observer.suspends(), 1); | 48 EXPECT_EQ(observer.suspends(), 1); |
55 | 49 |
56 // Send a second suspend notification. This should be suppressed. | 50 // Send a second suspend notification. This should be suppressed. |
57 message_filter->OnMessageReceived(suspend_msg); | 51 source()->Suspend(); |
58 base::RunLoop().RunUntilIdle(); | 52 base::RunLoop().RunUntilIdle(); |
59 EXPECT_EQ(observer.suspends(), 1); | 53 EXPECT_EQ(observer.suspends(), 1); |
60 | 54 |
61 // Pretend we were awakened. | 55 // Pretend we were awakened. |
62 message_filter->OnMessageReceived(resume_msg); | 56 source()->Resume(); |
63 base::RunLoop().RunUntilIdle(); | 57 base::RunLoop().RunUntilIdle(); |
64 EXPECT_EQ(observer.resumes(), 1); | 58 EXPECT_EQ(observer.resumes(), 1); |
65 | 59 |
66 // Send a duplicate resume notification. This should be suppressed. | 60 // Send a duplicate resume notification. This should be suppressed. |
67 message_filter->OnMessageReceived(resume_msg); | 61 source()->Resume(); |
68 base::RunLoop().RunUntilIdle(); | 62 base::RunLoop().RunUntilIdle(); |
69 EXPECT_EQ(observer.resumes(), 1); | 63 EXPECT_EQ(observer.resumes(), 1); |
70 | 64 |
71 PowerMonitorMsg_PowerStateChange on_battery_msg(true); | |
72 PowerMonitorMsg_PowerStateChange off_battery_msg(false); | |
73 | |
74 // Pretend the device has gone on battery power | 65 // Pretend the device has gone on battery power |
75 message_filter->OnMessageReceived(on_battery_msg); | 66 source()->PowerStateChange(true); |
76 base::RunLoop().RunUntilIdle(); | 67 base::RunLoop().RunUntilIdle(); |
77 EXPECT_EQ(observer.power_state_changes(), 1); | 68 EXPECT_EQ(observer.power_state_changes(), 1); |
78 EXPECT_EQ(observer.last_power_state(), true); | 69 EXPECT_EQ(observer.last_power_state(), true); |
79 | 70 |
80 // Repeated indications the device is on battery power should be suppressed. | 71 // Repeated indications the device is on battery power should be suppressed. |
81 message_filter->OnMessageReceived(on_battery_msg); | 72 source()->PowerStateChange(true); |
82 base::RunLoop().RunUntilIdle(); | 73 base::RunLoop().RunUntilIdle(); |
83 EXPECT_EQ(observer.power_state_changes(), 1); | 74 EXPECT_EQ(observer.power_state_changes(), 1); |
84 | 75 |
85 // Pretend the device has gone off battery power | 76 // Pretend the device has gone off battery power |
86 message_filter->OnMessageReceived(off_battery_msg); | 77 source()->PowerStateChange(false); |
87 base::RunLoop().RunUntilIdle(); | 78 base::RunLoop().RunUntilIdle(); |
88 EXPECT_EQ(observer.power_state_changes(), 2); | 79 EXPECT_EQ(observer.power_state_changes(), 2); |
89 EXPECT_EQ(observer.last_power_state(), false); | 80 EXPECT_EQ(observer.last_power_state(), false); |
90 | 81 |
91 // Repeated indications the device is off battery power should be suppressed. | 82 // Repeated indications the device is off battery power should be suppressed. |
92 message_filter->OnMessageReceived(off_battery_msg); | 83 source()->PowerStateChange(false); |
93 base::RunLoop().RunUntilIdle(); | 84 base::RunLoop().RunUntilIdle(); |
94 EXPECT_EQ(observer.power_state_changes(), 2); | 85 EXPECT_EQ(observer.power_state_changes(), 2); |
95 } | 86 } |
96 | 87 |
97 } // namespace base | 88 } // namespace base |
OLD | NEW |