blob: 39313e0c8d2bf0030c0277a914a0273587ae46ea [file] [log] [blame]
Gary Morain43bc6272012-01-30 14:01:15 -08001// 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 Chancd477322014-10-17 14:19:30 -07007#include <memory>
Gary Morain43bc6272012-01-30 14:01:15 -08008#include <string>
9
Eric Shienbrood3e20a232012-02-16 11:35:56 -050010#include <base/bind.h>
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070011#include <chromeos/dbus/service_constants.h>
Gary Morain43bc6272012-01-30 14:01:15 -080012#include <gmock/gmock.h>
13#include <gtest/gtest.h>
14
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070015#include "shill/dbus_manager.h"
16#include "shill/mock_dbus_service_proxy.h"
Darin Petkov3ec55342012-09-28 14:04:44 +020017#include "shill/mock_event_dispatcher.h"
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070018#include "shill/mock_metrics.h"
Gary Morain43bc6272012-01-30 14:01:15 -080019#include "shill/mock_power_manager_proxy.h"
20#include "shill/power_manager_proxy_interface.h"
21#include "shill/proxy_factory.h"
22
Eric Shienbrood3e20a232012-02-16 11:35:56 -050023using base::Bind;
24using base::Unretained;
Gary Morain52fabab2012-08-22 09:27:31 -070025using std::map;
Gary Morain43bc6272012-01-30 14:01:15 -080026using std::string;
27using testing::_;
Daniel Eratfac09532014-04-17 20:25:59 -070028using testing::DoAll;
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -070029using testing::IgnoreResult;
Prathmesh Prabhu0f4e0a42014-08-27 15:29:00 -070030using testing::InvokeWithoutArgs;
Daniel Eratfac09532014-04-17 20:25:59 -070031using testing::Mock;
Daniel Erat0818cca2012-12-14 10:16:21 -080032using testing::Return;
Daniel Eratfac09532014-04-17 20:25:59 -070033using testing::SetArgumentPointee;
Gary Morain43bc6272012-01-30 14:01:15 -080034using testing::Test;
35
36namespace shill {
37
Gary Morain43bc6272012-01-30 14:01:15 -080038namespace {
39
40class FakeProxyFactory : public ProxyFactory {
41 public:
Gary Morain52fabab2012-08-22 09:27:31 -070042 FakeProxyFactory()
Ben Chancc225ef2014-09-30 13:26:51 -070043 : delegate_(nullptr),
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070044 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 Morain43bc6272012-01-30 14:01:15 -080048
Paul Stewart3b30ca52015-06-16 13:13:10 -070049 virtual PowerManagerProxyInterface* CreatePowerManagerProxy(
50 PowerManagerProxyDelegate* delegate) {
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070051 CHECK(power_manager_proxy_);
Gary Morain43bc6272012-01-30 14:01:15 -080052 delegate_ = delegate;
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070053 // Passes ownership.
54 return power_manager_proxy_.release();
Gary Morain43bc6272012-01-30 14:01:15 -080055 }
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070056
Paul Stewart3b30ca52015-06-16 13:13:10 -070057 virtual DBusServiceProxyInterface* CreateDBusServiceProxy() {
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070058 CHECK(dbus_service_proxy_);
59 // Passes ownership.
60 return dbus_service_proxy_.release();
61 }
62
Paul Stewart3b30ca52015-06-16 13:13:10 -070063 PowerManagerProxyDelegate* delegate() const { return delegate_; }
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070064 // Can not guarantee that the returned object is alive.
Paul Stewart3b30ca52015-06-16 13:13:10 -070065 MockPowerManagerProxy* power_manager_proxy() const {
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070066 return power_manager_proxy_raw_;
67 }
68 // Can not guarantee that the returned object is alive.
Paul Stewart3b30ca52015-06-16 13:13:10 -070069 MockDBusServiceProxy* dbus_service_proxy() const {
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070070 return dbus_service_proxy_raw_;
71 }
Gary Morain43bc6272012-01-30 14:01:15 -080072
73 private:
Paul Stewart3b30ca52015-06-16 13:13:10 -070074 PowerManagerProxyDelegate* delegate_;
75 MockPowerManagerProxy* const power_manager_proxy_raw_;
76 MockDBusServiceProxy* const dbus_service_proxy_raw_;
Ben Chancd477322014-10-17 14:19:30 -070077 std::unique_ptr<MockPowerManagerProxy> power_manager_proxy_;
78 std::unique_ptr<MockDBusServiceProxy> dbus_service_proxy_;
Gary Morain43bc6272012-01-30 14:01:15 -080079};
80
81} // namespace
82
83class PowerManagerTest : public Test {
84 public:
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -070085 static const char kDescription[];
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -070086 static const char kDarkDescription[];
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070087 static const char kPowerManagerDefaultOwner[];
Daniel Erat0818cca2012-12-14 10:16:21 -080088 static const int kSuspendId1 = 123;
89 static const int kSuspendId2 = 456;
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -070090 static const int kDelayId = 4;
91 static const int kDelayId2 = 5;
Gary Morain52fabab2012-08-22 09:27:31 -070092
Gary Morain43bc6272012-01-30 14:01:15 -080093 PowerManagerTest()
Daniel Eratfac09532014-04-17 20:25:59 -070094 : kTimeout(base::TimeDelta::FromSeconds(3)),
95 power_manager_(&dispatcher_, &factory_),
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -070096 power_manager_proxy_(factory_.power_manager_proxy()),
Gary Morain52fabab2012-08-22 09:27:31 -070097 delegate_(factory_.delegate()) {
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -070098 suspend_imminent_callback_ =
99 Bind(&PowerManagerTest::SuspendImminentAction, Unretained(this));
100 suspend_done_callback_ =
101 Bind(&PowerManagerTest::SuspendDoneAction, Unretained(this));
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700102 dark_suspend_imminent_callback_ =
103 Bind(&PowerManagerTest::DarkSuspendImminentAction, Unretained(this));
Gary Morain52fabab2012-08-22 09:27:31 -0700104 }
Gary Morain43bc6272012-01-30 14:01:15 -0800105
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700106 MOCK_METHOD0(SuspendImminentAction, void());
107 MOCK_METHOD0(SuspendDoneAction, void());
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700108 MOCK_METHOD0(DarkSuspendImminentAction, void());
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500109
Gary Morain43bc6272012-01-30 14:01:15 -0800110 protected:
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700111 virtual void SetUp() {
112 dbus_manager_.proxy_factory_ = &factory_;
113 dbus_manager_.Start();
114
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700115 EXPECT_CALL(*factory_.dbus_service_proxy(),
Samuel Tan1897afa2015-05-21 14:21:56 -0700116 GetNameOwner(power_manager::kPowerManagerServiceName, _, _, _));
117 power_manager_.Start(&dbus_manager_, kTimeout, suspend_imminent_callback_,
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700118 suspend_done_callback_,
119 dark_suspend_imminent_callback_);
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700120 Mock::VerifyAndClearExpectations(factory_.dbus_service_proxy());
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700121 }
122
Samuel Tan1897afa2015-05-21 14:21:56 -0700123 virtual void TearDown() { dbus_manager_.Stop(); }
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700124
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700125 void AddProxyExpectationForRegisterSuspendDelay(int delay_id,
126 bool return_value) {
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700127 EXPECT_CALL(*power_manager_proxy_,
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700128 RegisterSuspendDelay(kTimeout, kDescription, _))
Samuel Tan1897afa2015-05-21 14:21:56 -0700129 .WillOnce(DoAll(SetArgumentPointee<2>(delay_id), Return(return_value)));
Daniel Eratfac09532014-04-17 20:25:59 -0700130 }
131
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700132 void AddProxyExpectationForUnregisterSuspendDelay(int delay_id,
133 bool return_value) {
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700134 EXPECT_CALL(*power_manager_proxy_, UnregisterSuspendDelay(delay_id))
Daniel Eratfac09532014-04-17 20:25:59 -0700135 .WillOnce(Return(return_value));
136 }
137
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700138 void AddProxyExpectationForReportSuspendReadiness(int delay_id,
139 int suspend_id,
140 bool return_value) {
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700141 EXPECT_CALL(*power_manager_proxy_,
142 ReportSuspendReadiness(delay_id, suspend_id))
Daniel Eratfac09532014-04-17 20:25:59 -0700143 .WillOnce(Return(return_value));
144 }
145
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700146 void AddProxyExpectationForRecordDarkResumeWakeReason(
Paul Stewart3b30ca52015-06-16 13:13:10 -0700147 const string& wake_reason, bool return_value) {
Samuel Tan1897afa2015-05-21 14:21:56 -0700148 EXPECT_CALL(*power_manager_proxy_, RecordDarkResumeWakeReason(wake_reason))
149 .WillOnce(Return(return_value));
150 }
151
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700152 void AddProxyExpectationForRegisterDarkSuspendDelay(int delay_id,
153 bool return_value) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700154 EXPECT_CALL(*power_manager_proxy_,
155 RegisterDarkSuspendDelay(kTimeout, kDarkDescription, _))
Samuel Tan1897afa2015-05-21 14:21:56 -0700156 .WillOnce(DoAll(SetArgumentPointee<2>(delay_id), Return(return_value)));
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700157 }
158
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700159 void AddProxyExpectationForReportDarkSuspendReadiness(int delay_id,
160 int suspend_id,
161 bool return_value) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700162 EXPECT_CALL(*power_manager_proxy_,
163 ReportDarkSuspendReadiness(delay_id, suspend_id))
164 .WillOnce(Return(return_value));
165 }
166
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700167 void AddProxyExpectationForUnregisterDarkSuspendDelay(int delay_id,
168 bool return_value) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700169 EXPECT_CALL(*power_manager_proxy_, UnregisterDarkSuspendDelay(delay_id))
170 .WillOnce(Return(return_value));
171 }
172
173 void RegisterSuspendDelays() {
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700174 AddProxyExpectationForRegisterSuspendDelay(kDelayId, true);
175 AddProxyExpectationForRegisterDarkSuspendDelay(kDelayId, true);
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700176 OnPowerManagerAppeared();
177 Mock::VerifyAndClearExpectations(power_manager_proxy_);
178 }
179
Daniel Erat0818cca2012-12-14 10:16:21 -0800180 void OnSuspendImminent(int suspend_id) {
Daniel Erat0818cca2012-12-14 10:16:21 -0800181 factory_.delegate()->OnSuspendImminent(suspend_id);
Daniel Eratfac09532014-04-17 20:25:59 -0700182 EXPECT_TRUE(power_manager_.suspending());
Darin Petkov3ec55342012-09-28 14:04:44 +0200183 }
184
Daniel Eratfac09532014-04-17 20:25:59 -0700185 void OnSuspendDone(int suspend_id) {
186 factory_.delegate()->OnSuspendDone(suspend_id);
187 EXPECT_FALSE(power_manager_.suspending());
Darin Petkov3ec55342012-09-28 14:04:44 +0200188 }
189
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700190 void OnDarkSuspendImminent(int suspend_id) {
191 factory_.delegate()->OnDarkSuspendImminent(suspend_id);
192 }
193
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700194 void OnPowerManagerAppeared() {
195 power_manager_.OnPowerManagerAppeared(
Samuel Tan1897afa2015-05-21 14:21:56 -0700196 power_manager::kPowerManagerServicePath, kPowerManagerDefaultOwner);
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700197 }
198
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700199 void OnPowerManagerVanished() {
200 power_manager_.OnPowerManagerVanished(
201 power_manager::kPowerManagerServicePath);
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700202 }
203
Daniel Eratfac09532014-04-17 20:25:59 -0700204 // This is non-static since it's a non-POD type.
205 const base::TimeDelta kTimeout;
206
Darin Petkov3ec55342012-09-28 14:04:44 +0200207 MockEventDispatcher dispatcher_;
Gary Morain43bc6272012-01-30 14:01:15 -0800208 FakeProxyFactory factory_;
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700209 DBusManager dbus_manager_;
Gary Morain43bc6272012-01-30 14:01:15 -0800210 PowerManager power_manager_;
Paul Stewart3b30ca52015-06-16 13:13:10 -0700211 MockPowerManagerProxy* const power_manager_proxy_;
212 PowerManagerProxyDelegate* const delegate_;
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700213 PowerManager::SuspendImminentCallback suspend_imminent_callback_;
214 PowerManager::SuspendDoneCallback suspend_done_callback_;
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700215 PowerManager::DarkSuspendImminentCallback dark_suspend_imminent_callback_;
Gary Morain43bc6272012-01-30 14:01:15 -0800216};
217
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700218const char PowerManagerTest::kDescription[] = "shill";
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700219const char PowerManagerTest::kDarkDescription[] = "shill";
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700220const char PowerManagerTest::kPowerManagerDefaultOwner[] =
221 "PowerManagerDefaultOwner";
Gary Morain52fabab2012-08-22 09:27:31 -0700222
Daniel Eratfac09532014-04-17 20:25:59 -0700223TEST_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 Petkovca621542012-07-25 14:25:56 +0200230}
231
Daniel Eratfac09532014-04-17 20:25:59 -0700232TEST_F(PowerManagerTest, RegisterSuspendDelayFailure) {
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700233 AddProxyExpectationForRegisterSuspendDelay(kDelayId, false);
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700234 OnPowerManagerAppeared();
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700235 Mock::VerifyAndClearExpectations(power_manager_proxy_);
Gary Morain52fabab2012-08-22 09:27:31 -0700236
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700237 // 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 Erat0818cca2012-12-14 10:16:21 -0800250 OnSuspendImminent(kSuspendId1);
Daniel Eratfac09532014-04-17 20:25:59 -0700251 OnSuspendDone(kSuspendId1);
252 Mock::VerifyAndClearExpectations(this);
253}
Gary Morain52fabab2012-08-22 09:27:31 -0700254
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700255TEST_F(PowerManagerTest, RegisterDarkSuspendDelayFailure) {
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700256 AddProxyExpectationForRegisterDarkSuspendDelay(kDelayId, false);
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700257 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 Eratfac09532014-04-17 20:25:59 -0700267TEST_F(PowerManagerTest, ReportSuspendReadinessFailure) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700268 RegisterSuspendDelays();
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700269 EXPECT_CALL(*this, SuspendImminentAction());
Daniel Erat0818cca2012-12-14 10:16:21 -0800270 OnSuspendImminent(kSuspendId1);
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700271 AddProxyExpectationForReportSuspendReadiness(kDelayId, kSuspendId1, false);
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700272 EXPECT_FALSE(power_manager_.ReportSuspendReadiness());
Gary Morain43bc6272012-01-30 14:01:15 -0800273}
274
Samuel Tan1897afa2015-05-21 14:21:56 -0700275TEST_F(PowerManagerTest, RecordDarkResumeWakeReasonFailure) {
276 const string kWakeReason = "WiFi.Disconnect";
277 RegisterSuspendDelays();
278 EXPECT_CALL(*this, DarkSuspendImminentAction());
279 OnDarkSuspendImminent(kSuspendId1);
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700280 AddProxyExpectationForRecordDarkResumeWakeReason(kWakeReason, false);
Samuel Tan1897afa2015-05-21 14:21:56 -0700281 EXPECT_FALSE(power_manager_.RecordDarkResumeWakeReason(kWakeReason));
282}
283
284TEST_F(PowerManagerTest, RecordDarkResumeWakeReasonSuccess) {
285 const string kWakeReason = "WiFi.Disconnect";
286 RegisterSuspendDelays();
287 EXPECT_CALL(*this, DarkSuspendImminentAction());
288 OnDarkSuspendImminent(kSuspendId1);
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700289 AddProxyExpectationForRecordDarkResumeWakeReason(kWakeReason, true);
Samuel Tan1897afa2015-05-21 14:21:56 -0700290 EXPECT_TRUE(power_manager_.RecordDarkResumeWakeReason(kWakeReason));
291}
292
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700293TEST_F(PowerManagerTest, ReportDarkSuspendReadinessFailure) {
294 RegisterSuspendDelays();
295 EXPECT_CALL(*this, DarkSuspendImminentAction());
296 OnDarkSuspendImminent(kSuspendId1);
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700297 AddProxyExpectationForReportDarkSuspendReadiness(kDelayId, kSuspendId1,
298 false);
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700299 EXPECT_FALSE(power_manager_.ReportDarkSuspendReadiness());
300}
301
Prathmesh Prabhu0f4e0a42014-08-27 15:29:00 -0700302TEST_F(PowerManagerTest, ReportSuspendReadinessFailsOutsideSuspend) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700303 RegisterSuspendDelays();
Samuel Tan1897afa2015-05-21 14:21:56 -0700304 EXPECT_CALL(*power_manager_proxy_, ReportSuspendReadiness(_, _)).Times(0);
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700305 EXPECT_FALSE(power_manager_.ReportSuspendReadiness());
306}
307
Prathmesh Prabhu0f4e0a42014-08-27 15:29:00 -0700308TEST_F(PowerManagerTest, ReportSuspendReadinessSynchronous) {
309 // Verifies that a synchronous ReportSuspendReadiness call by shill on a
310 // SuspendImminent callback is routed back to powerd.
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700311 RegisterSuspendDelays();
Samuel Tan1897afa2015-05-21 14:21:56 -0700312 EXPECT_CALL(*power_manager_proxy_, ReportSuspendReadiness(_, _))
Prathmesh Prabhu0f4e0a42014-08-27 15:29:00 -0700313 .WillOnce(Return(true));
314 EXPECT_CALL(*this, SuspendImminentAction())
Samuel Tan1897afa2015-05-21 14:21:56 -0700315 .WillOnce(IgnoreResult(InvokeWithoutArgs(
316 &power_manager_, &PowerManager::ReportSuspendReadiness)));
Prathmesh Prabhu0f4e0a42014-08-27 15:29:00 -0700317 OnSuspendImminent(kSuspendId1);
318}
319
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700320TEST_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 Tan1897afa2015-05-21 14:21:56 -0700324 EXPECT_CALL(*power_manager_proxy_, ReportDarkSuspendReadiness(_, _))
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700325 .WillOnce(Return(true));
326 EXPECT_CALL(*this, DarkSuspendImminentAction())
Samuel Tan1897afa2015-05-21 14:21:56 -0700327 .WillOnce(IgnoreResult(InvokeWithoutArgs(
328 &power_manager_, &PowerManager::ReportDarkSuspendReadiness)));
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700329 OnDarkSuspendImminent(kSuspendId1);
330}
331
332TEST_F(PowerManagerTest, Stop) {
333 RegisterSuspendDelays();
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700334 AddProxyExpectationForUnregisterSuspendDelay(kDelayId, true);
335 AddProxyExpectationForUnregisterDarkSuspendDelay(kDelayId, true);
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700336 power_manager_.Stop();
337}
338
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700339TEST_F(PowerManagerTest, StopFailure) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700340 RegisterSuspendDelays();
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700341
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700342 AddProxyExpectationForUnregisterSuspendDelay(kDelayId, false);
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700343 power_manager_.Stop();
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700344 Mock::VerifyAndClearExpectations(power_manager_proxy_);
Gary Morain43bc6272012-01-30 14:01:15 -0800345
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700346 // As a result, callbacks should still be invoked.
347 EXPECT_CALL(*this, SuspendImminentAction());
348 EXPECT_CALL(*this, SuspendDoneAction());
Daniel Eratfac09532014-04-17 20:25:59 -0700349 OnSuspendImminent(kSuspendId1);
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700350 OnSuspendDone(kSuspendId1);
Darin Petkov3ec55342012-09-28 14:04:44 +0200351}
352
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700353TEST_F(PowerManagerTest, OnPowerManagerReappeared) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700354 RegisterSuspendDelays();
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700355
356 // Check that we re-register suspend delay on powerd restart.
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700357 AddProxyExpectationForRegisterSuspendDelay(kDelayId2, true);
358 AddProxyExpectationForRegisterDarkSuspendDelay(kDelayId2, true);
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700359 OnPowerManagerVanished();
360 OnPowerManagerAppeared();
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700361 Mock::VerifyAndClearExpectations(power_manager_proxy_);
362
363 // Check that a |ReportSuspendReadiness| message is sent with the new delay
364 // id.
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700365 EXPECT_CALL(*this, SuspendImminentAction());
366 OnSuspendImminent(kSuspendId1);
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700367 AddProxyExpectationForReportSuspendReadiness(kDelayId2, kSuspendId1, true);
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700368 EXPECT_TRUE(power_manager_.ReportSuspendReadiness());
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700369 Mock::VerifyAndClearExpectations(power_manager_proxy_);
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700370
371 // Check that a |ReportDarkSuspendReadiness| message is sent with the new
372 // delay id.
373 EXPECT_CALL(*this, DarkSuspendImminentAction());
374 OnDarkSuspendImminent(kSuspendId1);
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700375 AddProxyExpectationForReportDarkSuspendReadiness(kDelayId2, kSuspendId1,
376 true);
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700377 EXPECT_TRUE(power_manager_.ReportDarkSuspendReadiness());
Prathmesh Prabhud9c786c2014-04-23 17:37:28 -0700378}
379
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700380TEST_F(PowerManagerTest, PowerManagerDiedInSuspend) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700381 RegisterSuspendDelays();
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700382 EXPECT_CALL(*this, SuspendImminentAction());
383 OnSuspendImminent(kSuspendId1);
384 Mock::VerifyAndClearExpectations(this);
385
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700386 EXPECT_CALL(*this, SuspendDoneAction());
Prathmesh Prabhu0f4e0a42014-08-27 15:29:00 -0700387 OnPowerManagerVanished();
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700388 EXPECT_FALSE(power_manager_.suspending());
389}
390
391TEST_F(PowerManagerTest, PowerManagerReappearedInSuspend) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700392 RegisterSuspendDelays();
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700393 EXPECT_CALL(*this, SuspendImminentAction());
394 OnSuspendImminent(kSuspendId1);
395 Mock::VerifyAndClearExpectations(this);
396
Samuel Tanf1e31ac2015-05-21 14:23:40 -0700397 AddProxyExpectationForRegisterSuspendDelay(kDelayId2, true);
398 AddProxyExpectationForRegisterDarkSuspendDelay(kDelayId2, true);
Prathmesh Prabhu0f4e0a42014-08-27 15:29:00 -0700399 EXPECT_CALL(*this, SuspendDoneAction());
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700400 OnPowerManagerVanished();
401 OnPowerManagerAppeared();
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700402 EXPECT_FALSE(power_manager_.suspending());
Prathmesh Prabhu0f4e0a42014-08-27 15:29:00 -0700403 Mock::VerifyAndClearExpectations(this);
404
405 // Let's check a normal suspend request after the fact.
406 EXPECT_CALL(*this, SuspendImminentAction());
407 OnSuspendImminent(kSuspendId2);
Prathmesh Prabhu9fdb84b2014-08-21 18:41:58 -0700408}
409
Gary Morain43bc6272012-01-30 14:01:15 -0800410} // namespace shill