toolkit/content/tests/browser/browser_findbar.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/content/tests/browser/browser_findbar.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,91 @@
     1.4 +XPCOMUtils.defineLazyModuleGetter(this, "Promise",
     1.5 +  "resource://gre/modules/Promise.jsm");
     1.6 +XPCOMUtils.defineLazyModuleGetter(this, "Task",
     1.7 +  "resource://gre/modules/Task.jsm");
     1.8 +Components.utils.import("resource://gre/modules/Timer.jsm", this);
     1.9 +
    1.10 +let gTabs = [];
    1.11 +
    1.12 +registerCleanupFunction(function() {
    1.13 +  for (let tab of gTabs) {
    1.14 +    if (!tab)
    1.15 +      continue;
    1.16 +    gBrowser.removeTab(tab);
    1.17 +  }
    1.18 +});
    1.19 +
    1.20 +function test() {
    1.21 +  waitForExplicitFinish();
    1.22 +
    1.23 +  Task.spawn(function() {
    1.24 +    info("Check correct 'Phrase not found' on new tab");
    1.25 +
    1.26 +    // Create a tab to run the test.
    1.27 +    yield promiseTestPageLoad();
    1.28 +
    1.29 +    // Search for the first word.
    1.30 +    yield promiseFindFinished("--- THIS SHOULD NEVER MATCH ---", false);
    1.31 +    let findbar = gBrowser.getFindBar();
    1.32 +    is(findbar._findStatusDesc.textContent, findbar._notFoundStr,
    1.33 +       "Findbar status text should be 'Phrase not found'");
    1.34 +
    1.35 +    // Create second tab.
    1.36 +    yield promiseTestPageLoad();
    1.37 +
    1.38 +    // Search for a string that WILL be found, with 'Highlight All' on
    1.39 +    yield promiseFindFinished("s", true);
    1.40 +    ok(!gBrowser.getFindBar()._findStatusDesc.textContent,
    1.41 +       "Findbar status should be empty");
    1.42 +
    1.43 +    finish();
    1.44 +  });
    1.45 +}
    1.46 +
    1.47 +function promiseTestPageLoad() {
    1.48 +  let deferred = Promise.defer();
    1.49 +
    1.50 +  let tab = gBrowser.selectedTab = gBrowser.addTab("data:text/html;charset=utf-8,The letter s.");
    1.51 +  gTabs.push(tab);
    1.52 +  let browser = gBrowser.selectedBrowser;
    1.53 +  browser.addEventListener("load", function listener() {
    1.54 +    if (browser.currentURI.spec == "about:blank")
    1.55 +      return;
    1.56 +    info("Page loaded: " + browser.currentURI.spec);
    1.57 +    browser.removeEventListener("load", listener, true);
    1.58 +
    1.59 +    deferred.resolve();
    1.60 +  }, true);
    1.61 +
    1.62 +  return deferred.promise;
    1.63 +}
    1.64 +
    1.65 +function promiseFindFinished(searchText, highlightOn) {
    1.66 +  let deferred = Promise.defer();
    1.67 +
    1.68 +  let findbar = gBrowser.getFindBar();
    1.69 +  findbar.startFind(findbar.FIND_NORMAL);
    1.70 +  let highlightElement = findbar.getElement("highlight");
    1.71 +  if (highlightElement.checked != highlightOn)
    1.72 +    highlightElement.click();
    1.73 +  executeSoon(() => {
    1.74 +    findbar._findField.value = searchText;
    1.75 +
    1.76 +    let resultListener;
    1.77 +    let findTimeout = setTimeout(() => foundOrTimedout(null), 2000);
    1.78 +    let foundOrTimedout = function(aData) {
    1.79 +      if (aData === null)
    1.80 +        info("Result listener not called, timeout reached.");
    1.81 +      clearTimeout(findTimeout);
    1.82 +      findbar.browser.finder.removeResultListener(resultListener);
    1.83 +      deferred.resolve();
    1.84 +    }
    1.85 +
    1.86 +    resultListener = {
    1.87 +      onFindResult: foundOrTimedout
    1.88 +    };
    1.89 +    findbar.browser.finder.addResultListener(resultListener);
    1.90 +    findbar._find();
    1.91 +  });
    1.92 +
    1.93 +  return deferred.promise;
    1.94 +}

mercurial