browser_tabfocus.js
Go to the documentation of this file.
1 /*
2  * This test checks that focus is adjusted properly when switching tabs.
3  */
4 
5 let testPage1 = "data:text/html,<html id='tab1'><body><button id='button1'>Tab 1</button></body></html>";
6 let testPage2 = "data:text/html,<html id='tab2'><body><button id='button2'>Tab 2</button></body></html>";
7 let testPage3 = "data:text/html,<html id='tab3'><body><button id='button3'>Tab 3</button></body></html>";
8 
9 function test() {
11 
12  var tab1 = gBrowser.addTab();
13  var browser1 = gBrowser.getBrowserForTab(tab1);
14 
15  var tab2 = gBrowser.addTab();
16  var browser2 = gBrowser.getBrowserForTab(tab2);
17 
18  gURLBar.focus();
19 
20  var loadCount = 0;
21  function check()
22  {
23  // wait for both tabs to load
24  if (++loadCount != 2)
25  return;
26 
27  window.focus();
28 
31 
32  window.addEventListener("focus", _browser_tabfocus_test_eventOccured, true);
33  window.addEventListener("blur", _browser_tabfocus_test_eventOccured, true);
34 
35  // make sure that the focus initially starts out blank
36  var fm = Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager);
37  var focusedWindow = {};
38  is(fm.getFocusedElementForWindow(browser1.contentWindow, false, focusedWindow), null, "initial focus in tab 1");
39  is(focusedWindow.value, browser1.contentWindow, "initial frame focus in tab 1");
40  is(fm.getFocusedElementForWindow(browser2.contentWindow, false, focusedWindow), null, "initial focus in tab 2");
41  is(focusedWindow.value, browser2.contentWindow, "initial frame focus in tab 2");
42 
43  expectFocusShift(function () gBrowser.selectedTab = tab2,
44  browser2.contentWindow, null, true,
45  "focusedElement after tab change, focus in new tab");
46 
47  // switching tabs when the urlbar is focused and nothing in the new tab is focused
48  // should focus the browser
49  expectFocusShift(function () gURLBar.focus(),
50  window, gURLBar.inputField, true,
51  "url field focused");
52  expectFocusShift(function () gBrowser.selectedTab = tab1,
53  browser1.contentWindow, null, true,
54  "focusedElement after tab change, focus in new tab");
55 
56  // focusing a button in the current tab should focus it
57  var button1 = browser1.contentWindow.document.getElementById("button1");
58  expectFocusShift(function () button1.focus(),
59  browser1.contentWindow, button1, true,
60  "focusedWindow after focus in focused tab");
61 
62  // focusing a button in a background tab should not change the actual
63  // focus, but should set the focus that would be in that background tab to
64  // that button.
65  var button2 = browser2.contentWindow.document.getElementById("button2");
66  button2.focus();
67 
68  expectFocusShift(function () button2.focus(),
69  browser1.contentWindow, button1, false,
70  "focusedWindow after focus in unfocused tab");
71  is(fm.getFocusedElementForWindow(browser2.contentWindow, false, {}), button2, "focus in unfocused tab");
72 
73  // switching tabs should now make the button in the other tab focused
74  expectFocusShift(function () gBrowser.selectedTab = tab2,
75  browser2.contentWindow, button2, true,
76  "focusedWindow after tab change");
77 
78  // blurring an element in a background tab should not change the active
79  // focus, but should clear the focus in that tab.
80  expectFocusShift(function () button1.blur(),
81  browser2.contentWindow, button2, false,
82  "focusedWindow after blur in unfocused tab");
83  is(fm.getFocusedElementForWindow(browser1.contentWindow, false, {}), null, "blur in unfocused tab");
84 
85  // focusing the url field should switch active focus away from the tab but
86  // not clear what would be the focus in the tab
87  button1.focus();
88  expectFocusShift(function () gURLBar.focus(),
89  window, gURLBar.inputField, true,
90  "focusedWindow after url field focused");
91  is(fm.getFocusedElementForWindow(browser2.contentWindow, false, {}), button2, "url field focused, button in tab");
92 
93  // when a chrome element is focused, switching tabs to a tab with a button
94  // with the current focus should focus the button
95  expectFocusShift(function () gBrowser.selectedTab = tab1,
96  browser1.contentWindow, button1, true,
97  "focusedWindow after tab change, focus in url field, button focused in new tab");
98  is(fm.getFocusedElementForWindow(browser2.contentWindow, false, {}), button2, "after switch tab, focus in unfocused tab");
99 
100  // blurring an element in the current tab should clear the active focus
101  expectFocusShift(function () button1.blur(),
102  browser1.contentWindow, null, true,
103  "focusedWindow after blur in focused tab");
104 
105  // blurring an non-focused url field should have no effect
106  expectFocusShift(function () gURLBar.blur(),
107  browser1.contentWindow, null, false,
108  "focusedWindow after blur in unfocused url field");
109 
110  // switch focus to a tab with a currently focused element
111  expectFocusShift(function () gBrowser.selectedTab = tab2,
112  browser2.contentWindow, button2, true,
113  "focusedWindow after switch from unfocused to focused tab");
114 
115  // clearing focus on the chrome window should switch the focus to the
116  // chrome window
117  expectFocusShift(function () fm.clearFocus(window),
118  window, null, true,
119  "focusedWindow after switch to chrome with no focused element");
120 
121  // switch focus to another tab when neither have an active focus
122  expectFocusShift(function () gBrowser.selectedTab = tab1,
123  browser1.contentWindow, null, true,
124  "focusedWindow after tab switch from no focus to no focus");
125 
126  window.addEventListener("focus", _browser_tabfocus_test_eventOccured, true);
127  window.addEventListener("blur", _browser_tabfocus_test_eventOccured, true);
128 
129  // next, check whether navigating forward, focusing the urlbar and then
130  // navigating back maintains the focus in the urlbar.
131  browser1.addEventListener("pageshow", _browser_tabfocus_navigation_test_eventOccured, true);
132  button1.focus();
133  browser1.contentWindow.location = testPage3;
134  }
135 
136  browser1.addEventListener("load", check, true);
137  browser2.addEventListener("load", check, true);
138  browser1.contentWindow.location = testPage1;
139  browser2.contentWindow.location = testPage2;
140 }
141 
145 
147 {
148  var id;
149  if (event.target instanceof Window)
150  id = event.originalTarget.document.documentElement.id + "-window";
151  else if (event.target instanceof Document)
152  id = event.originalTarget.documentElement.id + "-document";
153  else if (event.target.id == "urlbar" && event.originalTarget.localName == "input")
154  id = "urlbar";
155  else
156  id = event.originalTarget.id;
157 
158  if (_browser_tabfocus_test_events)
159  _browser_tabfocus_test_events += " ";
160  _browser_tabfocus_test_events += event.type + ": " + id;
161 }
162 
164 {
165  if (event.target instanceof Document) {
166  var contentwin = event.target.defaultView;
167  if (contentwin.location.toString().indexOf("3") > 0) {
168  // just moved forward, so focus the urlbar and go back
169  gURLBar.focus();
170  setTimeout(function () contentwin.history.back(), 0);
171  }
172  else if (contentwin.location.toString().indexOf("2") > 0) {
173  is(window.document.activeElement, gURLBar.inputField, "urlbar still focused after navigating back");
174  gBrowser.removeCurrentTab();
175  gBrowser.removeCurrentTab();
176  finish();
177  }
178  }
179 }
180 
181 function getId(element)
182 {
183  return (element.localName == "input") ? "urlbar" : element.id;
184 }
185 
186 function expectFocusShift(callback, expectedWindow, expectedElement, focusChanged, testid)
187 {
188  var expectedEvents = "";
189  if (focusChanged) {
190  if (_browser_tabfocus_test_lastfocus)
191  expectedEvents += "blur: " + getId(_browser_tabfocus_test_lastfocus);
192 
193  if (_browser_tabfocus_test_lastfocuswindow &&
194  _browser_tabfocus_test_lastfocuswindow != expectedWindow) {
195  if (expectedEvents)
196  expectedEvents += " ";
197  var windowid = _browser_tabfocus_test_lastfocuswindow.document.documentElement.id;
198  expectedEvents += "blur: " + windowid + "-document " +
199  "blur: " + windowid + "-window";
200  }
201 
202  if (expectedWindow && _browser_tabfocus_test_lastfocuswindow != expectedWindow) {
203  if (expectedEvents)
204  expectedEvents += " ";
205  var windowid = expectedWindow.document.documentElement.id;
206  expectedEvents += "focus: " + windowid + "-document " +
207  "focus: " + windowid + "-window";
208  }
209 
210  if (expectedElement) {
211  if (expectedEvents)
212  expectedEvents += " ";
213  expectedEvents += "focus: " + getId(expectedElement);
214  }
215 
216  _browser_tabfocus_test_lastfocus = expectedElement;
217  _browser_tabfocus_test_lastfocuswindow = expectedWindow;
218  }
219 
220  callback();
221 
222  is(_browser_tabfocus_test_events, expectedEvents, testid + " events");
223  _browser_tabfocus_test_events = "";
224 
225  var fm = Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager);
226 
227  var focusedElement = fm.focusedElement;
228  is(focusedElement ? getId(focusedElement) : "none",
229  expectedElement ? getId(expectedElement) : "none", testid + " focusedElement");
230  is(fm.focusedWindow, expectedWindow, testid + " focusedWindow");
231  var focusedWindow = {};
232  is(fm.getFocusedElementForWindow(expectedWindow, false, focusedWindow),
233  expectedElement, testid + " getFocusedElementForWindow");
234  is(focusedWindow.value, expectedWindow, testid + " getFocusedElementForWindow frame");
235  is(expectedWindow.document.hasFocus(), true, testid + " hasFocus");
236  var expectedActive = expectedElement;
237  if (!expectedActive)
238  expectedActive = expectedWindow.document instanceof XULDocument ?
239  expectedWindow.document.documentElement : expectedWindow.document.body;
240  is(expectedWindow.document.activeElement, expectedActive, testid + " activeElement");
241 }
const Cc
function _browser_tabfocus_test_eventOccured(event)
menuItem id
Definition: FeedWriter.js:971
function _browser_tabfocus_navigation_test_eventOccured(event)
let testPage3
var event
let testPage1
let window
var _browser_tabfocus_test_lastfocuswindow
function getId(element)
waitForExplicitFinish()
aWindow setTimeout(function(){_this.restoreHistory(aWindow, aTabs, aTabData, aIdMap);}, 0)
function expectFocusShift(callback, expectedWindow, expectedElement, focusChanged, testid)
grep callback
var Window
return null
Definition: FeedWriter.js:1143
function check(ch, cx)
var tab1
var tab2
let testPage2
var _browser_tabfocus_test_lastfocus
function test()
var _browser_tabfocus_test_events
var Document
const Ci