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