1 /* 2 * Copyright (C) 2015 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 package com.android.loganalysis.parser; 17 18 import com.android.loganalysis.item.BatteryStatsDetailedInfoItem; 19 20 import junit.framework.TestCase; 21 22 import java.util.Arrays; 23 import java.util.List; 24 25 /** 26 * Unit tests for {@link BatteryStatsDetailedInfoParser} 27 */ 28 public class BatteryStatsDetailedInfoParserTest extends TestCase { 29 30 /** 31 * Test that normal input is parsed. 32 */ 33 public void testBatteryStatsDetailedInfoParser() { 34 List<String> inputBlock = Arrays.asList( 35 " Time on battery: 2h 21m 5s 622ms (12.0%) realtime, 7m 54s 146ms (0.7%) uptime", 36 " Time on battery screen off: 2h 5m 55s 3ms (1%) realtime, 7m 4s 5ms (7%) uptime", 37 " Total run time: 19h 38m 43s 650ms realtime, 17h 25m 32s 175ms uptime", 38 " All kernel wake locks:", 39 " Kernel Wake lock PowerManagerService.WakeLocks: 1h 3m 50s 5ms (8 times) realtime", 40 " Kernel Wake lock event0-2656 : 3m 49s 268ms (2399 times) realtime", 41 " Kernel Wake lock wlan_wd_wake: 3m 34s 639ms (1751 times) realtime", 42 " Kernel Wake lock wlan_rx_wake: 3m 19s 887ms (225 times) realtime", 43 " Kernel Wake lock wlan_tx_wake: 2m 19s 887ms (225 times) realtime", 44 " Kernel Wake lock tx_wake: 1m 19s 887ms (225 times) realtime", 45 " ", 46 " All partial wake locks:", 47 " Wake lock u0a7 NlpWakeLock: 8m 13s 203ms (1479 times) realtime", 48 " Wake lock u0a7 NlpCollectorWakeLock: 6m 29s 18ms (238 times) realtime", 49 " Wake lock u0a7 GCM_CONN_ALARM: 6m 8s 587ms (239 times) realtime", 50 " Wake lock 1000 *alarm*: 5m 11s 316ms (1469 times) realtime", 51 " Wake lock u10 xxx: 4m 11s 316ms (1469 times) realtime", 52 " Wake lock u30 cst: 2m 11s 316ms (1469 times) realtime", 53 " ", 54 " All wakeup reasons:", 55 " Wakeup reason 200:qcom,smd-rpm:222:fc4: 11m 49s 332ms (0 times) realtime", 56 " Wakeup reason 200:qcom,smd-rpm: 48s 45ms (0 times) realtime", 57 " Wakeup reason 2:qcom,smd-rpm:2:f0.qm,mm:22:fc4mi: 3s 417ms (0 times) realtime", 58 " Wakeup reason 188:qcom,smd-adsp:200:qcom,smd-rpm: 1s 656ms (0 times) realtime", 59 " Wakeup reason 58:qcom,smsm-modem:2:qcom,smd-rpm: 6m 16s 1ms (5 times) realtime", 60 " Wakeup reason 57:qcom,smd-modem:200:qcom,smd-rpm: 40s 995ms (0 times) realtime", 61 " Wakeup reason unknown: 8s 455ms (0 times) realtime", 62 " Wakeup reason 9:bcmsdh_sdmmc:2:qcomd-rpm:240:mso: 8m 5s 9ms (0 times) realtime", 63 " ", 64 " 0:", 65 " User activity: 2 other", 66 " Wake lock SCREEN_FROZEN realtime", 67 " Sensor 0: 9s 908ms realtime (1 times)", 68 " Sensor 1: 9s 997ms realtime (1 times)", 69 " Foreground for: 2h 21m 5s 622ms", 70 " Apk android:", 71 " 24 wakeup alarms", 72 " u0a9:", 73 " Mobile network: 8.1KB received, 1.6KB sent (packets 291 received, 342 sent)", 74 " Mobile radio active: 3m 43s 890ms (34.2%) 39x @ 354 mspp", 75 " Sensor 2: 12m 13s 15ms realtime (5 times)", 76 " Sensor 32: (not used)", 77 " Sensor 35: (not used)"); 78 79 BatteryStatsDetailedInfoItem stats = new BatteryStatsDetailedInfoParser().parse(inputBlock); 80 81 assertEquals(8465622, stats.getTimeOnBattery()); 82 assertEquals(910619, stats.getScreenOnTime()); 83 assertNotNull(stats.getWakelockItem()); 84 assertNotNull(stats.getInterruptItem()); 85 assertNotNull(stats.getProcessUsageItem()); 86 } 87 88 /** 89 * Test with missing wakelock section 90 */ 91 public void testMissingWakelockSection() { 92 List<String> inputBlock = Arrays.asList( 93 " Time on battery: 2h 21m 5s 622ms (12.0%) realtime, 7m 54s 146ms (0.7%) uptime", 94 " Time on battery screen off: 2h 5m 55s 3ms (1%) realtime, 7m 4s 5ms (7%) uptime", 95 " Total run time: 19h 38m 43s 650ms realtime, 17h 25m 32s 175ms uptime", 96 " All wakeup reasons:", 97 " Wakeup reason 200:qcom,smd-rpm:222:fc4: 11m 49s 332ms (0 times) realtime", 98 " Wakeup reason 200:qcom,smd-rpm: 48s 45ms (0 times) realtime", 99 " Wakeup reason 2:qcom,smd-rpm:2:f0.qm,mm:22:fc4mi: 3s 417ms (0 times) realtime", 100 " Wakeup reason 188:qcom,smd-adsp:200:qcom,smd-rpm: 1s 656ms (0 times) realtime", 101 " Wakeup reason 58:qcom,smsm-modem:2:qcom,smd-rpm: 6m 16s 1ms (5 times) realtime", 102 " Wakeup reason 57:qcom,smd-modem:200:qcom,smd-rpm: 40s 995ms (0 times) realtime", 103 " Wakeup reason unknown: 8s 455ms (0 times) realtime", 104 " Wakeup reason 9:bcmsdh_sdmmc:2:qcomd-rpm:240:mso: 8m 5s 9ms (0 times) realtime", 105 " ", 106 " 0:", 107 " User activity: 2 other", 108 " Wake lock SCREEN_FROZEN realtime", 109 " Sensor 0: 9s 908ms realtime (1 times)", 110 " Sensor 1: 9s 997ms realtime (1 times)", 111 " Foreground for: 2h 21m 5s 622ms", 112 " Apk android:", 113 " 24 wakeup alarms", 114 " u0a9:", 115 " Mobile network: 8.1KB received, 1.6KB sent (packets 291 received, 342 sent)", 116 " Mobile radio active: 3m 43s 890ms (34.2%) 39x @ 354 mspp", 117 " Sensor 2: 12m 13s 15ms realtime (5 times)", 118 " Sensor 32: (not used)", 119 " Sensor 35: (not used)"); 120 BatteryStatsDetailedInfoItem stats = new BatteryStatsDetailedInfoParser().parse(inputBlock); 121 122 assertEquals(8465622, stats.getTimeOnBattery()); 123 assertEquals(910619, stats.getScreenOnTime()); 124 125 assertNull(stats.getWakelockItem()); 126 127 assertNotNull(stats.getInterruptItem()); 128 assertNotNull(stats.getProcessUsageItem()); 129 } 130 } 131 132