• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.cts;
18 
19 import java.util.List;
20 
21 import android.app.Activity;
22 import android.app.Application;
23 import android.app.Instrumentation;
24 import android.app.Instrumentation.ActivityMonitor;
25 import android.app.Instrumentation.ActivityResult;
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.IntentFilter;
30 import android.content.pm.ActivityInfo;
31 import android.content.res.Configuration;
32 import android.graphics.drawable.Drawable;
33 import android.net.Uri;
34 import android.os.Bundle;
35 import android.os.Debug;
36 import android.os.IBinder;
37 import android.os.SystemClock;
38 import android.test.InstrumentationTestCase;
39 import android.view.KeyEvent;
40 import android.view.LayoutInflater;
41 import android.view.MotionEvent;
42 import android.view.View;
43 import android.view.Window;
44 import android.view.ViewGroup.LayoutParams;
45 
46 import com.android.cts.stub.R;
47 
48 import dalvik.annotation.TestLevel;
49 import dalvik.annotation.TestTargetClass;
50 import dalvik.annotation.TestTargetNew;
51 import dalvik.annotation.TestTargets;
52 import dalvik.annotation.ToBeFixed;
53 
54 @TestTargetClass(Instrumentation.class)
55 public class InstrumentationTest extends InstrumentationTestCase {
56 
57     private static final int WAIT_TIME = 1000;
58     private Instrumentation mInstrumentation;
59     private InstrumentationTestActivity mActivity;
60     private Intent mIntent;
61     private boolean mRunOnMainSyncResult;
62     private Context mContext;
63     private MotionEvent mMotionEvent;
64 
65     @Override
setUp()66     protected void setUp() throws Exception {
67         super.setUp();
68         mInstrumentation = getInstrumentation();
69         mContext = mInstrumentation.getTargetContext();
70         final long downTime = SystemClock.uptimeMillis();
71         final long eventTime = SystemClock.uptimeMillis();
72         // use coordinates for MotionEvent that do not include the status bar
73         // TODO: is there a more deterministic way to get these values
74         final long x = 50;
75         final long y = 50;
76         final int metaState = 0;
77         mMotionEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y,
78                 metaState);
79         mIntent = new Intent(mContext, InstrumentationTestActivity.class);
80         mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
81         mActivity = (InstrumentationTestActivity) mInstrumentation.startActivitySync(mIntent);
82     }
83 
tearDown()84     protected void tearDown() throws Exception {
85         mInstrumentation = null;
86         mIntent = null;
87         if (mActivity != null) {
88             mActivity.finish();
89         }
90         super.tearDown();
91     }
92 
93     @TestTargetNew(
94         level = TestLevel.COMPLETE,
95         method = "Instrumentation",
96         args = {}
97     )
testConstructor()98     public void testConstructor() throws Exception {
99         new Instrumentation();
100     }
101 
102     @TestTargets({
103         @TestTargetNew(
104             level = TestLevel.COMPLETE,
105             method = "addMonitor",
106             args = {ActivityMonitor.class}
107         ),
108         @TestTargetNew(
109             level = TestLevel.COMPLETE,
110             method = "addMonitor",
111             args = {IntentFilter.class, ActivityResult.class, boolean.class}
112         ),
113         @TestTargetNew(
114             level = TestLevel.COMPLETE,
115             method = "addMonitor",
116             args = {String.class, ActivityResult.class, boolean.class}
117         ),
118         @TestTargetNew(
119             level = TestLevel.COMPLETE,
120             method = "checkMonitorHit",
121             args = {ActivityMonitor.class, int.class}
122         ),
123         @TestTargetNew(
124             level = TestLevel.COMPLETE,
125             method = "waitForMonitor",
126             args = {ActivityMonitor.class}
127         ),
128         @TestTargetNew(
129             level = TestLevel.COMPLETE,
130             method = "waitForMonitorWithTimeout",
131             args = {ActivityMonitor.class, long.class}
132         ),
133         @TestTargetNew(
134             level = TestLevel.COMPLETE,
135             method = "removeMonitor",
136             args = {ActivityMonitor.class}
137         ),
138         @TestTargetNew(
139             level = TestLevel.COMPLETE,
140             method = "startActivitySync",
141             args = {Intent.class}
142         )
143     })
testMonitor()144     public void testMonitor() throws Exception {
145         if (mActivity != null)
146             mActivity.finish();
147         ActivityResult result = new ActivityResult(Activity.RESULT_OK, new Intent());
148         ActivityMonitor monitor = new ActivityMonitor(
149                 InstrumentationTestActivity.class.getName(), result, false);
150         mInstrumentation.addMonitor(monitor);
151         Intent intent = new Intent(mContext, InstrumentationTestActivity.class);
152         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
153         mContext.startActivity(intent);
154         Activity activity = mInstrumentation.waitForMonitorWithTimeout(monitor, WAIT_TIME);
155         assertTrue(activity instanceof InstrumentationTestActivity);
156         assertTrue(mInstrumentation.checkMonitorHit(monitor, 1));
157         activity.finish();
158 
159         mInstrumentation.addMonitor(monitor);
160         mInstrumentation.removeMonitor(monitor);
161         Activity a = mInstrumentation.startActivitySync(intent);
162         assertTrue(a instanceof InstrumentationTestActivity);
163         activity = mInstrumentation.waitForMonitorWithTimeout(monitor, WAIT_TIME);
164         assertNull(activity);
165         a.finish();
166 
167         IntentFilter filter = new IntentFilter();
168         ActivityMonitor am = mInstrumentation.addMonitor(filter, result, false);
169         mContext.startActivity(intent);
170         mInstrumentation.waitForIdleSync();
171         activity = am.waitForActivity();
172         assertTrue(activity instanceof InstrumentationTestActivity);
173         activity.finish();
174         mInstrumentation.removeMonitor(am);
175         am = mInstrumentation
176                 .addMonitor(InstrumentationTestActivity.class.getName(), result, false);
177         mContext.startActivity(intent);
178         activity = am.waitForActivity();
179         assertTrue(activity instanceof InstrumentationTestActivity);
180         activity.finish();
181         mInstrumentation.removeMonitor(am);
182     }
183 
184     @TestTargets({
185         @TestTargetNew(
186             level = TestLevel.COMPLETE,
187             method = "callActivityOnCreate",
188             args = {Activity.class, Bundle.class}
189         ),
190         @TestTargetNew(
191             level = TestLevel.COMPLETE,
192             method = "waitForIdleSync",
193             args = {}
194         )
195     })
testCallActivityOnCreate()196     public void testCallActivityOnCreate() throws Throwable {
197         mActivity.setOnCreateCalled(false);
198         runTestOnUiThread(new Runnable() {
199             public void run() {
200                 mInstrumentation.callActivityOnCreate(mActivity, new Bundle());
201             }
202         });
203         mInstrumentation.waitForIdleSync();
204         assertTrue(mActivity.isOnCreateCalled());
205     }
206 
207     @TestTargets({
208         @TestTargetNew(
209             level = TestLevel.COMPLETE,
210             method = "stopAllocCounting",
211             args = {}
212         ),
213         @TestTargetNew(
214             level = TestLevel.COMPLETE,
215             method = "startAllocCounting",
216             args = {}
217         ),
218         @TestTargetNew(
219             level = TestLevel.COMPLETE,
220             method = "getAllocCounts",
221             args = {}
222         ),
223         @TestTargetNew(
224             level = TestLevel.COMPLETE,
225             method = "getBinderCounts",
226             args = {}
227         )
228     })
testAllocCounting()229     public void testAllocCounting() throws Exception {
230         mInstrumentation.startAllocCounting();
231 
232         Bundle b = mInstrumentation.getAllocCounts();
233         assertTrue(b.size() > 0);
234         b = mInstrumentation.getBinderCounts();
235         assertTrue(b.size() > 0);
236 
237         int globeAllocCount = Debug.getGlobalAllocCount();
238         int globeAllocSize = Debug.getGlobalAllocSize();
239         int globeExternalAllCount = Debug.getGlobalExternalAllocCount();
240         int globeExternalAllSize = Debug.getGlobalExternalAllocSize();
241         int threadAllocCount = Debug.getThreadAllocCount();
242 
243         assertTrue(Debug.getGlobalAllocCount() >= globeAllocCount);
244         assertTrue(Debug.getGlobalAllocSize() >= globeAllocSize);
245         assertTrue(Debug.getGlobalExternalAllocCount() >= globeExternalAllCount);
246         assertTrue(Debug.getGlobalExternalAllocSize() >= globeExternalAllSize);
247         assertTrue(Debug.getThreadAllocCount() >= threadAllocCount);
248 
249         mInstrumentation.stopAllocCounting();
250 
251         globeAllocCount = Debug.getGlobalAllocCount();
252         globeAllocSize = Debug.getGlobalAllocSize();
253         globeExternalAllCount = Debug.getGlobalExternalAllocCount();
254         globeExternalAllSize = Debug.getGlobalExternalAllocSize();
255         threadAllocCount = Debug.getThreadAllocCount();
256         assertEquals(globeAllocCount, Debug.getGlobalAllocCount());
257         assertEquals(globeAllocSize, Debug.getGlobalAllocSize());
258         assertEquals(globeExternalAllCount, Debug.getGlobalExternalAllocCount());
259         assertEquals(globeExternalAllSize, Debug.getGlobalExternalAllocSize());
260         assertEquals(threadAllocCount, Debug.getThreadAllocCount());
261     }
262 
263     @TestTargetNew(
264         level = TestLevel.COMPLETE,
265         method = "sendTrackballEventSync",
266         args = {MotionEvent.class}
267     )
testSendTrackballEventSync()268     public void testSendTrackballEventSync() throws Exception {
269         mInstrumentation.sendTrackballEventSync(mMotionEvent);
270         mInstrumentation.waitForIdleSync();
271 
272         MotionEvent motionEvent = mActivity.getMotionEvent();
273         assertEquals(mMotionEvent.getMetaState(), motionEvent.getMetaState());
274         assertEquals(mMotionEvent.getEventTime(), motionEvent.getEventTime());
275         assertEquals(mMotionEvent.getDownTime(), motionEvent.getDownTime());
276     }
277 
278     @TestTargetNew(
279         level = TestLevel.COMPLETE,
280         method = "callApplicationOnCreate",
281         args = {Application.class}
282     )
testCallApplicationOnCreate()283     public void testCallApplicationOnCreate() throws Exception {
284         InstrumentationTestStub ca = new InstrumentationTestStub();
285         mInstrumentation.callApplicationOnCreate(ca);
286         assertTrue(ca.mIsOnCreateCalled);
287     }
288 
289     @TestTargets({
290         @TestTargetNew(
291             level = TestLevel.COMPLETE,
292             method = "getContext",
293             args = {}
294         ),
295         @TestTargetNew(
296             level = TestLevel.COMPLETE,
297             method = "getTargetContext",
298             args = {}
299         )
300     })
testContext()301     public void testContext() throws Exception {
302         Context c1 = mInstrumentation.getContext();
303         Context c2 = mInstrumentation.getTargetContext();
304         assertNotSame(c1.getPackageName(), c2.getPackageName());
305     }
306 
307     @TestTargetNew(
308         level = TestLevel.COMPLETE,
309         method = "invokeMenuActionSync",
310         args = {Activity.class, int.class, int.class}
311     )
testInvokeMenuActionSync()312     public void testInvokeMenuActionSync() throws Exception {
313         final int resId = R.id.goto_menu_id;
314         mInstrumentation.invokeMenuActionSync(mActivity, resId, 0);
315         mInstrumentation.waitForIdleSync();
316 
317         assertEquals(resId, mActivity.getMenuID());
318     }
319 
320     @TestTargetNew(
321         level = TestLevel.COMPLETE,
322         method = "callActivityOnPostCreate",
323         args = {Activity.class, Bundle.class}
324     )
testCallActivityOnPostCreate()325     public void testCallActivityOnPostCreate() throws Throwable {
326         mActivity.setOnPostCreate(false);
327         runTestOnUiThread(new Runnable() {
328             public void run() {
329                 mInstrumentation.callActivityOnPostCreate(mActivity, new Bundle());
330             }
331         });
332         mInstrumentation.waitForIdleSync();
333         assertTrue(mActivity.isOnPostCreate());
334     }
335 
336     @TestTargetNew(
337         level = TestLevel.COMPLETE,
338         method = "callActivityOnNewIntent",
339         args = {Activity.class, Intent.class}
340     )
testCallActivityOnNewIntent()341     public void testCallActivityOnNewIntent() throws Throwable {
342         mActivity.setOnNewIntentCalled(false);
343         runTestOnUiThread(new Runnable() {
344             public void run() {
345                 mInstrumentation.callActivityOnNewIntent(mActivity, null);
346             }
347         });
348         mInstrumentation.waitForIdleSync();
349 
350         assertTrue(mActivity.isOnNewIntentCalled());
351     }
352 
353     @TestTargetNew(
354         level = TestLevel.COMPLETE,
355         method = "callActivityOnResume",
356         args = {Activity.class}
357     )
testCallActivityOnResume()358     public void testCallActivityOnResume() throws Throwable {
359         mActivity.setOnResume(false);
360         runTestOnUiThread(new Runnable() {
361             public void run() {
362                 mInstrumentation.callActivityOnResume(mActivity);
363             }
364         });
365         mInstrumentation.waitForIdleSync();
366         assertTrue(mActivity.isOnResume());
367     }
368 
369     @TestTargets({
370         @TestTargetNew(
371             level = TestLevel.NOT_FEASIBLE,
372             notes = "can't start a Instrumentation to test this method",
373             method = "onCreate",
374             args = {Bundle.class}
375         ),
376         @TestTargetNew(
377             level = TestLevel.NOT_FEASIBLE,
378             notes = "can't start a Instrumentation to test this method",
379             method = "onDestroy",
380             args = {}
381         ),
382         @TestTargetNew(
383             level = TestLevel.NOT_FEASIBLE,
384             notes = "call this method will crash the process",
385             method = "finish",
386             args = {int.class, Bundle.class}
387         ),
388         @TestTargetNew(
389             level = TestLevel.NOT_FEASIBLE,
390             notes = "can't start a Instrumentation to test this method",
391             method = "onStart",
392             args = {}
393         ),
394         @TestTargetNew(
395             level = TestLevel.NOT_FEASIBLE,
396             notes = "can't start a Instrumentation to test this method",
397             method = "onException",
398             args = {Object.class, Throwable.class}
399         ),
400         @TestTargetNew(
401             level = TestLevel.PARTIAL,
402             method = "start",
403             args = {}
404         ),
405         @TestTargetNew(
406             level = TestLevel.NOT_FEASIBLE,
407             method = "sendStatus",
408             args = {int.class, Bundle.class}
409         )
410     })
testMisc()411     public void testMisc() throws Exception {
412     }
413 
414     @TestTargets({
415         @TestTargetNew(
416             level = TestLevel.COMPLETE,
417             method = "setAutomaticPerformanceSnapshots",
418             args = {}
419         ),
420         @TestTargetNew(
421             level = TestLevel.COMPLETE,
422             method = "startPerformanceSnapshot",
423             args = {}
424         ),
425         @TestTargetNew(
426             level = TestLevel.COMPLETE,
427             method = "endPerformanceSnapshot",
428             args = {}
429         )
430     })
testPerformanceSnapshot()431     public void testPerformanceSnapshot() throws Exception {
432         mInstrumentation.setAutomaticPerformanceSnapshots();
433         mInstrumentation.startPerformanceSnapshot();
434         mInstrumentation.endPerformanceSnapshot();
435     }
436 
437     @TestTargets({
438         @TestTargetNew(
439             level = TestLevel.NOT_FEASIBLE,
440             method = "isProfiling",
441             args = {}
442         ),
443         @TestTargetNew(
444             level = TestLevel.COMPLETE,
445             method = "startProfiling",
446             args = {}
447         ),
448         @TestTargetNew(
449             level = TestLevel.COMPLETE,
450             method = "stopProfiling",
451             args = {}
452         )
453     })
testProfiling()454     public void testProfiling() throws Exception {
455         // by default, profiling was disabled. but after set the handleProfiling attribute in the
456         // manifest file for this Instrumentation to true, the profiling was also disabled.
457         assertFalse(mInstrumentation.isProfiling());
458 
459         mInstrumentation.startProfiling();
460         mInstrumentation.stopProfiling();
461     }
462 
463     @TestTargetNew(
464         level = TestLevel.COMPLETE,
465         method = "invokeContextMenuAction",
466         args = {Activity.class, int.class, int.class}
467     )
testInvokeContextMenuAction()468     public void testInvokeContextMenuAction() throws Exception {
469         MockActivity activity = new MockActivity();
470         final int id = 1;
471         final int flag = 2;
472         mInstrumentation.invokeContextMenuAction(activity, id, flag);
473         mInstrumentation.waitForIdleSync();
474 
475         assertEquals(id, activity.mWindow.mId);
476         assertEquals(flag, activity.mWindow.mFlags);
477     }
478 
479     @TestTargetNew(
480         level = TestLevel.COMPLETE,
481         method = "sendStringSync",
482         args = {String.class}
483     )
testSendStringSync()484     public void testSendStringSync() {
485         final String text = "abcd";
486         mInstrumentation.sendStringSync(text);
487         mInstrumentation.waitForIdleSync();
488 
489         List<KeyEvent> keyUpList = mActivity.getKeyUpList();
490         List<KeyEvent> keyDownList = mActivity.getKeyDownList();
491         assertEquals(text.length(), keyDownList.size());
492         assertEquals(text.length(), keyUpList.size());
493 
494         for (int i = 0; i < keyDownList.size(); i++) {
495             assertEquals(KeyEvent.KEYCODE_A + i, keyDownList.get(i).getKeyCode());
496         }
497 
498         for (int i = 0; i < keyUpList.size(); i++) {
499             assertEquals(KeyEvent.KEYCODE_A + i, keyUpList.get(i).getKeyCode());
500         }
501     }
502 
503     @TestTargetNew(
504         level = TestLevel.COMPLETE,
505         method = "callActivityOnSaveInstanceState",
506         args = {Activity.class, Bundle.class}
507     )
testCallActivityOnSaveInstanceState()508     public void testCallActivityOnSaveInstanceState() {
509         final Bundle bundle = new Bundle();
510         mActivity.setOnSaveInstanceState(false);
511         mInstrumentation.callActivityOnSaveInstanceState(mActivity, bundle);
512         mInstrumentation.waitForIdleSync();
513 
514         assertTrue(mActivity.isOnSaveInstanceState());
515         assertSame(bundle, mActivity.getBundle());
516     }
517 
518     @TestTargets({
519         @TestTargetNew(
520             level = TestLevel.PARTIAL,
521             method = "setInTouchMode",
522             args = {boolean.class}
523         ),
524         @TestTargetNew(
525             level = TestLevel.COMPLETE,
526             method = "sendPointerSync",
527             args = {MotionEvent.class}
528         )
529     })
testSendPointerSync()530     public void testSendPointerSync() throws Exception {
531         mInstrumentation.waitForIdleSync();
532         mInstrumentation.setInTouchMode(true);
533         mInstrumentation.sendPointerSync(mMotionEvent);
534         mInstrumentation.waitForIdleSync();
535         assertTrue(mActivity.isOnTouchEventCalled());
536         mActivity.setOnTouchEventCalled(false);
537     }
538 
539     @TestTargetNew(
540         level = TestLevel.COMPLETE,
541         method = "getComponentName",
542         args = {}
543     )
testGetComponentName()544     public void testGetComponentName() throws Exception {
545         ComponentName com = getInstrumentation().getComponentName();
546         assertNotNull(com.getPackageName());
547         assertNotNull(com.getClassName());
548         assertNotNull(com.getShortClassName());
549     }
550 
551     @TestTargets({
552         @TestTargetNew(
553             level = TestLevel.COMPLETE,
554             method = "newApplication",
555             args = {Class.class, Context.class}
556         ),
557         @TestTargetNew(
558             level = TestLevel.COMPLETE,
559             method = "newApplication",
560             args = {ClassLoader.class, String.class, Context.class}
561         )
562     })
testNewApplication()563     public void testNewApplication() throws Exception {
564         final String className = "android.app.cts.MockApplication";
565         ClassLoader cl = getClass().getClassLoader();
566 
567         Application app = mInstrumentation.newApplication(cl, className, mContext);
568         assertEquals(className, app.getClass().getName());
569 
570         app = Instrumentation.newApplication(MockApplication.class, mContext);
571         assertEquals(className, app.getClass().getName());
572     }
573 
574     @TestTargetNew(
575         level = TestLevel.COMPLETE,
576         method = "runOnMainSync",
577         args = {Runnable.class}
578     )
testRunOnMainSync()579     public void testRunOnMainSync() throws Exception {
580         mRunOnMainSyncResult = false;
581         mInstrumentation.runOnMainSync(new Runnable() {
582             public void run() {
583                 mRunOnMainSyncResult = true;
584             }
585         });
586         mInstrumentation.waitForIdleSync();
587         assertTrue(mRunOnMainSyncResult);
588     }
589 
590     @TestTargetNew(
591         level = TestLevel.COMPLETE,
592         method = "callActivityOnPause",
593         args = {Activity.class}
594     )
testCallActivityOnPause()595     public void testCallActivityOnPause() throws Exception {
596         mActivity.setOnPauseCalled(false);
597         mInstrumentation.callActivityOnPause(mActivity);
598         mInstrumentation.waitForIdleSync();
599         assertTrue(mActivity.isOnPauseCalled());
600     }
601 
602     @TestTargetNew(
603         level = TestLevel.COMPLETE,
604         method = "callActivityOnDestroy",
605         args = {Activity.class}
606     )
testCallActivityOnDestroy()607     public void testCallActivityOnDestroy() throws Exception {
608         mActivity.setOnDestroyCalled(false);
609         mInstrumentation.callActivityOnDestroy(mActivity);
610         mInstrumentation.waitForIdleSync();
611         assertTrue(mActivity.isOnDestroyCalled());
612     }
613 
614     @TestTargetNew(
615         level = TestLevel.COMPLETE,
616         method = "sendKeyDownUpSync",
617         args = {int.class}
618     )
testSendKeyDownUpSync()619     public void testSendKeyDownUpSync() throws Exception {
620         mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_0);
621         mInstrumentation.waitForIdleSync();
622         assertEquals(1, mActivity.getKeyUpList().size());
623         assertEquals(1, mActivity.getKeyDownList().size());
624         assertEquals(KeyEvent.KEYCODE_0, mActivity.getKeyUpList().get(0).getKeyCode());
625         assertEquals(KeyEvent.KEYCODE_0, mActivity.getKeyDownList().get(0).getKeyCode());
626     }
627 
628     @TestTargets({
629         @TestTargetNew(
630             level = TestLevel.COMPLETE,
631             method = "newActivity",
632             args = {ClassLoader.class, String.class, Intent.class}
633         ),
634         @TestTargetNew(
635             level = TestLevel.COMPLETE,
636             method = "newActivity",
637             args = {Class.class, Context.class, IBinder.class, Application.class, Intent.class,
638                     ActivityInfo.class, CharSequence.class, Activity.class, String.class,
639                     Object.class}
640         )
641     })
testNewActivity()642     public void testNewActivity() throws Exception {
643         Intent intent = new Intent();
644         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
645 
646         ClassLoader cl = getClass().getClassLoader();
647         Activity activity = mInstrumentation.newActivity(cl, InstrumentationTestActivity.class
648                 .getName(), intent);
649         assertEquals(InstrumentationTestActivity.class.getName(), activity.getClass().getName());
650         activity.finish();
651         activity = null;
652 
653         intent = new Intent(mContext, InstrumentationTestActivity.class);
654         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
655 
656         Activity father = new Activity();
657         ActivityInfo info = new ActivityInfo();
658 
659         activity = mInstrumentation
660                 .newActivity(InstrumentationTestActivity.class, mContext, null, null, intent, info,
661                         InstrumentationTestActivity.class.getName(), father, null, null);
662 
663         assertEquals(father, activity.getParent());
664         assertEquals(InstrumentationTestActivity.class.getName(), activity.getClass().getName());
665         activity.finish();
666     }
667 
668     @TestTargetNew(
669         level = TestLevel.COMPLETE,
670         method = "callActivityOnStart",
671         args = {Activity.class}
672     )
testCallActivityOnStart()673     public void testCallActivityOnStart() throws Exception {
674         mActivity.setOnStart(false);
675         mInstrumentation.callActivityOnStart(mActivity);
676         mInstrumentation.waitForIdleSync();
677         assertTrue(mActivity.isOnStart());
678     }
679 
680     @TestTargetNew(
681         level = TestLevel.COMPLETE,
682         method = "waitForIdle",
683         args = {Runnable.class}
684     )
testWaitForIdle()685     public void testWaitForIdle() throws Exception {
686         MockRunnable mr = new MockRunnable();
687         assertFalse(mr.isRunCalled());
688         mInstrumentation.waitForIdle(mr);
689         Thread.sleep(WAIT_TIME);
690         assertTrue(mr.isRunCalled());
691     }
692 
693     @TestTargetNew(
694         level = TestLevel.COMPLETE,
695         method = "sendCharacterSync",
696         args = {int.class}
697     )
testSendCharacterSync()698     public void testSendCharacterSync() throws Exception {
699         mInstrumentation.sendCharacterSync(KeyEvent.KEYCODE_0);
700         mInstrumentation.waitForIdleSync();
701         assertEquals(KeyEvent.KEYCODE_0, mActivity.getKeyDownCode());
702         assertEquals(KeyEvent.KEYCODE_0, mActivity.getKeyUpCode());
703     }
704 
705     @TestTargetNew(
706         level = TestLevel.COMPLETE,
707         method = "callActivityOnRestart",
708         args = {Activity.class}
709     )
testCallActivityOnRestart()710     public void testCallActivityOnRestart() throws Exception {
711         mActivity.setOnRestart(false);
712         mInstrumentation.callActivityOnRestart(mActivity);
713         mInstrumentation.waitForIdleSync();
714         assertTrue(mActivity.isOnRestart());
715     }
716 
717     @TestTargetNew(
718         level = TestLevel.COMPLETE,
719         method = "callActivityOnStop",
720         args = {Activity.class}
721     )
testCallActivityOnStop()722     public void testCallActivityOnStop() throws Exception {
723         mActivity.setOnStop(false);
724         mInstrumentation.callActivityOnStop(mActivity);
725         mInstrumentation.waitForIdleSync();
726         assertTrue(mActivity.isOnStop());
727     }
728 
729     @TestTargetNew(
730         level = TestLevel.COMPLETE,
731         method = "callActivityOnUserLeaving",
732         args = {Activity.class}
733     )
testCallActivityOnUserLeaving()734     public void testCallActivityOnUserLeaving() throws Exception {
735         assertFalse(mActivity.isOnLeave());
736         mInstrumentation.callActivityOnUserLeaving(mActivity);
737         mInstrumentation.waitForIdleSync();
738         assertTrue(mActivity.isOnLeave());
739     }
740 
741     @TestTargetNew(
742         level = TestLevel.COMPLETE,
743         method = "callActivityOnRestoreInstanceState",
744         args = {Activity.class, Bundle.class}
745     )
testCallActivityOnRestoreInstanceState()746     public void testCallActivityOnRestoreInstanceState() throws Exception {
747         mActivity.setOnRestoreInstanceState(false);
748         mInstrumentation.callActivityOnRestoreInstanceState(mActivity, new Bundle());
749         mInstrumentation.waitForIdleSync();
750         assertTrue(mActivity.isOnRestoreInstanceState());
751     }
752 
753     @TestTargetNew(
754         level = TestLevel.COMPLETE,
755         method = "sendKeySync",
756         args = {KeyEvent.class}
757     )
testSendKeySync()758     public void testSendKeySync() throws Exception {
759         KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0);
760         mInstrumentation.sendKeySync(key);
761         mInstrumentation.waitForIdleSync();
762         assertEquals(KeyEvent.KEYCODE_0, mActivity.getKeyDownCode());
763     }
764 
765     private static class MockRunnable implements Runnable {
766         private boolean mIsRunCalled ;
767 
run()768         public void run() {
769             mIsRunCalled = true;
770         }
771 
isRunCalled()772         public boolean isRunCalled() {
773             return mIsRunCalled;
774         }
775     }
776 
777     private class MockActivity extends Activity {
778         MockWindow mWindow = new MockWindow(mContext);
779 
780         @Override
getWindow()781         public Window getWindow() {
782             return mWindow;
783         }
784 
785         private class MockWindow extends Window {
786 
787             public int mId;
788             public int mFlags;
789 
MockWindow(Context context)790             public MockWindow(Context context) {
791                 super(context);
792             }
793 
794             @Override
addContentView(View view, LayoutParams params)795             public void addContentView(View view, LayoutParams params) {
796             }
797 
798             @Override
closeAllPanels()799             public void closeAllPanels() {
800             }
801 
802             @Override
closePanel(int featureId)803             public void closePanel(int featureId) {
804             }
805 
806             @Override
getCurrentFocus()807             public View getCurrentFocus() {
808                 return null;
809             }
810 
811             @Override
getDecorView()812             public View getDecorView() {
813                 return null;
814             }
815 
816             @Override
getLayoutInflater()817             public LayoutInflater getLayoutInflater() {
818                 return null;
819             }
820 
821             @Override
getVolumeControlStream()822             public int getVolumeControlStream() {
823                 return 0;
824             }
825 
826             @Override
isFloating()827             public boolean isFloating() {
828                 return false;
829             }
830 
831             @Override
isShortcutKey(int keyCode, KeyEvent event)832             public boolean isShortcutKey(int keyCode, KeyEvent event) {
833                 return false;
834             }
835 
836             @Override
onActive()837             protected void onActive() {
838             }
839 
840             @Override
onConfigurationChanged(Configuration newConfig)841             public void onConfigurationChanged(Configuration newConfig) {
842             }
843 
844             @Override
openPanel(int featureId, KeyEvent event)845             public void openPanel(int featureId, KeyEvent event) {
846             }
847 
848             @Override
peekDecorView()849             public View peekDecorView() {
850                 return null;
851             }
852 
853             @Override
performContextMenuIdentifierAction(int id, int flags)854             public boolean performContextMenuIdentifierAction(int id, int flags) {
855                 mId = id;
856                 mFlags = flags;
857                 return false;
858             }
859 
860             @Override
performPanelIdentifierAction(int featureId, int id, int flags)861             public boolean performPanelIdentifierAction(int featureId, int id, int flags) {
862                 return false;
863             }
864 
865             @Override
performPanelShortcut(int featureId, int keyCode, KeyEvent event, int flags)866             public boolean performPanelShortcut(int featureId, int keyCode,
867                     KeyEvent event, int flags) {
868                 return false;
869             }
870 
871             @Override
restoreHierarchyState(Bundle savedInstanceState)872             public void restoreHierarchyState(Bundle savedInstanceState) {
873             }
874 
875             @Override
saveHierarchyState()876             public Bundle saveHierarchyState() {
877                 return null;
878             }
879 
880             @Override
setBackgroundDrawable(Drawable drawable)881             public void setBackgroundDrawable(Drawable drawable) {
882             }
883 
884             @Override
setChildDrawable(int featureId, Drawable drawable)885             public void setChildDrawable(int featureId, Drawable drawable) {
886             }
887 
888             @Override
setChildInt(int featureId, int value)889             public void setChildInt(int featureId, int value) {
890             }
891 
892             @Override
setContentView(int layoutResID)893             public void setContentView(int layoutResID) {
894             }
895 
896             @Override
setContentView(View view)897             public void setContentView(View view) {
898             }
899 
900             @Override
setContentView(View view, LayoutParams params)901             public void setContentView(View view, LayoutParams params) {
902             }
903 
904             @Override
setFeatureDrawable(int featureId, Drawable drawable)905             public void setFeatureDrawable(int featureId, Drawable drawable) {
906             }
907 
908             @Override
setFeatureDrawableAlpha(int featureId, int alpha)909             public void setFeatureDrawableAlpha(int featureId, int alpha) {
910             }
911 
912             @Override
setFeatureDrawableResource(int featureId, int resId)913             public void setFeatureDrawableResource(int featureId, int resId) {
914             }
915 
916             @Override
setFeatureDrawableUri(int featureId, Uri uri)917             public void setFeatureDrawableUri(int featureId, Uri uri) {
918             }
919 
920             @Override
setFeatureInt(int featureId, int value)921             public void setFeatureInt(int featureId, int value) {
922             }
923 
924             @Override
setTitle(CharSequence title)925             public void setTitle(CharSequence title) {
926             }
927 
928             @Override
setTitleColor(int textColor)929             public void setTitleColor(int textColor) {
930             }
931 
932             @Override
setVolumeControlStream(int streamType)933             public void setVolumeControlStream(int streamType) {
934             }
935 
936             @Override
superDispatchKeyEvent(KeyEvent event)937             public boolean superDispatchKeyEvent(KeyEvent event) {
938                 return false;
939             }
940 
941             @Override
superDispatchTouchEvent(MotionEvent event)942             public boolean superDispatchTouchEvent(MotionEvent event) {
943                 return false;
944             }
945 
946             @Override
superDispatchTrackballEvent(MotionEvent event)947             public boolean superDispatchTrackballEvent(MotionEvent event) {
948                 return false;
949             }
950 
951             @Override
takeKeyEvents(boolean get)952             public void takeKeyEvents(boolean get) {
953             }
954 
955             @Override
togglePanel(int featureId, KeyEvent event)956             public void togglePanel(int featureId, KeyEvent event) {
957             }
958         }
959     }
960 
961     private static class InstrumentationTestStub extends Application {
962         boolean mIsOnCreateCalled = false;
963 
964         @Override
onCreate()965         public void onCreate() {
966             super.onCreate();
967             mIsOnCreateCalled = true;
968         }
969     }
970 }
971