summaryrefslogtreecommitdiffstats
path: root/dom/xhr/tests/common_temporaryFileBlob.js
blob: 81a66238dab406c47ea3979c49185c0381cfdd04 (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
var data = new Array(256).join("1234567890ABCDEF");

function createXHR() {
  var xhr = new XMLHttpRequest();
  xhr.open("POST", "temporaryFileBlob.sjs");
  xhr.responseType = 'blob';
  xhr.send({toString: function() { 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!");
    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!");
    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: function() { 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');
}