browser/components/tabview/test/browser_tabview_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_search.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,132 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +let newTabs = [];
     1.8 +
     1.9 +// ----------
    1.10 +function test() {
    1.11 +  waitForExplicitFinish();
    1.12 +
    1.13 +  // set up our tabs
    1.14 +  let urlBase = "http://mochi.test:8888/browser/browser/components/tabview/test/";
    1.15 +  let tabOne = gBrowser.addTab(urlBase + "search1.html");
    1.16 +  let tabTwo = gBrowser.addTab(urlBase + "search2.html");
    1.17 +  newTabs = [ tabOne, tabTwo ];
    1.18 +
    1.19 +  // make sure our tabs are loaded so their titles are right
    1.20 +  let stillToLoad = 0; 
    1.21 +  let onLoad = function() {
    1.22 +    this.removeEventListener("load", onLoad, true);
    1.23 +    
    1.24 +    stillToLoad--; 
    1.25 +    if (!stillToLoad) {    
    1.26 +      // show the tab view
    1.27 +      window.addEventListener("tabviewshown", onTabViewWindowLoaded, false);
    1.28 +      ok(!TabView.isVisible(), "Tab View is hidden");
    1.29 +      TabView.toggle();
    1.30 +    }
    1.31 +  }
    1.32 +  
    1.33 +  newTabs.forEach(function(tab) {
    1.34 +    stillToLoad++; 
    1.35 +    gBrowser.getBrowserForTab(tab).addEventListener("load", onLoad, true);
    1.36 +  });
    1.37 +}
    1.38 +
    1.39 +// ----------
    1.40 +function onTabViewWindowLoaded() {
    1.41 +  window.removeEventListener("tabviewshown", onTabViewWindowLoaded, false);
    1.42 +  ok(TabView.isVisible(), "Tab View is visible");
    1.43 +
    1.44 +  let contentWindow = document.getElementById("tab-view").contentWindow;
    1.45 +  let search = contentWindow.document.getElementById("search");
    1.46 +  let searchButton = contentWindow.document.getElementById("searchbutton");
    1.47 +
    1.48 +  ok(searchButton, "Search button exists");
    1.49 +
    1.50 +  let onSearchEnabled = function() {
    1.51 +    contentWindow.removeEventListener(
    1.52 +      "tabviewsearchenabled", onSearchEnabled, false);
    1.53 +
    1.54 +    ok(search.style.display != "none", "Search is enabled");
    1.55 +
    1.56 +    let searchBox = contentWindow.document.getElementById("searchbox");
    1.57 +    ok(contentWindow.document.hasFocus() && 
    1.58 +       contentWindow.document.activeElement == searchBox, 
    1.59 +       "The search box has focus");
    1.60 +
    1.61 +    searchTest(contentWindow);
    1.62 +  }
    1.63 +  contentWindow.addEventListener("tabviewsearchenabled", onSearchEnabled, false);
    1.64 +  // enter search mode
    1.65 +  EventUtils.sendMouseEvent({ type: "mousedown" }, searchButton, contentWindow);
    1.66 +}
    1.67 +
    1.68 +// ----------
    1.69 +function searchTest(contentWindow) {
    1.70 +  let searchBox = contentWindow.document.getElementById("searchbox");
    1.71 +
    1.72 +  // force an update to make sure the correct titles are in the TabItems
    1.73 +  let tabItems = contentWindow.TabItems.getItems();
    1.74 +  ok(tabItems.length == 3, "Have three tab items");
    1.75 +  tabItems.forEach(function(tabItem) {
    1.76 +    contentWindow.TabItems._update(tabItem.tab);
    1.77 +  });
    1.78 +
    1.79 +  // empty string
    1.80 +  searchBox.setAttribute("value", "");
    1.81 +  is(new contentWindow.TabMatcher(
    1.82 +      searchBox.getAttribute("value")).matched().length, 0,
    1.83 +     "Match nothing if it's an empty string");
    1.84 +
    1.85 +  // one char
    1.86 +  searchBox.setAttribute("value", "s");
    1.87 +  is(new contentWindow.TabMatcher(
    1.88 +      searchBox.getAttribute("value")).matched().length, 0,
    1.89 +     "Match nothing if the length of search term is less than 2");
    1.90 +
    1.91 +  // the full title
    1.92 +  searchBox.setAttribute("value", "search test 1");
    1.93 +  is(new contentWindow.TabMatcher(
    1.94 +      searchBox.getAttribute("value")).matched().length, 1,
    1.95 +     "Match something when the whole title exists");
    1.96 +  
    1.97 +  // part of title
    1.98 +  searchBox.setAttribute("value", "search");
    1.99 +  contentWindow.Search.perform();
   1.100 +  is(new contentWindow.TabMatcher(
   1.101 +      searchBox.getAttribute("value")).matched().length, 2,
   1.102 +     "Match something when a part of title exists");
   1.103 +
   1.104 +  // unique part of a url 
   1.105 +  searchBox.setAttribute("value", "search1.html");
   1.106 +  contentWindow.Search.perform();
   1.107 +  is(new contentWindow.TabMatcher(
   1.108 +      searchBox.getAttribute("value")).matched().length, 1,
   1.109 +     "Match something when a unique part of a url exists");
   1.110 +   
   1.111 +  // common part of a url
   1.112 +  searchBox.setAttribute("value", "tabview");
   1.113 +  contentWindow.Search.perform();
   1.114 +  is(new contentWindow.TabMatcher(
   1.115 +      searchBox.getAttribute("value")).matched().length, 2,
   1.116 +     "Match something when a common part of a url exists");
   1.117 +     
   1.118 +  cleanup(contentWindow);
   1.119 +}
   1.120 +
   1.121 +// ----------
   1.122 +function cleanup(contentWindow) {       
   1.123 +  contentWindow.Search.hide(null);     
   1.124 +  let onTabViewHidden = function() {
   1.125 +    window.removeEventListener("tabviewhidden", onTabViewHidden, false);
   1.126 +    ok(!TabView.isVisible(), "Tab View is hidden");
   1.127 +
   1.128 +    gBrowser.removeTab(newTabs[0]);
   1.129 +    gBrowser.removeTab(newTabs[1]);
   1.130 +
   1.131 +    finish();
   1.132 +  }
   1.133 +  window.addEventListener("tabviewhidden", onTabViewHidden, false);
   1.134 +  EventUtils.synthesizeKey("VK_RETURN", {});
   1.135 +}

mercurial