- add sources.
[platform/framework/web/crosswalk.git] / src / chromeos / dbus / power_policy_controller_unittest.cc
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.
4
5 #include "chromeos/dbus/power_policy_controller.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "chromeos/dbus/dbus_thread_manager.h"
9 #include "chromeos/dbus/fake_dbus_thread_manager.h"
10 #include "chromeos/dbus/fake_power_manager_client.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 using ::testing::_;
15 using ::testing::SaveArg;
16
17 namespace chromeos {
18
19 class PowerPolicyControllerTest : public testing::Test {
20  public:
21   PowerPolicyControllerTest() {}
22   virtual ~PowerPolicyControllerTest() {}
23
24   virtual void SetUp() OVERRIDE {
25     dbus_manager_ = new FakeDBusThreadManager;
26     DBusThreadManager::InitializeForTesting(dbus_manager_);  // Takes ownership.
27
28     policy_controller_.reset(
29         new PowerPolicyController(dbus_manager_, &fake_power_client_));
30   }
31
32   virtual void TearDown() OVERRIDE {
33     policy_controller_.reset();
34     DBusThreadManager::Shutdown();
35   }
36
37  protected:
38   FakeDBusThreadManager* dbus_manager_;  // Not owned.
39   FakePowerManagerClient fake_power_client_;
40   scoped_ptr<PowerPolicyController> policy_controller_;
41 };
42
43 TEST_F(PowerPolicyControllerTest, Prefs) {
44   PowerPolicyController::PrefValues prefs;
45   prefs.ac_screen_dim_delay_ms = 600000;
46   prefs.ac_screen_off_delay_ms = 660000;
47   prefs.ac_idle_delay_ms = 720000;
48   prefs.battery_screen_dim_delay_ms = 300000;
49   prefs.battery_screen_off_delay_ms = 360000;
50   prefs.battery_idle_delay_ms = 420000;
51   prefs.ac_idle_action = PowerPolicyController::ACTION_SUSPEND;
52   prefs.battery_idle_action = PowerPolicyController::ACTION_STOP_SESSION;
53   prefs.lid_closed_action = PowerPolicyController::ACTION_SHUT_DOWN;
54   prefs.use_audio_activity = true;
55   prefs.use_video_activity = true;
56   prefs.enable_screen_lock = false;
57   prefs.presentation_screen_dim_delay_factor = 3.0;
58   prefs.user_activity_screen_dim_delay_factor = 2.0;
59   prefs.wait_for_initial_user_activity = true;
60   policy_controller_->ApplyPrefs(prefs);
61
62   power_manager::PowerManagementPolicy expected_policy;
63   expected_policy.mutable_ac_delays()->set_screen_dim_ms(600000);
64   expected_policy.mutable_ac_delays()->set_screen_off_ms(660000);
65   expected_policy.mutable_ac_delays()->set_screen_lock_ms(-1);
66   expected_policy.mutable_ac_delays()->set_idle_warning_ms(-1);
67   expected_policy.mutable_ac_delays()->set_idle_ms(720000);
68   expected_policy.mutable_battery_delays()->set_screen_dim_ms(300000);
69   expected_policy.mutable_battery_delays()->set_screen_off_ms(360000);
70   expected_policy.mutable_battery_delays()->set_screen_lock_ms(-1);
71   expected_policy.mutable_battery_delays()->set_idle_warning_ms(-1);
72   expected_policy.mutable_battery_delays()->set_idle_ms(420000);
73   expected_policy.set_ac_idle_action(
74       power_manager::PowerManagementPolicy_Action_SUSPEND);
75   expected_policy.set_battery_idle_action(
76       power_manager::PowerManagementPolicy_Action_STOP_SESSION);
77   expected_policy.set_lid_closed_action(
78       power_manager::PowerManagementPolicy_Action_SHUT_DOWN);
79   expected_policy.set_use_audio_activity(true);
80   expected_policy.set_use_video_activity(true);
81   expected_policy.set_presentation_screen_dim_delay_factor(3.0);
82   expected_policy.set_user_activity_screen_dim_delay_factor(2.0);
83   expected_policy.set_wait_for_initial_user_activity(true);
84   expected_policy.set_reason("Prefs");
85   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
86             PowerPolicyController::GetPolicyDebugString(
87                 fake_power_client_.get_policy()));
88
89   // Change some prefs and check that an updated policy is sent.
90   prefs.ac_idle_warning_delay_ms = 700000;
91   prefs.battery_idle_warning_delay_ms = 400000;
92   prefs.lid_closed_action = PowerPolicyController::ACTION_SUSPEND;
93   policy_controller_->ApplyPrefs(prefs);
94   expected_policy.mutable_ac_delays()->set_idle_warning_ms(700000);
95   expected_policy.mutable_battery_delays()->set_idle_warning_ms(400000);
96   expected_policy.set_lid_closed_action(
97       power_manager::PowerManagementPolicy_Action_SUSPEND);
98   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
99             PowerPolicyController::GetPolicyDebugString(
100                 fake_power_client_.get_policy()));
101
102   // The enable-screen-lock pref should force the screen-lock delays to
103   // match the screen-off delays plus a constant value.
104   prefs.enable_screen_lock = true;
105   policy_controller_->ApplyPrefs(prefs);
106   expected_policy.mutable_ac_delays()->set_screen_lock_ms(
107       660000 + PowerPolicyController::kScreenLockAfterOffDelayMs);
108   expected_policy.mutable_battery_delays()->set_screen_lock_ms(
109       360000 + PowerPolicyController::kScreenLockAfterOffDelayMs);
110   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
111             PowerPolicyController::GetPolicyDebugString(
112                 fake_power_client_.get_policy()));
113
114   // If the screen-lock-delay prefs are set to lower values than the
115   // screen-off delays plus the constant, the lock prefs should take
116   // precedence.
117   prefs.ac_screen_lock_delay_ms = 70000;
118   prefs.battery_screen_lock_delay_ms = 60000;
119   policy_controller_->ApplyPrefs(prefs);
120   expected_policy.mutable_ac_delays()->set_screen_lock_ms(70000);
121   expected_policy.mutable_battery_delays()->set_screen_lock_ms(60000);
122   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
123             PowerPolicyController::GetPolicyDebugString(
124                 fake_power_client_.get_policy()));
125
126   // If the artificial screen-lock delays would exceed the idle delay, they
127   // shouldn't be set -- the power manager would ignore them since the
128   // idle action should lock the screen in this case.
129   prefs.ac_screen_off_delay_ms = prefs.ac_idle_delay_ms - 1;
130   prefs.battery_screen_off_delay_ms = prefs.battery_idle_delay_ms - 1;
131   prefs.ac_screen_lock_delay_ms = -1;
132   prefs.battery_screen_lock_delay_ms = -1;
133   policy_controller_->ApplyPrefs(prefs);
134   expected_policy.mutable_ac_delays()->set_screen_off_ms(
135       prefs.ac_screen_off_delay_ms);
136   expected_policy.mutable_battery_delays()->set_screen_off_ms(
137       prefs.battery_screen_off_delay_ms);
138   expected_policy.mutable_ac_delays()->set_screen_lock_ms(-1);
139   expected_policy.mutable_battery_delays()->set_screen_lock_ms(-1);
140   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
141             PowerPolicyController::GetPolicyDebugString(
142                 fake_power_client_.get_policy()));
143
144   // Set the "allow screen wake locks" pref to false.  The system should be
145   // prevented from suspending due to user inactivity on AC power but the
146   // pref-supplied screen-related delays should be left untouched.
147   prefs.allow_screen_wake_locks = false;
148   policy_controller_->ApplyPrefs(prefs);
149   policy_controller_->AddScreenWakeLock("Screen");
150   expected_policy.set_ac_idle_action(
151       power_manager::PowerManagementPolicy_Action_DO_NOTHING);
152   expected_policy.set_reason("Prefs, Screen");
153   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
154             PowerPolicyController::GetPolicyDebugString(
155                 fake_power_client_.get_policy()));
156 }
157
158 TEST_F(PowerPolicyControllerTest, WakeLocks) {
159   const char kSystemWakeLockReason[] = "system";
160   const int system_id =
161       policy_controller_->AddSystemWakeLock(kSystemWakeLockReason);
162   power_manager::PowerManagementPolicy expected_policy;
163   expected_policy.set_ac_idle_action(
164       power_manager::PowerManagementPolicy_Action_DO_NOTHING);
165   expected_policy.set_battery_idle_action(
166       power_manager::PowerManagementPolicy_Action_DO_NOTHING);
167   expected_policy.set_reason(kSystemWakeLockReason);
168   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
169             PowerPolicyController::GetPolicyDebugString(
170                 fake_power_client_.get_policy()));
171
172   const char kScreenWakeLockReason[] = "screen";
173   const int screen_id = policy_controller_->AddScreenWakeLock(
174       kScreenWakeLockReason);
175   expected_policy.mutable_ac_delays()->set_screen_dim_ms(0);
176   expected_policy.mutable_ac_delays()->set_screen_off_ms(0);
177   expected_policy.mutable_ac_delays()->set_screen_lock_ms(0);
178   expected_policy.mutable_battery_delays()->set_screen_dim_ms(0);
179   expected_policy.mutable_battery_delays()->set_screen_off_ms(0);
180   expected_policy.mutable_battery_delays()->set_screen_lock_ms(0);
181   expected_policy.set_reason(
182       std::string(kScreenWakeLockReason) + ", " + kSystemWakeLockReason);
183   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
184             PowerPolicyController::GetPolicyDebugString(
185                 fake_power_client_.get_policy()));
186
187   policy_controller_->RemoveWakeLock(system_id);
188   expected_policy.set_reason(kScreenWakeLockReason);
189   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
190             PowerPolicyController::GetPolicyDebugString(
191                 fake_power_client_.get_policy()));
192
193   policy_controller_->RemoveWakeLock(screen_id);
194   expected_policy.Clear();
195   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
196             PowerPolicyController::GetPolicyDebugString(
197                 fake_power_client_.get_policy()));
198 }
199
200 }  // namespace chromeos