summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prefetch/document-rules.https.html
blob: a5030f6938b0dd993554aafdd5872ae3aa7aea9e (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
<!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.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">

<body>
<script>
  subsetTestByKey('defaultPredicate', promise_test, async t => {
    assert_implements(HTMLScriptElement.supports('speculationrules'),
      'Speculation Rules not supported');

    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 => {
    assert_implements(HTMLScriptElement.supports('speculationrules'),
      'Speculation Rules not supported');

    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 => {
    assert_implements(HTMLScriptElement.supports('speculationrules'),
      'Speculation Rules not supported');

    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 => {
    assert_implements(HTMLScriptElement.supports('speculationrules'),
      'Speculation Rules not supported');

    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 => {
    assert_implements(HTMLScriptElement.supports('speculationrules'),
      "Speculation Rules not supported");

    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 => {
    assert_implements(HTMLScriptElement.supports('speculationrules'),
      'Speculation Rules not supported');

    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 => {
    assert_implements(HTMLScriptElement.supports('speculationrules'),
      'Speculation Rules not supported');

    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');

</script>
</body>