browser/components/tabview/test/browser_tabview_multiwindow_search.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/tabview/test/browser_tabview_multiwindow_search.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,148 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +let newWindows = [];
     1.8 +
     1.9 +function test() {
    1.10 +  waitForExplicitFinish();
    1.11 +  let windowOne = openDialog(location, "", "chrome,all,dialog=no", "data:text/html,");
    1.12 +  let windowTwo;
    1.13 +
    1.14 +  whenWindowLoaded(windowOne, function () {
    1.15 +    windowOne.gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
    1.16 +      windowOne.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
    1.17 +
    1.18 +      windowTwo = openDialog(location, "", "chrome,all,dialog=no", "http://mochi.test:8888/");
    1.19 +      whenWindowLoaded(windowTwo, function () {
    1.20 +        windowTwo.gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
    1.21 +          windowTwo.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
    1.22 +
    1.23 +          newWindows = [ windowOne, windowTwo ];
    1.24 +
    1.25 +          // show the tab view
    1.26 +          window.addEventListener("tabviewshown", onTabViewWindowLoaded, false);
    1.27 +          ok(!TabView.isVisible(), "Tab View is hidden");
    1.28 +          TabView.toggle();
    1.29 +        }, true);
    1.30 +      });
    1.31 +    }, true);
    1.32 +  });
    1.33 +}
    1.34 +
    1.35 +function onTabViewWindowLoaded() {
    1.36 +  window.removeEventListener("tabviewshown", onTabViewWindowLoaded, false);
    1.37 +  ok(TabView.isVisible(), "Tab View is visible");
    1.38 +
    1.39 +  let contentWindow = document.getElementById("tab-view").contentWindow;
    1.40 +  let search = contentWindow.document.getElementById("search");
    1.41 +  let searchButton = contentWindow.document.getElementById("searchbutton");
    1.42 +
    1.43 +  ok(searchButton, "Search button exists");
    1.44 +  
    1.45 +  let onSearchEnabled = function() {
    1.46 +    ok(search.style.display != "none", "Search is enabled");
    1.47 +    contentWindow.removeEventListener(
    1.48 +      "tabviewsearchenabled", onSearchEnabled, false);
    1.49 +    searchTest(contentWindow);
    1.50 +  }
    1.51 +  contentWindow.addEventListener("tabviewsearchenabled", onSearchEnabled, false);
    1.52 +  // enter search mode
    1.53 +  EventUtils.sendMouseEvent({ type: "mousedown" }, searchButton, contentWindow);
    1.54 +}
    1.55 +
    1.56 +// conveniently combine local and other window tab results from a query
    1.57 +function getMatchResults(contentWindow, query) {
    1.58 +  let matcher = new contentWindow.TabMatcher(query);
    1.59 +  let localMatchResults = matcher.matched();
    1.60 +  let otherMatchResults = matcher.matchedTabsFromOtherWindows();
    1.61 +  return localMatchResults.concat(otherMatchResults);
    1.62 +}
    1.63 +
    1.64 +function searchTest(contentWindow) {
    1.65 +  let searchBox = contentWindow.document.getElementById("searchbox");
    1.66 +  let matcher = null;
    1.67 +  let matchResults = [];
    1.68 +  
    1.69 +  // get the titles of tabs.
    1.70 +  let tabNames = [];
    1.71 +
    1.72 +  let tabItems = contentWindow.TabItems.getItems();
    1.73 +  is(tabItems.length, 1, "Have only one tab in the current window's tab items"); 
    1.74 +  tabItems.forEach(function(tab) {
    1.75 +    tabNames.push(tab.$tabTitle[0].innerHTML);
    1.76 +  });
    1.77 +
    1.78 +  newWindows.forEach(function(win) {
    1.79 +      for(var i=0; i<win.gBrowser.tabs.length; ++i) {
    1.80 +        tabNames.push(win.gBrowser.tabs[i].label);
    1.81 +      }
    1.82 +  });
    1.83 +
    1.84 +  ok(tabNames[0] && tabNames[0].length > 2, 
    1.85 +     "The title of tab item is longer than 2 chars")
    1.86 +
    1.87 +  // empty string
    1.88 +  searchBox.setAttribute("value", "");
    1.89 +  matchResults = getMatchResults(contentWindow, searchBox.getAttribute("value"));
    1.90 +  ok(matchResults.length == 0, "Match nothing if it's an empty string");
    1.91 +
    1.92 +  // one char
    1.93 +  searchBox.setAttribute("value", tabNames[0].charAt(0));
    1.94 +  matchResults = getMatchResults(contentWindow, searchBox.getAttribute("value"));
    1.95 +  ok(matchResults.length == 0,
    1.96 +     "Match nothing if the length of search term is less than 2");
    1.97 +
    1.98 +  // the full title
    1.99 +  searchBox.setAttribute("value", tabNames[2]);
   1.100 +  matchResults = getMatchResults(contentWindow, searchBox.getAttribute("value"));
   1.101 +  is(matchResults.length, 1,
   1.102 +    "Match something when the whole title exists");
   1.103 +
   1.104 +  // part of titled
   1.105 +  searchBox.setAttribute("value", tabNames[0].substr(1));
   1.106 +  contentWindow.Search.perform();
   1.107 +  matchResults = getMatchResults(contentWindow, searchBox.getAttribute("value"));
   1.108 +  is(matchResults.length, 1,
   1.109 +     "Match something when a part of title exists");
   1.110 +
   1.111 +  cleanup(contentWindow);
   1.112 +}
   1.113 +
   1.114 +function cleanup(contentWindow) {
   1.115 +  contentWindow.Search.hide(null);
   1.116 +
   1.117 +  let onTabViewHidden = function() {
   1.118 +      window.removeEventListener("tabviewhidden", onTabViewHidden, false);
   1.119 +      ok(!TabView.isVisible(), "Tab View is hidden");
   1.120 +      let numToClose = newWindows.length;
   1.121 +      newWindows.forEach(function(win) {
   1.122 +        whenWindowObservesOnce(win, "domwindowclosed", function() {
   1.123 +          --numToClose;
   1.124 +          if(numToClose==0) {
   1.125 +            finish();
   1.126 +          }
   1.127 +        });
   1.128 +        win.close();
   1.129 +      });
   1.130 +  }
   1.131 +  window.addEventListener("tabviewhidden", onTabViewHidden, false);
   1.132 +  EventUtils.synthesizeKey("VK_RETURN", {});
   1.133 +}
   1.134 +
   1.135 +function whenWindowObservesOnce(win, topic, func) {
   1.136 +    let windowWatcher = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
   1.137 +      .getService(Components.interfaces.nsIWindowWatcher);
   1.138 +    let origWin = win;
   1.139 +    let origTopic = topic;
   1.140 +    let origFunc = func;        
   1.141 +    function windowObserver(aSubject, aTopic, aData) {
   1.142 +      let theWin = aSubject.QueryInterface(Ci.nsIDOMWindow);
   1.143 +      if (origWin && theWin != origWin)
   1.144 +        return;
   1.145 +      if(aTopic == origTopic) {
   1.146 +          windowWatcher.unregisterNotification(windowObserver);
   1.147 +          origFunc.apply(this, []);
   1.148 +      }
   1.149 +    }
   1.150 +    windowWatcher.registerNotification(windowObserver);
   1.151 +}

mercurial