summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_windows_create.js
blob: 6c41abcd3e519eebf85650d66b6f70a3dfeaf014 (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

add_task(async function testWindowCreate() {
  let extension = ExtensionTestUtils.loadExtension({
    async background() {
      let _checkWindowPromise;
      browser.test.onMessage.addListener(msg => {
        if (msg == "checked-window") {
          _checkWindowPromise.resolve();
          _checkWindowPromise = null;
        }
      });

      let os;

      function checkWindow(expected) {
        return new Promise(resolve => {
          _checkWindowPromise = { resolve };
          browser.test.sendMessage("check-window", expected);
        });
      }

      async function createWindow(params, expected, keep = false) {
        let window = await browser.windows.create(...params);
        // params is null when testing create without createData
        params = params[0] || {};

        // Prevent frequent intermittent failures on macos where the newly created window
        // may have not always got into the fullscreen state before browser.window.create
        // resolves the windows details.
        if (
          os === "mac" &&
          params.state === "fullscreen" &&
          window.state !== params.state
        ) {
          browser.test.log(
            "Wait for window.state for the newly create window to be set to fullscreen"
          );
          while (window.state !== params.state) {
            window = await browser.windows.get(window.id, { populate: true });
          }
          browser.test.log(
            "Newly created browser window got into fullscreen state"
          );
        }

        for (let key of Object.keys(params)) {
          if (key == "state" && os == "mac" && params.state == "normal") {
            // OS-X doesn't have a hard distinction between "normal" and
            // "maximized" states.
            browser.test.assertTrue(
              window.state == "normal" || window.state == "maximized",
              `Expected window.state (currently ${window.state}) to be "normal" but will accept "maximized"`
            );
          } else {
            browser.test.assertEq(
              params[key],
              window[key],
              `Got expected value for window.${key}`
            );
          }
        }

        browser.test.assertEq(
          1,
          window.tabs.length,
          "tabs property got populated"
        );

        await checkWindow(expected);
        if (keep) {
          return window;
        }

        if (params.state == "fullscreen" && os == "win") {
          // FIXME: Closing a fullscreen window causes a window leak in
          // Windows tests.
          await browser.windows.update(window.id, { state: "normal" });
        }
        await browser.windows.remove(window.id);
      }

      try {
        ({ os } = await browser.runtime.getPlatformInfo());

        // Set the current window to state: "normal" because the test is failing on Windows
        // where the current window is maximized.
        let currentWindow = await browser.windows.getCurrent();
        await browser.windows.update(currentWindow.id, { state: "normal" });

        await createWindow([], { state: "STATE_NORMAL" });
        await createWindow([{ state: "maximized" }], {
          state: "STATE_MAXIMIZED",
        });
        await createWindow([{ state: "minimized" }], {
          state: "STATE_MINIMIZED",
        });
        await createWindow([{ state: "normal" }], {
          state: "STATE_NORMAL",
          hiddenChrome: [],
        });
        await createWindow([{ state: "fullscreen" }], {
          state: "STATE_FULLSCREEN",
        });

        let window = await createWindow(
          [{ type: "popup" }],
          {
            hiddenChrome: [
              "menubar",
              "toolbar",
              "location",
              "directories",
              "status",
              "extrachrome",
            ],
            chromeFlags: ["CHROME_OPENAS_DIALOG"],
          },
          true
        );

        let tabs = await browser.tabs.query({
          windowType: "popup",
          active: true,
        });

        browser.test.assertEq(1, tabs.length, "Expected only one popup");
        browser.test.assertEq(
          window.id,
          tabs[0].windowId,
          "Expected new window to be returned in query"
        );

        await browser.windows.remove(window.id);

        browser.test.notifyPass("window-create");
      } catch (e) {
        browser.test.fail(`${e} :: ${e.stack}`);
        browser.test.notifyFail("window-create");
      }
    },
  });

  let latestWindow;
  let windowListener = (window, topic) => {
    if (topic == "domwindowopened") {
      latestWindow = window;
    }
  };
  Services.ww.registerNotification(windowListener);

  extension.onMessage("check-window", expected => {
    if (expected.state != null) {
      let { windowState } = latestWindow;
      if (latestWindow.fullScreen) {
        windowState = latestWindow.STATE_FULLSCREEN;
      }

      if (expected.state == "STATE_NORMAL") {
        ok(
          windowState == window.STATE_NORMAL ||
            windowState == window.STATE_MAXIMIZED,
          `Expected windowState (currently ${windowState}) to be STATE_NORMAL but will accept STATE_MAXIMIZED`
        );
      } else {
        is(
          windowState,
          window[expected.state],
          `Expected window state to be ${expected.state}`
        );
      }
    }
    if (expected.hiddenChrome) {
      let chromeHidden =
        latestWindow.document.documentElement.getAttribute("chromehidden");
      is(
        chromeHidden.trim().split(/\s+/).sort().join(" "),
        expected.hiddenChrome.sort().join(" "),
        "Got expected hidden chrome"
      );
    }
    if (expected.chromeFlags) {
      let { chromeFlags } = latestWindow.docShell.treeOwner
        .QueryInterface(Ci.nsIInterfaceRequestor)
        .getInterface(Ci.nsIAppWindow);
      for (let flag of expected.chromeFlags) {
        ok(
          chromeFlags & Ci.nsIWebBrowserChrome[flag],
          `Expected window to have the ${flag} flag`
        );
      }
    }

    extension.sendMessage("checked-window");
  });

  await extension.startup();
  await extension.awaitFinish("window-create");
  await extension.unload();

  Services.ww.unregisterNotification(windowListener);
  latestWindow = null;
});