Bug 1463484 - 2. Add test for find-in-page; r=esawin,snorp a=lizzard
authorJim Chen <nchen@mozilla.com>
Tue, 10 Jul 2018 13:12:55 -0400
changeset 477949 1654819afa70
parent 477948 a3fcc54f9143
child 477950 36921dd17b6b
push id9475
push userarchaeopteryx@coole-files.de
push dateFri, 13 Jul 2018 21:33:22 +0000
treeherdermozilla-beta@d7ab2f3df084 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersesawin, snorp, lizzard
bugs1463484
milestone62.0
Bug 1463484 - 2. Add test for find-in-page; r=esawin,snorp a=lizzard Add some test cases for find-in-page and the different flags. MozReview-Commit-ID: BzN0OTSUy7z
mobile/android/geckoview/src/androidTest/assets/www/loremIpsum.html
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/BaseSessionTest.kt
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/FinderTest.kt
new file mode 100644
--- /dev/null
+++ b/mobile/android/geckoview/src/androidTest/assets/www/loremIpsum.html
@@ -0,0 +1,13 @@
+<html>
+    <head><title>Lorem ipsum</title></head>
+    <body>
+        <p>
+            <a href="#">Lorem ipsum</a> dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
+            incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
+            exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
+            dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
+            Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
+            mollit anim id est laborum.
+        </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
@@ -27,16 +27,17 @@ open class BaseSessionTest(noErrorCollec
     companion object {
         const val CLICK_TO_RELOAD_HTML_PATH = "/assets/www/clickToReload.html"
         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 LOREM_IPSUM_HTML_PATH = "/assets/www/loremIpsum.html"
         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()
new file mode 100644
--- /dev/null
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/FinderTest.kt
@@ -0,0 +1,170 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
+ * Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+package org.mozilla.geckoview.test
+
+import org.mozilla.geckoview.GeckoSession
+import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.ReuseSession
+import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.WithDevToolsAPI
+
+import android.support.test.filters.MediumTest
+import android.support.test.runner.AndroidJUnit4
+import org.hamcrest.Matchers.*
+import org.junit.Test
+import org.junit.runner.RunWith
+
+@RunWith(AndroidJUnit4::class)
+@MediumTest
+class FinderTest : BaseSessionTest() {
+
+    @ReuseSession(false) // "wrapped" in the first result depends on a fresh session.
+    @Test fun find() {
+        mainSession.loadTestPath(LOREM_IPSUM_HTML_PATH)
+        mainSession.waitForPageStop()
+
+        // Initial search.
+        var result = sessionRule.waitForResult(mainSession.finder.find("dolore", 0))
+
+        assertThat("Should be found", result.found, equalTo(true))
+        // "wrapped" is true for the first found result of a new session.
+        assertThat("Should have wrapped", result.wrapped, equalTo(true))
+        assertThat("Current count should be correct", result.current, equalTo(1))
+        assertThat("Total count should be correct", result.total, equalTo(2))
+        assertThat("Search string should be correct",
+                   result.searchString, equalTo("dolore"))
+        assertThat("Flags should be correct", result.flags, equalTo(0))
+
+        // Search again using new flags.
+        result = sessionRule.waitForResult(mainSession.finder.find(
+                null, GeckoSession.FINDER_FIND_BACKWARDS
+                                        or GeckoSession.FINDER_FIND_MATCH_CASE
+                                        or GeckoSession.FINDER_FIND_WHOLE_WORD))
+
+        assertThat("Should be found", result.found, equalTo(true))
+        assertThat("Should have wrapped", result.wrapped, equalTo(true))
+        assertThat("Current count should be correct", result.current, equalTo(2))
+        assertThat("Total count should be correct", result.total, equalTo(2))
+        assertThat("Search string should be correct",
+                   result.searchString, equalTo("dolore"))
+        assertThat("Flags should be correct", result.flags,
+                   equalTo(GeckoSession.FINDER_FIND_BACKWARDS
+                                            or GeckoSession.FINDER_FIND_MATCH_CASE
+                                            or GeckoSession.FINDER_FIND_WHOLE_WORD))
+
+        // And again using same flags.
+        result = sessionRule.waitForResult(mainSession.finder.find(
+                null, GeckoSession.FINDER_FIND_BACKWARDS
+                                        or GeckoSession.FINDER_FIND_MATCH_CASE
+                                        or GeckoSession.FINDER_FIND_WHOLE_WORD))
+
+        assertThat("Should be found", result.found, equalTo(true))
+        assertThat("Should not have wrapped", result.wrapped, equalTo(false))
+        assertThat("Current count should be correct", result.current, equalTo(1))
+        assertThat("Total count should be correct", result.total, equalTo(2))
+        assertThat("Search string should be correct",
+                   result.searchString, equalTo("dolore"))
+        assertThat("Flags should be correct", result.flags,
+                   equalTo(GeckoSession.FINDER_FIND_BACKWARDS
+                                            or GeckoSession.FINDER_FIND_MATCH_CASE
+                                            or GeckoSession.FINDER_FIND_WHOLE_WORD))
+
+        // And again but go forward.
+        result = sessionRule.waitForResult(mainSession.finder.find(
+                null, GeckoSession.FINDER_FIND_MATCH_CASE
+                                        or GeckoSession.FINDER_FIND_WHOLE_WORD))
+
+        assertThat("Should be found", result.found, equalTo(true))
+        assertThat("Should not have wrapped", result.wrapped, equalTo(false))
+        assertThat("Current count should be correct", result.current, equalTo(2))
+        assertThat("Total count should be correct", result.total, equalTo(2))
+        assertThat("Search string should be correct",
+                   result.searchString, equalTo("dolore"))
+        assertThat("Flags should be correct", result.flags,
+                   equalTo(GeckoSession.FINDER_FIND_MATCH_CASE
+                                            or GeckoSession.FINDER_FIND_WHOLE_WORD))
+    }
+
+    @Test fun find_notFound() {
+        mainSession.loadTestPath(LOREM_IPSUM_HTML_PATH)
+        mainSession.waitForPageStop()
+
+        var result = sessionRule.waitForResult(mainSession.finder.find("foo", 0))
+
+        assertThat("Should not be found", result.found, equalTo(false))
+        assertThat("Should have wrapped", result.wrapped, equalTo(true))
+        assertThat("Current count should be correct", result.current, equalTo(0))
+        assertThat("Total count should be correct", result.total, equalTo(0))
+        assertThat("Search string should be correct",
+                   result.searchString, equalTo("foo"))
+        assertThat("Flags should be correct", result.flags, equalTo(0))
+
+        result = sessionRule.waitForResult(mainSession.finder.find("lore", 0))
+
+        assertThat("Should be found", result.found, equalTo(true))
+    }
+
+    @Test fun find_matchCase() {
+        mainSession.loadTestPath(LOREM_IPSUM_HTML_PATH)
+        mainSession.waitForPageStop()
+
+        var result = sessionRule.waitForResult(mainSession.finder.find("lore", 0))
+
+        assertThat("Total count should be correct", result.total, equalTo(3))
+
+        result = sessionRule.waitForResult(mainSession.finder.find(
+                null, GeckoSession.FINDER_FIND_MATCH_CASE))
+
+        assertThat("Total count should be correct", result.total, equalTo(2))
+        assertThat("Flags should be correct",
+                   result.flags, equalTo(GeckoSession.FINDER_FIND_MATCH_CASE))
+    }
+
+    @Test fun find_wholeWord() {
+        mainSession.loadTestPath(LOREM_IPSUM_HTML_PATH)
+        mainSession.waitForPageStop()
+
+        var result = sessionRule.waitForResult(mainSession.finder.find("dolor", 0))
+
+        assertThat("Total count should be correct", result.total, equalTo(4))
+
+        result = sessionRule.waitForResult(mainSession.finder.find(
+                null, GeckoSession.FINDER_FIND_WHOLE_WORD))
+
+        assertThat("Total count should be correct", result.total, equalTo(2))
+        assertThat("Flags should be correct",
+                   result.flags, equalTo(GeckoSession.FINDER_FIND_WHOLE_WORD))
+    }
+
+    @Test fun find_linksOnly() {
+        mainSession.loadTestPath(LOREM_IPSUM_HTML_PATH)
+        mainSession.waitForPageStop()
+
+        val result = sessionRule.waitForResult(mainSession.finder.find(
+                "lore", GeckoSession.FINDER_FIND_LINKS_ONLY))
+
+        assertThat("Total count should be correct", result.total, equalTo(1))
+        assertThat("Flags should be correct",
+                   result.flags, equalTo(GeckoSession.FINDER_FIND_LINKS_ONLY))
+    }
+
+    @WithDevToolsAPI
+    @Test fun clear() {
+        mainSession.loadTestPath(LOREM_IPSUM_HTML_PATH)
+        mainSession.waitForPageStop()
+
+        val result = sessionRule.waitForResult(mainSession.finder.find("lore", 0))
+
+        assertThat("Match should be found", result.found, equalTo(true))
+
+        assertThat("Match should be selected",
+                   mainSession.evaluateJS("window.getSelection().toString()") as String,
+                   equalTo("Lore"))
+
+        mainSession.finder.clear()
+
+        assertThat("Match should be cleared",
+                   mainSession.evaluateJS("window.getSelection().isCollapsed") as Boolean,
+                   equalTo(true))
+    }
+}
\ No newline at end of file