test_create_locale_aware_index.js (4555B)
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: "a", options: { keyPath: "id", autoIncrement: true } }, 13 { name: "b", options: { keyPath: "id", autoIncrement: false } }, 14 ]; 15 const indexInfo = [ 16 { 17 name: "1", 18 keyPath: "unique_value", 19 options: { unique: true, locale: "es-ES" }, 20 }, 21 { 22 name: "2", 23 keyPath: "unique_value", 24 options: { unique: true, locale: null }, 25 }, 26 { 27 name: "3", 28 keyPath: "value", 29 options: { unique: false, locale: "es-ES" }, 30 }, 31 { 32 name: "4", 33 keyPath: "value", 34 options: { unique: false, locale: "es-ES" }, 35 }, 36 { name: "", keyPath: "value", options: { unique: false, locale: "es-ES" } }, 37 { 38 name: null, 39 keyPath: "value", 40 options: { unique: false, locale: "es-ES" }, 41 }, 42 { 43 name: undefined, 44 keyPath: "value", 45 options: { unique: false, locale: "es-ES" }, 46 }, 47 ]; 48 49 let request = indexedDB.open(name, 1); 50 request.onerror = errorHandler; 51 request.onupgradeneeded = grabEventAndContinueHandler; 52 request.onsuccess = unexpectedSuccessHandler; 53 let event = yield undefined; 54 let db = event.target.result; 55 56 for (let i = 0; i < objectStoreInfo.length; i++) { 57 let info = objectStoreInfo[i]; 58 let objectStore = info.hasOwnProperty("options") 59 ? db.createObjectStore(info.name, info.options) 60 : db.createObjectStore(info.name); 61 62 try { 63 request = objectStore.createIndex("Hola"); 64 ok(false, "createIndex with no keyPath should throw"); 65 } catch (e) { 66 ok(true, "createIndex with no keyPath should throw"); 67 } 68 69 let ex; 70 try { 71 objectStore.createIndex("Hola", ["foo"], { multiEntry: true }); 72 } catch (e) { 73 ex = e; 74 } 75 ok(ex, "createIndex with array keyPath and multiEntry should throw"); 76 is(ex.name, "InvalidAccessError", "should throw right exception"); 77 ok(ex instanceof DOMException, "should throw right exception"); 78 is( 79 ex.code, 80 DOMException.INVALID_ACCESS_ERR, 81 "should throw right exception" 82 ); 83 84 try { 85 objectStore.createIndex("foo", "bar", 10); 86 ok(false, "createIndex with bad options should throw"); 87 } catch (e) { 88 ok(true, "createIndex with bad options threw"); 89 } 90 91 ok( 92 objectStore.createIndex("foo", "bar", { foo: "" }), 93 "createIndex with unknown options should not throw" 94 ); 95 objectStore.deleteIndex("foo"); 96 97 // Test index creation, and that it ends up in indexNames. 98 let objectStoreName = info.name; 99 for (let j = 0; j < indexInfo.length; j++) { 100 let info = indexInfo[j]; 101 let count = objectStore.indexNames.length; 102 let index = info.hasOwnProperty("options") 103 ? objectStore.createIndex(info.name, info.keyPath, info.options) 104 : objectStore.createIndex(info.name, info.keyPath); 105 106 let name = info.name; 107 if (name === null) { 108 name = "null"; 109 } else if (name === undefined) { 110 name = "undefined"; 111 } 112 113 is(index.name, name, "correct name"); 114 is(index.keyPath, info.keyPath, "correct keyPath"); 115 is(index.unique, info.options.unique, "correct uniqueness"); 116 is(index.locale, info.options.locale, "correct locale"); 117 118 is(objectStore.indexNames.length, count + 1, "indexNames grew in size"); 119 let found = false; 120 for (let k = 0; k < objectStore.indexNames.length; k++) { 121 if (objectStore.indexNames.item(k) == name) { 122 found = true; 123 break; 124 } 125 } 126 ok(found, "Name is on objectStore.indexNames"); 127 128 ok(event.target.transaction, "event has a transaction"); 129 ok(event.target.transaction.db === db, "transaction has the right db"); 130 is( 131 event.target.transaction.mode, 132 "versionchange", 133 "transaction has the correct mode" 134 ); 135 is( 136 event.target.transaction.objectStoreNames.length, 137 i + 1, 138 "transaction only has one object store" 139 ); 140 ok( 141 event.target.transaction.objectStoreNames.contains(objectStoreName), 142 "transaction has the correct object store" 143 ); 144 } 145 } 146 147 request.onsuccess = grabEventAndContinueHandler; 148 request.onupgradeneeded = unexpectedSuccessHandler; 149 150 event = yield undefined; 151 152 finishTest(); 153 }