summaryrefslogtreecommitdiffstats
path: root/toolkit/components/windowwatcher/test/head.js
blob: f8ab11a8309e0ab19980579767fd6e557db4830e (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
/**
 * Given some nsIDOMWindow for a window running in the parent
 * process, return the nsIWebBrowserChrome chrome flags for
 * the associated XUL window.
 *
 * @param win (nsIDOMWindow)
 *        Some window in the parent process.
 * @returns int
 */
function getParentChromeFlags(win) {
  return win.docShell.treeOwner
    .QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIAppWindow).chromeFlags;
}

const WINDOW_OPEN_FEATURES_PATTERNS = [
  { features: "", popup: false },

  // If features isn't empty, the following should be true to open tab/window:
  //   * location or toolbar (defaults to false)
  //   * menubar (defaults to false)
  //   * resizable (defaults to true)
  //   * scrollbars (defaults to false)
  //   * status (defaults to false)
  { features: "location,menubar,resizable,scrollbars,status", popup: false },
  { features: "toolbar,menubar,resizable,scrollbars,status", popup: false },

  // resizable defaults to true.
  { features: "location,menubar,scrollbars,status", popup: false },

  // The following testcases use "location,menubar,scrollbars,status"
  // as the base non-popup case, and test the boundary between popup
  // vs non-popup.

  // If either location or toolbar is true, not popup.
  {
    features: "toolbar,menubar,resizable,scrollbars,status",
    popup: false,
  },
  {
    features: "location,menubar,resizable,scrollbars,status",
    popup: false,
  },

  // If both location and toolbar are false, popup.
  { features: "menubar,scrollbars,status", popup: true },

  // If menubar is false, popup.
  { features: "location,resizable,scrollbars,status", popup: true },

  // If resizable is true, not popup.
  {
    features: "location,menubar,resizable=yes,scrollbars,status",
    popup: false,
  },

  // If resizable is false, popup.
  { features: "location,menubar,resizable=0,scrollbars,status", popup: true },

  // If scrollbars is false, popup.
  { features: "location,menubar,resizable,status", popup: true },

  // If status is false, popup.
  { features: "location,menubar,resizable,scrollbars", popup: true },

  // position and size have no effect.
  {
    features:
      "location,menubar,scrollbars,status," +
      "left=100,screenX=100,top=100,screenY=100," +
      "width=100,innerWidth=100,outerWidth=100," +
      "height=100,innerHeight=100,outerHeight=100",
    popup: false,
  },

  // Most feature defaults to false if the feature is not empty.
  // Specifying only some of them opens a popup.
  { features: "location,toolbar,menubar", popup: true },
  { features: "resizable,scrollbars,status", popup: true },

  // Specifying unknown feature makes the feature not empty.
  { features: "someunknownfeature", popup: true },

  // noopener and noreferrer are removed before testing if feature is empty.
  { features: "noopener,noreferrer", popup: false },
];

const WINDOW_CHROME_FLAGS = {
  CHROME_WINDOW_BORDERS: true,
  CHROME_WINDOW_CLOSE: true,
  CHROME_WINDOW_RESIZE: true,
  CHROME_LOCATIONBAR: true,
  CHROME_STATUSBAR: true,
  CHROME_SCROLLBARS: true,
  CHROME_TITLEBAR: true,

  CHROME_MENUBAR: true,
  CHROME_TOOLBAR: true,
  CHROME_PERSONAL_TOOLBAR: true,
};

const POPUP_CHROME_FLAGS = {
  CHROME_WINDOW_BORDERS: true,
  CHROME_WINDOW_CLOSE: true,
  CHROME_WINDOW_RESIZE: true,
  CHROME_LOCATIONBAR: true,
  CHROME_STATUSBAR: true,
  CHROME_SCROLLBARS: true,
  CHROME_TITLEBAR: true,

  CHROME_MENUBAR: false,
  CHROME_TOOLBAR: false,
  CHROME_PERSONAL_TOOLBAR: false,
};

async function testPopupPatterns(nonPopup) {
  for (const { features, popup } of WINDOW_OPEN_FEATURES_PATTERNS) {
    const BLANK_PAGE = "data:text/html,";
    const OPEN_PAGE = "data:text/plain,hello";
    const SCRIPT_PAGE = `data:text/html,<script>window.open("${OPEN_PAGE}", "", "${features}");</script>`;

    async function testNewWindow(flags) {
      await BrowserTestUtils.withNewTab(
        {
          gBrowser,
          url: BLANK_PAGE,
        },
        async function (browser) {
          const newWinPromise = BrowserTestUtils.waitForNewWindow();
          BrowserTestUtils.loadURIString(gBrowser, SCRIPT_PAGE);

          const win = await newWinPromise;
          const parentChromeFlags = getParentChromeFlags(win);

          for (const [name, visible] of Object.entries(flags)) {
            if (visible) {
              Assert.equal(
                !!(parentChromeFlags & Ci.nsIWebBrowserChrome[name]),
                true,
                `${name} should be present for features "${features}"`
              );
            } else {
              Assert.equal(
                !!(parentChromeFlags & Ci.nsIWebBrowserChrome[name]),
                false,
                `${name} should not be present for features "${features}"`
              );
            }
          }

          await BrowserTestUtils.closeWindow(win);
        }
      );
    }

    async function testNewTab() {
      await BrowserTestUtils.withNewTab(
        {
          gBrowser,
          url: BLANK_PAGE,
        },
        async function (browser) {
          const newTabPromise = BrowserTestUtils.waitForNewTab(
            gBrowser,
            OPEN_PAGE
          );
          BrowserTestUtils.loadURIString(gBrowser, SCRIPT_PAGE);

          let tab = await newTabPromise;
          BrowserTestUtils.removeTab(tab);
        }
      );
    }

    async function testCurrentTab() {
      await BrowserTestUtils.withNewTab(
        {
          gBrowser,
          url: BLANK_PAGE,
        },
        async function (browser) {
          const pagePromise = BrowserTestUtils.browserLoaded(
            browser,
            false,
            OPEN_PAGE
          );
          BrowserTestUtils.loadURIString(gBrowser, SCRIPT_PAGE);

          await pagePromise;
        }
      );
    }

    if (!popup) {
      if (nonPopup == "window") {
        await testNewWindow(WINDOW_CHROME_FLAGS);
      } else if (nonPopup == "tab") {
        await testNewTab();
      } else {
        // current tab
        await testCurrentTab();
      }
    } else {
      await testNewWindow(POPUP_CHROME_FLAGS);
    }
  }
}