Bug 1316515 - clear the find selection when the findbar input box is cleared. r?Gijs draft
authorMike de Boer <mdeboer@mozilla.com>
Mon, 14 Nov 2016 13:29:12 +0100
changeset 438404 b90bba2a26e3cf28e5d969dbcfcc02882b1792d5
parent 438400 68856d91e9718ccaa214e8060b284f4c140bae52
child 438406 b313af635e9cc4a276e5b80df6baaf3ace3fa418
child 438407 4599b6fe4f83166d0789eae7ed0ccb435b36f3ca
push id35697
push usermdeboer@mozilla.com
push dateMon, 14 Nov 2016 12:29:53 +0000
reviewersGijs
bugs1316515
milestone52.0a1
Bug 1316515 - clear the find selection when the findbar input box is cleared. r?Gijs MozReview-Commit-ID: AfmubgbCevw
toolkit/content/tests/chrome/findbar_window.xul
toolkit/modules/FinderHighlighter.jsm
--- a/toolkit/content/tests/chrome/findbar_window.xul
+++ b/toolkit/content/tests/chrome/findbar_window.xul
@@ -268,16 +268,28 @@
           }
         };
         gFindBar.browser.finder.addResultListener(listener);
         // Make sure we resolve _at least_ after five times the find iterator timeout.
         setTimeout(resolve, (ITERATOR_TIMEOUT * 5) + 20);
       });
     }
 
+    function promiseHighlightFinished() {
+      return new Promise(resolve => {
+        let listener = {
+          onHighlightFinished() {
+            gFindBar.browser.finder.removeResultListener(listener);
+            resolve();
+          }
+        };
+        gFindBar.browser.finder.addResultListener(listener);
+      });
+    }
+
     var enterStringIntoFindField = Task.async(function* (str, waitForResult = true) {
       for (let promise, i = 0; i < str.length; i++) {
         if (waitForResult) {
           promise = promiseFindResult();
         }
         let event = document.createEvent("KeyboardEvent");
         event.initKeyEvent("keypress", true, true, null, false, false,
                            false, false, 0, str.charCodeAt(i));
@@ -444,17 +456,16 @@
           "testQuickFindLink: failed to find sample link");
       });
       testFindButtonsState();
       testClipboardSearchString(searchStr);
     }
 
     // See bug 963925 for more details on this test.
     function* testFindWithHighlight() {
-      //yield clearFocus();
       gFindBar._findField.value = "";
 
       // For this test, we want to closely control the selection. The easiest
       // way to do so is to replace the implementation of
       // Finder.getInitialSelection with a no-op and call the findbar's callback
       // (onCurrentSelection(..., true)) ourselves with our hand-picked
       // selection.
       let oldGetInitialSelection = gFindBar.browser.finder.getInitialSelection;
@@ -498,16 +509,53 @@
       gFindBar.onFindAgainCommand();
       a = gFindBar._findField.value;
       b = gFindBar._browser.finder._fastFind.searchString;
       c = gFindBar._browser.finder.searchString;
       ok(a == b && b == c, "testFindWithHighlight 5: " + a + ", " + b + ", " + c + ".");
 
       highlightButton.click();
       ok(!highlightButton.checked, "testFindWithHighlight: Highlight All should be unchecked.");
+
+      // Regression test for bug 1316515.
+      searchStr = "e";
+      gFindBar.clear();
+      yield enterStringIntoFindField(searchStr);
+
+      yield ContentTask.spawn(gBrowser, {}, function* () {
+        let controller = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
+                                 .getInterface(Ci.nsISelectionDisplay)
+                                 .QueryInterface(Ci.nsISelectionController);
+        let sel = controller.getSelection(Ci.nsISelectionController.SELECTION_FIND);
+        Assert.equal(sel.rangeCount, 0, "testFindWithHighlight - 1316515: no highlights, please [1]");
+      });
+
+      highlightButton.click();
+      ok(highlightButton.checked, "testFindWithHighlight: Highlight All should be checked.");
+
+      yield promiseHighlightFinished();
+
+      yield ContentTask.spawn(gBrowser, {}, function* () {
+        let controller = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
+                                 .getInterface(Ci.nsISelectionDisplay)
+                                 .QueryInterface(Ci.nsISelectionController);
+        let sel = controller.getSelection(Ci.nsISelectionController.SELECTION_FIND);
+        // The other 4 ranges are in a different controller.
+        Assert.equal(sel.rangeCount, 3, "testFindWithHighlight - 1316515: more highlights, please");
+      });
+
+      synthesizeKey("VK_BACK_SPACE", {});
+
+      yield ContentTask.spawn(gBrowser, null, function* () {
+        let controller = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
+                                 .getInterface(Ci.nsISelectionDisplay)
+                                 .QueryInterface(Ci.nsISelectionController);
+        let sel = controller.getSelection(Ci.nsISelectionController.SELECTION_FIND);
+        Assert.equal(sel.rangeCount, 0, "testFindWithHighlight - 1316515: no highlights, please [2]");
+      });
     }
 
     function* testQuickFindText() {
       yield clearFocus();
 
       yield ContentTask.spawn(gBrowser, null, function* () {
         let document = content.document;
         let event = document.createEvent("KeyboardEvent");
--- a/toolkit/modules/FinderHighlighter.jsm
+++ b/toolkit/modules/FinderHighlighter.jsm
@@ -406,33 +406,34 @@ FinderHighlighter.prototype = {
    *   {String}  searchString  The string the search was performed with.
    *   {Boolean} storeResult   Indicator if the search string should be stored
    *                           by the consumer of the Finder.
    */
   update(data) {
     let window = this.finder._getWindow();
     let dict = this.getForWindow(window);
     let foundRange = this.finder._fastFind.getFoundRange();
+
+    if (data.result == Ci.nsITypeAheadFind.FIND_NOTFOUND || !data.searchString || !foundRange) {
+      this.hide();
+      return;
+    }
+
     if (!this._modal) {
       if (this._highlightAll) {
         dict.currentFoundRange = foundRange;
         let params = this.iterator.params;
         if (this.iterator._areParamsEqual(params, dict.lastIteratorParams))
           return;
         if (params)
           this.highlight(true, params.word, params.linksOnly);
       }
       return;
     }
 
-    if (data.result == Ci.nsITypeAheadFind.FIND_NOTFOUND || !data.searchString || !foundRange) {
-      this.hide();
-      return;
-    }
-
     if (foundRange !== dict.currentFoundRange || data.findAgain) {
       dict.currentFoundRange = foundRange;
 
       let textContent = this._getRangeContentArray(foundRange);
       if (!textContent.length) {
         this.hide(window);
         return;
       }