summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/serviceworkers/test_post_message_advanced.html
blob: 8ea0d2300d10a646726c2e0e31ec7017ba223907 (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
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 982726 - Test service worker post message advanced </title>
  <script type="text/javascript" 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"></pre>
<script class="testbody" type="text/javascript">
  var registration;
  var base = ["string", true, 42];
  var blob = new Blob(["blob_content"]);
  var file = new File(["file_content"], "file");
  var obj = { body : "object_content" };

  function readBlob(blob) {
    return new Promise(function(resolve, reject) {
      var reader = new FileReader();
      reader.onloadend = () => resolve(reader.result);
      reader.readAsText(blob);
    });
  }

  function equals(v1, v2) {
    return Promise.all([v1, v2]).then(function(val) {
      ok(val[0] === val[1], "Values should match.");
    });
  }

  function blob_equals(b1, b2) {
    return equals(readBlob(b1), readBlob(b2));
  }

  function file_equals(f1, f2) {
    return equals(f1.name, f2.name).then(blob_equals(f1, f2));
  }

  function obj_equals(o1, o2) {
    return equals(o1.body, o2.body);
  }

  function start() {
    return navigator.serviceWorker.register("message_posting_worker.js",
                                            { scope: "./sw_clients/" })
      .then((swr) => registration = swr);
  }

  function unregister() {
    return registration.unregister().then(function(result) {
      ok(result, "Unregister should return true.");
    }, function(e) {
      dump("Unregistering the SW failed with " + e + "\n");
    });
  }

  function testPostMessageObject(obj, test) {
    var p = new Promise(function(res, rej) {
      window.onmessage = function(e) {
        if (e.data === "READY") {
          registration.active.postMessage(obj)
        } else {
          test(obj, e.data).then(res);
        }
      }
    });

    var content = document.getElementById("content");
    ok(content, "Parent exists.");

    iframe = document.createElement("iframe");
    iframe.setAttribute('src', "sw_clients/service_worker_controlled.html");
    content.appendChild(iframe);

    return p.then(() => content.removeChild(iframe));
  }

  function runTest() {
    start()
      .then(testPostMessageObject.bind(this, base[0], equals))
      .then(testPostMessageObject.bind(this, base[1], equals))
      .then(testPostMessageObject.bind(this, base[2], equals))
      .then(testPostMessageObject.bind(this, blob, blob_equals))
      .then(testPostMessageObject.bind(this, file, file_equals))
      .then(testPostMessageObject.bind(this, obj, obj_equals))
      .then(unregister)
      .catch(function(e) {
        ok(false, "Some test failed with error " + e);
      }).then(SimpleTest.finish);
  }

  SimpleTest.waitForExplicitFinish();
  SpecialPowers.pushPrefEnv({"set": [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.openWindow.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true]
  ]}, runTest);
</script>
</pre>
</body>
</html>