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