Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | <!-- |
michael@0 | 2 | Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | --> |
michael@0 | 5 | <html> |
michael@0 | 6 | <head> |
michael@0 | 7 | <title>Indexed Database Property Test</title> |
michael@0 | 8 | |
michael@0 | 9 | <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 10 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
michael@0 | 11 | |
michael@0 | 12 | <script type="text/javascript;version=1.7"> |
michael@0 | 13 | let testGenerator = testSteps(); |
michael@0 | 14 | |
michael@0 | 15 | function ok(val, message) { |
michael@0 | 16 | val = val ? "true" : "false"; |
michael@0 | 17 | window.parent.postMessage("SimpleTest.ok(" + val + ", '" + message + |
michael@0 | 18 | "');", "*"); |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | function is(a, b, message) { |
michael@0 | 22 | ok(a == b, message); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | function grabEventAndContinueHandler(event) { |
michael@0 | 26 | testGenerator.send(event); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | function errorHandler(event) { |
michael@0 | 30 | ok(false, "indexedDB error, code " + event.target.error.name); |
michael@0 | 31 | finishTest(); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | function unexpectedSuccessHandler(event) { |
michael@0 | 35 | ok(false, "got success when it was not expected!"); |
michael@0 | 36 | finishTest(); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | function finishTest() { |
michael@0 | 40 | // Let window.onerror have a chance to fire |
michael@0 | 41 | setTimeout(function() { |
michael@0 | 42 | setTimeout(function() { |
michael@0 | 43 | testGenerator.close(); |
michael@0 | 44 | window.parent.postMessage("SimpleTest.finish();", "*"); |
michael@0 | 45 | }, 0); |
michael@0 | 46 | }, 0); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | window.onerror = function() { |
michael@0 | 50 | return false; |
michael@0 | 51 | }; |
michael@0 | 52 | |
michael@0 | 53 | function testSteps() { |
michael@0 | 54 | // Test 1: Throwing an exception in an upgradeneeded handler should |
michael@0 | 55 | // abort the versionchange transaction and fire an error at the request. |
michael@0 | 56 | let request = indexedDB.open(window.location.pathname, 1); |
michael@0 | 57 | request.onerror = errorHandler; |
michael@0 | 58 | request.onsuccess = unexpectedSuccessHandler; |
michael@0 | 59 | request.onupgradeneeded = function () { |
michael@0 | 60 | let transaction = request.transaction; |
michael@0 | 61 | transaction.oncomplete = unexpectedSuccessHandler; |
michael@0 | 62 | transaction.onabort = grabEventAndContinueHandler |
michael@0 | 63 | throw "STOP"; |
michael@0 | 64 | }; |
michael@0 | 65 | |
michael@0 | 66 | let event = yield undefined; |
michael@0 | 67 | is(event.type, "abort", |
michael@0 | 68 | "Throwing during an upgradeneeded event should abort the transaction."); |
michael@0 | 69 | is(event.target.error.name, "AbortError", "Got AbortError object"); |
michael@0 | 70 | |
michael@0 | 71 | request.onerror = grabEventAndContinueHandler; |
michael@0 | 72 | event = yield undefined; |
michael@0 | 73 | |
michael@0 | 74 | is(event.type, "error", |
michael@0 | 75 | "Throwing during an upgradeneeded event should fire an error."); |
michael@0 | 76 | |
michael@0 | 77 | // Test 2: Throwing during a request's success handler should abort the |
michael@0 | 78 | // transaction. |
michael@0 | 79 | let request = indexedDB.open(window.location.pathname, 1); |
michael@0 | 80 | request.onerror = errorHandler; |
michael@0 | 81 | request.onblocked = errorHandler; |
michael@0 | 82 | request.onupgradeneeded = grabEventAndContinueHandler; |
michael@0 | 83 | request.onsuccess = unexpectedSuccessHandler; |
michael@0 | 84 | let openrequest = request; |
michael@0 | 85 | let event = yield undefined; |
michael@0 | 86 | |
michael@0 | 87 | request.onupgradeneeded = unexpectedSuccessHandler; |
michael@0 | 88 | |
michael@0 | 89 | let db = event.target.result; |
michael@0 | 90 | db.onerror = function(event) { |
michael@0 | 91 | event.preventDefault(); |
michael@0 | 92 | }; |
michael@0 | 93 | |
michael@0 | 94 | event.target.transaction.oncomplete = unexpectedSuccessHandler; |
michael@0 | 95 | event.target.transaction.onabort = grabEventAndContinueHandler; |
michael@0 | 96 | |
michael@0 | 97 | is(db.version, 1, "Correct version"); |
michael@0 | 98 | is(db.objectStoreNames.length, 0, "Correct objectStoreNames length"); |
michael@0 | 99 | |
michael@0 | 100 | let objectStore = db.createObjectStore("foo"); |
michael@0 | 101 | |
michael@0 | 102 | is(db.objectStoreNames.length, 1, "Correct objectStoreNames length"); |
michael@0 | 103 | ok(db.objectStoreNames.contains("foo"), "Has correct objectStore"); |
michael@0 | 104 | |
michael@0 | 105 | request = objectStore.add({}, 1); |
michael@0 | 106 | request.onsuccess = function(event) { |
michael@0 | 107 | throw "foo"; |
michael@0 | 108 | }; |
michael@0 | 109 | |
michael@0 | 110 | event = yield undefined; |
michael@0 | 111 | |
michael@0 | 112 | is(event.type, "abort", "Got transaction abort event"); |
michael@0 | 113 | is(event.target.error.name, "AbortError", "Got AbortError object"); |
michael@0 | 114 | openrequest.onerror = grabEventAndContinueHandler; |
michael@0 | 115 | |
michael@0 | 116 | event = yield undefined; |
michael@0 | 117 | |
michael@0 | 118 | is(event.type, "error", "Got IDBOpenDBRequest error event"); |
michael@0 | 119 | is(event.target, openrequest, "Right event target"); |
michael@0 | 120 | is(event.target.error.name, "AbortError", "Right error name"); |
michael@0 | 121 | |
michael@0 | 122 | // Test 3: Throwing during a request's error handler should abort the |
michael@0 | 123 | // transaction, even if preventDefault is called on the error event. |
michael@0 | 124 | let request = indexedDB.open(window.location.pathname, 1); |
michael@0 | 125 | request.onerror = errorHandler; |
michael@0 | 126 | request.onblocked = errorHandler; |
michael@0 | 127 | request.onupgradeneeded = grabEventAndContinueHandler; |
michael@0 | 128 | request.onsuccess = unexpectedSuccessHandler; |
michael@0 | 129 | let openrequest = request; |
michael@0 | 130 | let event = yield undefined; |
michael@0 | 131 | |
michael@0 | 132 | request.onupgradeneeded = unexpectedSuccessHandler; |
michael@0 | 133 | |
michael@0 | 134 | let db = event.target.result; |
michael@0 | 135 | db.onerror = function(event) { |
michael@0 | 136 | event.preventDefault(); |
michael@0 | 137 | }; |
michael@0 | 138 | |
michael@0 | 139 | event.target.transaction.oncomplete = unexpectedSuccessHandler; |
michael@0 | 140 | event.target.transaction.onabort = grabEventAndContinueHandler; |
michael@0 | 141 | |
michael@0 | 142 | is(db.version, 1, "Correct version"); |
michael@0 | 143 | is(db.objectStoreNames.length, 0, "Correct objectStoreNames length"); |
michael@0 | 144 | |
michael@0 | 145 | let objectStore = db.createObjectStore("foo"); |
michael@0 | 146 | |
michael@0 | 147 | is(db.objectStoreNames.length, 1, "Correct objectStoreNames length"); |
michael@0 | 148 | ok(db.objectStoreNames.contains("foo"), "Has correct objectStore"); |
michael@0 | 149 | |
michael@0 | 150 | request = objectStore.add({}, 1); |
michael@0 | 151 | request.onerror = errorHandler; |
michael@0 | 152 | request = objectStore.add({}, 1); |
michael@0 | 153 | request.onsuccess = unexpectedSuccessHandler; |
michael@0 | 154 | request.onerror = function (event) { |
michael@0 | 155 | event.preventDefault(); |
michael@0 | 156 | throw "STOP"; |
michael@0 | 157 | }; |
michael@0 | 158 | |
michael@0 | 159 | event = yield undefined; |
michael@0 | 160 | |
michael@0 | 161 | is(event.type, "abort", "Got transaction abort event"); |
michael@0 | 162 | is(event.target.error.name, "AbortError", "Got AbortError object"); |
michael@0 | 163 | openrequest.onerror = grabEventAndContinueHandler; |
michael@0 | 164 | |
michael@0 | 165 | event = yield undefined; |
michael@0 | 166 | |
michael@0 | 167 | is(event.type, "error", "Got IDBOpenDBRequest error event"); |
michael@0 | 168 | is(event.target, openrequest, "Right event target"); |
michael@0 | 169 | is(event.target.error.name, "AbortError", "Right error name"); |
michael@0 | 170 | |
michael@0 | 171 | finishTest(); |
michael@0 | 172 | yield undefined; |
michael@0 | 173 | } |
michael@0 | 174 | </script> |
michael@0 | 175 | |
michael@0 | 176 | </head> |
michael@0 | 177 | |
michael@0 | 178 | <body onload="testGenerator.next();"></body> |
michael@0 | 179 | |
michael@0 | 180 | </html> |