summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabdialogs/browser_tabdialogbox_navigation.js
blob: fd75ed4dcff260b1b1ae342bb2dbcaf0bc8e50e4 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_ROOT_CHROME = getRootDirectory(gTestPath);
const TEST_DIALOG_PATH = TEST_ROOT_CHROME + "subdialog.xhtml";

/**
 * Tests that all tab dialogs are closed on navigation.
 */
add_task(async function test_tabdialogbox_multiple_close_on_nav() {
  await BrowserTestUtils.withNewTab(
    "https://example.com",
    async function (browser) {
      // Open two dialogs and wait for them to be ready.
      let dialogBox = gBrowser.getTabDialogBox(browser);
      let closedPromises = [
        dialogBox.open(TEST_DIALOG_PATH).closedPromise,
        dialogBox.open(TEST_DIALOG_PATH).closedPromise,
      ];

      let dialogs = dialogBox.getTabDialogManager()._dialogs;

      is(dialogs.length, 2, "Dialog manager has two dialogs.");

      info("Waiting for dialogs to open.");
      await Promise.all(dialogs.map(dialog => dialog._dialogReady));

      // Navigate to a different page
      BrowserTestUtils.startLoadingURIString(browser, "https://example.org");

      info("Waiting for dialogs to close.");
      await closedPromises;

      ok(true, "All open dialogs should close on navigation");
    }
  );
});

/**
 * Tests dialog close on navigation triggered by web content.
 */
add_task(async function test_tabdialogbox_close_on_content_nav() {
  await BrowserTestUtils.withNewTab(
    "https://example.com",
    async function (browser) {
      // Open a dialog and wait for it to be ready
      let dialogBox = gBrowser.getTabDialogBox(browser);
      let { closedPromise } = dialogBox.open(TEST_DIALOG_PATH);

      let dialog = dialogBox.getTabDialogManager()._topDialog;

      is(
        dialogBox.getTabDialogManager()._dialogs.length,
        1,
        "Dialog manager has one dialog."
      );

      info("Waiting for dialog to open.");
      await dialog._dialogReady;

      // Trigger a same origin navigation by the content
      await ContentTask.spawn(browser, {}, () => {
        // eslint-disable-next-line @microsoft/sdl/no-insecure-url
        content.location = "http://example.com/1";
      });

      info("Waiting for dialog to close.");
      await closedPromise;
      ok(
        true,
        "Dialog should close for same origin navigation by the content."
      );

      // Open a new dialog
      closedPromise = dialogBox.open(TEST_DIALOG_PATH, {
        keepOpenSameOriginNav: true,
      }).closedPromise;

      info("Waiting for dialog to open.");
      await dialog._dialogReady;

      SimpleTest.requestFlakyTimeout("Waiting to ensure dialog does not close");
      let race = Promise.race([
        closedPromise,
        // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
        new Promise(resolve => setTimeout(() => resolve("success"), 1000)),
      ]);

      // Trigger a same origin navigation by the content
      await ContentTask.spawn(browser, {}, () => {
        // eslint-disable-next-line @microsoft/sdl/no-insecure-url
        content.location = "http://example.com/test";
      });

      is(
        await race,
        "success",
        "Dialog should not close for same origin navigation by the content."
      );

      // Trigger a cross origin navigation by the content
      await ContentTask.spawn(browser, {}, () => {
        // eslint-disable-next-line @microsoft/sdl/no-insecure-url
        content.location = "http://example.org/test2";
      });

      info("Waiting for dialog to close");
      await closedPromise;

      ok(
        true,
        "Dialog should close for cross origin navigation by the content."
      );
    }
  );
});

/**
 * Hides a dialog stack and tests that behavior doesn't change. Ensures
 * navigation triggered by web content still closes all dialogs.
 */
add_task(async function test_tabdialogbox_hide() {
  await BrowserTestUtils.withNewTab(
    "https://example.com",
    async function (browser) {
      // Open a dialog and wait for it to be ready
      let dialogBox = gBrowser.getTabDialogBox(browser);
      let dialogBoxManager = dialogBox.getTabDialogManager();
      let closedPromises = [
        dialogBox.open(TEST_DIALOG_PATH).closedPromise,
        dialogBox.open(TEST_DIALOG_PATH).closedPromise,
      ];

      let dialogs = dialogBox.getTabDialogManager()._dialogs;

      is(
        dialogBox.getTabDialogManager()._dialogs.length,
        2,
        "Dialog manager has two dialogs."
      );

      info("Waiting for dialogs to open.");
      await Promise.all(dialogs.map(dialog => dialog._dialogReady));

      ok(
        !BrowserTestUtils.isHidden(dialogBoxManager._dialogStack),
        "Dialog stack is showing"
      );

      dialogBoxManager.hideDialog(browser);

      is(
        dialogBoxManager._dialogs.length,
        2,
        "Dialog manager still has two dialogs."
      );

      ok(
        BrowserTestUtils.isHidden(dialogBoxManager._dialogStack),
        "Dialog stack is hidden"
      );

      // Navigate to a different page
      BrowserTestUtils.startLoadingURIString(browser, "https://example.org");

      info("Waiting for dialogs to close.");
      await closedPromises;

      ok(true, "All open dialogs should still close on navigation");
    }
  );
});