summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/test_subworkers_suspended.html
blob: d5c62a28d04a1fd1789c8bd71536167ee6222d6a (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for sub workers+bfcache behavior</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
  <script type="application/javascript">



  /**
   * - main page opens testUrl1
   * - testUrl1 ---"onpageshow"---> to main page
   * - main page ---"startWorker"---> testUrl1
   * - testUrl1 starts workers, also ---"verifyCacheData"---> main page
   * - main page ---"changeLocation"---> testUrl1
   * - testUrl1 navigated to testUrl2
   * - testUrl2 ---"onpageshow"---> to main page
   * - main page ---"startWorker"---> testUrl2
   * - testUrl2 starts workers, also ---"verifyCacheData"---> main page
   * - main page ---"goBack"---> testUrl2
   * - testUrl2 navigates back to testUrl1
   * - testUrl1 ---"onpageshow"---> to main page
   * - main page checks cache data and ---"finish"---> testUrl2
   * - testUrl1 ---"finished"---> to main page
   */
  var testUrl1 = "window_suspended.html?page1Shown";
  var counter = 0;
  const SUB_WORKERS = 3;

  function cacheData() {
    return caches.open("test")
           .then(function(cache) {
             return cache.match("http://mochi.test:888/foo");
           })
           .then(function(response) {
             return response.text();
           });
  }

  function runTest() {
    var bc1 = new BroadcastChannel("page1Shown");
    bc1.onmessage = async (msgEvent) => {
      var msg = msgEvent.data;
      var command = msg.command;
      info(`Main page, received command=${command}`);
      if (command == "onpageshow") {
        info("Page1Shown: " + msg.location);
        // First time this page is shown.
        if (counter == 0) {
          ok(!msg.persisted, "test page should have been persisted initially");
          var workerMessage = { type: "page1", count: SUB_WORKERS };
          bc1.postMessage({command: "startWorker", workerMessage});
        } else {
          is(msg.persisted, true, "test page should have been persisted in pageshow");
          var promise = new Promise((resolve, reject) => {
            info("Waiting a few seconds...");
            setTimeout(resolve, 10000);
          });

          promise.then(function() {
            info("Retrieving data from cache...");
            return cacheData();
          })

          .then(function(content) {
            is(content.indexOf("page1-"), 0, "We have data from the worker");
          })
          .then(function() {
            bc1.postMessage({command: "finish"});
          });
        }
        counter++;
      } else if (command == "workerMessage") {
        is(msg.workerMessage, "ready", "We want to receive: -ready-");
      } else if (command == "verifyCacheData") {
        var content = await cacheData();
        is(content.indexOf("page1-"), 0, "We have data from the worker");
        bc1.postMessage({command: "changeLocation"});
      } else if (command == "finished") {
        bc1.close();
        bc2.close();
        SimpleTest.finish();
      }
    }
    var bc2 = new BroadcastChannel("page2Shown");
    bc2.onmessage = async (msgEvent) => {
      var msg = msgEvent.data;
      var command = msg.command;
      if (command == "onpageshow") {
        info("Page1Shown: " + msg.location);
        var workerMessage = { type: "page2" };
        bc2.postMessage({command: "startWorker", workerMessage});
      } else if (command == "workerMessage") {
        is(msg.workerMessage, "ready", "We want to receive: -ready-");
      } else if (command == "verifyCacheData") {
        var content = await cacheData();
        is(content, "page2-0", "We have data from the second worker");
        bc2.postMessage({command: "goBack"});
      }
    }

    SpecialPowers.pushPrefEnv({ set: [
      ["dom.caches.enabled", true],
      ["dom.caches.testing.enabled", true],
      // If Fission is disabled, the pref is no-op.
      ["fission.bfcacheInParent", true],
    ] },
    function() {
      window.open(testUrl1, "", "noopener");
    });

  }

  if (isXOrigin) {
    // Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5)
    // Acquire storage access permission here so that the BroadcastChannel used to
    // communicate with the opened windows works in xorigin tests. Otherwise,
    // the iframe containing this page is isolated from first-party storage access,
    // which isolates BroadcastChannel communication.
    SpecialPowers.wrap(document).notifyUserGestureActivation();
    SpecialPowers.pushPrefEnv({
      set: [["privacy.partition.always_partition_third_party_non_cookie_storage", false]],
    }).then(() => {
      SpecialPowers.pushPermissions([{'type': 'storageAccessAPI', 'allow': 1, 'context': document}], () =>{
        SpecialPowers.wrap(document).requestStorageAccess().then(() => {
          runTest();
        }).then(() => {
          SpecialPowers.removePermission("3rdPartyStorage^http://mochi.test:8888", "http://mochi.xorigin-test:8888");
        });
      });
    });
  } else {
    runTest();
  }

  SimpleTest.waitForExplicitFinish();
  SimpleTest.requestFlakyTimeout("untriaged");

  </script>
</body>
</html>