Home | History | Annotate | Download | only in parser
      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.WakelockItem;
     19 import com.android.loganalysis.item.WakelockItem.WakeLockCategory;
     20 
     21 import junit.framework.TestCase;
     22 
     23 import java.util.Arrays;
     24 import java.util.List;
     25 
     26 /**
     27  * Unit tests for {@link WakelockParser}
     28  */
     29 public class WakelockParserTest extends TestCase {
     30 
     31     /**
     32      * Test that normal input is parsed.
     33      */
     34     public void testKernelWakelockParser() {
     35         List<String> inputBlock = Arrays.asList(
     36                " All kernel wake locks:",
     37                " Kernel Wake lock PowerManagerService.WakeLocks: 1h 3m 50s 5ms (8 times) realtime",
     38                " Kernel Wake lock event0-2656 : 3m 49s 268ms (2399 times) realtime",
     39                " Kernel Wake lock wlan_wd_wake: 3m 34s 639ms (1751 times) realtime",
     40                " Kernel Wake lock wlan_rx_wake: 3m 19s 887ms (225 times) realtime",
     41                " Kernel Wake lock wlan_tx_wake: 2m 19s 887ms (225 times) realtime",
     42                " Kernel Wake lock tx_wake: 1m 19s 887ms (225 times) realtime",
     43                " "
     44               );
     45 
     46         WakelockItem wakelock = new WakelockParser().parse(inputBlock);
     47 
     48         assertEquals(WakelockParser.TOP_WAKELOCK_COUNT,
     49                 wakelock.getWakeLocks(WakeLockCategory.KERNEL_WAKELOCK).size());
     50         assertEquals("event0-2656 ",
     51                 wakelock.getWakeLocks(WakeLockCategory.KERNEL_WAKELOCK).get(0).getName());
     52         assertEquals(229268, wakelock.getWakeLocks(WakeLockCategory.KERNEL_WAKELOCK).
     53                 get(0).getHeldTime());
     54         assertEquals(2399, wakelock.getWakeLocks(WakeLockCategory.KERNEL_WAKELOCK).
     55                 get(0).getLockedCount());
     56     }
     57 
     58     public void testPartialWakelockParser() {
     59         List<String> inputBlock = Arrays.asList(
     60                 " All partial wake locks:",
     61                 " Wake lock u0a7 NlpWakeLock: 8m 13s 203ms (1479 times) max=0 realtime",
     62                 " Wake lock u0a7 NlpCollectorWakeLock: 6m 29s 18ms (238 times) max=0 realtime",
     63                 " Wake lock u0a7 GCM_CONN_ALARM: 6m 8s 587ms (239 times) max=0 realtime",
     64                 " Wake lock 1000 *alarm*: 5m 11s 316ms (1469 times) max=0 realtime",
     65                 " Wake lock u10 xxx: 4m 11s 316ms (1469 times) max=0 realtime",
     66                 " Wake lock u30 cst: 2m 11s 316ms (1469 times) max=0 realtime",
     67                 "");
     68 
     69         WakelockItem wakelock = new WakelockParser().parse(inputBlock);
     70 
     71         assertEquals(WakelockParser.TOP_WAKELOCK_COUNT,
     72                 wakelock.getWakeLocks(WakeLockCategory.PARTIAL_WAKELOCK).size());
     73         assertEquals("NlpWakeLock", wakelock.getWakeLocks(WakeLockCategory.PARTIAL_WAKELOCK).
     74                 get(0).getName());
     75         assertEquals("u0a7", wakelock.getWakeLocks(WakeLockCategory.PARTIAL_WAKELOCK).
     76                 get(0).getProcessUID());
     77         assertEquals(493203, wakelock.getWakeLocks(WakeLockCategory.PARTIAL_WAKELOCK).
     78                 get(0).getHeldTime());
     79         assertEquals(1479, wakelock.getWakeLocks(WakeLockCategory.PARTIAL_WAKELOCK).
     80                 get(0).getLockedCount());
     81     }
     82 
     83     public void testPartialWakelockParserOnOldFormat() {
     84         List<String> inputBlock = Arrays.asList(
     85                 " All partial wake locks:",
     86                 " Wake lock u0a7 NlpWakeLock: 8m 13s 203ms (1479 times) realtime",
     87                 " Wake lock u0a7 NlpCollectorWakeLock: 6m 29s 18ms (238 times) realtime",
     88                 " Wake lock u0a7 GCM_CONN_ALARM: 6m 8s 587ms (239 times) realtime",
     89                 " Wake lock 1000 *alarm*: 5m 11s 316ms (1469 times) realtime",
     90                 " Wake lock u10 xxx: 4m 11s 316ms (1469 times) realtime",
     91                 " Wake lock u30 cst: 2m 11s 316ms (1469 times) realtime",
     92                 "");
     93 
     94         WakelockItem wakelock = new WakelockParser().parse(inputBlock);
     95 
     96         assertEquals(WakelockParser.TOP_WAKELOCK_COUNT,
     97                 wakelock.getWakeLocks(WakeLockCategory.PARTIAL_WAKELOCK).size());
     98         assertEquals("NlpWakeLock", wakelock.getWakeLocks(WakeLockCategory.PARTIAL_WAKELOCK).
     99                 get(0).getName());
    100         assertEquals("u0a7", wakelock.getWakeLocks(WakeLockCategory.PARTIAL_WAKELOCK).
    101                 get(0).getProcessUID());
    102         assertEquals(493203, wakelock.getWakeLocks(WakeLockCategory.PARTIAL_WAKELOCK).
    103                 get(0).getHeldTime());
    104         assertEquals(1479, wakelock.getWakeLocks(WakeLockCategory.PARTIAL_WAKELOCK).
    105                 get(0).getLockedCount());
    106     }
    107 
    108     public void testInvalidInputWakelockParser() {
    109         List<String> inputBlock = Arrays.asList(
    110              " lock PowerManagerService.WakeLocks: 1h 3m 50s 5ms (8 times) realtime",
    111              " lock event0-2656 : 3m 49s 268ms (2399 times) realtime",
    112              " lock wlan_wd_wake: 3m 34s 639ms (1751 times) realtime",
    113              " lock wlan_rx_wake: 3m 19s 887ms (225 times) realtime",
    114              " wlan_tx_wake: 2m 19s 887ms (225 times) realtime",
    115              " tx_wake: 1m 19s 887ms (225 times) realtime",
    116              " "
    117             );
    118 
    119         WakelockItem wakelock = new WakelockParser().parse(inputBlock);
    120 
    121         assertEquals(0,
    122                 wakelock.getWakeLocks(WakeLockCategory.KERNEL_WAKELOCK).size());
    123         assertEquals(0,
    124                 wakelock.getWakeLocks(WakeLockCategory.PARTIAL_WAKELOCK).size());
    125     }
    126 }
    127 
    128