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

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

function testObserver() {
  ok("FetchObserver" in self, "We have a FetchObserver prototype");

  fetch('http://mochi.test:8888/tests/dom/tests/mochitest/fetch/slow.sjs', { observe: o => {
    ok(!!o, "We have an observer");
    ok(o instanceof FetchObserver, "The correct object has been passed");
    is(o.state, "requesting", "By default the state is requesting");
    next();
  }});
}

function testObserveAbort() {
  var ac = new AbortController();

  fetch('http://mochi.test:8888/tests/dom/tests/mochitest/fetch/slow.sjs', {
    signal: ac.signal,
    observe: o => {
      o.onstatechange = () => {
        ok(true, "StateChange event dispatched");
        if (o.state == "aborted") {
          ok(true, "Aborted!");
          next();
        }
      }
      ac.abort();
    }
  });
}

function testObserveComplete() {
  var ac = new AbortController();

  fetch('http://mochi.test:8888/tests/dom/tests/mochitest/fetch/slow.sjs', {
    signal: ac.signal,
    observe: o => {
      o.onstatechange = () => {
        ok(true, "StateChange event dispatched");
        if (o.state == "complete") {
          ok(true, "Operation completed");
          next();
        }
      }
    }
  });
}

function testObserveErrored() {
  var ac = new AbortController();

  fetch('foo: bar', {
    signal: ac.signal,
    observe: o => {
      o.onstatechange = () => {
        ok(true, "StateChange event dispatched");
        if (o.state == "errored") {
          ok(true, "Operation completed");
          next();
        }
      }
    }
  });
}

function testObserveResponding() {
  var ac = new AbortController();

  fetch('http://mochi.test:8888/tests/dom/tests/mochitest/fetch/slow.sjs', {
    signal: ac.signal,
    observe: o => {
      o.onstatechange = () => {
        if (o.state == "responding") {
          ok(true, "We have responding events");
          next();
        }
      }
    }
  });
}

function workify(worker) {
  function methods() {
    function ok(a, msg) {
      postMessage( { type: 'check', state: !!a, message: msg });
    };
    function is(a, b, msg) {
      postMessage( { type: 'check', state: a === b, message: msg });
    };
    function next() {
      postMessage( { type: 'finish' });
    };
  }

  var str = methods.toString();
  var methodsContent = str.substring(0, str.length - 1).split('\n').splice(1).join('\n');

  str = worker.toString();
  var workerContent = str.substring(0, str.length - 1).split('\n').splice(1).join('\n');

  var content = methodsContent + workerContent;
  var url = URL.createObjectURL(new Blob([content], { type: "application/javascript" }));
  var w = new Worker(url);
  w.onmessage = e => {
    if (e.data.type == 'check') {
      ok(e.data.state, "WORKER: " + e.data.message);
    } else if (e.data.type == 'finish') {
      next();
    } else {
      ok(false, "Something went wrong");
    }
  }
}

var steps = [
  testObserver,
  testObserveAbort,
  function() { workify(testObserveAbort); },
  testObserveComplete,
  function() { workify(testObserveComplete); },
  testObserveErrored,
  function() { workify(testObserveErrored); },
  testObserveResponding,
  function() { workify(testObserveResponding); },
];

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

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

next();

</script>