Skip to content

Commit 879b764

Browse files
authored
Merge pull request #381 from marcodb97/deprecations_intents_advanced_sample_test
Fixed deprecations in IntentsAdvancedSample tests
2 parents cb136a9 + 5295da9 commit 879b764

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

ui/espresso/IntentsAdvancedSample/app/src/androidTest/java/com/example/android/testing/espresso/intents/AdvancedSample/ImageViewHasDrawableMatcher.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,27 @@
1616

1717
package com.example.android.testing.espresso.intents.AdvancedSample;
1818

19-
import androidx.test.espresso.matcher.BoundedMatcher;
19+
import androidx.test.espresso.matcher.BoundedDiagnosingMatcher;
2020
import android.view.View;
2121
import android.widget.ImageView;
2222

2323
import org.hamcrest.Description;
24+
import org.hamcrest.Matcher;
2425

2526
/**
2627
* A Matcher for Espresso that checks if an ImageView has a drawable applied to it.
2728
*/
2829
public class ImageViewHasDrawableMatcher {
2930

30-
public static BoundedMatcher<View, ImageView> hasDrawable() {
31-
return new BoundedMatcher<View, ImageView>(ImageView.class) {
31+
public static Matcher<View> hasDrawable() {
32+
return new BoundedDiagnosingMatcher<View, ImageView>(ImageView.class) {
3233
@Override
33-
public void describeTo(Description description) {
34+
protected void describeMoreTo(Description description) {
3435
description.appendText("has drawable");
3536
}
3637

3738
@Override
38-
public boolean matchesSafely(ImageView imageView) {
39+
protected boolean matchesSafely(ImageView imageView, Description mismatchDescription) {
3940
return imageView.getDrawable() != null;
4041
}
4142
};

ui/espresso/IntentsAdvancedSample/app/src/androidTest/java/com/example/android/testing/espresso/intents/AdvancedSample/ImageViewerActivityTest.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121
import android.graphics.BitmapFactory;
2222
import android.os.Bundle;
2323
import android.provider.MediaStore;
24-
import androidx.test.espresso.intent.rule.IntentsTestRule;
24+
25+
import androidx.test.espresso.intent.Intents;
26+
import androidx.test.ext.junit.rules.ActivityScenarioRule;
2527
import androidx.test.ext.junit.runners.AndroidJUnit4;
2628
import androidx.test.filters.LargeTest;
29+
import androidx.test.platform.app.InstrumentationRegistry;
2730

31+
import org.junit.After;
2832
import org.junit.Before;
2933
import org.junit.Rule;
3034
import org.junit.Test;
@@ -48,27 +52,30 @@
4852
public class ImageViewerActivityTest {
4953

5054
/**
51-
* A JUnit {@link Rule @Rule} to init and release Espresso Intents before and after each
52-
* test run.
53-
* <p>
54-
* Rules are interceptors which are executed for each test method and will run before
55-
* any of your setup code in the {@link Before @Before} method.
56-
* <p>
57-
* This rule is based on {@link ActivityTestRule} and will create and launch of the activity
58-
* for you and also expose the activity under test.
55+
* Use {@link ActivityScenarioRule} to create and launch the activity under test, and close it
56+
* after test completes. This is a replacement for {@link androidx.test.rule.ActivityTestRule}.
5957
*/
6058
@Rule
61-
public IntentsTestRule<ImageViewerActivity> mIntentsRule = new IntentsTestRule<>(
59+
public ActivityScenarioRule<ImageViewerActivity> mActivityScenarioRule = new ActivityScenarioRule<>(
6260
ImageViewerActivity.class);
6361

6462
@Before
6563
public void stubCameraIntent() {
64+
// Initializes Intents and begins recording intents.
65+
Intents.init();
66+
6667
ActivityResult result = createImageCaptureActivityResultStub();
6768

6869
// Stub the Intent.
6970
intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result);
7071
}
7172

73+
@After
74+
public void tearDown() {
75+
// Clears Intents state.
76+
Intents.release();
77+
}
78+
7279
@Test
7380
public void takePhoto_drawableIsApplied() {
7481
// Check that the ImageView doesn't have a drawable applied.
@@ -85,7 +92,8 @@ private ActivityResult createImageCaptureActivityResultStub() {
8592
// Put the drawable in a bundle.
8693
Bundle bundle = new Bundle();
8794
bundle.putParcelable(ImageViewerActivity.KEY_IMAGE_DATA, BitmapFactory.decodeResource(
88-
mIntentsRule.getActivity().getResources(), R.drawable.ic_launcher));
95+
InstrumentationRegistry.getInstrumentation().getTargetContext().getResources(),
96+
R.drawable.ic_launcher));
8997

9098
// Create the Intent that will include the bundle.
9199
Intent resultData = new Intent();

0 commit comments

Comments
 (0)