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() : defender(kPathWirelessChargerPresent) {}
133 
SetUp()134     void SetUp() {
135         mock = &mockFixture;
136 
137         props = {};
138         battDefender = &defender;
139 
140         EXPECT_CALL(*mock, SetProperty(_, _)).Times(AnyNumber());
141         EXPECT_CALL(*mock, ReadFileToString(_, _, _)).Times(AnyNumber());
142         EXPECT_CALL(*mock, GetIntProperty(_, _, _, _)).Times(AnyNumber());
143         EXPECT_CALL(*mock, GetBoolProperty(_, _)).Times(AnyNumber());
144         EXPECT_CALL(*mock, WriteStringToFile(_, _, _)).Times(AnyNumber());
145 
146         ON_CALL(*mock, ReadFileToString(_, _, _))
147                 .WillByDefault(DoAll(SetArgPointee<1>(std::string("0")), Return(true)));
148 
149         ON_CALL(*mock, WriteStringToFile(_, _, _)).WillByDefault(Return(true));
150     }
151 
TearDown()152     void TearDown() {}
153 
154   private:
155     HealthInterfaceMock mockFixture;
156     BatteryDefender defender;
157 };
158 
enableDefender(void)159 static void enableDefender(void) {
160     ON_CALL(*mock, GetIntProperty(kPropChargeLevelVendorStart, _, _, _)).WillByDefault(Return(0));
161     ON_CALL(*mock, GetIntProperty(kPropChargeLevelVendorStop, _, _, _)).WillByDefault(Return(100));
162     ON_CALL(*mock, GetBoolProperty(kPropBatteryDefenderDisable, _)).WillByDefault(Return(false));
163 
164     ON_CALL(*mock, GetBoolProperty(kPropBatteryDefenderCtrlEnable, _)).WillByDefault(Return(true));
165 }
166 
usbPresent(void)167 static void usbPresent(void) {
168     ON_CALL(*mock, ReadFileToString(kPathWiredChargerPresent, _, _))
169             .WillByDefault(DoAll(SetArgPointee<1>(std::string("1")), Return(true)));
170 }
171 
wirelessPresent(void)172 static void wirelessPresent(void) {
173     ON_CALL(*mock, ReadFileToString(kPathWirelessChargerPresent, _, _))
174             .WillByDefault(DoAll(SetArgPointee<1>(std::string("1")), Return(true)));
175 }
176 
wirelessNotPresent(void)177 static void wirelessNotPresent(void) {
178     ON_CALL(*mock, ReadFileToString(kPathWirelessChargerPresent, _, _))
179             .WillByDefault(DoAll(SetArgPointee<1>(std::string("0")), Return(true)));
180 }
181 
powerAvailable(void)182 static void powerAvailable(void) {
183     wirelessPresent();
184     usbPresent();
185 }
186 
defaultThresholds(void)187 static void defaultThresholds(void) {
188     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderThreshold, _, _, _))
189             .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
190 
191     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlActivateTime, _, _, _))
192             .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
193     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlResumeTime, _, _, _))
194             .WillByDefault(Return(DEFAULT_TIME_TO_CLEAR_SECONDS));
195 
196     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlStartSOC, _, _, _))
197             .WillByDefault(Return(70));
198     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlStopSOC, _, _, _))
199             .WillByDefault(Return(80));
200 
201     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlTriggerSOC, _, _, _))
202             .WillByDefault(Return(100));
203 }
204 
capacityReached(void)205 static void capacityReached(void) {
206     props.batteryLevel = 100;
207 }
208 
initTo1000sConnectedCapacityReached(void)209 static void initTo1000sConnectedCapacityReached(void) {
210     ON_CALL(*mock, ReadFileToString(kPathPersistChargerPresentTime, _, _))
211             .WillByDefault(DoAll(SetArgPointee<1>(std::to_string(1000)), Return(true)));
212 }
213 
initToActive(void)214 static void initToActive(void) {
215     ON_CALL(*mock, ReadFileToString(kPathPersistChargerPresentTime, _, _))
216             .WillByDefault(
217                     DoAll(SetArgPointee<1>(std::to_string(DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1)),
218                           Return(true)));
219 }
220 
TEST_F(BatteryDefenderTest,EnableAndDisconnected)221 TEST_F(BatteryDefenderTest, EnableAndDisconnected) {
222     enableDefender();
223     // No power
224 
225     InSequence s;
226 
227     // Enable Battery Defender
228     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "DISCONNECTED"));
229     battDefender->update(&props);
230 }
231 
TEST_F(BatteryDefenderTest,DisableNonDefaultLevels)232 TEST_F(BatteryDefenderTest, DisableNonDefaultLevels) {
233     InSequence s;
234 
235     // Enable Battery Defender
236     EXPECT_CALL(*mock, GetIntProperty(kPropChargeLevelVendorStart, _, _, _)).WillOnce(Return(30));
237     EXPECT_CALL(*mock, GetIntProperty(kPropChargeLevelVendorStop, _, _, _)).WillOnce(Return(35));
238 
239     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "DISABLED"));
240     battDefender->update(&props);
241 }
242 
TEST_F(BatteryDefenderTest,DisableExplicit)243 TEST_F(BatteryDefenderTest, DisableExplicit) {
244     InSequence s;
245 
246     // Enable Battery Defender
247     EXPECT_CALL(*mock, GetBoolProperty(kPropBatteryDefenderDisable, _)).WillOnce(Return(true));
248 
249     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "DISABLED"));
250     battDefender->update(&props);
251 }
252 
TEST_F(BatteryDefenderTest,InitActive)253 TEST_F(BatteryDefenderTest, InitActive) {
254     enableDefender();
255     powerAvailable();
256     defaultThresholds();
257 
258     InSequence s;
259 
260     EXPECT_CALL(*mock, ReadFileToString(kPathPersistChargerPresentTime, _, _))
261             .WillOnce(DoAll(SetArgPointee<1>(std::to_string(DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1)),
262                             Return(true)));
263     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
264     battDefender->update(&props);
265 }
266 
TEST_F(BatteryDefenderTest,InitConnectedCapacityReached)267 TEST_F(BatteryDefenderTest, InitConnectedCapacityReached) {
268     enableDefender();
269     powerAvailable();
270     defaultThresholds();
271 
272     InSequence s;
273 
274     int time_expected = DEFAULT_TIME_TO_ACTIVATE_SECONDS - 1;
275     EXPECT_CALL(*mock, ReadFileToString(kPathPersistChargerPresentTime, _, _))
276             .WillOnce(DoAll(SetArgPointee<1>(std::to_string(time_expected)), Return(true)));
277     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
278     battDefender->update(&props);
279 
280     testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
281     time_expected += MIN_TIME_BETWEEN_FILE_UPDATES;
282     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(time_expected),
283                                          kPathPersistChargerPresentTime, _));
284     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
285     battDefender->update(&props);
286 }
287 
TEST_F(BatteryDefenderTest,InitConnected)288 TEST_F(BatteryDefenderTest, InitConnected) {
289     enableDefender();
290     powerAvailable();
291     defaultThresholds();
292 
293     InSequence s;
294 
295     EXPECT_CALL(*mock, ReadFileToString(kPathPersistChargerPresentTime, _, _))
296             .WillOnce(DoAll(SetArgPointee<1>(std::to_string(0)), Return(true)));
297     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
298     battDefender->update(&props);
299 
300     // mHasReachedHighCapacityLevel shall be false
301     testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
302     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
303     battDefender->update(&props);
304 
305     // Would be active if mHasReachedHighCapacityLevel was true
306     testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
307     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
308     battDefender->update(&props);
309 }
310 
TEST_F(BatteryDefenderTest,TriggerTime)311 TEST_F(BatteryDefenderTest, TriggerTime) {
312     enableDefender();
313     powerAvailable();
314     defaultThresholds();
315 
316     InSequence s;
317 
318     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
319     testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
320     battDefender->update(&props);
321 
322     // Reached 100% capacity at least once
323     capacityReached();
324     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
325     testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
326     battDefender->update(&props);
327 
328     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(DEFAULT_TIME_TO_ACTIVATE_SECONDS),
329                                          kPathPersistChargerPresentTime, _));
330     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
331     testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS;
332     battDefender->update(&props);
333 
334     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(DEFAULT_TIME_TO_ACTIVATE_SECONDS +
335                                                         MIN_TIME_BETWEEN_FILE_UPDATES),
336                                          kPathPersistChargerPresentTime, _));
337     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
338     testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
339     battDefender->update(&props);
340 }
341 
TEST_F(BatteryDefenderTest,ChargeLevels)342 TEST_F(BatteryDefenderTest, ChargeLevels) {
343     enableDefender();
344     powerAvailable();
345     defaultThresholds();
346     initTo1000sConnectedCapacityReached();
347 
348     InSequence s;
349 
350     // No expectations needed; default values already set
351     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
352     testvar_systemTimeSecs += 0;
353     battDefender->update(&props);
354 
355     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(70), kPathStartLevel, _));
356     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(80), kPathStopLevel, _));
357     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
358     testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
359     battDefender->update(&props);
360 }
361 
TEST_F(BatteryDefenderTest,ActiveTime)362 TEST_F(BatteryDefenderTest, ActiveTime) {
363     enableDefender();
364     powerAvailable();
365     defaultThresholds();
366     initToActive();
367 
368     InSequence s;
369 
370     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(70), kPathStartLevel, _));
371     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(80), kPathStopLevel, _));
372     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
373     battDefender->update(&props);
374 }
375 
TEST_F(BatteryDefenderTest,ActiveTime_NonDefaultLevels)376 TEST_F(BatteryDefenderTest, ActiveTime_NonDefaultLevels) {
377     enableDefender();
378     powerAvailable();
379     initToActive();
380     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderThreshold, _, _, _))
381             .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
382     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlActivateTime, _, _, _))
383             .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
384     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlResumeTime, _, _, _))
385             .WillByDefault(Return(DEFAULT_TIME_TO_CLEAR_SECONDS));
386 
387     // Non-default
388     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlStartSOC, _, _, _))
389             .WillByDefault(Return(50));
390     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlStopSOC, _, _, _))
391             .WillByDefault(Return(60));
392 
393     InSequence s;
394 
395     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(50), kPathStartLevel, _));
396     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(60), kPathStopLevel, _));
397     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
398     battDefender->update(&props);
399 }
400 
TEST_F(BatteryDefenderTest,ActiveTime_NonDefaultLevels_invalid)401 TEST_F(BatteryDefenderTest, ActiveTime_NonDefaultLevels_invalid) {
402     enableDefender();
403     powerAvailable();
404     initToActive();
405     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderThreshold, _, _, _))
406             .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
407     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlActivateTime, _, _, _))
408             .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
409     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlResumeTime, _, _, _))
410             .WillByDefault(Return(DEFAULT_TIME_TO_CLEAR_SECONDS));
411 
412     // Non-default
413     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlStartSOC, _, _, _))
414             .WillByDefault(Return(30));
415     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlStopSOC, _, _, _))
416             .WillByDefault(Return(10));
417 
418     InSequence s;
419 
420     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(70), kPathStartLevel, _));
421     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(80), kPathStopLevel, _));
422     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
423     battDefender->update(&props);
424 }
425 
TEST_F(BatteryDefenderTest,ConnectDisconnectCycle)426 TEST_F(BatteryDefenderTest, ConnectDisconnectCycle) {
427     enableDefender();
428     defaultThresholds();
429     initTo1000sConnectedCapacityReached();
430 
431     InSequence s;
432 
433     // Power ON
434     wirelessPresent();
435 
436     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(1000), kPathPersistChargerPresentTime, _));
437     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
438     testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
439     battDefender->update(&props);
440 
441     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(1000 + MIN_TIME_BETWEEN_FILE_UPDATES),
442                                          kPathPersistChargerPresentTime, _));
443     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
444     testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
445     battDefender->update(&props);
446 
447     // Power OFF
448     wirelessNotPresent();
449 
450     // Maintain kPathPersistChargerPresentTime = 1000 + MIN_TIME_BETWEEN_FILE_UPDATES
451     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
452     testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
453     battDefender->update(&props);
454 
455     // Maintain kPathPersistChargerPresentTime = 1000 + MIN_TIME_BETWEEN_FILE_UPDATES
456     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
457     testvar_systemTimeSecs += DEFAULT_TIME_TO_CLEAR_SECONDS - MIN_TIME_BETWEEN_FILE_UPDATES - 1;
458     battDefender->update(&props);
459 
460     testvar_systemTimeSecs += 1;
461     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(0), kPathPersistChargerPresentTime, _));
462     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "DISCONNECTED"));
463     testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
464     battDefender->update(&props);
465 
466     // Power ON
467     wirelessPresent();
468 
469     // Maintain kPathPersistChargerPresentTime = 0
470     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
471     testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
472     battDefender->update(&props);
473 
474     capacityReached();
475     // Maintain kPathPersistChargerPresentTime = 0
476     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
477     testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
478     battDefender->update(&props);
479 
480     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(MIN_TIME_BETWEEN_FILE_UPDATES),
481                                          kPathPersistChargerPresentTime, _));
482     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
483     testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
484     battDefender->update(&props);
485 }
486 
TEST_F(BatteryDefenderTest,ConnectDisconnectResumeTimeThreshold)487 TEST_F(BatteryDefenderTest, ConnectDisconnectResumeTimeThreshold) {
488     enableDefender();
489     initTo1000sConnectedCapacityReached();
490     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderThreshold, _, _, _))
491             .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
492     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlActivateTime, _, _, _))
493             .WillByDefault(Return(DEFAULT_TIME_TO_ACTIVATE_SECONDS));
494 
495     // Non-default thresholds
496     ON_CALL(*mock, GetIntProperty(kPropBatteryDefenderCtrlResumeTime, _, _, _))
497             .WillByDefault(Return(0));
498 
499     InSequence s;
500 
501     // Power ON
502     wirelessPresent();
503 
504     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(1000), kPathPersistChargerPresentTime, _));
505     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
506     testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
507     battDefender->update(&props);
508 
509     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(1000 + MIN_TIME_BETWEEN_FILE_UPDATES),
510                                          kPathPersistChargerPresentTime, _));
511     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
512     testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
513     battDefender->update(&props);
514 
515     // Power OFF
516     wirelessNotPresent();
517 
518     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(0), kPathPersistChargerPresentTime, _));
519     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "DISCONNECTED"));
520     testvar_systemTimeSecs += MIN_TIME_BETWEEN_FILE_UPDATES;
521     battDefender->update(&props);
522 }
523 
TEST_F(BatteryDefenderTest,PropsOverride_InitActive_allOnlineFalse)524 TEST_F(BatteryDefenderTest, PropsOverride_InitActive_allOnlineFalse) {
525     enableDefender();
526     usbPresent();
527     defaultThresholds();
528     initToActive();
529 
530     InSequence s;
531 
532     props.chargerAcOnline = false;
533     props.chargerUsbOnline = false;
534     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
535     battDefender->update(&props);
536     ASSERT_EQ(props.chargerAcOnline, false);
537     ASSERT_EQ(props.chargerUsbOnline, true);
538 
539     props.chargerAcOnline = false;
540     props.chargerUsbOnline = false;
541     battDefender->update(&props);
542     ASSERT_EQ(props.chargerAcOnline, false);
543     ASSERT_EQ(props.chargerUsbOnline, true);
544 }
545 
TEST_F(BatteryDefenderTest,PropsOverride_InitActive_usbOnline)546 TEST_F(BatteryDefenderTest, PropsOverride_InitActive_usbOnline) {
547     enableDefender();
548     usbPresent();
549     defaultThresholds();
550     initToActive();
551 
552     InSequence s;
553 
554     props.chargerAcOnline = false;
555     props.chargerUsbOnline = true;
556     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
557     battDefender->update(&props);
558     ASSERT_EQ(props.chargerAcOnline, false);
559     ASSERT_EQ(props.chargerUsbOnline, true);
560 
561     props.chargerAcOnline = false;
562     props.chargerUsbOnline = false;
563     battDefender->update(&props);
564     ASSERT_EQ(props.chargerAcOnline, false);
565     ASSERT_EQ(props.chargerUsbOnline, true);
566 }
567 
TEST_F(BatteryDefenderTest,PropsOverride_InitActive_acOnline)568 TEST_F(BatteryDefenderTest, PropsOverride_InitActive_acOnline) {
569     enableDefender();
570     usbPresent();
571     defaultThresholds();
572     initToActive();
573 
574     InSequence s;
575 
576     props.chargerAcOnline = true;
577     props.chargerUsbOnline = false;
578     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
579     battDefender->update(&props);
580     ASSERT_EQ(props.chargerAcOnline, true);
581     ASSERT_EQ(props.chargerUsbOnline, false);
582 
583     props.chargerAcOnline = false;
584     props.chargerUsbOnline = false;
585     battDefender->update(&props);
586     ASSERT_EQ(props.chargerAcOnline, true);
587     ASSERT_EQ(props.chargerUsbOnline, false);
588 }
589 
TEST_F(BatteryDefenderTest,PropsOverride_InitActive_allOnline)590 TEST_F(BatteryDefenderTest, PropsOverride_InitActive_allOnline) {
591     enableDefender();
592     usbPresent();
593     defaultThresholds();
594     initToActive();
595 
596     InSequence s;
597 
598     props.chargerAcOnline = true;
599     props.chargerUsbOnline = true;
600     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
601     battDefender->update(&props);
602     ASSERT_EQ(props.chargerAcOnline, true);
603     ASSERT_EQ(props.chargerUsbOnline, true);
604 
605     props.chargerAcOnline = false;
606     props.chargerUsbOnline = false;
607     battDefender->update(&props);
608     ASSERT_EQ(props.chargerAcOnline, true);
609     ASSERT_EQ(props.chargerUsbOnline, true);
610 }
611 
TEST_F(BatteryDefenderTest,PropsOverride_InitConnected_allOnlineFalse)612 TEST_F(BatteryDefenderTest, PropsOverride_InitConnected_allOnlineFalse) {
613     enableDefender();
614     usbPresent();
615     defaultThresholds();
616     initTo1000sConnectedCapacityReached();
617 
618     InSequence s;
619 
620     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
621     battDefender->update(&props);
622 
623     props.chargerAcOnline = false;
624     props.chargerUsbOnline = false;
625     testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
626     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
627     battDefender->update(&props);
628     ASSERT_EQ(props.chargerAcOnline, false);
629     ASSERT_EQ(props.chargerUsbOnline, true);
630 
631     props.chargerAcOnline = false;
632     props.chargerUsbOnline = false;
633     battDefender->update(&props);
634     ASSERT_EQ(props.chargerAcOnline, false);
635     ASSERT_EQ(props.chargerUsbOnline, true);
636 }
637 
TEST_F(BatteryDefenderTest,PropsOverride_InitConnected_usbOnline)638 TEST_F(BatteryDefenderTest, PropsOverride_InitConnected_usbOnline) {
639     enableDefender();
640     usbPresent();
641     defaultThresholds();
642     initTo1000sConnectedCapacityReached();
643 
644     InSequence s;
645 
646     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
647     battDefender->update(&props);
648 
649     props.chargerAcOnline = false;
650     props.chargerUsbOnline = true;
651     testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
652     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
653     battDefender->update(&props);
654     ASSERT_EQ(props.chargerAcOnline, false);
655     ASSERT_EQ(props.chargerUsbOnline, true);
656 
657     props.chargerAcOnline = false;
658     props.chargerUsbOnline = false;
659     battDefender->update(&props);
660     ASSERT_EQ(props.chargerAcOnline, false);
661     ASSERT_EQ(props.chargerUsbOnline, true);
662 }
663 
TEST_F(BatteryDefenderTest,PropsOverride_InitConnected_acOnline)664 TEST_F(BatteryDefenderTest, PropsOverride_InitConnected_acOnline) {
665     enableDefender();
666     usbPresent();
667     defaultThresholds();
668     initTo1000sConnectedCapacityReached();
669 
670     InSequence s;
671 
672     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
673     battDefender->update(&props);
674 
675     props.chargerAcOnline = true;
676     props.chargerUsbOnline = false;
677     testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
678     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
679     battDefender->update(&props);
680     ASSERT_EQ(props.chargerAcOnline, true);
681     ASSERT_EQ(props.chargerUsbOnline, false);
682 
683     props.chargerAcOnline = false;
684     props.chargerUsbOnline = false;
685     battDefender->update(&props);
686     ASSERT_EQ(props.chargerAcOnline, true);
687     ASSERT_EQ(props.chargerUsbOnline, false);
688 }
689 
TEST_F(BatteryDefenderTest,PropsOverride_InitConnected_allOnline)690 TEST_F(BatteryDefenderTest, PropsOverride_InitConnected_allOnline) {
691     enableDefender();
692     usbPresent();
693     defaultThresholds();
694     initTo1000sConnectedCapacityReached();
695 
696     InSequence s;
697 
698     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
699     battDefender->update(&props);
700 
701     props.chargerAcOnline = true;
702     props.chargerUsbOnline = true;
703     testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
704     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE")).Times(2);
705     battDefender->update(&props);
706     ASSERT_EQ(props.chargerAcOnline, true);
707     ASSERT_EQ(props.chargerUsbOnline, true);
708 
709     props.chargerAcOnline = false;
710     props.chargerUsbOnline = false;
711     battDefender->update(&props);
712     ASSERT_EQ(props.chargerAcOnline, true);
713     ASSERT_EQ(props.chargerUsbOnline, true);
714 }
715 
TEST_F(BatteryDefenderTest,PropsOverride_InitConnected_overrideHealth)716 TEST_F(BatteryDefenderTest, PropsOverride_InitConnected_overrideHealth) {
717     enableDefender();
718     usbPresent();
719     defaultThresholds();
720     initTo1000sConnectedCapacityReached();
721 
722     InSequence s;
723 
724     props.batteryHealth = android::BATTERY_HEALTH_UNKNOWN;
725     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
726     battDefender->update(&props);
727     ASSERT_EQ(props.batteryHealth, android::BATTERY_HEALTH_UNKNOWN);
728 
729     props.batteryHealth = android::BATTERY_HEALTH_UNKNOWN;
730     testvar_systemTimeSecs += DEFAULT_TIME_TO_ACTIVATE_SECONDS + 1;
731     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "ACTIVE"));
732     battDefender->update(&props);
733     ASSERT_EQ(props.batteryHealth, android::BATTERY_HEALTH_OVERHEAT);
734 }
735 
TEST_F(BatteryDefenderTest,PropsOverride_InitConnected_kernelDefend)736 TEST_F(BatteryDefenderTest, PropsOverride_InitConnected_kernelDefend) {
737     enableDefender();
738     usbPresent();
739     defaultThresholds();
740     initTo1000sConnectedCapacityReached();
741 
742     InSequence s;
743 
744     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED")).Times(3);
745     battDefender->update(&props);
746 
747     props.chargerAcOnline = true;
748     props.chargerUsbOnline = true;
749     props.batteryHealth = android::BATTERY_HEALTH_OVERHEAT;
750     battDefender->update(&props);
751     ASSERT_EQ(props.chargerAcOnline, true);
752     ASSERT_EQ(props.chargerUsbOnline, true);
753 
754     props.chargerAcOnline = false;
755     props.chargerUsbOnline = false;
756     battDefender->update(&props);
757     ASSERT_EQ(props.chargerAcOnline, true);
758     ASSERT_EQ(props.chargerUsbOnline, true);
759 }
760 
TEST_F(BatteryDefenderTest,WirelessPathUnsupported)761 TEST_F(BatteryDefenderTest, WirelessPathUnsupported) {
762     enableDefender();
763     wirelessPresent();
764     defaultThresholds();
765 
766     InSequence s;
767 
768     battDefender->setWirelessNotSupported();
769 
770     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "DISCONNECTED"));
771     battDefender->update(&props);
772 }
773 
774 }  // namespace health
775 }  // namespace pixel
776 }  // namespace google
777 }  // namespace hardware
778