browser/components/tabview/test/browser_tabview_search.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:e56a05dde56f
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 let newTabs = [];
5
6 // ----------
7 function test() {
8 waitForExplicitFinish();
9
10 // set up our tabs
11 let urlBase = "http://mochi.test:8888/browser/browser/components/tabview/test/";
12 let tabOne = gBrowser.addTab(urlBase + "search1.html");
13 let tabTwo = gBrowser.addTab(urlBase + "search2.html");
14 newTabs = [ tabOne, tabTwo ];
15
16 // make sure our tabs are loaded so their titles are right
17 let stillToLoad = 0;
18 let onLoad = function() {
19 this.removeEventListener("load", onLoad, true);
20
21 stillToLoad--;
22 if (!stillToLoad) {
23 // show the tab view
24 window.addEventListener("tabviewshown", onTabViewWindowLoaded, false);
25 ok(!TabView.isVisible(), "Tab View is hidden");
26 TabView.toggle();
27 }
28 }
29
30 newTabs.forEach(function(tab) {
31 stillToLoad++;
32 gBrowser.getBrowserForTab(tab).addEventListener("load", onLoad, true);
33 });
34 }
35
36 // ----------
37 function onTabViewWindowLoaded() {
38 window.removeEventListener("tabviewshown", onTabViewWindowLoaded, false);
39 ok(TabView.isVisible(), "Tab View is visible");
40
41 let contentWindow = document.getElementById("tab-view").contentWindow;
42 let search = contentWindow.document.getElementById("search");
43 let searchButton = contentWindow.document.getElementById("searchbutton");
44
45 ok(searchButton, "Search button exists");
46
47 let onSearchEnabled = function() {
48 contentWindow.removeEventListener(
49 "tabviewsearchenabled", onSearchEnabled, false);
50
51 ok(search.style.display != "none", "Search is enabled");
52
53 let searchBox = contentWindow.document.getElementById("searchbox");
54 ok(contentWindow.document.hasFocus() &&
55 contentWindow.document.activeElement == searchBox,
56 "The search box has focus");
57
58 searchTest(contentWindow);
59 }
60 contentWindow.addEventListener("tabviewsearchenabled", onSearchEnabled, false);
61 // enter search mode
62 EventUtils.sendMouseEvent({ type: "mousedown" }, searchButton, contentWindow);
63 }
64
65 // ----------
66 function searchTest(contentWindow) {
67 let searchBox = contentWindow.document.getElementById("searchbox");
68
69 // force an update to make sure the correct titles are in the TabItems
70 let tabItems = contentWindow.TabItems.getItems();
71 ok(tabItems.length == 3, "Have three tab items");
72 tabItems.forEach(function(tabItem) {
73 contentWindow.TabItems._update(tabItem.tab);
74 });
75
76 // empty string
77 searchBox.setAttribute("value", "");
78 is(new contentWindow.TabMatcher(
79 searchBox.getAttribute("value")).matched().length, 0,
80 "Match nothing if it's an empty string");
81
82 // one char
83 searchBox.setAttribute("value", "s");
84 is(new contentWindow.TabMatcher(
85 searchBox.getAttribute("value")).matched().length, 0,
86 "Match nothing if the length of search term is less than 2");
87
88 // the full title
89 searchBox.setAttribute("value", "search test 1");
90 is(new contentWindow.TabMatcher(
91 searchBox.getAttribute("value")).matched().length, 1,
92 "Match something when the whole title exists");
93
94 // part of title
95 searchBox.setAttribute("value", "search");
96 contentWindow.Search.perform();
97 is(new contentWindow.TabMatcher(
98 searchBox.getAttribute("value")).matched().length, 2,
99 "Match something when a part of title exists");
100
101 // unique part of a url
102 searchBox.setAttribute("value", "search1.html");
103 contentWindow.Search.perform();
104 is(new contentWindow.TabMatcher(
105 searchBox.getAttribute("value")).matched().length, 1,
106 "Match something when a unique part of a url exists");
107
108 // common part of a url
109 searchBox.setAttribute("value", "tabview");
110 contentWindow.Search.perform();
111 is(new contentWindow.TabMatcher(
112 searchBox.getAttribute("value")).matched().length, 2,
113 "Match something when a common part of a url exists");
114
115 cleanup(contentWindow);
116 }
117
118 // ----------
119 function cleanup(contentWindow) {
120 contentWindow.Search.hide(null);
121 let onTabViewHidden = function() {
122 window.removeEventListener("tabviewhidden", onTabViewHidden, false);
123 ok(!TabView.isVisible(), "Tab View is hidden");
124
125 gBrowser.removeTab(newTabs[0]);
126 gBrowser.removeTab(newTabs[1]);
127
128 finish();
129 }
130 window.addEventListener("tabviewhidden", onTabViewHidden, false);
131 EventUtils.synthesizeKey("VK_RETURN", {});
132 }

mercurial