Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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 "shill/power_manager.h" |
| 6 | |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 7 | #include <memory> |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 10 | #include <base/bind.h> |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 11 | #include <chromeos/dbus/service_constants.h> |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 12 | #include <gmock/gmock.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 15 | #include "shill/dbus_manager.h" |
| 16 | #include "shill/mock_dbus_service_proxy.h" |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 17 | #include "shill/mock_event_dispatcher.h" |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 18 | #include "shill/mock_metrics.h" |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 19 | #include "shill/mock_power_manager_proxy.h" |
| 20 | #include "shill/power_manager_proxy_interface.h" |
| 21 | #include "shill/proxy_factory.h" |
| 22 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 23 | using base::Bind; |
| 24 | using base::Unretained; |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 25 | using std::map; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 26 | using std::string; |
| 27 | using testing::_; |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 28 | using testing::DoAll; |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 29 | using testing::IgnoreResult; |
Prathmesh Prabhu | 0f4e0a4 | 2014-08-27 15:29:00 -0700 | [diff] [blame] | 30 | using testing::InvokeWithoutArgs; |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 31 | using testing::Mock; |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 32 | using testing::Return; |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 33 | using testing::SetArgumentPointee; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 34 | using testing::Test; |
| 35 | |
| 36 | namespace shill { |
| 37 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 38 | namespace { |
| 39 | |
| 40 | class FakeProxyFactory : public ProxyFactory { |
| 41 | public: |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 42 | FakeProxyFactory() |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 43 | : delegate_(nullptr), |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 44 | power_manager_proxy_raw_(new MockPowerManagerProxy), |
| 45 | dbus_service_proxy_raw_(new MockDBusServiceProxy), |
| 46 | power_manager_proxy_(power_manager_proxy_raw_), |
| 47 | dbus_service_proxy_(dbus_service_proxy_raw_) {} |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 48 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 49 | virtual PowerManagerProxyInterface* CreatePowerManagerProxy( |
| 50 | PowerManagerProxyDelegate* delegate) { |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 51 | CHECK(power_manager_proxy_); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 52 | delegate_ = delegate; |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 53 | // Passes ownership. |
| 54 | return power_manager_proxy_.release(); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 55 | } |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 56 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 57 | virtual DBusServiceProxyInterface* CreateDBusServiceProxy() { |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 58 | CHECK(dbus_service_proxy_); |
| 59 | // Passes ownership. |
| 60 | return dbus_service_proxy_.release(); |
| 61 | } |
| 62 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 63 | PowerManagerProxyDelegate* delegate() const { return delegate_; } |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 64 | // Can not guarantee that the returned object is alive. |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 65 | MockPowerManagerProxy* power_manager_proxy() const { |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 66 | return power_manager_proxy_raw_; |
| 67 | } |
| 68 | // Can not guarantee that the returned object is alive. |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 69 | MockDBusServiceProxy* dbus_service_proxy() const { |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 70 | return dbus_service_proxy_raw_; |
| 71 | } |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 72 | |
| 73 | private: |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 74 | PowerManagerProxyDelegate* delegate_; |
| 75 | MockPowerManagerProxy* const power_manager_proxy_raw_; |
| 76 | MockDBusServiceProxy* const dbus_service_proxy_raw_; |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 77 | std::unique_ptr<MockPowerManagerProxy> power_manager_proxy_; |
| 78 | std::unique_ptr<MockDBusServiceProxy> dbus_service_proxy_; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | } // namespace |
| 82 | |
| 83 | class PowerManagerTest : public Test { |
| 84 | public: |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 85 | static const char kDescription[]; |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 86 | static const char kDarkDescription[]; |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 87 | static const char kPowerManagerDefaultOwner[]; |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 88 | static const int kSuspendId1 = 123; |
| 89 | static const int kSuspendId2 = 456; |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 90 | static const int kDelayId = 4; |
| 91 | static const int kDelayId2 = 5; |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 92 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 93 | PowerManagerTest() |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 94 | : kTimeout(base::TimeDelta::FromSeconds(3)), |
| 95 | power_manager_(&dispatcher_, &factory_), |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 96 | power_manager_proxy_(factory_.power_manager_proxy()), |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 97 | delegate_(factory_.delegate()) { |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 98 | suspend_imminent_callback_ = |
| 99 | Bind(&PowerManagerTest::SuspendImminentAction, Unretained(this)); |
| 100 | suspend_done_callback_ = |
| 101 | Bind(&PowerManagerTest::SuspendDoneAction, Unretained(this)); |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 102 | dark_suspend_imminent_callback_ = |
| 103 | Bind(&PowerManagerTest::DarkSuspendImminentAction, Unretained(this)); |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 104 | } |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 105 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 106 | MOCK_METHOD0(SuspendImminentAction, void()); |
| 107 | MOCK_METHOD0(SuspendDoneAction, void()); |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 108 | MOCK_METHOD0(DarkSuspendImminentAction, void()); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 109 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 110 | protected: |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 111 | virtual void SetUp() { |
| 112 | dbus_manager_.proxy_factory_ = &factory_; |
| 113 | dbus_manager_.Start(); |
| 114 | |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 115 | EXPECT_CALL(*factory_.dbus_service_proxy(), |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 116 | GetNameOwner(power_manager::kPowerManagerServiceName, _, _, _)); |
| 117 | power_manager_.Start(&dbus_manager_, kTimeout, suspend_imminent_callback_, |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 118 | suspend_done_callback_, |
| 119 | dark_suspend_imminent_callback_); |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 120 | Mock::VerifyAndClearExpectations(factory_.dbus_service_proxy()); |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 123 | virtual void TearDown() { dbus_manager_.Stop(); } |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 124 | |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 125 | void AddProxyExpectationForRegisterSuspendDelay(int delay_id, |
| 126 | bool return_value) { |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 127 | EXPECT_CALL(*power_manager_proxy_, |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 128 | RegisterSuspendDelay(kTimeout, kDescription, _)) |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 129 | .WillOnce(DoAll(SetArgumentPointee<2>(delay_id), Return(return_value))); |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 132 | void AddProxyExpectationForUnregisterSuspendDelay(int delay_id, |
| 133 | bool return_value) { |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 134 | EXPECT_CALL(*power_manager_proxy_, UnregisterSuspendDelay(delay_id)) |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 135 | .WillOnce(Return(return_value)); |
| 136 | } |
| 137 | |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 138 | void AddProxyExpectationForReportSuspendReadiness(int delay_id, |
| 139 | int suspend_id, |
| 140 | bool return_value) { |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 141 | EXPECT_CALL(*power_manager_proxy_, |
| 142 | ReportSuspendReadiness(delay_id, suspend_id)) |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 143 | .WillOnce(Return(return_value)); |
| 144 | } |
| 145 | |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 146 | void AddProxyExpectationForRecordDarkResumeWakeReason( |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 147 | const string& wake_reason, bool return_value) { |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 148 | EXPECT_CALL(*power_manager_proxy_, RecordDarkResumeWakeReason(wake_reason)) |
| 149 | .WillOnce(Return(return_value)); |
| 150 | } |
| 151 | |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 152 | void AddProxyExpectationForRegisterDarkSuspendDelay(int delay_id, |
| 153 | bool return_value) { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 154 | EXPECT_CALL(*power_manager_proxy_, |
| 155 | RegisterDarkSuspendDelay(kTimeout, kDarkDescription, _)) |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 156 | .WillOnce(DoAll(SetArgumentPointee<2>(delay_id), Return(return_value))); |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 159 | void AddProxyExpectationForReportDarkSuspendReadiness(int delay_id, |
| 160 | int suspend_id, |
| 161 | bool return_value) { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 162 | EXPECT_CALL(*power_manager_proxy_, |
| 163 | ReportDarkSuspendReadiness(delay_id, suspend_id)) |
| 164 | .WillOnce(Return(return_value)); |
| 165 | } |
| 166 | |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 167 | void AddProxyExpectationForUnregisterDarkSuspendDelay(int delay_id, |
| 168 | bool return_value) { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 169 | EXPECT_CALL(*power_manager_proxy_, UnregisterDarkSuspendDelay(delay_id)) |
| 170 | .WillOnce(Return(return_value)); |
| 171 | } |
| 172 | |
| 173 | void RegisterSuspendDelays() { |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 174 | AddProxyExpectationForRegisterSuspendDelay(kDelayId, true); |
| 175 | AddProxyExpectationForRegisterDarkSuspendDelay(kDelayId, true); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 176 | OnPowerManagerAppeared(); |
| 177 | Mock::VerifyAndClearExpectations(power_manager_proxy_); |
| 178 | } |
| 179 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 180 | void OnSuspendImminent(int suspend_id) { |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 181 | factory_.delegate()->OnSuspendImminent(suspend_id); |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 182 | EXPECT_TRUE(power_manager_.suspending()); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 185 | void OnSuspendDone(int suspend_id) { |
| 186 | factory_.delegate()->OnSuspendDone(suspend_id); |
| 187 | EXPECT_FALSE(power_manager_.suspending()); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 190 | void OnDarkSuspendImminent(int suspend_id) { |
| 191 | factory_.delegate()->OnDarkSuspendImminent(suspend_id); |
| 192 | } |
| 193 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 194 | void OnPowerManagerAppeared() { |
| 195 | power_manager_.OnPowerManagerAppeared( |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 196 | power_manager::kPowerManagerServicePath, kPowerManagerDefaultOwner); |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 199 | void OnPowerManagerVanished() { |
| 200 | power_manager_.OnPowerManagerVanished( |
| 201 | power_manager::kPowerManagerServicePath); |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 204 | // This is non-static since it's a non-POD type. |
| 205 | const base::TimeDelta kTimeout; |
| 206 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 207 | MockEventDispatcher dispatcher_; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 208 | FakeProxyFactory factory_; |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 209 | DBusManager dbus_manager_; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 210 | PowerManager power_manager_; |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 211 | MockPowerManagerProxy* const power_manager_proxy_; |
| 212 | PowerManagerProxyDelegate* const delegate_; |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 213 | PowerManager::SuspendImminentCallback suspend_imminent_callback_; |
| 214 | PowerManager::SuspendDoneCallback suspend_done_callback_; |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 215 | PowerManager::DarkSuspendImminentCallback dark_suspend_imminent_callback_; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 216 | }; |
| 217 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 218 | const char PowerManagerTest::kDescription[] = "shill"; |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 219 | const char PowerManagerTest::kDarkDescription[] = "shill"; |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 220 | const char PowerManagerTest::kPowerManagerDefaultOwner[] = |
| 221 | "PowerManagerDefaultOwner"; |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 222 | |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 223 | TEST_F(PowerManagerTest, SuspendingState) { |
| 224 | const int kSuspendId = 3; |
| 225 | EXPECT_FALSE(power_manager_.suspending()); |
| 226 | OnSuspendImminent(kSuspendId); |
| 227 | EXPECT_TRUE(power_manager_.suspending()); |
| 228 | OnSuspendDone(kSuspendId); |
| 229 | EXPECT_FALSE(power_manager_.suspending()); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 232 | TEST_F(PowerManagerTest, RegisterSuspendDelayFailure) { |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 233 | AddProxyExpectationForRegisterSuspendDelay(kDelayId, false); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 234 | OnPowerManagerAppeared(); |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 235 | Mock::VerifyAndClearExpectations(power_manager_proxy_); |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 236 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 237 | // Outstanding shill callbacks should still be invoked. |
| 238 | // - suspend_done_callback: If powerd died in the middle of a suspend |
| 239 | // we want to wake shill up with suspend_done_action, so this callback |
| 240 | // should be invoked anyway. |
| 241 | // See PowerManagerTest::PowerManagerDiedInSuspend and |
| 242 | // PowerManagerTest::PowerManagerReappearedInSuspend. |
| 243 | EXPECT_CALL(*this, SuspendDoneAction()); |
| 244 | // - suspend_imminent_callback: The only case this can happen is if this |
| 245 | // callback was put on the queue, and then powerd reappeared, but we failed |
| 246 | // to registered a suspend delay with it. |
| 247 | // It is safe to go through the suspend_imminent -> timeout -> suspend_done |
| 248 | // path in this black swan case. |
| 249 | EXPECT_CALL(*this, SuspendImminentAction()); |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 250 | OnSuspendImminent(kSuspendId1); |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 251 | OnSuspendDone(kSuspendId1); |
| 252 | Mock::VerifyAndClearExpectations(this); |
| 253 | } |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 254 | |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 255 | TEST_F(PowerManagerTest, RegisterDarkSuspendDelayFailure) { |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 256 | AddProxyExpectationForRegisterDarkSuspendDelay(kDelayId, false); |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 257 | OnPowerManagerAppeared(); |
| 258 | Mock::VerifyAndClearExpectations(power_manager_proxy_); |
| 259 | |
| 260 | // Outstanding dark suspend imminent signal should be ignored, since we |
| 261 | // probably won't have time to cleanly do dark resume actions. Might as well |
| 262 | // ignore the signal. |
| 263 | EXPECT_CALL(*this, DarkSuspendImminentAction()).Times(0); |
| 264 | OnDarkSuspendImminent(kSuspendId1); |
| 265 | } |
| 266 | |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 267 | TEST_F(PowerManagerTest, ReportSuspendReadinessFailure) { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 268 | RegisterSuspendDelays(); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 269 | EXPECT_CALL(*this, SuspendImminentAction()); |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 270 | OnSuspendImminent(kSuspendId1); |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 271 | AddProxyExpectationForReportSuspendReadiness(kDelayId, kSuspendId1, false); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 272 | EXPECT_FALSE(power_manager_.ReportSuspendReadiness()); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 275 | TEST_F(PowerManagerTest, RecordDarkResumeWakeReasonFailure) { |
| 276 | const string kWakeReason = "WiFi.Disconnect"; |
| 277 | RegisterSuspendDelays(); |
| 278 | EXPECT_CALL(*this, DarkSuspendImminentAction()); |
| 279 | OnDarkSuspendImminent(kSuspendId1); |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 280 | AddProxyExpectationForRecordDarkResumeWakeReason(kWakeReason, false); |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 281 | EXPECT_FALSE(power_manager_.RecordDarkResumeWakeReason(kWakeReason)); |
| 282 | } |
| 283 | |
| 284 | TEST_F(PowerManagerTest, RecordDarkResumeWakeReasonSuccess) { |
| 285 | const string kWakeReason = "WiFi.Disconnect"; |
| 286 | RegisterSuspendDelays(); |
| 287 | EXPECT_CALL(*this, DarkSuspendImminentAction()); |
| 288 | OnDarkSuspendImminent(kSuspendId1); |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 289 | AddProxyExpectationForRecordDarkResumeWakeReason(kWakeReason, true); |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 290 | EXPECT_TRUE(power_manager_.RecordDarkResumeWakeReason(kWakeReason)); |
| 291 | } |
| 292 | |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 293 | TEST_F(PowerManagerTest, ReportDarkSuspendReadinessFailure) { |
| 294 | RegisterSuspendDelays(); |
| 295 | EXPECT_CALL(*this, DarkSuspendImminentAction()); |
| 296 | OnDarkSuspendImminent(kSuspendId1); |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 297 | AddProxyExpectationForReportDarkSuspendReadiness(kDelayId, kSuspendId1, |
| 298 | false); |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 299 | EXPECT_FALSE(power_manager_.ReportDarkSuspendReadiness()); |
| 300 | } |
| 301 | |
Prathmesh Prabhu | 0f4e0a4 | 2014-08-27 15:29:00 -0700 | [diff] [blame] | 302 | TEST_F(PowerManagerTest, ReportSuspendReadinessFailsOutsideSuspend) { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 303 | RegisterSuspendDelays(); |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 304 | EXPECT_CALL(*power_manager_proxy_, ReportSuspendReadiness(_, _)).Times(0); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 305 | EXPECT_FALSE(power_manager_.ReportSuspendReadiness()); |
| 306 | } |
| 307 | |
Prathmesh Prabhu | 0f4e0a4 | 2014-08-27 15:29:00 -0700 | [diff] [blame] | 308 | TEST_F(PowerManagerTest, ReportSuspendReadinessSynchronous) { |
| 309 | // Verifies that a synchronous ReportSuspendReadiness call by shill on a |
| 310 | // SuspendImminent callback is routed back to powerd. |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 311 | RegisterSuspendDelays(); |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 312 | EXPECT_CALL(*power_manager_proxy_, ReportSuspendReadiness(_, _)) |
Prathmesh Prabhu | 0f4e0a4 | 2014-08-27 15:29:00 -0700 | [diff] [blame] | 313 | .WillOnce(Return(true)); |
| 314 | EXPECT_CALL(*this, SuspendImminentAction()) |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 315 | .WillOnce(IgnoreResult(InvokeWithoutArgs( |
| 316 | &power_manager_, &PowerManager::ReportSuspendReadiness))); |
Prathmesh Prabhu | 0f4e0a4 | 2014-08-27 15:29:00 -0700 | [diff] [blame] | 317 | OnSuspendImminent(kSuspendId1); |
| 318 | } |
| 319 | |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 320 | TEST_F(PowerManagerTest, ReportDarkSuspendReadinessSynchronous) { |
| 321 | // Verifies that a synchronous ReportDarkSuspendReadiness call by shill on a |
| 322 | // DarkSuspendImminent callback is routed back to powerd. |
| 323 | RegisterSuspendDelays(); |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 324 | EXPECT_CALL(*power_manager_proxy_, ReportDarkSuspendReadiness(_, _)) |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 325 | .WillOnce(Return(true)); |
| 326 | EXPECT_CALL(*this, DarkSuspendImminentAction()) |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 327 | .WillOnce(IgnoreResult(InvokeWithoutArgs( |
| 328 | &power_manager_, &PowerManager::ReportDarkSuspendReadiness))); |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 329 | OnDarkSuspendImminent(kSuspendId1); |
| 330 | } |
| 331 | |
| 332 | TEST_F(PowerManagerTest, Stop) { |
| 333 | RegisterSuspendDelays(); |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 334 | AddProxyExpectationForUnregisterSuspendDelay(kDelayId, true); |
| 335 | AddProxyExpectationForUnregisterDarkSuspendDelay(kDelayId, true); |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 336 | power_manager_.Stop(); |
| 337 | } |
| 338 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 339 | TEST_F(PowerManagerTest, StopFailure) { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 340 | RegisterSuspendDelays(); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 341 | |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 342 | AddProxyExpectationForUnregisterSuspendDelay(kDelayId, false); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 343 | power_manager_.Stop(); |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 344 | Mock::VerifyAndClearExpectations(power_manager_proxy_); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 345 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 346 | // As a result, callbacks should still be invoked. |
| 347 | EXPECT_CALL(*this, SuspendImminentAction()); |
| 348 | EXPECT_CALL(*this, SuspendDoneAction()); |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 349 | OnSuspendImminent(kSuspendId1); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 350 | OnSuspendDone(kSuspendId1); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 353 | TEST_F(PowerManagerTest, OnPowerManagerReappeared) { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 354 | RegisterSuspendDelays(); |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 355 | |
| 356 | // Check that we re-register suspend delay on powerd restart. |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 357 | AddProxyExpectationForRegisterSuspendDelay(kDelayId2, true); |
| 358 | AddProxyExpectationForRegisterDarkSuspendDelay(kDelayId2, true); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 359 | OnPowerManagerVanished(); |
| 360 | OnPowerManagerAppeared(); |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 361 | Mock::VerifyAndClearExpectations(power_manager_proxy_); |
| 362 | |
| 363 | // Check that a |ReportSuspendReadiness| message is sent with the new delay |
| 364 | // id. |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 365 | EXPECT_CALL(*this, SuspendImminentAction()); |
| 366 | OnSuspendImminent(kSuspendId1); |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 367 | AddProxyExpectationForReportSuspendReadiness(kDelayId2, kSuspendId1, true); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 368 | EXPECT_TRUE(power_manager_.ReportSuspendReadiness()); |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 369 | Mock::VerifyAndClearExpectations(power_manager_proxy_); |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 370 | |
| 371 | // Check that a |ReportDarkSuspendReadiness| message is sent with the new |
| 372 | // delay id. |
| 373 | EXPECT_CALL(*this, DarkSuspendImminentAction()); |
| 374 | OnDarkSuspendImminent(kSuspendId1); |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 375 | AddProxyExpectationForReportDarkSuspendReadiness(kDelayId2, kSuspendId1, |
| 376 | true); |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 377 | EXPECT_TRUE(power_manager_.ReportDarkSuspendReadiness()); |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 380 | TEST_F(PowerManagerTest, PowerManagerDiedInSuspend) { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 381 | RegisterSuspendDelays(); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 382 | EXPECT_CALL(*this, SuspendImminentAction()); |
| 383 | OnSuspendImminent(kSuspendId1); |
| 384 | Mock::VerifyAndClearExpectations(this); |
| 385 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 386 | EXPECT_CALL(*this, SuspendDoneAction()); |
Prathmesh Prabhu | 0f4e0a4 | 2014-08-27 15:29:00 -0700 | [diff] [blame] | 387 | OnPowerManagerVanished(); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 388 | EXPECT_FALSE(power_manager_.suspending()); |
| 389 | } |
| 390 | |
| 391 | TEST_F(PowerManagerTest, PowerManagerReappearedInSuspend) { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 392 | RegisterSuspendDelays(); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 393 | EXPECT_CALL(*this, SuspendImminentAction()); |
| 394 | OnSuspendImminent(kSuspendId1); |
| 395 | Mock::VerifyAndClearExpectations(this); |
| 396 | |
Samuel Tan | f1e31ac | 2015-05-21 14:23:40 -0700 | [diff] [blame] | 397 | AddProxyExpectationForRegisterSuspendDelay(kDelayId2, true); |
| 398 | AddProxyExpectationForRegisterDarkSuspendDelay(kDelayId2, true); |
Prathmesh Prabhu | 0f4e0a4 | 2014-08-27 15:29:00 -0700 | [diff] [blame] | 399 | EXPECT_CALL(*this, SuspendDoneAction()); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 400 | OnPowerManagerVanished(); |
| 401 | OnPowerManagerAppeared(); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 402 | EXPECT_FALSE(power_manager_.suspending()); |
Prathmesh Prabhu | 0f4e0a4 | 2014-08-27 15:29:00 -0700 | [diff] [blame] | 403 | Mock::VerifyAndClearExpectations(this); |
| 404 | |
| 405 | // Let's check a normal suspend request after the fact. |
| 406 | EXPECT_CALL(*this, SuspendImminentAction()); |
| 407 | OnSuspendImminent(kSuspendId2); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 410 | } // namespace shill |