summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/clients-matchall-order.https.html
blob: ec650f2264df530bd34b11737c4b1d9ed87f4b56 (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
<!DOCTYPE html>
<title>Service Worker: Clients.matchAll ordering</title>
<meta name=timeout content=long>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>

// Utility function for URLs this test will open.
function makeURL(name, num, type) {
  let u = new URL('resources/empty.html', location);
  u.searchParams.set('name', name);
  if (num !== undefined) {
    u.searchParams.set('q', num);
  }
  if (type === 'nested') {
    u.searchParams.set('nested', true);
  }
  return u.href;
}

// Non-test URLs that will be open during each test.  The harness URLs
// are from the WPT harness.  The "extra" URL is a final window opened
// by the test.
const EXTRA_URL = makeURL('extra');
const TEST_HARNESS_URL = location.href;
const TOP_HARNESS_URL = new URL('/testharness_runner.html', location).href;

// Utility function to open an iframe in the target parent window.  We
// can't just use with_iframe() because it does not support a configurable
// parent window.
function openFrame(parentWindow, url) {
  return new Promise(resolve => {
    let frame = parentWindow.document.createElement('iframe');
    frame.src = url;
    parentWindow.document.body.appendChild(frame);

    frame.contentWindow.addEventListener('load', evt => {
      resolve(frame);
    }, { once: true });
  });
}

// Utility function to open a window and wait for it to load.  The
// window may optionally have a nested iframe as well.  Returns
// a result like `{ top: <frame ref> nested: <nested frame ref if present> }`.
function openFrameConfig(opts) {
  let url = new URL(opts.url, location.href);
  return openFrame(window, url.href).then(top => {
    if (!opts.withNested) {
      return { top: top };
    }

    url.searchParams.set('nested', true);
    return openFrame(top.contentWindow, url.href).then(nested => {
      return { top: top, nested: nested };
    });
  });
}

// Utility function that takes a list of configurations and opens the
// corresponding windows in sequence.  An array of results is returned.
function openFrameConfigList(optList) {
  let resultList = [];
  function openNextWindow(optList, nextWindow) {
    if (nextWindow >= optList.length) {
      return resultList;
    }
    return openFrameConfig(optList[nextWindow]).then(result => {
      resultList.push(result);
      return openNextWindow(optList, nextWindow + 1);
    });
  }
  return openNextWindow(optList, 0);
}

// Utility function that focuses the given entry in window result list.
function executeFocus(frameResultList, opts) {
  return new Promise(resolve => {
    let w = frameResultList[opts.index][opts.type];
    let target = w.contentWindow ? w.contentWindow : w;
    target.addEventListener('focus', evt => {
      resolve();
    }, { once: true });
    target.focus();
  });
}

// Utility function that performs a list of focus commands in sequence
// based on the window result list.
function executeFocusList(frameResultList, optList) {
  function executeNextCommand(frameResultList, optList, nextCommand) {
    if (nextCommand >= optList.length) {
      return;
    }
    return executeFocus(frameResultList, optList[nextCommand]).then(_ => {
      return executeNextCommand(frameResultList, optList, nextCommand + 1);
    });
  }
  return executeNextCommand(frameResultList, optList, 0);
}

// Perform a `clients.matchAll()` in the service worker with the given
// options dictionary.
function doMatchAll(worker, options) {
  return new Promise(resolve => {
    let channel = new MessageChannel();
    channel.port1.onmessage = evt => {
      resolve(evt.data);
    };
    worker.postMessage({ port: channel.port2, options: options, disableSort: true },
                       [channel.port2]);
  });
}

// Function that performs a single test case.  It takes a configuration object
// describing the windows to open, how to focus them, the matchAll options,
// and the resulting expectations.  See the test cases for examples of how to
// use this.
function matchAllOrderTest(t, opts) {
  let script = 'resources/clients-matchall-worker.js';
  let worker;
  let frameResultList;
  let extraWindowResult;
  return service_worker_unregister_and_register(t, script, opts.scope).then(swr => {
    t.add_cleanup(() => service_worker_unregister(t, opts.scope));

    worker = swr.installing;
    return wait_for_state(t, worker, 'activated');
  }).then(_ => {
    return openFrameConfigList(opts.frameConfigList);
  }).then(results => {
    frameResultList = results;
    return openFrameConfig({ url: EXTRA_URL });
  }).then(result => {
    extraWindowResult = result;
    return executeFocusList(frameResultList, opts.focusConfigList);
  }).then(_ => {
    return doMatchAll(worker, opts.matchAllOptions);
  }).then(data => {
    assert_equals(data.length, opts.expected.length);
    for (let i = 0; i < data.length; ++i) {
      assert_equals(data[i][2], opts.expected[i], 'expected URL index ' + i);
    }
  }).then(_ => {
    frameResultList.forEach(result => result.top.remove());
    extraWindowResult.top.remove();
  }).catch(e => {
    if (frameResultList) {
      frameResultList.forEach(result => result.top.remove());
    }
    if (extraWindowResult) {
      extraWindowResult.top.remove();
    }
    throw(e);
  });
}

// ----------
// Test cases
// ----------

promise_test(t => {
  let name = 'no-focus-controlled-windows';
  let opts = {
    scope: makeURL(name),

    frameConfigList: [
      { url: makeURL(name, 0), withNested: false },
      { url: makeURL(name, 1), withNested: false },
      { url: makeURL(name, 2), withNested: false },
    ],

    focusConfigList: [
      // no focus commands
    ],

    matchAllOptions: {
      includeUncontrolled: false
    },

    expected: [
      makeURL(name, 0),
      makeURL(name, 1),
      makeURL(name, 2),
    ],
  };

  return matchAllOrderTest(t, opts);
}, 'Clients.matchAll() returns non-focused controlled windows in creation order.');

promise_test(t => {
  let name = 'focus-controlled-windows-1';
  let opts = {
    scope: makeURL(name),

    frameConfigList: [
      { url: makeURL(name, 0), withNested: false },
      { url: makeURL(name, 1), withNested: false },
      { url: makeURL(name, 2), withNested: false },
    ],

    focusConfigList: [
      { index: 0, type: 'top' },
      { index: 1, type: 'top' },
      { index: 2, type: 'top' },
    ],

    matchAllOptions: {
      includeUncontrolled: false
    },

    expected: [
      makeURL(name, 2),
      makeURL(name, 1),
      makeURL(name, 0),
    ],
  };

  return matchAllOrderTest(t, opts);
}, 'Clients.matchAll() returns controlled windows in focus order.  Case 1.');

promise_test(t => {
  let name = 'focus-controlled-windows-2';
  let opts = {
    scope: makeURL(name),

    frameConfigList: [
      { url: makeURL(name, 0), withNested: false },
      { url: makeURL(name, 1), withNested: false },
      { url: makeURL(name, 2), withNested: false },
    ],

    focusConfigList: [
      { index: 2, type: 'top' },
      { index: 1, type: 'top' },
      { index: 0, type: 'top' },
    ],

    matchAllOptions: {
      includeUncontrolled: false
    },

    expected: [
      makeURL(name, 0),
      makeURL(name, 1),
      makeURL(name, 2),
    ],
  };

  return matchAllOrderTest(t, opts);
}, 'Clients.matchAll() returns controlled windows in focus order.  Case 2.');

promise_test(t => {
  let name = 'no-focus-uncontrolled-windows';
  let opts = {
    scope: makeURL(name + '-outofscope'),

    frameConfigList: [
      { url: makeURL(name, 0), withNested: false },
      { url: makeURL(name, 1), withNested: false },
      { url: makeURL(name, 2), withNested: false },
    ],

    focusConfigList: [
      // no focus commands
    ],

    matchAllOptions: {
      includeUncontrolled: true
    },

    expected: [
      // The harness windows have been focused, so appear first
      TEST_HARNESS_URL,
      TOP_HARNESS_URL,

      // Test frames have not been focused, so appear in creation order
      makeURL(name, 0),
      makeURL(name, 1),
      makeURL(name, 2),
      EXTRA_URL,
    ],
  };

  return matchAllOrderTest(t, opts);
}, 'Clients.matchAll() returns non-focused uncontrolled windows in creation order.');

promise_test(t => {
  let name = 'focus-uncontrolled-windows-1';
  let opts = {
    scope: makeURL(name + '-outofscope'),

    frameConfigList: [
      { url: makeURL(name, 0), withNested: false },
      { url: makeURL(name, 1), withNested: false },
      { url: makeURL(name, 2), withNested: false },
    ],

    focusConfigList: [
      { index: 0, type: 'top' },
      { index: 1, type: 'top' },
      { index: 2, type: 'top' },
    ],

    matchAllOptions: {
      includeUncontrolled: true
    },

    expected: [
      // The test harness window is a parent of all test frames.  It will
      // always have the same focus time or later as its frames.  So it
      // appears first.
      TEST_HARNESS_URL,

      makeURL(name, 2),
      makeURL(name, 1),
      makeURL(name, 0),

      // The overall harness has been focused
      TOP_HARNESS_URL,

      // The extra frame was never focused
      EXTRA_URL,
    ],
  };

  return matchAllOrderTest(t, opts);
}, 'Clients.matchAll() returns uncontrolled windows in focus order.  Case 1.');

promise_test(t => {
  let name = 'focus-uncontrolled-windows-2';
  let opts = {
    scope: makeURL(name + '-outofscope'),

    frameConfigList: [
      { url: makeURL(name, 0), withNested: false },
      { url: makeURL(name, 1), withNested: false },
      { url: makeURL(name, 2), withNested: false },
    ],

    focusConfigList: [
      { index: 2, type: 'top' },
      { index: 1, type: 'top' },
      { index: 0, type: 'top' },
    ],

    matchAllOptions: {
      includeUncontrolled: true
    },

    expected: [
      // The test harness window is a parent of all test frames.  It will
      // always have the same focus time or later as its frames.  So it
      // appears first.
      TEST_HARNESS_URL,

      makeURL(name, 0),
      makeURL(name, 1),
      makeURL(name, 2),

      // The overall harness has been focused
      TOP_HARNESS_URL,

      // The extra frame was never focused
      EXTRA_URL,
    ],
  };

  return matchAllOrderTest(t, opts);
}, 'Clients.matchAll() returns uncontrolled windows in focus order.  Case 2.');

promise_test(t => {
  let name = 'focus-controlled-nested-windows';
  let opts = {
    scope: makeURL(name),

    frameConfigList: [
      { url: makeURL(name, 0), withNested: true },
      { url: makeURL(name, 1), withNested: true },
      { url: makeURL(name, 2), withNested: true },
    ],

    focusConfigList: [
      { index: 0, type: 'top' },

      // Note, some browsers don't let programmatic focus of a frame unless
      // an ancestor window is already focused.  So focus the window and
      // then the frame.
      { index: 1, type: 'top' },
      { index: 1, type: 'nested' },

      { index: 2, type: 'top' },
    ],

    matchAllOptions: {
      includeUncontrolled: false
    },

    expected: [
      // Focus order for window 2, but not its frame.  We only focused
      // the window.
      makeURL(name, 2),

      // Window 1 is next via focus order, but the window is always
      // shown first here.  The window gets its last focus time updated
      // when the frame is focused.  Since the times match between the
      // two it falls back to creation order.  The window was created
      // before the frame.  This behavior is being discussed in:
      // https://github.com/w3c/ServiceWorker/issues/1080
      makeURL(name, 1),
      makeURL(name, 1, 'nested'),

      // Focus order for window 0, but not its frame.  We only focused
      // the window.
      makeURL(name, 0),

      // Creation order of the frames since they are not focused by
      // default when they are created.
      makeURL(name, 0, 'nested'),
      makeURL(name, 2, 'nested'),
    ],
  };

  return matchAllOrderTest(t, opts);
}, 'Clients.matchAll() returns controlled windows and frames in focus order.');
</script>