Bug 1456190 - 3. Add tests for evaluateJS(); r=snorp
authorJim Chen <nchen@mozilla.com>
Fri, 27 Apr 2018 11:57:14 -0400
changeset 472184 3e1004a7729fbd5af2d4ba3cf9c2bedbdb97768f
parent 472183 6bac8d381551e1349e1d534f76e526d5ead75309
child 472185 298e7cf3cef66c869adf2eaba9700fe197638e6f
push id1728
push userjlund@mozilla.com
push dateMon, 18 Jun 2018 21:12:27 +0000
treeherdermozilla-release@c296fde26f5f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1456190
milestone61.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1456190 - 3. Add tests for evaluateJS(); r=snorp Add some tests in GeckoSessionTestRuleTest for evaluateJS(), including checking the results from a variety of expressions. MozReview-Commit-ID: 9KWIhTogSCg
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/BaseSessionTest.kt
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/GeckoSessionTestRuleTest.kt
--- 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
@@ -107,9 +107,9 @@ open class BaseSessionTest(noErrorCollec
     infix fun Any?.dot(prop: Any): Any? =
             if (prop is Int) this.asJSList<Any>()[prop] else this.asJSMap<Any>()[prop]
 
     @Suppress("UNCHECKED_CAST")
     fun <T> Any?.asJSMap(): Map<String, T> = this as Map<String, T>
 
     @Suppress("UNCHECKED_CAST")
     fun <T> Any?.asJSList(): List<T> = this as List<T>
-}
\ No newline at end of file
+}
--- 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
@@ -7,16 +7,17 @@ package org.mozilla.geckoview.test
 import org.mozilla.geckoview.GeckoSession
 import org.mozilla.geckoview.GeckoSessionSettings
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.AssertCalled
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.ClosedSessionAtStart
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.NullDelegate
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.Setting
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.TimeoutException
 import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.TimeoutMillis
+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
@@ -1170,9 +1171,101 @@ class GeckoSessionTestRuleTest : BaseSes
         assumeThat(sessionRule.env.isMultiprocess, equalTo(false))
 
         sessionRule.session.loadTestPath(CLICK_TO_RELOAD_HTML_PATH)
         sessionRule.session.waitForPageStop()
 
         sessionRule.session.synthesizeTap(5, 5)
         sessionRule.session.waitForPageStop()
     }
+
+    @WithDevToolsAPI
+    @Test fun evaluateJS() {
+        assertThat("JS string result should be correct",
+                   sessionRule.session.evaluateJS("'foo'") as String, equalTo("foo"))
+
+        assertThat("JS number result should be correct",
+                   sessionRule.session.evaluateJS("1+1") as Double, equalTo(2.0))
+
+        assertThat("JS boolean result should be correct",
+                   sessionRule.session.evaluateJS("!0") as Boolean, equalTo(true))
+
+        assertThat("JS object result should be correct",
+                   sessionRule.session.evaluateJS("({foo:'bar',bar:42,baz:true})").asJSMap(),
+                   equalTo(mapOf("foo" to "bar", "bar" to 42.0, "baz" to true)))
+
+        assertThat("JS array result should be correct",
+                   sessionRule.session.evaluateJS("[1,2,3]").asJSList(),
+                   equalTo(listOf(1.0, 2.0, 3.0)))
+
+        assertThat("JS DOM object result should be correct",
+                   sessionRule.session.evaluateJS("document.body").asJSMap(),
+                   hasEntry("tagName", "BODY"))
+
+        assertThat("JS DOM array result should be correct",
+                   sessionRule.session.evaluateJS("document.childNodes").asJSList<Any>(),
+                   not(empty()))
+    }
+
+    @WithDevToolsAPI
+    @Test fun evaluateJS_windowObject() {
+        sessionRule.session.loadTestPath(HELLO_HTML_PATH)
+        sessionRule.session.waitForPageStop()
+
+        // Make sure we can access large objects like "window", which can strain our RDP connection.
+        // Also make sure we can dynamically access sub-objects like Location.
+        assertThat("JS DOM window result should be correct",
+                   (sessionRule.session.evaluateJS("window")
+                           dot "location"
+                           dot "pathname") as String,
+                   equalTo(HELLO_HTML_PATH))
+    }
+
+    @WithDevToolsAPI
+    @Test fun evaluateJS_multipleSessions() {
+        val newSession = sessionRule.createOpenSession()
+
+        sessionRule.session.evaluateJS("this.foo = 42")
+        assertThat("Variable should be set",
+                   sessionRule.session.evaluateJS("this.foo") as Double, equalTo(42.0))
+
+        assertThat("New session should have separate JS context",
+                   newSession.evaluateJS("this.foo"), nullValue())
+    }
+
+    @WithDevToolsAPI
+    @Test fun evaluateJS_jsToString() {
+        val obj = sessionRule.session.evaluateJS("({foo:'bar'})")
+        assertThat("JS object toString should follow lazy evaluation",
+                   obj.toString(), equalTo("[Object]"))
+
+        assertThat("JS object should be correct",
+                   (obj dot "foo") as String, equalTo("bar"))
+        assertThat("JS object toString should be expanded after evaluation",
+                   obj.toString(), equalTo("[Object]{foo=bar}"))
+
+        val array = sessionRule.session.evaluateJS("['foo','bar']")
+        assertThat("JS array toString should follow lazy evaluation",
+                   array.toString(), equalTo("[Array(2)]"))
+
+        assertThat("JS array should be correct",
+                   (array dot 0) as String, equalTo("foo"))
+        assertThat("JS array toString should be expanded after evaluation",
+                   array.toString(), equalTo("[Array(2)][foo, bar]"))
+    }
+
+    @Test(expected = AssertionError::class)
+    fun evaluateJS_throwOnNotWithDevTools() {
+        sessionRule.session.evaluateJS("0")
+    }
+
+    @WithDevToolsAPI
+    @Test(expected = RuntimeException::class)
+    fun evaluateJS_throwOnJSException() {
+        sessionRule.session.evaluateJS("throw Error()")
+    }
+
+    @WithDevToolsAPI
+    @Test(expected = RuntimeException::class)
+    fun evaluateJS_throwOnSyntaxError() {
+        sessionRule.session.evaluateJS("<{[")
+    }
 }