dom/tests/browser/browser_focus_steal_from_chrome.js

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 function test() {
     2   waitForExplicitFinish();
     4   let secMan = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
     5                          .getService(Components.interfaces
     6                                                .nsIScriptSecurityManager);
     8   let fm = Components.classes["@mozilla.org/focus-manager;1"]
     9                      .getService(Components.interfaces.nsIFocusManager);
    11   let tabs = [ gBrowser.addTab(), gBrowser.addTab() ];
    12   gBrowser.selectedTab = tabs[0];
    14   let testingList = [
    15     { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><input id='target'></body>",
    16       tagName: "INPUT", methodName: "focus" },
    17     { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').select(); }, 10);\"><input id='target'></body>",
    18       tagName: "INPUT", methodName: "select" },
    19     { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><a href='about:blank' id='target'>anchor</a></body>",
    20       tagName: "A", methodName: "focus" },
    21     { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><button id='target'>button</button></body>",
    22       tagName: "BUTTON", methodName: "focus" },
    23     { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><select id='target'><option>item1</option></select></body>",
    24       tagName: "SELECT", methodName: "focus" },
    25     { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><textarea id='target'>textarea</textarea></body>",
    26       tagName: "TEXTAREA", methodName: "focus" },
    27     { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').select(); }, 10);\"><textarea id='target'>textarea</textarea></body>",
    28       tagName: "TEXTAREA", methodName: "select" },
    29     { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><label id='target'><input></label></body>",
    30       tagName: "INPUT", methodName: "focus of label element" },
    31     { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><fieldset><legend id='target'>legend</legend><input></fieldset></body>",
    32       tagName: "INPUT", methodName: "focus of legend element" },
    33     { uri: "data:text/html,<body onload=\"setTimeout(function () {" +
    34            "  var element = document.getElementById('target');" +
    35            "  var event = document.createEvent('MouseEvent');" +
    36            "  event.initMouseEvent('click', true, true, window," +
    37            "    1, 0, 0, 0, 0, false, false, false, false, 0, element);" +
    38            "  element.dispatchEvent(event); }, 10);\">" +
    39            "<label id='target'><input></label></body>",
    40       tagName: "INPUT", methodName: "click event on the label element" },
    41   ];
    43   if (navigator.platform.indexOf("Mac") == -1) {
    44     // clicking buttons doesn't focus on mac, so skip this test
    45     testingList.push(
    46       { uri: "data:text/html,<body onload=\"setTimeout(function () {" +
    47              "  var element = document.getElementById('target');" +
    48              "  var event = document.createEvent('MouseEvent');" +
    49              "  event.initMouseEvent('mousedown', true, true, window," +
    50              "    0, 0, 0, 0, 0, false, false, false, false, 0, element);" +
    51              "  element.dispatchEvent(event); }, 10);\">" +
    52              "<button id='target'>button</button></body>",
    53         tagName: "BUTTON", methodName: "mousedown event on the button element" });
    54   }
    56   let testingIndex = -1;
    57   let canRetry;
    58   let callback;
    59   let loadedCount;
    60   let setFocusToChrome;
    62   function runNextTest() {
    63     if (++testingIndex >= testingList.length) {
    64       // cleaning-up...
    65       for (let i = 0; i < tabs.length; i++) {
    66         gBrowser.removeTab(tabs[i]);
    67       }
    68       finish();
    69       return;
    70     }
    71     callback = doTest1;
    72     loadTestPage(false);
    73   }
    75   function loadTestPage(aSetFocusToChrome) {
    76     loadedCount = 0;
    77     canRetry = 10;
    78     setFocusToChrome = aSetFocusToChrome;
    79     // Set the focus to the contents.
    80     tabs[0].linkedBrowser.focus();
    81     // Load on the tabs
    82     tabs[0].linkedBrowser.addEventListener("load", onLoadForegroundTab, true);
    83     tabs[0].linkedBrowser.loadURI(testingList[testingIndex].uri);
    84     tabs[1].linkedBrowser.addEventListener("load", onLoadBackgroundTab, true);
    85     tabs[1].linkedBrowser.loadURI(testingList[testingIndex].uri);
    86   }
    88   function onLoadForegroundTab() {
    89     tabs[0].linkedBrowser.removeEventListener("load", onLoadForegroundTab, true);
    90     if (setFocusToChrome) {
    91       // Set focus to a chrome element before the loaded content tries to move
    92       // focus.
    93       document.getElementById("urlbar").focus();
    94     }
    95     onLoadComplete();
    96   }
    98   function onLoadBackgroundTab() {
    99     tabs[1].linkedBrowser.removeEventListener("load", onLoadBackgroundTab, true);
   100     onLoadComplete();
   101   }
   103   function onLoadComplete() {
   104     if (++loadedCount == tabs.length) {
   105       setTimeout(callback, 20);
   106     }
   107   }
   109   function isPrepared() {
   110     for (let i = 0; i < tabs.length; i++) {
   111       if (tabs[i].linkedBrowser.contentDocument.activeElement.tagName !=
   112             testingList[testingIndex].tagName) {
   113         return false;
   114       }
   115     }
   116     return true;
   117   }
   119   function doTest1() {
   120     if (canRetry-- > 0 && !isPrepared()) {
   121       setTimeout(callback, 10); // retry
   122       return;
   123     }
   125     // The contents should be able to steal the focus from content.
   127     // in foreground tab
   128     let e = tabs[0].linkedBrowser.contentDocument.activeElement;
   129     is(e.tagName, testingList[testingIndex].tagName,
   130        "the foreground tab's " + testingList[testingIndex].tagName +
   131        " element is not active by the " + testingList[testingIndex].methodName +
   132        " (Test1: content can steal focus)");
   133     is(fm.focusedElement, e,
   134        "the " + testingList[testingIndex].tagName +
   135        " element isn't focused by the " + testingList[testingIndex].methodName +
   136        " (Test1: content can steal focus)");
   138     // in background tab
   139     e = tabs[1].linkedBrowser.contentDocument.activeElement;
   140     is(e.tagName, testingList[testingIndex].tagName,
   141        "the background tab's " + testingList[testingIndex].tagName +
   142        " element is not active by the " + testingList[testingIndex].methodName +
   143        " (Test1: content can steal focus)");
   144     isnot(fm.focusedElement, e,
   145           "the " + testingList[testingIndex].tagName +
   146           " element is focused by the " + testingList[testingIndex].methodName +
   147           " (Test1: content can steal focus)");
   149     callback = doTest2;
   150     loadTestPage(true);
   151   }
   154   function doTest2() {
   155     if (canRetry-- > 0 && !isPrepared()) {
   156       setTimeout(callback, 10); // retry
   157       return;
   158     }
   160     // The contents shouldn't be able to steal the focus from chrome.
   162     // in foreground tab
   163     let e = tabs[0].linkedBrowser.contentDocument.activeElement;
   164     is(e.tagName, testingList[testingIndex].tagName,
   165        "the foreground tab's " + testingList[testingIndex].tagName +
   166        " element is not active by the " + testingList[testingIndex].methodName +
   167        " (Test2: content can NOT steal focus)");
   168     isnot(fm.focusedElement, e,
   169           "the " + testingList[testingIndex].tagName +
   170           " element is focused by the " + testingList[testingIndex].methodName +
   171           " (Test2: content can NOT steal focus)");
   173     // in background tab
   174     e = tabs[1].linkedBrowser.contentDocument.activeElement;
   175     is(e.tagName, testingList[testingIndex].tagName,
   176        "the background tab's " + testingList[testingIndex].tagName +
   177        " element is not active by the " + testingList[testingIndex].methodName +
   178        " (Test2: content can NOT steal focus)");
   179     isnot(fm.focusedElement, e,
   180           "the " + testingList[testingIndex].tagName +
   181           " element is focused by the " + testingList[testingIndex].methodName +
   182           " (Test2: content can NOT steal focus)");
   184     runNextTest();
   185   }
   187   runNextTest();
   188 }

mercurial