OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "chromeos/dbus/power_policy_controller.h" | 5 #include "chromeos/dbus/power_policy_controller.h" |
6 | 6 |
7 #include "chromeos/dbus/dbus_thread_manager.h" | 7 #include "chromeos/dbus/dbus_thread_manager.h" |
8 #include "chromeos/dbus/mock_dbus_thread_manager.h" | 8 #include "chromeos/dbus/mock_dbus_thread_manager.h" |
9 #include "chromeos/dbus/mock_power_manager_client.h" | 9 #include "chromeos/dbus/mock_power_manager_client.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 using ::testing::_; | 13 using ::testing::_; |
14 using ::testing::SaveArg; | 14 using ::testing::SaveArg; |
15 | 15 |
16 namespace chromeos { | 16 namespace chromeos { |
17 | 17 |
18 class PowerPolicyControllerTest : public testing::Test { | 18 class PowerPolicyControllerTest : public testing::Test { |
19 public: | 19 public: |
20 PowerPolicyControllerTest() {} | 20 PowerPolicyControllerTest() {} |
21 virtual ~PowerPolicyControllerTest() {} | 21 virtual ~PowerPolicyControllerTest() {} |
22 | 22 |
23 virtual void SetUp() OVERRIDE { | 23 virtual void SetUp() OVERRIDE { |
24 dbus_manager_ = new MockDBusThreadManager; | 24 dbus_manager_ = new MockDBusThreadManager; |
25 DBusThreadManager::InitializeForTesting(dbus_manager_); // Takes ownership. | 25 DBusThreadManager::InitializeForTesting(dbus_manager_); // Takes ownership. |
26 power_client_ = dbus_manager_->mock_power_manager_client(); | 26 power_client_ = dbus_manager_->mock_power_manager_client(); |
27 EXPECT_CALL(*power_client_, SetPolicy(_)) | 27 EXPECT_CALL(*power_client_, SetPolicy(_)) |
28 .WillRepeatedly(SaveArg<0>(&last_policy_)); | 28 .WillRepeatedly(SaveArg<0>(&last_policy_)); |
29 | |
30 policy_controller_ = dbus_manager_->GetPowerPolicyController(); | 29 policy_controller_ = dbus_manager_->GetPowerPolicyController(); |
31 | |
32 // TODO(derat): Write what looks like it will be a ridiculously large | |
33 // amount of code to register prefs so that UpdatePolicyFromPrefs() can | |
34 // be tested. | |
35 } | 30 } |
36 | 31 |
37 virtual void TearDown() OVERRIDE { | 32 virtual void TearDown() OVERRIDE { |
38 DBusThreadManager::Shutdown(); | 33 DBusThreadManager::Shutdown(); |
39 } | 34 } |
40 | 35 |
41 protected: | 36 protected: |
42 MockDBusThreadManager* dbus_manager_; // Not owned. | 37 MockDBusThreadManager* dbus_manager_; // Not owned. |
43 MockPowerManagerClient* power_client_; // Not owned. | 38 MockPowerManagerClient* power_client_; // Not owned. |
44 PowerPolicyController* policy_controller_; // Not owned. | 39 PowerPolicyController* policy_controller_; // Not owned. |
45 | 40 |
46 power_manager::PowerManagementPolicy last_policy_; | 41 power_manager::PowerManagementPolicy last_policy_; |
47 }; | 42 }; |
48 | 43 |
| 44 TEST_F(PowerPolicyControllerTest, Prefs) { |
| 45 PowerPolicyController::PrefValues prefs; |
| 46 prefs.ac_screen_dim_delay_ms = 600000; |
| 47 prefs.ac_screen_off_delay_ms = 660000; |
| 48 prefs.ac_idle_delay_ms = 720000; |
| 49 prefs.battery_screen_dim_delay_ms = 300000; |
| 50 prefs.battery_screen_off_delay_ms = 360000; |
| 51 prefs.battery_idle_delay_ms = 420000; |
| 52 prefs.idle_action = PowerPolicyController::ACTION_SUSPEND; |
| 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_idle_delay_factor = 2.0; |
| 58 policy_controller_->ApplyPrefs(prefs); |
| 59 |
| 60 power_manager::PowerManagementPolicy expected_policy; |
| 61 expected_policy.mutable_ac_delays()->set_screen_dim_ms(600000); |
| 62 expected_policy.mutable_ac_delays()->set_screen_off_ms(660000); |
| 63 expected_policy.mutable_ac_delays()->set_screen_lock_ms(-1); |
| 64 expected_policy.mutable_ac_delays()->set_idle_warning_ms(-1); |
| 65 expected_policy.mutable_ac_delays()->set_idle_ms(720000); |
| 66 expected_policy.mutable_battery_delays()->set_screen_dim_ms(300000); |
| 67 expected_policy.mutable_battery_delays()->set_screen_off_ms(360000); |
| 68 expected_policy.mutable_battery_delays()->set_screen_lock_ms(-1); |
| 69 expected_policy.mutable_battery_delays()->set_idle_warning_ms(-1); |
| 70 expected_policy.mutable_battery_delays()->set_idle_ms(420000); |
| 71 expected_policy.set_idle_action( |
| 72 power_manager::PowerManagementPolicy_Action_SUSPEND); |
| 73 expected_policy.set_lid_closed_action( |
| 74 power_manager::PowerManagementPolicy_Action_SHUT_DOWN); |
| 75 expected_policy.set_use_audio_activity(true); |
| 76 expected_policy.set_use_video_activity(true); |
| 77 expected_policy.set_presentation_idle_delay_factor(2.0); |
| 78 expected_policy.set_reason("Prefs"); |
| 79 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
| 80 PowerPolicyController::GetPolicyDebugString(last_policy_)); |
| 81 |
| 82 // Change some prefs and check that an updated policy is sent. |
| 83 prefs.ac_idle_warning_delay_ms = 700000; |
| 84 prefs.battery_idle_warning_delay_ms = 400000; |
| 85 prefs.lid_closed_action = PowerPolicyController::ACTION_SUSPEND; |
| 86 policy_controller_->ApplyPrefs(prefs); |
| 87 expected_policy.mutable_ac_delays()->set_idle_warning_ms(700000); |
| 88 expected_policy.mutable_battery_delays()->set_idle_warning_ms(400000); |
| 89 expected_policy.set_lid_closed_action( |
| 90 power_manager::PowerManagementPolicy_Action_SUSPEND); |
| 91 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
| 92 PowerPolicyController::GetPolicyDebugString(last_policy_)); |
| 93 |
| 94 // The enable-screen-lock pref should force the screen-lock delays to |
| 95 // match the screen-off delays. |
| 96 prefs.enable_screen_lock = true; |
| 97 policy_controller_->ApplyPrefs(prefs); |
| 98 expected_policy.mutable_ac_delays()->set_screen_lock_ms(660000); |
| 99 expected_policy.mutable_battery_delays()->set_screen_lock_ms(360000); |
| 100 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
| 101 PowerPolicyController::GetPolicyDebugString(last_policy_)); |
| 102 |
| 103 // If the screen-lock-delay prefs are set to lower values than the |
| 104 // screen-off delays, the lock prefs should take precedence. |
| 105 prefs.ac_screen_lock_delay_ms = 70000; |
| 106 prefs.battery_screen_lock_delay_ms = 60000; |
| 107 policy_controller_->ApplyPrefs(prefs); |
| 108 expected_policy.mutable_ac_delays()->set_screen_lock_ms(70000); |
| 109 expected_policy.mutable_battery_delays()->set_screen_lock_ms(60000); |
| 110 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
| 111 PowerPolicyController::GetPolicyDebugString(last_policy_)); |
| 112 } |
| 113 |
49 TEST_F(PowerPolicyControllerTest, Blocks) { | 114 TEST_F(PowerPolicyControllerTest, Blocks) { |
50 const char kSuspendBlockReason[] = "suspend"; | 115 const char kSuspendBlockReason[] = "suspend"; |
51 const int suspend_id = | 116 const int suspend_id = |
52 policy_controller_->AddSuspendBlock(kSuspendBlockReason); | 117 policy_controller_->AddSuspendBlock(kSuspendBlockReason); |
53 power_manager::PowerManagementPolicy expected_policy; | 118 power_manager::PowerManagementPolicy expected_policy; |
54 expected_policy.set_idle_action( | 119 expected_policy.set_idle_action( |
55 power_manager::PowerManagementPolicy_Action_DO_NOTHING); | 120 power_manager::PowerManagementPolicy_Action_DO_NOTHING); |
56 expected_policy.set_reason(kSuspendBlockReason); | 121 expected_policy.set_reason(kSuspendBlockReason); |
57 EXPECT_EQ(expected_policy.SerializeAsString(), | 122 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
58 last_policy_.SerializeAsString()); | 123 PowerPolicyController::GetPolicyDebugString(last_policy_)); |
59 | 124 |
60 const char kScreenBlockReason[] = "screen"; | 125 const char kScreenBlockReason[] = "screen"; |
61 const int screen_id = policy_controller_->AddScreenBlock(kScreenBlockReason); | 126 const int screen_id = policy_controller_->AddScreenBlock(kScreenBlockReason); |
62 expected_policy.mutable_ac_delays()->set_screen_dim_ms(0); | 127 expected_policy.mutable_ac_delays()->set_screen_dim_ms(0); |
63 expected_policy.mutable_ac_delays()->set_screen_off_ms(0); | 128 expected_policy.mutable_ac_delays()->set_screen_off_ms(0); |
64 expected_policy.mutable_battery_delays()->set_screen_dim_ms(0); | 129 expected_policy.mutable_battery_delays()->set_screen_dim_ms(0); |
65 expected_policy.mutable_battery_delays()->set_screen_off_ms(0); | 130 expected_policy.mutable_battery_delays()->set_screen_off_ms(0); |
66 expected_policy.set_reason( | 131 expected_policy.set_reason( |
67 std::string(kScreenBlockReason) + ", " + kSuspendBlockReason); | 132 std::string(kScreenBlockReason) + ", " + kSuspendBlockReason); |
68 EXPECT_EQ(expected_policy.SerializeAsString(), | 133 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
69 last_policy_.SerializeAsString()); | 134 PowerPolicyController::GetPolicyDebugString(last_policy_)); |
70 | 135 |
71 policy_controller_->RemoveBlock(suspend_id); | 136 policy_controller_->RemoveBlock(suspend_id); |
72 expected_policy.set_reason(kScreenBlockReason); | 137 expected_policy.set_reason(kScreenBlockReason); |
73 EXPECT_EQ(expected_policy.SerializeAsString(), | 138 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
74 last_policy_.SerializeAsString()); | 139 PowerPolicyController::GetPolicyDebugString(last_policy_)); |
75 | 140 |
76 policy_controller_->RemoveBlock(screen_id); | 141 policy_controller_->RemoveBlock(screen_id); |
77 expected_policy.Clear(); | 142 expected_policy.Clear(); |
78 EXPECT_EQ(expected_policy.SerializeAsString(), | 143 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
79 last_policy_.SerializeAsString()); | 144 PowerPolicyController::GetPolicyDebugString(last_policy_)); |
80 } | 145 } |
81 | 146 |
82 } // namespace chromeos | 147 } // namespace chromeos |
OLD | NEW |