summaryrefslogtreecommitdiffstats
path: root/docshell/test/navigation/test_online_offline_bfcache.html
blob: 4ad90fd52e935e5c8cf56bacae7e9c6096706283 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Online/Offline with BFCache</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>

    /*
     * The test is designed to work with and without bfcache.
     * (1) First the test opens a window which then loads another page which
     * goes back to the original page to detect if bfcache is enabled. If
     * bfcache isn't enabled, close message is sent to the opened window and it
     * closes itself and sends a message back and the test finishes.
     * (2) The browser is set to offline mode. The opened page sends message
     * that it has received offline event. This controller page then asks the
     * page to go forward. The page which comes out from the bfcache gets
     * offline event and sends message about that to this controller.
     * (3) Browser is set to online mode. Similar cycle as with offline happens.
     * (4) Controller page sends close message to the opened window and it
     * closes itself and sends a message back and the test finishes.
     */

    function offlineOnline(online) {
      function offlineFn() {
        /* eslint-env mozilla/chrome-script */
        Services.io.offline = true;
      }
      function onlineFn() {
        /* eslint-env mozilla/chrome-script */
        Services.io.offline = false;
      }
      SpecialPowers.loadChromeScript(online ? onlineFn : offlineFn);
    }

    var bc = new BroadcastChannel("online_offline_bfcache");
    var pageshowCount = 0;
    var offlineCount = 0;
    var onlineCount = 0;

    bc.onmessage = function(event) {
      if (event.data.event == "pageshow") {
        ++pageshowCount;
        info("pageshow " + pageshowCount);
        if (pageshowCount == 1) {
          ok(!event.data.persisted);
          bc.postMessage("nextpage");
        } else if (pageshowCount == 2) {
          ok(!event.data.persisted);
          bc.postMessage("back");
        } else if (pageshowCount == 3) {
          if (!event.data.persisted) {
            info("BFCache is not enabled, return early");
            bc.postMessage("close");
          } else {
            offlineOnline(false);
          }
        }
      } else if (event.data == "offline") {
        ++offlineCount;
        info("offline " + offlineCount);
        if (offlineCount == 1) {
          bc.postMessage("forward");
        } else if (offlineCount == 2) {
          offlineOnline(true);
        } else {
          ok(false, "unexpected offline event");
        }
      }  else if (event.data == "online") {
        ++onlineCount;
        info("online " + onlineCount);
        if (onlineCount == 1) {
          bc.postMessage("back");
        } else if (onlineCount == 2) {
          bc.postMessage("close");
        } else {
          ok(false, "unexpected online event");
        }
      } else if ("closed") {
        ok(true, "Did pass the test");
        bc.close();
        SimpleTest.finish();
      }
    };

    function runTest() {
      SpecialPowers.pushPrefEnv({"set": [["network.manage-offline-status", false]]}, function() {
          window.open("file_online_offline_bfcache.html", "", "noopener");
      });
    }

    SimpleTest.waitForExplicitFinish();
  </script>
</head>
<body onload="runTest()">
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
</body>
</html>