本文整理汇总了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;
}
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}
}
示例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;
}
}
示例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();
}
示例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();
}
示例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;
}
示例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);
}
示例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()));
}
示例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();
}
}
示例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);
}
}
示例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!");
}