summaryrefslogtreecommitdiff
path: root/dom/indexedDB/test/exceptions_in_events_iframe.html
blob: e8002d56c6a6a121a4fba34d63294e687bbba489 (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
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
  <title>Indexed Database Property Test</title>

  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>

  <script type="text/javascript;version=1.7">
    let testGenerator = testSteps();

    function ok(val, message) {
      val = val ? "true" : "false";
      window.parent.postMessage("SimpleTest.ok(" + val + ", '" + message +
                                "');", "*");
    }

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

    function grabEventAndContinueHandler(event) {
      testGenerator.send(event);
    }

    function errorHandler(event) {
      ok(false, "indexedDB error, code " + event.target.error.name);
      finishTest();
    }

    function unexpectedSuccessHandler(event) {
      ok(false, "got success when it was not expected!");
      finishTest();
    }

    function finishTest() {
      // Let window.onerror have a chance to fire
      setTimeout(function() {
        setTimeout(function() {
          testGenerator.close();
          window.parent.postMessage("SimpleTest.finish();", "*");
        }, 0);
      }, 0);
    }

    window.onerror = function() {
      return false;
    };

    function testSteps() {
      window.parent.SpecialPowers.addPermission("indexedDB", true, document);

      // Test 1: Throwing an exception in an upgradeneeded handler should
      // abort the versionchange transaction and fire an error at the request.
      let request = indexedDB.open(window.location.pathname, 1);
      request.onerror = errorHandler;
      request.onsuccess = unexpectedSuccessHandler;
      request.onupgradeneeded = function () {
        let transaction = request.transaction;
        transaction.oncomplete = unexpectedSuccessHandler;
        transaction.onabort = grabEventAndContinueHandler
        throw "STOP";
      };

      let event = yield undefined;
      is(event.type, "abort",
         "Throwing during an upgradeneeded event should abort the transaction.");
      is(event.target.error.name, "AbortError", "Got AbortError object");

      request.onerror = grabEventAndContinueHandler;
      event = yield undefined;

      is(event.type, "error",
         "Throwing during an upgradeneeded event should fire an error.");

      // Test 2: Throwing during a request's success handler should abort the
      // transaction.
      request = indexedDB.open(window.location.pathname, 1);
      request.onerror = errorHandler;
      request.onblocked = errorHandler;
      request.onupgradeneeded = grabEventAndContinueHandler;
      request.onsuccess = unexpectedSuccessHandler;
      let openrequest = request;
      event = yield undefined;

      request.onupgradeneeded = unexpectedSuccessHandler;

      let db = event.target.result;
      db.onerror = function(event) {
        event.preventDefault();
      };

      event.target.transaction.oncomplete = unexpectedSuccessHandler;
      event.target.transaction.onabort = grabEventAndContinueHandler;

      is(db.version, 1, "Correct version");
      is(db.objectStoreNames.length, 0, "Correct objectStoreNames length");

      let objectStore = db.createObjectStore("foo");

      is(db.objectStoreNames.length, 1, "Correct objectStoreNames length");
      ok(db.objectStoreNames.contains("foo"), "Has correct objectStore");

      request = objectStore.add({}, 1);
      request.onsuccess = function(event) {
        throw "foo";
      };

      event = yield undefined;

      is(event.type, "abort", "Got transaction abort event");
      is(event.target.error.name, "AbortError", "Got AbortError object");
      openrequest.onerror = grabEventAndContinueHandler;

      event = yield undefined;

      is(event.type, "error", "Got IDBOpenDBRequest error event");
      is(event.target, openrequest, "Right event target");
      is(event.target.error.name, "AbortError", "Right error name");

      // Test 3: Throwing during a request's error handler should abort the
      // transaction, even if preventDefault is called on the error event.
      request = indexedDB.open(window.location.pathname, 1);
      request.onerror = errorHandler;
      request.onblocked = errorHandler;
      request.onupgradeneeded = grabEventAndContinueHandler;
      request.onsuccess = unexpectedSuccessHandler;
      openrequest = request;
      event = yield undefined;

      request.onupgradeneeded = unexpectedSuccessHandler;

      db = event.target.result;
      db.onerror = function(event) {
        event.preventDefault();
      };

      event.target.transaction.oncomplete = unexpectedSuccessHandler;
      event.target.transaction.onabort = grabEventAndContinueHandler;

      is(db.version, 1, "Correct version");
      is(db.objectStoreNames.length, 0, "Correct objectStoreNames length");

      objectStore = db.createObjectStore("foo");

      is(db.objectStoreNames.length, 1, "Correct objectStoreNames length");
      ok(db.objectStoreNames.contains("foo"), "Has correct objectStore");

      request = objectStore.add({}, 1);
      request.onerror = errorHandler;
      request = objectStore.add({}, 1);
      request.onsuccess = unexpectedSuccessHandler;
      request.onerror = function (event) {
        event.preventDefault();
        throw "STOP";
      };

      event = yield undefined;

      is(event.type, "abort", "Got transaction abort event");
      is(event.target.error.name, "AbortError", "Got AbortError object");
      openrequest.onerror = grabEventAndContinueHandler;

      event = yield undefined;

      is(event.type, "error", "Got IDBOpenDBRequest error event");
      is(event.target, openrequest, "Right event target");
      is(event.target.error.name, "AbortError", "Right error name");

      finishTest();
      yield undefined;
    }
  </script>

</head>

<body onload="testGenerator.next();"></body>

</html>