| OLD | NEW |
| (Empty) |
| 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/macros.h" | |
| 6 #include "base/message_loop/message_loop.h" | |
| 7 #include "base/run_loop.h" | |
| 8 #include "base/test/power_monitor_test_base.h" | |
| 9 #include "content/child/power_monitor_broadcast_source.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace content { | |
| 13 | |
| 14 class PowerMonitorBroadcastSourceTest : public testing::Test { | |
| 15 protected: | |
| 16 PowerMonitorBroadcastSourceTest() { | |
| 17 power_monitor_source_ = new PowerMonitorBroadcastSource(); | |
| 18 power_monitor_.reset(new base::PowerMonitor( | |
| 19 std::unique_ptr<base::PowerMonitorSource>(power_monitor_source_))); | |
| 20 } | |
| 21 ~PowerMonitorBroadcastSourceTest() override {} | |
| 22 | |
| 23 PowerMonitorBroadcastSource* source() { return power_monitor_source_; } | |
| 24 base::PowerMonitor* monitor() { return power_monitor_.get(); } | |
| 25 | |
| 26 base::MessageLoop message_loop_; | |
| 27 | |
| 28 private: | |
| 29 PowerMonitorBroadcastSource* power_monitor_source_; | |
| 30 std::unique_ptr<base::PowerMonitor> power_monitor_; | |
| 31 | |
| 32 DISALLOW_COPY_AND_ASSIGN(PowerMonitorBroadcastSourceTest); | |
| 33 }; | |
| 34 | |
| 35 TEST_F(PowerMonitorBroadcastSourceTest, PowerMessageReceiveBroadcast) { | |
| 36 | |
| 37 base::PowerMonitorTestObserver observer; | |
| 38 monitor()->AddObserver(&observer); | |
| 39 | |
| 40 // Sending resume when not suspended should have no effect. | |
| 41 source()->Resume(); | |
| 42 base::RunLoop().RunUntilIdle(); | |
| 43 EXPECT_EQ(observer.resumes(), 0); | |
| 44 | |
| 45 // Pretend we suspended. | |
| 46 source()->Suspend(); | |
| 47 base::RunLoop().RunUntilIdle(); | |
| 48 EXPECT_EQ(observer.suspends(), 1); | |
| 49 | |
| 50 // Send a second suspend notification. This should be suppressed. | |
| 51 source()->Suspend(); | |
| 52 base::RunLoop().RunUntilIdle(); | |
| 53 EXPECT_EQ(observer.suspends(), 1); | |
| 54 | |
| 55 // Pretend we were awakened. | |
| 56 source()->Resume(); | |
| 57 base::RunLoop().RunUntilIdle(); | |
| 58 EXPECT_EQ(observer.resumes(), 1); | |
| 59 | |
| 60 // Send a duplicate resume notification. This should be suppressed. | |
| 61 source()->Resume(); | |
| 62 base::RunLoop().RunUntilIdle(); | |
| 63 EXPECT_EQ(observer.resumes(), 1); | |
| 64 | |
| 65 // Pretend the device has gone on battery power | |
| 66 source()->PowerStateChange(true); | |
| 67 base::RunLoop().RunUntilIdle(); | |
| 68 EXPECT_EQ(observer.power_state_changes(), 1); | |
| 69 EXPECT_EQ(observer.last_power_state(), true); | |
| 70 | |
| 71 // Repeated indications the device is on battery power should be suppressed. | |
| 72 source()->PowerStateChange(true); | |
| 73 base::RunLoop().RunUntilIdle(); | |
| 74 EXPECT_EQ(observer.power_state_changes(), 1); | |
| 75 | |
| 76 // Pretend the device has gone off battery power | |
| 77 source()->PowerStateChange(false); | |
| 78 base::RunLoop().RunUntilIdle(); | |
| 79 EXPECT_EQ(observer.power_state_changes(), 2); | |
| 80 EXPECT_EQ(observer.last_power_state(), false); | |
| 81 | |
| 82 // Repeated indications the device is off battery power should be suppressed. | |
| 83 source()->PowerStateChange(false); | |
| 84 base::RunLoop().RunUntilIdle(); | |
| 85 EXPECT_EQ(observer.power_state_changes(), 2); | |
| 86 } | |
| 87 | |
| 88 } // namespace base | |
| OLD | NEW |