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 com.android.cts.taskswitching.control; 18 19 import java.util.concurrent.Semaphore; 20 import java.util.concurrent.TimeUnit; 21 22 import android.content.BroadcastReceiver; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.IntentFilter; 27 28 import com.android.cts.util.MeasureRun; 29 import com.android.cts.util.MeasureTime; 30 import com.android.cts.util.ResultType; 31 import com.android.cts.util.ResultUnit; 32 import android.cts.util.CtsAndroidTestCase; 33 import com.android.cts.util.Stat; 34 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 PKG_A = "com.android.cts.taskswitching.appa"; 43 private static final String PKG_B = "com.android.cts.taskswitching.appb"; 44 private static final String ACTIVITY_A = "AppAActivity"; 45 private static final String ACTIVITY_B = "AppBActivity"; 46 private static final long TASK_SWITCHING_WAIT_TIME = 5; 47 private final AppBroadcastReceiver mReceiverA = new AppBroadcastReceiver(); 48 private final AppBroadcastReceiver mReceiverB = new AppBroadcastReceiver(); 49 50 @Override setUp()51 protected void setUp() throws Exception { 52 super.setUp(); 53 startActivity(PKG_A, ACTIVITY_A); 54 startActivity(PKG_B, ACTIVITY_B); 55 IntentFilter filterA = new IntentFilter(); 56 filterA.addAction(PKG_A); 57 IntentFilter filterB = new IntentFilter(); 58 filterB.addAction(PKG_B); 59 getContext().registerReceiver(mReceiverA, filterA); 60 getContext().registerReceiver(mReceiverB, filterB); 61 } 62 63 @Override tearDown()64 protected void tearDown() throws Exception { 65 getContext().unregisterReceiver(mReceiverA); 66 getContext().unregisterReceiver(mReceiverB); 67 super.tearDown(); 68 } 69 testMeasureTaskSwitching()70 public void testMeasureTaskSwitching() throws Exception { 71 final int NUMBER_REPEAT = 10; 72 final int SWITCHING_PER_ONE_TRY = 10; 73 74 double[] results = MeasureTime.measure(NUMBER_REPEAT, new MeasureRun() { 75 76 @Override 77 public void run(int i) throws Exception { 78 for (int j = 0; j < SWITCHING_PER_ONE_TRY; j++) { 79 startActivity(PKG_A, ACTIVITY_A); 80 assertTrue(mReceiverA.waitForBroadcast(TASK_SWITCHING_WAIT_TIME)); 81 startActivity(PKG_B, ACTIVITY_B); 82 assertTrue(mReceiverB.waitForBroadcast(TASK_SWITCHING_WAIT_TIME)); 83 } 84 } 85 }); 86 getReportLog().printArray("taskswitching time", results, ResultType.LOWER_BETTER, 87 ResultUnit.MS); 88 Stat.StatResult stat = Stat.getStat(results); 89 getReportLog().printSummary("taskswitching time", stat.mAverage, 90 ResultType.LOWER_BETTER, ResultUnit.MS); 91 } 92 startActivity(String packageName, String activityName)93 private void startActivity(String packageName, String activityName) { 94 Context context = getContext(); 95 Intent intent = new Intent(); 96 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 97 intent.addCategory(Intent.CATEGORY_LAUNCHER); 98 intent.setComponent(new ComponentName(packageName, packageName + "." + activityName)); 99 context.startActivity(intent); 100 } 101 102 class AppBroadcastReceiver extends BroadcastReceiver { 103 private final Semaphore mSemaphore = new Semaphore(0); 104 waitForBroadcast(long timeoutInSec)105 public boolean waitForBroadcast(long timeoutInSec) throws InterruptedException { 106 return mSemaphore.tryAcquire(timeoutInSec, TimeUnit.SECONDS); 107 } 108 @Override onReceive(Context context, Intent intent)109 public void onReceive(Context context, Intent intent) { 110 mSemaphore.release(); 111 } 112 } 113 } 114