summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/test_file_blob_upload.html
blob: e60e65badddfe3fd9bc86d9bfbc840a366e53053 (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
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 1203680 - Test interception of file blob uploads</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"></pre>
<script src="utils.js"></script>
<script class="testbody" type="text/javascript">
  var registration;
  var iframe;
  function start() {
    return navigator.serviceWorker.register("empty.js",
                                            { scope: "./sw_clients/" })
      .then((swr) => {
        registration = swr
        return waitForState(swr.installing, 'activated', swr);
      });
  }

  function unregister() {
    if (iframe) {
      iframe.remove();
      iframe = null;
    }

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

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

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

    return new Promise(function(resolve, reject) {
      window.addEventListener('message', function(evt) {
        if (evt.data.status === 'READY') {
          resolve();
        } else {
          reject(evt.data.result);
        }
      }, {once: true});
    });
  }

  function postBlob(body) {
    return new Promise(function(resolve, reject) {
      window.addEventListener('message', function(evt) {
        if (evt.data.status === 'OK') {
          is(JSON.stringify(body), JSON.stringify(evt.data.result),
             'body echoed back correctly');
          resolve();
        } else {
          reject(evt.data.result);
        }
      }, {once: true});

      iframe.contentWindow.postMessage({ type: 'TEST', body }, '*');
    });
  }

  function generateMessage(length) {

    var lorem =
      'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis egestas '
      'vehicula tortor eget ultrices. Sed et luctus est. Nunc eu orci ligula. '
      'In vel ornare eros, eget lacinia diam. Praesent vel metus mattis, '
      'cursus nulla sit amet, rhoncus diam. Aliquam nulla tortor, aliquet et '
      'viverra non, dignissim vel tellus. Praesent sed ex in dolor aliquet '
      'aliquet. In at facilisis sem, et aliquet eros. Maecenas feugiat nisl '
      'quis elit blandit posuere. Duis viverra odio sed eros consectetur, '
      'viverra mattis ligula volutpat.';

    var result = '';

    while (result.length < length) {
      var remaining = length - result.length;
      if (remaining < lorem.length) {
        result += lorem.slice(0, remaining);
      } else {
        result += lorem;
      }
    }

    return result;
  }

  var smallBody = generateMessage(64);
  var mediumBody = generateMessage(1024);

  // TODO: Test large bodies over the default pipe size.  Currently stalls
  //       due to bug 1134372.
  //var largeBody = generateMessage(100 * 1024);

  function runTest() {
    start()
      .then(withFrame)
      .then(function() {
        return postBlob({ hops: 0, message: smallBody });
      })
      .then(function() {
        return postBlob({ hops: 1, message: smallBody });
      })
      .then(function() {
        return postBlob({ hops: 10, message: smallBody });
      })
      .then(function() {
        return postBlob({ hops: 0, message: mediumBody });
      })
      .then(function() {
        return postBlob({ hops: 1, message: mediumBody });
      })
      .then(function() {
        return postBlob({ hops: 10, message: mediumBody });
      })
      .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.testing.enabled", true]
  ]}, runTest);
</script>
</pre>
</body>
</html>