Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2012 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 android.ui.cts;
     18 
     19 import com.android.compatibility.common.util.AbiUtils;
     20 import com.android.compatibility.common.util.MetricsStore;
     21 import com.android.compatibility.common.util.ReportLog;
     22 import com.android.cts.migration.MigrationHelper;
     23 import com.android.ddmlib.testrunner.RemoteAndroidTestRunner;
     24 import com.android.ddmlib.testrunner.TestIdentifier;
     25 import com.android.ddmlib.testrunner.TestRunResult;
     26 import com.android.tradefed.build.IBuildInfo;
     27 import com.android.tradefed.device.ITestDevice;
     28 import com.android.tradefed.result.CollectingTestListener;
     29 import com.android.tradefed.testtype.DeviceTestCase;
     30 import com.android.tradefed.testtype.IAbi;
     31 import com.android.tradefed.testtype.IAbiReceiver;
     32 import com.android.tradefed.testtype.IBuildReceiver;
     33 
     34 import org.xmlpull.v1.XmlPullParserException;
     35 
     36 import java.io.File;
     37 import java.io.IOException;
     38 import java.util.Map;
     39 
     40 /**
     41  * Measure time to taskswitching between two Apps: A & B
     42  * Actual test is done in device, but this host side code installs all necessary APKs
     43  * and starts device test which is in CtsDeviceTaskswitchingControl.
     44  */
     45 public class TaskSwitchingTest extends DeviceTestCase implements IAbiReceiver, IBuildReceiver {
     46     private static final String TAG = "TaskSwitchingTest";
     47     private final static String RUNNER = "android.support.test.runner.AndroidJUnitRunner";
     48     private static final String RESULT_KEY = "COMPATIBILITY_TEST_RESULT";
     49     private IBuildInfo mBuild;
     50     private ITestDevice mDevice;
     51     private ReportLog mReport = null;
     52     private IAbi mAbi;
     53 
     54     static final String[] PACKAGES = {
     55         "android.taskswitching.control.cts",
     56         "android.taskswitching.appa",
     57         "android.taskswitching.appb"
     58     };
     59     static final String[] APKS = {
     60         "CtsDeviceTaskSwitchingControl.apk",
     61         "CtsDeviceTaskSwitchingAppA.apk",
     62         "CtsDeviceTaskSwitchingAppB.apk"
     63     };
     64 
     65     @Override
     66     public void setAbi(IAbi abi) {
     67         mAbi = abi;
     68     }
     69 
     70     @Override
     71     public void setBuild(IBuildInfo buildInfo) {
     72         mBuild = buildInfo;
     73     }
     74 
     75     @Override
     76     protected void setUp() throws Exception {
     77         super.setUp();
     78         mDevice = getDevice();
     79         String[] options = {AbiUtils.createAbiFlag(mAbi.getName())};
     80         for (int i = 0; i < PACKAGES.length; i++) {
     81             mDevice.uninstallPackage(PACKAGES[i]);
     82             File app = MigrationHelper.getTestFile(mBuild, APKS[i]);
     83             mDevice.installPackage(app, false, options);
     84         }
     85     }
     86 
     87 
     88     @Override
     89     protected void tearDown() throws Exception {
     90         for (int i = 0; i < PACKAGES.length; i++) {
     91             mDevice.uninstallPackage(PACKAGES[i]);
     92         }
     93         super.tearDown();
     94     }
     95 
     96     public void testTaskSwitching() throws Exception {
     97         RemoteAndroidTestRunner testRunner = new RemoteAndroidTestRunner(PACKAGES[0], RUNNER,
     98                 mDevice.getIDevice());
     99         LocalListener listener = new LocalListener();
    100         mDevice.runInstrumentationTests(testRunner, listener);
    101         TestRunResult result = listener.getCurrentRunResults();
    102         if (result.isRunFailure()) {
    103             fail(result.getRunFailureMessage());
    104         }
    105         assertNotNull("no performance data", mReport);
    106         MetricsStore.storeResult(mBuild, mAbi.getName(),
    107                 String.format("%s#%s", getClass().getName(), "testTaskSwitching"), mReport);
    108 
    109     }
    110 
    111     public class LocalListener extends CollectingTestListener {
    112         @Override
    113         public void testEnded(TestIdentifier test, Map<String, String> testMetrics) {
    114             // necessary as testMetrics passed from CollectingTestListerner is empty
    115             if (testMetrics.containsKey(RESULT_KEY)) {
    116                 try {
    117                     mReport = ReportLog.parse(testMetrics.get(RESULT_KEY));
    118                 } catch (XmlPullParserException | IOException e) {
    119                     e.printStackTrace();
    120                 }
    121             }
    122             super.testEnded(test, testMetrics);
    123         }
    124     }
    125 }
    126