|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 function test() { |
|
6 /** Tests for NetworkPrioritizer.jsm (Bug 514490) **/ |
|
7 |
|
8 waitForExplicitFinish(); |
|
9 |
|
10 const PRIORITY_DELTA = -10; // same as in NetworkPrioritizer |
|
11 |
|
12 // Test helper functions. |
|
13 // getPriority and setPriority can take a Tab or a Browser |
|
14 function getPriority(aBrowser) { |
|
15 // Assume we were passed a tab if it's not a browser |
|
16 if (!aBrowser.webNavigation) |
|
17 aBrowser = aBrowser.linkedBrowser; |
|
18 return aBrowser.webNavigation.QueryInterface(Ci.nsIDocumentLoader) |
|
19 .loadGroup.QueryInterface(Ci.nsISupportsPriority).priority; |
|
20 } |
|
21 function setPriority(aBrowser, aPriority) { |
|
22 if (!aBrowser.webNavigation) |
|
23 aBrowser = aBrowser.linkedBrowser; |
|
24 aBrowser.webNavigation.QueryInterface(Ci.nsIDocumentLoader) |
|
25 .loadGroup.QueryInterface(Ci.nsISupportsPriority).priority = aPriority; |
|
26 } |
|
27 |
|
28 function isWindowState(aWindow, aTabPriorities) { |
|
29 let browsers = aWindow.gBrowser.browsers; |
|
30 // Make sure we have the right number of tabs & priorities |
|
31 is(browsers.length, aTabPriorities.length, |
|
32 "Window has expected number of tabs"); |
|
33 // aState should be in format [ priority, priority, priority ] |
|
34 for (let i = 0; i < browsers.length; i++) { |
|
35 is(getPriority(browsers[i]), aTabPriorities[i], |
|
36 "Tab had expected priority"); |
|
37 } |
|
38 } |
|
39 |
|
40 |
|
41 // This is the real test. It creates multiple tabs & windows, changes focus, |
|
42 // closes windows/tabs to make sure we behave correctly. |
|
43 // This test assumes that no priorities have been adjusted and the loadgroup |
|
44 // priority starts at 0. |
|
45 function test_behavior() { |
|
46 |
|
47 // Call window "window_A" to make the test easier to follow |
|
48 let window_A = window; |
|
49 |
|
50 // Test 1 window, 1 tab case. |
|
51 isWindowState(window_A, [-10]); |
|
52 |
|
53 // Exising tab is tab_A1 |
|
54 let tab_A2 = window_A.gBrowser.addTab("http://example.com"); |
|
55 let tab_A3 = window_A.gBrowser.addTab("about:config"); |
|
56 tab_A3.linkedBrowser.addEventListener("load", function(aEvent) { |
|
57 tab_A3.linkedBrowser.removeEventListener("load", arguments.callee, true); |
|
58 |
|
59 // tab_A2 isn't focused yet |
|
60 isWindowState(window_A, [-10, 0, 0]); |
|
61 |
|
62 // focus tab_A2 & make sure priority got updated |
|
63 window_A.gBrowser.selectedTab = tab_A2; |
|
64 isWindowState(window_A, [0, -10, 0]); |
|
65 |
|
66 window_A.gBrowser.removeTab(tab_A2); |
|
67 // Next tab is auto selected |
|
68 isWindowState(window_A, [0, -10]); |
|
69 |
|
70 // Open another window then play with focus |
|
71 let window_B = openDialog(location, "_blank", "chrome,all,dialog=no", "http://example.com"); |
|
72 window_B.addEventListener("load", function(aEvent) { |
|
73 window_B.removeEventListener("load", arguments.callee, false); |
|
74 window_B.gBrowser.addEventListener("load", function(aEvent) { |
|
75 // waitForFocus can attach to the wrong "window" with about:blank loading first |
|
76 // So just ensure that we're getting the load event for the right URI |
|
77 if (window_B.gBrowser.currentURI.spec == "about:blank") |
|
78 return; |
|
79 window_B.gBrowser.removeEventListener("load", arguments.callee, true); |
|
80 |
|
81 waitForFocus(function() { |
|
82 isWindowState(window_A, [10, 0]); |
|
83 isWindowState(window_B, [-10]); |
|
84 |
|
85 waitForFocus(function() { |
|
86 isWindowState(window_A, [0, -10]); |
|
87 isWindowState(window_B, [0]); |
|
88 |
|
89 waitForFocus(function() { |
|
90 isWindowState(window_A, [10, 0]); |
|
91 isWindowState(window_B, [-10]); |
|
92 |
|
93 // And we're done. Cleanup & run the next test |
|
94 window_B.close(); |
|
95 window_A.gBrowser.removeTab(tab_A3); |
|
96 executeSoon(runNextTest); |
|
97 }, window_B); |
|
98 }, window_A); |
|
99 }, window_B); |
|
100 }, true); |
|
101 }, false); |
|
102 }, true); |
|
103 |
|
104 } |
|
105 |
|
106 |
|
107 // This is more a test of nsLoadGroup and how it handles priorities. But since |
|
108 // we depend on its behavior, it's good to test it. This is testing that there |
|
109 // are no errors if we adjust beyond nsISupportsPriority's bounds. |
|
110 function test_extremePriorities() { |
|
111 let tab_A1 = gBrowser.tabContainer.getItemAtIndex(0); |
|
112 let oldPriority = getPriority(tab_A1); |
|
113 |
|
114 // Set the priority of tab_A1 to the lowest possible. Selecting the other tab |
|
115 // will try to lower it |
|
116 setPriority(tab_A1, Ci.nsISupportsPriority.PRIORITY_LOWEST); |
|
117 |
|
118 let tab_A2 = gBrowser.addTab("http://example.com"); |
|
119 tab_A2.linkedBrowser.addEventListener("load", function(aEvent) { |
|
120 tab_A2.linkedBrowser.removeEventListener("load", arguments.callee, true); |
|
121 gBrowser.selectedTab = tab_A2; |
|
122 is(getPriority(tab_A1), Ci.nsISupportsPriority.PRIORITY_LOWEST - PRIORITY_DELTA, |
|
123 "Can adjust priority beyond 'lowest'"); |
|
124 |
|
125 // Now set priority to "highest" and make sure that no errors occur. |
|
126 setPriority(tab_A1, Ci.nsISupportsPriority.PRIORITY_HIGHEST); |
|
127 gBrowser.selectedTab = tab_A1; |
|
128 |
|
129 is(getPriority(tab_A1), Ci.nsISupportsPriority.PRIORITY_HIGHEST + PRIORITY_DELTA, |
|
130 "Can adjust priority beyond 'highest'"); |
|
131 |
|
132 // Cleanup, run next test |
|
133 gBrowser.removeTab(tab_A2); |
|
134 executeSoon(function() { |
|
135 setPriority(tab_A1, oldPriority); |
|
136 runNextTest(); |
|
137 }); |
|
138 |
|
139 }, true); |
|
140 } |
|
141 |
|
142 |
|
143 let tests = [test_behavior, test_extremePriorities]; |
|
144 function runNextTest() { |
|
145 if (tests.length) { |
|
146 // Linux has problems if window isn't focused. Should help prevent [orange]. |
|
147 waitForFocus(tests.shift()); |
|
148 } else { |
|
149 finish(); |
|
150 } |
|
151 } |
|
152 |
|
153 runNextTest(); |
|
154 } |