TWiStErRob / net.twisterrob.libraries

1 stars 0 forks source link

Add more coverage to DialogMatchers #4

Closed TWiStErRob closed 1 year ago

TWiStErRob commented 1 year ago

Don't remember what, but found this patch that wasn't committed yet:

Index: twister-lib-android/espresso/src/androidTest/java/net/twisterrob/android/test/espresso/DialogMatchersTest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/twister-lib-android/espresso/src/androidTest/java/net/twisterrob/android/test/espresso/DialogMatchersTest.java b/twister-lib-android/espresso/src/androidTest/java/net/twisterrob/android/test/espresso/DialogMatchersTest.java
--- a/twister-lib-android/espresso/src/androidTest/java/net/twisterrob/android/test/espresso/DialogMatchersTest.java    (revision b6572c6765c1bdad43e64514fd5dbc59328a7d9e)
+++ b/twister-lib-android/espresso/src/androidTest/java/net/twisterrob/android/test/espresso/DialogMatchersTest.java    (date 1677266931543)
@@ -15,7 +15,10 @@
 import android.content.Context;
 import android.widget.Toast;

+import androidx.test.espresso.Espresso;
 import androidx.test.espresso.base.RootViewPicker;
+import androidx.test.platform.app.InstrumentationRegistry;
+import androidx.test.runner.lifecycle.Stage;
 import junitparams.*;
 import junitparams.naming.TestCaseName;

@@ -25,6 +28,9 @@
 import net.twisterrob.inventory.android.test.activity.TestActivityCompat;

 import static net.twisterrob.android.test.espresso.DialogMatchers.*;
+import static net.twisterrob.android.test.espresso.EspressoExtensions.loopMainThreadUntilIdle;
+import static net.twisterrob.android.test.espresso.EspressoExtensions.onRoot;
+import static net.twisterrob.android.test.junit.InstrumentationExtensions.getAllActivitiesByStage;
 import static net.twisterrob.test.hamcrest.Matchers.*;
 import static net.twisterrob.test.junit.Assert.*;

@@ -174,11 +180,16 @@
        }
    }

-   private void displayAppCompatAlertDialog(final boolean positive, final boolean negative, final boolean neutral,
-           final boolean cancellable) {
+   private void displayAppCompatAlertDialog(
+           final Context context,
+           final boolean positive,
+           final boolean negative,
+           final boolean neutral,
+           final boolean cancellable
+   ) {
        InstrumentationExtensions.runOnMainIfNecessary(new Runnable() {
            @Override public void run() {
-               showAppCompatAlertDialog(activity.getActivity(), positive, negative, neutral, cancellable);
+               showAppCompatAlertDialog(context, positive, negative, neutral, cancellable);
            }
        });
    }
@@ -187,7 +198,22 @@
    @Test(timeout = DIALOG_TIMEOUT)
    public void testDialogIsDisplayedForAppCompatAlert(
            final boolean positive, final boolean negative, final boolean neutral, final boolean cancellable) {
-       displayAppCompatAlertDialog(positive, negative, neutral, cancellable);
+       Espresso.onIdle();
+       onRoot().perform(loopMainThreadUntilIdle());
+       assertThat(getAllActivitiesByStage().keySet(), containsInAnyOrder(Stage.RESUMED));
+       activity.getActivity().finish();
+       assertThat(getAllActivitiesByStage().keySet(), containsInAnyOrder(Stage.DESTROYED));
+       Context context = getInstrumentation().getContext();
+       displayAppCompatAlertDialog(context, positive, negative, neutral, cancellable);
+
+       assertDialogIsDisplayed_withTimeout();
+   }
+   @Parameters(named = POTENTIAL_DIALOGS)
+   @TestCaseName(POTENTIAL_DIALOGS_NAME)
+   @Test(timeout = DIALOG_TIMEOUT)
+   public void testDialogIsDisplayedWithoutActivityForAppCompatAlert(
+           final boolean positive, final boolean negative, final boolean neutral, final boolean cancellable) {
+       displayAppCompatAlertDialog(activity.getActivity(), positive, negative, neutral, cancellable);

        assertDialogIsDisplayed_withTimeout();
    }
@@ -196,7 +222,7 @@
    @Test(timeout = DIALOG_TIMEOUT)
    public void testDialogIsDisplayedForAppCompatAlert_fail(
            final boolean positive, final boolean negative, final boolean neutral, final boolean cancellable) {
-       displayAppCompatAlertDialog(positive, negative, neutral, cancellable);
+       displayAppCompatAlertDialog(activity.getActivity(), positive, negative, neutral, cancellable);

        Throwable expectedFailure = assertThrows(AssertionError.class, new ThrowingRunnable() {
            @Override public void run() {
TWiStErRob commented 1 year ago

Revised code: an added test

@Parameters(named = POTENTIAL_DIALOGS)
@TestCaseName(POTENTIAL_DIALOGS_NAME)
@Test(timeout = DIALOG_TIMEOUT)
public void testDialogIsDisplayedWithoutActivityForAppCompatAlert(
        final boolean positive, final boolean negative, final boolean neutral, final boolean cancellable) {
    Espresso.onIdle();
    onRoot().perform(loopMainThreadUntilIdle());
    assertThat(getAllActivitiesByStage().get(Stage.RESUMED), not(Matchers.<Activity>empty()));
    activity.getActivity().finish();
    Espresso.onIdle();
    assertThat(getAllActivitiesByStage().get(Stage.DESTROYED), not(Matchers.<Activity>empty()));
    Context context = getInstrumentation().getContext();
    androidx.appcompat.app.AlertDialog dialog = displayAppCompatAlertDialog(
            context, positive, negative, neutral, cancellable
    );

    try {
        assertDialogIsDisplayed_withTimeout();
    } finally {
        dialog.dismiss();
    }
}

but this is not viable: this throws an exception wanting an AppCompat activity, and the built-in version throws because the app context doesn't have a window token.