summaryrefslogtreecommitdiffstats
path: root/dom/xhr/tests/common_temporaryFileBlob.js
blob: a84d58dd771b4fdfe570bd5027aa709c80e26e68 (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
// This file expects next() to be defined in the scope it is imported into.
/* global next */
var data = new Array(256).join("1234567890ABCDEF");

function createXHR() {
  var xhr = new XMLHttpRequest();
  xhr.open("POST", "temporaryFileBlob.sjs");
  xhr.responseType = "blob";
  xhr.send({
    toString() {
      return data;
    },
  });
  return xhr;
}

function test_simple() {
  info("Simple test");

  var xhr = createXHR();

  xhr.onloadend = function () {
    ok(xhr.response instanceof Blob, "We have a blob!");
    ok(!(xhr.response instanceof File), "Our blob is not a file!");
    if ("SpecialPowers" in self) {
      is(
        SpecialPowers.wrap(xhr.response).blobImplType,
        "StreamBlobImpl[TemporaryFileBlobImpl]",
        "We have a blob stored into a stream file"
      );
    }
    is(xhr.response.size, data.length, "Data length matches");

    var fr = new FileReader();
    fr.readAsText(xhr.response);
    fr.onload = function () {
      is(fr.result, data, "Data content matches");
      next();
    };
  };
}

function test_abort() {
  info("Aborting during onloading");

  var xhr = createXHR();

  xhr.onprogress = function () {
    xhr.abort();
  };

  xhr.onloadend = function () {
    ok(!xhr.response, "We should not have a Blob!");
    next();
  };
}

function test_reuse() {
  info("Reuse test");

  var xhr = createXHR();

  var count = 0;
  xhr.onloadend = function () {
    ok(xhr.response instanceof Blob, "We have a blob!");
    ok(!(xhr.response instanceof File), "Our blob is not a file!");
    if ("SpecialPowers" in self) {
      is(
        SpecialPowers.wrap(xhr.response).blobImplType,
        "StreamBlobImpl[TemporaryFileBlobImpl]",
        "We have a blob stored into a stream file"
      );
    }
    is(xhr.response.size, data.length, "Data length matches");

    var fr = new FileReader();
    fr.readAsText(xhr.response);
    fr.onload = function () {
      is(fr.result, data, "Data content matches");
      if (++count > 2) {
        next();
        return;
      }

      xhr.open("POST", "temporaryFileBlob.sjs");
      xhr.responseType = "blob";
      xhr.send({
        toString() {
          return data;
        },
      });
    };
  };
}

function test_worker_generic(test) {
  var w = new Worker("worker_temporaryFileBlob.js");
  w.onmessage = function (e) {
    if (e.data.type == "info") {
      info(e.data.msg);
    } else if (e.data.type == "check") {
      ok(e.data.what, e.data.msg);
    } else if (e.data.type == "finish") {
      next();
    } else {
      ok(false, "Something wrong happened");
    }
  };

  w.postMessage(test);
}

function test_worker() {
  info("XHR in workers");
  test_worker_generic("simple");
}

function test_worker_abort() {
  info("XHR in workers");
  test_worker_generic("abort");
}

function test_worker_reuse() {
  info("XHR in workers");
  test_worker_generic("reuse");
}