summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/fetch-later/send-on-deactivate.tentative.https.window.js
blob: 3bcf07483add874c42e6c26cc4343905fcddc596 (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
// META: script=/common/dispatcher/dispatcher.js
// META: script=/common/get-host-info.sub.js
// META: script=/common/utils.js
// META: script=/html/browsers/browsing-the-web/remote-context-helper/resources/remote-context-helper.js
// META: script=/html/browsers/browsing-the-web/back-forward-cache/resources/rc-helper.js
// META: script=/fetch/fetch-later/resources/fetch-later-helper.js

'use strict';

// NOTE: Due to the restriction of WPT runner, the following tests are all run
// with BackgroundSync off, which is different from some browsers,
// e.g. Chrome, default behavior, as the testing infra does not support enabling
// it.

parallelPromiseTest(async t => {
  const uuid = token();
  const url = generateSetBeaconURL(uuid);
  // Sets no option to test the default behavior when a document enters BFCache.
  const helper = new RemoteContextHelper();
  // Opens a window with noopener so that BFCache will work.
  const rc1 = await helper.addWindow(
      /*config=*/ null, /*options=*/ {features: 'noopener'});

  // Creates a fetchLater request with default config in remote, which should
  // only be sent on page discarded (not on entering BFCache).
  await rc1.executeScript(url => {
    fetchLater(url);
    // Add a pageshow listener to stash the BFCache event.
    window.addEventListener('pageshow', e => {
      window.pageshowEvent = e;
    });
  }, [url]);
  // Navigates away to let page enter BFCache.
  const rc2 = await rc1.navigateToNew();
  // Navigates back.
  await rc2.historyBack();
  // Verifies the page was BFCached.
  assert_true(await rc1.executeScript(() => {
    return window.pageshowEvent.persisted;
  }));

  // Theoretically, the request should still be pending thus 0 request received.
  // However, 1 request is sent, as by default the WPT test runner, e.g.
  // content_shell in Chromium, does not enable BackgroundSync permission,
  // resulting in forcing request sending on every navigation.
  await expectBeacon(uuid, {count: 1});
}, `fetchLater() sends on page entering BFCache if BackgroundSync is off.`);

parallelPromiseTest(async t => {
  const uuid = token();
  const url = generateSetBeaconURL(uuid);
  const helper = new RemoteContextHelper();
  // Opens a window with noopener so that BFCache will work.
  const rc1 = await helper.addWindow(
      /*config=*/ null, /*options=*/ {features: 'noopener'});

  // When the remote is put into BFCached, creates a fetchLater request w/
  // activateAfter = 0s. It should be sent out immediately.
  await rc1.executeScript(url => {
    window.addEventListener('pagehide', e => {
      if (e.persisted) {
        fetchLater(url, {activateAfter: 0});
      }
    });
    // Add a pageshow listener to stash the BFCache event.
    window.addEventListener('pageshow', e => {
      window.pageshowEvent = e;
    });
  }, [url]);
  // Navigates away to trigger request sending.
  const rc2 = await rc1.navigateToNew();
  // Navigates back.
  await rc2.historyBack();
  // Verifies the page was BFCached.
  assert_true(await rc1.executeScript(() => {
    return window.pageshowEvent.persisted;
  }));

  // NOTE: In this case, it does not matter if BackgroundSync is on or off.
  await expectBeacon(uuid, {count: 1});
}, `Call fetchLater() when BFCached with activateAfter=0 sends immediately.`);

parallelPromiseTest(async t => {
  const uuid = token();
  const url = generateSetBeaconURL(uuid);
  // Sets no option to test the default behavior when a document gets discarded
  // on navigated away.
  const helper = new RemoteContextHelper();
  // Opens a window without BFCache.
  const rc1 = await helper.addWindow();

  // Creates a fetchLater request in remote which should only be sent on
  // navigating away.
  await rc1.executeScript(url => {
    fetchLater(url);
    // Add a pageshow listener to stash the BFCache event.
    window.addEventListener('pageshow', e => {
      window.pageshowEvent = e;
    });
  }, [url]);
  // Navigates away to trigger request sending.
  const rc2 = await rc1.navigateToNew();
  // Navigates back.
  await rc2.historyBack();
  // Verifies the page was NOT BFCached.
  assert_equals(undefined, await rc1.executeScript(() => {
    return window.pageshowEvent;
  }));

  // NOTE: In this case, it does not matter if BackgroundSync is on or off.
  await expectBeacon(uuid, {count: 1});
}, `fetchLater() sends on navigating away a page w/o BFCache.`);

parallelPromiseTest(async t => {
  const uuid = token();
  const url = generateSetBeaconURL(uuid);
  // Sets no option to test the default behavior when a document gets discarded
  // on navigated away.
  const helper = new RemoteContextHelper();
  // Opens a window without BFCache.
  const rc1 = await helper.addWindow();

  // Creates 2 fetchLater requests in remote, and one of them is aborted
  // immediately. The other one should only be sent right on navigating away.
  await rc1.executeScript(url => {
    const controller = new AbortController();
    fetchLater(url, {signal: controller.signal});
    fetchLater(url);
    controller.abort();
    // Add a pageshow listener to stash the BFCache event.
    window.addEventListener('pageshow', e => {
      window.pageshowEvent = e;
    });
  }, [url]);
  // Navigates away to trigger request sending.
  const rc2 = await rc1.navigateToNew();
  // Navigates back.
  await rc2.historyBack();
  // Verifies the page was NOT BFCached.
  assert_equals(undefined, await rc1.executeScript(() => {
    return window.pageshowEvent;
  }));

  // NOTE: In this case, it does not matter if BackgroundSync is on or off.
  await expectBeacon(uuid, {count: 1});
}, `fetchLater() does not send aborted request on navigating away a page w/o BFCache.`);

parallelPromiseTest(async t => {
  const uuid = token();
  const url = generateSetBeaconURL(uuid);
  const options = {activateAfter: 60000};
  const helper = new RemoteContextHelper();
  // Opens a window with noopener so that BFCache will work.
  const rc1 = await helper.addWindow(
      /*config=*/ null, /*options=*/ {features: 'noopener'});

  // Creates a fetchLater request in remote which should only be sent on
  // navigating away.
  await rc1.executeScript((url) => {
    // Sets activateAfter = 1m to indicate the request should NOT be sent out
    // immediately.
    fetchLater(url, {activateAfter: 60000});
    // Adds a pageshow listener to stash the BFCache event.
    window.addEventListener('pageshow', e => {
      window.pageshowEvent = e;
    });
  }, [url]);
  // Navigates away to trigger request sending.
  const rc2 = await rc1.navigateToNew();
  // Navigates back.
  await rc2.historyBack();
  // Verifies the page was BFCached.
  assert_true(await rc1.executeScript(() => {
    return window.pageshowEvent.persisted;
  }));

  // Theoretically, the request should still be pending thus 0 request received.
  // However, 1 request is sent, as by default the WPT test runner, e.g.
  // content_shell in Chromium, does not enable BackgroundSync permission,
  // resulting in forcing request sending on every navigation, even if page is
  // put into BFCache.
  await expectBeacon(uuid, {count: 1});
}, `fetchLater() with activateAfter=1m sends on page entering BFCache if BackgroundSync is off.`);