1 if (this.importScripts) {
2 importScripts('../../../resources/js-test.js');
3 importScripts('shared.js');
6 description("Test IDBTransaction.error cases.");
8 indexedDBTest(prepareDatabase, startTest);
9 function prepareDatabase()
11 db = event.target.result;
12 evalAndLog("store = db.createObjectStore('storeName')");
13 request = evalAndLog("store.add('value', 'key')");
14 request.onerror = unexpectedErrorCallback;
17 var nonConvertibleToString = {toString: function() { throw "Exception in toString()"; }};
21 evalAndLog("trans = db.transaction('storeName')");
24 debug("IDBTransaction.error should be null if transaction is not finished:");
25 shouldBeNull("trans.error");
28 debug("transaction() should throw if one of the DOMStringList items cannot be converted to a String:");
29 shouldThrow("db.transaction(['storeName', nonConvertibleToString])", "'Exception in toString()'");
32 debug("If IDBTransaction.abort() is explicitly called, IDBTransaction.error should be null:");
33 evalAndLog("trans.abort()");
34 trans.oncomplete = unexpectedCompleteCallback;
35 trans.onabort = function() {
36 shouldBeNull("trans.error");
37 testErrorFromRequest();
41 function testErrorFromRequest()
44 debug("If the transaction is aborted due to a request error that is not prevented, IDBTransaction.error should match:");
45 evalAndLog("trans = db.transaction('storeName', 'readwrite')");
46 evalAndLog("request = trans.objectStore('storeName').add('value2', 'key')");
47 request.onsuccess = unexpectedSuccessCallback;
48 request.onerror = function() {
49 shouldBeUndefined("request.result");
50 shouldBeNonNull("request.error");
51 shouldBe("request.error.name", "'ConstraintError'");
52 evalAndLog("request_error = request.error");
54 trans.oncomplete = unexpectedCompleteCallback;
55 trans.onabort = function() {
56 debug("Transaction received abort event.");
57 shouldBeNonNull("trans.error");
58 debug("trans.error.message = " + trans.error.message);
59 shouldBeNonNull("trans.error.message");
60 shouldBe("trans.error", "request_error");
61 testErrorFromException();
65 function testErrorFromException()
68 debug("If the transaction is aborted due to an exception thrown from event callback, IDBTransaction.error should be AbortError:");
69 evalAndLog("trans = db.transaction('storeName', 'readwrite')");
70 evalAndLog("request = trans.objectStore('storeName').add('value2', 'key')");
71 request.onsuccess = unexpectedSuccessCallback;
72 request.onerror = function() {
73 shouldBeUndefined("request.result");
74 shouldBeNonNull("request.error");
75 shouldBe("request.error.name", "'ConstraintError'");
76 debug("Throwing exception...");
78 // Ensure the test harness error handler is not invoked.
79 self.originalWindowOnError = self.onerror;
82 throw new Error("This should *NOT* be caught!");
84 trans.oncomplete = unexpectedCompleteCallback;
85 trans.onabort = function() {
86 debug("Transaction received abort event.");
88 // Restore test harness error handler.
89 self.onerror = self.originalWindowOnError;
91 shouldBeNonNull("trans.error");
92 debug("trans.error.message = " + trans.error.message);
93 shouldBeNonNull("trans.error.message");
94 shouldBe("trans.error.name", "'AbortError'");
95 testErrorFromCommit();
99 function testErrorFromCommit()
102 debug("If the transaction is aborted due to an error during commit, IDBTransaction.error should reflect that error:");
103 evalAndLog("trans = db.transaction('storeName', 'readwrite')");
104 evalAndLog("request = trans.objectStore('storeName').add({id: 1}, 'record1')");
105 request.onerror = unexpectedErrorCallback;
106 evalAndLog("request = trans.objectStore('storeName').add({id: 1}, 'record2')");
107 request.onerror = unexpectedErrorCallback;
108 trans.onabort = unexpectedAbortCallback;
109 trans.oncomplete = function() {
111 evalAndLog("request = indexedDB.open(dbname, 2)");
112 request.onsuccess = unexpectedSuccessCallback;
113 request.onblocked = unexpectedBlockedCallback;
114 request.onupgradeneeded = function() {
115 evalAndLog("trans = request.transaction");
116 debug("This should fail due to the unique constraint:");
117 evalAndLog("indexName = 'Also test utf8: \u6F22'");
118 evalAndLog("trans.objectStore('storeName').createIndex(indexName, 'id', {unique: true})");
119 trans.oncomplete = unexpectedCompleteCallback;
120 trans.onabort = function() {
121 debug("Transaction received abort event.");
122 shouldBeNonNull("trans.error");
123 shouldBe("trans.error.name", "'ConstraintError'");
124 debug("trans.error.message = " + trans.error.message);
125 shouldBeNonNull("trans.error.message");
126 debug("Note: This fails because of http://wkb.ug/37327");
127 shouldNotBe("trans.error.message.indexOf(indexName)", "-1");