summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/fetch/file_fetch_controller.html
blob: 026ff16a873f5b022e1076269243891dad8691b0 (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
<script>
function ok(a, msg) {
  parent.postMessage({ type: "check", status: !!a, message: msg }, "*");
}

function is(a, b, msg) {
  ok(a === b, msg);
}

function testWebIDL() {
  ok("FetchController" in self, "We have a FetchController prototype");
  ok("FetchSignal" in self, "We have a FetchSignal prototype");

  var fc = new FetchController();
  ok(!!fc, "FetchController can be created");
  ok(fc instanceof FetchController, "FetchController is a FetchController");

  ok(!!fc.signal, "FetchController has a signal");
  ok(fc.signal instanceof FetchSignal, "fetchSignal is a FetchSignal");
  is(fc.signal.aborted, false, "By default FetchSignal.aborted is false");
  next();
}

function testUpdateData() {
  var fc = new FetchController();
 
  is(fc.signal.aborted, false, "By default FetchSignal.aborted is false");
 
  fc.abort();
  is(fc.signal.aborted, true, "Signal is aborted");
 
  next();
}
 
function testFollowingOurself() {
  // Let's follow ourself
  var fc = new FetchController();
  fc.follow(fc.signal);
 
  fc.abort();
  is(fc.signal.aborted, true, "Signal is aborted");
 
  next();
}
 
function testFollowingOther() {
  // Let's follow another one
  var fc1 = new FetchController();
  var fc2 = new FetchController();
  fc1.follow(fc2.signal);
 
  fc2.abort();
 
  is(fc1.signal.aborted, true, "Signal is aborted");
  is(fc2.signal.aborted, true, "Signal is aborted");
 
  next();
}

function testFollowingLoop() {
  // fc1 -> fc2 -> fc3 -> fc1
  var fc1 = new FetchController();
  var fc2 = new FetchController();
  var fc3 = new FetchController();
  fc1.follow(fc2.signal);
  fc2.follow(fc3.signal);
  fc3.follow(fc1.signal);
 
  fc3.abort();
 
  is(fc1.signal.aborted, true, "Signal is aborted");
  is(fc2.signal.aborted, true, "Signal is aborted");
  is(fc3.signal.aborted, true, "Signal is aborted");
 
  next();
}
 
function testAbortEvent() {
  var fc = new FetchController();
  fc.signal.onabort = function(e) {
    is(e.type, "abort", "Abort received");
    next();
  }
  fc.abort();
}
 
function testAbortedFetch() {
  var fc = new FetchController();
  fc.abort();

  fetch('data:,foo', { signal: fc.signal }).then(() => {
    ok(false, "Fetch should not return a resolved promise");
  }, e => {
    is(e.name, "AbortError", "We have an abort error");
  }).then(next);
}

function testFetchAndAbort() {
  var fc = new FetchController();

  var p = fetch('data:,foo', { signal: fc.signal });
  fc.abort();

  p.then(() => {
    ok(false, "Fetch should not return a resolved promise");
  }, e => {
    is(e.name, "AbortError", "We have an abort error");
  }).then(next);
}

function testWorkerAbortedFetch() {
  function worker() {
    var fc = new FetchController();
    fc.abort();

    fetch('data:,foo', { signal: fc.signal }).then(() => {
      postMessage(false);
    }, e => {
      postMessage(e.name == "AbortError");
    });
  }

  var str = worker.toString();
  var content = str.substring(0, str.length - 1).split('\n').splice(1).join(' ');
  var url = URL.createObjectURL(new Blob([content], { type: "application/javascript" }));
  var w = new Worker(url);
  w.onmessage = function(e) {
    ok(e.data, "Abort + Fetch works in workers");
    next();
  }
}

function testWorkerFetchAndAbort() {
  function worker() {
    var fc = new FetchController();

    var p = fetch('data:,foo', { signal: fc.signal });
    fc.abort();

    p.then(() => {
      postMessage(false);
    }, e => {
      postMessage(e.name == "AbortError");
    });
  }

  var str = worker.toString();
  var content = str.substring(0, str.length - 1).split('\n').splice(1).join(' ');
  var url = URL.createObjectURL(new Blob([content], { type: "application/javascript" }));
  var w = new Worker(url);
  w.onmessage = function(e) {
    ok(e.data, "Abort + Fetch works in workers");
    next();
  }
}

var steps = [
  // Simple stuff
  testWebIDL,
  testUpdateData,

  // Following algorithm
  testFollowingOurself,
  testFollowingOther,
  testFollowingLoop,

  // Event propagation
  testAbortEvent,

  // fetch + signaling
  testAbortedFetch,
  testFetchAndAbort,
  testWorkerAbortedFetch,
  testWorkerFetchAndAbort,
];

function next() {
  if (!steps.length) {
    parent.postMessage({ type: "finish" }, "*");
    return;
  }

  var step = steps.shift();
  step();
}

next();

</script>