Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2011 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.framework.tests;
     18 
     19 import com.android.ddmlib.testrunner.IRemoteAndroidTestRunner;
     20 import com.android.ddmlib.testrunner.RemoteAndroidTestRunner;
     21 import com.android.ddmlib.testrunner.TestResult;
     22 import com.android.tradefed.config.Option;
     23 import com.android.tradefed.device.DeviceNotAvailableException;
     24 import com.android.tradefed.device.ITestDevice;
     25 import com.android.tradefed.log.LogUtil.CLog;
     26 import com.android.tradefed.result.ByteArrayInputStreamSource;
     27 import com.android.tradefed.result.CollectingTestListener;
     28 import com.android.tradefed.result.ITestInvocationListener;
     29 import com.android.tradefed.result.InputStreamSource;
     30 import com.android.tradefed.result.LogDataType;
     31 import com.android.tradefed.testtype.IDeviceTest;
     32 import com.android.tradefed.testtype.IRemoteTest;
     33 import com.android.tradefed.util.RunUtil;
     34 
     35 import org.junit.Assert;
     36 
     37 import java.util.Collection;
     38 import java.util.HashMap;
     39 import java.util.Map;
     40 
     41 /**
     42  * Test that measures the data usage for an idle device and reports it to the result listener.
     43  */
     44 public class DataIdleTest implements IDeviceTest, IRemoteTest {
     45 
     46     ITestDevice mTestDevice = null;
     47     DataIdleTestHelper mTestHelper = null;
     48 
     49     @Option(name = "test-package-name", description = "Android test package name.")
     50     private String mTestPackageName;
     51 
     52     @Option(name = "test-class-name", description = "Optional test class name to test.")
     53     private String mTestClassName;
     54 
     55     @Option(name = "test-method-name", description = "Optional test method name to run.")
     56     private String mTestMethodName;
     57 
     58     @Option(name = "test-label",
     59             description = "Test label to identify the test run.")
     60     private String mTestLabel;
     61 
     62     @Option(name = "mobile-data-only",
     63             description = "If this test is to use mobile data or not.")
     64     private boolean mMobileDataOnly;
     65 
     66     @Option(name = "idle-time-msecs", description = "Time in msecs to wait for data to propagate.")
     67     private int mIdleTime = 60 * 60 * 1000;
     68 
     69     private static final String BUG_REPORT_LABEL = "bugreport";
     70     private static final String DUMPSYS_REPORT_LABEL = "dumpsys";
     71 
     72     @Override
     73     public void run(ITestInvocationListener listener) throws DeviceNotAvailableException {
     74         Assert.assertNotNull(mTestDevice);
     75         mTestHelper = new DataIdleTestHelper(mTestDevice);
     76         // if mobile data only, then wifi should be disabled, or vice versa
     77         Assert.assertEquals("incorrect wifi status for current test parameters",
     78                 mMobileDataOnly, !mTestDevice.isWifiEnabled());
     79         // Test the Internet connection.
     80         Assert.assertTrue("Failed to connect to get data.", mTestHelper.pingTest());
     81 
     82         CLog.v("Sleeping for %d", mIdleTime);
     83         RunUtil.getDefault().sleep(mIdleTime);
     84 
     85 
     86         // Run test to dump all the data stats gathered by the system.
     87         IRemoteAndroidTestRunner runner = new RemoteAndroidTestRunner(mTestPackageName,
     88                 mTestDevice.getIDevice());
     89 
     90         if (mTestClassName != null) {
     91             runner.setClassName(mTestClassName);
     92         }
     93         if (mTestClassName != null && mTestMethodName != null) {
     94             runner.setMethodName(mTestClassName, mTestMethodName);
     95         }
     96 
     97         CollectingTestListener collectingListener = new CollectingTestListener();
     98         Assert.assertTrue(mTestDevice.runInstrumentationTests(runner, collectingListener));
     99 
    100         // Collect bandwidth metrics from the instrumentation test out.
    101         Map<String, String> idleTestMetrics = new HashMap<String, String>();
    102         Collection<TestResult> testResults =
    103                 collectingListener.getCurrentRunResults().getTestResults().values();
    104         if (testResults != null && testResults.iterator().hasNext()) {
    105             Map<String, String> testMetrics = testResults.iterator().next().getMetrics();
    106             if (testMetrics != null) {
    107                 idleTestMetrics.putAll(testMetrics);
    108                 idleTestMetrics.put("Idle time", Integer.toString(mIdleTime));
    109                 reportMetrics(listener, mTestLabel, idleTestMetrics);
    110             }
    111         }
    112         // Capture the bugreport.
    113         logBugReport(listener);
    114         logNetStats(listener);
    115     }
    116 
    117     /**
    118      * Capture the bugreport and log it.
    119      * @param listener {@link ITestInvocationListener}
    120      */
    121     void logBugReport(ITestInvocationListener listener) {
    122         InputStreamSource bugreport = mTestDevice.getBugreport();
    123         listener.testLog(BUG_REPORT_LABEL, LogDataType.BUGREPORT, bugreport);
    124         bugreport.cancel();
    125     }
    126 
    127     /**
    128      * Fetch the whole netstats details.
    129      * @param listener {@link ITestInvocationListener}
    130      * @throws DeviceNotAvailableException
    131      */
    132     void logNetStats(ITestInvocationListener listener) throws DeviceNotAvailableException {
    133         String output = mTestDevice.executeShellCommand("dumpsys netstats detail full");
    134         InputStreamSource is = new ByteArrayInputStreamSource(output.getBytes());
    135         listener.testLog(DUMPSYS_REPORT_LABEL, LogDataType.TEXT, is);
    136     }
    137 
    138     /**
    139      * Report run metrics by creating an empty test run to stick them in.
    140      *
    141      * @param listener the {@link ITestInvocationListener} of test results
    142      * @param runName the test name
    143      * @param metrics the {@link Map} that contains metrics for the given test
    144      */
    145     void reportMetrics(ITestInvocationListener listener, String runName,
    146             Map<String, String> metrics) {
    147         // Create an empty testRun to report the parsed runMetrics
    148         CLog.d("About to report metrics: %s", metrics);
    149         listener.testRunStarted(runName, 0);
    150         listener.testRunEnded(0, metrics);
    151     }
    152 
    153     @Override
    154     public void setDevice(ITestDevice device) {
    155         mTestDevice = device;
    156     }
    157 
    158     @Override
    159     public ITestDevice getDevice() {
    160         return mTestDevice;
    161     }
    162 
    163 }
    164