summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/im/test/browser/browser_browserRequest.js
blob: 7ffdb1c725a6ead7187677fd4e4401f005027f37 (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
/* 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/. */

const { InteractiveBrowser, CancelledError } = ChromeUtils.importESModule(
  "resource:///modules/InteractiveBrowser.sys.mjs"
);
const kBaseWindowUri = "chrome://messenger/content/browserRequest.xhtml";

add_task(async function testBrowserRequestObserverNotification() {
  const windowPromise = BrowserTestUtils.domWindowOpenedAndLoaded(
    undefined,
    win => win.document.documentURI === kBaseWindowUri
  );
  let notifyLoaded;
  const loadedPromise = new Promise(resolve => {
    notifyLoaded = resolve;
  });
  const cancelledPromise = new Promise(resolve => {
    Services.obs.notifyObservers(
      {
        promptText: "",
        iconURI: "",
        url: "about:blank",
        cancelled() {
          resolve();
        },
        loaded(window, webProgress) {
          ok(webProgress);
          notifyLoaded(window);
        },
      },
      "browser-request"
    );
  });

  const requestWindow = await windowPromise;
  const loadedWindow = await loadedPromise;
  ok(loadedWindow);
  is(loadedWindow.document.documentURI, kBaseWindowUri);

  const closeEvent = new Event("close");
  requestWindow.dispatchEvent(closeEvent);
  await BrowserTestUtils.closeWindow(requestWindow);

  await cancelledPromise;
});

add_task(async function testWaitForRedirect() {
  const initialUrl = "about:blank";
  const promptText = "just testing";
  const completionUrl = InteractiveBrowser.COMPLETION_URL + "/done?info=foo";
  const windowPromise = BrowserTestUtils.domWindowOpenedAndLoaded(
    undefined,
    win => win.document.documentURI === kBaseWindowUri
  );
  const request = InteractiveBrowser.waitForRedirect(initialUrl, promptText);
  const requestWindow = await windowPromise;
  is(requestWindow.document.title, promptText, "set window title");

  const closedWindow = BrowserTestUtils.domWindowClosed(requestWindow);
  const browser = requestWindow.document.getElementById("requestFrame");
  await BrowserTestUtils.browserLoaded(browser);
  BrowserTestUtils.loadURIString(browser, completionUrl);
  const result = await request;
  is(result, completionUrl, "finished with correct URL");

  await closedWindow;
});

add_task(async function testCancelWaitForRedirect() {
  const initialUrl = "about:blank";
  const promptText = "just testing";
  const windowPromise = BrowserTestUtils.domWindowOpenedAndLoaded(
    undefined,
    win => win.document.documentURI === kBaseWindowUri
  );
  const request = InteractiveBrowser.waitForRedirect(initialUrl, promptText);
  const requestWindow = await windowPromise;
  is(requestWindow.document.title, promptText, "set window title");

  await new Promise(resolve => setTimeout(resolve));

  const closeEvent = new Event("close");
  requestWindow.dispatchEvent(closeEvent);
  await BrowserTestUtils.closeWindow(requestWindow);

  try {
    await request;
    ok(false, "request should be rejected");
  } catch (error) {
    ok(error instanceof CancelledError, "request was rejected");
  }
});

add_task(async function testAlreadyComplete() {
  const completionUrl = InteractiveBrowser.COMPLETION_URL + "/done?info=foo";
  const promptText = "just testing";
  const windowPromise = BrowserTestUtils.domWindowOpenedAndLoaded(
    undefined,
    win => win.document.documentURI === kBaseWindowUri
  );
  const request = InteractiveBrowser.waitForRedirect(completionUrl, promptText);
  const requestWindow = await windowPromise;
  is(requestWindow.document.title, promptText, "set window title");

  const closedWindow = BrowserTestUtils.domWindowClosed(requestWindow);
  const result = await request;
  is(result, completionUrl, "finished with correct URL");

  await closedWindow;
});