summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_documentnavigation.js
blob: eb789d0766a97283614a7cc6ce7e46a8d8fae57a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * This test checks that focus is adjusted properly in a browser when pressing F6 and Shift+F6.
 * There are additional tests in dom/tests/mochitest/chrome/test_focus_docnav.xul which test
 * non-browser cases.
 */

var testPage1 = "data:text/html,<html id='html1'><body id='body1'><button id='button1'>Tab 1</button></body></html>";
var testPage2 = "data:text/html,<html id='html2'><body id='body2'><button id='button2'>Tab 2</button></body></html>";
var testPage3 = "data:text/html,<html id='html3'><body id='body3' contenteditable='true'><button id='button3'>Tab 3</button></body></html>";

var fm = Services.focus;

function* expectFocusOnF6(backward, expectedDocument, expectedElement, onContent, desc)
{
  let focusChangedInChildResolver = null;
  let focusPromise = onContent ? new Promise(resolve => focusChangedInChildResolver = resolve) :
                                 BrowserTestUtils.waitForEvent(window, "focus", true);

  function focusChangedListener(msg) {
    let expected = expectedDocument;
    if (!expectedElement.startsWith("html")) {
      expected += "," + expectedElement;
    }

    is(msg.data.details, expected, desc + " child focus matches");
    focusChangedInChildResolver();
  }

  if (onContent) {
    messageManager.addMessageListener("BrowserTest:FocusChanged", focusChangedListener);

    yield ContentTask.spawn(gBrowser.selectedBrowser, { expectedElementId: expectedElement }, function* (arg) {
      let contentExpectedElement = content.document.getElementById(arg.expectedElementId);
      if (!contentExpectedElement) {
        // Element not found, so look in the child frames.
        for (let f = 0; f < content.frames.length; f++) {
          if (content.frames[f].document.getElementById(arg.expectedElementId)) {
            contentExpectedElement = content.frames[f].document;
            break;
          }
        }
      }
      else if (contentExpectedElement.localName == "html") {
        contentExpectedElement = contentExpectedElement.ownerDocument;
      }

      if (!contentExpectedElement) {
        sendSyncMessage("BrowserTest:FocusChanged",
                        { details : "expected element " + arg.expectedElementId + " not found" });
        return;
      }

      contentExpectedElement.addEventListener("focus", function focusReceived() {
        contentExpectedElement.removeEventListener("focus", focusReceived, true);

        const contentFM = Components.classes["@mozilla.org/focus-manager;1"].
                            getService(Components.interfaces.nsIFocusManager);
        let details = contentFM.focusedWindow.document.documentElement.id;
        if (contentFM.focusedElement) {
          details += "," + contentFM.focusedElement.id;
        }

        sendSyncMessage("BrowserTest:FocusChanged", { details : details });
      }, true);
    });
  }

  EventUtils.synthesizeKey("VK_F6", { shiftKey: backward });
  yield focusPromise;

  if (typeof expectedElement == "string") {
    expectedElement = fm.focusedWindow.document.getElementById(expectedElement);
  }

  if (gMultiProcessBrowser && onContent) {
    expectedDocument = "main-window";
    expectedElement = gBrowser.selectedBrowser;
  }

  is(fm.focusedWindow.document.documentElement.id, expectedDocument, desc + " document matches");
  is(fm.focusedElement, expectedElement, desc + " element matches");

  if (onContent) {
    messageManager.removeMessageListener("BrowserTest:FocusChanged", focusChangedListener);
  }
}

// Load a page and navigate between it and the chrome window.
add_task(function* ()
{
  let page1Promise = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
  gBrowser.selectedBrowser.loadURI(testPage1);
  yield page1Promise;

  // When the urlbar is focused, pressing F6 should focus the root of the content page.
  gURLBar.focus();
  yield* expectFocusOnF6(false, "html1", "html1",
                                true, "basic focus content page");

  // When the content is focused, pressing F6 should focus the urlbar.
  yield* expectFocusOnF6(false, "main-window", gURLBar.inputField,
                                false, "basic focus content page urlbar");

  // When a button in content is focused, pressing F6 should focus the urlbar.
  yield* expectFocusOnF6(false, "html1", "html1",
                                true, "basic focus content page with button focused");

  yield ContentTask.spawn(gBrowser.selectedBrowser, { }, function* () {
    return content.document.getElementById("button1").focus();
  });

  yield* expectFocusOnF6(false, "main-window", gURLBar.inputField,
                                false, "basic focus content page with button focused urlbar");

  // The document root should be focused, not the button
  yield* expectFocusOnF6(false, "html1", "html1",
                                true, "basic focus again content page with button focused");

  // Check to ensure that the root element is focused
  yield ContentTask.spawn(gBrowser.selectedBrowser, { }, function* () {
    Assert.ok(content.document.activeElement == content.document.documentElement,
      "basic focus again content page with button focused child root is focused");
  });
});

// Open a second tab. Document focus should skip the background tab.
add_task(function* ()
{
  yield BrowserTestUtils.openNewForegroundTab(gBrowser, testPage2);

  yield* expectFocusOnF6(false, "main-window", gURLBar.inputField,
                                false, "basic focus content page and second tab urlbar");
  yield* expectFocusOnF6(false, "html2", "html2",
                                true, "basic focus content page with second tab");

  yield BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

// Shift+F6 should navigate backwards. There's only one document here so the effect
// is the same.
add_task(function* ()
{
  gURLBar.focus();
  yield* expectFocusOnF6(true, "html1", "html1",
                               true, "back focus content page");
  yield* expectFocusOnF6(true, "main-window", gURLBar.inputField,
                               false, "back focus content page urlbar");
});

// Open the sidebar and navigate between the sidebar, content and top-level window
add_task(function* ()
{
  let sidebar = document.getElementById("sidebar");

  let loadPromise = BrowserTestUtils.waitForEvent(sidebar, "load", true);
  SidebarUI.toggle('viewBookmarksSidebar');
  yield loadPromise;


  gURLBar.focus();
  yield* expectFocusOnF6(false, "bookmarksPanel",
                                sidebar.contentDocument.getElementById("search-box").inputField,
                                false, "focus with sidebar open sidebar");
  yield* expectFocusOnF6(false, "html1", "html1",
                                true, "focus with sidebar open content");
  yield* expectFocusOnF6(false, "main-window", gURLBar.inputField,
                                false, "focus with sidebar urlbar");

  // Now go backwards
  yield* expectFocusOnF6(true, "html1", "html1",
                               true, "back focus with sidebar open content");
  yield* expectFocusOnF6(true, "bookmarksPanel",
                               sidebar.contentDocument.getElementById("search-box").inputField,
                               false, "back focus with sidebar open sidebar");
  yield* expectFocusOnF6(true, "main-window", gURLBar.inputField,
                               false, "back focus with sidebar urlbar");

  SidebarUI.toggle('viewBookmarksSidebar');
});

// Navigate when the downloads panel is open
add_task(function* ()
{
  yield pushPrefs(["accessibility.tabfocus", 7]);

  let popupShownPromise = BrowserTestUtils.waitForEvent(document, "popupshown", true);
  EventUtils.synthesizeMouseAtCenter(document.getElementById("downloads-button"), { });
  yield popupShownPromise;

  gURLBar.focus();
  yield* expectFocusOnF6(false, "main-window", document.getElementById("downloadsHistory"),
                                false, "focus with downloads panel open panel");
  yield* expectFocusOnF6(false, "html1", "html1",
                                true, "focus with downloads panel open");
  yield* expectFocusOnF6(false, "main-window", gURLBar.inputField,
                                false, "focus downloads panel open urlbar");

  // Now go backwards
  yield* expectFocusOnF6(true, "html1", "html1",
                               true, "back focus with downloads panel open");
  yield* expectFocusOnF6(true, "main-window", document.getElementById("downloadsHistory"),
                               false, "back focus with downloads panel open");
  yield* expectFocusOnF6(true, "main-window", gURLBar.inputField,
                               false, "back focus downloads panel open urlbar");

  let downloadsPopup = document.getElementById("downloadsPanel");
  let popupHiddenPromise = BrowserTestUtils.waitForEvent(downloadsPopup, "popuphidden", true);
  downloadsPopup.hidePopup();
  yield popupHiddenPromise;
});

// Navigation with a contenteditable body
add_task(function* ()
{
  yield BrowserTestUtils.openNewForegroundTab(gBrowser, testPage3);

  // The body should be focused when it is editable, not the root.
  gURLBar.focus();
  yield* expectFocusOnF6(false, "html3", "body3",
                                true, "focus with contenteditable body");
  yield* expectFocusOnF6(false, "main-window", gURLBar.inputField,
                                false, "focus with contenteditable body urlbar");

  // Now go backwards

  yield* expectFocusOnF6(false, "html3", "body3",
                                true, "back focus with contenteditable body");
  yield* expectFocusOnF6(false, "main-window", gURLBar.inputField,
                                false, "back focus with contenteditable body urlbar");

  yield BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

// Navigation with a frameset loaded
add_task(function* ()
{
  yield BrowserTestUtils.openNewForegroundTab(gBrowser,
    "http://mochi.test:8888/browser/browser/base/content/test/general/file_documentnavigation_frameset.html");

  gURLBar.focus();
  yield* expectFocusOnF6(false, "htmlframe1", "htmlframe1",
                                true, "focus on frameset frame 0");
  yield* expectFocusOnF6(false, "htmlframe2", "htmlframe2",
                                true, "focus on frameset frame 1");
  yield* expectFocusOnF6(false, "htmlframe3", "htmlframe3",
                                true, "focus on frameset frame 2");
  yield* expectFocusOnF6(false, "htmlframe4", "htmlframe4",
                                true, "focus on frameset frame 3");
  yield* expectFocusOnF6(false, "main-window", gURLBar.inputField,
                                false, "focus on frameset frame urlbar");

  yield* expectFocusOnF6(true, "htmlframe4", "htmlframe4",
                               true, "back focus on frameset frame 3");
  yield* expectFocusOnF6(true, "htmlframe3", "htmlframe3",
                               true, "back focus on frameset frame 2");
  yield* expectFocusOnF6(true, "htmlframe2", "htmlframe2",
                               true, "back focus on frameset frame 1");
  yield* expectFocusOnF6(true, "htmlframe1", "htmlframe1",
                               true, "back focus on frameset frame 0");
  yield* expectFocusOnF6(true, "main-window", gURLBar.inputField,
                               false, "back focus on frameset frame urlbar");

  yield BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

// XXXndeakin add tests for browsers inside of panels