File: client-url-creation-url.https.html

package info (click to toggle)
firefox-esr 140.4.0esr-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,539,284 kB
  • sloc: cpp: 7,381,286; javascript: 6,388,710; ansic: 3,710,139; python: 1,393,780; xml: 628,165; asm: 426,916; java: 184,004; sh: 65,742; makefile: 19,302; objc: 13,059; perl: 12,912; yacc: 4,583; cs: 3,846; pascal: 3,352; lex: 1,720; ruby: 1,226; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (129 lines) | stat: -rw-r--r-- 5,131 bytes parent folder | download | duplicates (15)
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
<!DOCTYPE html>
<title>Service Worker: Client.url is Window creation URL tests</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
// These tests all validate that the Service Worker's Client.url property is
// correct for different types of Window client navigations.
// All test cases involve an iframe navigating in some manner and then
// the service worker validating that the Client.url property is correct.

// Waits for and returns the next postMessage from `frame` to this window.
function wait_for_message_from_frame(frame) {
  return new Promise(resolve => {
    const message_handler = e => {
      if (e.source === frame.contentWindow) {
        frame.removeEventListener('message', message_handler);
        resolve(e.data);
      }
    };
    window.addEventListener('message', message_handler);
  });
}

// Returns a promise to get a client URL under the ServiceWorker's
// (client-url-creation-url-sw.js) control. `target` should be an active
// ServiceWorker to post message.  We expect it to send back the client URL
// via the given channel.
function post_message_and_wait_for_response(
    target, data) {
  return new Promise(resolve => {
    const channel = new MessageChannel();
    channel.port1.onmessage = e => {
      resolve(e.data);
    };
    target.postMessage(
        {message: data, port: channel.port2},
        [channel.port2]);
  });
}

const SCOPE = 'resources/client-url-creation-url';
const IFRAME_URL = SCOPE + '-iframe.html?step=1';

// The test helper function implements a test case by
// using an iframe to navigate in various ways described by `navigation_kinds`
// and then validates the ServiceWorker Client.url property of the iframe
// against the provided `expected_client_url`.
// `navigation_kinds` is an array of strings each of which is one of the
// navigation actions supported by client-url-creation-url-iframe.html.
async function test_client_url_helper(
    t, navigation_kinds, expected_client_url) {
  const registration = await service_worker_unregister_and_register(
      t, 'resources/client-url-creation-url-sw.js', SCOPE);
  t.add_cleanup(_ => registration.unregister());
  await wait_for_state(t, registration.installing, 'activated');

  const frame = await with_iframe(IFRAME_URL);
  t.add_cleanup(_ => frame.remove());
  const message = await wait_for_message_from_frame(frame);
  assert_equals(message, 'done', 'iframe loaded successfully');

  for (let navigation_kind_idx = 0;
      navigation_kind_idx < navigation_kinds.length;
      ++navigation_kind_idx) {
    const navigation_kind = navigation_kinds[navigation_kind_idx];

    frame.contentWindow.postMessage(navigation_kind);
    const result = await wait_for_message_from_frame(frame);
    assert_equals(result, 'done', 'iframe navigation successfully completed');
  }

  const actual_client_url = await post_message_and_wait_for_response(
      registration.active, "get_client_url");

  assert_equals(
      actual_client_url, expected_client_url, 'Client.url should match');
}

const EXPECTED_URL_STEP_1 = new URL(
    './resources/client-url-creation-url-iframe.html?step=1', location).href;
const EXPECTED_URL_STEP_2 = new URL(
    './resources/client-url-creation-url-iframe.html?step=2', location).href;

// Tests that perform navigations that don't create a new document
promise_test(async t => {
  await test_client_url_helper(t, [], EXPECTED_URL_STEP_1);
}, 'No navigation creation URL is same as window URL');

promise_test(async t => {
  await test_client_url_helper(t, ['fragment'], EXPECTED_URL_STEP_1);
}, 'Fragment only navigation doesn\'t change creation URL');

promise_test(async t => {
  await test_client_url_helper(t, ['pushstate'], EXPECTED_URL_STEP_1);
}, 'Pushstate doesn\'t change creation URL');

promise_test(async t => {
  await test_client_url_helper(t, ['replacestate'], EXPECTED_URL_STEP_1);
}, 'Replacestate doesn\'t change creation URL');

promise_test(async t => {
  await test_client_url_helper(
      t, ['pushstate', 'pushstate', 'back-within-same-document'], EXPECTED_URL_STEP_1);
}, 'Going back over pushstate to other pushstate via back');

// Tests that perform navigations that create new document(s)
promise_test(async t => {
  await test_client_url_helper(t, ['query'], EXPECTED_URL_STEP_2);
}, 'Query navigation changes creation URL');

promise_test(async t => {
  await test_client_url_helper(t, ['reload'], EXPECTED_URL_STEP_1);
}, 'Reloading doesn\'t change creation URL');

promise_test(async t => {
  await test_client_url_helper(t, ['pushstate', 'reload'], EXPECTED_URL_STEP_2);
}, 'Reloading pushstate URL changes creation URL');

promise_test(async t => {
  await test_client_url_helper(
      t, ['query', 'pushstate', 'back-within-same-document'], EXPECTED_URL_STEP_2);
}, 'Going back over pushstate to creation URL');

promise_test(async t => {
  await test_client_url_helper(
      t, ['query', 'query', 'back-cross-document'], EXPECTED_URL_STEP_2);
}, 'Going back to new document changes creation URL');
</script>