Bug 1468048 - Add tests for GeckoSession.saveState() and restoreState() r=droeh,jchen

MozReview-Commit-ID: 7oIHXAQvGcq
This commit is contained in:
James Willcox 2018-06-15 11:42:04 -07:00
Родитель aad0d2a7f6
Коммит d04a9bd093
3 изменённых файлов: 59 добавлений и 1 удалений

Просмотреть файл

@ -0,0 +1,17 @@
<html>
<head>
<title>Hello, world!</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
p {
height: 300vh;
}
</style>
</head>
<body>
<form id="form">
<input type="text" id="name">
</form>
<p>Hello, world!</p>
</body>
</html>

Просмотреть файл

@ -34,6 +34,7 @@ open class BaseSessionTest(noErrorCollector: Boolean = false) {
const val INVALID_URI = "http://www.test.invalid/"
const val NEW_SESSION_HTML_PATH = "/assets/www/newSession.html"
const val NEW_SESSION_CHILD_HTML_PATH = "/assets/www/newSession_child.html"
const val SAVE_STATE_PATH = "/assets/www/saveState.html"
const val TITLE_CHANGE_HTML_PATH = "/assets/www/titleChange.html"
const val TRACKERS_PATH = "/assets/www/trackers.html"
}
@ -64,8 +65,11 @@ open class BaseSessionTest(noErrorCollector: Boolean = false) {
val GeckoSession.isRemote
get() = this.settings.getBoolean(GeckoSessionSettings.USE_MULTIPROCESS)
fun createTestUrl(path: String) =
GeckoSessionTestRule.APK_URI_PREFIX + path.removePrefix("/")
fun GeckoSession.loadTestPath(path: String) =
this.loadUri(GeckoSessionTestRule.APK_URI_PREFIX + path.removePrefix("/"))
this.loadUri(createTestUrl(path))
inline fun GeckoSession.toParcel(lambda: (Parcel) -> Unit) {
val parcel = Parcel.obtain()
@ -84,6 +88,7 @@ open class BaseSessionTest(noErrorCollector: Boolean = false) {
}
}
fun GeckoSession.open() =
sessionRule.openSession(this)

Просмотреть файл

@ -9,6 +9,8 @@ import org.mozilla.geckoview.GeckoSession
import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.AssertCalled
import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.IgnoreCrash
import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.ReuseSession
import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.WithDevToolsAPI
import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.WithDisplay
import org.mozilla.geckoview.test.util.Callbacks
import android.support.test.filters.MediumTest
@ -119,4 +121,38 @@ class ContentDelegateTest : BaseSessionTest() {
})
}
}
@WithDevToolsAPI
@WithDisplay(width = 400, height = 400)
@Test fun saveAndRestoreState() {
val startUri = createTestUrl(SAVE_STATE_PATH)
mainSession.loadUri(startUri)
sessionRule.waitForPageStop()
mainSession.evaluateJS("$('#name').value = 'the name'; window.scrollBy(0, 100);")
val state = sessionRule.waitForResult(mainSession.saveState())
assertThat("State should not be null", state, notNullValue())
mainSession.loadUri("about:blank")
sessionRule.waitForPageStop()
mainSession.restoreState(state)
sessionRule.waitForPageStop()
sessionRule.forCallbacksDuringWait(object : Callbacks.NavigationDelegate {
@AssertCalled
override fun onLocationChange(session: GeckoSession, url: String) {
assertThat("URI should match", url, equalTo(startUri))
}
})
assertThat("'name' field should match",
mainSession.evaluateJS("$('#name').value").toString(),
equalTo("the name"))
assertThat("Scroll position should match",
mainSession.evaluateJS("window.scrollY") as Double,
closeTo(100.0, .5))
}
}