browser_NetworkPrioritizer.js
Go to the documentation of this file.
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is mozilla.org code.
15  *
16  * The Initial Developer of the Original Code is Mozilla Corporation.
17  * Portions created by the Initial Developer are Copyright (C) 2009
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul O’Shannessy <paul@oshannessy.com> (original author)
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */
36 
37 function test() {
38  /* Permaorange on Linux right now, so disable there. */
39  let osString = Components.classes["@mozilla.org/xre/app-info;1"].
40  getService(Components.interfaces.nsIXULRuntime).OS;
41  if (osString == "Linux")
42  return;
43 
47 
48  const PRIORITY_DELTA = -10; // same as in NetworkPrioritizer
49 
50  // Test helper functions.
51  // getPriority and setPriority can take a Tab or a Browser
52  function getPriority(aBrowser) {
53  // Assume we were passed a tab if it's not a browser
54  if (!aBrowser.webNavigation)
55  aBrowser = aBrowser.linkedBrowser;
56  return aBrowser.webNavigation.QueryInterface(Ci.nsIDocumentLoader)
57  .loadGroup.QueryInterface(Ci.nsISupportsPriority).priority;
58  }
59  function setPriority(aBrowser, aPriority) {
60  if (!aBrowser.webNavigation)
61  aBrowser = aBrowser.linkedBrowser;
62  aBrowser.webNavigation.QueryInterface(Ci.nsIDocumentLoader)
63  .loadGroup.QueryInterface(Ci.nsISupportsPriority).priority = aPriority;
64  }
65 
66  function isWindowState(aWindow, aTabPriorities) {
67  let browsers = aWindow.gBrowser.browsers;
68  // Make sure we have the right number of tabs & priorities
69  is(browsers.length, aTabPriorities.length,
70  "Window has expected number of tabs");
71  // aState should be in format [ priority, priority, priority ]
72  for (let i = 0; i < browsers.length; i++) {
73  is(getPriority(browsers[i]), aTabPriorities[i],
74  "Tab had expected priority");
75  }
76  }
77 
78 
79  // This is the real test. It creates multiple tabs & windows, changes focus,
80  // closes windows/tabs to make sure we behave correctly.
81  // This test assumes that no priorities have been adjusted and the loadgroup
82  // priority starts at 0.
83  function test_behavior() {
84 
85  // Call window "window_A" to make the test easier to follow
86  let window_A = window;
87 
88  // Test 1 window, 1 tab case.
89  isWindowState(window_A, [-10]);
90 
91  // Exising tab is tab_A1
92  let tab_A2 = window_A.gBrowser.addTab("http://example.com");
93  let tab_A3 = window_A.gBrowser.addTab("about:config");
94  tab_A3.linkedBrowser.addEventListener("load", function(aEvent) {
95  tab_A3.removeEventListener("load", arguments.callee, true);
96 
97  // tab_A2 isn't focused yet
98  isWindowState(window_A, [-10, 0, 0]);
99 
100  // focus tab_A2 & make sure priority got updated
101  window_A.gBrowser.selectedTab = tab_A2;
102  isWindowState(window_A, [0, -10, 0]);
103 
104  window_A.gBrowser.removeTab(tab_A2);
105  // Next tab is auto selected
106  isWindowState(window_A, [0, -10]);
107 
108  // Open another window then play with focus
109  let window_B = openDialog(location, "_blank", "chrome,all,dialog=no", "http://example.com");
110  window_B.addEventListener("load", function(aEvent) {
111  window_B.removeEventListener("load", arguments.callee, false);
112  window_B.gBrowser.addEventListener("load", function(aEvent) {
113  // waitForFocus can attach to the wrong "window" with about:blank loading first
114  // So just ensure that we're getting the load event for the right URI
115  if (window_B.gBrowser.currentURI.spec == "about:blank")
116  return;
117  window_B.gBrowser.removeEventListener("load", arguments.callee, true);
118 
119  waitForFocus(function() {
120  isWindowState(window_A, [10, 0]);
121  isWindowState(window_B, [-10]);
122 
123  waitForFocus(function() {
124  isWindowState(window_A, [0, -10]);
125  isWindowState(window_B, [0]);
126 
127  waitForFocus(function() {
128  isWindowState(window_A, [10, 0]);
129  isWindowState(window_B, [-10]);
130 
131  // And we're done. Cleanup & run the next test
132  window_B.close();
133  window_A.gBrowser.removeTab(tab_A3);
134  executeSoon(runNextTest);
135  }, window_B);
136  }, window_A);
137  }, window_B);
138 
139  }, true);
140  }, false);
141  }, true);
142 
143  }
144 
145 
146  // This is more a test of nsLoadGroup and how it handles priorities. But since
147  // we depend on its behavior, it's good to test it. This is testing that there
148  // are no errors if we adjust beyond nsISupportsPriority's bounds.
149  function test_extremePriorities() {
150  let tab_A1 = gBrowser.tabContainer.getItemAtIndex(0);
151  let oldPriority = getPriority(tab_A1);
152 
153  // Set the priority of tab_A1 to the lowest possible. Selecting the other tab
154  // will try to lower it
155  setPriority(tab_A1, Ci.nsISupportsPriority.PRIORITY_LOWEST);
156 
157  let tab_A2 = gBrowser.addTab("http://example.com");
158  tab_A2.linkedBrowser.addEventListener("load", function(aEvent) {
159  tab_A2.removeEventListener("load", arguments.callee, true);
160  gBrowser.selectedTab = tab_A2;
161  is(getPriority(tab_A1), Ci.nsISupportsPriority.PRIORITY_LOWEST - PRIORITY_DELTA,
162  "Can adjust priority beyond 'lowest'");
163 
164  // Now set priority to "highest" and make sure that no errors occur.
165  setPriority(tab_A1, Ci.nsISupportsPriority.PRIORITY_HIGHEST);
166  gBrowser.selectedTab = tab_A1;
167 
168  is(getPriority(tab_A1), Ci.nsISupportsPriority.PRIORITY_HIGHEST + PRIORITY_DELTA,
169  "Can adjust priority beyond 'highest'");
170 
171  // Cleanup, run next test
172  gBrowser.removeTab(tab_A2);
173  executeSoon(function() {
174  setPriority(tab_A1, oldPriority);
175  runNextTest();
176  });
177 
178  }, true);
179  }
180 
181 
182  let tests = [test_behavior, test_extremePriorities];
183  function runNextTest() {
184  if (tests.length) {
185  // Linux has problems if window isn't focused. Should help prevent [orange].
186  waitForFocus(tests.shift());
187  } else {
188  finish();
189  }
190  }
191 
192  runNextTest();
193 }
const PRIORITY_DELTA
getService(Ci.sbIFaceplateManager)
let window
waitForExplicitFinish()
function test()
return!aWindow arguments!aWindow arguments[0]
_updateTextAndScrollDataForTab aBrowser
const Ci
function runNextTest()
_getSelectedPageStyle s i