1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/modules/test/browser_NetworkPrioritizer.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,154 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +function test() { 1.9 + /** Tests for NetworkPrioritizer.jsm (Bug 514490) **/ 1.10 + 1.11 + waitForExplicitFinish(); 1.12 + 1.13 + const PRIORITY_DELTA = -10; // same as in NetworkPrioritizer 1.14 + 1.15 + // Test helper functions. 1.16 + // getPriority and setPriority can take a Tab or a Browser 1.17 + function getPriority(aBrowser) { 1.18 + // Assume we were passed a tab if it's not a browser 1.19 + if (!aBrowser.webNavigation) 1.20 + aBrowser = aBrowser.linkedBrowser; 1.21 + return aBrowser.webNavigation.QueryInterface(Ci.nsIDocumentLoader) 1.22 + .loadGroup.QueryInterface(Ci.nsISupportsPriority).priority; 1.23 + } 1.24 + function setPriority(aBrowser, aPriority) { 1.25 + if (!aBrowser.webNavigation) 1.26 + aBrowser = aBrowser.linkedBrowser; 1.27 + aBrowser.webNavigation.QueryInterface(Ci.nsIDocumentLoader) 1.28 + .loadGroup.QueryInterface(Ci.nsISupportsPriority).priority = aPriority; 1.29 + } 1.30 + 1.31 + function isWindowState(aWindow, aTabPriorities) { 1.32 + let browsers = aWindow.gBrowser.browsers; 1.33 + // Make sure we have the right number of tabs & priorities 1.34 + is(browsers.length, aTabPriorities.length, 1.35 + "Window has expected number of tabs"); 1.36 + // aState should be in format [ priority, priority, priority ] 1.37 + for (let i = 0; i < browsers.length; i++) { 1.38 + is(getPriority(browsers[i]), aTabPriorities[i], 1.39 + "Tab had expected priority"); 1.40 + } 1.41 + } 1.42 + 1.43 + 1.44 + // This is the real test. It creates multiple tabs & windows, changes focus, 1.45 + // closes windows/tabs to make sure we behave correctly. 1.46 + // This test assumes that no priorities have been adjusted and the loadgroup 1.47 + // priority starts at 0. 1.48 + function test_behavior() { 1.49 + 1.50 + // Call window "window_A" to make the test easier to follow 1.51 + let window_A = window; 1.52 + 1.53 + // Test 1 window, 1 tab case. 1.54 + isWindowState(window_A, [-10]); 1.55 + 1.56 + // Exising tab is tab_A1 1.57 + let tab_A2 = window_A.gBrowser.addTab("http://example.com"); 1.58 + let tab_A3 = window_A.gBrowser.addTab("about:config"); 1.59 + tab_A3.linkedBrowser.addEventListener("load", function(aEvent) { 1.60 + tab_A3.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.61 + 1.62 + // tab_A2 isn't focused yet 1.63 + isWindowState(window_A, [-10, 0, 0]); 1.64 + 1.65 + // focus tab_A2 & make sure priority got updated 1.66 + window_A.gBrowser.selectedTab = tab_A2; 1.67 + isWindowState(window_A, [0, -10, 0]); 1.68 + 1.69 + window_A.gBrowser.removeTab(tab_A2); 1.70 + // Next tab is auto selected 1.71 + isWindowState(window_A, [0, -10]); 1.72 + 1.73 + // Open another window then play with focus 1.74 + let window_B = openDialog(location, "_blank", "chrome,all,dialog=no", "http://example.com"); 1.75 + window_B.addEventListener("load", function(aEvent) { 1.76 + window_B.removeEventListener("load", arguments.callee, false); 1.77 + window_B.gBrowser.addEventListener("load", function(aEvent) { 1.78 + // waitForFocus can attach to the wrong "window" with about:blank loading first 1.79 + // So just ensure that we're getting the load event for the right URI 1.80 + if (window_B.gBrowser.currentURI.spec == "about:blank") 1.81 + return; 1.82 + window_B.gBrowser.removeEventListener("load", arguments.callee, true); 1.83 + 1.84 + waitForFocus(function() { 1.85 + isWindowState(window_A, [10, 0]); 1.86 + isWindowState(window_B, [-10]); 1.87 + 1.88 + waitForFocus(function() { 1.89 + isWindowState(window_A, [0, -10]); 1.90 + isWindowState(window_B, [0]); 1.91 + 1.92 + waitForFocus(function() { 1.93 + isWindowState(window_A, [10, 0]); 1.94 + isWindowState(window_B, [-10]); 1.95 + 1.96 + // And we're done. Cleanup & run the next test 1.97 + window_B.close(); 1.98 + window_A.gBrowser.removeTab(tab_A3); 1.99 + executeSoon(runNextTest); 1.100 + }, window_B); 1.101 + }, window_A); 1.102 + }, window_B); 1.103 + }, true); 1.104 + }, false); 1.105 + }, true); 1.106 + 1.107 + } 1.108 + 1.109 + 1.110 + // This is more a test of nsLoadGroup and how it handles priorities. But since 1.111 + // we depend on its behavior, it's good to test it. This is testing that there 1.112 + // are no errors if we adjust beyond nsISupportsPriority's bounds. 1.113 + function test_extremePriorities() { 1.114 + let tab_A1 = gBrowser.tabContainer.getItemAtIndex(0); 1.115 + let oldPriority = getPriority(tab_A1); 1.116 + 1.117 + // Set the priority of tab_A1 to the lowest possible. Selecting the other tab 1.118 + // will try to lower it 1.119 + setPriority(tab_A1, Ci.nsISupportsPriority.PRIORITY_LOWEST); 1.120 + 1.121 + let tab_A2 = gBrowser.addTab("http://example.com"); 1.122 + tab_A2.linkedBrowser.addEventListener("load", function(aEvent) { 1.123 + tab_A2.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.124 + gBrowser.selectedTab = tab_A2; 1.125 + is(getPriority(tab_A1), Ci.nsISupportsPriority.PRIORITY_LOWEST - PRIORITY_DELTA, 1.126 + "Can adjust priority beyond 'lowest'"); 1.127 + 1.128 + // Now set priority to "highest" and make sure that no errors occur. 1.129 + setPriority(tab_A1, Ci.nsISupportsPriority.PRIORITY_HIGHEST); 1.130 + gBrowser.selectedTab = tab_A1; 1.131 + 1.132 + is(getPriority(tab_A1), Ci.nsISupportsPriority.PRIORITY_HIGHEST + PRIORITY_DELTA, 1.133 + "Can adjust priority beyond 'highest'"); 1.134 + 1.135 + // Cleanup, run next test 1.136 + gBrowser.removeTab(tab_A2); 1.137 + executeSoon(function() { 1.138 + setPriority(tab_A1, oldPriority); 1.139 + runNextTest(); 1.140 + }); 1.141 + 1.142 + }, true); 1.143 + } 1.144 + 1.145 + 1.146 + let tests = [test_behavior, test_extremePriorities]; 1.147 + function runNextTest() { 1.148 + if (tests.length) { 1.149 + // Linux has problems if window isn't focused. Should help prevent [orange]. 1.150 + waitForFocus(tests.shift()); 1.151 + } else { 1.152 + finish(); 1.153 + } 1.154 + } 1.155 + 1.156 + runNextTest(); 1.157 +}