• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.taskswitching.control.cts;
18 
19 import android.content.Intent;
20 import android.net.Uri;
21 import android.os.RemoteCallback;
22 
23 import com.android.compatibility.common.util.CtsAndroidTestCase;
24 import com.android.compatibility.common.util.DeviceReportLog;
25 import com.android.compatibility.common.util.MeasureRun;
26 import com.android.compatibility.common.util.MeasureTime;
27 import com.android.compatibility.common.util.ResultType;
28 import com.android.compatibility.common.util.ResultUnit;
29 import com.android.compatibility.common.util.Stat;
30 
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.Semaphore;
33 import java.util.concurrent.TimeUnit;
34 import java.util.concurrent.TimeoutException;
35 
36 /**
37  * Device test which actually launches two apps sequentially and
38  * measure time for switching.
39  * Completion of launch is notified via broadcast.
40  */
41 public class TaskSwitchingDeviceTest extends CtsAndroidTestCase {
42     private static final String REPORT_LOG_NAME = "CtsUiHostTestCases";
43     private static final long TASK_SWITCHING_WAIT_TIME = 5;
44 
45     private final Semaphore mSemaphore = new Semaphore(0);
46 
47     @Override
setUp()48     protected void setUp() throws Exception {
49         super.setUp();
50 
51         startActivitiesABSequentially();
52     }
53 
testMeasureTaskSwitching()54     public void testMeasureTaskSwitching() throws Exception {
55         final int NUMBER_REPEAT = 10;
56         final int SWITCHING_PER_ONE_TRY = 10;
57 
58         double[] results = MeasureTime.measure(NUMBER_REPEAT, new MeasureRun() {
59 
60             @Override
61             public void run(int i) throws Exception {
62                 for (int j = 0; j < SWITCHING_PER_ONE_TRY; j++) {
63                     startActivitiesABSequentially();
64                 }
65             }
66         });
67         String streamName = "test_measure_task_switching";
68         DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName);
69         report.addValues("task_switching_time", results, ResultType.LOWER_BETTER, ResultUnit.MS);
70         Stat.StatResult stat = Stat.getStat(results);
71         report.setSummary("task_switching_time_average", stat.mAverage, ResultType.LOWER_BETTER,
72                 ResultUnit.MS);
73         report.submit(getInstrumentation());
74     }
75 
startActivitiesABSequentially()76     private void startActivitiesABSequentially()
77             throws InterruptedException, TimeoutException, ExecutionException {
78         startActivityAndWait('a');
79         startActivityAndWait('b');
80     }
81 
startActivityAndWait(char activityLetter)82     private void startActivityAndWait(char activityLetter)
83             throws InterruptedException, TimeoutException, ExecutionException {
84         getContext().startActivity(new Intent(Intent.ACTION_VIEW)
85                 .setData(Uri.parse("https://foo.com/app" + activityLetter))
86                 .addCategory(Intent.CATEGORY_DEFAULT)
87                 .addCategory(Intent.CATEGORY_BROWSABLE)
88                 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
89                 .putExtra("callback", new RemoteCallback(b -> mSemaphore.release())));
90         mSemaphore.tryAcquire(TASK_SWITCHING_WAIT_TIME, TimeUnit.SECONDS);
91     }
92 }
93