|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 let newWindows = []; |
|
5 |
|
6 function test() { |
|
7 waitForExplicitFinish(); |
|
8 let windowOne = openDialog(location, "", "chrome,all,dialog=no", "data:text/html,"); |
|
9 let windowTwo; |
|
10 |
|
11 whenWindowLoaded(windowOne, function () { |
|
12 windowOne.gBrowser.selectedBrowser.addEventListener("load", function onLoad() { |
|
13 windowOne.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); |
|
14 |
|
15 windowTwo = openDialog(location, "", "chrome,all,dialog=no", "http://mochi.test:8888/"); |
|
16 whenWindowLoaded(windowTwo, function () { |
|
17 windowTwo.gBrowser.selectedBrowser.addEventListener("load", function onLoad() { |
|
18 windowTwo.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); |
|
19 |
|
20 newWindows = [ windowOne, windowTwo ]; |
|
21 |
|
22 // show the tab view |
|
23 window.addEventListener("tabviewshown", onTabViewWindowLoaded, false); |
|
24 ok(!TabView.isVisible(), "Tab View is hidden"); |
|
25 TabView.toggle(); |
|
26 }, true); |
|
27 }); |
|
28 }, true); |
|
29 }); |
|
30 } |
|
31 |
|
32 function onTabViewWindowLoaded() { |
|
33 window.removeEventListener("tabviewshown", onTabViewWindowLoaded, false); |
|
34 ok(TabView.isVisible(), "Tab View is visible"); |
|
35 |
|
36 let contentWindow = document.getElementById("tab-view").contentWindow; |
|
37 let search = contentWindow.document.getElementById("search"); |
|
38 let searchButton = contentWindow.document.getElementById("searchbutton"); |
|
39 |
|
40 ok(searchButton, "Search button exists"); |
|
41 |
|
42 let onSearchEnabled = function() { |
|
43 ok(search.style.display != "none", "Search is enabled"); |
|
44 contentWindow.removeEventListener( |
|
45 "tabviewsearchenabled", onSearchEnabled, false); |
|
46 searchTest(contentWindow); |
|
47 } |
|
48 contentWindow.addEventListener("tabviewsearchenabled", onSearchEnabled, false); |
|
49 // enter search mode |
|
50 EventUtils.sendMouseEvent({ type: "mousedown" }, searchButton, contentWindow); |
|
51 } |
|
52 |
|
53 // conveniently combine local and other window tab results from a query |
|
54 function getMatchResults(contentWindow, query) { |
|
55 let matcher = new contentWindow.TabMatcher(query); |
|
56 let localMatchResults = matcher.matched(); |
|
57 let otherMatchResults = matcher.matchedTabsFromOtherWindows(); |
|
58 return localMatchResults.concat(otherMatchResults); |
|
59 } |
|
60 |
|
61 function searchTest(contentWindow) { |
|
62 let searchBox = contentWindow.document.getElementById("searchbox"); |
|
63 let matcher = null; |
|
64 let matchResults = []; |
|
65 |
|
66 // get the titles of tabs. |
|
67 let tabNames = []; |
|
68 |
|
69 let tabItems = contentWindow.TabItems.getItems(); |
|
70 is(tabItems.length, 1, "Have only one tab in the current window's tab items"); |
|
71 tabItems.forEach(function(tab) { |
|
72 tabNames.push(tab.$tabTitle[0].innerHTML); |
|
73 }); |
|
74 |
|
75 newWindows.forEach(function(win) { |
|
76 for(var i=0; i<win.gBrowser.tabs.length; ++i) { |
|
77 tabNames.push(win.gBrowser.tabs[i].label); |
|
78 } |
|
79 }); |
|
80 |
|
81 ok(tabNames[0] && tabNames[0].length > 2, |
|
82 "The title of tab item is longer than 2 chars") |
|
83 |
|
84 // empty string |
|
85 searchBox.setAttribute("value", ""); |
|
86 matchResults = getMatchResults(contentWindow, searchBox.getAttribute("value")); |
|
87 ok(matchResults.length == 0, "Match nothing if it's an empty string"); |
|
88 |
|
89 // one char |
|
90 searchBox.setAttribute("value", tabNames[0].charAt(0)); |
|
91 matchResults = getMatchResults(contentWindow, searchBox.getAttribute("value")); |
|
92 ok(matchResults.length == 0, |
|
93 "Match nothing if the length of search term is less than 2"); |
|
94 |
|
95 // the full title |
|
96 searchBox.setAttribute("value", tabNames[2]); |
|
97 matchResults = getMatchResults(contentWindow, searchBox.getAttribute("value")); |
|
98 is(matchResults.length, 1, |
|
99 "Match something when the whole title exists"); |
|
100 |
|
101 // part of titled |
|
102 searchBox.setAttribute("value", tabNames[0].substr(1)); |
|
103 contentWindow.Search.perform(); |
|
104 matchResults = getMatchResults(contentWindow, searchBox.getAttribute("value")); |
|
105 is(matchResults.length, 1, |
|
106 "Match something when a part of title exists"); |
|
107 |
|
108 cleanup(contentWindow); |
|
109 } |
|
110 |
|
111 function cleanup(contentWindow) { |
|
112 contentWindow.Search.hide(null); |
|
113 |
|
114 let onTabViewHidden = function() { |
|
115 window.removeEventListener("tabviewhidden", onTabViewHidden, false); |
|
116 ok(!TabView.isVisible(), "Tab View is hidden"); |
|
117 let numToClose = newWindows.length; |
|
118 newWindows.forEach(function(win) { |
|
119 whenWindowObservesOnce(win, "domwindowclosed", function() { |
|
120 --numToClose; |
|
121 if(numToClose==0) { |
|
122 finish(); |
|
123 } |
|
124 }); |
|
125 win.close(); |
|
126 }); |
|
127 } |
|
128 window.addEventListener("tabviewhidden", onTabViewHidden, false); |
|
129 EventUtils.synthesizeKey("VK_RETURN", {}); |
|
130 } |
|
131 |
|
132 function whenWindowObservesOnce(win, topic, func) { |
|
133 let windowWatcher = Components.classes["@mozilla.org/embedcomp/window-watcher;1"] |
|
134 .getService(Components.interfaces.nsIWindowWatcher); |
|
135 let origWin = win; |
|
136 let origTopic = topic; |
|
137 let origFunc = func; |
|
138 function windowObserver(aSubject, aTopic, aData) { |
|
139 let theWin = aSubject.QueryInterface(Ci.nsIDOMWindow); |
|
140 if (origWin && theWin != origWin) |
|
141 return; |
|
142 if(aTopic == origTopic) { |
|
143 windowWatcher.unregisterNotification(windowObserver); |
|
144 origFunc.apply(this, []); |
|
145 } |
|
146 } |
|
147 windowWatcher.registerNotification(windowObserver); |
|
148 } |