Java Code Examples for android.app.Instrumentation#ActivityResult

The following examples show how to use android.app.Instrumentation#ActivityResult . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CameraHelperTest.java    From android-sdk with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example 2
Source File: MainActivityTest.java    From quickstart-android with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: HackInstrumentation.java    From Android-Plugin-Framework with MIT License 5 votes vote down vote up
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;
}
 
Example 4
Source File: HackInstrumentation.java    From Android-Plugin-Framework with MIT License 5 votes vote down vote up
@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;
}
 
Example 5
Source File: HackInstrumentation.java    From Android-Plugin-Framework with MIT License 5 votes vote down vote up
@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;
}
 
Example 6
Source File: HackInstrumentation.java    From Android-Plugin-Framework with MIT License 5 votes vote down vote up
@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;
}
 
Example 7
Source File: HackInstrumentation.java    From Android-Plugin-Framework with MIT License 5 votes vote down vote up
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;
}
 
Example 8
Source File: ReflectAccelerator.java    From Small with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 9
Source File: PermissiveTesting.java    From permissive with Apache License 2.0 5 votes vote down vote up
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]);
    }
  }
}
 
Example 10
Source File: HackInstrumentation.java    From Android-Plugin-Framework with MIT License 5 votes vote down vote up
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;
}
 
Example 11
Source File: GalleryHelperTest.java    From android-sdk with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 12
Source File: InstrumentationInternal.java    From Small with Apache License 2.0 4 votes vote down vote up
Instrumentation.ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode, android.os.Bundle options);
 
Example 13
Source File: InstrumentationInternal.java    From Small with Apache License 2.0 4 votes vote down vote up
Instrumentation.ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode);
 
Example 14
Source File: PermissiveTesting.java    From permissive with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 15
Source File: PermissiveTesting.java    From permissive with Apache License 2.0 4 votes vote down vote up
private static Instrumentation.ActivityResult createResult(String[] permissions, int grantPermission) {
  final int[] grants = new int[permissions.length];
  Arrays.fill(grants, grantPermission);
  return createResult(permissions, grants);
}
 
Example 16
Source File: PermissiveTesting.java    From permissive with Apache License 2.0 4 votes vote down vote up
private static Instrumentation.ActivityResult createResult(String permission, int grantPermission) {
  return createResult(new String[]{permission}, grantPermission);
}
 
Example 17
Source File: MainActivityTest.java    From otp-authenticator with MIT License 4 votes vote down vote up
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();
}
 
Example 18
Source File: MainActivityTest.java    From otp-authenticator with MIT License 4 votes vote down vote up
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)));
    }
}
 
Example 19
Source File: MainActivityTest.java    From otp-authenticator with MIT License 3 votes vote down vote up
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();
    }
 
Example 20
Source File: MainActivityTest.java    From otp-authenticator with MIT License 3 votes vote down vote up
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();
}