Home | History | Annotate | Download | only in collectors
      1 /*
      2  * Copyright (C) 2017 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 android.device.collectors;
     17 
     18 import android.app.Instrumentation;
     19 import android.device.collectors.util.SendToInstrumentation;
     20 import android.os.Bundle;
     21 import android.support.test.runner.AndroidJUnit4;
     22 
     23 import org.junit.After;
     24 import org.junit.Before;
     25 import org.junit.Test;
     26 import org.junit.runner.Description;
     27 import org.junit.runner.Result;
     28 import org.junit.runner.RunWith;
     29 import org.mockito.ArgumentCaptor;
     30 import org.mockito.Mock;
     31 import org.mockito.Mockito;
     32 import org.mockito.MockitoAnnotations;
     33 
     34 import java.io.File;
     35 import java.util.List;
     36 
     37 import static org.junit.Assert.assertEquals;
     38 import static org.mockito.ArgumentMatchers.anyString;
     39 import static org.mockito.Mockito.doReturn;
     40 import static org.mockito.Mockito.spy;
     41 import static org.mockito.Mockito.times;
     42 import static org.mockito.Mockito.verify;
     43 
     44 /**
     45  * Android Unit tests for {@link BatteryStatsListener}.
     46  *
     47  * To run:
     48  * atest CollectorDeviceLibTest:android.device.collectors.BatteryStatsListenerTest
     49  */
     50 @RunWith(AndroidJUnit4.class)
     51 public class BatteryStatsListenerTest {
     52 
     53     private File mLogDir;
     54     private File mLogFile;
     55     private Description mRunDesc;
     56     private Description mTestDesc;
     57     private BatteryStatsListener mListener;
     58 
     59     @Mock
     60     private Instrumentation mInstrumentation;
     61 
     62 
     63     @Before
     64     public void setUp() {
     65         MockitoAnnotations.initMocks(this);
     66         mLogDir = new File("tmp/");
     67         mLogFile = new File("unique_log_file.log");
     68         mRunDesc = Description.createSuiteDescription("run");
     69         mTestDesc = Description.createTestDescription("run", "test");
     70     }
     71 
     72     @After
     73     public void tearDown() {
     74         if (mLogFile != null) {
     75             mLogFile.delete();
     76         }
     77         if (mLogDir != null) {
     78             mLogDir.delete();
     79         }
     80     }
     81 
     82     private BatteryStatsListener initListener(Bundle b) {
     83         BatteryStatsListener listener = spy(new BatteryStatsListener(b));
     84         listener.setInstrumentation(mInstrumentation);
     85         doReturn(new byte[0]).when(listener).executeCommandBlocking(anyString());
     86         doReturn(mLogDir).when(listener).createAndEmptyDirectory(anyString());
     87         doReturn(mLogFile).when(listener).dumpBatteryStats(anyString());
     88         doReturn(true).when(listener).resetBatteryStats();
     89         return listener;
     90     }
     91 
     92     @Test
     93     public void testTestRunCollector() throws Exception {
     94         Bundle b = new Bundle();
     95         b.putString(BatteryStatsListener.KEY_PER_RUN, "true");
     96         mListener = initListener(b);
     97 
     98         // Test run start behavior
     99         mListener.testRunStarted(mRunDesc);
    100         verify(mListener).createAndEmptyDirectory(BatteryStatsListener.DEFAULT_DIR);
    101         verify(mListener, times(1)).resetBatteryStats();
    102 
    103         // Test test start behavior
    104         mListener.testStarted(mTestDesc);
    105         verify(mListener, times(1)).resetBatteryStats();
    106 
    107         // Test test end behavior
    108         mListener.testFinished(mTestDesc);
    109         verify(mListener, times(1)).resetBatteryStats();
    110         verify(mListener, times(0)).dumpBatteryStats(anyString());
    111 
    112         // Test run end behavior
    113         mListener.testRunFinished(new Result());
    114         verify(mListener, times(1)).resetBatteryStats();
    115         verify(mListener, times(1)).dumpBatteryStats(anyString());
    116 
    117         Bundle resultBundle = new Bundle();
    118         mListener.instrumentationRunFinished(System.out, resultBundle, new Result());
    119 
    120         int protoFileCount = 0;
    121         for(String key : resultBundle.keySet()) {
    122             if (key.contains(mLogFile.getName())) protoFileCount++;
    123         }
    124         assertEquals(1, protoFileCount);
    125     }
    126 
    127     @Test
    128     public void testTestRunSaveToSpecifiedDirCollector() throws Exception {
    129         Bundle b = new Bundle();
    130         b.putString(BatteryStatsListener.KEY_PER_RUN, "true");
    131         b.putString(BatteryStatsListener.KEY_FORMAT, "file:unique/bs/dir");
    132         mListener = initListener(b);
    133 
    134         // Test run start behavior
    135         mListener.testRunStarted(mRunDesc);
    136         verify(mListener).createAndEmptyDirectory("unique/bs/dir");
    137     }
    138 
    139     @Test
    140     public void testTestRunToBytesCollector() throws Exception {
    141         Bundle b = new Bundle();
    142         b.putString(BatteryStatsListener.KEY_FORMAT, BatteryStatsListener.OPTION_BYTE);
    143         mListener = initListener(b);
    144         final int numTestCase = 5;
    145 
    146         // Test run start behavior
    147         mListener.testRunStarted(mRunDesc);
    148         verify(mListener, times(0)).createAndEmptyDirectory(BatteryStatsListener.DEFAULT_DIR);
    149         verify(mListener, times(0)).resetBatteryStats();
    150 
    151         for (int i = 1; i <= numTestCase; i++) {
    152             // Test test start behavior
    153             mListener.testStarted(mTestDesc);
    154             verify(mListener, times(i)).resetBatteryStats();
    155 
    156             // Test test end behavior
    157             mListener.testFinished(mTestDesc);
    158             verify(mListener, times(i)).resetBatteryStats();
    159             verify(mListener, times(i)).executeCommandBlocking(BatteryStatsListener.CMD_DUMPSYS);
    160         }
    161 
    162         // Test run end behavior
    163         mListener.testRunFinished(new Result());
    164         verify(mListener, times(numTestCase)).resetBatteryStats();
    165         verify(mListener, times(numTestCase)).executeCommandBlocking(BatteryStatsListener.CMD_DUMPSYS);
    166 
    167         Bundle resultBundle = new Bundle();
    168         mListener.instrumentationRunFinished(System.out, resultBundle, new Result());
    169 
    170         ArgumentCaptor<Bundle> capture = ArgumentCaptor.forClass(Bundle.class);
    171         Mockito.verify(mInstrumentation, times(numTestCase))
    172                 .sendStatus(Mockito.eq(
    173                         SendToInstrumentation.INST_STATUS_IN_PROGRESS), capture.capture());
    174         List<Bundle> capturedBundle = capture.getAllValues();
    175         assertEquals(numTestCase, capturedBundle.size());
    176 
    177         int protoCount = 0;
    178         for(Bundle bundle:capturedBundle){
    179             for(String key : bundle.keySet()) {
    180                 if (key.contains("bytes")) protoCount++;
    181             }
    182         }
    183         assertEquals(numTestCase, protoCount);
    184     }
    185 
    186     @Test
    187     public void testTestCaseCollector() throws Exception {
    188         Bundle b = new Bundle();
    189         mListener = initListener(b);
    190         final int numTestCase = 5;
    191 
    192         // Test run start behavior
    193         mListener.testRunStarted(mRunDesc);
    194         verify(mListener).createAndEmptyDirectory(BatteryStatsListener.DEFAULT_DIR);
    195         verify(mListener, times(0)).resetBatteryStats();
    196 
    197         for (int i = 1; i <= numTestCase; i++) {
    198             // Test test start behavior
    199             mListener.testStarted(mTestDesc);
    200             verify(mListener, times(i)).resetBatteryStats();
    201 
    202             // Test test end behavior
    203             mListener.testFinished(mTestDesc);
    204             verify(mListener, times(i)).resetBatteryStats();
    205             verify(mListener, times(i)).dumpBatteryStats(anyString());
    206         }
    207 
    208         // Test run end behavior
    209         mListener.testRunFinished(new Result());
    210         verify(mListener, times(numTestCase)).resetBatteryStats();
    211         verify(mListener, times(numTestCase)).dumpBatteryStats(anyString());
    212 
    213         Bundle resultBundle = new Bundle();
    214         mListener.instrumentationRunFinished(System.out, resultBundle, new Result());
    215 
    216         ArgumentCaptor<Bundle> capture = ArgumentCaptor.forClass(Bundle.class);
    217         Mockito.verify(mInstrumentation, times(numTestCase))
    218                 .sendStatus(Mockito.eq(
    219                         SendToInstrumentation.INST_STATUS_IN_PROGRESS), capture.capture());
    220         List<Bundle> capturedBundle = capture.getAllValues();
    221         assertEquals(numTestCase, capturedBundle.size());
    222 
    223         int protoFileCount = 0;
    224         for(Bundle bundle:capturedBundle){
    225             for(String key : bundle.keySet()) {
    226                 if (key.contains(mLogFile.getName())) protoFileCount++;
    227             }
    228         }
    229         assertEquals(numTestCase, protoFileCount);
    230     }
    231 }
    232