summaryrefslogtreecommitdiffstats
path: root/mobile/android/components/extensions/test/mochitest/test_ext_options_ui.html
blob: 138bb054a95e2c2b605a418239a90f968a4bb29e (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>PageAction Test</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

async function waitAboutAddonsRendered(addonId) {
  await ContentTaskUtils.waitForCondition(() => {
    return content.document.querySelector(`div.addon-item[addonID="${addonId}"]`);
  }, `wait Addon Item for ${addonId} to be rendered`);
}

async function navigateToAddonDetails(addonId) {
  const item = content.document.querySelector(`div.addon-item[addonID="${addonId}"]`);
  const rect = item.getBoundingClientRect();
  const x = rect.left + rect.width / 2;
  const y = rect.top + rect.height / 2;
  const domWinUtils = content.window.windowUtils;

  domWinUtils.sendMouseEventToWindow("mousedown", x, y, 0, 1, 0);
  domWinUtils.sendMouseEventToWindow("mouseup", x, y, 0, 1, 0);
}

async function waitAddonOptionsPage([addonId, expectedText]) {
  await ContentTaskUtils.waitForCondition(() => {
    const optionsIframe = content.document.querySelector(`#addon-options`);
    return optionsIframe && optionsIframe.contentDocument.readyState === "complete" &&
           optionsIframe.contentDocument.body.innerText.includes(expectedText);
  }, `wait Addon Options ${expectedText} for ${addonId} to be loaded`);

  const optionsIframe = content.document.querySelector(`#addon-options`);

  return {
    iframeHeight: optionsIframe.style.height,
    documentHeight: optionsIframe.contentDocument.documentElement.scrollHeight,
    bodyHeight: optionsIframe.contentDocument.body.scrollHeight,
  };
}

async function clickOnLinkInOptionsPage(selector) {
  const optionsIframe = content.document.querySelector(`#addon-options`);
  optionsIframe.contentDocument.querySelector(selector).click();
}

async function clickAddonOptionButton() {
  content.document.querySelector(`button#open-addon-options`).click();
}

async function navigateBack() {
  content.window.history.back();
}

function waitDOMContentLoaded(checkUrlCb) {
  const {BrowserApp} = Services.wm.getMostRecentWindow("navigator:browser");

  return new Promise(resolve => {
    const listener = (event) => {
      if (checkUrlCb(event.target.defaultView.location.href)) {
        BrowserApp.deck.removeEventListener("DOMContentLoaded", listener);
        resolve();
      }
    };

    BrowserApp.deck.addEventListener("DOMContentLoaded", listener);
  });
}

function waitAboutAddonsLoaded() {
  return waitDOMContentLoaded(url => url === "about:addons");
}

function clickAddonDisable() {
  content.document.querySelector("#disable-btn").click();
}

function clickAddonEnable() {
  content.document.querySelector("#enable-btn").click();
}

add_task(async function test_options_ui_iframe_height() {
  const addonID = "test-options-ui@mozilla.org";

  const extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    manifest: {
      browser_specific_settings: {
        gecko: {id: addonID},
      },
      name: "Options UI Extension",
      description: "Longer addon description",
      options_ui: {
        page: "options.html",
      },
    },
    files: {
      // An option page with the document element bigger than the body.
      "options.html": `<!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
            <style>
              html { height: 500px; border: 1px solid black; }
              body { height: 200px; }
            </style>
          </head>
          <body>
            <h1>Options page 1</h1>
            <a href="options2.html">go to page 2</a>
          </body>
        </html>
      `,
      // A second option page with the body element bigger than the document.
      "options2.html": `<!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
            <style>
              html { height: 200px; border: 1px solid black; }
              body { height: 350px; }
            </style>
          </head>
          <body>
            <h1>Options page 2</h1>
          </body>
        </html>
      `,
    },
  });

  await extension.startup();

  const {BrowserApp} = Services.wm.getMostRecentWindow("navigator:browser");

  const onceAboutAddonsLoaded = waitAboutAddonsLoaded();

  BrowserApp.addTab("about:addons", {
    selected: true,
    parentId: BrowserApp.selectedTab.id,
  });

  await onceAboutAddonsLoaded;

  is(BrowserApp.selectedTab.currentURI.spec, "about:addons",
     "about:addons is the currently selected tab");

  await SpecialPowers.spawn(BrowserApp.selectedTab.browser, [addonID], waitAboutAddonsRendered);

  await SpecialPowers.spawn(BrowserApp.selectedTab.browser, [addonID], navigateToAddonDetails);

  const optionsSizes = await SpecialPowers.spawn(
    BrowserApp.selectedTab.browser, [[addonID, "Options page 1"]], waitAddonOptionsPage
  );

  ok(parseInt(optionsSizes.iframeHeight, 10) >= 500,
     "The addon options iframe is at least 500px");

  is(optionsSizes.iframeHeight, optionsSizes.documentHeight + "px",
     "The addon options iframe has the expected height");

  await SpecialPowers.spawn(BrowserApp.selectedTab.browser, ["a"], clickOnLinkInOptionsPage);

  const options2Sizes = await SpecialPowers.spawn(
    BrowserApp.selectedTab.browser, [[addonID, "Options page 2"]], waitAddonOptionsPage
  );

  // The second option page has a body bigger than the document element
  // and we expect the iframe to be bigger than that.
  ok(parseInt(options2Sizes.iframeHeight, 10) > 200,
     `The iframe is bigger then 200px (${options2Sizes.iframeHeight})`);

  // The second option page has a body smaller than the document element of the first
  // page and we expect the iframe to be smaller than for the previous options page.
  ok(parseInt(options2Sizes.iframeHeight, 10) < 500,
     `The iframe is smaller then 500px (${options2Sizes.iframeHeight})`);

  is(options2Sizes.iframeHeight, options2Sizes.documentHeight + "px",
     "The second addon options page has the expected height");

  await SpecialPowers.spawn(BrowserApp.selectedTab.browser, [], navigateBack);

  const backToOptionsSizes =  await SpecialPowers.spawn(
    BrowserApp.selectedTab.browser, [[addonID, "Options page 1"]], waitAddonOptionsPage
  );

  // After going back to the first options page,
  // we expect the iframe to have the same size of the previous load.
  is(backToOptionsSizes.iframeHeight, optionsSizes.iframeHeight,
     `When navigating back, the old iframe size is restored (${backToOptionsSizes.iframeHeight})`);

  BrowserApp.closeTab(BrowserApp.selectedTab);

  await extension.unload();
});

add_task(async function test_options_ui_open_aboutaddons_details() {
  const addonID = "test-options-ui-open-addon-details@mozilla.org";

  function background() {
    browser.test.onMessage.addListener(msg => {
      if (msg !== "runtime.openOptionsPage") {
        browser.test.fail(`Received unexpected test message: ${msg}`);
        return;
      }

      browser.runtime.openOptionsPage();
    });
  }

  function optionsScript() {
    browser.test.sendMessage("options-page-loaded", window.location.href);
  }

  const extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    background,
    manifest: {
      browser_specific_settings: {
        gecko: {id: addonID},
      },
      name: "Options UI open addon details Extension",
      description: "Longer addon description",
      options_ui: {
        page: "options.html",
      },
    },
    files: {
      "options.js": optionsScript,
      "options.html": `<!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
          </head>
          <body>
            <h1>Options page</h1>
            <script src="options.js"><\/script>
          </body>
        </html>
      `,
    },
  });

  await extension.startup();

  const {BrowserApp} = Services.wm.getMostRecentWindow("navigator:browser");

  const onceAboutAddonsLoaded = waitAboutAddonsLoaded();

  BrowserApp.addTab("about:addons", {
    selected: true,
    parentId: BrowserApp.selectedTab.id,
  });

  await onceAboutAddonsLoaded;

  is(BrowserApp.selectedTab.currentURI.spec, "about:addons",
     "about:addons is the currently selected tab");

  info("Wait runtime.openOptionsPage to open the about:addond details in the existent tab");
  extension.sendMessage("runtime.openOptionsPage");
  await extension.awaitMessage("options-page-loaded");

  is(BrowserApp.selectedTab.currentURI.spec, "about:addons",
     "about:addons is still the currently selected tab once the options has been loaded");

  BrowserApp.closeTab(BrowserApp.selectedTab);

  await extension.unload();
});

add_task(async function test_options_ui_open_in_tab() {
  const addonID = "test-options-ui@mozilla.org";

  function background() {
    browser.test.onMessage.addListener(msg => {
      if (msg !== "runtime.openOptionsPage") {
        browser.test.fail(`Received unexpected test message: ${msg}`);
        return;
      }

      browser.runtime.openOptionsPage();
    });
  }

  function optionsScript() {
    browser.test.sendMessage("options-page-loaded", window.location.href);
  }

  const extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    background,
    manifest: {
      browser_specific_settings: {
        gecko: {id: addonID},
      },
      name: "Options UI open_in_tab Extension",
      description: "Longer addon description",
      options_ui: {
        page: "options.html",
        open_in_tab: true,
      },
    },
    files: {
      "options.js": optionsScript,
      "options.html": `<!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
          </head>
          <body>
            <h1>Options page</h1>
            <script src="options.js"><\/script>
          </body>
        </html>
      `,
    },
  });

  await extension.startup();

  const {BrowserApp} = Services.wm.getMostRecentWindow("navigator:browser");

  const onceAboutAddonsLoaded = waitAboutAddonsLoaded();

  BrowserApp.selectOrAddTab("about:addons", {
    selected: true,
    parentId: BrowserApp.selectedTab.id,
  });

  await onceAboutAddonsLoaded;

  const aboutAddonsTab = BrowserApp.selectedTab;

  is(aboutAddonsTab.currentURI.spec, "about:addons",
     "about:addons is the currently selected tab");

  await SpecialPowers.spawn(aboutAddonsTab.browser, [addonID], waitAboutAddonsRendered);
  await SpecialPowers.spawn(aboutAddonsTab.browser, [addonID], navigateToAddonDetails);

  const onceAddonOptionsLoaded = waitDOMContentLoaded(url => url.endsWith("options.html"));

  info("Click the Options button in the addon details");
  await SpecialPowers.spawn(aboutAddonsTab.browser, [], clickAddonOptionButton);

  info("Waiting that the addon options are loaded in a new tab");
  await onceAddonOptionsLoaded;

  const addonOptionsTab = BrowserApp.selectedTab;

  ok(aboutAddonsTab.id !== addonOptionsTab.id,
     "The Addon Options page has been loaded in a new tab");

  let optionsURL = await extension.awaitMessage("options-page-loaded");

  is(addonOptionsTab.currentURI.spec, optionsURL,
     "Got the expected extension url opened in the addon options tab");

  const waitTabClosed = (nativeTab) => {
    return new Promise(resolve => {
      const {BrowserApp} = Services.wm.getMostRecentWindow("navigator:browser");
      const expectedBrowser = nativeTab.browser;

      const tabCloseListener = (event) => {
        const browser = event.target;
        if (browser !== expectedBrowser) {
          return;
        }

        BrowserApp.deck.removeEventListener("TabClose", tabCloseListener);
        resolve();
      };

      BrowserApp.deck.addEventListener("TabClose", tabCloseListener);
    });
  };

  const onceOptionsTabClosed = waitTabClosed(addonOptionsTab);
  const onceAboutAddonsClosed = waitTabClosed(aboutAddonsTab);

  info("Close the opened about:addons and options tab");
  BrowserApp.closeTab(addonOptionsTab);
  BrowserApp.closeTab(aboutAddonsTab);

  info("Wait the tabs to be closed");
  await Promise.all([onceOptionsTabClosed, onceAboutAddonsClosed]);

  const oldSelectedTab = BrowserApp.selectedTab;
  info("Call runtime.openOptionsPage");
  extension.sendMessage("runtime.openOptionsPage");

  info("Wait runtime.openOptionsPage to open the options in a new tab");
  optionsURL = await extension.awaitMessage("options-page-loaded");
  is(BrowserApp.selectedTab.currentURI.spec, optionsURL,
     "runtime.openOptionsPage has opened the expected extension page");
  ok(BrowserApp.selectedTab !== oldSelectedTab,
     "runtime.openOptionsPage has opened a new tab");

  BrowserApp.closeTab(BrowserApp.selectedTab);

  await extension.unload();
});

add_task(async function test_options_ui_on_disable_and_enable() {
  // Temporarily disabled for races.
  /* eslint-disable no-unreachable */
  return;

  const addonID = "test-options-ui-disable-enable@mozilla.org";

  function optionsScript() {
    browser.test.sendMessage("options-page-loaded", window.location.href);
  }

  const extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    manifest: {
      browser_specific_settings: {
        gecko: {id: addonID},
      },
      name: "Options UI open addon details Extension",
      description: "Longer addon description",
      options_ui: {
        page: "options.html",
      },
    },
    files: {
      "options.js": optionsScript,
      "options.html": `<!DOCTYPE html>
       <html>
         <head>
           <meta charset="utf-8">
         </head>
         <body>
           <h1>Options page</h1>
           <script src="options.js"><\/script>
         </body>
       </html>
     `,
    },
  });

  await extension.startup();

  const {BrowserApp} = Services.wm.getMostRecentWindow("navigator:browser");

  const onceAboutAddonsLoaded = waitAboutAddonsLoaded();

  BrowserApp.addTab("about:addons", {
    selected: true,
    parentId: BrowserApp.selectedTab.id,
  });

  await onceAboutAddonsLoaded;

  const aboutAddonsTab = BrowserApp.selectedTab;

  is(aboutAddonsTab.currentURI.spec, "about:addons",
     "about:addons is the currently selected tab");

  info("Wait the addon details to have been loaded");
  await SpecialPowers.spawn(aboutAddonsTab.browser, [addonID], waitAboutAddonsRendered);
  await SpecialPowers.spawn(aboutAddonsTab.browser, [addonID], navigateToAddonDetails);

  info("Wait the addon options page to have been loaded");
  await extension.awaitMessage("options-page-loaded");

  info("Click the addon disable button");
  await SpecialPowers.spawn(aboutAddonsTab.browser, [], clickAddonDisable);

  // NOTE: Currently after disabling the addon the extension.awaitMessage seems
  // to fail be able to receive events coming from the browser.test.sendMessage API
  // (nevertheless `await extension.unload()` seems to be able to remove the extension),
  // falling back to wait for the options page to be loaded here.
  const onceAddonOptionsLoaded = waitDOMContentLoaded(url => url.endsWith("options.html"));

  info("Click the addon enable button");
  await SpecialPowers.spawn(aboutAddonsTab.browser, [], clickAddonEnable);

  info("Wait the addon options page to have been loaded after clicking the addon enable button");
  await onceAddonOptionsLoaded;

  BrowserApp.closeTab(BrowserApp.selectedTab);

  await extension.unload();
});

</script>

</body>
</html>