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