1 /* 2 * Copyright (C) 2008 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.app.cts; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.test.AndroidTestCase; 23 import android.test.PerformanceTestCase; 24 25 public class ActivityTestsBase extends AndroidTestCase implements PerformanceTestCase, 26 LaunchpadActivity.CallingTest { 27 public static final String PERMISSION_GRANTED = "android.app.cts.permission.TEST_GRANTED"; 28 public static final String PERMISSION_DENIED = "android.app.cts.permission.TEST_DENIED"; 29 30 protected Intent mIntent; 31 32 private PerformanceTestCase.Intermediates mIntermediates; 33 private String mExpecting; 34 35 // Synchronization of activity result. 36 private boolean mFinished; 37 private int mResultCode = 0; 38 private Intent mData; 39 private RuntimeException mResultStack = null; 40 41 @Override setUp()42 protected void setUp() throws Exception { 43 super.setUp(); 44 mIntent = new Intent(mContext, LaunchpadActivity.class); 45 mIntermediates = null; 46 } 47 48 @Override tearDown()49 protected void tearDown() throws Exception { 50 mIntermediates = null; 51 super.tearDown(); 52 } 53 isPerformanceOnly()54 public boolean isPerformanceOnly() { 55 return false; 56 } 57 setInternalIterations(int count)58 public void setInternalIterations(int count) { 59 } 60 startTiming(boolean realTime)61 public void startTiming(boolean realTime) { 62 if (mIntermediates != null) { 63 mIntermediates.startTiming(realTime); 64 } 65 } 66 addIntermediate(String name)67 public void addIntermediate(String name) { 68 if (mIntermediates != null) { 69 mIntermediates.addIntermediate(name); 70 } 71 } 72 addIntermediate(String name, long timeInNS)73 public void addIntermediate(String name, long timeInNS) { 74 if (mIntermediates != null) { 75 mIntermediates.addIntermediate(name, timeInNS); 76 } 77 } 78 finishTiming(boolean realTime)79 public void finishTiming(boolean realTime) { 80 if (mIntermediates != null) { 81 mIntermediates.finishTiming(realTime); 82 } 83 } 84 activityFinished(int resultCode, Intent data, RuntimeException where)85 public void activityFinished(int resultCode, Intent data, RuntimeException where) { 86 finishWithResult(resultCode, data, where); 87 } 88 editIntent()89 public Intent editIntent() { 90 return mIntent; 91 } 92 93 @Override getContext()94 public Context getContext() { 95 return mContext; 96 } 97 startPerformance(Intermediates intermediates)98 public int startPerformance(Intermediates intermediates) { 99 mIntermediates = intermediates; 100 return 1; 101 } 102 finishGood()103 public void finishGood() { 104 finishWithResult(Activity.RESULT_OK, null); 105 } 106 finishBad(String error)107 public void finishBad(String error) { 108 finishWithResult(Activity.RESULT_CANCELED, new Intent().setAction(error)); 109 } 110 finishWithResult(int resultCode, Intent data)111 public void finishWithResult(int resultCode, Intent data) { 112 final RuntimeException where = new RuntimeException("Original error was here"); 113 where.fillInStackTrace(); 114 finishWithResult(resultCode, data, where); 115 } 116 finishWithResult(int resultCode, Intent data, RuntimeException where)117 public void finishWithResult(int resultCode, Intent data, RuntimeException where) { 118 synchronized (this) { 119 mResultCode = resultCode; 120 mData = data; 121 mResultStack = where; 122 mFinished = true; 123 notifyAll(); 124 } 125 } 126 runLaunchpad(String action)127 public int runLaunchpad(String action) { 128 LaunchpadActivity.setCallingTest(this); 129 130 synchronized (this) { 131 mIntent.setAction(action); 132 mFinished = false; 133 mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 134 mContext.startActivity(mIntent); 135 } 136 137 return waitForResultOrThrow(60 * 1000); 138 } 139 waitForResultOrThrow(int timeoutMs)140 public int waitForResultOrThrow(int timeoutMs) { 141 return waitForResultOrThrow(timeoutMs, null); 142 } 143 waitForResultOrThrow(int timeoutMs, String expected)144 public int waitForResultOrThrow(int timeoutMs, String expected) { 145 final int res = waitForResult(timeoutMs, expected); 146 147 if (res == Activity.RESULT_CANCELED) { 148 if (mResultStack != null) { 149 throw new RuntimeException(mData != null ? mData.toString() : "Unable to launch", 150 mResultStack); 151 } else { 152 throw new RuntimeException(mData != null ? mData.toString() : "Unable to launch"); 153 } 154 } 155 return res; 156 } 157 waitForResult(int timeoutMs, String expected)158 public int waitForResult(int timeoutMs, String expected) { 159 mExpecting = expected; 160 161 final long endTime = System.currentTimeMillis() + timeoutMs; 162 163 boolean timeout = false; 164 synchronized (this) { 165 while (!mFinished) { 166 final long delay = endTime - System.currentTimeMillis(); 167 if (delay < 0) { 168 timeout = true; 169 break; 170 } 171 172 try { 173 wait(delay); 174 } catch (final java.lang.InterruptedException e) { 175 // do nothing 176 } 177 } 178 } 179 180 mFinished = false; 181 182 if (timeout) { 183 mResultCode = Activity.RESULT_CANCELED; 184 onTimeout(); 185 } 186 return mResultCode; 187 } 188 getResultCode()189 public int getResultCode() { 190 return mResultCode; 191 } 192 getResultData()193 public Intent getResultData() { 194 return mData; 195 } 196 getResultStack()197 public RuntimeException getResultStack() { 198 return mResultStack; 199 } 200 onTimeout()201 public void onTimeout() { 202 final String msg = mExpecting == null ? "Timeout" : "Timeout while expecting " + mExpecting; 203 finishWithResult(Activity.RESULT_CANCELED, new Intent().setAction(msg)); 204 } 205 } 206