Bug 1456190 - 3. Add tests for evaluateJS(); r?snorp draft
authorJim Chen <nchen@mozilla.com>
Fri, 27 Apr 2018 11:57:14 -0400
changeset 789106 16256d0f62ab1f19f4ff82e1245ee3503d6365fd
parent 789105 458706ad3fa505d0e0fe9d2f4fd982b4eb78b941
child 789107 572819103dcc17e39257426a6b825b8567976cb6
push id108180
push userbmo:nchen@mozilla.com
push dateFri, 27 Apr 2018 15:59:58 +0000
reviewerssnorp
bugs1456190
milestone61.0a1
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("<{[")
+    }
 }