Skip to content

Commit 0ebc57d

Browse files
Retry on Espresso Tests
1 parent 2030aef commit 0ebc57d

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

samples/containers/hello-back-button/src/androidTest/java/com/squareup/sample/hellobackbutton/HelloBackButtonEspressoTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
1212
import com.squareup.workflow1.ui.internal.test.IdlingDispatcherRule
1313
import com.squareup.workflow1.ui.internal.test.actuallyPressBack
1414
import com.squareup.workflow1.ui.internal.test.inAnyView
15+
import com.squareup.workflow1.ui.internal.test.retryBlocking
1516
import org.junit.Rule
1617
import org.junit.Test
1718
import org.junit.rules.RuleChain
@@ -26,7 +27,7 @@ class HelloBackButtonEspressoTest {
2627
.around(scenarioRule)
2728
.around(IdlingDispatcherRule)
2829

29-
@Test fun wrappedTakesPrecedence() {
30+
@Test fun wrappedTakesPrecedence() = retryBlocking {
3031
inAnyView(withId(R.id.hello_message)).apply {
3132
check(matches(withText("Able")))
3233
perform(click())
@@ -40,7 +41,7 @@ class HelloBackButtonEspressoTest {
4041
}
4142
}
4243

43-
@Test fun outerHandlerAppliesIfWrappedHandlerIsNull() {
44+
@Test fun outerHandlerAppliesIfWrappedHandlerIsNull() = retryBlocking {
4445
inAnyView(withId(R.id.hello_message)).apply {
4546
actuallyPressBack()
4647
inAnyView(withText("Are you sure you want to do this thing?"))

samples/tictactoe/app/src/androidTest/java/com/squareup/sample/TicTacToeEspressoTest.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
2323
import com.squareup.workflow1.ui.internal.test.IdlingDispatcherRule
2424
import com.squareup.workflow1.ui.internal.test.actuallyPressBack
2525
import com.squareup.workflow1.ui.internal.test.inAnyView
26+
import com.squareup.workflow1.ui.internal.test.retryBlocking
2627
import org.hamcrest.CoreMatchers.allOf
2728
import org.hamcrest.CoreMatchers.endsWith
2829
import org.junit.After
@@ -64,7 +65,7 @@ class TicTacToeEspressoTest {
6465
}
6566
}
6667

67-
@Test fun configChangeReflectsWorkflowState() {
68+
@Test fun configChangeReflectsWorkflowState() = retryBlocking {
6869
inAnyView(withId(R.id.login_email)).type("bad email")
6970
inAnyView(withId(R.id.login_button)).perform(click())
7071

@@ -73,7 +74,7 @@ class TicTacToeEspressoTest {
7374
inAnyView(withId(R.id.login_error_message)).check(matches(withText("Invalid address")))
7475
}
7576

76-
@Test fun editTextSurvivesConfigChange() {
77+
@Test fun editTextSurvivesConfigChange() = retryBlocking {
7778
inAnyView(withId(R.id.login_email)).type("foo@bar")
7879
inAnyView(withId(R.id.login_password)).type("password")
7980
rotate()
@@ -82,7 +83,7 @@ class TicTacToeEspressoTest {
8283
inAnyView(withId(R.id.login_password)).check(matches(withText("")))
8384
}
8485

85-
@Test fun backStackPopRestoresViewState() {
86+
@Test fun backStackPopRestoresViewState() = retryBlocking {
8687
// The loading screen is pushed onto the back stack.
8788
inAnyView(withId(R.id.login_email)).type("foo@bar")
8889
inAnyView(withId(R.id.login_password)).type("bad password")
@@ -95,7 +96,7 @@ class TicTacToeEspressoTest {
9596
.check(matches(withText("Unknown email or invalid password")))
9697
}
9798

98-
@Test fun dialogSurvivesConfigChange() {
99+
@Test fun dialogSurvivesConfigChange() = retryBlocking {
99100
inAnyView(withId(R.id.login_email)).type("foo@bar")
100101
inAnyView(withId(R.id.login_password)).type("password")
101102
inAnyView(withId(R.id.login_button)).perform(click())
@@ -112,7 +113,7 @@ class TicTacToeEspressoTest {
112113
.check(matches(isDisplayed()))
113114
}
114115

115-
@Test fun canGoBackFromAlert() {
116+
@Test fun canGoBackFromAlert() = retryBlocking {
116117
inAnyView(withId(R.id.login_email)).type("foo@bar")
117118
inAnyView(withId(R.id.login_password)).type("password")
118119
inAnyView(withId(R.id.login_button)).perform(click())
@@ -133,7 +134,7 @@ class TicTacToeEspressoTest {
133134
clickCell(0)
134135
}
135136

136-
@Test fun canGoBackInModalViewAndSeeRestoredViewState() {
137+
@Test fun canGoBackInModalViewAndSeeRestoredViewState() = retryBlocking {
137138
// Log in and hit the 2fa screen.
138139
inAnyView(withId(R.id.login_email)).type("foo@2fa")
139140
inAnyView(withId(R.id.login_password)).type("password")
@@ -146,7 +147,7 @@ class TicTacToeEspressoTest {
146147
inAnyView(withId(R.id.login_email)).check(matches(withText("foo@2fa")))
147148
}
148149

149-
@Test fun canGoBackInModalViewAfterConfigChangeAndSeeRestoredViewState() {
150+
@Test fun canGoBackInModalViewAfterConfigChangeAndSeeRestoredViewState() = retryBlocking {
150151
// Log in and hit the 2fa screen.
151152
inAnyView(withId(R.id.login_email)).type("foo@2fa")
152153
inAnyView(withId(R.id.login_password)).type("password")
@@ -164,7 +165,7 @@ class TicTacToeEspressoTest {
164165
* On tablets this revealed a problem with SavedStateRegistry.
165166
* https://github.com/square/workflow-kotlin/pull/656#issuecomment-1027274391
166167
*/
167-
@Test fun fullJourney() {
168+
@Test fun fullJourney() = retryBlocking {
168169
inAnyView(withId(R.id.login_email)).type("foo@bar")
169170
inAnyView(withId(R.id.login_password)).type("password")
170171
inAnyView(withId(R.id.login_button)).perform(click())

workflow-ui/internal-testing-android/api/internal-testing-android.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public final class com/squareup/workflow1/ui/internal/test/EspressoKt {
6161
public static final fun inAnyView (Lorg/hamcrest/Matcher;)Landroidx/test/espresso/ViewInteraction;
6262
public static final fun retry (Ljava/lang/String;JLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
6363
public static synthetic fun retry$default (Ljava/lang/String;JLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
64+
public static final fun retryBlocking (Ljava/lang/String;JLkotlin/jvm/functions/Function0;)V
65+
public static synthetic fun retryBlocking$default (Ljava/lang/String;JLkotlin/jvm/functions/Function0;ILjava/lang/Object;)V
6466
}
6567

6668
public final class com/squareup/workflow1/ui/internal/test/IdleAfterTestRule : org/junit/rules/TestRule {

workflow-ui/internal-testing-android/src/main/java/com/squareup/workflow1/ui/internal/test/Espresso.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import androidx.test.espresso.matcher.ViewMatchers.isRoot
1212
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
1313
import junit.framework.AssertionFailedError
1414
import kotlinx.coroutines.delay
15+
import kotlinx.coroutines.runBlocking
1516
import kotlinx.coroutines.withTimeoutOrNull
1617
import org.hamcrest.Description
1718
import org.hamcrest.Matcher
@@ -91,3 +92,13 @@ public suspend inline fun retry(
9192
throw it
9293
}
9394
}
95+
96+
public inline fun retryBlocking(
97+
clue: String = "",
98+
timeout_ms: Long = DEFAULT_RETRY_TIMEOUT,
99+
crossinline predicate: () -> Any
100+
) {
101+
runBlocking {
102+
retry(clue, timeout_ms, predicate)
103+
}
104+
}

0 commit comments

Comments
 (0)