1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "BatteryDefender.h"
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include <utils/Timers.h>
23
24 #include <android-base/file.h>
25 #include <android-base/properties.h>
26
27 #define MIN_TIME_BETWEEN_FILE_UPDATES (WRITE_DELAY_SECS + 1)
28
29 class HealthInterface {
30 public:
~HealthInterface()31 virtual ~HealthInterface() {}
32 virtual bool ReadFileToString(const std::string &path, std::string *content,
33 bool follow_symlinks);
34 virtual int GetIntProperty(const std::string &key, int default_value, int min, int max);
35 virtual bool GetBoolProperty(const std::string &key, bool default_value);
36 virtual bool SetProperty(const std::string &key, const std::string &value);
37 virtual bool WriteStringToFile(const std::string &content, const std::string &path,
38 bool follow_symlinks);
39 };
40
41 class HealthInterfaceMock : public HealthInterface {
42 public:
~HealthInterfaceMock()43 virtual ~HealthInterfaceMock() {}
44
45 MOCK_METHOD3(ReadFileToString,
46 bool(const std::string &path, std::string *content, bool follow_symlinks));
47 MOCK_METHOD4(GetIntProperty, int(const std::string &key, int default_value, int min, int max));
48 MOCK_METHOD2(GetBoolProperty, bool(const std::string &key, bool default_value));
49 MOCK_METHOD2(SetProperty, bool(const std::string &key, const std::string &value));
50 MOCK_METHOD3(WriteStringToFile,
51 bool(const std::string &content, const std::string &path, bool follow_symlinks));
52 };
53
54 HealthInterfaceMock *mock;
55
56 namespace android {
57 namespace base {
58
ReadFileToString(const std::string & path,std::string * content,bool follow_symlinks)59 bool ReadFileToString(const std::string &path, std::string *content, bool follow_symlinks) {
60 return mock->ReadFileToString(path, content, follow_symlinks);
61 }
62
WriteStringToFile(const std::string & content,const std::string & path,bool follow_symlinks)63 bool WriteStringToFile(const std::string &content, const std::string &path, bool follow_symlinks) {
64 return mock->WriteStringToFile(content, path, follow_symlinks);
65 }
66
67 template <typename T>
GetIntProperty(const std::string & key,T default_value,T min,T max)68 T GetIntProperty(const std::string &key, T default_value, T min, T max) {
69 return (T)(mock->GetIntProperty(key, default_value, min, max));
70 }
71
GetBoolProperty(const std::string & key,bool default_value)72 bool GetBoolProperty(const std::string &key, bool default_value) {
73 return mock->GetBoolProperty(key, default_value);
74 }
75
76 template int8_t GetIntProperty(const std::string &, int8_t, int8_t, int8_t);
77 template int16_t GetIntProperty(const std::string &, int16_t, int16_t, int16_t);
78 template int32_t GetIntProperty(const std::string &, int32_t, int32_t, int32_t);
79 template int64_t GetIntProperty(const std::string &, int64_t, int64_t, int64_t);
80
SetProperty(const std::string & key,const std::string & value)81 bool SetProperty(const std::string &key, const std::string &value) {
82 return mock->SetProperty(key, value);
83 }
84
85 } // namespace base
86 } // namespace android
87
88 nsecs_t testvar_systemTimeSecs = 0;
systemTime(int clock)89 nsecs_t systemTime(int clock) {
90 UNUSED(clock);
91 return seconds_to_nanoseconds(testvar_systemTimeSecs);
92 }
93
94 namespace hardware {
95 namespace google {
96 namespace pixel {
97 namespace health {
98
99 using ::testing::_;
100 using ::testing::AnyNumber;
101 using ::testing::AtLeast;
102 using ::testing::DoAll;
103 using ::testing::InSequence;
104 using ::testing::Return;
105 using ::testing::SetArgPointee;
106
107 struct android::BatteryProperties props;
108 BatteryDefender *battDefender;
109
110 const char *kPathWiredChargerPresent = "/sys/class/power_supply/usb/present";
111 const char *kPathWirelessChargerPresent = "/sys/class/power_supply/wireless/present";
112 const char *kPathPersistChargerPresentTime = "/mnt/vendor/persist/battery/defender_charger_time";
113 const char *kPathPersistDefenderActiveTime = "/mnt/vendor/persist/battery/defender_active_time";
114 const char *kPathStartLevel = "/sys/devices/platform/soc/soc:google,charger/charge_start_level";
115 const char *kPathStopLevel = "/sys/devices/platform/soc/soc:google,charger/charge_stop_level";
116
117 const char *kPropChargeLevelVendorStart = "persist.vendor.charge.start.level";
118 const char *kPropChargeLevelVendorStop = "persist.vendor.charge.stop.level";
119 const char *kPropBatteryDefenderState = "vendor.battery.defender.state";
120 const char *kPropBatteryDefenderDisable = "vendor.battery.defender.disable";
121 const char *kPropBatteryDefenderThreshold = "vendor.battery.defender.threshold";
122
123 const char *kPropBatteryDefenderCtrlEnable = "vendor.battery.defender.ctrl.enable";
124 const char *kPropBatteryDefenderCtrlActivateTime = "vendor.battery.defender.ctrl.trigger_time";
125 const char *kPropBatteryDefenderCtrlResumeTime = "vendor.battery.defender.ctrl.resume_time";
126 const char *kPropBatteryDefenderCtrlStartSOC = "vendor.battery.defender.ctrl.recharge_soc_start";
127 const char *kPropBatteryDefenderCtrlStopSOC = "vendor.battery.defender.ctrl.recharge_soc_stop";
128 const char *kPropBatteryDefenderCtrlTriggerSOC = "vendor.battery.defender.ctrl.trigger_soc";
129
130 class BatteryDefenderTest : public ::testing::Test {
131 public:
BatteryDefenderTest()132 BatteryDefenderTest()
133 : defender(kPathWirelessChargerPresent, DEFAULT_START_LEVEL_PATH, DEFAULT_STOP_LEVEL_PATH,
134 DEFAULT_TIME_TO_ACTIVATE_SECONDS, DEFAULT_TIME_TO_CLEAR_SECONDS, false) {}
135
SetUp()136 void SetUp() {
137 mock = &mockFixture;
138
139 props = {};
140 battDefender = &defender;
141
142 EXPECT_CALL(*mock, SetProperty(_, _)).Times(AnyNumber());
143 EXPECT_CALL(*mock, ReadFileToString(_, _, _)).Times(AnyNumber());
144 EXPECT_CALL(*mock, GetIntProperty(_, _, _, _)).Times(AnyNumber());
145 EXPECT_CALL(*mock, GetBoolProperty(_, _)).Times(AnyNumber());
146 EXPECT_CALL(*mock, WriteStringToFile(_, _, _)).Times(AnyNumber());
147
148 ON_CALL(*mock, ReadFileToString(_, _, _))
149 .WillByDefault(DoAll(SetArgPointee<1>(std::string("0")), Return(true)));
150
151 ON_CALL(*mock, WriteStringToFile(_, _, _)).WillByDefault(Return(true));
152 }
153
TearDown()154 void TearDown() {}
155
156 private:
157 HealthInterfaceMock mockFixture;
158 BatteryDefender defender;
159 };
160
enableDefender(void)161 static void enableDefender(void) {
162 ON_CALL(*mock, GetIntProperty(kPropChargeLevelVendorStart, _, _, _)).WillByDefault(Return(0));
163 ON_CALL(*mock, GetIntProperty(kPropChargeLevelVendorStop, _, _, _)).WillByDefault(Return(100));
164 ON_CALL(*mock, GetBoolProperty(kPropBatteryDefenderDisable, _)).WillByDefault(Return(false));
165
166 ON_CALL(*mock, GetBoolProperty(kPropBatteryDefenderCtrlEnable, _)).WillByDefault(Return(true));
167 }
168
usbPresent(void)169 static void usbPresent(void) {
170 ON_CALL(*mock, ReadFileToString(kPathWiredChargerPresent, _, _))
171 .WillByDefault(DoAll(SetArgPointee<1>(std::string("1")), Return(true)));
172 }
173
wirelessPresent(void)174 static void wirelessPresent(void) {
175 ON_CALL(*mock, ReadFileToString(kPathWirelessChargerPresent, _, _))
176 .WillByDefault(DoAll(SetArgPointee<1>(std::string("1")), Return(true)));
177 }
178
wirelessNotPresent(void)179 static void wirelessNotPresent(void) {
180 ON_CALL(*mock, ReadFileToString(kPathWirelessChargerPresent, _, _))
181 .WillByDefault(DoAll(SetArgPointee<1>(std::string("0")), Return(true)));
182 }
183
powerAvailable(void)184 static void powerAvailable(void) {
185 wirelessPresent();
186 usbPresent();
187 }
188
defaultThresholds(void)189 static void defaultThresholds(void) {
190 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderThreshold, _, _, _))
191 .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
192
193 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlActivateTime, _, _, _))
194 .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
195 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlResumeTime, _, _, _))
196 .WillByDefault(Return(DEFAULT_TIME_TO_CLEAR_SECONDS));
197
198 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlStartSOC, _, _, _))
199 .WillByDefault(Return(70));
200 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlStopSOC, _, _, _))
201 .WillByDefault(Return(80));
202
203 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlTriggerSOC, _, _, _))
204 .WillByDefault(Return(100));
205 }
206
capacityReached(void)207 static void capacityReached(void) {
208 props.batteryLevel = 100;
209 }
210
initTo1000sConnectedCapacityReached(void)211 static void initTo1000sConnectedCapacityReached(void) {
212 ON_CALL(*mock, ReadFileToString(kPathPersistChargerPresentTime, _, _))
213 .WillByDefault(DoAll(SetArgPointee<1>(std::to_string(1000)), Return(true)));
214 }
215
initToActive(void)216 static void initToActive(void) {
217 ON_CALL(*mock, ReadFileToString(kPathPersistChargerPresentTime, _, _))
218 .WillByDefault(
219 DoAll(SetArgPointee<1>(std::to_string(DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1)),
220 Return(true)));
221 }
222
TEST_F(BatteryDefenderTest,EnableAndDisconnected)223 TEST_F(BatteryDefenderTest, EnableAndDisconnected) {
224 enableDefender();
225 // No power
226
227 InSequence s;
228
229 // Enable Battery Defender
230 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "DISCONNECTED"));
231 battDefender->update(&props);
232 }
233
TEST_F(BatteryDefenderTest,DisableNonDefaultLevels)234 TEST_F(BatteryDefenderTest, DisableNonDefaultLevels) {
235 InSequence s;
236
237 // Enable Battery Defender
238 EXPECT_CALL(*mock, GetIntProperty(kPropChargeLevelVendorStart, _, _, _)).WillOnce(Return(30));
239 EXPECT_CALL(*mock, GetIntProperty(kPropChargeLevelVendorStop, _, _, _)).WillOnce(Return(35));
240
241 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "DISABLED"));
242 battDefender->update(&props);
243 }
244
TEST_F(BatteryDefenderTest,DisableExplicit)245 TEST_F(BatteryDefenderTest, DisableExplicit) {
246 InSequence s;
247
248 // Enable Battery Defender
249 EXPECT_CALL(*mock, GetBoolProperty(kPropBatteryDefenderDisable, _)).WillOnce(Return(true));
250
251 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "DISABLED"));
252 battDefender->update(&props);
253 }
254
TEST_F(BatteryDefenderTest,InitActive)255 TEST_F(BatteryDefenderTest, InitActive) {
256 enableDefender();
257 powerAvailable();
258 defaultThresholds();
259
260 InSequence s;
261
262 EXPECT_CALL(*mock, ReadFileToString(kPathPersistChargerPresentTime, _, _))
263 .WillOnce(DoAll(SetArgPointee<1>(std::to_string(DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1)),
264 Return(true)));
265 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
266 battDefender->update(&props);
267 }
268
TEST_F(BatteryDefenderTest,InitConnectedCapacityReached)269 TEST_F(BatteryDefenderTest, InitConnectedCapacityReached) {
270 enableDefender();
271 powerAvailable();
272 defaultThresholds();
273
274 InSequence s;
275
276 int time_expected = DEFAULT_TIME_TO_ACTIVATE_SECONDS - 1;
277 EXPECT_CALL(*mock, ReadFileToString(kPathPersistChargerPresentTime, _, _))
278 .WillOnce(DoAll(SetArgPointee<1>(std::to_string(time_expected)), Return(true)));
279 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
280 battDefender->update(&props);
281
282 testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
283 time_expected += MIN_TIME_BETWEEN_FILE_UPDATES;
284 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(time_expected),
285 kPathPersistChargerPresentTime, _));
286 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
287 battDefender->update(&props);
288 }
289
TEST_F(BatteryDefenderTest,InitConnected)290 TEST_F(BatteryDefenderTest, InitConnected) {
291 enableDefender();
292 powerAvailable();
293 defaultThresholds();
294
295 InSequence s;
296
297 EXPECT_CALL(*mock, ReadFileToString(kPathPersistChargerPresentTime, _, _))
298 .WillOnce(DoAll(SetArgPointee<1>(std::to_string(0)), Return(true)));
299 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
300 battDefender->update(&props);
301
302 // mHasReachedHighCapacityLevel shall be false
303 testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
304 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
305 battDefender->update(&props);
306
307 // Would be active if mHasReachedHighCapacityLevel was true
308 testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
309 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
310 battDefender->update(&props);
311 }
312
TEST_F(BatteryDefenderTest,TriggerTime)313 TEST_F(BatteryDefenderTest, TriggerTime) {
314 enableDefender();
315 powerAvailable();
316 defaultThresholds();
317
318 InSequence s;
319
320 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
321 testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
322 battDefender->update(&props);
323
324 // Reached 100% capacity at least once
325 capacityReached();
326 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
327 testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
328 battDefender->update(&props);
329
330 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(DEFAULT_TIME_TO_ACTIVATE_SECONDS),
331 kPathPersistChargerPresentTime, _));
332 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
333 testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS;
334 battDefender->update(&props);
335
336 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(DEFAULT_TIME_TO_ACTIVATE_SECONDS +
337 MIN_TIME_BETWEEN_FILE_UPDATES),
338 kPathPersistChargerPresentTime, _));
339 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
340 testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
341 battDefender->update(&props);
342 }
343
TEST_F(BatteryDefenderTest,ChargeLevels)344 TEST_F(BatteryDefenderTest, ChargeLevels) {
345 enableDefender();
346 powerAvailable();
347 defaultThresholds();
348 initTo1000sConnectedCapacityReached();
349
350 InSequence s;
351
352 // No expectations needed; default values already set
353 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
354 testvar_systemTimeSecs += 0;
355 battDefender->update(&props);
356
357 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(70), kPathStartLevel, _));
358 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(80), kPathStopLevel, _));
359 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
360 testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
361 battDefender->update(&props);
362 }
363
TEST_F(BatteryDefenderTest,ActiveTime)364 TEST_F(BatteryDefenderTest, ActiveTime) {
365 enableDefender();
366 powerAvailable();
367 defaultThresholds();
368 initToActive();
369
370 InSequence s;
371
372 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(70), kPathStartLevel, _));
373 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(80), kPathStopLevel, _));
374 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
375 battDefender->update(&props);
376 }
377
TEST_F(BatteryDefenderTest,ActiveTime_NonDefaultLevels)378 TEST_F(BatteryDefenderTest, ActiveTime_NonDefaultLevels) {
379 enableDefender();
380 powerAvailable();
381 initToActive();
382 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderThreshold, _, _, _))
383 .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
384 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlActivateTime, _, _, _))
385 .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
386 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlResumeTime, _, _, _))
387 .WillByDefault(Return(DEFAULT_TIME_TO_CLEAR_SECONDS));
388
389 // Non-default
390 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlStartSOC, _, _, _))
391 .WillByDefault(Return(50));
392 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlStopSOC, _, _, _))
393 .WillByDefault(Return(60));
394
395 InSequence s;
396
397 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(50), kPathStartLevel, _));
398 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(60), kPathStopLevel, _));
399 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
400 battDefender->update(&props);
401 }
402
TEST_F(BatteryDefenderTest,ActiveTime_NonDefaultLevels_invalid)403 TEST_F(BatteryDefenderTest, ActiveTime_NonDefaultLevels_invalid) {
404 enableDefender();
405 powerAvailable();
406 initToActive();
407 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderThreshold, _, _, _))
408 .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
409 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlActivateTime, _, _, _))
410 .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
411 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlResumeTime, _, _, _))
412 .WillByDefault(Return(DEFAULT_TIME_TO_CLEAR_SECONDS));
413
414 // Non-default
415 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlStartSOC, _, _, _))
416 .WillByDefault(Return(30));
417 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlStopSOC, _, _, _))
418 .WillByDefault(Return(10));
419
420 InSequence s;
421
422 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(70), kPathStartLevel, _));
423 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(80), kPathStopLevel, _));
424 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
425 battDefender->update(&props);
426 }
427
TEST_F(BatteryDefenderTest,ConnectDisconnectCycle)428 TEST_F(BatteryDefenderTest, ConnectDisconnectCycle) {
429 enableDefender();
430 defaultThresholds();
431 initTo1000sConnectedCapacityReached();
432
433 InSequence s;
434
435 // Power ON
436 wirelessPresent();
437
438 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(1000), kPathPersistChargerPresentTime, _));
439 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
440 testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
441 battDefender->update(&props);
442
443 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(1000 + MIN_TIME_BETWEEN_FILE_UPDATES),
444 kPathPersistChargerPresentTime, _));
445 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
446 testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
447 battDefender->update(&props);
448
449 // Power OFF
450 wirelessNotPresent();
451
452 // Maintain kPathPersistChargerPresentTime = 1000 + MIN_TIME_BETWEEN_FILE_UPDATES
453 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
454 testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
455 battDefender->update(&props);
456
457 // Maintain kPathPersistChargerPresentTime = 1000 + MIN_TIME_BETWEEN_FILE_UPDATES
458 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
459 testvar_systemTimeSecs += DEFAULT_TIME_TO_CLEAR_SECONDS - MIN_TIME_BETWEEN_FILE_UPDATES - 1;
460 battDefender->update(&props);
461
462 testvar_systemTimeSecs += 1;
463 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(0), kPathPersistChargerPresentTime, _));
464 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "DISCONNECTED"));
465 testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
466 battDefender->update(&props);
467
468 // Power ON
469 wirelessPresent();
470
471 // Maintain kPathPersistChargerPresentTime = 0
472 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
473 testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
474 battDefender->update(&props);
475
476 capacityReached();
477 // Maintain kPathPersistChargerPresentTime = 0
478 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
479 testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
480 battDefender->update(&props);
481
482 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(MIN_TIME_BETWEEN_FILE_UPDATES),
483 kPathPersistChargerPresentTime, _));
484 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
485 testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
486 battDefender->update(&props);
487 }
488
TEST_F(BatteryDefenderTest,ConnectDisconnectResumeTimeThreshold)489 TEST_F(BatteryDefenderTest, ConnectDisconnectResumeTimeThreshold) {
490 enableDefender();
491 initTo1000sConnectedCapacityReached();
492 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderThreshold, _, _, _))
493 .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
494 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlActivateTime, _, _, _))
495 .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
496
497 // Non-default thresholds
498 ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlResumeTime, _, _, _))
499 .WillByDefault(Return(0));
500
501 InSequence s;
502
503 // Power ON
504 wirelessPresent();
505
506 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(1000), kPathPersistChargerPresentTime, _));
507 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
508 testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
509 battDefender->update(&props);
510
511 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(1000 + MIN_TIME_BETWEEN_FILE_UPDATES),
512 kPathPersistChargerPresentTime, _));
513 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
514 testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
515 battDefender->update(&props);
516
517 // Power OFF
518 wirelessNotPresent();
519
520 EXPECT_CALL(*mock, WriteStringToFile(std::to_string(0), kPathPersistChargerPresentTime, _));
521 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "DISCONNECTED"));
522 testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
523 battDefender->update(&props);
524 }
525
TEST_F(BatteryDefenderTest,PropsOverride_InitActive_allOnlineFalse)526 TEST_F(BatteryDefenderTest, PropsOverride_InitActive_allOnlineFalse) {
527 enableDefender();
528 usbPresent();
529 defaultThresholds();
530 initToActive();
531
532 InSequence s;
533
534 props.chargerAcOnline = false;
535 props.chargerUsbOnline = false;
536 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
537 battDefender->update(&props);
538 ASSERT_EQ(props.chargerAcOnline, false);
539 ASSERT_EQ(props.chargerUsbOnline, true);
540
541 props.chargerAcOnline = false;
542 props.chargerUsbOnline = false;
543 battDefender->update(&props);
544 ASSERT_EQ(props.chargerAcOnline, false);
545 ASSERT_EQ(props.chargerUsbOnline, true);
546 }
547
TEST_F(BatteryDefenderTest,PropsOverride_InitActive_usbOnline)548 TEST_F(BatteryDefenderTest, PropsOverride_InitActive_usbOnline) {
549 enableDefender();
550 usbPresent();
551 defaultThresholds();
552 initToActive();
553
554 InSequence s;
555
556 props.chargerAcOnline = false;
557 props.chargerUsbOnline = true;
558 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
559 battDefender->update(&props);
560 ASSERT_EQ(props.chargerAcOnline, false);
561 ASSERT_EQ(props.chargerUsbOnline, true);
562
563 props.chargerAcOnline = false;
564 props.chargerUsbOnline = false;
565 battDefender->update(&props);
566 ASSERT_EQ(props.chargerAcOnline, false);
567 ASSERT_EQ(props.chargerUsbOnline, true);
568 }
569
TEST_F(BatteryDefenderTest,PropsOverride_InitActive_acOnline)570 TEST_F(BatteryDefenderTest, PropsOverride_InitActive_acOnline) {
571 enableDefender();
572 usbPresent();
573 defaultThresholds();
574 initToActive();
575
576 InSequence s;
577
578 props.chargerAcOnline = true;
579 props.chargerUsbOnline = false;
580 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
581 battDefender->update(&props);
582 ASSERT_EQ(props.chargerAcOnline, true);
583 ASSERT_EQ(props.chargerUsbOnline, false);
584
585 props.chargerAcOnline = false;
586 props.chargerUsbOnline = false;
587 battDefender->update(&props);
588 ASSERT_EQ(props.chargerAcOnline, true);
589 ASSERT_EQ(props.chargerUsbOnline, false);
590 }
591
TEST_F(BatteryDefenderTest,PropsOverride_InitActive_allOnline)592 TEST_F(BatteryDefenderTest, PropsOverride_InitActive_allOnline) {
593 enableDefender();
594 usbPresent();
595 defaultThresholds();
596 initToActive();
597
598 InSequence s;
599
600 props.chargerAcOnline = true;
601 props.chargerUsbOnline = true;
602 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
603 battDefender->update(&props);
604 ASSERT_EQ(props.chargerAcOnline, true);
605 ASSERT_EQ(props.chargerUsbOnline, true);
606
607 props.chargerAcOnline = false;
608 props.chargerUsbOnline = false;
609 battDefender->update(&props);
610 ASSERT_EQ(props.chargerAcOnline, true);
611 ASSERT_EQ(props.chargerUsbOnline, true);
612 }
613
TEST_F(BatteryDefenderTest,PropsOverride_InitConnected_allOnlineFalse)614 TEST_F(BatteryDefenderTest, PropsOverride_InitConnected_allOnlineFalse) {
615 enableDefender();
616 usbPresent();
617 defaultThresholds();
618 initTo1000sConnectedCapacityReached();
619
620 InSequence s;
621
622 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
623 battDefender->update(&props);
624
625 props.chargerAcOnline = false;
626 props.chargerUsbOnline = false;
627 testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
628 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
629 battDefender->update(&props);
630 ASSERT_EQ(props.chargerAcOnline, false);
631 ASSERT_EQ(props.chargerUsbOnline, true);
632
633 props.chargerAcOnline = false;
634 props.chargerUsbOnline = false;
635 battDefender->update(&props);
636 ASSERT_EQ(props.chargerAcOnline, false);
637 ASSERT_EQ(props.chargerUsbOnline, true);
638 }
639
TEST_F(BatteryDefenderTest,PropsOverride_InitConnected_usbOnline)640 TEST_F(BatteryDefenderTest, PropsOverride_InitConnected_usbOnline) {
641 enableDefender();
642 usbPresent();
643 defaultThresholds();
644 initTo1000sConnectedCapacityReached();
645
646 InSequence s;
647
648 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
649 battDefender->update(&props);
650
651 props.chargerAcOnline = false;
652 props.chargerUsbOnline = true;
653 testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
654 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
655 battDefender->update(&props);
656 ASSERT_EQ(props.chargerAcOnline, false);
657 ASSERT_EQ(props.chargerUsbOnline, true);
658
659 props.chargerAcOnline = false;
660 props.chargerUsbOnline = false;
661 battDefender->update(&props);
662 ASSERT_EQ(props.chargerAcOnline, false);
663 ASSERT_EQ(props.chargerUsbOnline, true);
664 }
665
TEST_F(BatteryDefenderTest,PropsOverride_InitConnected_acOnline)666 TEST_F(BatteryDefenderTest, PropsOverride_InitConnected_acOnline) {
667 enableDefender();
668 usbPresent();
669 defaultThresholds();
670 initTo1000sConnectedCapacityReached();
671
672 InSequence s;
673
674 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
675 battDefender->update(&props);
676
677 props.chargerAcOnline = true;
678 props.chargerUsbOnline = false;
679 testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
680 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
681 battDefender->update(&props);
682 ASSERT_EQ(props.chargerAcOnline, true);
683 ASSERT_EQ(props.chargerUsbOnline, false);
684
685 props.chargerAcOnline = false;
686 props.chargerUsbOnline = false;
687 battDefender->update(&props);
688 ASSERT_EQ(props.chargerAcOnline, true);
689 ASSERT_EQ(props.chargerUsbOnline, false);
690 }
691
TEST_F(BatteryDefenderTest,PropsOverride_InitConnected_allOnline)692 TEST_F(BatteryDefenderTest, PropsOverride_InitConnected_allOnline) {
693 enableDefender();
694 usbPresent();
695 defaultThresholds();
696 initTo1000sConnectedCapacityReached();
697
698 InSequence s;
699
700 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
701 battDefender->update(&props);
702
703 props.chargerAcOnline = true;
704 props.chargerUsbOnline = true;
705 testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
706 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
707 battDefender->update(&props);
708 ASSERT_EQ(props.chargerAcOnline, true);
709 ASSERT_EQ(props.chargerUsbOnline, true);
710
711 props.chargerAcOnline = false;
712 props.chargerUsbOnline = false;
713 battDefender->update(&props);
714 ASSERT_EQ(props.chargerAcOnline, true);
715 ASSERT_EQ(props.chargerUsbOnline, true);
716 }
717
TEST_F(BatteryDefenderTest,PropsOverride_InitConnected_overrideHealth)718 TEST_F(BatteryDefenderTest, PropsOverride_InitConnected_overrideHealth) {
719 enableDefender();
720 usbPresent();
721 defaultThresholds();
722 initTo1000sConnectedCapacityReached();
723
724 InSequence s;
725
726 props.batteryHealth = android::BATTERY_HEALTH_UNKNOWN;
727 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
728 battDefender->update(&props);
729 ASSERT_EQ(props.batteryHealth, android::BATTERY_HEALTH_UNKNOWN);
730
731 props.batteryHealth = android::BATTERY_HEALTH_UNKNOWN;
732 testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
733 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
734 battDefender->update(&props);
735 ASSERT_EQ(props.batteryHealth, android::BATTERY_HEALTH_OVERHEAT);
736 }
737
TEST_F(BatteryDefenderTest,PropsOverride_InitConnected_kernelDefend)738 TEST_F(BatteryDefenderTest, PropsOverride_InitConnected_kernelDefend) {
739 enableDefender();
740 usbPresent();
741 defaultThresholds();
742 initTo1000sConnectedCapacityReached();
743
744 InSequence s;
745
746 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED")).Times(3);
747 battDefender->update(&props);
748
749 props.chargerAcOnline = true;
750 props.chargerUsbOnline = true;
751 props.batteryHealth = android::BATTERY_HEALTH_OVERHEAT;
752 battDefender->update(&props);
753 ASSERT_EQ(props.chargerAcOnline, true);
754 ASSERT_EQ(props.chargerUsbOnline, true);
755
756 props.chargerAcOnline = false;
757 props.chargerUsbOnline = false;
758 battDefender->update(&props);
759 ASSERT_EQ(props.chargerAcOnline, true);
760 ASSERT_EQ(props.chargerUsbOnline, true);
761 }
762
TEST_F(BatteryDefenderTest,WirelessPathUnsupported)763 TEST_F(BatteryDefenderTest, WirelessPathUnsupported) {
764 enableDefender();
765 wirelessPresent();
766 defaultThresholds();
767
768 InSequence s;
769
770 battDefender->setWirelessNotSupported();
771
772 EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "DISCONNECTED"));
773 battDefender->update(&props);
774 }
775
776 } // namespace health
777 } // namespace pixel
778 } // namespace google
779 } // namespace hardware
780