android-instrumentation

How to write Android instrumented UI test for native Camera activity


I have a MainActivity with a button that will launch the native Camera Activity via Intent(MediaStore.ACTION_IMAGE_CAPTURE). I would like to write an Android UI test that will:

  1. Confirm we are on the MainActivity
  2. Tap the button to launch the native Camera Activity
  3. Press the shutter button that is in it
  4. Confirm we are back to the MainActivity

The problem is, on step 3., how do I press the default shutter button when it doesn't have any labels or texts? Does anyone know how to navigate this scenario?


Solution

  • how do I press the default shutter button when it doesn't have any labels or texts? Does anyone know how to navigate this scenario?

    You probably shouldn't.

    Once you're out of your app you can't make any assumptions about what app (if any) will be launched in response to your request to take a picture.

    Instead, use the Espresso Intents API to very that your intent request gets fired as expected, then mock a response of what you expect the camera to return to you.

    This keeps your test independent of whatever version of whatever camera app of whatever device you happen to write the test against.

    If you're set on actually interacting with whatever version of whatever camera app happens to be the default on whatever device you happen to be testing on at the time you write your test, you could use UiAutomator.