Home | History | Annotate | Download | only in parser
      1 /*
      2  * Copyright (C) 2016 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 com.android.loganalysis.parser;
     18 
     19 import com.android.loganalysis.item.LatencyItem;
     20 import com.android.loganalysis.item.TransitionDelayItem;
     21 
     22 import junit.framework.TestCase;
     23 
     24 import java.io.BufferedReader;
     25 import java.io.BufferedWriter;
     26 import java.io.File;
     27 import java.io.FileInputStream;
     28 import java.io.FileWriter;
     29 import java.io.IOException;
     30 import java.io.InputStreamReader;
     31 import java.util.Arrays;
     32 import java.util.List;
     33 
     34 /**
     35  * Unit tests for {@link EventsLogParser}.
     36  */
     37 public class EventsLogParserTest extends TestCase {
     38 
     39     private File mTempFile = null;
     40 
     41     @Override
     42     protected void tearDown() throws Exception {
     43         mTempFile.delete();
     44     }
     45 
     46     /**
     47      * Test for empty events logs passed to the transition delay parser
     48      */
     49     public void testEmptyEventsLog() throws IOException {
     50         List<String> lines = Arrays.asList("");
     51         List<TransitionDelayItem> transitionItems = (new EventsLogParser()).
     52                 parseTransitionDelayInfo(readInputBuffer(getTempFile(lines)));
     53         assertEquals("Transition Delay items list should be empty", 0,transitionItems.size());
     54     }
     55 
     56     /**
     57      * Test for no transition delay info in the events log
     58      */
     59     public void testNoTransitionDelayInfo() throws IOException {
     60         List<String> lines = Arrays
     61                 .asList(
     62                         "08-25 12:56:15.850  1152  8968 I am_focused_stack: [0,0,1,appDied setFocusedActivity]",
     63                         "08-25 12:56:15.850  1152  8968 I wm_task_moved: [6,1,1]",
     64                         "08-25 12:56:15.852  1152  8968 I am_focused_activity: [0,com.google.android.apps.nexuslauncher/.NexusLauncherActivity,appDied]",
     65                         "08-25 12:56:15.852  1152  8968 I wm_task_removed: [27,removeTask]",
     66                         "08-25 12:56:15.852  1152  8968 I wm_stack_removed: 1");
     67         List<TransitionDelayItem> transitionItems = (new EventsLogParser()).
     68                 parseTransitionDelayInfo(readInputBuffer(getTempFile(lines)));
     69         assertEquals("Transition Delay items list should be empty", 0,
     70                 transitionItems.size());
     71     }
     72 
     73     /**
     74      * Test for Cold launch transition delay and starting window delay info
     75      */
     76     public void testValidColdTransitionDelay() throws IOException {
     77         List<String> lines = Arrays
     78                 .asList("09-18 23:56:19.376  1140  1221 I sysui_multi_action: [319,51,321,50,322,190,325,670,757,761,758,7,759,1,806,com.google.android.calculator,871,com.android.calculator2.Calculator,904,com.google.android.apps.nexuslauncher,905,0,945,41]",
     79                         "09-18 23:56:19.376  1140  1221 I sysui_multi_action: [319,51,321,50,322,190,325,670,757,761,758,7,759,1,806,com.google.android.calculator,871,com.android.calculator2.Calculator,905,0,945,41]");
     80         List<TransitionDelayItem> transitionItems = (new EventsLogParser()).
     81                 parseTransitionDelayInfo(readInputBuffer(getTempFile(lines)));
     82         assertEquals("Startinng Window Delay items list should have two item", 2,
     83                 transitionItems.size());
     84         assertEquals("Component name not parsed correctly",
     85                 "com.google.android.calculator/com.android.calculator2.Calculator",
     86                 transitionItems.get(0).getComponentName());
     87         assertEquals("Transition delay is not parsed correctly", Long.valueOf(51),
     88                 transitionItems.get(0).getTransitionDelay());
     89         assertEquals("Starting window delay is not parsed correctly", Long.valueOf(50),
     90                 transitionItems.get(0).getStartingWindowDelay());
     91         assertEquals("Date and time is not parsed correctly", "09-18 23:56:19.376",
     92                 transitionItems.get(0).getDateTime());
     93     }
     94 
     95     /**
     96      * Test for Hot launch transition delay and starting window delay info
     97      */
     98     public void testValidHotTransitionDelay() throws IOException {
     99         List<String> lines = Arrays
    100                 .asList("09-18 23:56:19.376  1140  1221 I sysui_multi_action: [319,51,321,50,322,190,325,670,757,761,758,7,759,1,806,com.google.android.calculator,871,com.android.calculator2.Calculator,904,com.google.android.apps.nexuslauncher,905,0]",
    101                         "09-18 23:56:19.376  1140  1221 I sysui_multi_action: [319,51,321,50,322,190,325,670,757,761,758,7,759,1,806,com.google.android.calculator,871,com.android.calculator2.Calculator,905,0]",
    102                         "09-19 02:26:30.182  1143  1196 I sysui_multi_action: [319,87,322,75,325,212,757,761,758,9,759,2,806,com.google.android.apps.nexuslauncher,871,com.google.android.apps.nexuslauncher.NexusLauncherActivity,904,com.google.android.apps.nexuslauncher,905,0]",
    103                         "09-19 02:26:30.182  1143  1196 I sysui_multi_action: [319,87,322,75,325,212,757,761,758,9,759,2,806,com.google.android.apps.nexuslauncher,871,com.google.android.apps.nexuslauncher.NexusLauncherActivity,905,0]");
    104         List<TransitionDelayItem> transitionItems = (new EventsLogParser()).
    105                 parseTransitionDelayInfo(readInputBuffer(getTempFile(lines)));
    106         assertEquals("Transition Delay items list should have four item", 4,
    107                 transitionItems.size());
    108         assertEquals("Component name not parsed correctly",
    109                 "com.google.android.calculator/com.android.calculator2.Calculator",
    110                 transitionItems.get(0).getComponentName());
    111         assertEquals("Transition delay is not parsed correctly", Long.valueOf(51),
    112                 transitionItems.get(0).getTransitionDelay());
    113         assertEquals("Date is not parsed correctly", "09-18 23:56:19.376",
    114                 transitionItems.get(0).getDateTime());
    115     }
    116 
    117     /**
    118      * Test for same app transition delay items order after parsing from the events log
    119      */
    120     public void testTransitionDelayOrder() throws IOException {
    121         List<String> lines = Arrays
    122                 .asList("09-18 23:56:19.376  1140  1221 I sysui_multi_action: [319,51,321,59,322,190,325,670,757,761,758,7,759,1,806,com.google.android.calculator,871,com.android.calculator2.Calculator,904,com.google.android.apps.nexuslauncher,905,0,945,41]",
    123                         "09-18 23:59:18.380  1140  1221 I sysui_multi_action: [319,55,321,65,322,190,325,670,757,761,758,7,759,1,806,com.google.android.calculator,871,com.android.calculator2.Calculator,905,0,945,41]");
    124         List<TransitionDelayItem> transitionItems = (new EventsLogParser()).
    125                 parseTransitionDelayInfo(readInputBuffer(getTempFile(lines)));
    126         assertEquals("Transition Delay items list should have two items", 2,
    127                 transitionItems.size());
    128         assertEquals("Transition delay for the first item is not set correct", Long.valueOf(59),
    129                 transitionItems.get(0).getStartingWindowDelay());
    130         assertEquals("Transition delay for the second item is not set correct", Long.valueOf(65),
    131                 transitionItems.get(1).getStartingWindowDelay());
    132     }
    133 
    134     /**
    135      * Test for two different different apps transition delay items
    136      */
    137     public void testDifferentAppTransitionDelay() throws IOException {
    138         List<String> lines = Arrays
    139                 .asList("09-18 23:56:19.376  1140  1221 I sysui_multi_action: [319,51,321,50,322,190,325,670,757,761,758,7,759,1,806,com.google.android.calculator,871,com.android.calculator2.Calculator,904,com.google.android.apps.nexuslauncher,905,0]",
    140                         "09-19 02:26:30.182  1143  1196 I sysui_multi_action: [319,87,322,75,325,212,757,761,758,9,759,2,806,com.google.android.apps.nexuslauncher,871,com.google.android.apps.nexuslauncher.NexusLauncherActivity,904,com.google.android.apps.nexuslauncher,905,0]");
    141         List<TransitionDelayItem> transitionItems = (new EventsLogParser()).
    142                 parseTransitionDelayInfo(readInputBuffer(getTempFile(lines)));
    143         assertEquals("Transition Delay items list should have two items", 2,
    144                 transitionItems.size());
    145         assertEquals("Calculator is not the first transition delay item",
    146                 "com.google.android.calculator/com.android.calculator2.Calculator",
    147                 transitionItems.get(0).getComponentName());
    148         assertEquals("Maps is not the second transition delay item",
    149                 "com.google.android.apps.nexuslauncher/"
    150                 + "com.google.android.apps.nexuslauncher.NexusLauncherActivity",
    151                 transitionItems.get(1).getComponentName());
    152     }
    153 
    154     /**
    155      * Test for invalid transition delay items pattern having different code.
    156      */
    157     public void testInvalidTransitionPattern() throws IOException {
    158         List<String> lines = Arrays
    159                 .asList("01-02 08:11:58.691   934   986 I sysui_multi_action: a[319,48,322,82,325,84088,757,761,758,9,759,4,807,com.google.android.calculator,871,com.android.calculator2.Calculator,905,0]",
    160                         "01-02 08:12:03.639   934   970 I sysui_multi_action: [757,803,799,window_time_0,802,5]",
    161                         "01-02 08:12:10.849   934   986 I sysui_multi_action: 319,42,321,59,322,208,325,84100,757,761,758,9,759,4,806,com.google.android.apps.maps,871,com.google.android.maps.MapsActivity,905,0]",
    162                         "01-02 08:12:16.895  1446  1446 I sysui_multi_action: [757,803,799,overview_trigger_nav_btn,802,1]",
    163                         "01-02 08:12:16.895  1446  1446 I sysui_multi_action: [757,803,799,overview_source_app,802,1]",
    164                         "01-02 08:12:16.895  1446  1446 I sysui_multi_action: [757,804,799,overview_source_app_index,801,8,802,1]");
    165         List<TransitionDelayItem> transitionItems = (new EventsLogParser()).
    166                 parseTransitionDelayInfo(readInputBuffer(getTempFile(lines)));
    167         assertEquals("Transition Delay items list should be empty", 0,
    168                 transitionItems.size());
    169     }
    170 
    171     /**
    172      * Test for valid latency item
    173      */
    174     public void testValidLatencyInfo() throws IOException {
    175         List<String> lines = Arrays
    176                 .asList("08-25 13:01:19.412  1152  9031 I am_restart_activity: [com.google.android.gm/.ConversationListActivityGmail,0,85290699,38]",
    177                         "08-25 13:01:19.437  1152  1226 I sysui_action: [321,85]",
    178                         "08-25 13:01:19.437  1152  1226 I sysui_action: [320,1]",
    179                         "08-25 13:01:19.437  1152  1226 I sysui_action: [319,85]",
    180                         "08-25 12:56:15.850  1152  8968 I am_focused_stack: [0,0,1,appDied setFocusedActivity]",
    181                         "09-19 11:53:16.893  1080  1160 I sysui_latency: [1,50]");
    182         List<LatencyItem> latencyItems = (new EventsLogParser()).
    183                 parseLatencyInfo(readInputBuffer(getTempFile(lines)));
    184         assertEquals("One latency item should present in the list", 1, latencyItems.size());
    185         assertEquals("Action Id is not correct", 1, latencyItems.get(0).getActionId());
    186         assertEquals("Delay is not correct", 50L, latencyItems.get(0).getDelay());
    187     }
    188 
    189     /**
    190      * Test for empty delay info
    191      */
    192     public void testInvalidLatencyInfo() throws IOException {
    193         List<String> lines = Arrays
    194                 .asList("08-25 13:01:19.412  1152  9031 I am_restart_activity: [com.google.android.gm/.ConversationListActivityGmail,0,85290699,38]",
    195                         "08-25 13:01:19.437  1152  1226 I sysui_action: [321,85]",
    196                         "08-25 13:01:19.437  1152  1226 I sysui_action: [320,1]",
    197                         "08-25 13:01:19.437  1152  1226 I sysui_action: [319,85]",
    198                         "08-25 12:56:15.850  1152  8968 I am_focused_stack: [0,0,1,appDied setFocusedActivity]",
    199                         "09-19 11:53:16.893  1080  1160 I sysui_latency: [1]");
    200         List<LatencyItem> latencyItems = (new EventsLogParser()).
    201                 parseLatencyInfo(readInputBuffer(getTempFile(lines)));
    202         assertEquals("Latency items list should be empty", 0, latencyItems.size());
    203     }
    204 
    205     /**
    206      * Test for empty latency info
    207      */
    208     public void testEmptyLatencyInfo() throws IOException {
    209         List<String> lines = Arrays
    210                 .asList("08-25 13:01:19.412  1152  9031 I am_restart_activity: [com.google.android.gm/.ConversationListActivityGmail,0,85290699,38]",
    211                         "08-25 13:01:19.437  1152  1226 I sysui_action: [321,85]",
    212                         "08-25 13:01:19.437  1152  1226 I sysui_action: [320,1]",
    213                         "08-25 13:01:19.437  1152  1226 I sysui_action: [319,85]",
    214                         "08-25 12:56:15.850  1152  8968 I am_focused_stack: [0,0,1,appDied setFocusedActivity]",
    215                         "09-19 11:53:16.893  1080  1160 I sysui_latency: []");
    216         List<LatencyItem> latencyItems = (new EventsLogParser()).
    217                 parseLatencyInfo(readInputBuffer(getTempFile(lines)));
    218         assertEquals("Latency items list should be empty", 0, latencyItems.size());
    219     }
    220 
    221 
    222     /**
    223      * Test for order of the latency items
    224      */
    225     public void testLatencyInfoOrder() throws IOException {
    226         List<String> lines = Arrays
    227                 .asList("09-19 11:53:16.893  1080  1160 I sysui_latency: [1,50]",
    228                         "08-25 13:01:19.437  1152  1226 I sysui_action: [321,85]",
    229                         "08-25 13:01:19.437  1152  1226 I sysui_action: [320,1]",
    230                         "08-25 13:01:19.437  1152  1226 I sysui_action: [319,85]",
    231                         "08-25 12:56:15.850  1152  8968 I am_focused_stack: [0,0,1,appDied setFocusedActivity]",
    232                         "09-19 11:53:16.893  1080  1160 I sysui_latency: [2,100]");
    233         List<LatencyItem> latencyItems = (new EventsLogParser()).
    234                 parseLatencyInfo(readInputBuffer(getTempFile(lines)));
    235         assertEquals("Latency list should have 2 items", 2, latencyItems.size());
    236         assertEquals("First latency id is not 1", 1, latencyItems.get(0).getActionId());
    237         assertEquals("Second latency id is not 2", 2, latencyItems.get(1).getActionId());
    238     }
    239 
    240     /**
    241      * Write list of strings to file and use it for testing.
    242      */
    243     public File getTempFile(List<String> sampleEventsLogs) throws IOException {
    244         mTempFile = File.createTempFile("events_logcat", ".txt");
    245         BufferedWriter out = new BufferedWriter(new FileWriter(mTempFile));
    246         for (String line : sampleEventsLogs) {
    247             out.write(line);
    248             out.newLine();
    249         }
    250         out.close();
    251         return mTempFile;
    252     }
    253 
    254     /**
    255      * Reader to read the input from the given temp file
    256      */
    257     public BufferedReader readInputBuffer(File tempFile) throws IOException {
    258         return (new BufferedReader(new InputStreamReader(new FileInputStream(tempFile))));
    259     }
    260 
    261 }