1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package android.app.cts; 18 19 20 import android.app.Activity; 21 import android.app.ActivityGroup; 22 import android.app.LocalActivityManager; 23 import android.content.Intent; 24 import android.os.Bundle; 25 import android.view.Window; 26 import android.app.cts.CTSResult; 27 28 public class LocalActivityManagerTestHelper extends ActivityGroup { 29 30 public static final String ACTION_DISPATCH_RESUME = "dispatchResume"; 31 public static final String ACTION_START_ACTIVITY = "startActivity"; 32 public static final String ACTION_DISPATCH_CREATE = "dispatchCreate"; 33 public static final String ACTION_DISPATCH_STOP = "dispatchStop"; 34 public static final String ACTION_DISPATCH_PAUSE_TRUE = "dispatchPauseTrue"; 35 public static final String ACTION_DISPATCH_PAUSE_FALSE = "dispatchPauseFalse"; 36 public static final String ACTION_SAVE_INSTANCE_STATE = "saveInstanceState"; 37 public static final String ACTION_DISPATCH_DESTROY = "dispatchDestroy"; 38 public static final String ACTION_REMOVE_ALL_ACTIVITY = "removeAllActivities"; 39 40 private String mCurrentAction; 41 private LocalActivityManager mLocalActivityManager; 42 43 private static CTSResult sResult; 44 setResult(CTSResult cr)45 public static void setResult(CTSResult cr) { 46 sResult = cr; 47 } 48 LocalActivityManagerTestHelper()49 public LocalActivityManagerTestHelper() { 50 super(); 51 } 52 53 @Override onCreate(Bundle savedInstanceState)54 protected void onCreate(Bundle savedInstanceState) { 55 super.onCreate(savedInstanceState); 56 mCurrentAction = getIntent().getAction(); 57 mLocalActivityManager = getLocalActivityManager(); 58 } 59 60 @Override onStart()61 protected void onStart() { 62 super.onStart(); 63 } 64 onResume()65 protected void onResume() { 66 super.onResume(); 67 if (mCurrentAction.equals(ACTION_DISPATCH_RESUME)) { 68 testDispatchResume(); 69 } else if (mCurrentAction.equals(ACTION_START_ACTIVITY)) { 70 testStartActivity(); 71 } else if (mCurrentAction.equals(ACTION_DISPATCH_CREATE)) { 72 testDispatchCreate(); 73 } else if (mCurrentAction.equals(ACTION_DISPATCH_STOP)) { 74 testDispatchStop(); 75 } else if (mCurrentAction.equals(ACTION_DISPATCH_PAUSE_TRUE)) { 76 testDispatchPauseTrue(); 77 } else if (mCurrentAction.equals(ACTION_DISPATCH_PAUSE_FALSE)) { 78 testDispatchPauseFalse(); 79 } else if (mCurrentAction.equals(ACTION_SAVE_INSTANCE_STATE)) { 80 testSaveInstanceState(); 81 } else if (mCurrentAction.equals(ACTION_DISPATCH_DESTROY)) { 82 testDispatchDestroy(); 83 } else if (mCurrentAction.equals(ACTION_REMOVE_ALL_ACTIVITY)) { 84 testRemoveAllActivity(); 85 } 86 } 87 testRemoveAllActivity()88 private void testRemoveAllActivity() { 89 final String id = "id_remove_activity"; 90 final Intent intent = new Intent(this, LocalActivityManagerStubActivity.class); 91 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 92 mLocalActivityManager.startActivity(id, intent); 93 94 Activity activity = mLocalActivityManager.getActivity(id); 95 if (activity == null) { 96 fail(); 97 return; 98 } 99 100 if (!activity.getClass().getName().equals("android.app.cts." 101 + "LocalActivityManagerStubActivity")) { 102 fail(); 103 return; 104 } 105 106 mLocalActivityManager.removeAllActivities(); 107 activity = mLocalActivityManager.getActivity(id); 108 if (activity != null) { 109 fail(); 110 return; 111 } 112 pass(); 113 } 114 testDispatchDestroy()115 private void testDispatchDestroy() { 116 final String id1 = "id_dispatch_destroy1"; 117 final String id2 = "id_dispatch_destroy2"; 118 final Intent intent = new Intent(this, LocalActivityManagerStubActivity.class); 119 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 120 mLocalActivityManager.startActivity(id1, intent); 121 122 LocalActivityManagerStubActivity.sIsOnDestroyCalled = false; 123 mLocalActivityManager.dispatchDestroy(false); 124 if (mLocalActivityManager.getCurrentActivity().isFinishing()){ 125 fail(); 126 return; 127 } 128 129 if (!LocalActivityManagerStubActivity.sIsOnDestroyCalled) { 130 fail(); 131 return; 132 } 133 134 mLocalActivityManager.startActivity(id2, intent); 135 LocalActivityManagerStubActivity.sIsOnDestroyCalled = false; 136 mLocalActivityManager.dispatchDestroy(true); 137 138 if (!LocalActivityManagerStubActivity.sIsOnDestroyCalled) { 139 fail(); 140 return; 141 } 142 143 if (!mLocalActivityManager.getCurrentActivity().isFinishing()){ 144 fail(); 145 return; 146 } 147 pass(); 148 } 149 testSaveInstanceState()150 private void testSaveInstanceState() { 151 final String key = "_id1"; 152 mLocalActivityManager.dispatchCreate(null); 153 final Bundle bundle = mLocalActivityManager.saveInstanceState(); 154 if (bundle != null) { 155 fail(); 156 return; 157 } 158 159 final String id = "id_dispatch_pause"; 160 final Intent intent = new Intent(this, LocalActivityManagerStubActivity.class); 161 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 162 mLocalActivityManager.startActivity(id, intent); 163 164 final Bundle savedBundle = new Bundle(); 165 final Bundle bb = new Bundle(); 166 savedBundle.putBundle(key, bb); 167 168 mLocalActivityManager.dispatchCreate(savedBundle); 169 final Bundle returnedBundle = mLocalActivityManager.saveInstanceState(); 170 if (returnedBundle.getBundle(key) == null ) { 171 fail(); 172 return; 173 } 174 pass(); 175 } 176 testDispatchPauseFalse()177 private void testDispatchPauseFalse() { 178 final String id = "id_dispatch_pause"; 179 final Intent intent = new Intent(this, LocalActivityManagerStubActivity.class); 180 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 181 mLocalActivityManager.startActivity(id, intent); 182 LocalActivityManagerStubActivity.sIsOnPauseCalled = false; 183 mLocalActivityManager.dispatchPause(false); 184 if (!LocalActivityManagerStubActivity.sIsOnPauseCalled) { 185 fail(); 186 return; 187 } 188 pass(); 189 } 190 testDispatchPauseTrue()191 private void testDispatchPauseTrue() { 192 final String id = "id_dispatch_pause"; 193 final Intent intent = new Intent(this, LocalActivityManagerStubActivity.class); 194 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 195 mLocalActivityManager.startActivity(id, intent); 196 LocalActivityManagerStubActivity.sIsOnPauseCalled = false; 197 mLocalActivityManager.dispatchPause(true); 198 if (!LocalActivityManagerStubActivity.sIsOnPauseCalled) { 199 fail(); 200 return; 201 } 202 pass(); 203 } 204 testDispatchStop()205 private void testDispatchStop() { 206 final String id = "id_dispatch_stop"; 207 final Intent intent = new Intent(this, LocalActivityManagerStubActivity.class); 208 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 209 mLocalActivityManager.startActivity(id, intent); 210 if (mLocalActivityManager.getCurrentActivity() == null) { 211 fail(); 212 return; 213 } 214 215 LocalActivityManagerStubActivity.sIsOnStopCalled = false; 216 mLocalActivityManager.dispatchStop(); 217 218 if (!LocalActivityManagerStubActivity.sIsOnStopCalled) { 219 fail(); 220 return; 221 } 222 pass(); 223 } 224 testDispatchCreate()225 private void testDispatchCreate() { 226 final Bundle EXPECTED = new Bundle(); 227 final String id = "id"; 228 229 final Intent intent = new Intent(this, LocalActivityManagerStubActivity.class); 230 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 231 mLocalActivityManager.startActivity("_id" + System.currentTimeMillis(), intent); 232 final Bundle bundle = mLocalActivityManager.saveInstanceState(); 233 if (bundle == null) { 234 fail(); 235 return; 236 } 237 238 if (bundle.keySet().size() != 1) { 239 fail(); 240 return; 241 } 242 243 bundle.putBundle(id, EXPECTED); 244 // test null parameter 245 mLocalActivityManager.dispatchCreate(null); 246 247 if (mLocalActivityManager.saveInstanceState().keySet().size() != 1) { 248 fail(); 249 return; 250 } 251 252 mLocalActivityManager.dispatchCreate(bundle); 253 254 final Bundle b = mLocalActivityManager.saveInstanceState(); 255 final Bundle bb = b.getBundle(id); 256 if (bb != EXPECTED) { 257 fail(); 258 return; 259 } 260 pass(); 261 } 262 testStartActivity()263 private void testStartActivity() { 264 final Intent intent = new Intent(this, LocalActivityManagerStubActivity.class); 265 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 266 final String id = "_id_resume_test"; 267 final Window w = mLocalActivityManager.startActivity(id, intent); 268 if (w == null) { 269 fail(); 270 return; 271 } 272 273 Activity activity = mLocalActivityManager.getActivity(id); 274 if (activity == null) { 275 fail(); 276 return; 277 } 278 279 // testing null id 280 activity = mLocalActivityManager.getActivity("null id"); 281 if (activity != null) { 282 fail(); 283 return; 284 } 285 286 if (!mLocalActivityManager.getCurrentId().equals(id)) { 287 fail(); 288 return; 289 } 290 291 if (mLocalActivityManager.getActivity(id) != mLocalActivityManager 292 .getCurrentActivity()) { 293 fail(); 294 return; 295 } 296 297 if (mLocalActivityManager.destroyActivity(id, true) == null) { 298 fail(); 299 return; 300 } 301 302 if (mLocalActivityManager.startActivity(null, intent) == null) { 303 fail(); 304 return; 305 } 306 307 try { 308 // test when calling startActivity with both null parameter. 309 mLocalActivityManager.startActivity(null, null); 310 fail(); 311 return; 312 } catch (NullPointerException e) { 313 } 314 pass(); 315 } 316 testDispatchResume()317 private void testDispatchResume() { 318 final Intent intent = new Intent(this, LocalActivityManagerStubActivity.class); 319 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 320 mLocalActivityManager.startActivity("_id_resume_test", intent); 321 mLocalActivityManager.dispatchStop(); 322 LocalActivityManagerStubActivity.sIsOnResumeCalled = false; 323 mLocalActivityManager.dispatchResume(); 324 if (LocalActivityManagerStubActivity.sIsOnResumeCalled) { 325 pass(); 326 } else { 327 fail(); 328 } 329 } 330 fail()331 private void fail() { 332 sResult.setResult(CTSResult.RESULT_FAIL); 333 finish(); 334 } 335 pass()336 private void pass() { 337 sResult.setResult(CTSResult.RESULT_OK); 338 finish(); 339 } 340 } 341