1 /* 2 * Copyright (C) 2009 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.stubs; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 import android.app.Activity; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.os.Bundle; 25 import android.view.KeyEvent; 26 import android.view.Menu; 27 import android.view.MenuInflater; 28 import android.view.MenuItem; 29 import android.view.MotionEvent; 30 import android.view.View; 31 import android.view.WindowManager; 32 import android.widget.TextView; 33 34 public class InstrumentationTestActivity extends Activity { 35 36 public static final String START_INTENT = "android.app.stubs.activity.INSTRUMENTATION_TEST"; 37 38 private boolean mOnCreateCalled; 39 private boolean mOnDestroyCalled ; 40 private boolean mOnNewIntentCalled; 41 private boolean mOnPauseCalled; 42 private boolean mOnPostCreate; 43 private boolean mOnRestart; 44 private boolean mOnRestoreInstanceState; 45 private boolean mOnResume; 46 private boolean mOnSaveInstanceState; 47 private boolean mOnStart; 48 private boolean mOnStop; 49 private boolean mOnMenuOpened; 50 private boolean mOnLeave; 51 private int mMenuID; 52 private boolean mOnTouchEventCalled; 53 private int mKeyDownCode; 54 private int mKeyUpCode; 55 private MotionEvent mMotionEvent; 56 private Bundle mBundle; 57 private MockTextView mTextView; 58 private List<KeyEvent> mKeyDownList = new ArrayList<KeyEvent>(); 59 private List<KeyEvent> mKeyUpList = new ArrayList<KeyEvent>(); 60 61 @Override onCreate(Bundle savedInstanceState)62 public void onCreate(Bundle savedInstanceState) { 63 super.onCreate(savedInstanceState); 64 mTextView = new MockTextView(this); 65 setContentView(mTextView); 66 mOnCreateCalled = true; 67 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 68 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON 69 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 70 } 71 72 class MockTextView extends TextView { 73 MockTextView(Context context)74 public MockTextView(Context context) { 75 super(context); 76 } 77 78 @Override onTouchEvent(MotionEvent event)79 public boolean onTouchEvent(MotionEvent event) { 80 return super.onTouchEvent(event); 81 } 82 83 @Override onTrackballEvent(MotionEvent event)84 public boolean onTrackballEvent(MotionEvent event) { 85 return super.onTrackballEvent(event); 86 } 87 88 @Override getLocationOnScreen(int[] location)89 public void getLocationOnScreen(int[] location) { 90 super.getLocationOnScreen(location); 91 location[0] = location[1] = 10; 92 } 93 } 94 95 @Override onDestroy()96 protected void onDestroy() { 97 super.onDestroy(); 98 mOnDestroyCalled = true; 99 } 100 101 @Override onNewIntent(Intent intent)102 protected void onNewIntent(Intent intent) { 103 super.onNewIntent(intent); 104 mOnNewIntentCalled = true; 105 } 106 107 @Override onPause()108 protected void onPause() { 109 super.onPause(); 110 mOnPauseCalled = true; 111 } 112 113 @Override onPostCreate(Bundle savedInstanceState)114 protected void onPostCreate(Bundle savedInstanceState) { 115 super.onPostCreate(savedInstanceState); 116 mOnPostCreate = true; 117 } 118 119 @Override onRestart()120 protected void onRestart() { 121 super.onRestart(); 122 mOnRestart = true; 123 } 124 125 @Override onRestoreInstanceState(Bundle savedInstanceState)126 protected void onRestoreInstanceState(Bundle savedInstanceState) { 127 super.onRestoreInstanceState(savedInstanceState); 128 mOnRestoreInstanceState = true; 129 } 130 131 @Override onTouchEvent(MotionEvent event)132 public boolean onTouchEvent(MotionEvent event) { 133 mOnTouchEventCalled = true; 134 mMotionEvent = event; 135 return super.onTouchEvent(event); 136 } 137 138 @Override onResume()139 protected void onResume() { 140 super.onResume(); 141 mOnResume = true; 142 } 143 144 @Override onSaveInstanceState(Bundle outState)145 protected void onSaveInstanceState(Bundle outState) { 146 super.onSaveInstanceState(outState); 147 mOnSaveInstanceState = true; 148 mBundle = outState; 149 } 150 151 @Override onStart()152 protected void onStart() { 153 super.onStart(); 154 mOnStart = true; 155 } 156 157 @Override onStop()158 protected void onStop() { 159 super.onStop(); 160 mOnStop = true; 161 } 162 163 @Override onUserLeaveHint()164 protected void onUserLeaveHint() { 165 super.onUserLeaveHint(); 166 mOnLeave = true; 167 } 168 169 @Override onMenuOpened(int featureId, Menu menu)170 public boolean onMenuOpened(int featureId, Menu menu) { 171 mOnMenuOpened = true; 172 return super.onMenuOpened(featureId, menu); 173 } 174 175 @Override onCreateOptionsMenu(Menu menu)176 public boolean onCreateOptionsMenu(Menu menu) { 177 super.onCreateOptionsMenu(menu); 178 MenuInflater inflater = getMenuInflater(); 179 inflater.inflate(R.menu.browser, menu); 180 181 menu.add("title"); 182 mOnMenuOpened = true; 183 return true; 184 } 185 186 @Override onCreatePanelMenu(int featureId, Menu menu)187 public boolean onCreatePanelMenu(int featureId, Menu menu) { 188 return super.onCreatePanelMenu(featureId, menu); 189 } 190 191 @Override onMenuItemSelected(int featureId, MenuItem item)192 public boolean onMenuItemSelected(int featureId, MenuItem item) { 193 mMenuID = item.getItemId(); 194 return super.onMenuItemSelected(featureId, item); 195 } 196 197 @Override openContextMenu(View view)198 public void openContextMenu(View view) { 199 super.openContextMenu(view); 200 } 201 202 @Override openOptionsMenu()203 public void openOptionsMenu() { 204 super.openOptionsMenu(); 205 } 206 207 @Override onOptionsItemSelected(MenuItem item)208 public boolean onOptionsItemSelected(MenuItem item) { 209 return super.onOptionsItemSelected(item); 210 } 211 212 @Override onKeyDown(int keyCode, KeyEvent event)213 public boolean onKeyDown(int keyCode, KeyEvent event) { 214 mKeyDownList.add(event); 215 mKeyDownCode = keyCode; 216 return super.onKeyDown(keyCode, event); 217 } 218 219 @Override onTrackballEvent(MotionEvent event)220 public boolean onTrackballEvent(MotionEvent event) { 221 mMotionEvent = event; 222 return super.onTrackballEvent(event); 223 } 224 225 @Override onKeyUp(int keyCode, KeyEvent event)226 public boolean onKeyUp(int keyCode, KeyEvent event) { 227 mKeyUpList.add(event); 228 mKeyUpCode = keyCode; 229 return super.onKeyUp(keyCode, event); 230 } 231 isOnCreateCalled()232 public boolean isOnCreateCalled() { 233 return mOnCreateCalled; 234 } 235 setOnCreateCalled(boolean onCreateCalled)236 public void setOnCreateCalled(boolean onCreateCalled) { 237 mOnCreateCalled = onCreateCalled; 238 } 239 isOnDestroyCalled()240 public boolean isOnDestroyCalled() { 241 return mOnDestroyCalled; 242 } 243 setOnDestroyCalled(boolean onDestroyCalled)244 public void setOnDestroyCalled(boolean onDestroyCalled) { 245 mOnDestroyCalled = onDestroyCalled; 246 } 247 isOnNewIntentCalled()248 public boolean isOnNewIntentCalled() { 249 return mOnNewIntentCalled; 250 } 251 setOnNewIntentCalled(boolean onNewIntentCalled)252 public void setOnNewIntentCalled(boolean onNewIntentCalled) { 253 mOnNewIntentCalled = onNewIntentCalled; 254 } 255 isOnPauseCalled()256 public boolean isOnPauseCalled() { 257 return mOnPauseCalled; 258 } 259 setOnPauseCalled(boolean onPauseCalled)260 public void setOnPauseCalled(boolean onPauseCalled) { 261 mOnPauseCalled = onPauseCalled; 262 } 263 isOnPostCreate()264 public boolean isOnPostCreate() { 265 return mOnPostCreate; 266 } 267 setOnPostCreate(boolean onPostCreate)268 public void setOnPostCreate(boolean onPostCreate) { 269 mOnPostCreate = onPostCreate; 270 } 271 isOnRestart()272 public boolean isOnRestart() { 273 return mOnRestart; 274 } 275 setOnRestart(boolean onRestart)276 public void setOnRestart(boolean onRestart) { 277 mOnRestart = onRestart; 278 } 279 isOnRestoreInstanceState()280 public boolean isOnRestoreInstanceState() { 281 return mOnRestoreInstanceState; 282 } 283 setOnRestoreInstanceState(boolean onRestoreInstanceState)284 public void setOnRestoreInstanceState(boolean onRestoreInstanceState) { 285 mOnRestoreInstanceState = onRestoreInstanceState; 286 } 287 isOnResume()288 public boolean isOnResume() { 289 return mOnResume; 290 } 291 setOnResume(boolean onResume)292 public void setOnResume(boolean onResume) { 293 mOnResume = onResume; 294 } 295 isOnSaveInstanceState()296 public boolean isOnSaveInstanceState() { 297 return mOnSaveInstanceState; 298 } 299 setOnSaveInstanceState(boolean onSaveInstanceState)300 public void setOnSaveInstanceState(boolean onSaveInstanceState) { 301 mOnSaveInstanceState = onSaveInstanceState; 302 } 303 isOnStart()304 public boolean isOnStart() { 305 return mOnStart; 306 } 307 setOnStart(boolean onStart)308 public void setOnStart(boolean onStart) { 309 mOnStart = onStart; 310 } 311 isOnStop()312 public boolean isOnStop() { 313 return mOnStop; 314 } 315 isOnLeave()316 public boolean isOnLeave() { 317 return mOnLeave; 318 } 319 setOnStop(boolean onStop)320 public void setOnStop(boolean onStop) { 321 mOnStop = onStop; 322 } 323 isMOnMenuOpened()324 public boolean isMOnMenuOpened() { 325 return mOnMenuOpened; 326 } 327 setOnMenuOpened(boolean onMenuOpened)328 public void setOnMenuOpened(boolean onMenuOpened) { 329 mOnMenuOpened = onMenuOpened; 330 } 331 getMenuID()332 public int getMenuID() { 333 return mMenuID; 334 } 335 setMenuID(int menuID)336 public void setMenuID(int menuID) { 337 mMenuID = menuID; 338 } 339 getMotionEvent()340 public MotionEvent getMotionEvent() { 341 return mMotionEvent; 342 } 343 getBundle()344 public Bundle getBundle() { 345 return mBundle; 346 } 347 isOnTouchEventCalled()348 public boolean isOnTouchEventCalled() { 349 return mOnTouchEventCalled; 350 } 351 setOnTouchEventCalled(boolean onTouchEventCalled)352 public void setOnTouchEventCalled(boolean onTouchEventCalled) { 353 mOnTouchEventCalled = onTouchEventCalled; 354 } 355 getKeyUpCode()356 public int getKeyUpCode() { 357 return mKeyUpCode; 358 } 359 getKeyDownCode()360 public int getKeyDownCode() { 361 return mKeyDownCode; 362 } 363 getKeyUpList()364 public List<KeyEvent> getKeyUpList() { 365 return mKeyUpList; 366 } 367 getKeyDownList()368 public List<KeyEvent> getKeyDownList() { 369 return mKeyDownList; 370 } 371 } 372