Code example for Instrumentation

Methods: sendKeyDownUpSyncsleep

0
    } 
 
    private void sleepFor(long time) {
        Log.v(TAG,TAG2+":sleepFor "+time+" ms");
        try { 
            Thread.sleep(time);
        } catch (InterruptedException e) {
        } 
    } 
 
    // 
    //  Make sure the all the UI shows correctly when the activity pop up. 
    // 
    public void testCase01_activityDisplay() { 
        try { 
            //get worker thread filed 
            Field f = GalleryPicker.class.getDeclaredField("mWorkerThread");
            f.setAccessible(true);
            Thread t = (Thread)f.get(mActivity);
            //wait till this thread finished 
            Log.d(TAG,TAG2+":testCase01_activityDisplay:call mWorkerThread.join()");
            t.join();
            Log.d(TAG,TAG2+":testCase01_activityDisplay:mWorkerThread.join() returned");
            //get mScanning filed 
            f = GalleryPicker.class.getDeclaredField("mScanning");
            f.setAccessible(true);
            boolean scanning = f.getBoolean(mActivity);
            Log.v(TAG,TAG2+":testCase01_activityDisplay:mScanning="+scanning);
            //get item nums 
            int numItems = mActivity.mNumItems_test;
            Log.d(TAG,TAG2+":testCase01_activityDisplay:mAdapter.mItems.size()="+numItems);
 
            if (numItems == 0) {
                mNoMedia = true;
                Log.d(TAG,TAG2+":testCase01_activityDisplay:mNoMedia="+mNoMedia);
            } else if (numItems == 1) {
                mEnteredOnlyFolder = true;
                Log.d(TAG,TAG2+":testCase01_activityDisplay:mEnteredOnlyFolder="+mEnteredOnlyFolder);
            } 
 
        } catch (java.lang.Exception ex) {
            Log.e(TAG,TAG2+":testCase01_activityDisplay:got Exception:"+ex);
        } 
 
        assertFalse(mNoMedia);
 
    } 
 
    // 
    //  Make sure options menu can correctly pop up. 
    // 
    public void testCase02_OptionsMenu() { 
        //make sure it is reasonable to test 
        assertFalse(mNoMedia);
        if (mEnteredOnlyFolder) {
            Log.e(TAG,TAG2+":testCase02_OptionsMenu:already entered only folder, no bother...");
            return; 
        } 
 
        Log.d(TAG,TAG2+":testCase02_OptionsMenu:create options menu");
        //send menu key message to show options menu 
    	mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
        //sleep for a while. 
        sleepFor(MEDIUM_SLEEP_TIME);
 
        Log.d(TAG,TAG2+":testCase02_OptionsMenu:close options menu");
        //send back key message to close options menu 
    	mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
        //sleep for a while. 
        sleepFor(MEDIUM_SLEEP_TIME);
    } 
     
    // 
    //  Make sure options menu can correctly pop up and enter Camera. 
    // 
/* 
    public void testCase03_OptionsMenuEnterCamera() { 
        //make sure it is reasonable to test 
        assertFalse(mNoMedia); 
        if (mEnteredOnlyFolder) { 
            Log.e(TAG,TAG2+":testCase02_OptionsMenu:already entered only folder, no bother..."); 
            return; 
        } 
 
        Log.d(TAG,TAG2+":testCase03_OptionsMenuEnterCamera:create options menu"); 
        //send menu key message to show options menu 
    	mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU); 
        try { 
            Thread.sleep(1000); 
        } catch (InterruptedException e) { 
        } 
 
        //check whether Window has Focus 
        boolean hasWinFocus = mActivity.hasWindowFocus(); 
        Log.d(TAG,TAG2+":testCase03_OptionsMenuEnterCamera:hasWinFocus="+hasWinFocus); 
 
        if (hasWinFocus) { 
            Log.d(TAG,TAG2+":testCase03_OptionsMenuEnterCamera:send DPAD_CENTER to enter camera"); 
            //if window has focus, then send KeyEvent.KEYCODE_DPAD_CENTER to enter camera 
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER); 
        } else { 
            Log.d(TAG,TAG2+":testCase03_OptionsMenuEnterCamera:send DPAD_LEFT to focus Camera item"); 
            //send KEYCODE_DPAD_LEFT to let window aquire a focus 
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_LEFT); 
            //sleep for a while. 
            sleepFor(MEDIUM_SLEEP_TIME); 
 
            Log.d(TAG,TAG2+":testCase03_OptionsMenuEnterCamera:send DPAD_CENTER to enter camera"); 
            //hen send KeyEvent.KEYCODE_DPAD_CENTER to enter camera 
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER); 
        } 
 
        //sleep for a long time. 
        sleepFor(LONG_SLEEP_TIME); 
 
        Log.d(TAG,TAG2+":testCase03_OptionsMenuEnterCamera:send BACK key to exit camera"); 
        //send back key message to exit camera 
    	mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK); 
        //sleep for a while. 
        sleepFor(MEDIUM_SLEEP_TIME); 
    } 
*/ 
    // 
    //  Make sure options menu can correctly pop up and enter CameraVideo. 
    // 
/* 
    public void testCase04_OptionsMenuEnterCameraVideo() { 
        //make sure it is reasonable to test 
        assertFalse(mNoMedia); 
        if (mEnteredOnlyFolder) { 
            Log.e(TAG,TAG2+":testCase04_OptionsMenuEnterCameraVideo:already entered only folder, no bother..."); 
            return; 
        } 
 
        Log.d(TAG,TAG2+":testCase04_OptionsMenuEnterCameraVideo:create options menu"); 
        //send menu key message to show options menu 
    	mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU); 
        try { 
            Thread.sleep(1000); 
        } catch (InterruptedException e) { 
        } 
 
        //check whether Window has Focus 
        boolean hasWinFocus = mActivity.hasWindowFocus(); 
        Log.d(TAG,TAG2+":testCase04_OptionsMenuEnterCameraVideo:hasWinFocus="+hasWinFocus); 
 
        if (hasWinFocus) { 
            Log.d(TAG,TAG2+":testCase04_OptionsMenuEnterCameraVideo:send DPAD_CENTER to enter camera video"); 
            //send KeyEvent.KEYCODE_DPAD_RIGHT to focus camera video 
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT); 
            //sleep for a short time. 
            sleepFor(SHORT_SLEEP_TIME); 
 
            //send KeyEvent.KEYCODE_DPAD_CENTER to enter camera video 
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER); 
        } else { 
            Log.d(TAG,TAG2+":testCase04_OptionsMenuEnterCameraVideo:send DPAD_LEFT to focus Camera video item"); 
            //send KEYCODE_DPAD_LEFT to let window aquire a focus 
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_LEFT); 
            //sleep for a short time. 
            sleepFor(SHORT_SLEEP_TIME); 
 
            //send KeyEvent.KEYCODE_DPAD_RIGHT to focus camera video 
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT); 
            //sleep for a short time. 
            sleepFor(SHORT_SLEEP_TIME); 
 
            Log.d(TAG,TAG2+":testCase04_OptionsMenuEnterCameraVideo:send DPAD_CENTER to enter camera video"); 
            //hen send KeyEvent.KEYCODE_DPAD_CENTER to enter camera Video 
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER); 
        } 
 
        //sleep for a long time. 
        sleepFor(LONG_SLEEP_TIME); 
 
        Log.d(TAG,TAG2+":testCase04_OptionsMenuEnterCameraVideo:send BACK key to exit camera video"); 
        //send back key message to exit camera video 
    	mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK); 
        //sleep for a while. 
        sleepFor(MEDIUM_SLEEP_TIME); 
    } 
*/ 
    // 
    //  Make sure options menu can correctly pop up and enter Settings. 
    // 
    public void testCase05_OptionsMenuEnterSettings() { 
        //make sure it is reasonable to test 
        assertFalse(mNoMedia);
        if (mEnteredOnlyFolder) {
            Log.e(TAG,TAG2+":testCase05_OptionsMenuEnterSettings:already entered only folder, no bother...");
            return; 
        } 
 
        Log.d(TAG,TAG2+":testCase05_OptionsMenuEnterSettings:create options menu");
        //send menu key message to show options menu 
    	mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
        try { 
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        } 
 
        //check whether Window has Focus 
        boolean hasWinFocus = mActivity.hasWindowFocus();
        Log.d(TAG,TAG2+":testCase05_OptionsMenuEnterSettings:hasWinFocus="+hasWinFocus);
 
        if (hasWinFocus) {
            Log.d(TAG,TAG2+":testCase05_OptionsMenuEnterSettings:send DPAD_RIGHT twice to focus Settings");
            //if window has focus, then send KeyEvent.KEYCODE_DPAD_RIGHT twice to focus Settings 
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
            //sleep for a short time. 
            sleepFor(SHORT_SLEEP_TIME);
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
            //sleep for a short time. 
            sleepFor(SHORT_SLEEP_TIME);
            //send DPAD_CENTER to enter settings 
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
        } else { 
            Log.d(TAG,TAG2+":testCase05_OptionsMenuEnterSettings:send DPAD_LEFT & DPAD_RIGHT to focus Settings");
            //send KEYCODE_DPAD_LEFT to let window aquire a focus 
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_LEFT);
            //sleep for a short time. 
            sleepFor(SHORT_SLEEP_TIME);
 
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
            //sleep for a short time. 
            sleepFor(SHORT_SLEEP_TIME);
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
            //sleep for a short time. 
            sleepFor(SHORT_SLEEP_TIME);
 
            Log.d(TAG,TAG2+":testCase05_OptionsMenuEnterSettings:send DPAD_CENTER to enter Settings");
            //hen send KeyEvent.KEYCODE_DPAD_CENTER to enter Settings 
            mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
        } 
 
        //sleep for a long time. 
        sleepFor(LONG_SLEEP_TIME);
 
        Log.d(TAG,TAG2+":testCase05_OptionsMenuEnterSettings:send BACK key to exit Settings");
        //send back key message to exit Settings 
    	mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
        //sleep for a while. 
        sleepFor(MEDIUM_SLEEP_TIME);
    } 
 
/*