1 /*
2  * Copyright (C) 2016 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 package com.android.internal.os;
18 
19 import android.os.BatteryStats;
20 import android.os.Parcel;
21 
22 import androidx.test.filters.SmallTest;
23 
24 import junit.framework.Assert;
25 import junit.framework.TestCase;
26 
27 /**
28  * Provides test cases for android.os.BatteryStats.
29  */
30 public class BatteryStatsServTest extends TestCase {
31     private static final String TAG = "BatteryStatsServTest";
32 
33     public static class TestServ extends BatteryStatsImpl.Uid.Pkg.Serv {
TestServ(MockBatteryStatsImpl bsi)34         TestServ(MockBatteryStatsImpl bsi) {
35             super(bsi);
36         }
37 
populate()38         void populate() {
39             mStartTime = 1010;
40             mRunningSince = 2021;
41             mRunning = true;
42             mStarts = 4042;
43             mLaunchedTime = 5053;
44             mLaunchedSince = 6064;
45             mLaunched = true;
46             mLaunches = 8085;
47         }
48 
getStartTime()49         long getStartTime() {
50             return mStartTime;
51         }
52 
getRunningSince()53         long getRunningSince() {
54             return mRunningSince;
55         }
56 
setRunning(boolean val)57         void setRunning(boolean val) {
58             mRunning = val;
59         }
60 
getRunning()61         boolean getRunning() {
62             return mRunning;
63         }
64 
getStarts()65         int getStarts() {
66             return mStarts;
67         }
68 
getLaunchedTime()69         long getLaunchedTime() {
70             return mLaunchedTime;
71         }
72 
getLaunchedSince()73         long getLaunchedSince() {
74             return mLaunchedSince;
75         }
76 
setLaunched(boolean val)77         void setLaunched(boolean val) {
78             mLaunched = val;
79         }
80 
getLaunched()81         boolean getLaunched() {
82             return mLaunched;
83         }
84 
getLaunches()85         int getLaunches() {
86             return mLaunches;
87         }
88     }
89 
90     /**
91      * Test that the constructor and detach methods touch the time bast observer list.
92      */
93     @SmallTest
testConstructAndDetach()94     public void testConstructAndDetach() throws Exception  {
95         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl();
96 
97         TestServ serv = new TestServ(bsi);
98         Assert.assertTrue(bsi.getOnBatteryTimeBase().hasObserver(serv));
99 
100         serv.detach();
101         Assert.assertFalse(bsi.getOnBatteryTimeBase().hasObserver(serv));
102     }
103 
104     /**
105      * Test parceling and unparceling.
106      */
107     @SmallTest
testParceling()108     public void testParceling() throws Exception  {
109         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl();
110         TestServ orig = new TestServ(bsi);
111         orig.populate();
112 
113         Parcel parcel = Parcel.obtain();
114         orig.writeToParcelLocked(parcel);
115 
116         parcel.setDataPosition(0);
117 
118         TestServ serv = new TestServ(bsi);
119         serv.readFromParcelLocked(parcel);
120 
121         Assert.assertEquals(1010, serv.getStartTime());
122         Assert.assertEquals(2021, serv.getRunningSince());
123         Assert.assertTrue(serv.getRunning());
124         Assert.assertEquals(4042, serv.getStarts());
125         Assert.assertEquals(5053, serv.getLaunchedTime());
126         Assert.assertEquals(6064, serv.getLaunchedSince());
127         Assert.assertTrue(serv.getLaunched());
128         Assert.assertEquals(8085, serv.getLaunches());
129 
130         parcel.recycle();
131     }
132 
133     /**
134      * Test getLaunchTimeToNow()
135      */
136     @SmallTest
testLaunchTimeToNow()137     public void testLaunchTimeToNow() throws Exception  {
138         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl();
139         TestServ serv = new TestServ(bsi);
140 
141         serv.populate();
142         serv.setLaunched(true);
143         Assert.assertEquals(8989, serv.getLaunchTimeToNowLocked(10000));
144 
145         serv.populate();
146         serv.setLaunched(false);
147         Assert.assertEquals(5053, serv.getLaunchTimeToNowLocked(10000));
148 
149     }
150 
151     /**
152      * Test getStartTimeToNow()
153      */
154     @SmallTest
testStartTimeToNow()155     public void testStartTimeToNow() throws Exception  {
156         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl();
157         TestServ serv = new TestServ(bsi);
158 
159         serv.populate();
160         serv.setRunning(true);
161         Assert.assertEquals(18989, serv.getStartTimeToNowLocked(20000));
162 
163         serv.populate();
164         serv.setRunning(false);
165         Assert.assertEquals(1010, serv.getStartTimeToNowLocked(20000));
166     }
167 
168     /**
169      * Test startLaunchedLocked while not previously launched
170      */
171     @SmallTest
testStartLaunchedLockedWhileLaunched()172     public void testStartLaunchedLockedWhileLaunched() throws Exception  {
173         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl() {
174             @Override
175             public long getBatteryUptimeLocked() {
176                 return 777777L;
177             }
178         };
179 
180         TestServ serv = new TestServ(bsi);
181 
182         serv.populate();
183         serv.setLaunched(true);
184         serv.startLaunchedLocked();
185 
186         // No changes
187         Assert.assertEquals(1010, serv.getStartTime());
188         Assert.assertEquals(2021, serv.getRunningSince());
189         Assert.assertTrue(serv.getRunning());
190         Assert.assertEquals(4042, serv.getStarts());
191         Assert.assertEquals(5053, serv.getLaunchedTime());
192         Assert.assertEquals(6064, serv.getLaunchedSince());
193         Assert.assertTrue(serv.getLaunched());
194         Assert.assertEquals(8085, serv.getLaunches());
195     }
196 
197     /**
198      * Test startLaunchedLocked while previously launched
199      */
200     @SmallTest
testStartLaunchedLockedWhileNotLaunched()201     public void testStartLaunchedLockedWhileNotLaunched() throws Exception  {
202         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl() {
203             @Override
204             public long getBatteryUptimeLocked() {
205                 return 777777L;
206             }
207         };
208 
209         TestServ serv = new TestServ(bsi);
210 
211         serv.populate();
212         serv.setLaunched(false);
213         serv.startLaunchedLocked();
214         Assert.assertEquals(1010, serv.getStartTime());
215         Assert.assertEquals(2021, serv.getRunningSince());
216         Assert.assertTrue(serv.getRunning());
217         Assert.assertEquals(4042, serv.getStarts());
218         Assert.assertEquals(5053, serv.getLaunchedTime());
219         Assert.assertEquals(777777L, serv.getLaunchedSince()); // <-- changed
220         Assert.assertTrue(serv.getLaunched()); // <-- changed
221         Assert.assertEquals(8086, serv.getLaunches()); // <-- changed
222     }
223 
224     /**
225      * Test stopLaunchedLocked when not previously launched.
226      */
227     @SmallTest
testStopLaunchedLockedWhileNotLaunched()228     public void testStopLaunchedLockedWhileNotLaunched() throws Exception  {
229         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl() {
230             @Override
231             public long getBatteryUptimeLocked() {
232                 return 777777L;
233             }
234         };
235         TestServ serv = new TestServ(bsi);
236 
237         serv.populate();
238         serv.setLaunched(false);
239 
240         serv.stopLaunchedLocked();
241 
242         // No changes
243         Assert.assertEquals(1010, serv.getStartTime());
244         Assert.assertEquals(2021, serv.getRunningSince());
245         Assert.assertTrue(serv.getRunning());
246         Assert.assertEquals(4042, serv.getStarts());
247         Assert.assertEquals(5053, serv.getLaunchedTime());
248         Assert.assertEquals(6064, serv.getLaunchedSince());
249         Assert.assertFalse(serv.getLaunched());
250         Assert.assertEquals(8085, serv.getLaunches());
251     }
252 
253     /**
254      * Test stopLaunchedLocked when previously launched, with measurable time between
255      * start and stop.
256      */
257     @SmallTest
testStopLaunchedLockedWhileLaunchedNormal()258     public void testStopLaunchedLockedWhileLaunchedNormal() throws Exception  {
259         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl() {
260             @Override
261             public long getBatteryUptimeLocked() {
262                 return 777777L;
263             }
264         };
265         TestServ serv = new TestServ(bsi);
266 
267         serv.populate();
268         serv.setLaunched(true);
269 
270         serv.stopLaunchedLocked();
271 
272         Assert.assertEquals(1010, serv.getStartTime());
273         Assert.assertEquals(2021, serv.getRunningSince());
274         Assert.assertTrue(serv.getRunning());
275         Assert.assertEquals(4042, serv.getStarts());
276         Assert.assertEquals(777777L-6064+5053, serv.getLaunchedTime()); // <-- changed
277         Assert.assertEquals(6064, serv.getLaunchedSince());
278         Assert.assertFalse(serv.getLaunched());
279         Assert.assertEquals(8085, serv.getLaunches());
280     }
281 
282     /**
283      * Test stopLaunchedLocked when previously launched, with no measurable time between
284      * start and stop.
285      */
286     @SmallTest
testStopLaunchedLockedWhileLaunchedTooQuick()287     public void testStopLaunchedLockedWhileLaunchedTooQuick() throws Exception  {
288         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl() {
289             @Override
290             public long getBatteryUptimeLocked() {
291                 return 6064L;
292             }
293         };
294         TestServ serv = new TestServ(bsi);
295 
296         serv.populate();
297         serv.setLaunched(true);
298 
299         serv.stopLaunchedLocked();
300 
301         Assert.assertEquals(1010, serv.getStartTime());
302         Assert.assertEquals(2021, serv.getRunningSince());
303         Assert.assertTrue(serv.getRunning());
304         Assert.assertEquals(4042, serv.getStarts());
305         Assert.assertEquals(5053, serv.getLaunchedTime());
306         Assert.assertEquals(6064, serv.getLaunchedSince());
307         Assert.assertFalse(serv.getLaunched());
308         Assert.assertEquals(8085-1, serv.getLaunches()); // <-- changed
309     }
310 
311     /**
312      * Test startRunningLocked while previously running
313      */
314     @SmallTest
testStartRunningLockedWhileRunning()315     public void testStartRunningLockedWhileRunning() throws Exception  {
316         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl() {
317             @Override
318             public long getBatteryUptimeLocked() {
319                 return 777777L;
320             }
321         };
322         TestServ serv = new TestServ(bsi);
323 
324         serv.populate();
325         serv.setRunning(true);
326 
327         serv.startRunningLocked();
328 
329         // no change
330         Assert.assertEquals(1010, serv.getStartTime());
331         Assert.assertEquals(2021, serv.getRunningSince());
332         Assert.assertTrue(serv.getRunning());
333         Assert.assertEquals(4042, serv.getStarts());
334         Assert.assertEquals(5053, serv.getLaunchedTime());
335         Assert.assertEquals(6064, serv.getLaunchedSince());
336         Assert.assertTrue(serv.getLaunched());
337         Assert.assertEquals(8085, serv.getLaunches());
338     }
339 
340     /**
341      * Test startRunningLocked while not previously launched
342      */
343     @SmallTest
testStartRunningLockedWhileNotRunning()344     public void testStartRunningLockedWhileNotRunning() throws Exception  {
345         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl() {
346             @Override
347             public long getBatteryUptimeLocked() {
348                 return 777777L;
349             }
350         };
351         TestServ serv = new TestServ(bsi);
352 
353         serv.populate();
354         serv.setRunning(false);
355 
356         serv.startRunningLocked();
357 
358         Assert.assertEquals(1010, serv.getStartTime());
359         Assert.assertEquals(777777L, serv.getRunningSince());
360         Assert.assertTrue(serv.getRunning());
361         Assert.assertEquals(4042+1, serv.getStarts());
362         Assert.assertEquals(5053, serv.getLaunchedTime());
363         Assert.assertEquals(6064, serv.getLaunchedSince());
364         Assert.assertTrue(serv.getLaunched());
365         Assert.assertEquals(8085, serv.getLaunches());
366     }
367 
368     /**
369      * Test stopRunningLocked when previously launched, with measurable time between
370      * start and stop.
371      */
372     @SmallTest
testStopRunningLockedWhileRunningNormal()373     public void testStopRunningLockedWhileRunningNormal() throws Exception  {
374         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl() {
375             @Override
376             public long getBatteryUptimeLocked() {
377                 return 777777L;
378             }
379         };
380         TestServ serv = new TestServ(bsi);
381 
382         serv.populate();
383         serv.setRunning(true);
384 
385         serv.stopRunningLocked();
386 
387         Assert.assertEquals(777777L-2021+1010, serv.getStartTime()); // <-- changed
388         Assert.assertEquals(2021, serv.getRunningSince());
389         Assert.assertFalse(serv.getRunning()); // <-- changed
390         Assert.assertEquals(4042, serv.getStarts());
391         Assert.assertEquals(5053, serv.getLaunchedTime());
392         Assert.assertEquals(6064, serv.getLaunchedSince());
393         Assert.assertTrue(serv.getLaunched());
394         Assert.assertEquals(8085, serv.getLaunches());
395     }
396 
397     /**
398      * Test stopRunningLocked when previously launched, with measurable time between
399      * start and stop.
400      */
401     @SmallTest
testStopRunningLockedWhileRunningTooQuick()402     public void testStopRunningLockedWhileRunningTooQuick() throws Exception  {
403         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl() {
404             @Override
405             public long getBatteryUptimeLocked() {
406                 return 2021;
407             }
408         };
409         TestServ serv = new TestServ(bsi);
410 
411         serv.populate();
412         serv.setRunning(true);
413 
414         serv.stopRunningLocked();
415 
416         Assert.assertEquals(1010, serv.getStartTime());
417         Assert.assertEquals(2021, serv.getRunningSince());
418         Assert.assertFalse(serv.getRunning()); // <-- changed
419         Assert.assertEquals(4042-1, serv.getStarts()); // <-- changed
420         Assert.assertEquals(5053, serv.getLaunchedTime());
421         Assert.assertEquals(6064, serv.getLaunchedSince());
422         Assert.assertTrue(serv.getLaunched());
423         Assert.assertEquals(8085, serv.getLaunches());
424     }
425 
426     /**
427      * Test that getBatteryStats returns the BatteryStatsImpl passed in to the contstructor.
428      */
429     @SmallTest
testGetBatteryStats()430     public void testGetBatteryStats() throws Exception  {
431         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl();
432         TestServ serv = new TestServ(bsi);
433 
434         Assert.assertEquals(bsi, serv.getBatteryStats());
435     }
436 
437     /**
438      * Test getLaunches
439      */
440     @SmallTest
testGetLaunches()441     public void testGetLaunches() throws Exception  {
442         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl();
443         TestServ serv = new TestServ(bsi);
444         serv.populate();
445 
446         Assert.assertEquals(8085, serv.getLaunches(BatteryStats.STATS_SINCE_CHARGED));
447 
448         // No change to fields
449         Assert.assertEquals(1010, serv.getStartTime());
450         Assert.assertEquals(2021, serv.getRunningSince());
451         Assert.assertTrue(serv.getRunning());
452         Assert.assertEquals(4042, serv.getStarts());
453         Assert.assertEquals(5053, serv.getLaunchedTime());
454         Assert.assertEquals(6064, serv.getLaunchedSince());
455         Assert.assertTrue(serv.getLaunched());
456         Assert.assertEquals(8085, serv.getLaunches());
457     }
458 
459     /**
460      * Test getStartTime while running
461      */
462     @SmallTest
testGetStartTimeRunning()463     public void testGetStartTimeRunning() throws Exception  {
464         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl();
465         TestServ serv = new TestServ(bsi);
466         serv.populate();
467         serv.setRunning(true);
468 
469         final long startTimeToNow = 1010 + 20000 - 2021;
470 
471         Assert.assertEquals(startTimeToNow,
472                 serv.getStartTime(20000, BatteryStats.STATS_SINCE_CHARGED));
473 
474         // No change to fields
475         Assert.assertEquals(1010, serv.getStartTime());
476         Assert.assertEquals(2021, serv.getRunningSince());
477         Assert.assertTrue(serv.getRunning());
478         Assert.assertEquals(4042, serv.getStarts());
479         Assert.assertEquals(5053, serv.getLaunchedTime());
480         Assert.assertEquals(6064, serv.getLaunchedSince());
481         Assert.assertTrue(serv.getLaunched());
482         Assert.assertEquals(8085, serv.getLaunches());
483     }
484 
485     /**
486      * Test getStartTime while not running
487      */
488     @SmallTest
testGetStartTimeNotRunning()489     public void testGetStartTimeNotRunning() throws Exception  {
490         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl();
491         TestServ serv = new TestServ(bsi);
492         serv.populate();
493         serv.setRunning(false);
494 
495         final long startTimeToNow = 1010;
496 
497         Assert.assertEquals(startTimeToNow,
498                 serv.getStartTime(20000, BatteryStats.STATS_SINCE_CHARGED));
499 
500         // No change to fields
501         Assert.assertEquals(1010, serv.getStartTime());
502         Assert.assertEquals(2021, serv.getRunningSince());
503         Assert.assertFalse(serv.getRunning());
504         Assert.assertEquals(4042, serv.getStarts());
505         Assert.assertEquals(5053, serv.getLaunchedTime());
506         Assert.assertEquals(6064, serv.getLaunchedSince());
507         Assert.assertTrue(serv.getLaunched());
508         Assert.assertEquals(8085, serv.getLaunches());
509     }
510 
511 
512     /**
513      * Test getStarts
514      */
515     @SmallTest
testGetStarts()516     public void testGetStarts() throws Exception  {
517         MockBatteryStatsImpl bsi = new MockBatteryStatsImpl();
518         TestServ serv = new TestServ(bsi);
519         serv.populate();
520 
521         Assert.assertEquals(4042, serv.getStarts(BatteryStats.STATS_SINCE_CHARGED));
522 
523         // No change to fields
524         Assert.assertEquals(1010, serv.getStartTime());
525         Assert.assertEquals(2021, serv.getRunningSince());
526         Assert.assertTrue(serv.getRunning());
527         Assert.assertEquals(4042, serv.getStarts());
528         Assert.assertEquals(5053, serv.getLaunchedTime());
529         Assert.assertEquals(6064, serv.getLaunchedSince());
530         Assert.assertTrue(serv.getLaunched());
531         Assert.assertEquals(8085, serv.getLaunches());
532     }
533 
534 }
535 
536