summaryrefslogtreecommitdiffstats
path: root/dom/abort/tests/file_abort_controller.html
blob: 3a15fa34692b6d4fdf5b17f151198647b1ce3662 (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
<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 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('slow.sjs', { 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('slow.sjs', { 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() {
  var w = new Worker('worker_fetch_controller.js');
  w.onmessage = function(e) {
    ok(e.data, "Abort + Fetch works in workers");
    next();
  }
  w.postMessage('testWorkerAbortedFetch');
}

function testWorkerFetchAndAbort() {
  var w = new Worker('worker_fetch_controller.js');
  w.onmessage = function(e) {
    ok(e.data, "Abort + Fetch works in workers");
    next();
  }
  w.postMessage('testWorkerFetchAndAbort');
}

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

  // 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>