下面列出了android.app.Instrumentation#ActivityResult ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Create a file to be selected by tests.
*/
private void createTempFile() {
// Create fake RESULT_OK Intent
Intent intent = new Intent();
intent.putExtra("is-espresso-test", true);
// Create a temporary file for the result of the intent
File external = mActivityTestRule.getActivity().getExternalFilesDir(null);
File imageFile = new File(external, "tmp.jpg");
try {
imageFile.createNewFile();
} catch (IOException e) {
Log.e(TAG, "createNewFile", e);
}
intent.setData(Uri.fromFile(imageFile));
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(
Activity.RESULT_OK, intent);
// Intercept photo intent
Matcher<Intent> pictureIntentMatch = allOf(hasAction(Intent.ACTION_GET_CONTENT));
intending(pictureIntentMatch).respondWith(result);
}
@Test
public void testCameraFlow() {
Bitmap icon = BitmapFactory.decodeResource(
InstrumentationRegistry.getTargetContext().getResources(),
R.mipmap.ic_launcher);
Intent resultData = new Intent();
resultData.putExtra("data", icon);
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);
intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result);
Espresso.onView(withId(R.id.camera_button)).perform(click());
intended(hasAction(MediaStore.ACTION_IMAGE_CAPTURE));
}
@Before
public void setupImageUri() {
Resources resources = InstrumentationRegistry.getTargetContext().getResources();
Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
resources.getResourcePackageName(R.mipmap.ic_launcher) + '/' +
resources.getResourceTypeName(R.mipmap.ic_launcher) + '/' +
resources.getResourceEntryName(R.mipmap.ic_launcher));
Intent resultData = new Intent();
resultData.setData(imageUri);
result = new Instrumentation.ActivityResult(
Activity.RESULT_OK, resultData);
}
public Instrumentation.ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode) {
Object result = RefInvoker.invokeMethod(instance, Instrumentation.class.getName(),
Method_execStartActivity, new Class[] { Context.class, IBinder.class, IBinder.class, Activity.class,
Intent.class, int.class }, new Object[] { who, contextThread,
token, target, intent, requestCode });
return (Instrumentation.ActivityResult) result;
}
private void applyActivityResult(Instrumentation.ActivityResult result) {
String[] permissions = result.getResultData().getStringArrayExtra(EXTRA_REQUEST_PERMISSIONS_NAMES);
int[] resultPermissions = result.getResultData().getIntArrayExtra(EXTRA_REQUEST_PERMISSIONS_RESULTS);
for (int i = 0; i < permissions.length; ++i) {
if (resultPermissions[i] == PackageManager.PERMISSION_GRANTED) {
grantFakePermission(permissions[i]);
} else {
revokeFakePermission(permissions[i]);
}
}
}
public Instrumentation.ActivityResult execStartActivityAsCaller(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode, Bundle options, boolean ignoreTargetSecurity,
int userId) {
Object result = RefInvoker.invokeMethod(instance, Instrumentation.class.getName(),
Method_execStartActivityAsCaller, new Class[] { Context.class, IBinder.class, IBinder.class, Activity.class,
Intent.class, int.class, Bundle.class, boolean.class, int.class}, new Object[] { who, contextThread,
token, target, intent, requestCode, options, ignoreTargetSecurity, userId});
return (Instrumentation.ActivityResult)result;
}
public static Instrumentation.ActivityResult execStartActivity(
Instrumentation instrumentation, Context who, IBinder contextThread, IBinder token,
Activity target, Intent intent, int requestCode, Bundle options) {
if (sInstrumentation_execStartActivityV21_method == null) {
Class[] types = new Class[] {Context.class, IBinder.class, IBinder.class,
Activity.class, Intent.class, int.class, android.os.Bundle.class};
sInstrumentation_execStartActivityV21_method = getMethod(Instrumentation.class,
"execStartActivity", types);
}
if (sInstrumentation_execStartActivityV21_method == null) return null;
return invoke(sInstrumentation_execStartActivityV21_method, instrumentation,
who, contextThread, token, target, intent, requestCode, options);
}
public Instrumentation.ActivityResult execStartActivityAsCaller(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode, Bundle options, int userId) {
Object result = RefInvoker.invokeMethod(instance, Instrumentation.class.getName(),
Method_execStartActivityAsCaller, new Class[] { Context.class, IBinder.class, IBinder.class, Activity.class,
Intent.class, int.class, Bundle.class, int.class}, new Object[] { who, contextThread,
token, target, intent, requestCode, options, userId});
return (Instrumentation.ActivityResult)result;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public Instrumentation.ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Fragment target,
Intent intent, int requestCode) {
Object result = RefInvoker.invokeMethod(instance, Instrumentation.class.getName(),
Method_execStartActivity, new Class[] { Context.class, IBinder.class, IBinder.class, Fragment.class,
Intent.class, int.class }, new Object[] { who, contextThread,
token, target, intent, requestCode });
return (Instrumentation.ActivityResult) result;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public Instrumentation.ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode, Bundle options, UserHandle user) {
Object result = RefInvoker.invokeMethod(instance, Instrumentation.class.getName(),
Method_execStartActivity, new Class[] { Context.class, IBinder.class, IBinder.class, Activity.class,
Intent.class, int.class, Bundle.class, UserHandle.class }, new Object[] { who, contextThread,
token, target, intent, requestCode, options, user });
return (Instrumentation.ActivityResult) result;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public Instrumentation.ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token,
Fragment target, Intent intent, int requestCode, Bundle options) {
Object result = RefInvoker.invokeMethod(instance, Instrumentation.class.getName(),
Method_execStartActivity , new Class[] { Context.class, IBinder.class, IBinder.class,
Fragment.class, Intent.class, int.class, Bundle.class }, new Object[] { who,
contextThread, token, target, intent, requestCode, options });
return (Instrumentation.ActivityResult) result;
}
private static Instrumentation.ActivityResult createResult(String permission, int grantPermission) {
return createResult(new String[]{permission}, grantPermission);
}
private static Instrumentation.ActivityResult createResult(String[] permissions, int grantPermission) {
final int[] grants = new int[permissions.length];
Arrays.fill(grants, grantPermission);
return createResult(permissions, grants);
}
private static Instrumentation.ActivityResult createResult(String[] permissions, int[] grantPermissions) {
Intent data = new Intent();
data.putExtra(EXTRA_REQUEST_PERMISSIONS_NAMES, permissions);
data.putExtra(EXTRA_REQUEST_PERMISSIONS_RESULTS, grantPermissions);
return new Instrumentation.ActivityResult(Activity.RESULT_OK, data);
}
Instrumentation.ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode, android.os.Bundle options);
Instrumentation.ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode);
public void test002NocodeScanned() throws InterruptedException {
Intents.init();
// Build a result to return from the ZXING app
Intent resultData = new Intent();
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_CANCELED, 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(hasAction("com.google.zxing.client.android.SCAN")).respondWith(result);
// Now that we have the stub in place, click on the button in our app that launches into the Camera
onView(withId(R.id.action_scan)).perform(click());
// We can also validate that an intent resolving to the "camera" activity has been sent out by our app
intended(hasAction("com.google.zxing.client.android.SCAN"));
onView(withText("No account has been added yet")).check(matches(isDisplayed()));
Intents.release();
}
public void test003AddCodes() throws InterruptedException {
for(String[] code: codes){
Intents.init();
String qr = "otpauth://totp/"+code[0] +"?secret="+new String(new Base32().encode(code[1].getBytes())) ;
// Build a result to return from the ZXING app
Intent resultData = new Intent();
resultData.putExtra(com.google.zxing.client.android.Intents.Scan.RESULT, qr);
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(hasAction("com.google.zxing.client.android.SCAN")).respondWith(result);
// Now that we have the stub in place, click on the button in our app that launches into the Camera
onView(withId(R.id.action_scan)).perform(click());
// We can also validate that an intent resolving to the "camera" activity has been sent out by our app
intended(hasAction("com.google.zxing.client.android.SCAN"));
onView(withText("Account added")).check(matches(isDisplayed()));
Intents.release();
}
Thread.sleep(500);
for(int i = 0; i < codes.length; i++){
onData(anything()).inAdapterView(withId(R.id.listView))
.atPosition(i)
.onChildView(withId(R.id.textViewLabel))
.check(matches(withText(codes[i][0])));
String otp = TOTPHelper.generate(codes[i][1].getBytes());
onData(anything()).inAdapterView(withId(R.id.listView))
.atPosition(i)
.onChildView(withId(R.id.textViewOTP))
.check(matches(withText(otp)));
}
}
public void test000EmptyStart() throws InterruptedException {
onView(withText("No account has been added yet")).check(matches(isDisplayed()));
onView(withText("Add")).check(matches(isDisplayed()));
Intents.init();
String qr = "XXX" ;
// Build a result to return from the ZXING app
Intent resultData = new Intent();
resultData.putExtra(com.google.zxing.client.android.Intents.Scan.RESULT, qr);
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(hasAction("com.google.zxing.client.android.SCAN")).respondWith(result);
onView(withText("Add")).check(matches(isDisplayed()));
onView(withText("Add")).perform(click());
// We can also validate that an intent resolving to the "camera" activity has been sent out by our app
intended(hasAction("com.google.zxing.client.android.SCAN"));
Intents.release();
}
public void test001InvalidQRCode() throws InterruptedException {
Intents.init();
String qr ="invalid qr code";
// Build a result to return from the ZXING app
Intent resultData = new Intent();
resultData.putExtra(com.google.zxing.client.android.Intents.Scan.RESULT, qr);
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(hasAction("com.google.zxing.client.android.SCAN")).respondWith(result);
// Now that we have the stub in place, click on the button in our app that launches into the Camera
onView(withId(R.id.action_scan)).perform(click());
// We can also validate that an intent resolving to the "camera" activity has been sent out by our app
intended(hasAction("com.google.zxing.client.android.SCAN"));
onView(withText("Invalid QR Code")).check(matches(isDisplayed()));
Thread.sleep(5000);
onView(withText("No account has been added yet")).check(matches(isDisplayed()));
Intents.release();
}