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.BatteryStatsSummaryInfoItem; 19 20 import junit.framework.TestCase; 21 22 import java.util.Arrays; 23 import java.util.GregorianCalendar; 24 import java.util.List; 25 import java.util.TimeZone; 26 27 /** 28 * Unit tests for {@link BatteryStatsSummaryInfoParser} 29 */ 30 public class BatteryStatsSummaryInfoParserTest extends TestCase { 31 32 /** 33 * Test that normal input is parsed. 34 */ testBatteryStatsSummaryInfoParser()35 public void testBatteryStatsSummaryInfoParser() { 36 List<String> inputBlock = Arrays.asList( 37 "Battery History (37% used, 95KB used of 256KB, 166 strings using 15KB):", 38 " 0 (9) RESET:TIME: 2014-12-09-11-33-29", 39 " +1s067ms (1) 100 c0500020 -wifi_full_lock -wifi_scan", 40 " +3s297ms (2) 100 80400020 -wake_lock -screen", 41 " +30m02s075ms (1) 100 c0500020 wifi_signal_strength=4 wifi_suppl=completed", 42 " +30m03s012ms (2) 099 c0500020 temp=306 volt=4217", 43 " +33m48s967ms (1) 099 f8400020 +wifi_scan", 44 " +33m49s335ms (2) 098 f0400020 temp=324 -wifi_scan", 45 " +1h07m27s735ms (1) 098 80400020 -wake_lock", 46 " +1h07m27s950ms (2) 097 c0400020", 47 " +1h07m29s000ms (2) 097 c0400020 -sync=u0a41:\"gmail-ls/com.google/a@g", 48 " +1h25m34s877ms (2) 097 00400020 -running wake_reason=0:200:qcom,smd-rpm", 49 " +1h25m41s948ms (2) 096 80400020 wifi_suppl=associated", 50 " +2h13m40s055ms (1) 096 00400018 -running", 51 " +2h13m40s570ms (2) 095 c0400008 temp=304 volt=4167", 52 " +2h56m50s792ms (1) 095 80400020 -wake_lock", 53 " +2h56m50s967ms (2) 094 00400020 temp=317 -running", 54 " +3h38m57s986ms (2) 094 80400020 +running wake_reason=0:289:bcmsdh_sdmmc", 55 " +3h38m58s241ms (2) 093 00400020 temp=327 -running", 56 " +3h56m33s329ms (1) 093 00400020 -running -wake_lock", 57 " +3h56m43s245ms (2) 092 00400020 -running", 58 " +4h13m00s551ms (1) 092 00400020 -running -wake_lock", 59 " +4h13m24s250ms (2) 091 00400020 -running", 60 " +4h34m52s233ms (2) 091 80400020 +running wake_reason=0:289:bcmsdh_sdmmc", 61 " +4h34m52s485ms (3) 090 00400020 -running wake_reason=0:200:qcom,smd-rpm", 62 " +4h57m20s644ms (1) 090 00400020 -running", 63 " +4h57m38s484ms (2) 089 00400020 -running", 64 " +5h20m58s226ms (1) 089 80400020 +running wifi_suppl=associated", 65 " +5h21m03s909ms (1) 088 80400020 -wake_lock -wifi_full_lock", 66 " +5h40m38s169ms (2) 088 c0500020 +top=u0a19:com.google.android.googlequick", 67 " +5h40m38s479ms (2) 087 c0500020 volt=4036", 68 " +6h16m45s248ms (2) 087 d0440020 -sync=u0a41:gmail-ls/com.google/avellore@go", 69 " +6h16m45s589ms (2) 086 d0440020 volt=4096", 70 " +6h52m43s316ms (1) 086 80400020 -wake_lock", 71 " +6h53m18s952ms (2) 085 c0400020", 72 " +7h24m02s415ms (1) 085 80400020 -wake_lock", 73 " +7h24m02s904ms (3) 084 c0400020 volt=4105 +wake_lock=u0a7:NlpWakeLock", 74 " +7h29m10s379ms (1) 084 00400020 -running -wake_lock", 75 " +7h29m11s841ms (2) 083 00400020 temp=317 volt=4047 -running", 76 " +7h41m08s963ms (1) 083 00400020 -running", 77 " +7h41m20s494ms (2) 082 00400020 temp=300 -running", 78 " +7h54m57s288ms (1) 082 52441420 -running", 79 " +7h55m00s801ms (1) 081 52441420 -running", 80 " +8h02m18s594ms (1) 081 50440020 -running", 81 " +8h02m23s493ms (2) 080 50440020 temp=313 -running"); 82 83 BatteryStatsSummaryInfoItem summary = new BatteryStatsSummaryInfoParser().parse(inputBlock); 84 85 assertEquals("The battery dropped a level 24 mins on average", 86 summary.getBatteryDischargeRate()); 87 88 // Get the current timezone short name (PST, GMT) to properly output the time as expected. 89 String timezone = 90 new GregorianCalendar().getTimeZone().getDisplayName(false, TimeZone.SHORT); 91 assertEquals( 92 String.format( 93 "The peak discharge time was during Tue Dec 09 16:31:07 %s 2014 to " 94 + "Tue Dec 09 19:35:52 %s 2014 where battery dropped from 89 to 80", 95 timezone, timezone), 96 summary.getPeakDischargeTime()); 97 } 98 testNoBatteryDischarge()99 public void testNoBatteryDischarge() { 100 List<String> inputBlock = Arrays.asList( 101 "Battery History (37% used, 95KB used of 256KB, 166 strings using 15KB):", 102 " 0 (9) RESET:TIME: 2014-12-09-11-33-29"); 103 BatteryStatsSummaryInfoItem summary = new BatteryStatsSummaryInfoParser().parse(inputBlock); 104 105 assertEquals("The battery did not discharge", summary.getBatteryDischargeRate()); 106 assertEquals("The battery did not discharge", summary.getPeakDischargeTime()); 107 } 108 } 109 110