dom/tests/browser/browser_autofocus_background.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/browser/browser_autofocus_background.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,55 @@
     1.4 +function test() {
     1.5 +  waitForExplicitFinish();
     1.6 +
     1.7 +  let fm = Components.classes["@mozilla.org/focus-manager;1"]
     1.8 +                     .getService(Components.interfaces.nsIFocusManager);
     1.9 +
    1.10 +  let tabs = [ gBrowser.selectedTab, gBrowser.addTab() ];
    1.11 +
    1.12 +  // The first tab has an autofocused element.
    1.13 +  // The second tab is exactly like the first one without the autofocus.
    1.14 +  let testingList = [
    1.15 +    { uri: "data:text/html,<!DOCTYPE html><html><body><input autofocus id='target'></body></html>",
    1.16 +      tagName: "INPUT"},
    1.17 +  ];
    1.18 +
    1.19 +  function runTest() {
    1.20 +    // Set the focus to the first tab.
    1.21 +    tabs[0].linkedBrowser.focus();
    1.22 +
    1.23 +    // Load the first tab on background.
    1.24 +    tabs[1].linkedBrowser.addEventListener("load", onLoadBackgroundTab, true);
    1.25 +    tabs[1].linkedBrowser.loadURI(testingList[0].uri);
    1.26 +  }
    1.27 +
    1.28 +  function onLoadBackgroundTab() {
    1.29 +    tabs[1].linkedBrowser.removeEventListener("load", onLoadBackgroundTab, true);
    1.30 +
    1.31 +    // The focus event (from autofocus) has been sent,
    1.32 +    // let's test with executeSoon so we are sure the test is done
    1.33 +    // after the focus event is processed.
    1.34 +    executeSoon(doTest);
    1.35 +  }
    1.36 +
    1.37 +  function doTest() {
    1.38 +    for (var i=0; i<testingList.length; ++i) {
    1.39 +      // Get active element in the tab.
    1.40 +      var e = tabs[i+1].linkedBrowser.contentDocument.activeElement;
    1.41 +
    1.42 +      is(e.tagName, testingList[i].tagName,
    1.43 +         "The background tab's focused element should be " +
    1.44 +         testingList[i].tagName);
    1.45 +      isnot(fm.focusedElement, e,
    1.46 +            "The background tab's focused element should not be the focus " +
    1.47 +            "manager focused element");
    1.48 +    }
    1.49 +
    1.50 +    // Cleaning up.
    1.51 +    for (let i = 1; i < tabs.length; i++) {
    1.52 +      gBrowser.removeTab(tabs[i]);
    1.53 +    }
    1.54 +    finish();
    1.55 +  }
    1.56 +
    1.57 +  runTest();
    1.58 +}

mercurial