1 /*
2  * Copyright (C) 2017 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 android.dumpsys.cts;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
22 import com.android.tradefed.log.LogUtil.CLog;
23 
24 import java.io.BufferedReader;
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.StringReader;
28 import java.util.HashSet;
29 import java.util.Set;
30 
31 /**
32  * Test to check the format of the dumps of the batterystats test.
33  */
34 public class BatteryStatsDumpsysTest extends BaseDumpsysTest {
35    private static final String TEST_APK = "CtsFramestatsTestApp.apk";
36     private static final String TEST_PKG = "com.android.cts.framestatstestapp";
37 
38     /**
39      * Tests the output of "dumpsys batterystats --checkin".
40      *
41      * @throws Exception
42      */
testBatterystatsOutput()43     public void testBatterystatsOutput() throws Exception {
44         String batterystats = mDevice.executeShellCommand("dumpsys batterystats --checkin");
45         assertNotNull(batterystats);
46         assertTrue(batterystats.length() > 0);
47 
48         Set<String> seenTags = new HashSet<>();
49 
50         try (BufferedReader reader = new BufferedReader(
51                 new StringReader(batterystats))) {
52 
53             String line;
54             while ((line = reader.readLine()) != null) {
55                 if (line.isEmpty()) {
56                     continue;
57                 }
58 
59 
60                 try {
61                     // With a default limit of 0, empty strings at the end are discarded.
62                     // We still consider the empty string as a valid value in some cases.
63                     // Using any negative number for the limit will preserve a trailing empty string.
64                     // @see String#split(String, int)
65                     String[] parts = line.split(",", -1);
66                     assertInteger(parts[0]); // old version
67                     assertInteger(parts[1]); // UID
68                     switch (parts[2]) { // aggregation type
69                         case "i":
70                         case "l":
71                         case "c":
72                         case "u":
73                             break;
74                         default:
75                             fail("malformed stat: " + parts[2]);
76                     }
77                     assertNotNull(parts[3]);
78                     seenTags.add(parts[3]);
79 
80                     // Note the time fields are measured in milliseconds by default.
81                     switch (parts[3]) {
82                         case "vers":
83                             checkVersion(parts);
84                             break;
85                         case "uid":
86                             checkUid(parts);
87                             break;
88                         case "apk":
89                             checkApk(parts);
90                             break;
91                         case "pr":
92                             checkProcess(parts);
93                             break;
94                         case "sr":
95                             checkSensor(parts);
96                             break;
97                         case "vib":
98                             checkVibrator(parts);
99                             break;
100                         case "fg":
101                             checkForegroundActivity(parts);
102                             break;
103                         case "fgs":
104                             checkForegroundService(parts);
105                             break;
106                         case "st":
107                             checkStateTime(parts);
108                             break;
109                         case "wl":
110                             checkWakelock(parts);
111                             break;
112                         case "awl":
113                             checkAggregatedWakelock(parts);
114                             break;
115                         case "sy":
116                             checkSync(parts);
117                             break;
118                         case "jb":
119                             checkJob(parts);
120                             break;
121                         case "jbc":
122                             checkJobCompletion(parts);
123                             break;
124                         case "jbd":
125                             checkJobsDeferred(parts);
126                             break;
127                         case "kwl":
128                             checkKernelWakelock(parts);
129                             break;
130                         case "wr":
131                             checkWakeupReason(parts);
132                             break;
133                         case "nt":
134                             checkNetwork(parts);
135                             break;
136                         case "ua":
137                             checkUserActivity(parts);
138                             break;
139                         case "bt":
140                             checkBattery(parts);
141                             break;
142                         case "dc":
143                             checkBatteryDischarge(parts);
144                             break;
145                         case "lv":
146                             checkBatteryLevel(parts);
147                             break;
148                         case "wfl":
149                             checkWifi(parts);
150                             break;
151                         case "m":
152                             checkMisc(parts);
153                             break;
154                         case "gn":
155                             checkGlobalNetwork(parts);
156                             break;
157                         case "br":
158                             checkScreenBrightness(parts);
159                             break;
160                         case "sgt":
161                         case "sgc":
162                             checkSignalStrength(parts);
163                             break;
164                         case "sst":
165                             checkSignalScanningTime(parts);
166                             break;
167                         case "dct":
168                         case "dcc":
169                             checkDataConnection(parts);
170                             break;
171                         case "wst":
172                         case "wsc":
173                             checkWifiState(parts);
174                             break;
175                         case "wsst":
176                         case "wssc":
177                             checkWifiSupplState(parts);
178                             break;
179                         case "wsgt":
180                         case "wsgc":
181                             checkWifiSignalStrength(parts);
182                             break;
183                         case "bst":
184                         case "bsc":
185                             checkBluetoothState(parts);
186                             break;
187                         case "blem":
188                             checkBluetoothMisc(parts);
189                             break;
190                         case "pws":
191                             checkPowerUseSummary(parts);
192                             break;
193                         case "pwi":
194                             checkPowerUseItem(parts);
195                             break;
196                         case "dsd":
197                         case "csd":
198                             checkChargeDischargeStep(parts);
199                             break;
200                         case "dtr":
201                             checkDischargeTimeRemain(parts);
202                             break;
203                         case "ctr":
204                             checkChargeTimeRemain(parts);
205                             break;
206                         case "cpu":
207                             checkUidCpuUsage(parts);
208                             break;
209                         default:
210                             break;
211                     }
212                 } catch (AssertionError e) {
213                     CLog.e("Assert fail for line <" + line + ">");
214                     throw e;
215                 }
216             }
217         }
218 
219         // spot check a few tags
220         assertSeenTag(seenTags, "vers");
221         assertSeenTag(seenTags, "bt");
222         assertSeenTag(seenTags, "dc");
223         assertSeenTag(seenTags, "m");
224     }
225 
checkVersion(String[] parts)226     private void checkVersion(String[] parts) {
227         assertEquals(8, parts.length);
228         assertInteger(parts[4]); // checkinVersion
229         assertInteger(parts[5]); // parcelVersion
230         assertNotNull(parts[6]); // startPlatformVersion
231         assertNotNull(parts[7]); // endPlatformVersion
232     }
233 
checkUid(String[] parts)234     private void checkUid(String[] parts) {
235         assertEquals(6, parts.length);
236         assertInteger(parts[4]); // uid
237         assertNotNull(parts[5]); // pkgName
238     }
239 
checkApk(String[] parts)240     private void checkApk(String[] parts) {
241         assertEquals(10, parts.length);
242         long wakeup_count = assertInteger(parts[4]); // wakeups
243         assertNotNull(parts[5]); // apk
244         assertNotNull(parts[6]); // service
245         assertInteger(parts[7]); // startTime
246         assertInteger(parts[8]); // starts
247         assertInteger(parts[9]); // launches
248 
249         // Validation check.
250         assertTrue("wakeup count must be >= 0", wakeup_count >= 0);
251     }
252 
checkProcess(String[] parts)253     private void checkProcess(String[] parts) {
254         assertTrue(parts.length >= 9);
255         assertNotNull(parts[4]); // process
256         assertInteger(parts[5]); // userMillis
257         assertInteger(parts[6]); // systemMillis
258         assertInteger(parts[7]); // foregroundMillis
259         assertInteger(parts[8]); // starts
260     }
261 
checkSensor(String[] parts)262     private void checkSensor(String[] parts) {
263         assertEquals(10, parts.length);
264         assertInteger(parts[4]); // sensorNumber
265         assertInteger(parts[5]); // totalTime
266         assertInteger(parts[6]); // count
267         assertInteger(parts[7]); // backgroundCount
268         assertInteger(parts[8]); // actualTime
269         assertInteger(parts[9]); // backgroundActualTime
270     }
271 
checkVibrator(String[] parts)272     private void checkVibrator(String[] parts) {
273         assertEquals(6, parts.length);
274         assertInteger(parts[4]); // totalTime
275         assertInteger(parts[5]); // count
276     }
277 
checkForegroundActivity(String[] parts)278     private void checkForegroundActivity(String[] parts) {
279         assertEquals(6, parts.length);
280         assertInteger(parts[4]); // totalTime
281         assertInteger(parts[5]); // count
282     }
283 
checkForegroundService(String[] parts)284     private void checkForegroundService(String[] parts) {
285         assertEquals(6, parts.length);
286         assertInteger(parts[4]); // totalTime
287         assertInteger(parts[5]); // count
288     }
289 
checkStateTime(String[] parts)290     private void checkStateTime(String[] parts) {
291         assertEquals(11, parts.length);
292         assertInteger(parts[4]);  // top
293         assertInteger(parts[5]);  // foreground_service
294         assertInteger(parts[6]);  // foreground
295         assertInteger(parts[7]);  // background
296         assertInteger(parts[8]);  // top_sleeping
297         assertInteger(parts[9]);  // heavy_weight
298         assertInteger(parts[10]); // cached
299     }
300 
checkWakelock(String[] parts)301     private void checkWakelock(String[] parts) {
302         assertEquals(29, parts.length);
303         assertNotNull(parts[4]);      // wakelock
304 
305         assertInteger(parts[5]);      // full totalTime
306         assertEquals("f", parts[6]);  // full
307         long full_count = assertInteger(parts[7]);      // full count
308         assertInteger(parts[8]);      // current duration
309         assertInteger(parts[9]);      // max duration
310         assertInteger(parts[10]);     // total duration
311 
312         assertInteger(parts[11]);      // partial totalTime
313         assertEquals("p", parts[12]);  // partial
314         long partial_count = assertInteger(parts[13]);     // partial count
315         assertInteger(parts[14]);      // current duration
316         assertInteger(parts[15]);      // max duration
317         assertInteger(parts[16]);      // total duration
318 
319         assertInteger(parts[17]);      // background partial totalTime
320         assertEquals("bp", parts[18]); // background partial
321         long bg_partial_count = assertInteger(parts[19]);     // background partial count
322         assertInteger(parts[20]);      // current duration
323         assertInteger(parts[21]);      // max duration
324         assertInteger(parts[22]);      // total duration
325 
326         assertInteger(parts[23]);      // window totalTime
327         assertEquals("w", parts[24]);  // window
328         long window_count = assertInteger(parts[25]);     // window count
329         assertInteger(parts[26]);      // current duration
330         assertInteger(parts[27]);      // max duration
331         assertInteger(parts[28]);      // total duration
332 
333         // Validation checks.
334         assertTrue("full wakelock count must be >= 0", full_count >= 0);
335         assertTrue("partial wakelock count must be >= 0", partial_count >= 0);
336         assertTrue("background partial wakelock count must be >= 0", bg_partial_count >= 0);
337         assertTrue("window wakelock count must be >= 0", window_count >= 0);
338     }
339 
checkAggregatedWakelock(String[] parts)340     private void checkAggregatedWakelock(String[] parts) {
341         assertEquals(6, parts.length);
342         assertInteger(parts[4]); // total time
343         assertInteger(parts[5]); // background time
344     }
345 
checkSync(String[] parts)346     private void checkSync(String[] parts) {
347         assertEquals(9, parts.length);
348         assertNotNull(parts[4]); // sync
349         assertInteger(parts[5]); // totalTime
350         assertInteger(parts[6]); // count
351         assertInteger(parts[7]); // bgTime
352         assertInteger(parts[8]); // bgCount
353     }
354 
checkJob(String[] parts)355     private void checkJob(String[] parts) {
356         assertEquals(9, parts.length);
357         assertNotNull(parts[4]); // job
358         assertInteger(parts[5]); // totalTime
359         assertInteger(parts[6]); // count
360         assertInteger(parts[7]); // bgTime
361         assertInteger(parts[8]); // bgCount
362     }
363 
checkJobCompletion(String[] parts)364     private void checkJobCompletion(String[] parts) {
365         // This line contains a number for each job cancel reason.
366         // (See JobParameters.JOB_STOP_REASON_CODES), and future mainline updates may introudce
367         // more codes, so we have no upper bound for the number of columns.
368         assertThat(parts.length).isAtLeast(11);
369         assertNotNull(parts[4]); // job
370 
371         // Values for each of JOB_STOP_REASON_CODES.
372         for (int i = 5; i < parts.length; i++) {
373             assertInteger(parts[i]);
374         }
375     }
376 
checkJobsDeferred(String[] parts)377     private void checkJobsDeferred(String[] parts) {
378         assertEquals(12, parts.length);
379         assertInteger(parts[4]); // jobsDeferredEventCount
380         assertInteger(parts[5]); // jobsDeferredCount
381         assertInteger(parts[6]); // totalLatencyMillis
382         assertInteger(parts[7]); // count at latency < 1 hr
383         assertInteger(parts[8]); // count at latency 1-2 hrs
384         assertInteger(parts[9]); // count at latency 2-4 hrs
385         assertInteger(parts[10]); // count at latency 4-8 hrs
386         assertInteger(parts[11]); // count at latency 8+ hrs
387     }
388 
checkKernelWakelock(String[] parts)389     private void checkKernelWakelock(String[] parts) {
390         assertTrue(parts.length >= 7);
391 	assertNotNull(parts[4]); // Kernel wakelock
392 	assertInteger(parts[parts.length-2]); // totalTime
393         assertInteger(parts[parts.length-1]); // count
394     }
395 
checkWakeupReason(String[] parts)396     private void checkWakeupReason(String[] parts) {
397         assertTrue(parts.length >= 7);
398         for (int i = 4; i < parts.length-2; i++) {
399             assertNotNull(parts[i]); // part of wakeup
400         }
401         assertInteger(parts[parts.length-2]); // totalTime
402         assertInteger(parts[parts.length-1]); // count
403     }
404 
checkNetwork(String[] parts)405     private void checkNetwork(String[] parts) {
406         assertEquals(26, parts.length);
407         long mbRx = assertInteger(parts[4]);  // mobileBytesRx
408         long mbTx = assertInteger(parts[5]);  // mobileBytesTx
409         long wbRx = assertInteger(parts[6]);  // wifiBytesRx
410         long wbTx = assertInteger(parts[7]);  // wifiBytesTx
411         long mpRx = assertInteger(parts[8]);  // mobilePacketsRx
412         long mpTx = assertInteger(parts[9]);  // mobilePacketsTx
413         long wpRx = assertInteger(parts[10]); // wifiPacketsRx
414         long wpTx = assertInteger(parts[11]); // wifiPacketsTx
415         assertInteger(parts[12]); // mobileActiveTime (usec)
416         assertInteger(parts[13]); // mobileActiveCount
417         assertInteger(parts[14]); // btBytesRx
418         assertInteger(parts[15]); // btBytesTx
419         assertInteger(parts[16]); // mobileWakeup
420         assertInteger(parts[17]); // wifiWakeup
421         long mbBgRx = assertInteger(parts[18]);  // mobileBytesRx
422         long mbBgTx = assertInteger(parts[19]);  // mobileBytesTx
423         long wbBgRx = assertInteger(parts[20]);  // wifiBytesRx
424         long wbBgTx = assertInteger(parts[21]);  // wifiBytesTx
425         long mpBgRx = assertInteger(parts[22]);  // mobilePacketsRx
426         long mpBgTx = assertInteger(parts[23]);  // mobilePacketsTx
427         long wpBgRx = assertInteger(parts[24]); // wifiPacketsRx
428         long wpBgTx = assertInteger(parts[25]); // wifiPacketsTx
429 
430         // Assuming each packet contains some bytes, bytes >= packets >= 0.
431         assertTrue("mobileBytesRx must be >= mobilePacketsRx", mbRx >= mpRx);
432         assertTrue("mobilePacketsRx must be >= 0", mpRx >= 0);
433         assertTrue("mobileBytesTx must be >= mobilePacketsTx", mbTx >= mpTx);
434         assertTrue("mobilePacketsTx must be >= 0", mpTx >= 0);
435         assertTrue("wifiBytesRx must be >= wifiPacketsRx", wbRx >= wpRx);
436         assertTrue("wifiPacketsRx must be >= 0", wpRx >= 0);
437         assertTrue("wifiBytesTx must be >= wifiPacketsTx", wbTx >= wpTx);
438         assertTrue("wifiPacketsTx must be >= 0", wpTx >= 0);
439         // Totals should be greater than or equal to background data numbers
440         assertTrue("mobileBytesRx must be >= mobileBytesBgRx", mbRx >= mbBgRx);
441         assertTrue("mobilePacketsRx must be >= mobilePacketsBgRx", mpRx >= mpBgRx);
442         assertTrue("mobileBytesTx must be >= mobileBytesBgTx", mbTx >= mbBgTx);
443         assertTrue("mobilePacketsTx must be >= mobilePacketsBgTx", mpTx >= mpBgTx);
444         assertTrue("wifiBytesRx must be >= wifiBytesBgRx", wbRx >= wbBgRx);
445         assertTrue("wifiPacketsRx must be >= wifiPacketsBgRx", wpRx >= wpBgRx);
446         assertTrue("wifiBytesTx must be >= wifiBytesBgTx", wbTx >= wbBgTx);
447         assertTrue("wifiPacketsTx must be >= wifiPacketsBgTx", wpTx >= wpBgTx);
448     }
449 
checkUserActivity(String[] parts)450     private void checkUserActivity(String[] parts) {
451         assertEquals(9, parts.length);
452         assertInteger(parts[4]); // other
453         assertInteger(parts[5]); // button
454         assertInteger(parts[6]); // touch
455         assertInteger(parts[7]); // accessibility
456         assertInteger(parts[8]); // attention
457     }
458 
checkBattery(String[] parts)459     private void checkBattery(String[] parts) {
460         assertEquals(16, parts.length);
461         if (!parts[4].equals("N/A")) {
462             assertInteger(parts[4]);  // startCount
463         }
464         long bReal = assertInteger(parts[5]);  // batteryRealtime
465         long bUp = assertInteger(parts[6]);  // batteryUptime
466         long tReal = assertInteger(parts[7]);  // totalRealtime
467         long tUp = assertInteger(parts[8]);  // totalUptime
468         assertInteger(parts[9]);  // startClockTime
469         long bOffReal = assertInteger(parts[10]); // batteryScreenOffRealtime
470         long bOffUp = assertInteger(parts[11]); // batteryScreenOffUptime
471         long bEstCap = assertInteger(parts[12]); // batteryEstimatedCapacity
472         assertInteger(parts[13]); // minLearnedBatteryCapacity
473         assertInteger(parts[14]); // maxLearnedBatteryCapacity
474         long bDoze = assertInteger(parts[15]); // screenDozeTime
475 
476         // The device cannot be up more than there are real-world seconds.
477         assertTrue("batteryRealtime must be >= batteryUptime", bReal >= bUp);
478         assertTrue("totalRealtime must be >= totalUptime", tReal >= tUp);
479         assertTrue("batteryScreenOffRealtime must be >= batteryScreenOffUptime",
480                 bOffReal >= bOffUp);
481 
482         // total >= battery >= battery screen-off >= 0
483         assertTrue("totalRealtime must be >= batteryRealtime", tReal >= bReal);
484         assertTrue("batteryRealtime must be >= batteryScreenOffRealtime", bReal >= bOffReal);
485         assertTrue("batteryScreenOffRealtime must be >= 0", bOffReal >= 0);
486         assertTrue("totalUptime must be >= batteryUptime", tUp >= bUp);
487         assertTrue("batteryUptime must be >= batteryScreenOffUptime", bUp >= bOffUp);
488         assertTrue("batteryScreenOffUptime must be >= 0", bOffUp >= 0);
489         assertTrue("batteryEstimatedCapacity must be >= 0", bEstCap >= 0);
490         assertTrue("screenDozeTime must be >= 0", bDoze >= 0);
491         assertTrue("screenDozeTime must be <= batteryScreenOffRealtime", bDoze <= bOffReal);
492     }
493 
checkBatteryDischarge(String[] parts)494     private void checkBatteryDischarge(String[] parts) {
495         assertEquals(14, parts.length);
496         assertInteger(parts[4]); // low
497         assertInteger(parts[5]); // high
498         assertInteger(parts[6]); // screenOn
499         assertInteger(parts[7]); // screenOff
500         assertInteger(parts[8]); // dischargeMah
501         assertInteger(parts[9]); // dischargeScreenOffMah
502         assertInteger(parts[10]); // dischargeDozeCount
503         assertInteger(parts[11]); // dischargeDozeMah
504         assertInteger(parts[12]); // dischargeLightDozeMah
505         assertInteger(parts[13]); // dischargeDeepDozeMah
506     }
507 
checkBatteryLevel(String[] parts)508     private void checkBatteryLevel(String[] parts) {
509         assertEquals(6, parts.length);
510         assertInteger(parts[4]); // startLevel
511         assertInteger(parts[5]); // currentLevel
512     }
513 
checkWifi(String[] parts)514     private void checkWifi(String[] parts) {
515         assertEquals(14, parts.length);
516         assertInteger(parts[4]); // fullWifiLockOnTime (usec)
517         assertInteger(parts[5]); // wifiScanTime (usec)
518         assertInteger(parts[6]); // uidWifiRunningTime (usec)
519         assertInteger(parts[7]); // wifiScanCount
520         // Fields for parts[8 and 9 and 10] are deprecated.
521         assertInteger(parts[11]); // wifiScanCountBg
522         assertInteger(parts[12]); // wifiScanActualTimeMs (msec)
523         assertInteger(parts[13]); // wifiScanActualTimeMsBg (msec)
524     }
525 
checkMisc(String[] parts)526     private void checkMisc(String[] parts) {
527         assertTrue(parts.length >= 19);
528         assertInteger(parts[4]);      // screenOnTime
529         assertInteger(parts[5]);      // phoneOnTime
530         assertInteger(parts[6]);      // fullWakeLockTimeTotal
531         assertInteger(parts[7]);      // partialWakeLockTimeTotal
532         assertInteger(parts[8]);      // mobileRadioActiveTime
533         assertInteger(parts[9]);      // mobileRadioActiveAdjustedTime
534         assertInteger(parts[10]);     // interactiveTime
535         assertInteger(parts[11]);     // lowPowerModeEnabledTime
536         assertInteger(parts[12]);     // connChanges
537         assertInteger(parts[13]);     // deviceIdleModeEnabledTime
538         assertInteger(parts[14]);     // deviceIdleModeEnabledCount
539         assertInteger(parts[15]);     // deviceIdlingTime
540         assertInteger(parts[16]);     // deviceIdlingCount
541         assertInteger(parts[17]);     // mobileRadioActiveCount
542         assertInteger(parts[18]);     // mobileRadioActiveUnknownTime
543     }
544 
checkGlobalNetwork(String[] parts)545     private void checkGlobalNetwork(String[] parts) {
546         assertEquals(14, parts.length);
547         assertInteger(parts[4]);  // mobileRxTotalBytes
548         assertInteger(parts[5]);  // mobileTxTotalBytes
549         assertInteger(parts[6]);  // wifiRxTotalBytes
550         assertInteger(parts[7]);  // wifiTxTotalBytes
551         assertInteger(parts[8]);  // mobileRxTotalPackets
552         assertInteger(parts[9]);  // mobileTxTotalPackets
553         assertInteger(parts[10]); // wifiRxTotalPackets
554         assertInteger(parts[11]); // wifiTxTotalPackets
555         assertInteger(parts[12]); // btRxTotalBytes
556         assertInteger(parts[13]); // btTxTotalBytes
557     }
558 
checkScreenBrightness(String[] parts)559     private void checkScreenBrightness(String[] parts) {
560         assertEquals(9, parts.length);
561         assertInteger(parts[4]); // dark
562         assertInteger(parts[5]); // dim
563         assertInteger(parts[6]); // medium
564         assertInteger(parts[7]); // light
565         assertInteger(parts[8]); // bright
566     }
567 
checkSignalStrength(String[] parts)568     private void checkSignalStrength(String[] parts) {
569         assertTrue(parts.length >= 9);
570         assertInteger(parts[4]); // none
571         assertInteger(parts[5]); // poor
572         assertInteger(parts[6]); // moderate
573         assertInteger(parts[7]); // good
574         assertInteger(parts[8]); // great
575     }
576 
checkSignalScanningTime(String[] parts)577     private void checkSignalScanningTime(String[] parts) {
578         assertEquals(5, parts.length);
579         assertInteger(parts[4]); // signalScanningTime
580     }
581 
checkDataConnection(String[] parts)582     private void checkDataConnection(String[] parts) {
583         assertEquals(27, parts.length);
584         assertInteger(parts[4]);  // none
585         assertInteger(parts[5]);  // gprs
586         assertInteger(parts[6]);  // edge
587         assertInteger(parts[7]);  // umts
588         assertInteger(parts[8]);  // cdma
589         assertInteger(parts[9]);  // evdo_0
590         assertInteger(parts[10]); // evdo_A
591         assertInteger(parts[11]); // 1xrtt
592         assertInteger(parts[12]); // hsdpa
593         assertInteger(parts[13]); // hsupa
594         assertInteger(parts[14]); // hspa
595         assertInteger(parts[15]); // iden
596         assertInteger(parts[16]); // evdo_b
597         assertInteger(parts[17]); // lte
598         assertInteger(parts[18]); // ehrpd
599         assertInteger(parts[19]); // hspap
600         assertInteger(parts[20]); // gsm
601         assertInteger(parts[21]); // td_scdma
602         assertInteger(parts[22]); // iwlan
603         assertInteger(parts[23]); // lte_ca
604         assertInteger(parts[24]); // nr
605         assertInteger(parts[25]); // emngcy
606         assertInteger(parts[26]); // other
607     }
608 
checkWifiState(String[] parts)609     private void checkWifiState(String[] parts) {
610         assertEquals(12, parts.length);
611         assertInteger(parts[4]);  // off
612         assertInteger(parts[5]);  // scanning
613         assertInteger(parts[6]);  // no_net
614         assertInteger(parts[7]);  // disconn
615         assertInteger(parts[8]);  // sta
616         assertInteger(parts[9]);  // p2p
617         assertInteger(parts[10]); // sta_p2p
618         assertInteger(parts[11]); // soft_ap
619     }
620 
checkWifiSupplState(String[] parts)621     private void checkWifiSupplState(String[] parts) {
622         assertEquals(17, parts.length);
623         assertInteger(parts[4]);  // inv
624         assertInteger(parts[5]);  // dsc
625         assertInteger(parts[6]);  // dis
626         assertInteger(parts[7]);  // inact
627         assertInteger(parts[8]);  // scan
628         assertInteger(parts[9]);  // auth
629         assertInteger(parts[10]); // ascing
630         assertInteger(parts[11]); // asced
631         assertInteger(parts[12]); // 4-way
632         assertInteger(parts[13]); // group
633         assertInteger(parts[14]); // compl
634         assertInteger(parts[15]); // dorm
635         assertInteger(parts[16]); // uninit
636     }
637 
checkWifiSignalStrength(String[] parts)638     private void checkWifiSignalStrength(String[] parts) {
639         assertEquals(9, parts.length);
640         assertInteger(parts[4]); // none
641         assertInteger(parts[5]); // poor
642         assertInteger(parts[6]); // moderate
643         assertInteger(parts[7]); // good
644         assertInteger(parts[8]); // great
645     }
646 
checkBluetoothState(String[] parts)647     private void checkBluetoothState(String[] parts) {
648         assertEquals(8, parts.length);
649         assertInteger(parts[4]); // inactive
650         assertInteger(parts[5]); // low
651         assertInteger(parts[6]); // med
652         assertInteger(parts[7]); // high
653     }
654 
checkPowerUseSummary(String[] parts)655     private void checkPowerUseSummary(String[] parts) {
656         assertEquals(8, parts.length);
657         assertDouble(parts[4]); // batteryCapacity
658         assertDouble(parts[5]); // computedPower
659         assertDouble(parts[6]); // minDrainedPower
660         assertDouble(parts[7]); // maxDrainedPower
661     }
662 
checkPowerUseItem(String[] parts)663     private void checkPowerUseItem(String[] parts) {
664         assertEquals(9, parts.length);
665         assertNotNull(parts[4]); // label
666         final double totalPowerMah = assertDouble(parts[5]);  // totalPowerMah
667         final long shouldHide = assertInteger(parts[6]);  // shouldHide (0 or 1)
668         final double screenPowerMah = assertDouble(parts[7]);  // screenPowerMah
669         final double proportionalSmearMah = assertDouble(parts[8]);  // proportionalSmearMah
670 
671         assertTrue("powerUseItem totalPowerMah must be >= 0", totalPowerMah >= 0);
672         assertTrue("powerUseItem screenPowerMah must be >= 0", screenPowerMah >= 0);
673         assertTrue("powerUseItem proportionalSmearMah must be >= 0", proportionalSmearMah >= 0);
674         assertTrue("powerUseItem shouldHide must be 0 or 1", shouldHide == 0 || shouldHide == 1);
675 
676         // Largest current Android battery is ~5K. 100K shouldn't get made for a while.
677         assertTrue("powerUseItem totalPowerMah is expected to be <= 100000", totalPowerMah <= 100000);
678     }
679 
checkChargeDischargeStep(String[] parts)680     private void checkChargeDischargeStep(String[] parts) {
681         assertEquals(9, parts.length);
682         assertInteger(parts[4]); // duration
683         if (!parts[5].equals("?")) {
684             assertInteger(parts[5]); // level
685         }
686         assertNotNull(parts[6]); // screen
687         assertNotNull(parts[7]); // power-save
688         assertNotNull(parts[8]); // device-idle
689     }
690 
checkDischargeTimeRemain(String[] parts)691     private void checkDischargeTimeRemain(String[] parts) {
692         assertEquals(5, parts.length);
693         assertInteger(parts[4]); // batteryTimeRemaining
694     }
695 
checkChargeTimeRemain(String[] parts)696     private void checkChargeTimeRemain(String[] parts) {
697         assertEquals(5, parts.length);
698         assertInteger(parts[4]); // chargeTimeRemaining
699     }
700 
checkUidCpuUsage(String[] parts)701     private void checkUidCpuUsage(String[] parts) {
702         assertTrue(parts.length >= 6);
703         assertInteger(parts[4]); // user time
704         assertInteger(parts[5]); // system time
705     }
706 
checkBluetoothMisc(String[] parts)707     private void checkBluetoothMisc(String[] parts) {
708         assertEquals(15, parts.length);
709         assertInteger(parts[4]); // totalTime
710         assertInteger(parts[5]); // count
711         assertInteger(parts[6]); // countBg
712         assertInteger(parts[7]); // actualTime
713         assertInteger(parts[8]); // actualTimeBg
714         assertInteger(parts[9]); // resultsCount
715         assertInteger(parts[10]); // resultsCountBg
716         assertInteger(parts[11]); // unoptimizedScanTotalTime
717         assertInteger(parts[12]); // unoptimizedScanTotalTimeBg
718         assertInteger(parts[13]); // unoptimizedScanMaxTime
719         assertInteger(parts[14]); // unoptimizedScanMaxTimeBg
720     }
721 }
722