Android Instrumentation - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android Instrumentation class.

This code example shows how to use the following methods:sendKeySync
        // foreground, and steal UI focus 
        // unfortunately this means the next key event will get directed to the 
        // 'volume adjustment' UI, instead of this test's activity 
        // for this reason this test must be signed with platform certificate, to grant this test 
        // permission to inject key events to another process 
        getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
                KeyEvent.KEYCODE_VOLUME_DOWN));
        getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
                KeyEvent.KEYCODE_POWER));
    } 
 
    /** 
     * Get the directory where screenshot images are stored. 
     */ 
    private File getScreenshotDir() {
        // TODO: get this dir location from a constant 
        return new File(Environment.getExternalStorageDirectory(), "Pictures" + File.separator +
                "Screenshots"); 
    } 
 
7
CodeRank
This code example shows how to use the following methods:getTargetContext
     * 
     * @param instrumentation 
     */ 
    public BootstrapApplication(final Instrumentation instrumentation) {
        this(); 
        attachBaseContext(instrumentation.getTargetContext());
    } 
 
    public static BootstrapApplication getInstance() { 
        return instance;
    } 
} 
7
CodeRank
This code example shows how to use the following methods:callActivityOnPause, callActivityOnResume, sleep
    } 
     
 
    public void testPauseAndResume() 
    { 
        mInstr.callActivityOnPause(testActivity);
        sleep(); 
        assertTrue(testView.isPaused());
        mInstr.callActivityOnResume(testActivity);
        sleep(); 
        assertFalse(testView.isPaused());
    } 
     
    private void sleep() { 
        try { 
          Thread.sleep(TIMEOUT);
        } catch (InterruptedException e) {
          fail("Unexpected Timeout");
        } 
    } 
} 
8
CodeRank
This code example shows how to use the following methods:startActivitySync, getFlags, setFlags
        instrumentation.sendStringSync("s");
        poseForScreenshotNamed("ConfigureMorseActivity-SOS"); 
    } 
 
    private <T extends Activity> T startActivitySync(Class<T> clazz) {
        Intent intent = new Intent(getInstrumentation().getTargetContext(), clazz);
        intent.setFlags(intent.getFlags() | FLAG_ACTIVITY_NEW_TASK);
        return (T) getInstrumentation().startActivitySync(intent);
    } 
 
} 
8
CodeRank
This code example shows how to use the following methods:getTargetContext
This code example shows how to use the following methods:sendKeySync
 
    /** 
     * Inject the key sequence to take a screenshot. 
     */ 
    private void takeScreenshot() { 
        getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN,
                KeyEvent.KEYCODE_POWER));
        getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN,
                KeyEvent.KEYCODE_VOLUME_DOWN));
        // the volume down key event will cause the 'volume adjustment' UI to appear in the 
        // foreground, and steal UI focus 
        // unfortunately this means the next key event will get directed to the 
        // 'volume adjustment' UI, instead of this test's activity 
        // for this reason this test must be signed with platform certificate, to grant this test 
        // permission to inject key events to another process 
        getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
                KeyEvent.KEYCODE_VOLUME_DOWN));
        getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
                KeyEvent.KEYCODE_POWER));
    } 
7
CodeRank
This code example shows how to use the following methods:callActivityOnCreate, callApplicationOnCreate, newActivity, newApplication
 
        IcedRobotContext context = new IcedRobotAppContext();
        Instrumentation instrumentation = appManager.getInstrumentation();
         
        ClassLoader loader = appManager.getClassLoader();
        Activity activity =
                instrumentation.newActivity(loader, activityClass.getName(),
                                            null); 
        Application application =
                instrumentation.newApplication(loader,
                                               Application.class.getName(),
                                               context);
        instrumentation.callApplicationOnCreate(application);
         
        appManager.attachActivity(activity, context, instrumentation, null, 0,
                                  application, null, null, null, null, null,
                                  null, null, null); 
        // TODO: attach activity 
        instrumentation.callActivityOnCreate(activity, null);        
    } 

6
CodeRank
This code example shows how to use the following methods:sendCharacterSync, waitForIdleSync, sleep
    public void testTouchScrollingToTrackballMode() { 
        Instrumentation inst = getInstrumentation();
 
        int firstVisiblePosition = touchScroll(inst);
 
        inst.sendCharacterSync(KeyEvent.KEYCODE_DPAD_DOWN);
        inst.waitForIdleSync();
        inst.sendCharacterSync(KeyEvent.KEYCODE_DPAD_DOWN);
        inst.waitForIdleSync();
        inst.sendCharacterSync(KeyEvent.KEYCODE_BACK);
        inst.waitForIdleSync();
        assertTrue("List not in trackball mode", !mListView.isInTouchMode());
        assertTrue("List did not preserve scroll position", firstVisiblePosition == mListView
                .getFirstVisiblePosition()); 
    } 
     
    public int arrowScroll(Instrumentation inst) {
        int count = mListView.getChildCount();
 
        for (int i = 0; i < count * 2; i++) {

7
CodeRank
This code example shows how to use the following methods:getContext, sendPointerSync, waitForIdleSync
      throw new WebDriverException(exception);
    } 
  } 
 
  protected void sendMotionEvent(MotionEvent motionEvent) {
    instrumentation.waitForIdleSync();
    instrumentation.sendPointerSync(motionEvent);
  } 
 
  protected boolean isTouchStateReleased() { 
    return downTime == UNDEFINED_TIME;
  } 
 
  protected int getScaledTouchSlopAdjustment() { 
    int touchSlop = ViewConfiguration.get(instrumentation.getContext())
        .getScaledTouchSlop();
    return touchSlop / 2;
  } 
 
  private void updateActiveCoordinates(Coordinates coordinates) {
6
CodeRank
This code example shows how to use the following methods:
  public SpadeModule() 
  { 
    super(); 
 
    TiApplication appContext = TiApplication.getInstance();
    mInst = new Instrumentation();
    Activity activity = appContext.getCurrentActivity();
 
    mSolo = new Solo(mInst, activity);
 
    mSleeper = new Sleeper();
    mActivityUtils = new ActivityUtils(mInst, activity, mSleeper);
    mViewFetcher = new ViewFetcher(mActivityUtils);
    mScroller = new Scroller(mInst, mActivityUtils, mViewFetcher, mSleeper);
    mSearcher = new Searcher(mViewFetcher, mScroller, mSleeper);
    mClicker = new Clicker(mViewFetcher, mScroller, mRobotiumUtils, mInst, mSleeper, mWaiter);
    mTextEnterer = new TextEnterer(mInst, mClicker);
  } 
 
  @Kroll.onAppCreate 

6
CodeRank

Related Android Instrumentation Questions & Answers:

See Code Examples for Android Instrumentation Methods: