1 // Copyright (c) 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 "chromeos/dbus/fake_power_manager_client.h"
8 #include "base/bind_helpers.h"
9 #include "base/location.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/time/time.h"
15 FakePowerManagerClient::FakePowerManagerClient()
16 : num_request_restart_calls_(0),
17 num_request_shutdown_calls_(0),
18 num_set_policy_calls_(0),
19 num_set_is_projecting_calls_(0),
20 num_pending_suspend_readiness_callbacks_(0),
21 is_projecting_(false),
22 weak_ptr_factory_(this) {
25 FakePowerManagerClient::~FakePowerManagerClient() {
28 void FakePowerManagerClient::Init(dbus::Bus
* bus
) {
29 props_
.set_battery_percent(50);
30 props_
.set_is_calculating_battery_time(false);
31 props_
.set_battery_state(
32 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING
);
33 props_
.set_external_power(
34 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED
);
35 props_
.set_battery_time_to_full_sec(0);
36 props_
.set_battery_time_to_empty_sec(18000);
39 void FakePowerManagerClient::AddObserver(Observer
* observer
) {
40 observers_
.AddObserver(observer
);
43 void FakePowerManagerClient::RemoveObserver(Observer
* observer
) {
44 observers_
.RemoveObserver(observer
);
47 bool FakePowerManagerClient::HasObserver(const Observer
* observer
) const {
48 return observers_
.HasObserver(observer
);
51 void FakePowerManagerClient::SetRenderProcessManagerDelegate(
52 base::WeakPtr
<RenderProcessManagerDelegate
> delegate
) {
53 render_process_manager_delegate_
= delegate
;
56 void FakePowerManagerClient::DecreaseScreenBrightness(bool allow_off
) {
59 void FakePowerManagerClient::IncreaseScreenBrightness() {
62 void FakePowerManagerClient::SetScreenBrightnessPercent(double percent
,
66 void FakePowerManagerClient::GetScreenBrightnessPercent(
67 const GetScreenBrightnessPercentCallback
& callback
) {
70 void FakePowerManagerClient::DecreaseKeyboardBrightness() {
73 void FakePowerManagerClient::IncreaseKeyboardBrightness() {
76 void FakePowerManagerClient::RequestStatusUpdate() {
77 // RequestStatusUpdate() calls and notifies the observers
78 // asynchronously on a real device. On the fake implementation, we call
79 // observers in a posted task to emulate the same behavior.
80 base::ThreadTaskRunnerHandle::Get()->PostTask(
81 FROM_HERE
, base::Bind(&FakePowerManagerClient::NotifyObservers
,
82 weak_ptr_factory_
.GetWeakPtr()));
85 void FakePowerManagerClient::RequestSuspend() {
88 void FakePowerManagerClient::RequestRestart() {
89 ++num_request_restart_calls_
;
92 void FakePowerManagerClient::RequestShutdown() {
93 ++num_request_shutdown_calls_
;
96 void FakePowerManagerClient::NotifyUserActivity(
97 power_manager::UserActivityType type
) {
100 void FakePowerManagerClient::NotifyVideoActivity(bool is_fullscreen
) {
103 void FakePowerManagerClient::SetPolicy(
104 const power_manager::PowerManagementPolicy
& policy
) {
106 ++num_set_policy_calls_
;
109 void FakePowerManagerClient::SetIsProjecting(bool is_projecting
) {
110 ++num_set_is_projecting_calls_
;
111 is_projecting_
= is_projecting
;
114 void FakePowerManagerClient::SetPowerSource(const std::string
& id
) {}
116 base::Closure
FakePowerManagerClient::GetSuspendReadinessCallback() {
117 ++num_pending_suspend_readiness_callbacks_
;
119 return base::Bind(&FakePowerManagerClient::HandleSuspendReadiness
,
120 base::Unretained(this));
123 int FakePowerManagerClient::GetNumPendingSuspendReadinessCallbacks() {
124 return num_pending_suspend_readiness_callbacks_
;
127 void FakePowerManagerClient::SendSuspendImminent() {
128 FOR_EACH_OBSERVER(Observer
, observers_
, SuspendImminent());
129 if (render_process_manager_delegate_
)
130 render_process_manager_delegate_
->SuspendImminent();
133 void FakePowerManagerClient::SendSuspendDone() {
134 if (render_process_manager_delegate_
)
135 render_process_manager_delegate_
->SuspendDone();
137 FOR_EACH_OBSERVER(Observer
, observers_
, SuspendDone(base::TimeDelta()));
140 void FakePowerManagerClient::SendDarkSuspendImminent() {
141 FOR_EACH_OBSERVER(Observer
, observers_
, DarkSuspendImminent());
144 void FakePowerManagerClient::SendPowerButtonEvent(
146 const base::TimeTicks
& timestamp
) {
147 FOR_EACH_OBSERVER(Observer
, observers_
,
148 PowerButtonEventReceived(down
, timestamp
));
151 void FakePowerManagerClient::UpdatePowerProperties(
152 const power_manager::PowerSupplyProperties
& power_props
) {
153 props_
= power_props
;
157 void FakePowerManagerClient::NotifyObservers() {
158 FOR_EACH_OBSERVER(Observer
, observers_
, PowerChanged(props_
));
161 void FakePowerManagerClient::HandleSuspendReadiness() {
162 CHECK(num_pending_suspend_readiness_callbacks_
> 0);
164 --num_pending_suspend_readiness_callbacks_
;
167 } // namespace chromeos