summaryrefslogtreecommitdiffstats
path: root/dom/file/tests/common_fileReader.js
blob: 9ee6a32b937ce12f0ccc8c5b9f009330f3ed9607 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
function test_setup() {
  return new Promise(resolve => {
    const minFileSize = 20000;

    // Create strings containing data we'll test with. We'll want long
    // strings to ensure they span multiple buffers while loading
    let testTextData = "asd b\tlah\u1234w\u00a0r";
    while (testTextData.length < minFileSize) {
      testTextData = testTextData + testTextData;
    }

    let testASCIIData = "abcdef 123456\n";
    while (testASCIIData.length < minFileSize) {
      testASCIIData = testASCIIData + testASCIIData;
    }

    let testBinaryData = "";
    for (let i = 0; i < 256; i++) {
      testBinaryData += String.fromCharCode(i);
    }
    while (testBinaryData.length < minFileSize) {
      testBinaryData = testBinaryData + testBinaryData;
    }

    let dataurldata0 = testBinaryData.substr(
      0,
      testBinaryData.length - (testBinaryData.length % 3)
    );
    let dataurldata1 = testBinaryData.substr(
      0,
      testBinaryData.length - 2 - (testBinaryData.length % 3)
    );
    let dataurldata2 = testBinaryData.substr(
      0,
      testBinaryData.length - 1 - (testBinaryData.length % 3)
    );

    //Set up files for testing
    let openerURL = SimpleTest.getTestFileURL("fileapi_chromeScript.js");
    let opener = SpecialPowers.loadChromeScript(openerURL);

    opener.addMessageListener("files.opened", message => {
      let [
        asciiFile,
        binaryFile,
        nonExistingFile,
        utf8TextFile,
        utf16TextFile,
        emptyFile,
        dataUrlFile0,
        dataUrlFile1,
        dataUrlFile2,
      ] = message;

      resolve({
        blobs: {
          asciiFile,
          binaryFile,
          nonExistingFile,
          utf8TextFile,
          utf16TextFile,
          emptyFile,
          dataUrlFile0,
          dataUrlFile1,
          dataUrlFile2,
        },
        data: {
          text: testTextData,
          ascii: testASCIIData,
          binary: testBinaryData,
          url0: dataurldata0,
          url1: dataurldata1,
          url2: dataurldata2,
        },
      });
    });

    opener.sendAsyncMessage("files.open", [
      testASCIIData,
      testBinaryData,
      null,
      convertToUTF8(testTextData),
      convertToUTF16(testTextData),
      "",
      dataurldata0,
      dataurldata1,
      dataurldata2,
    ]);
  });
}

function runBasicTests(data) {
  return test_basic()
    .then(() => {
      return test_readAsText(data.blobs.asciiFile, data.data.ascii);
    })
    .then(() => {
      return test_readAsBinaryString(data.blobs.binaryFile, data.data.binary);
    })
    .then(() => {
      return test_readAsArrayBuffer(data.blobs.binaryFile, data.data.binary);
    });
}

function runEncodingTests(data) {
  return test_readAsTextWithEncoding(
    data.blobs.asciiFile,
    data.data.ascii,
    data.data.ascii.length,
    ""
  )
    .then(() => {
      return test_readAsTextWithEncoding(
        data.blobs.asciiFile,
        data.data.ascii,
        data.data.ascii.length,
        "iso8859-1"
      );
    })
    .then(() => {
      return test_readAsTextWithEncoding(
        data.blobs.utf8TextFile,
        data.data.text,
        convertToUTF8(data.data.text).length,
        "utf8"
      );
    })
    .then(() => {
      return test_readAsTextWithEncoding(
        data.blobs.utf16TextFile,
        data.data.text,
        convertToUTF16(data.data.text).length,
        "utf-16"
      );
    })
    .then(() => {
      return test_readAsTextWithEncoding(data.blobs.emptyFile, "", 0, "");
    })
    .then(() => {
      return test_readAsTextWithEncoding(data.blobs.emptyFile, "", 0, "utf8");
    })
    .then(() => {
      return test_readAsTextWithEncoding(data.blobs.emptyFile, "", 0, "utf-16");
    });
}

function runEmptyTests(data) {
  return test_onlyResult()
    .then(() => {
      return test_readAsText(data.blobs.emptyFile, "");
    })
    .then(() => {
      return test_readAsBinaryString(data.blobs.emptyFile, "");
    })
    .then(() => {
      return test_readAsArrayBuffer(data.blobs.emptyFile, "");
    })
    .then(() => {
      return test_readAsDataURL(data.blobs.emptyFile, convertToDataURL(""), 0);
    });
}

function runTwiceTests(data) {
  return test_readAsTextTwice(data.blobs.asciiFile, data.data.ascii)
    .then(() => {
      return test_readAsBinaryStringTwice(
        data.blobs.binaryFile,
        data.data.binary
      );
    })
    .then(() => {
      return test_readAsDataURLTwice(
        data.blobs.binaryFile,
        convertToDataURL(data.data.binary),
        data.data.binary.length
      );
    })
    .then(() => {
      return test_readAsArrayBufferTwice(
        data.blobs.binaryFile,
        data.data.binary
      );
    })
    .then(() => {
      return test_readAsArrayBufferTwice2(
        data.blobs.binaryFile,
        data.data.binary
      );
    });
}

function runOtherTests(data) {
  return test_readAsDataURL_customLength(
    data.blobs.dataUrlFile0,
    convertToDataURL(data.data.url0),
    data.data.url0.length,
    0
  )
    .then(() => {
      return test_readAsDataURL_customLength(
        data.blobs.dataUrlFile1,
        convertToDataURL(data.data.url1),
        data.data.url1.length,
        1
      );
    })
    .then(() => {
      return test_readAsDataURL_customLength(
        data.blobs.dataUrlFile2,
        convertToDataURL(data.data.url2),
        data.data.url2.length,
        2
      );
    })
    .then(() => {
      return test_abort(data.blobs.asciiFile);
    })
    .then(() => {
      return test_abort_readAsX(data.blobs.asciiFile, data.data.ascii);
    })
    .then(() => {
      return test_nonExisting(data.blobs.nonExistingFile);
    });
}

function convertToUTF16(s) {
  let res = "";
  for (let i = 0; i < s.length; ++i) {
    c = s.charCodeAt(i);
    res += String.fromCharCode(c & 255, c >>> 8);
  }
  return res;
}

function convertToUTF8(s) {
  return unescape(encodeURIComponent(s));
}

function convertToDataURL(s) {
  return "data:application/octet-stream;base64," + btoa(s);
}

function loadEventHandler_string(
  event,
  resolve,
  reader,
  data,
  dataLength,
  testName
) {
  is(event.target, reader, "Correct target.");
  is(
    event.target.readyState,
    FileReader.DONE,
    "readyState in test " + testName
  );
  is(event.target.error, null, "no error in test " + testName);
  is(event.target.result, data, "result in test " + testName);
  is(event.lengthComputable, true, "lengthComputable in test " + testName);
  is(event.loaded, dataLength, "loaded in test " + testName);
  is(event.total, dataLength, "total in test " + testName);
  resolve();
}

function loadEventHandler_arrayBuffer(event, resolve, reader, data, testName) {
  is(
    event.target.readyState,
    FileReader.DONE,
    "readyState in test " + testName
  );
  is(event.target.error, null, "no error in test " + testName);
  is(event.lengthComputable, true, "lengthComputable in test " + testName);
  is(event.loaded, data.length, "loaded in test " + testName);
  is(event.total, data.length, "total in test " + testName);
  is(
    event.target.result.byteLength,
    data.length,
    "array buffer size in test " + testName
  );

  let u8v = new Uint8Array(event.target.result);
  is(
    String.fromCharCode.apply(String, u8v),
    data,
    "array buffer contents in test " + testName
  );
  u8v = null;

  if ("SpecialPowers" in self) {
    SpecialPowers.gc();

    is(
      event.target.result.byteLength,
      data.length,
      "array buffer size after gc in test " + testName
    );
    u8v = new Uint8Array(event.target.result);
    is(
      String.fromCharCode.apply(String, u8v),
      data,
      "array buffer contents after gc in test " + testName
    );
  }

  resolve();
}

function test_basic() {
  return new Promise(resolve => {
    is(FileReader.EMPTY, 0, "correct EMPTY value");
    is(FileReader.LOADING, 1, "correct LOADING value");
    is(FileReader.DONE, 2, "correct DONE value");
    resolve();
  });
}

function test_readAsText(blob, text) {
  return new Promise(resolve => {
    let onloadHasRun = false;
    let onloadStartHasRun = false;

    let r = new FileReader();
    is(r.readyState, FileReader.EMPTY, "correct initial text readyState");

    r.onload = event => {
      loadEventHandler_string(
        event,
        resolve,
        r,
        text,
        text.length,
        "readAsText"
      );
    };

    r.addEventListener("load", () => {
      onloadHasRun = true;
    });
    r.addEventListener("loadstart", () => {
      onloadStartHasRun = true;
    });

    r.readAsText(blob);

    is(r.readyState, FileReader.LOADING, "correct loading text readyState");
    is(onloadHasRun, false, "text loading must be async");
    is(onloadStartHasRun, false, "text loadstart should fire async");
  });
}

function test_readAsBinaryString(blob, text) {
  return new Promise(resolve => {
    let onloadHasRun = false;
    let onloadStartHasRun = false;

    let r = new FileReader();
    is(r.readyState, FileReader.EMPTY, "correct initial binary readyState");

    r.addEventListener("load", function () {
      onloadHasRun = true;
    });
    r.addEventListener("loadstart", function () {
      onloadStartHasRun = true;
    });

    r.readAsBinaryString(blob);

    r.onload = event => {
      loadEventHandler_string(
        event,
        resolve,
        r,
        text,
        text.length,
        "readAsBinaryString"
      );
    };

    is(r.readyState, FileReader.LOADING, "correct loading binary readyState");
    is(onloadHasRun, false, "binary loading must be async");
    is(onloadStartHasRun, false, "binary loadstart should fire async");
  });
}

function test_readAsArrayBuffer(blob, text) {
  return new Promise(resolve => {
    let onloadHasRun = false;
    let onloadStartHasRun = false;

    r = new FileReader();
    is(
      r.readyState,
      FileReader.EMPTY,
      "correct initial arrayBuffer readyState"
    );

    r.addEventListener("load", function () {
      onloadHasRun = true;
    });
    r.addEventListener("loadstart", function () {
      onloadStartHasRun = true;
    });

    r.readAsArrayBuffer(blob);

    r.onload = event => {
      loadEventHandler_arrayBuffer(
        event,
        resolve,
        r,
        text,
        "readAsArrayBuffer"
      );
    };

    is(
      r.readyState,
      FileReader.LOADING,
      "correct loading arrayBuffer readyState"
    );
    is(onloadHasRun, false, "arrayBuffer loading must be async");
    is(onloadStartHasRun, false, "arrayBuffer loadstart should fire sync");
  });
}

// Test a variety of encodings, and make sure they work properly
function test_readAsTextWithEncoding(blob, text, length, charset) {
  return new Promise(resolve => {
    let r = new FileReader();
    r.onload = event => {
      loadEventHandler_string(
        event,
        resolve,
        r,
        text,
        length,
        "readAsText-" + charset
      );
    };
    r.readAsText(blob, charset);
  });
}

// Test get result without reading
function test_onlyResult() {
  return new Promise(resolve => {
    let r = new FileReader();
    is(
      r.readyState,
      FileReader.EMPTY,
      "readyState in test reader get result without reading"
    );
    is(r.error, null, "no error in test reader get result without reading");
    is(r.result, null, "result in test reader get result without reading");
    resolve();
  });
}

function test_readAsDataURL(blob, text, length) {
  return new Promise(resolve => {
    let r = new FileReader();
    r.onload = event => {
      loadEventHandler_string(event, resolve, r, text, length, "readAsDataURL");
    };
    r.readAsDataURL(blob);
  });
}

// Test reusing a FileReader to read multiple times
function test_readAsTextTwice(blob, text) {
  return new Promise(resolve => {
    let r = new FileReader();
    r.onload = event => {
      loadEventHandler_string(
        event,
        () => {},
        r,
        text,
        text.length,
        "readAsText-reused-once"
      );
    };

    let anotherListener = event => {
      let r1 = event.target;
      r1.removeEventListener("load", anotherListener);
      r1.onload = evt => {
        loadEventHandler_string(
          evt,
          resolve,
          r1,
          text,
          text.length,
          "readAsText-reused-twice"
        );
      };
      r1.readAsText(blob);
    };

    r.addEventListener("load", anotherListener);
    r.readAsText(blob);
  });
}

// Test reusing a FileReader to read multiple times
function test_readAsBinaryStringTwice(blob, text) {
  return new Promise(resolve => {
    let r = new FileReader();
    r.onload = event => {
      loadEventHandler_string(
        event,
        () => {},
        r,
        text,
        text.length,
        "readAsBinaryString-reused-once"
      );
    };

    let anotherListener = event => {
      let r1 = event.target;
      r1.removeEventListener("load", anotherListener);
      r1.onload = evt => {
        loadEventHandler_string(
          evt,
          resolve,
          r1,
          text,
          text.length,
          "readAsBinaryString-reused-twice"
        );
      };
      r1.readAsBinaryString(blob);
    };

    r.addEventListener("load", anotherListener);
    r.readAsBinaryString(blob);
  });
}

function test_readAsDataURLTwice(blob, text, length) {
  return new Promise(resolve => {
    let r = new FileReader();
    r.onload = event => {
      loadEventHandler_string(
        event,
        () => {},
        r,
        text,
        length,
        "readAsDataURL-reused-once"
      );
    };

    let anotherListener = event => {
      let r1 = event.target;
      r1.removeEventListener("load", anotherListener);
      r1.onload = evt => {
        loadEventHandler_string(
          evt,
          resolve,
          r1,
          text,
          length,
          "readAsDataURL-reused-twice"
        );
      };
      r1.readAsDataURL(blob);
    };

    r.addEventListener("load", anotherListener);
    r.readAsDataURL(blob);
  });
}

function test_readAsArrayBufferTwice(blob, text) {
  return new Promise(resolve => {
    let r = new FileReader();
    r.onload = event => {
      loadEventHandler_arrayBuffer(
        event,
        () => {},
        r,
        text,
        "readAsArrayBuffer-reused-once"
      );
    };

    let anotherListener = event => {
      let r1 = event.target;
      r1.removeEventListener("load", anotherListener);
      r1.onload = evt => {
        loadEventHandler_arrayBuffer(
          evt,
          resolve,
          r1,
          text,
          "readAsArrayBuffer-reused-twice"
        );
      };
      r1.readAsArrayBuffer(blob);
    };

    r.addEventListener("load", anotherListener);
    r.readAsArrayBuffer(blob);
  });
}

// Test first reading as ArrayBuffer then read as something else (BinaryString)
// and doesn't crash
function test_readAsArrayBufferTwice2(blob, text) {
  return new Promise(resolve => {
    let r = new FileReader();
    r.onload = event => {
      loadEventHandler_arrayBuffer(
        event,
        () => {},
        r,
        text,
        "readAsArrayBuffer-reused-once2"
      );
    };

    let anotherListener = event => {
      let r1 = event.target;
      r1.removeEventListener("load", anotherListener);
      r1.onload = evt => {
        loadEventHandler_string(
          evt,
          resolve,
          r1,
          text,
          text.length,
          "readAsArrayBuffer-reused-twice2"
        );
      };
      r1.readAsBinaryString(blob);
    };

    r.addEventListener("load", anotherListener);
    r.readAsArrayBuffer(blob);
  });
}

function test_readAsDataURL_customLength(blob, text, length, numb) {
  return new Promise(resolve => {
    is(length % 3, numb, "Want to test data with length %3 == " + numb);
    let r = new FileReader();
    r.onload = event => {
      loadEventHandler_string(
        event,
        resolve,
        r,
        text,
        length,
        "dataurl reading, %3 = " + numb
      );
    };
    r.readAsDataURL(blob);
  });
}

// Test abort()
function test_abort(blob) {
  return new Promise(resolve => {
    let abortHasRun = false;
    let loadEndHasRun = false;

    let r = new FileReader();

    r.onabort = function (event) {
      is(abortHasRun, false, "abort should only fire once");
      is(loadEndHasRun, false, "loadend shouldn't have fired yet");
      abortHasRun = true;
      is(
        event.target.readyState,
        FileReader.DONE,
        "should be DONE while firing onabort"
      );
      is(
        event.target.error.name,
        "AbortError",
        "error set to AbortError for aborted reads"
      );
      is(
        event.target.result,
        null,
        "file data should be null on aborted reads"
      );
    };

    r.onloadend = function (event) {
      is(abortHasRun, true, "abort should fire before loadend");
      is(loadEndHasRun, false, "loadend should only fire once");
      loadEndHasRun = true;
      is(
        event.target.readyState,
        FileReader.DONE,
        "should be DONE while firing onabort"
      );
      is(
        event.target.error.name,
        "AbortError",
        "error set to AbortError for aborted reads"
      );
      is(
        event.target.result,
        null,
        "file data should be null on aborted reads"
      );
    };

    r.onload = function () {
      ok(false, "load should not fire for aborted reads");
    };
    r.onerror = function () {
      ok(false, "error should not fire for aborted reads");
    };
    r.onprogress = function () {
      ok(false, "progress should not fire for aborted reads");
    };

    let abortThrew = false;
    try {
      r.abort();
    } catch (e) {
      abortThrew = true;
    }

    is(abortThrew, false, "abort() doesn't throw");
    is(abortHasRun, false, "abort() is a no-op unless loading");

    r.readAsText(blob);
    r.abort();

    is(abortHasRun, true, "abort should fire sync");
    is(loadEndHasRun, true, "loadend should fire sync");

    resolve();
  });
}

// Test calling readAsX to cause abort()
function test_abort_readAsX(blob, text) {
  return new Promise(resolve => {
    let reuseAbortHasRun = false;

    let r = new FileReader();
    r.onabort = function (event) {
      is(reuseAbortHasRun, false, "abort should only fire once");
      reuseAbortHasRun = true;
      is(
        event.target.readyState,
        FileReader.DONE,
        "should be DONE while firing onabort"
      );
      is(
        event.target.error.name,
        "AbortError",
        "error set to AbortError for aborted reads"
      );
      is(
        event.target.result,
        null,
        "file data should be null on aborted reads"
      );
    };
    r.onload = function () {
      ok(false, "load should fire for nested reads");
    };

    let abortThrew = false;
    try {
      r.abort();
    } catch (e) {
      abortThrew = true;
    }

    is(abortThrew, false, "abort() should not throw");
    is(reuseAbortHasRun, false, "abort() is a no-op unless loading");
    r.readAsText(blob);

    let readThrew = false;
    try {
      r.readAsText(blob);
    } catch (e) {
      readThrew = true;
    }

    is(readThrew, true, "readAsText() must throw if loading");
    is(reuseAbortHasRun, false, "abort should not fire");

    r.onload = event => {
      loadEventHandler_string(
        event,
        resolve,
        r,
        text,
        text.length,
        "reuse-as-abort reading"
      );
    };
  });
}

// Test reading from nonexistent files
function test_nonExisting(blob) {
  return new Promise(resolve => {
    let r = new FileReader();

    r.onerror = function (event) {
      is(
        event.target.readyState,
        FileReader.DONE,
        "should be DONE while firing onerror"
      );
      is(
        event.target.error.name,
        "NotFoundError",
        "error set to NotFoundError for nonexistent files"
      );
      is(
        event.target.result,
        null,
        "file data should be null on aborted reads"
      );
      resolve();
    };
    r.onload = function (event) {
      is(false, "nonexistent file shouldn't load! (FIXME: bug 1122788)");
    };

    let didThrow = false;
    try {
      r.readAsDataURL(blob);
    } catch (ex) {
      didThrow = true;
    }

    // Once this test passes, we should test that onerror gets called and
    // that the FileReader object is in the right state during that call.
    is(
      didThrow,
      false,
      "shouldn't throw when opening nonexistent file, should fire error instead"
    );
  });
}