Bug 1468048 - Add tests for GeckoSession.saveState() and restoreState() r=droeh,jchen a=lizzard
authorJames Willcox <snorp@snorp.net>
Fri, 15 Jun 2018 11:42:04 -0700
changeset 477943 2c0ec484c4eca7721900a7a0a5d8779f1d2bd46f
parent 477942 867449d771f06b4b49fdfe21fae948874d656efa
child 477944 0861f4656f09d9a44a95b8d7e9f431c4c2b3d63c
push id9474
push userarchaeopteryx@coole-files.de
push dateFri, 13 Jul 2018 21:13:56 +0000
treeherdermozilla-beta@8c9f4cc4061b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdroeh, jchen, lizzard
bugs1468048
milestone62.0
Bug 1468048 - Add tests for GeckoSession.saveState() and restoreState() r=droeh,jchen a=lizzard MozReview-Commit-ID: 7oIHXAQvGcq
mobile/android/geckoview/src/androidTest/assets/www/saveState.html
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/BaseSessionTest.kt
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ContentDelegateTest.kt
new file mode 100644
--- /dev/null
+++ b/mobile/android/geckoview/src/androidTest/assets/www/saveState.html
@@ -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>
\ No newline at end of file
--- 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
@@ -29,16 +29,17 @@ open class BaseSessionTest(noErrorCollec
         const val CONTENT_CRASH_URL = "about:crashcontent"
         const val DOWNLOAD_HTML_PATH = "/assets/www/download.html"
         const val HELLO_HTML_PATH = "/assets/www/hello.html"
         const val HELLO2_HTML_PATH = "/assets/www/hello2.html"
         const val INPUTS_PATH = "/assets/www/inputs.html"
         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"
     }
 
     @get:Rule val sessionRule = GeckoSessionTestRule()
 
     @get:Rule val errors = ErrorCollector()
 
@@ -59,18 +60,21 @@ open class BaseSessionTest(noErrorCollec
 
     fun getTestBytes(path: String) =
             InstrumentationRegistry.getTargetContext().resources.assets
                     .open(path.removePrefix("/assets/")).readBytes()
 
     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()
         try {
             this.writeToParcel(parcel, 0)
 
             val pos = parcel.dataPosition()
             parcel.setDataPosition(0)
@@ -79,16 +83,17 @@ open class BaseSessionTest(noErrorCollec
 
             assertThat("Read parcel matches written parcel",
                        parcel.dataPosition(), Matchers.equalTo(pos))
         } finally {
             parcel.recycle()
         }
     }
 
+
     fun GeckoSession.open() =
             sessionRule.openSession(this)
 
     fun GeckoSession.waitForPageStop() =
             sessionRule.waitForPageStop(this)
 
     fun GeckoSession.waitForPageStops(count: Int) =
             sessionRule.waitForPageStops(this, count)
--- 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
@@ -4,16 +4,18 @@
 
 package org.mozilla.geckoview.test
 
 import org.mozilla.geckoview.GeckoResponse
 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
 import android.support.test.runner.AndroidJUnit4
 import org.hamcrest.Matchers.*
 import org.junit.Assume.assumeThat
 import org.junit.Test
 import org.junit.runner.RunWith
@@ -114,9 +116,43 @@ class ContentDelegateTest : BaseSessionT
             sessionRule.waitUntilCalled(object : Callbacks.ContentDelegate {
                 @AssertCalled(count = 1)
                 override fun onCrash(session: GeckoSession) {
                     remainingSessions.remove(session)
                 }
             })
         }
     }
+
+    @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))
+    }
 }