browser/base/content/test/general/browser_tabfocus.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/test/general/browser_tabfocus.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,278 @@
     1.4 +/*
     1.5 + * This test checks that focus is adjusted properly when switching tabs.
     1.6 + */
     1.7 +
     1.8 +let testPage1 = "data:text/html,<html id='tab1'><body><button id='button1'>Tab 1</button></body></html>";
     1.9 +let testPage2 = "data:text/html,<html id='tab2'><body><button id='button2'>Tab 2</button></body></html>";
    1.10 +let testPage3 = "data:text/html,<html id='tab3'><body><button id='button3'>Tab 3</button></body></html>";
    1.11 +
    1.12 +function test() {
    1.13 +  waitForExplicitFinish();
    1.14 +
    1.15 +  var tab1 = gBrowser.addTab();
    1.16 +  var browser1 = gBrowser.getBrowserForTab(tab1);
    1.17 +
    1.18 +  var tab2 = gBrowser.addTab();
    1.19 +  var browser2 = gBrowser.getBrowserForTab(tab2);
    1.20 +
    1.21 +  gURLBar.focus();
    1.22 +
    1.23 +  var loadCount = 0;
    1.24 +  function check()
    1.25 +  {
    1.26 +    // wait for both tabs to load
    1.27 +    if (++loadCount != 2)
    1.28 +      return;
    1.29 +
    1.30 +    browser1.removeEventListener("load", check, true);
    1.31 +    browser2.removeEventListener("load", check, true);
    1.32 +    executeSoon(_run_focus_tests);
    1.33 +  }
    1.34 +
    1.35 +  function _run_focus_tests() {
    1.36 +    window.focus();
    1.37 +
    1.38 +    _browser_tabfocus_test_lastfocus = gURLBar;
    1.39 +    _browser_tabfocus_test_lastfocuswindow = window;
    1.40 +
    1.41 +    window.addEventListener("focus", _browser_tabfocus_test_eventOccured, true);
    1.42 +    window.addEventListener("blur", _browser_tabfocus_test_eventOccured, true);
    1.43 +
    1.44 +    // make sure that the focus initially starts out blank
    1.45 +    var fm = Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager);
    1.46 +    var focusedWindow = {};
    1.47 +    is(fm.getFocusedElementForWindow(browser1.contentWindow, false, focusedWindow), null, "initial focus in tab 1");
    1.48 +    is(focusedWindow.value, browser1.contentWindow, "initial frame focus in tab 1");
    1.49 +    is(fm.getFocusedElementForWindow(browser2.contentWindow, false, focusedWindow), null, "initial focus in tab 2");
    1.50 +    is(focusedWindow.value, browser2.contentWindow, "initial frame focus in tab 2");
    1.51 +
    1.52 +    expectFocusShift(function () gBrowser.selectedTab = tab2,
    1.53 +                     browser2.contentWindow, null, true,
    1.54 +                     "focusedElement after tab change, focus in new tab");
    1.55 +
    1.56 +    // switching tabs when nothing in the new tab is focused
    1.57 +    // should focus the browser
    1.58 +    expectFocusShift(function () gBrowser.selectedTab = tab1,
    1.59 +                     browser1.contentWindow, null, true,
    1.60 +                     "focusedElement after tab change, focus in new tab");
    1.61 +
    1.62 +    // focusing a button in the current tab should focus it
    1.63 +    var button1 = browser1.contentDocument.getElementById("button1");
    1.64 +    expectFocusShift(function () button1.focus(),
    1.65 +                     browser1.contentWindow, button1, true,
    1.66 +                     "focusedWindow after focus in focused tab");
    1.67 +
    1.68 +    // focusing a button in a background tab should not change the actual
    1.69 +    // focus, but should set the focus that would be in that background tab to
    1.70 +    // that button.
    1.71 +    var button2 = browser2.contentDocument.getElementById("button2");
    1.72 +    button2.focus();
    1.73 +
    1.74 +    expectFocusShift(function () button2.focus(),
    1.75 +                     browser1.contentWindow, button1, false,
    1.76 +                     "focusedWindow after focus in unfocused tab");
    1.77 +    is(fm.getFocusedElementForWindow(browser2.contentWindow, false, {}), button2, "focus in unfocused tab");
    1.78 +
    1.79 +    // switching tabs should now make the button in the other tab focused
    1.80 +    expectFocusShift(function () gBrowser.selectedTab = tab2,
    1.81 +                     browser2.contentWindow, button2, true,
    1.82 +                     "focusedWindow after tab change");
    1.83 +
    1.84 +    // blurring an element in a background tab should not change the active
    1.85 +    // focus, but should clear the focus in that tab.
    1.86 +    expectFocusShift(function () button1.blur(),
    1.87 +                     browser2.contentWindow, button2, false,
    1.88 +                     "focusedWindow after blur in unfocused tab");
    1.89 +    is(fm.getFocusedElementForWindow(browser1.contentWindow, false, {}), null, "blur in unfocused tab");
    1.90 +
    1.91 +    // When focus is in the tab bar, it should be retained there
    1.92 +    expectFocusShift(function () gBrowser.selectedTab.focus(),
    1.93 +                     window, gBrowser.selectedTab, true,
    1.94 +                     "focusing tab element");
    1.95 +    expectFocusShift(function () gBrowser.selectedTab = tab1,
    1.96 +                     window, tab1, true,
    1.97 +                     "tab change when selected tab element was focused");
    1.98 +    expectFocusShift(function () gBrowser.selectedTab = tab2,
    1.99 +                     window, tab2, true,
   1.100 +                     "tab change when selected tab element was focused");
   1.101 +    expectFocusShift(function () gBrowser.selectedTab.blur(),
   1.102 +                     window, null, true,
   1.103 +                     "blurring tab element");
   1.104 +
   1.105 +    // focusing the url field should switch active focus away from the browser but
   1.106 +    // not clear what would be the focus in the browser
   1.107 +    button1.focus();
   1.108 +    expectFocusShift(function () gURLBar.focus(),
   1.109 +                     window, gURLBar.inputField, true,
   1.110 +                     "focusedWindow after url field focused");
   1.111 +    is(fm.getFocusedElementForWindow(browser2.contentWindow, false, {}), button2, "url field focused, button in browser");
   1.112 +    expectFocusShift(function () gURLBar.blur(),
   1.113 +                     window, null, true,
   1.114 +                     "blurring url field");
   1.115 +
   1.116 +    // when a chrome element is focused, switching tabs to a tab with a button
   1.117 +    // with the current focus should focus the button
   1.118 +    expectFocusShift(function () gBrowser.selectedTab = tab1,
   1.119 +                     browser1.contentWindow, button1, true,
   1.120 +                     "focusedWindow after tab change, focus in url field, button focused in new tab");
   1.121 +    is(fm.getFocusedElementForWindow(browser2.contentWindow, false, {}), button2, "after switch tab, focus in unfocused tab");
   1.122 +
   1.123 +    // blurring an element in the current tab should clear the active focus
   1.124 +    expectFocusShift(function () button1.blur(),
   1.125 +                     browser1.contentWindow, null, true,
   1.126 +                     "focusedWindow after blur in focused tab");
   1.127 +
   1.128 +    // blurring an non-focused url field should have no effect
   1.129 +    expectFocusShift(function () gURLBar.blur(),
   1.130 +                     browser1.contentWindow, null, false,
   1.131 +                     "focusedWindow after blur in unfocused url field");
   1.132 +
   1.133 +    // switch focus to a tab with a currently focused element
   1.134 +    expectFocusShift(function () gBrowser.selectedTab = tab2,
   1.135 +                     browser2.contentWindow, button2, true,
   1.136 +                     "focusedWindow after switch from unfocused to focused tab");
   1.137 +
   1.138 +    // clearing focus on the chrome window should switch the focus to the
   1.139 +    // chrome window
   1.140 +    expectFocusShift(function () fm.clearFocus(window),
   1.141 +                     window, null, true,
   1.142 +                     "focusedWindow after switch to chrome with no focused element");
   1.143 +
   1.144 +    // switch focus to another tab when neither have an active focus
   1.145 +    expectFocusShift(function () gBrowser.selectedTab = tab1,
   1.146 +                     browser1.contentWindow, null, true,
   1.147 +                     "focusedWindow after tab switch from no focus to no focus");
   1.148 +
   1.149 +    gURLBar.focus();
   1.150 +    _browser_tabfocus_test_events = "";
   1.151 +    _browser_tabfocus_test_lastfocus = gURLBar;
   1.152 +    _browser_tabfocus_test_lastfocuswindow = window;
   1.153 +
   1.154 +    expectFocusShift(function () EventUtils.synthesizeKey("VK_F6", { }),
   1.155 +                     browser1.contentWindow, browser1.contentDocument.documentElement,
   1.156 +                     true, "switch document forward with f6");
   1.157 +    EventUtils.synthesizeKey("VK_F6", { });
   1.158 +    is(fm.focusedWindow, window, "switch document forward again with f6");
   1.159 +
   1.160 +    browser1.style.MozUserFocus = "ignore";
   1.161 +    browser1.clientWidth;
   1.162 +    EventUtils.synthesizeKey("VK_F6", { });
   1.163 +    is(fm.focusedWindow, window, "switch document forward again with f6 when browser non-focusable");
   1.164 +
   1.165 +    window.removeEventListener("focus", _browser_tabfocus_test_eventOccured, true);
   1.166 +    window.removeEventListener("blur", _browser_tabfocus_test_eventOccured, true);
   1.167 +
   1.168 +    // next, check whether navigating forward, focusing the urlbar and then
   1.169 +    // navigating back maintains the focus in the urlbar.
   1.170 +    browser1.addEventListener("pageshow", _browser_tabfocus_navigation_test_eventOccured, true);
   1.171 +    button1.focus();
   1.172 +    browser1.contentWindow.location = testPage3;
   1.173 +  }
   1.174 +
   1.175 +  browser1.addEventListener("load", check, true);
   1.176 +  browser2.addEventListener("load", check, true);
   1.177 +  browser1.contentWindow.location = testPage1;
   1.178 +  browser2.contentWindow.location = testPage2;
   1.179 +}
   1.180 +
   1.181 +var _browser_tabfocus_test_lastfocus;
   1.182 +var _browser_tabfocus_test_lastfocuswindow = null;
   1.183 +var _browser_tabfocus_test_events = "";
   1.184 +
   1.185 +function _browser_tabfocus_test_eventOccured(event)
   1.186 +{
   1.187 +  var id;
   1.188 +  if (event.target instanceof Window)
   1.189 +    id = event.originalTarget.document.documentElement.id + "-window";
   1.190 +  else if (event.target instanceof Document)
   1.191 +    id = event.originalTarget.documentElement.id + "-document";
   1.192 +  else if (event.target.id == "urlbar" && event.originalTarget.localName == "input")
   1.193 +    id = "urlbar";
   1.194 +  else
   1.195 +    id = event.originalTarget.id;
   1.196 +
   1.197 +  if (_browser_tabfocus_test_events)
   1.198 +    _browser_tabfocus_test_events += " ";
   1.199 +  _browser_tabfocus_test_events += event.type + ": " + id;
   1.200 +}
   1.201 +
   1.202 +function _browser_tabfocus_navigation_test_eventOccured(event)
   1.203 +{
   1.204 +  if (event.target instanceof Document) {
   1.205 +    var contentwin = event.target.defaultView;
   1.206 +    if (contentwin.location.toString().indexOf("3") > 0) {
   1.207 +      // just moved forward, so focus the urlbar and go back
   1.208 +      gURLBar.focus();
   1.209 +      setTimeout(function () contentwin.history.back(), 0);
   1.210 +    }
   1.211 +    else if (contentwin.location.toString().indexOf("2") > 0) {
   1.212 +      event.currentTarget.removeEventListener("pageshow", _browser_tabfocus_navigation_test_eventOccured, true);
   1.213 +      is(window.document.activeElement, gURLBar.inputField, "urlbar still focused after navigating back");
   1.214 +      gBrowser.removeCurrentTab();
   1.215 +      gBrowser.removeCurrentTab();
   1.216 +      finish();
   1.217 +    }
   1.218 +  }
   1.219 +}
   1.220 +
   1.221 +function getId(element)
   1.222 +{
   1.223 +  return (element.localName == "input") ? "urlbar" : element.id;
   1.224 +}
   1.225 +
   1.226 +function expectFocusShift(callback, expectedWindow, expectedElement, focusChanged, testid)
   1.227 +{
   1.228 +  var expectedEvents = "";
   1.229 +  if (focusChanged) {
   1.230 +    if (_browser_tabfocus_test_lastfocus)
   1.231 +      expectedEvents += "blur: " + getId(_browser_tabfocus_test_lastfocus);
   1.232 +
   1.233 +    if (_browser_tabfocus_test_lastfocuswindow &&
   1.234 +        _browser_tabfocus_test_lastfocuswindow != expectedWindow) {
   1.235 +      if (expectedEvents)
   1.236 +        expectedEvents += " ";
   1.237 +      var windowid = _browser_tabfocus_test_lastfocuswindow.document.documentElement.id;
   1.238 +      expectedEvents += "blur: " + windowid + "-document " +
   1.239 +                        "blur: " + windowid + "-window";
   1.240 +    }
   1.241 +
   1.242 +    if (expectedWindow && _browser_tabfocus_test_lastfocuswindow != expectedWindow) {
   1.243 +      if (expectedEvents)
   1.244 +        expectedEvents += " ";
   1.245 +      var windowid = expectedWindow.document.documentElement.id;
   1.246 +      expectedEvents += "focus: " + windowid + "-document " +
   1.247 +                        "focus: " + windowid + "-window";
   1.248 +    }
   1.249 +
   1.250 +    if (expectedElement && expectedElement != expectedElement.ownerDocument.documentElement) {
   1.251 +      if (expectedEvents)
   1.252 +        expectedEvents += " ";
   1.253 +      expectedEvents += "focus: " + getId(expectedElement);
   1.254 +    }
   1.255 +
   1.256 +    _browser_tabfocus_test_lastfocus = expectedElement;
   1.257 +    _browser_tabfocus_test_lastfocuswindow = expectedWindow;
   1.258 +  }
   1.259 +
   1.260 +  callback();
   1.261 +
   1.262 +  is(_browser_tabfocus_test_events, expectedEvents, testid + " events");
   1.263 +  _browser_tabfocus_test_events = "";
   1.264 +
   1.265 +  var fm = Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager);
   1.266 +
   1.267 +  var focusedElement = fm.focusedElement;
   1.268 +  is(focusedElement ? getId(focusedElement) : "none",
   1.269 +     expectedElement ? getId(expectedElement) : "none", testid + " focusedElement");
   1.270 +  is(fm.focusedWindow, expectedWindow, testid + " focusedWindow");
   1.271 +  var focusedWindow = {};
   1.272 +  is(fm.getFocusedElementForWindow(expectedWindow, false, focusedWindow),
   1.273 +     expectedElement, testid + " getFocusedElementForWindow");
   1.274 +  is(focusedWindow.value, expectedWindow, testid + " getFocusedElementForWindow frame");
   1.275 +  is(expectedWindow.document.hasFocus(), true, testid + " hasFocus");
   1.276 +  var expectedActive = expectedElement;
   1.277 +  if (!expectedActive)
   1.278 +    expectedActive = expectedWindow.document instanceof XULDocument ?
   1.279 +                     expectedWindow.document.documentElement : expectedWindow.document.body;
   1.280 +  is(expectedWindow.document.activeElement, expectedActive, testid + " activeElement");
   1.281 +}

mercurial