summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/openWindow_worker.js
blob: ffaad009be7075cbade2af5bbcea63e495f5cf16 (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
// the worker won't shut down between events because we increased
// the timeout values.
var client;
var window_count = 0;
var expected_window_count = 9;
var isolated_window_count = 0;
var expected_isolated_window_count = 2;
var resolve_got_all_windows = null;
var got_all_windows = new Promise(function (res, rej) {
  resolve_got_all_windows = res;
});

// |expected_window_count| needs to be updated for every new call that's
// expected to actually open a new window regardless of what |clients.openWindow|
// returns.
function testForUrl(url, throwType, clientProperties, resultsArray) {
  return clients
    .openWindow(url)
    .then(function (e) {
      if (throwType != null) {
        resultsArray.push({
          result: false,
          message: "openWindow should throw " + throwType,
        });
      } else if (clientProperties) {
        resultsArray.push({
          result: e instanceof WindowClient,
          message: `openWindow should resolve to a WindowClient for url ${url}, got ${e}`,
        });
        resultsArray.push({
          result: e.url == clientProperties.url,
          message: "Client url should be " + clientProperties.url,
        });
        // Add more properties
      } else {
        resultsArray.push({
          result: e == null,
          message: "Open window should resolve to null. Got: " + e,
        });
      }
    })
    .catch(function (err) {
      if (throwType == null) {
        resultsArray.push({
          result: false,
          message: "Unexpected throw: " + err,
        });
      } else {
        resultsArray.push({
          result: err.toString().includes(throwType),
          message: "openWindow should throw: " + err,
        });
      }
    });
}

onmessage = function (event) {
  if (event.data == "testNoPopup") {
    client = event.source;

    var results = [];
    var promises = [];
    promises.push(testForUrl("about:blank", "TypeError", null, results));
    promises.push(
      testForUrl("http://example.com", "InvalidAccessError", null, results)
    );
    promises.push(
      testForUrl("_._*`InvalidURL", "InvalidAccessError", null, results)
    );
    event.waitUntil(
      Promise.all(promises).then(function (e) {
        client.postMessage(results);
      })
    );
  }

  if (event.data == "NEW_WINDOW" || event.data == "NEW_ISOLATED_WINDOW") {
    window_count += 1;
    if (event.data == "NEW_ISOLATED_WINDOW") {
      isolated_window_count += 1;
    }
    if (window_count == expected_window_count) {
      resolve_got_all_windows();
    }
  }

  if (event.data == "CHECK_NUMBER_OF_WINDOWS") {
    event.waitUntil(
      got_all_windows
        .then(function () {
          return clients.matchAll();
        })
        .then(function (cl) {
          event.source.postMessage([
            {
              result: cl.length == expected_window_count,
              message: `The number of windows is correct. ${cl.length} == ${expected_window_count}`,
            },
            {
              result: isolated_window_count == expected_isolated_window_count,
              message: `The number of isolated windows is correct. ${isolated_window_count} == ${expected_isolated_window_count}`,
            },
          ]);
          for (i = 0; i < cl.length; i++) {
            cl[i].postMessage("CLOSE");
          }
        })
    );
  }
};

onnotificationclick = function (e) {
  var results = [];
  var promises = [];

  var redirect =
    "http://mochi.test:8888/tests/dom/serviceworkers/test/redirect.sjs?";
  var redirect_xorigin =
    "http://example.com/tests/dom/serviceworkers/test/redirect.sjs?";
  var same_origin =
    "http://mochi.test:8888/tests/dom/serviceworkers/test/open_window/client.sjs";
  var different_origin =
    "http://example.com/tests/dom/serviceworkers/test/open_window/client.sjs";

  promises.push(testForUrl("about:blank", "TypeError", null, results));
  promises.push(testForUrl(different_origin, null, null, results));
  promises.push(testForUrl(same_origin, null, { url: same_origin }, results));
  promises.push(
    testForUrl("open_window/client.sjs", null, { url: same_origin }, results)
  );

  // redirect tests
  promises.push(
    testForUrl(
      redirect + "open_window/client.sjs",
      null,
      { url: same_origin },
      results
    )
  );
  promises.push(testForUrl(redirect + different_origin, null, null, results));

  promises.push(
    testForUrl(redirect_xorigin + "open_window/client.sjs", null, null, results)
  );
  promises.push(
    testForUrl(
      redirect_xorigin + same_origin,
      null,
      { url: same_origin },
      results
    )
  );

  // coop+coep tests
  promises.push(
    testForUrl(
      same_origin + "?crossOriginIsolated=true",
      null,
      { url: same_origin + "?crossOriginIsolated=true" },
      results
    )
  );
  promises.push(
    testForUrl(
      different_origin + "?crossOriginIsolated=true",
      null,
      null,
      results
    )
  );

  e.waitUntil(
    Promise.all(promises).then(function () {
      client.postMessage(results);
    })
  );
};