michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function test() { michael@0: /** Tests for NetworkPrioritizer.jsm (Bug 514490) **/ michael@0: michael@0: waitForExplicitFinish(); michael@0: michael@0: const PRIORITY_DELTA = -10; // same as in NetworkPrioritizer michael@0: michael@0: // Test helper functions. michael@0: // getPriority and setPriority can take a Tab or a Browser michael@0: function getPriority(aBrowser) { michael@0: // Assume we were passed a tab if it's not a browser michael@0: if (!aBrowser.webNavigation) michael@0: aBrowser = aBrowser.linkedBrowser; michael@0: return aBrowser.webNavigation.QueryInterface(Ci.nsIDocumentLoader) michael@0: .loadGroup.QueryInterface(Ci.nsISupportsPriority).priority; michael@0: } michael@0: function setPriority(aBrowser, aPriority) { michael@0: if (!aBrowser.webNavigation) michael@0: aBrowser = aBrowser.linkedBrowser; michael@0: aBrowser.webNavigation.QueryInterface(Ci.nsIDocumentLoader) michael@0: .loadGroup.QueryInterface(Ci.nsISupportsPriority).priority = aPriority; michael@0: } michael@0: michael@0: function isWindowState(aWindow, aTabPriorities) { michael@0: let browsers = aWindow.gBrowser.browsers; michael@0: // Make sure we have the right number of tabs & priorities michael@0: is(browsers.length, aTabPriorities.length, michael@0: "Window has expected number of tabs"); michael@0: // aState should be in format [ priority, priority, priority ] michael@0: for (let i = 0; i < browsers.length; i++) { michael@0: is(getPriority(browsers[i]), aTabPriorities[i], michael@0: "Tab had expected priority"); michael@0: } michael@0: } michael@0: michael@0: michael@0: // This is the real test. It creates multiple tabs & windows, changes focus, michael@0: // closes windows/tabs to make sure we behave correctly. michael@0: // This test assumes that no priorities have been adjusted and the loadgroup michael@0: // priority starts at 0. michael@0: function test_behavior() { michael@0: michael@0: // Call window "window_A" to make the test easier to follow michael@0: let window_A = window; michael@0: michael@0: // Test 1 window, 1 tab case. michael@0: isWindowState(window_A, [-10]); michael@0: michael@0: // Exising tab is tab_A1 michael@0: let tab_A2 = window_A.gBrowser.addTab("http://example.com"); michael@0: let tab_A3 = window_A.gBrowser.addTab("about:config"); michael@0: tab_A3.linkedBrowser.addEventListener("load", function(aEvent) { michael@0: tab_A3.linkedBrowser.removeEventListener("load", arguments.callee, true); michael@0: michael@0: // tab_A2 isn't focused yet michael@0: isWindowState(window_A, [-10, 0, 0]); michael@0: michael@0: // focus tab_A2 & make sure priority got updated michael@0: window_A.gBrowser.selectedTab = tab_A2; michael@0: isWindowState(window_A, [0, -10, 0]); michael@0: michael@0: window_A.gBrowser.removeTab(tab_A2); michael@0: // Next tab is auto selected michael@0: isWindowState(window_A, [0, -10]); michael@0: michael@0: // Open another window then play with focus michael@0: let window_B = openDialog(location, "_blank", "chrome,all,dialog=no", "http://example.com"); michael@0: window_B.addEventListener("load", function(aEvent) { michael@0: window_B.removeEventListener("load", arguments.callee, false); michael@0: window_B.gBrowser.addEventListener("load", function(aEvent) { michael@0: // waitForFocus can attach to the wrong "window" with about:blank loading first michael@0: // So just ensure that we're getting the load event for the right URI michael@0: if (window_B.gBrowser.currentURI.spec == "about:blank") michael@0: return; michael@0: window_B.gBrowser.removeEventListener("load", arguments.callee, true); michael@0: michael@0: waitForFocus(function() { michael@0: isWindowState(window_A, [10, 0]); michael@0: isWindowState(window_B, [-10]); michael@0: michael@0: waitForFocus(function() { michael@0: isWindowState(window_A, [0, -10]); michael@0: isWindowState(window_B, [0]); michael@0: michael@0: waitForFocus(function() { michael@0: isWindowState(window_A, [10, 0]); michael@0: isWindowState(window_B, [-10]); michael@0: michael@0: // And we're done. Cleanup & run the next test michael@0: window_B.close(); michael@0: window_A.gBrowser.removeTab(tab_A3); michael@0: executeSoon(runNextTest); michael@0: }, window_B); michael@0: }, window_A); michael@0: }, window_B); michael@0: }, true); michael@0: }, false); michael@0: }, true); michael@0: michael@0: } michael@0: michael@0: michael@0: // This is more a test of nsLoadGroup and how it handles priorities. But since michael@0: // we depend on its behavior, it's good to test it. This is testing that there michael@0: // are no errors if we adjust beyond nsISupportsPriority's bounds. michael@0: function test_extremePriorities() { michael@0: let tab_A1 = gBrowser.tabContainer.getItemAtIndex(0); michael@0: let oldPriority = getPriority(tab_A1); michael@0: michael@0: // Set the priority of tab_A1 to the lowest possible. Selecting the other tab michael@0: // will try to lower it michael@0: setPriority(tab_A1, Ci.nsISupportsPriority.PRIORITY_LOWEST); michael@0: michael@0: let tab_A2 = gBrowser.addTab("http://example.com"); michael@0: tab_A2.linkedBrowser.addEventListener("load", function(aEvent) { michael@0: tab_A2.linkedBrowser.removeEventListener("load", arguments.callee, true); michael@0: gBrowser.selectedTab = tab_A2; michael@0: is(getPriority(tab_A1), Ci.nsISupportsPriority.PRIORITY_LOWEST - PRIORITY_DELTA, michael@0: "Can adjust priority beyond 'lowest'"); michael@0: michael@0: // Now set priority to "highest" and make sure that no errors occur. michael@0: setPriority(tab_A1, Ci.nsISupportsPriority.PRIORITY_HIGHEST); michael@0: gBrowser.selectedTab = tab_A1; michael@0: michael@0: is(getPriority(tab_A1), Ci.nsISupportsPriority.PRIORITY_HIGHEST + PRIORITY_DELTA, michael@0: "Can adjust priority beyond 'highest'"); michael@0: michael@0: // Cleanup, run next test michael@0: gBrowser.removeTab(tab_A2); michael@0: executeSoon(function() { michael@0: setPriority(tab_A1, oldPriority); michael@0: runNextTest(); michael@0: }); michael@0: michael@0: }, true); michael@0: } michael@0: michael@0: michael@0: let tests = [test_behavior, test_extremePriorities]; michael@0: function runNextTest() { michael@0: if (tests.length) { michael@0: // Linux has problems if window isn't focused. Should help prevent [orange]. michael@0: waitForFocus(tests.shift()); michael@0: } else { michael@0: finish(); michael@0: } michael@0: } michael@0: michael@0: runNextTest(); michael@0: }