browser/modules/test/browser_NetworkPrioritizer.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial