summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/extensions/test/browser/browser_ext_composeAction_popup_click.js
blob: 2c858cf8abb594bc48a5b19cfee4d3c8aded84c3 (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 Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

let account;

add_setup(async () => {
  account = createAccount();
  addIdentity(account);
});

// This test clicks on the action button to open the popup.
add_task(async function test_popup_open_with_click() {
  for (let area of [null, "formattoolbar"]) {
    let composeWindow = await openComposeWindow(account);
    await focusWindow(composeWindow);

    await run_popup_test({
      actionType: "compose_action",
      testType: "open-with-mouse-click",
      window: composeWindow,
      default_area: area,
    });

    await run_popup_test({
      actionType: "compose_action",
      testType: "open-with-mouse-click",
      window: composeWindow,
      default_area: area,
      disable_button: true,
    });

    await run_popup_test({
      actionType: "compose_action",
      testType: "open-with-mouse-click",
      window: composeWindow,
      default_area: area,
      use_default_popup: true,
    });

    composeWindow.close();
    Services.xulStore.removeDocument(
      "chrome://messenger/content/messengercompose/messengercompose.xhtml"
    );
  }
});

let background_for_openPopup_tests = async () => {
  let composeTab = await browser.compose.beginNew();
  browser.test.assertTrue(!!composeTab, "should have found a compose tab");

  let windows = await browser.windows.getAll();
  let composeWindow = windows.find(window => window.type == "messageCompose");
  browser.test.assertTrue(
    !!composeWindow,
    "should have found a compose window"
  );

  // The test starts with an opened composeWindow, the compose_action
  // is allowed there and should be visible, openPopup() should succeed.
  browser.test.assertTrue(
    (await browser.windows.get(composeWindow.id)).focused,
    "composeWindow should be focused"
  );
  browser.test.assertTrue(
    await browser.composeAction.openPopup(),
    "openPopup() should have succeeded while the compose window is active"
  );
  await window.waitForMessage();

  // Disable the compose_action, openPopup() should fail.
  await browser.composeAction.disable();
  browser.test.assertFalse(
    await browser.composeAction.openPopup(),
    "openPopup() should have failed after the action button was disabled"
  );

  // Enable the compose_action, openPopup() should succeed.
  await browser.composeAction.enable();
  browser.test.assertTrue(
    await browser.composeAction.openPopup(),
    "openPopup() should have succeeded after the action button was enabled again"
  );
  await window.waitForMessage();

  // Create a popup window, which does not have a compose_action, openPopup()
  // should fail.
  let popupWindow = await browser.windows.create({
    type: "popup",
    url: "https://www.example.com",
  });
  browser.test.assertTrue(
    (await browser.windows.get(popupWindow.id)).focused,
    "popupWindow should be focused"
  );
  browser.test.assertFalse(
    await browser.composeAction.openPopup(),
    "openPopup() should have failed while the popup window is active"
  );

  // Specifically open the compose_action of the compose window, should become
  // focused and openPopup() should succeed.
  browser.test.assertTrue(
    await browser.composeAction.openPopup({
      windowId: composeWindow.id,
    }),
    "openPopup() should have succeeded when explicitly requesting the compose window"
  );
  await window.waitForMessage();
  browser.test.assertTrue(
    (await browser.windows.get(composeWindow.id)).focused,
    "composeWindow should be focused"
  );

  // The compose window is focused now, openPopup() should succeed.
  browser.test.assertTrue(
    await browser.composeAction.openPopup(),
    "openPopup() should have succeeded while the compose window is active"
  );
  await window.waitForMessage();

  // Collapse the toolbar, openPopup() should fail.
  await window.sendMessage("collapseToolbar", true);
  browser.test.assertFalse(
    await browser.composeAction.openPopup(),
    "openPopup() should have failed while the toolbar is collapsed"
  );

  // Restore the toolbar, openPopup() should succeed.
  await window.sendMessage("collapseToolbar", false);
  browser.test.assertTrue(
    await browser.composeAction.openPopup(),
    "openPopup() should have succeeded after the toolbar is restored"
  );
  await window.waitForMessage();

  // Close the popup window and finish
  await browser.windows.remove(popupWindow.id);
  await browser.windows.remove(composeWindow.id);
  browser.test.notifyPass("finished");
};

// This test uses openPopup() to open the popup in a compose window.
add_task(
  async function test_popup_open_with_openPopup_in_compose_maintoolbar() {
    let files = {
      "background.js": background_for_openPopup_tests,
      "utils.js": await getUtilsJS(),
      "popup.html": `<!DOCTYPE html>
      <html>
        <head>
          <title>Popup</title>
        </head>
        <body>
          <p>Hello</p>
          <script src="popup.js"></script>
        </body>
      </html>`,
      "popup.js": async function () {
        browser.test.sendMessage("popup opened");
        window.close();
      },
    };
    let extension = ExtensionTestUtils.loadExtension({
      files,
      useAddonManager: "temporary",
      manifest: {
        applications: {
          gecko: {
            id: "compose_action_openPopup@mochi.test",
          },
        },
        background: { scripts: ["utils.js", "background.js"] },
        compose_action: {
          default_title: "default",
          default_popup: "popup.html",
        },
      },
    });

    extension.onMessage("popup opened", async () => {
      // Wait a moment to make sure the popup has closed.
      // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
      await new Promise(r => window.setTimeout(r, 150));
      extension.sendMessage();
    });

    extension.onMessage("collapseToolbar", state => {
      let window = Services.wm.getMostRecentWindow("msgcompose");
      let toolbar = window.document.getElementById("composeToolbar2");
      if (state) {
        toolbar.setAttribute("collapsed", "true");
      } else {
        toolbar.removeAttribute("collapsed");
      }
      extension.sendMessage();
    });

    await extension.startup();
    await extension.awaitFinish("finished");
    await extension.unload();
  }
);

// This test uses openPopup() to open the popup in a compose window.
add_task(
  async function test_popup_open_with_openPopup_in_compose_formatoolbar() {
    let files = {
      "background.js": background_for_openPopup_tests,
      "utils.js": await getUtilsJS(),
      "popup.html": `<!DOCTYPE html>
      <html>
        <head>
          <title>Popup</title>
        </head>
        <body>
          <p>Hello</p>
          <script src="popup.js"></script>
        </body>
      </html>`,
      "popup.js": async function () {
        browser.test.sendMessage("popup opened");
        window.close();
      },
    };
    let extension = ExtensionTestUtils.loadExtension({
      files,
      useAddonManager: "temporary",
      manifest: {
        applications: {
          gecko: {
            id: "compose_action_openPopup@mochi.test",
          },
        },
        background: { scripts: ["utils.js", "background.js"] },
        compose_action: {
          default_title: "default",
          default_popup: "popup.html",
          default_area: "formattoolbar",
        },
      },
    });

    extension.onMessage("popup opened", async () => {
      // Wait a moment to make sure the popup has closed.
      // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
      await new Promise(r => window.setTimeout(r, 150));
      extension.sendMessage();
    });

    extension.onMessage("collapseToolbar", state => {
      let window = Services.wm.getMostRecentWindow("msgcompose");
      let toolbar = window.document.getElementById("FormatToolbar");
      if (state) {
        toolbar.setAttribute("collapsed", "true");
      } else {
        toolbar.removeAttribute("collapsed");
      }
      extension.sendMessage();
    });

    await extension.startup();
    await extension.awaitFinish("finished");
    await extension.unload();
  }
);