summaryrefslogtreecommitdiff
path: root/dom/base/test/test_ipc_messagemanager_blob.html
blob: 74eab29459ab01913b5b88996dab0809e2362356 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for OOP Blobs in MessageManager</title>
  <script type="application/javascript"
          src="/tests/SimpleTest/SimpleTest.js">
  </script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

  <script type="application/javascript;version=1.7">
    "use strict";

    SimpleTest.waitForExplicitFinish();

    const childFrameURL =
      "data:text/html,<!DOCTYPE HTML><html><body></body></html>";

    function childFrameScript() {
      "use strict";

      addMessageListener("test:ipcClonedMessage", function(message) {
        if (!(message.json instanceof Components.interfaces.nsIDOMBlob)) {
          sendAsyncMessage(message.name, message.json);
          return;
        }

        let reader = new FileReader();
        reader.addEventListener("load", function() {
          let response = reader.result == "this is a great success!" ?
                         message.json :
                         "error";
          sendAsyncMessage(message.name, response);
        });
        reader.readAsText(message.json);
      });
    }

    function runTests() {
      function done() {
        SpecialPowers.removePermission("browser", document);
        SimpleTest.finish();
      }

      ok("Browser prefs set.");

      let iframe = document.createElement("iframe");
      SpecialPowers.wrap(iframe).mozbrowser = true;
      iframe.id = "iframe";
      iframe.src = childFrameURL;

      iframe.addEventListener("mozbrowserloadend", function() {
        ok(true, "Got iframe load event.");

        const blobString = "this is a great success!";

        const messages = [
          "hi!",
          "",
          2,
          -.04,
          3432987324987239872948732982,
          true,
          false,
          null,
          0,

          // Make sure this one is always last.
          new Blob(["this ", "is ", "a ", "great ", "success!"],
                   {"type" : "text\/plain"}),
        ];
        let receivedMessageIndex = 0;

        let mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
        mm.addMessageListener("test:ipcClonedMessage", function(message) {
          let data = message.json;

          if (data instanceof Blob) {
            is(receivedMessageIndex, messages.length - 1, "Blob is last");
            is (data.size,
                messages[receivedMessageIndex].size,
                "Correct blob size");
            is (data.type,
                messages[receivedMessageIndex].type,
                "Correct blob type");

            let result1, result2;

            let reader1 = new FileReader();
            reader1.onload = function() {
              result1 = reader1.result == blobString ? reader1.result : "bad1";
              if (result2) {
                is(result1, result2, "Same results");
                done();
              }
            };

            let reader2 = new FileReader();
            reader2.onload = function() {
              result2 = reader2.result == blobString ? reader2.result : "bad2";
              if (result1) {
                is(result1, result2, "Same results");
                done();
              }
            };

            reader1.readAsText(data);
            reader2.readAsText(messages[receivedMessageIndex]);
            return;
          }

          is(message.json,
             messages[receivedMessageIndex++],
             "Got correct round-tripped response");
        });
        mm.loadFrameScript("data:,(" + childFrameScript.toString() + ")();",
                           false);

        for (let message of messages) {
          mm.sendAsyncMessage("test:ipcClonedMessage", message);
        }
      });

      document.body.appendChild(iframe);
    }

    addEventListener("load", function() {
      info("Got load event.");

      SpecialPowers.addPermission("browser", true, document);
      SpecialPowers.pushPrefEnv({
        "set": [
          ["dom.ipc.browser_frames.oop_by_default", true],
          ["dom.mozBrowserFramesEnabled", true],
          ["network.disable.ipc.security", true],
          ["browser.pagethumbnails.capturing_disabled", true]
        ]
      }, runTests);
    });
  </script>
</body>
</html>