Home | History | Annotate | Download | only in parser
      1 /*
      2  * Copyright (C) 2013 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.SmartMonkeyLogItem;
     19 import com.android.loganalysis.parser.SmartMonkeyLogParser;
     20 
     21 import java.text.ParseException;
     22 import java.util.Arrays;
     23 import java.util.List;
     24 import junit.framework.TestCase;
     25 
     26 /**
     27  * Unit tests for {@link SmartMonkeyLogParser} and {@link SmartMonkeyLogItem}
     28  */
     29 public class SmartMonkeyLogParserTest extends TestCase {
     30 
     31     /**
     32      * Test for detecting UI exceptions
     33      * @throws ParseException
     34      */
     35     public void testExceptions() throws ParseException {
     36         List<String> lines = Arrays.asList(
     37             "2013-03-04 12:33:18.789: Starting [UiAutomator Tests][com.android.cts.uiautomator]",
     38             "2013-03-04 12:33:18.792: Target invocation count: 1000",
     39             "2013-03-04 12:33:18.793: Throttle: 0 ms",
     40             "2013-03-04 12:33:19.795: [  0](Seq: -1)-Launching UiAutomator Tests",
     41             "2013-03-04 12:33:37.211: [  0](Seq:  0)-Found 6 candidates. Using index: 1",
     42             "2013-03-04 12:33:37.336: [  0](Seq:  0)-Clicking: CheckBox (760,194)",
     43             "2013-03-04 12:33:38.443: [  1](Seq:  0)-Found 6 candidates. Using index: 5",
     44             "2013-03-04 12:33:38.533: [  1](Seq:  0)-Clicking: Button~Description for Button (723,874)",
     45             "2013-03-04 12:33:39.510: [  2](Seq:  0)-UI Exception: CRASH: Unfortunately, UiAutomator Test App has stopped.",
     46             "2013-03-04 12:43:39.510: [  2](Seq:  0)-UI Exception: ANR: UiAutomator is not responding.",
     47             "2013-03-04 12:53:39.513: Invocations requested: 1000",
     48             "2013-03-04 12:53:39.518: Invocations completed: 2",
     49             "2013-03-04 12:53:39.520: Device uptime: 608193 sec, Monkey run duration: 20 sec");
     50 
     51         SmartMonkeyLogItem monkeyLog = new SmartMonkeyLogParser().parse(lines);
     52         assertNotNull(monkeyLog);
     53         assertEquals(1, monkeyLog.getCrashTimes().size());
     54         assertEquals(1, monkeyLog.getAnrTimes().size());
     55         assertEquals(SmartMonkeyLogParser.parseTime("2013-03-04 12:33:39.510"),
     56                 monkeyLog.getCrashTimes().toArray()[0]);
     57         assertEquals(SmartMonkeyLogParser.parseTime("2013-03-04 12:43:39.510"),
     58                 monkeyLog.getAnrTimes().toArray()[0]);
     59     }
     60 
     61     /**
     62      * Tests for parsing smart monkey log header
     63      * @throws ParseException
     64      */
     65     public void testHeader() throws ParseException {
     66         List<String> lines = Arrays.asList(
     67             "2013-03-04 12:33:18.789: Starting [UiAutomator Tests|YouTube][com.android.cts.uiautomator|com.google.android.youtube]",
     68             "2013-03-04 12:33:18.792: Target invocation count: 1000",
     69             "2013-03-04 12:33:18.793: Throttle: 1500 ms",
     70             "2013-03-04 12:33:18.793: Device uptime: 608173 sec",
     71             "2013-03-04 12:33:19.795: [  0](Seq: -1)-Launching UiAutomator Tests",
     72             "2013-03-04 12:33:37.211: [  0](Seq:  0)-Found 6 candidates. Using index: 1",
     73             "2013-03-04 12:33:37.336: [  0](Seq:  0)-Clicking: CheckBox (760,194)",
     74             "2013-03-04 12:33:38.443: [  1](Seq:  0)-Found 6 candidates. Using index: 5",
     75             "2013-03-04 12:33:38.533: [  1](Seq:  0)-Clicking: Button~Description for Button (723,874)",
     76             "2013-03-04 12:33:39.510: [  2](Seq:  0)-UI Exception: CRASH: Unfortunately, UiAutomator Test App has stopped.",
     77             "2013-03-04 12:43:39.510: [  2](Seq:  0)-UI Exception: ANR: UiAutomator is not responding.",
     78             "2013-03-04 12:53:39.513: Invocations requested: 1000",
     79             "2013-03-04 12:53:39.518: Invocations completed: 2",
     80             "2013-03-04 12:53:39.520: Device uptime: 608193 sec, Monkey run duration: 20 sec");
     81         SmartMonkeyLogItem monkeyLog = new SmartMonkeyLogParser().parse(lines);
     82         assertNotNull(monkeyLog);
     83         assertEquals(2, monkeyLog.getApplications().size());
     84         assertEquals("UiAutomator Tests", monkeyLog.getApplications().get(0));
     85         assertEquals("YouTube", monkeyLog.getApplications().get(1));
     86         assertEquals(2, monkeyLog.getPackages().size());
     87         assertEquals("com.android.cts.uiautomator", monkeyLog.getPackages().get(0));
     88         assertEquals("com.google.android.youtube", monkeyLog.getPackages().get(1));
     89         assertEquals(SmartMonkeyLogParser.parseTime("2013-03-04 12:33:18.789"),
     90                 monkeyLog.getStartTime());
     91         assertEquals(608173, monkeyLog.getStartUptimeDuration());
     92         assertEquals(1000, monkeyLog.getTargetInvocations());
     93         assertEquals(1500, monkeyLog.getThrottle());
     94     }
     95 
     96     /**
     97      * Test for parsing log in flight
     98      * @throws ParseException
     99      */
    100     public void testIntermidiateStop() throws ParseException {
    101         List<String> lines = Arrays.asList(
    102                 "2013-03-04 12:33:18.789: Starting [UiAutomator Tests|YouTube][com.android.cts.uiautomator|com.google.android.youtube]",
    103                 "2013-03-04 12:33:18.792: Target invocation count: 1000",
    104                 "2013-03-04 12:33:18.793: Throttle: 1500 ms",
    105                 "2013-03-04 12:33:19.795: [  0](Seq: -1)-Launching UiAutomator Tests",
    106                 "2013-03-04 12:33:37.211: [  0](Seq:  0)-Found 6 candidates. Using index: 1",
    107                 "2013-03-04 12:33:37.336: [  0](Seq:  0)-Clicking: CheckBox (760,194)",
    108                 "2013-03-04 12:33:38.443: [  1](Seq:  0)-Found 6 candidates. Using index: 5",
    109                 "2013-03-04 12:33:38.533: [ 12](Seq:  3)-Clicking: Button~Description for Button (723,874)",
    110                 "2013-03-04 12:33:39.510: [ 12](Seq:  3)-UI Exception: CRASH: Unfortunately, UiAutomator Test App has stopped.");
    111 
    112         SmartMonkeyLogItem monkeyLog = new SmartMonkeyLogParser().parse(lines);
    113         assertNotNull(monkeyLog);
    114         assertEquals(12, monkeyLog.getIntermediateCount());
    115         assertEquals(SmartMonkeyLogParser.parseTime("2013-03-04 12:33:39.510"),
    116                 monkeyLog.getIntermediateTime());
    117     }
    118 
    119     /**
    120      * Tests for parsing smart monkey log footer
    121      * @throws ParseException
    122      */
    123     public void testFooter() throws ParseException {
    124         List<String> lines = Arrays.asList(
    125                 "2013-03-04 12:33:18.789: Starting [UiAutomator Tests|YouTube][com.android.cts.uiautomator|com.google.android.youtube]",
    126                 "2013-03-04 12:33:18.792: Target invocation count: 1000",
    127                 "2013-03-04 12:33:18.793: Throttle: 1500 ms",
    128                 "2013-03-04 12:33:19.795: [  0](Seq: -1)-Launching UiAutomator Tests",
    129                 "2013-03-04 12:33:37.211: [  0](Seq:  0)-Found 6 candidates. Using index: 1",
    130                 "2013-03-04 12:33:37.336: [  0](Seq:  0)-Clicking: CheckBox (760,194)",
    131                 "2013-03-04 12:33:38.443: [  1](Seq:  0)-Found 6 candidates. Using index: 5",
    132                 "2013-03-04 12:33:38.533: [  1](Seq:  0)-Clicking: Button~Description for Button (723,874)",
    133                 "2013-03-04 12:33:38.443: [  2](Seq:  0)-Found 6 candidates. Using index: 5",
    134                 "2013-03-04 12:33:38.533: [  2](Seq:  0)-Clicking: Button~Description for Button (723,874)",
    135                 "2013-03-04 12:53:39.513: Monkey aborted.",
    136                 "2013-03-04 12:53:39.513: Invocations requested: 1000",
    137                 "2013-03-04 12:53:39.518: Invocations completed: 999",
    138                 "2013-03-04 12:53:39.520: Device uptime: 608193 sec, Monkey run duration: 20 sec");
    139 
    140         SmartMonkeyLogItem monkeyLog = new SmartMonkeyLogParser().parse(lines);
    141         assertNotNull(monkeyLog);
    142         assertEquals(999, monkeyLog.getFinalCount());
    143         assertEquals(20, monkeyLog.getTotalDuration());
    144         assertEquals(608193, monkeyLog.getStopUptimeDuration());
    145         assertEquals(true, monkeyLog.getIsAborted());
    146     }
    147 }
    148