Bug 1452326 - Add BaseSessionTest.forEachCall; r?esawin
Given a list of values, use forEachCall to easily match to the value
that corresponds to the current call.
MozReview-Commit-ID: 11Cd0zXcxjj
--- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/BaseSessionTest.kt
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/BaseSessionTest.kt
@@ -39,16 +39,18 @@ open class BaseSessionTest(noErrorCollec
fun <T> assertThat(reason: String, v: T, m: Matcher<T>) = sessionRule.assertThat(reason, v, m)
init {
if (!noErrorCollector) {
sessionRule.errorCollector = errors
}
}
+ fun <T> forEachCall(vararg values: T): T = sessionRule.forEachCall(*values)
+
fun GeckoSession.getTestBytes(path: String) =
InstrumentationRegistry.getTargetContext().resources.assets
.open(path.removePrefix("/assets/")).readBytes()
fun GeckoSession.loadTestPath(path: String) =
this.loadUri(GeckoSessionTestRule.APK_URI_PREFIX + path.removePrefix("/"))
inline fun GeckoSession.toParcel(lambda: (Parcel) -> Unit) {
--- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ContentDelegateTest.kt
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ContentDelegateTest.kt
@@ -17,22 +17,21 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
@MediumTest
class ContentDelegateTest : BaseSessionTest() {
@Test fun titleChange() {
sessionRule.session.loadTestPath(TITLE_CHANGE_HTML_PATH)
- val titles = mutableListOf("Title1", "Title2")
sessionRule.waitUntilCalled(object : Callbacks.ContentDelegate {
@AssertCalled(count = 2)
override fun onTitleChange(session: GeckoSession, title: String) {
assertThat("Title should match", title,
- equalTo(titles.removeAt(0)))
+ equalTo(forEachCall("Title1", "Title2")))
}
})
}
@Test fun download() {
sessionRule.session.loadTestPath(DOWNLOAD_HTML_PATH)
sessionRule.waitUntilCalled(object : Callbacks.NavigationDelegate, Callbacks.ContentDelegate {
--- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/GeckoSessionTestRuleTest.kt
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/GeckoSessionTestRuleTest.kt
@@ -240,17 +240,17 @@ class GeckoSessionTestRuleTest : BaseSes
var counter = 0
sessionRule.waitUntilCalled(object : Callbacks.ProgressDelegate {
@AssertCalled(count = 2, order = intArrayOf(1, 2))
override fun onPageStop(session: GeckoSession, success: Boolean) {
val info = sessionRule.currentCall
assertThat("Method info should be valid", info, notNullValue())
assertThat("Counter should be correct",
- info.counter, isOneOf(1, 2))
+ info.counter, equalTo(forEachCall(1, 2)))
assertThat("Order should equal counter",
info.order, equalTo(info.counter))
counter++
}
})
assertThat("Callback count should be correct", counter, equalTo(2))
}
--- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ProgressDelegateTest.kt
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ProgressDelegateTest.kt
@@ -88,26 +88,25 @@ class ProgressDelegateTest : BaseSession
sessionRule.session.loadUri(INVALID_URI)
sessionRule.session.loadTestPath(HELLO_HTML_PATH)
sessionRule.waitForPageStops(2)
sessionRule.forCallbacksDuringWait(object : Callbacks.ProgressDelegate {
@AssertCalled(count = 2, order = intArrayOf(1, 3))
override fun onPageStart(session: GeckoSession, url: String) {
assertThat("URL should match", url,
- endsWith(if (sessionRule.currentCall.counter == 1)
- INVALID_URI else HELLO_HTML_PATH))
+ endsWith(forEachCall(INVALID_URI, HELLO_HTML_PATH)))
}
@AssertCalled(count = 2, order = intArrayOf(2, 4))
override fun onPageStop(session: GeckoSession, success: Boolean) {
// The first load is certain to fail because of interruption by the second load
// or by invalid domain name, whereas the second load is certain to succeed.
assertThat("Success flag should match", success,
- equalTo(sessionRule.currentCall.counter != 1))
+ equalTo(forEachCall(false, true)))
};
})
}
@Test fun reload() {
sessionRule.session.loadTestPath(HELLO_HTML_PATH)
sessionRule.waitForPageStop()
--- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/rule/GeckoSessionTestRule.java
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/rule/GeckoSessionTestRule.java
@@ -1243,18 +1243,19 @@ public class GeckoSessionTestRule extend
}
assertThat("Should have called one of " +
Arrays.toString(callback.getClass().getInterfaces()),
calledAny, equalTo(true));
}
/**
- * Get information about the current call. Only valid during a {@link #forCallbacksDuringWait}
- * callback.
+ * Get information about the current call. Only valid during a {@link
+ * #forCallbacksDuringWait}, {@link #delegateDuringNextWait}, or {@link
+ * #delegateUntilTestEnd} callback.
*
* @return Call information
*/
public @NonNull CallInfo getCurrentCall() {
assertThat("Should be in a method call", mCurrentMethodCall, notNullValue());
return mCurrentMethodCall.getInfo();
}
@@ -1388,9 +1389,27 @@ public class GeckoSessionTestRule extend
* Create a new, closed session using the specified settings.
*
* @param settings Settings for the new session.
* @return New session.
*/
public GeckoSession createClosedSession(final GeckoSessionSettings settings) {
return createSession(settings, /* open */ false);
}
+
+ /**
+ * Return a value from the given array indexed by the current call counter. Only valid
+ * during a {@link #forCallbacksDuringWait}, {@link #delegateDuringNextWait}, or
+ * {@link #delegateUntilTestEnd} callback.
+ * <p><p>
+ * Asserts that {@code foo} is equal to {@code "bar"} during the first call and {@code
+ * "baz"} during the second call:
+ * <pre>{@code assertThat("Foo should match", foo, equalTo(forEachCall("bar",
+ * "baz")));}</pre>
+ *
+ * @param values Input array
+ * @return Value from input array indexed by the current call counter.
+ */
+ public <T> T forEachCall(T... values) {
+ assertThat("Should be in a method call", mCurrentMethodCall, notNullValue());
+ return values[Math.min(mCurrentMethodCall.getCurrentCount(), values.length) - 1];
+ }
}