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