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