content: /*
   * Copyright 2015, The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.example.android.testing.espresso.intents.AdvancedSample;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.graphics.BitmapFactory;
  import android.os.Bundle;
  import android.provider.MediaStore;
  
  import androidx.test.espresso.intent.Intents;
  import androidx.test.ext.junit.rules.ActivityScenarioRule;
  import androidx.test.ext.junit.runners.AndroidJUnit4;
  import androidx.test.filters.LargeTest;
  import androidx.test.platform.app.InstrumentationRegistry;
  
  import org.junit.After;
  import org.junit.Before;
  import org.junit.Rule;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  
  import static android.app.Instrumentation.ActivityResult;
  import static androidx.test.espresso.Espresso.onView;
  import static androidx.test.espresso.action.ViewActions.click;
  import static androidx.test.espresso.assertion.ViewAssertions.matches;
  import static androidx.test.espresso.intent.Intents.intending;
  import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction;
  import static androidx.test.espresso.matcher.ViewMatchers.withId;
  import static com.example.android.testing.espresso.intents.AdvancedSample.ImageViewHasDrawableMatcher.hasDrawable;
  import static org.hamcrest.Matchers.not;
  
  /**
   * Espresso tests for {@link ImageViewerActivity}.
   */
  @RunWith(AndroidJUnit4.class)
  @LargeTest
  public class ImageViewerActivityTest {
  
      /**
       * Use {@link ActivityScenarioRule} to create and launch the activity under test, and close it
       * after test completes. This is a replacement for {@link androidx.test.rule.ActivityTestRule}.
       */
      @Rule
      public ActivityScenarioRule<ImageViewerActivity> mActivityScenarioRule = new ActivityScenarioRule<>(
              ImageViewerActivity.class);
  
      @Before
      public void stubCameraIntent() {
          // Initializes Intents and begins recording intents.
          Intents.init();
  
          ActivityResult result = createImageCaptureActivityResultStub();
  
          // Stub the Intent.
          intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result);
      }
  
      @After
      public void tearDown() {
          // Clears Intents state.
          Intents.release();
      }
  
      @Test
      public void takePhoto_drawableIsApplied() {
          // Check that the ImageView doesn't have a drawable applied.
          onView(withId(R.id.imageView)).check(matches(not(hasDrawable())));
  
          // Click on the button that will trigger the stubbed intent.
          onView(withId(R.id.button_take_photo)).perform(click());
  
          // With no user interaction, the ImageView will have a drawable.
          onView(withId(R.id.imageView)).check(matches(hasDrawable()));
      }
  
      private ActivityResult createImageCaptureActivityResultStub() {
          // Put the drawable in a bundle.
          Bundle bundle = new Bundle();
          bundle.putParcelable(ImageViewerActivity.KEY_IMAGE_DATA, BitmapFactory.decodeResource(
                  InstrumentationRegistry.getInstrumentation().getTargetContext().getResources(),
                  R.drawable.ic_launcher));
  
          // Create the Intent that will include the bundle.
          Intent resultData = new Intent();
          resultData.putExtras(bundle);
  
          // Create the ActivityResult with the Intent.
          return new ActivityResult(Activity.RESULT_OK, resultData);
      }
  }