test_create_objectStore.js (4354B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 /* exported testGenerator */ 7 var testGenerator = testSteps(); 8 9 function* testSteps() { 10 const name = this.window ? window.location.pathname : "Splendid Test"; 11 const objectStoreInfo = [ 12 { name: "1", options: { keyPath: null } }, 13 { name: "2", options: { keyPath: null, autoIncrement: true } }, 14 { name: "3", options: { keyPath: null, autoIncrement: false } }, 15 { name: "4", options: { keyPath: null } }, 16 { name: "5", options: { keyPath: "foo" } }, 17 { name: "6" }, 18 { name: "7", options: null }, 19 { name: "8", options: { autoIncrement: true } }, 20 { name: "9", options: { autoIncrement: false } }, 21 { name: "10", options: { keyPath: "foo", autoIncrement: false } }, 22 { name: "11", options: { keyPath: "foo", autoIncrement: true } }, 23 { name: "" }, 24 { name: null }, 25 { name: undefined }, 26 ]; 27 28 let request = indexedDB.open(name, 1); 29 request.onerror = errorHandler; 30 request.onupgradeneeded = grabEventAndContinueHandler; 31 request.onsuccess = unexpectedSuccessHandler; 32 let event = yield undefined; 33 34 let db = event.target.result; 35 36 let count = db.objectStoreNames.length; 37 is(count, 0, "correct objectStoreNames length"); 38 39 try { 40 db.createObjectStore("foo", "bar"); 41 ok(false, "createObjectStore with bad options should throw"); 42 } catch (e) { 43 ok(true, "createObjectStore with bad options"); 44 } 45 46 ok( 47 db.createObjectStore("foo", { foo: "" }), 48 "createObjectStore with unknown options should not throw" 49 ); 50 db.deleteObjectStore("foo"); 51 52 for (let index in objectStoreInfo) { 53 index = parseInt(index); 54 const info = objectStoreInfo[index]; 55 56 let objectStore = info.hasOwnProperty("options") 57 ? db.createObjectStore(info.name, info.options) 58 : db.createObjectStore(info.name); 59 60 is(db.objectStoreNames.length, index + 1, "updated objectStoreNames list"); 61 62 let name = info.name; 63 if (name === null) { 64 name = "null"; 65 } else if (name === undefined) { 66 name = "undefined"; 67 } 68 69 let found = false; 70 for (let i = 0; i <= index; i++) { 71 if (db.objectStoreNames.item(i) == name) { 72 found = true; 73 break; 74 } 75 } 76 is(found, true, "objectStoreNames contains name"); 77 78 is(objectStore.name, name, "Bad name"); 79 is( 80 objectStore.keyPath, 81 info.options && info.options.keyPath ? info.options.keyPath : null, 82 "Bad keyPath" 83 ); 84 is(objectStore.indexNames.length, 0, "Bad indexNames"); 85 86 ok(event.target.transaction, "event has a transaction"); 87 ok(event.target.transaction.db === db, "transaction has the right db"); 88 is( 89 event.target.transaction.mode, 90 "versionchange", 91 "transaction has the correct mode" 92 ); 93 is( 94 event.target.transaction.objectStoreNames.length, 95 index + 1, 96 "transaction has correct objectStoreNames list" 97 ); 98 found = false; 99 for (let j = 0; j < event.target.transaction.objectStoreNames.length; j++) { 100 if (event.target.transaction.objectStoreNames.item(j) == name) { 101 found = true; 102 break; 103 } 104 } 105 is(found, true, "transaction has correct objectStoreNames list"); 106 } 107 108 // Can't handle autoincrement and empty keypath 109 let ex; 110 try { 111 db.createObjectStore("storefail", { keyPath: "", autoIncrement: true }); 112 } catch (e) { 113 ex = e; 114 } 115 ok(ex, "createObjectStore with empty keyPath and autoIncrement should throw"); 116 is(ex.name, "InvalidAccessError", "should throw right exception"); 117 ok(ex instanceof DOMException, "should throw right exception"); 118 is(ex.code, DOMException.INVALID_ACCESS_ERR, "should throw right exception"); 119 120 // Can't handle autoincrement and array keypath 121 try { 122 db.createObjectStore("storefail", { keyPath: ["a"], autoIncrement: true }); 123 } catch (e) { 124 ex = e; 125 } 126 ok(ex, "createObjectStore with array keyPath and autoIncrement should throw"); 127 is(ex.name, "InvalidAccessError", "should throw right exception"); 128 ok(ex instanceof DOMException, "should throw right exception"); 129 is(ex.code, DOMException.INVALID_ACCESS_ERR, "should throw right exception"); 130 131 request.onsuccess = grabEventAndContinueHandler; 132 request.onupgradeneeded = unexpectedSuccessHandler; 133 134 event = yield undefined; 135 136 finishTest(); 137 }