summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/tests/browser/browser_waitForFocus.js
blob: 8f9e2758690b29c32b478383cf6c7f195cb24752 (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

const gBaseURL = "https://example.com/browser/testing/mochitest/tests/browser/";

function *promiseTabLoadEvent(tab, url)
{
  return new Promise(function (resolve, reject) {
    function handleLoadEvent(event) {
      if (event.originalTarget != tab.linkedBrowser.contentDocument ||
          event.target.location.href == "about:blank" ||
          (url && event.target.location.href != url)) {
        return;
      }

      tab.linkedBrowser.removeEventListener("load", handleLoadEvent, true);
      resolve(event);
    }

    tab.linkedBrowser.addEventListener("load", handleLoadEvent, true, true);
    if (url)
      tab.linkedBrowser.loadURI(url);
  });
}

// Load a new blank tab
add_task(function *() {
  yield BrowserTestUtils.openNewForegroundTab(gBrowser);

  gURLBar.focus();

  let browser = gBrowser.selectedBrowser;
  yield SimpleTest.promiseFocus(browser.contentWindowAsCPOW, true);

  is(document.activeElement, browser, "Browser is focused when about:blank is loaded");

  gBrowser.removeCurrentTab();
  gURLBar.focus();
});

// Load a tab with a subframe inside it and wait until the subframe is focused
add_task(function *() {
  let tab = gBrowser.addTab();
  gBrowser.selectedTab = tab;

  let browser = gBrowser.getBrowserForTab(tab);
  yield promiseTabLoadEvent(tab, gBaseURL + "waitForFocusPage.html");

  yield SimpleTest.promiseFocus(browser.contentWindowAsCPOW);

  is(document.activeElement, browser, "Browser is focused when page is loaded");

  yield SimpleTest.promiseFocus(browser.contentWindowAsCPOW.frames[0]);

  is(browser.contentWindowAsCPOW.document.activeElement.localName, "iframe", "Child iframe is focused");

  gBrowser.removeCurrentTab();
});

// Pass a browser to promiseFocus
add_task(function *() {
  yield BrowserTestUtils.openNewForegroundTab(gBrowser, gBaseURL + "waitForFocusPage.html");

  gURLBar.focus();

  yield SimpleTest.promiseFocus(gBrowser.selectedBrowser);

  is(document.activeElement, gBrowser.selectedBrowser, "Browser is focused when promiseFocus is passed a browser");

  gBrowser.removeCurrentTab();
});