summaryrefslogtreecommitdiffstats
path: root/accessible/tests/mochitest/browser.js
blob: b08214cfa824ac89e5e2570afba23aaf5cee716a (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
/* import-globals-from common.js */

var { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);

/**
 * Load the browser with the given url and then invokes the given function.
 */
function openBrowserWindow(aFunc, aURL, aRect) {
  gBrowserContext.testFunc = aFunc;
  gBrowserContext.startURL = aURL;
  gBrowserContext.browserRect = aRect;

  addLoadEvent(openBrowserWindowIntl);
}

/**
 * Close the browser window.
 */
function closeBrowserWindow() {
  gBrowserContext.browserWnd.close();
}

/**
 * Return the browser window object.
 */
function browserWindow() {
  return gBrowserContext.browserWnd;
}

/**
 * Return the document of the browser window.
 */
function browserDocument() {
  return browserWindow().document;
}

/**
 * Return tab browser object.
 */
function tabBrowser() {
  return browserWindow().gBrowser;
}

/**
 * Return browser element of the current tab.
 */
function currentBrowser() {
  return tabBrowser().selectedBrowser;
}

/**
 * Return DOM document of the current tab.
 */
function currentTabDocument() {
  return currentBrowser().contentDocument;
}

/**
 * Return window of the current tab.
 */
function currentTabWindow() {
  return currentTabDocument().defaultView;
}

/**
 * Return browser element of the tab at the given index.
 */
function browserAt(aIndex) {
  return tabBrowser().getBrowserAtIndex(aIndex);
}

/**
 * Return DOM document of the tab at the given index.
 */
function tabDocumentAt(aIndex) {
  return browserAt(aIndex).contentDocument;
}

/**
 * Return input element of address bar.
 */
function urlbarInput() {
  return browserWindow().document.getElementById("urlbar").inputField;
}

/**
 * Return reload button.
 */
function reloadButton() {
  return browserWindow().document.getElementById("urlbar-reload-button");
}

// //////////////////////////////////////////////////////////////////////////////
// private section

var gBrowserContext = {
  browserWnd: null,
  testFunc: null,
  startURL: "",
};

function openBrowserWindowIntl() {
  var params = "chrome,all,dialog=no,non-remote";
  var rect = gBrowserContext.browserRect;
  if (rect) {
    if ("left" in rect) {
      params += ",left=" + rect.left;
    }
    if ("top" in rect) {
      params += ",top=" + rect.top;
    }
    if ("width" in rect) {
      params += ",width=" + rect.width;
    }
    if ("height" in rect) {
      params += ",height=" + rect.height;
    }
  }

  gBrowserContext.browserWnd =
    window.browsingContext.topChromeWindow.openDialog(
      AppConstants.BROWSER_CHROME_URL,
      "_blank",
      params,
      gBrowserContext.startURL || "data:text/html,<html></html>"
    );

  whenDelayedStartupFinished(browserWindow(), function () {
    addA11yLoadEvent(startBrowserTests, browserWindow());
  });
}

function startBrowserTests() {
  if (gBrowserContext.startURL) {
    // Make sure the window is the one loading our URL.
    if (currentBrowser().contentWindow.location != gBrowserContext.startURL) {
      setTimeout(startBrowserTests, 0);
      return;
    }
    // wait for load
    addA11yLoadEvent(gBrowserContext.testFunc, currentBrowser().contentWindow);
  } else {
    gBrowserContext.testFunc();
  }
}

function whenDelayedStartupFinished(aWindow, aCallback) {
  Services.obs.addObserver(function observer(aSubject, aTopic) {
    if (aWindow == aSubject) {
      Services.obs.removeObserver(observer, aTopic);
      setTimeout(aCallback, 0);
    }
  }, "browser-delayed-startup-finished");
}