• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/test/power_monitor_test_base.h"
6 
7 #include "base/message_loop/message_loop.h"
8 #include "base/message_loop/message_loop_current.h"
9 #include "base/power_monitor/power_monitor.h"
10 #include "base/power_monitor/power_monitor_source.h"
11 #include "base/run_loop.h"
12 
13 namespace base {
14 
PowerMonitorTestSource()15 PowerMonitorTestSource::PowerMonitorTestSource()
16     : test_on_battery_power_(false) {
17   DCHECK(MessageLoopCurrent::Get())
18       << "PowerMonitorTestSource requires a MessageLoop.";
19 }
20 
21 PowerMonitorTestSource::~PowerMonitorTestSource() = default;
22 
Shutdown()23 void PowerMonitorTestSource::Shutdown() {}
24 
GeneratePowerStateEvent(bool on_battery_power)25 void PowerMonitorTestSource::GeneratePowerStateEvent(bool on_battery_power) {
26   test_on_battery_power_ = on_battery_power;
27   ProcessPowerEvent(POWER_STATE_EVENT);
28   RunLoop().RunUntilIdle();
29 }
30 
GenerateSuspendEvent()31 void PowerMonitorTestSource::GenerateSuspendEvent() {
32   ProcessPowerEvent(SUSPEND_EVENT);
33   RunLoop().RunUntilIdle();
34 }
35 
GenerateResumeEvent()36 void PowerMonitorTestSource::GenerateResumeEvent() {
37   ProcessPowerEvent(RESUME_EVENT);
38   RunLoop().RunUntilIdle();
39 }
40 
IsOnBatteryPowerImpl()41 bool PowerMonitorTestSource::IsOnBatteryPowerImpl() {
42   return test_on_battery_power_;
43 };
44 
PowerMonitorTestObserver()45 PowerMonitorTestObserver::PowerMonitorTestObserver()
46     : last_power_state_(false),
47       power_state_changes_(0),
48       suspends_(0),
49       resumes_(0) {
50 }
51 
52 PowerMonitorTestObserver::~PowerMonitorTestObserver() = default;
53 
54 // PowerObserver callbacks.
OnPowerStateChange(bool on_battery_power)55 void PowerMonitorTestObserver::OnPowerStateChange(bool on_battery_power) {
56   last_power_state_ = on_battery_power;
57   power_state_changes_++;
58 }
59 
OnSuspend()60 void PowerMonitorTestObserver::OnSuspend() {
61   suspends_++;
62 }
63 
OnResume()64 void PowerMonitorTestObserver::OnResume() {
65   resumes_++;
66 }
67 
68 }  // namespace base
69