summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/test_suspend.html
blob: 9ab1a6a7ec9052714478a92e03f46b1ed5b35b08 (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
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for DOM Worker Threads</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test">
<script class="testbody" type="text/javascript">

  SimpleTest.waitForExplicitFinish();

  /**
   * - main page tells subpage to call startWorker()
   * - subpage starts worker
   * - worker calls setInterval() and keeps calling postMessage()
   * - onmessage(), as setup by the subpage, calls messageCallback
   * - when messageCallback gets called more than 25 times
   *    - subpage gets navigated to blank.html
   * - blank page posts message to main page, and main page calls suspendCallback()
   * - suspendCallback() schedules waitInterval() to be fired off every second
   * - after 5 times, it clears the interval and navigates subpage back
   * - suspend_window subpage starts receiving messages again and
   *    does a final call to messageCallback()
   * - finishTest() is called
   */

  var lastCount;

  var suspended = false;
  var resumed = false;
  var finished = false;
  var suspendBlankPageCurrentlyShowing = false;

  var interval;
  var oldMessageCount;
  var waitCount = 0;

  var bcSuspendWindow, bcSuspendBlank;

  function runTest() {
    bcSuspendWindow = new BroadcastChannel("suspendWindow");
    bcSuspendWindow.onmessage = (msgEvent) => {
      var msg = msgEvent.data;
      var command = msg.command;
      var data = msg.data;
      if (command == "loaded") {
        if (finished) {
          return;
        }
        bcSuspendWindow.postMessage({command: "startWorker"});
      } else if (command == "messageCallback") {
        messageCallback(data);
      } else if (command == "errorCallback") {
        errorCallback(data);
      } else if (command == "finished") {
        SimpleTest.finish();
      }
    }

    bcSuspendBlank = new BroadcastChannel("suspendBlank");
    bcSuspendBlank.onmessage = (msgEvent) => {
      var msg = msgEvent.data;
      var command = msg.command;
      if (command == "loaded") {
        suspendBlankPageCurrentlyShowing = true;
        if (suspended) {
          badOnloadCallback();
        } else {
          suspendCallback();
        }
      } else if (command == "pagehide") {
        suspendBlankPageCurrentlyShowing = false;
      }
    }

    // If Fission is disabled, the pref is no-op.
    SpecialPowers.pushPrefEnv({set: [["fission.bfcacheInParent", true]]}, () => {
      window.open("suspend_window.html", "testWin", "noopener");
    });
  }

  function finishTest() {
    if (finished) {
      return;
    }
    finished = true;
    bcSuspendWindow.postMessage({command: "finish"});
  }

  function waitInterval() {
    if (finished) {
      return;
    }
    ok(suspendBlankPageCurrentlyShowing, "correct page is showing");
    is(suspended, true, "Not suspended?");
    is(resumed, false, "Already resumed?!");
    is(lastCount, oldMessageCount, "Received a message while suspended!");
    if (++waitCount == 5) {
      clearInterval(interval);
      resumed = true;
      bcSuspendBlank.postMessage({command: "navigateBack"});
    }
  }

  function badOnloadCallback() {
    if (finished) {
      return;
    }
    ok(false, "We don't want suspend_window.html to fire a new load event, we want it to come out of the bfcache!");
    finishTest();
  }

  function suspendCallback() {
    if (finished) {
      return;
    }
    ok(suspendBlankPageCurrentlyShowing, "correct page is showing");
    is(suspended, false, "Already suspended?");
    is(resumed, false, "Already resumed?");
    suspended = true;
    oldMessageCount = lastCount;
    interval = setInterval(waitInterval, 1000);
  }

  function messageCallback(data) {
    if (finished) {
      return;
    }

    if (!suspended) {
      ok(lastCount === undefined || lastCount == data - 1,
         "Got good data, lastCount = " + lastCount + ", data = " + data);
      lastCount = data;
      if (lastCount == 25) {
        bcSuspendWindow.postMessage({command: "navigate"});
      }
      return;
    }

    ok(!suspendBlankPageCurrentlyShowing, "correct page is showing");
    is(resumed, true, "Got message before resumed!");
    is(lastCount, data - 1, "Missed a message, suspend failed!");
    finishTest();
  }

  function errorCallback(data) {
    if (finished) {
      return;
    }
    ok(false, "testWin had an error: '" + data + "'");
    finishTest();
  }

  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();
  }

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