summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_domcursor.html
blob: d33581f124e6b07d665a99196288742c2efa92df (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for DOMCursor</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">
"use strict";

SimpleTest.waitForExplicitFinish();

var reqserv = SpecialPowers.getDOMRequestService();
ok("createRequest" in reqserv, "appears to be a service");

var req;
var lastContinue = false;

var index = 0;

function next() {
  if (index < tests.length) {
    ok(true, "Begin test");
    tests[index++]();
  } else {
    ok(true, "All done");
    SimpleTest.finish();
  }
}

var tests = [
  function() {
    // create a cursor, test its interface and its initial state
    req = reqserv.createCursor(window, function() {
      if (lastContinue) {
        reqserv.fireDone(req);
      } else {
        reqserv.fireSuccess(req, "next result")
      }
    });
    ok("result" in req, "cursor has result");
    ok("error" in req, "cursor has error");
    ok("onsuccess" in req, "cursor has onsuccess");
    ok("onerror" in req, "cursor has onerror");
    ok("readyState" in req, "cursor has readyState");
    ok("done" in req, "cursor has finished");
    ok("continue" in req, "cursor has continue");
    ok(!("then" in req), "cursor should not have a then method");

    is(req.readyState, "pending", "readyState is pending");
    is(req.result, undefined, "result is undefined");
    is(req.onsuccess, null, "onsuccess is null");
    is(req.onerror, null, "onerror is null");
    next();
  },
  function() {
    // fire success
    req.onsuccess = function(e) {
      ok(e, "got success event");
      is(e.type, "success", "correct type during success");
      is(e.target, req, "correct target during success");
      is(req.readyState, "done", "correct readyState after success");
      is(req.error, null, "correct error after success");
      is(req.result, "my result", "correct result after success");
      is(req.done, false, "cursor is not done after continue")
      next();
    }
    reqserv.fireSuccess(req, "my result");
  },
  function() {
    // continue
    req.onsuccess = function(e) {
      ok(e, "got success event after continue");
      is(e.type, "success", "correct type during continue");
      is(e.target, req, "correct target during continue");
      is(req.readyState, "done", "correct readyState after continue");
      is(req.error, null, "correct error after continue");
      is(req.result, "next result", "correct result after continue");
      is(req.done, false, "cursor is not done after continue")
      next();
    }
    req.continue();
  },
  function() {
    // FireDone
    req.onsuccess = function(e) {
      ok(e, "got success event after continue");
      is(e.type, "success", "correct type during continue");
      is(e.target, req, "correct target during continue");
      is(req.readyState, "done", "correct readyState after continue");
      is(req.error, null, "correct error after continue");
      is(req.result, undefined, "no result after last continue");
      is(req.done, true, "cursor is done after last continue")
      try {
        req.continue();
        ok(false, "continue when cursor is done should fail");
      } catch (e) {
        ok(true, "continue when cursor is done should fail");
      }

      next();
    }
    lastContinue = true;
    req.continue();
  },
  function() {
    // fire error
    req = reqserv.createCursor(window, function(){});
    req.onerror = function(e) {
      ok(e, "got success event");
      is(e.type, "error", "correct type during error");
      is(e.target, req, "correct target during error");
      is(req.readyState, "done", "correct readyState after error");
      is(req.error.name, "error msg", "correct error after error");
      is(req.result, undefined, "correct result after error");
      try {
        req.continue();
        ok(false, "continue while in an error state should fail");
      } catch (e) {
        ok(true, "continue while in an error state should fail");
      }

      next();
    }
    reqserv.fireError(req, "error msg");
  }
];

next();

</script>
</pre>
</body>
</html>