當前位置: 首頁>>代碼示例>>Java>>正文


Java Instrumentation類代碼示例

本文整理匯總了Java中android.app.Instrumentation的典型用法代碼示例。如果您正苦於以下問題:Java Instrumentation類的具體用法?Java Instrumentation怎麽用?Java Instrumentation使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Instrumentation類屬於android.app包,在下文中一共展示了Instrumentation類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: makeApplication

import android.app.Instrumentation; //導入依賴的package包/類
private Application makeApplication(boolean forceDefaultAppClass, Instrumentation instrumentation) {
    if (null != this.mApplication) {
        return this.mApplication;
    }

    String appClass = this.mPackage.applicationInfo.className;
    if (forceDefaultAppClass || null == appClass) {
        appClass = "android.app.Application";
    }

    try {
        this.mApplication = instrumentation.newApplication(this.mClassLoader, appClass, this.getPluginContext());
        instrumentation.callApplicationOnCreate(this.mApplication);
        return this.mApplication;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
開發者ID:didi,項目名稱:VirtualAPK,代碼行數:20,代碼來源:LoadedPlugin.java

示例2: testA

import android.app.Instrumentation; //導入依賴的package包/類
/**
 * 測試CollapsingToolbarLayout
 * 被測Demo下載地址:https://github.com/alidili/DesignSupportDemo
 *
 * @throws UiObjectNotFoundException
 */
public void testA() throws UiObjectNotFoundException {
    // 獲取設備對象
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    UiDevice uiDevice = UiDevice.getInstance(instrumentation);
    // 獲取上下文
    Context context = instrumentation.getContext();

    // 啟動測試App
    Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.yang.designsupportdemo");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    // 打開CollapsingToolbarLayout
    String resourceId = "com.yang.designsupportdemo:id/CollapsingToolbarLayout";
    UiObject collapsingToolbarLayout = uiDevice.findObject(new UiSelector().resourceId(resourceId));
    collapsingToolbarLayout.click();

    for (int i = 0; i < 5; i++) {
        // 向上移動
        uiDevice.swipe(uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight(),
                uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight() / 2, 10);

        // 向下移動
        uiDevice.swipe(uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight() / 2,
                uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight(), 10);
    }

    // 點擊應用返回按鈕
    UiObject back = uiDevice.findObject(new UiSelector().description("Navigate up"));
    back.click();

    // 點擊設備返回按鈕
    uiDevice.pressBack();
}
 
開發者ID:alidili,項目名稱:Demos,代碼行數:41,代碼來源:UiTest.java

示例3: testB

import android.app.Instrumentation; //導入依賴的package包/類
/**
 * 滑動界麵,打開About phone選項
 * 測試環境為標準Android 7.1.1版本,不同設備控件查找方式會有不同
 *
 * @throws UiObjectNotFoundException
 */
public void testB() throws UiObjectNotFoundException {
    // 獲取設備對象
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    UiDevice uiDevice = UiDevice.getInstance(instrumentation);
    // 獲取上下文
    Context context = instrumentation.getContext();

    // 點擊Settings按鈕
    UiObject uiObject = uiDevice.findObject(new UiSelector().description("Settings"));
    uiObject.click();

    // 滑動列表到最後,點擊About phone選項
    UiScrollable settings = new UiScrollable(new UiSelector().className("android.support.v7.widget.RecyclerView"));
    UiObject about = settings.getChildByText(new UiSelector().className("android.widget.LinearLayout"), "About phone");
    about.click();

    // 點擊設備返回按鈕
    uiDevice.pressBack();
    uiDevice.pressBack();
}
 
開發者ID:alidili,項目名稱:Demos,代碼行數:27,代碼來源:UiTest.java

示例4: startTestActivity

import android.app.Instrumentation; //導入依賴的package包/類
@Test
public void startTestActivity() throws Exception {
    final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();

    // start the activity for the first time
    final TestActivity activity = startTestActivity(instrumentation);

    // make sure the attached presenter filled the UI
    Espresso.onView(withId(R.id.helloworld_text))
            .check(matches(allOf(isDisplayed(), withText("Hello World 1"))));

    Espresso.onView(withId(R.id.fragment_helloworld_text))
            .check(matches(allOf(isDisplayed(), withText("Hello World 1"))));

    activity.finish();
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:17,代碼來源:TiPluginTest.java

示例5: testFullLifecycleIncludingConfigurationChange

import android.app.Instrumentation; //導入依賴的package包/類
/**
 * Tests the full Activity lifecycle. Guarantees every lifecycle method gets called
 */
@Test
public void testFullLifecycleIncludingConfigurationChange() throws Throwable {
    final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();

    // start the activity for the first time
    final TestActivity activity = startTestActivity(instrumentation);

    // make sure the attached presenter filled the UI
    Espresso.onView(withId(R.id.helloworld_text))
            .check(matches(allOf(isDisplayed(), withText("Hello World 1"))));
    Espresso.onView(withId(R.id.fragment_helloworld_text))
            .check(matches(allOf(isDisplayed(), withText("Hello World 1"))));

    // restart the activity
    rotateOrientation(activity);

    // assert the activity was bound to the presenter. The presenter should update the UI
    // correctly
    Espresso.onView(withId(R.id.helloworld_text))
            .check(matches(allOf(isDisplayed(), withText("Hello World 2"))));
    Espresso.onView(withId(R.id.fragment_helloworld_text))
            .check(matches(allOf(isDisplayed(), withText("Hello World 2"))));

    activity.finish();
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:29,代碼來源:TiPluginTest.java

示例6: waitForOverlay

import android.app.Instrumentation; //導入依賴的package包/類
void waitForOverlay(long millis) {
    if (!testSystemLayer()) {
        Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(MainActivity.class.getCanonicalName(),
                null, false);
        getInstrumentation().addMonitor(monitor);
        getActivityRule().launchActivity(new Intent(getApplication(), MainActivity.class));
        try {
            monitor.waitForActivityWithTimeout(5000);
        } finally {
            getInstrumentation().removeMonitor(monitor);
        }
    }
    try {
        Thread.currentThread().sleep(millis);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
 
開發者ID:Manabu-GT,項目名稱:DebugOverlay-Android,代碼行數:19,代碼來源:DebugOverlayInstrumentedTest.java

示例7: execStartActivity

import android.app.Instrumentation; //導入依賴的package包/類
public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, Fragment fragment,
                                        Intent intent, int requestCode, Bundle options) {
    try {
        Intent targetIntent = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            targetIntent = HookActivity_Intent.modify(fragment.getActivity(), intent);
        }
        if (targetIntent == null) {
            targetIntent = intent;
        }
        ApkMethod method = new ApkMethod(Instrumentation.class, mInstrumentation, "execStartActivity", Context.class, IBinder.class, IBinder.class, Fragment.class, Intent.class, int.class, Bundle.class);
        return method.invoke(who, contextThread, token, fragment, targetIntent, requestCode, options);
    } catch (Exception error) {
        ApkLogger.get().error("execStartActivity Exception", error);
        return null;
    }
}
 
開發者ID:LiangMaYong,項目名稱:android-apkbox,代碼行數:18,代碼來源:HookActivityInstrumentationHnadler.java

示例8: openMenu

import android.app.Instrumentation; //導入依賴的package包/類
/** IMAGE PROCESSING FUNCTION */


    public void openMenu()
    {
        new Thread(new Runnable()
        {
            public void run()
            {
                KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU);
                new Instrumentation().sendKeySync(event);
                KeyEvent event2 = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU);
                new Instrumentation().sendKeySync(event2);
            }
        }).start();
    }
 
開發者ID:icaynia,項目名稱:pracler,代碼行數:17,代碼來源:PlayerActivity.java

示例9: callOnMainSync

import android.app.Instrumentation; //導入依賴的package包/類
private static <X> X callOnMainSync(Instrumentation instrumentation, final Callable<X> callable)
        throws Exception {
    final AtomicReference<X> retAtomic = new AtomicReference<>();
    final AtomicReference<Throwable> exceptionAtomic = new AtomicReference<>();
    instrumentation.runOnMainSync(new Runnable() {
        @Override
        public void run() {
            try {
                retAtomic.set(callable.call());
            } catch (Throwable e) {
                exceptionAtomic.set(e);
            }
        }
    });
    final Throwable exception = exceptionAtomic.get();
    if (exception != null) {
        Throwables.propagateIfInstanceOf(exception, Exception.class);
        Throwables.propagate(exception);
    }
    return retAtomic.get();
}
 
開發者ID:Adyen,項目名稱:adyen-android,代碼行數:22,代碼來源:EspressoTestUtils.java

示例10: closeActivity

import android.app.Instrumentation; //導入依賴的package包/類
private static boolean closeActivity(Instrumentation instrumentation) throws Exception {
    final Boolean activityClosed = callOnMainSync(instrumentation, new Callable<Boolean>() {
        @Override
        public Boolean call() throws Exception {
            final Set<Activity> activities = getActivitiesInStages(Stage.RESUMED,
                    Stage.STARTED, Stage.PAUSED, Stage.STOPPED, Stage.CREATED);
            activities.removeAll(getActivitiesInStages(Stage.DESTROYED));
            if (activities.size() > 0) {
                final Activity activity = activities.iterator().next();
                activity.finish();
                return true;
            } else {
                return false;
            }
        }
    });
    if (activityClosed) {
        instrumentation.waitForIdleSync();
    }
    return activityClosed;
}
 
開發者ID:Adyen,項目名稱:adyen-android,代碼行數:22,代碼來源:EspressoTestUtils.java

示例11: getWriteableDir

import android.app.Instrumentation; //導入依賴的package包/類
/**
 * Prefer internal over external storage, because external tends to be FAT filesystems,
 * which don't support symlinks (which we test using this method).
 */
public static File getWriteableDir(Instrumentation instrumentation) {
    Context context = instrumentation.getContext();
    Context targetContext = instrumentation.getTargetContext();

    File[] dirsToTry = new File[]{
            context.getCacheDir(),
            context.getFilesDir(),
            targetContext.getCacheDir(),
            targetContext.getFilesDir(),
            context.getExternalCacheDir(),
            context.getExternalFilesDir(null),
            targetContext.getExternalCacheDir(),
            targetContext.getExternalFilesDir(null),
            Environment.getExternalStorageDirectory(),
    };

    return getWriteableDir(dirsToTry);
}
 
開發者ID:uhuru-mobile,項目名稱:mobile-store,代碼行數:23,代碼來源:FileCompatTest.java

示例12: pickGalleryPhoto

import android.app.Instrumentation; //導入依賴的package包/類
private static void pickGalleryPhoto() throws IOException {

        Drawable drawable = getTargetContext().getResources().getDrawable(R.drawable.upc_library);
        Bitmap bmp = drawableToBitmap(drawable);

        File storageDir =
                new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
                        "Camera");
        File image = File.createTempFile("testImage", ".jpg", storageDir);

        FileOutputStream out = new FileOutputStream(image);
        bmp.compress(CompressFormat.JPEG, 100, out);

        // Build a result to return from the Camera app
        Intent resultData = new Intent();
        resultData.setData(Uri.fromFile(image));
        Instrumentation.ActivityResult result =
                new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);

        // Stub out the Camera. When an intent is sent to the Camera, this tells Espresso to respond
        // with the ActivityResult we just created
        intending(not(isInternal())).respondWith(result);
        intended(not(isInternal()));
    }
 
開發者ID:ArnauBlanch,項目名稱:civify-app,代碼行數:25,代碼來源:CreateIssueActivityTest.java

示例13: hookStartActivity

import android.app.Instrumentation; //導入依賴的package包/類
private void hookStartActivity() {
    try {
        Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
        Method currentActivityThreadMethod = activityThreadClass.getDeclaredMethod("currentActivityThread");
        currentActivityThreadMethod.setAccessible(true);
        Object currentActivityThread = currentActivityThreadMethod.invoke(null);

        Field mInstrumentationField = activityThreadClass.getDeclaredField("mInstrumentation");
        mInstrumentationField.setAccessible(true);
        Instrumentation mInstrumentation = (Instrumentation) mInstrumentationField.get(currentActivityThread);
        Instrumentation myInstrumentation = new InstrumentationProxy(mInstrumentation, mHandler);
        mInstrumentationField.set(currentActivityThread, myInstrumentation);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
開發者ID:zhangjianli,項目名稱:StallBuster,代碼行數:17,代碼來源:StallBuster.java

示例14: assertOutput

import android.app.Instrumentation; //導入依賴的package包/類
/**
 * Asserts that dump of this {@link FakeExtractorOutput} is equal to expected dump which is read
 * from {@code dumpFile}.
 *
 * <p>If assertion fails because of an intended change in the output or a new dump file needs to
 * be created, set {@link #WRITE_DUMP} flag to true and run the test again. Instead of assertion,
 * actual dump will be written to {@code dumpFile}. This new dump file needs to be copied to the
 * project, {@code library/src/androidTest/assets} folder manually.
 */
public void assertOutput(Instrumentation instrumentation, String dumpFile) throws IOException {
  String actual = new Dumper().add(this).toString();

  if (WRITE_DUMP) {
    File directory = instrumentation.getContext().getExternalFilesDir(null);
    File file = new File(directory, dumpFile);
    file.getParentFile().mkdirs();
    PrintWriter out = new PrintWriter(file);
    out.print(actual);
    out.close();
  } else {
    String expected = TestUtil.getString(instrumentation, dumpFile);
    Assert.assertEquals(dumpFile, expected, actual);
  }
}
 
開發者ID:ashwanijanghu,項目名稱:ExoPlayer-Offline,代碼行數:25,代碼來源:FakeExtractorOutput.java

示例15: waitInner

import android.app.Instrumentation; //導入依賴的package包/類
private static void waitInner(ReactBridgeIdleSignaler idleSignaler, long timeToWait) {
  // TODO gets broken in gradle, do we need it?
  Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
  long startTime = SystemClock.uptimeMillis();
  boolean bridgeWasIdle = false;
  while (SystemClock.uptimeMillis() - startTime < timeToWait) {
    boolean bridgeIsIdle = idleSignaler.isBridgeIdle();
    if (bridgeIsIdle && bridgeWasIdle) {
      return;
    }
    bridgeWasIdle = bridgeIsIdle;
    long newTimeToWait = Math.max(1, timeToWait - (SystemClock.uptimeMillis() - startTime));
    idleSignaler.waitForIdle(newTimeToWait);
    instrumentation.waitForIdleSync();
  }
  throw new RuntimeException("Timed out waiting for bridge and UI idle!");
}
 
開發者ID:qq565999484,項目名稱:RNLearn_Project1,代碼行數:18,代碼來源:ReactIdleDetectionUtil.java


注:本文中的android.app.Instrumentation類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。