summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prefetch/document-rules.https.html
blob: 805f1cfbc36bfddaf84f5063d8f0d990a0ef78a1 (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
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/dispatcher/dispatcher.js"></script>
<script src="../resources/utils.js"></script>
<script src="resources/utils.sub.js"></script>
<script src="/common/subset-tests-by-key.js"></script>

<meta name="variant" content="?include=defaultPredicate">
<meta name="variant" content="?include=hrefMatches">
<meta name="variant" content="?include=and">
<meta name="variant" content="?include=or">
<meta name="variant" content="?include=not">
<meta name="variant" content="?include=invalidPredicate">
<meta name="variant" content="?include=linkInShadowTree">
<meta name="variant" content="?include=linkHrefChanged">
<meta name="variant" content="?include=newRuleSetAdded">
<meta name="variant" content="?include=selectorMatches">
<meta name="variant" content="?include=selectorMatchesScopingRoot">
<meta name="variant" content="?include=selectorMatchesInShadowTree">
<meta name="variant" content="?include=selectorMatchesDisplayNone">
<meta name="variant" content="?include=selectorMatchesDisplayLocked">
<meta name="variant" content="?include=unslottedLink">
<meta name="variant" content="?include=immediateMutation">
<meta name="variant" content="?include=baseURLChangedBySameDocumentNavigation">
<meta name="variant" content="?include=baseURLChangedByBaseElement">
<meta name="variant" content="?include=linkToSelfFragment">

<body>
<script>
  setup(() => assertSpeculationRulesIsSupported());

  subsetTestByKey('defaultPredicate', promise_test, async t => {
    const url = getPrefetchUrl();
    addLink(url);
    insertDocumentRule();
    await new Promise(resolve => t.step_timeout(resolve, 2000));

    assert_equals(await isUrlPrefetched(url), 1);
  }, 'test document rule with no predicate');

  subsetTestByKey('hrefMatches', promise_test, async t => {
    insertDocumentRule({ href_matches: '*\\?uuid=*&foo=bar' });

    const url_1 = getPrefetchUrl({foo: 'bar'});
    addLink(url_1);
    const url_2 = getPrefetchUrl({foo: 'buzz'});
    addLink(url_2)
    await new Promise(resolve => t.step_timeout(resolve, 2000));

    assert_equals(await isUrlPrefetched(url_1), 1);
    assert_equals(await isUrlPrefetched(url_2), 0);
  }, 'test href_matches document rule');

  subsetTestByKey('and', promise_test, async t => {
    insertDocumentRule({
      'and': [
        { href_matches: '*\\?*foo=bar*' },
        { href_matches: '*\\?*fizz=buzz*' }]
    });

    const url_1 = getPrefetchUrl({foo: 'bar'});
    const url_2 = getPrefetchUrl({fizz: 'buzz'});
    const url_3 = getPrefetchUrl({foo: 'bar', fizz: 'buzz'});
    [url_1, url_2, url_3].forEach(url => addLink(url));
    await new Promise(resolve => t.step_timeout(resolve, 2000));

    assert_equals(await isUrlPrefetched(url_1), 0);
    assert_equals(await isUrlPrefetched(url_2), 0);
    assert_equals(await isUrlPrefetched(url_3), 1);
  }, 'test document rule with conjunction predicate');

  subsetTestByKey('or', promise_test, async t => {
    insertDocumentRule({
      'or': [
        { href_matches: '*\\?*foo=bar*' },
        { href_matches: '*\\?*fizz=buzz*' }]
    });

    const url_1 = getPrefetchUrl({ foo: 'buzz' });
    const url_2 = getPrefetchUrl({ fizz: 'buzz' });
    const url_3 = getPrefetchUrl({ foo: 'bar'});
    [url_1, url_2, url_3].forEach(url => addLink(url));
    await new Promise(resolve => t.step_timeout(resolve, 2000));

    assert_equals(await isUrlPrefetched(url_1), 0);
    assert_equals(await isUrlPrefetched(url_2), 1);
    assert_equals(await isUrlPrefetched(url_3), 1);
  }, 'test document rule with disjunction predicate');

  subsetTestByKey('not', promise_test, async t => {
    insertDocumentRule({ not: { href_matches: '*\\?uuid=*&foo=bar' } });

    const url_1 = getPrefetchUrl({foo: 'bar'});
    addLink(url_1);
    const url_2 = getPrefetchUrl({foo: 'buzz'});
    addLink(url_2)
    await new Promise(resolve => t.step_timeout(resolve, 2000));

    assert_equals(await isUrlPrefetched(url_1), 0);
    assert_equals(await isUrlPrefetched(url_2), 1);
  }, 'test document rule with negation predicate');

  subsetTestByKey('invalidPredicate', promise_test, async t => {
    const url = getPrefetchUrl();
    addLink(url);
    insertDocumentRule({invalid: 'predicate'});
    await new Promise(resolve => t.step_timeout(resolve, 2000));

    assert_equals(await isUrlPrefetched(url), 0);
  }, 'invalid predicate should not throw error or start prefetch');

  subsetTestByKey('linkInShadowTree', promise_test, async t => {
    insertDocumentRule();

    // Create shadow root.
    const shadowHost = document.createElement('div');
    document.body.appendChild(shadowHost);
    const shadowRoot = shadowHost.attachShadow({mode: 'open'});

    const url = getPrefetchUrl();
    addLink(url, shadowRoot);
    await new Promise(resolve => t.step_timeout(resolve, 2000));

    assert_equals(await isUrlPrefetched(url), 1);
  }, 'test that matching link in a shadow tree is prefetched');

  subsetTestByKey('linkHrefChanged', promise_test, async t => {
    insertDocumentRule({href_matches: "*\\?*foo=bar*"});

    const url = getPrefetchUrl();
    const link = addLink(url);
    await new Promise(resolve => t.step_timeout(resolve, 2000));
    assert_equals(await isUrlPrefetched(url), 0);

    const matching_url = getPrefetchUrl({foo: 'bar'});
    link.href = matching_url;
    await new Promise(resolve => t.step_timeout(resolve, 2000));
    assert_equals(await isUrlPrefetched(matching_url), 1);
  }, 'test that changing the href of an invalid link to a matching value triggers a prefetch');

  subsetTestByKey('newRuleSetAdded', promise_test, async t => {
    insertDocumentRule({href_matches: "*\\?*foo=bar*"});
    const url = getPrefetchUrl({fizz: "buzz"});
    addLink(url);
    await new Promise(resolve => t.step_timeout(resolve, 2000));
    assert_equals(await isUrlPrefetched(url), 0);

    insertDocumentRule({href_matches: "*\\?*fizz=buzz*"});
    await new Promise(resolve => t.step_timeout(resolve, 2000));
    assert_equals(await isUrlPrefetched(url), 1);
  }, 'test that adding a second rule set triggers prefetch');

  subsetTestByKey('selectorMatches', promise_test, async t => {
    insertDocumentRule({ selector_matches: 'a.important-link' });

    const url_1 = getPrefetchUrl({foo: 'bar'});
    const importantLink = addLink(url_1);
    importantLink.className = 'important-link';
    const url_2 = getPrefetchUrl({foo: 'buzz'});
    addLink(url_2)
    await new Promise(resolve => t.step_timeout(resolve, 2000));

    assert_equals(await isUrlPrefetched(url_1), 1);
    assert_equals(await isUrlPrefetched(url_2), 0);
  }, 'test selector_matches document rule');

  subsetTestByKey('selectorMatchesScopingRoot', promise_test, async t => {
    insertDocumentRule({ selector_matches: ':root > body > a' });

    const url_1 = getPrefetchUrl({ foo: 'bar' });
    addLink(url_1);

    const url_2 = getPrefetchUrl({ foo: 'buzz' });
    const extraContainer = document.createElement('div');
    document.body.appendChild(extraContainer);
    addLink(url_2, extraContainer);

    await new Promise(resolve => t.step_timeout(resolve, 2000));

    assert_equals(await isUrlPrefetched(url_1), 1);
    assert_equals(await isUrlPrefetched(url_2), 0);
  }, 'test selector_matches with :root');

  // 'selector_matches' should use the shadowRoot as the scoping root when
  // matching links inside a shadow tree.
  subsetTestByKey('selectorMatchesInShadowTree', promise_test, async t => {
    insertDocumentRule({ selector_matches: ':scope a.important-link' });

    // Create shadow root.
    const shadowHost = document.createElement('div');
    document.body.appendChild(shadowHost);
    const shadowRoot = shadowHost.attachShadow({ mode: 'open' });

    const url = getPrefetchUrl();
    const link = addLink(url, shadowRoot);
    link.className = 'important-link';
    await new Promise(resolve => t.step_timeout(resolve, 2000));

    assert_equals(await isUrlPrefetched(url), 1);
  }, 'test selector_matches with link inside shadow tree');

  subsetTestByKey('selectorMatchesDisplayNone', promise_test, async t => {
    const style = document.createElement('style');
    style.innerText = ".important-section { display: none; }";
    document.head.appendChild(style);
    insertDocumentRule();

    const importantSection = document.createElement('div');
    importantSection.className = 'important-section';
    document.body.appendChild(importantSection);
    const url = getPrefetchUrl();
    addLink(url, importantSection);

    await new Promise(resolve => t.step_timeout(resolve, 2000));
    assert_equals(await isUrlPrefetched(url), 0);

    style.remove();
    await new Promise(resolve => t.step_timeout(resolve, 2000));
    assert_equals(await isUrlPrefetched(url), 1);
  }, 'test selector_matches with link inside display:none container');

  subsetTestByKey('selectorMatchesDisplayLocked', promise_test, async t => {
    const style = document.createElement('style');
    style.innerText = ".important-section { content-visibility: hidden; }";
    document.head.appendChild(style);
    insertDocumentRule({ selector_matches: '.important-section a' });

    const importantSection = document.createElement('div');
    importantSection.className = 'important-section';
    document.body.appendChild(importantSection);
    const url = getPrefetchUrl();
    addLink(url, importantSection);

    await new Promise(resolve => t.step_timeout(resolve, 2000));
    assert_equals(await isUrlPrefetched(url), 0);

    style.remove();
    await new Promise(resolve => t.step_timeout(resolve, 2000));
    assert_equals(await isUrlPrefetched(url), 1);
  }, 'test selector_matches with link inside display locked container');

  subsetTestByKey('unslottedLink', promise_test, async t => {
    insertDocumentRule();

    // Create shadow root.
    const shadowHost = document.createElement('div');
    document.body.appendChild(shadowHost);
    const shadowRoot = shadowHost.attachShadow({ mode: 'open' });

    // Add unslotted link.
    const url = getPrefetchUrl();
    addLink(url, shadowHost);

    await new Promise(resolve => t.step_timeout(resolve, 2000));
    assert_equals(await isUrlPrefetched(url), 0);
  }, 'test that unslotted link never matches document rule');

  subsetTestByKey('immediateMutation', promise_test, async t => {
    // Add a link and allow it to get its style computed.
    // (Double RAF lets this happen normally.)
    const url = getPrefetchUrl();
    const link = addLink(url, document.body);
    await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(() => resolve())));

    // Add a document rule and then immediately change the DOM to make it match.
    insertDocumentRule({ selector_matches: '.late-class *' });
    document.body.className = 'late-class';

    await new Promise(resolve => t.step_timeout(resolve, 2000));
    assert_equals(await isUrlPrefetched(url), 1);
  }, 'test that selector_matches predicates respect changes immediately');

  const baseURLChangedTestFixture = (testName, modifyBaseURLFunc) => {
    return subsetTestByKey(testName, promise_test, async t => {
      const url = getPrefetchUrl();
      const link = addLink(url);
      const url_pattern_string = `prefetch.py${url.search}`;

      // Insert a document rule with a url pattern predicate that uses a
      // relative URL which will not match with |url|, due to |document.baseURI|
      // being different from |url|'s path.
      assert_false((new URLPattern(url_pattern_string, document.baseURI)).test(url));
      insertDocumentRule({ href_matches: url_pattern_string });
      await new Promise(resolve => t.step_timeout(resolve, 2000));
      assert_equals(await isUrlPrefetched(url), 0);

      // Change the baseURL of the document to |url|. |url| should now be
      // prefetched.
      modifyBaseURLFunc(url);
      assert_true((new URLPattern(url_pattern_string, document.baseURI)).test(url));
      await new Promise(resolve => t.step_timeout(resolve, 2000));
      assert_equals(await isUrlPrefetched(url), 1);
    });
  }

  baseURLChangedTestFixture('baseURLChangedBySameDocumentNavigation', url => {
    history.pushState({}, "", url);
  });

  baseURLChangedTestFixture('baseURLChangedByBaseElement', url => {
    const base = document.createElement('base');
    base.href = url;
    document.head.appendChild(base);
  });

  subsetTestByKey('linkToSelfFragment', promise_test, async t => {
    const url = getPrefetchUrl();
    history.pushState({}, "", url);
    addLink(new URL('#fragment', url));
    insertDocumentRule();
    await new Promise(resolve => t.step_timeout(resolve, 2000));
    assert_equals(await isUrlPrefetched(url), 0);
  }, 'test that a fragment link to the current document does not prefetch');

</script>
</body>