summaryrefslogtreecommitdiffstats
path: root/dom/file/ipc/tests/browser_ipcBlob.js
blob: 17fe31e0bd4b328bee7edc7d1c5336c7f81abb30 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*- */

requestLongerTimeout(3);

const BASE_URI = "http://mochi.test:8888/browser/dom/file/ipc/tests/empty.html";

add_task(async function setup() {
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.partition.bloburl_per_agent_cluster", false]],
  });
});

// More than 1mb memory blob childA-parent-childB.
add_task(async function test_CtoPtoC_big() {
  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser1 = gBrowser.getBrowserForTab(tab1);

  let blob = await SpecialPowers.spawn(browser1, [], function () {
    Cu.importGlobalProperties(["Blob"]);
    let blob = new Blob([new Array(1024 * 1024).join("123456789ABCDEF")]);
    return blob;
  });

  ok(blob, "CtoPtoC-big: We have a blob!");
  is(
    blob.size,
    new Array(1024 * 1024).join("123456789ABCDEF").length,
    "CtoPtoC-big: The size matches"
  );

  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser2 = gBrowser.getBrowserForTab(tab2);

  let status = await SpecialPowers.spawn(browser2, [blob], function (blob) {
    return new Promise(resolve => {
      let fr = new content.FileReader();
      fr.readAsText(blob);
      fr.onloadend = function () {
        resolve(fr.result == new Array(1024 * 1024).join("123456789ABCDEF"));
      };
    });
  });

  ok(status, "CtoPtoC-big: Data match!");

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
});

// Less than 1mb memory blob childA-parent-childB.
add_task(async function test_CtoPtoC_small() {
  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser1 = gBrowser.getBrowserForTab(tab1);

  let blob = await SpecialPowers.spawn(browser1, [], function () {
    Cu.importGlobalProperties(["Blob"]);
    let blob = new Blob(["hello world!"]);
    return blob;
  });

  ok(blob, "CtoPtoC-small: We have a blob!");
  is(blob.size, "hello world!".length, "CtoPtoC-small: The size matches");

  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser2 = gBrowser.getBrowserForTab(tab2);

  let status = await SpecialPowers.spawn(browser2, [blob], function (blob) {
    return new Promise(resolve => {
      let fr = new content.FileReader();
      fr.readAsText(blob);
      fr.onloadend = function () {
        resolve(fr.result == "hello world!");
      };
    });
  });

  ok(status, "CtoPtoC-small: Data match!");

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
});

// More than 1mb memory blob childA-parent-childB: BroadcastChannel
add_task(async function test_CtoPtoC_bc_big() {
  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser1 = gBrowser.getBrowserForTab(tab1);

  await SpecialPowers.spawn(browser1, [], function () {
    Cu.importGlobalProperties(["Blob"]);
    var bc = new content.BroadcastChannel("test");
    bc.onmessage = function () {
      bc.postMessage(
        new Blob([new Array(1024 * 1024).join("123456789ABCDEF")])
      );
    };
  });

  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser2 = gBrowser.getBrowserForTab(tab2);

  let status = await SpecialPowers.spawn(browser2, [], function () {
    return new Promise(resolve => {
      var bc = new content.BroadcastChannel("test");
      bc.onmessage = function (e) {
        let fr = new content.FileReader();
        fr.readAsText(e.data);
        fr.onloadend = function () {
          resolve(fr.result == new Array(1024 * 1024).join("123456789ABCDEF"));
        };
      };

      bc.postMessage("GO!");
    });
  });

  ok(status, "CtoPtoC-broadcastChannel-big: Data match!");

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
});

// Less than 1mb memory blob childA-parent-childB: BroadcastChannel
add_task(async function test_CtoPtoC_bc_small() {
  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser1 = gBrowser.getBrowserForTab(tab1);

  await SpecialPowers.spawn(browser1, [], function () {
    Cu.importGlobalProperties(["Blob"]);
    var bc = new content.BroadcastChannel("test");
    bc.onmessage = function () {
      bc.postMessage(new Blob(["hello world!"]));
    };
  });

  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser2 = gBrowser.getBrowserForTab(tab2);

  let status = await SpecialPowers.spawn(browser2, [], function () {
    return new Promise(resolve => {
      var bc = new content.BroadcastChannel("test");
      bc.onmessage = function (e) {
        let fr = new content.FileReader();
        fr.readAsText(e.data);
        fr.onloadend = function () {
          resolve(fr.result == "hello world!");
        };
      };

      bc.postMessage("GO!");
    });
  });

  ok(status, "CtoPtoC-broadcastChannel-small: Data match!");

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
});

// blob URL childA-parent-childB
add_task(async function test_CtoPtoC_bc_small() {
  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser1 = gBrowser.getBrowserForTab(tab1);

  let blobURL = await SpecialPowers.spawn(browser1, [], function () {
    Cu.importGlobalProperties(["Blob"]);
    return content.URL.createObjectURL(new content.Blob(["hello world!"]));
  });

  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser2 = gBrowser.getBrowserForTab(tab2);

  let status = await SpecialPowers.spawn(
    browser2,
    [blobURL],
    function (blobURL) {
      return new Promise(resolve => {
        var xhr = new content.XMLHttpRequest();
        xhr.open("GET", blobURL);
        xhr.onloadend = function () {
          resolve(xhr.response == "hello world!");
        };

        xhr.send();
      });
    }
  );

  ok(status, "CtoPtoC-blobURL: Data match!");

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
});

// Multipart Blob childA-parent-childB.
add_task(async function test_CtoPtoC_multipart() {
  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser1 = gBrowser.getBrowserForTab(tab1);

  let blob = await SpecialPowers.spawn(browser1, [], function () {
    Cu.importGlobalProperties(["Blob"]);
    return new Blob(["!"]);
  });

  ok(blob, "CtoPtoC-multipart: We have a blob!");
  is(blob.size, "!".length, "CtoPtoC-multipart: The size matches");

  let newBlob = new Blob(["world", blob]);

  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser2 = gBrowser.getBrowserForTab(tab2);

  let status = await SpecialPowers.spawn(browser2, [newBlob], function (blob) {
    Cu.importGlobalProperties(["Blob"]);
    return new Promise(resolve => {
      let fr = new content.FileReader();
      fr.readAsText(new Blob(["hello ", blob]));
      fr.onloadend = function () {
        resolve(fr.result == "hello world!");
      };
    });
  });

  ok(status, "CtoPtoC-multipart: Data match!");

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
});

// Multipart Blob childA-parent with a max size
add_task(async function test_CtoPsize_multipart() {
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser = gBrowser.getBrowserForTab(tab);

  let blob = await SpecialPowers.spawn(browser, [], function () {
    Cu.importGlobalProperties(["Blob"]);

    let data = new Array(1024 * 512).join("A");
    let blob1 = new Blob([data]);
    let blob2 = new Blob([data]);
    let blob3 = new Blob([data]);

    return new Blob([blob1, blob2, blob3]);
  });

  ok(blob, "CtoPsize-multipart: We have a blob!");
  is(
    blob.size,
    new Array(1024 * 512).join("A").length * 3,
    "CtoPsize-multipart: The size matches"
  );

  BrowserTestUtils.removeTab(tab);
});