1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/indexedDB/test/unit/test_indexes_funny_things.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,168 @@ 1.4 +/** 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.7 + */ 1.8 + 1.9 +var testGenerator = testSteps(); 1.10 + 1.11 +function testSteps() 1.12 +{ 1.13 + // Blob constructor is not implemented outside of windows yet (Bug 827723). 1.14 + if (!this.window) { 1.15 + finishTest(); 1.16 + yield undefined; 1.17 + } 1.18 + 1.19 + const name = this.window ? window.location.pathname : "Splendid Test"; 1.20 + 1.21 + const objectStoreName = "Things"; 1.22 + 1.23 + const blob1 = new Blob(["foo", "bar"], { type: "text/plain" }); 1.24 + const blob2 = new Blob(["foobazybar"], { type: "text/plain" }); 1.25 + const blob3 = new Blob(["2"], { type: "bogus/" }); 1.26 + const str = "The Book of Mozilla"; 1.27 + str.type = blob1; 1.28 + const arr = [1, 2, 3, 4, 5]; 1.29 + 1.30 + const objectStoreData = [ 1.31 + { key: "1", value: blob1}, 1.32 + { key: "2", value: blob2}, 1.33 + { key: "3", value: blob3}, 1.34 + { key: "4", value: str}, 1.35 + { key: "5", value: arr}, 1.36 + ]; 1.37 + 1.38 + const indexData = [ 1.39 + { name: "type", keyPath: "type", options: { } }, 1.40 + { name: "length", keyPath: "length", options: { unique: true } } 1.41 + ]; 1.42 + 1.43 + const objectStoreDataTypeSort = [ 1.44 + { key: "3", value: blob3}, 1.45 + { key: "1", value: blob1}, 1.46 + { key: "2", value: blob2}, 1.47 + ]; 1.48 + 1.49 + const objectStoreDataLengthSort = [ 1.50 + { key: "5", value: arr}, 1.51 + //{ key: "4", value: str}, 1.52 + ]; 1.53 + 1.54 + let request = indexedDB.open(name, 1); 1.55 + request.onerror = errorHandler; 1.56 + request.onupgradeneeded = grabEventAndContinueHandler; 1.57 + request.onsuccess = grabEventAndContinueHandler; 1.58 + let event = yield undefined; 1.59 + let db = event.target.result; 1.60 + 1.61 + let objectStore = db.createObjectStore(objectStoreName, { keyPath: null }); 1.62 + 1.63 + // First, add all our data to the object store. 1.64 + let addedData = 0; 1.65 + for (let i in objectStoreData) { 1.66 + request = objectStore.add(objectStoreData[i].value, 1.67 + objectStoreData[i].key); 1.68 + request.onerror = errorHandler; 1.69 + request.onsuccess = function(event) { 1.70 + if (++addedData == objectStoreData.length) { 1.71 + testGenerator.send(event); 1.72 + } 1.73 + } 1.74 + } 1.75 + event = yield undefined; 1.76 + // Now create the indexes. 1.77 + for (let i in indexData) { 1.78 + objectStore.createIndex(indexData[i].name, indexData[i].keyPath, 1.79 + indexData[i].options); 1.80 + } 1.81 + is(objectStore.indexNames.length, indexData.length, "Good index count"); 1.82 + yield undefined; 1.83 + objectStore = db.transaction(objectStoreName) 1.84 + .objectStore(objectStoreName); 1.85 + 1.86 + // Check global properties to make sure they are correct. 1.87 + is(objectStore.indexNames.length, indexData.length, "Good index count"); 1.88 + for (let i in indexData) { 1.89 + let found = false; 1.90 + for (let j = 0; j < objectStore.indexNames.length; j++) { 1.91 + if (objectStore.indexNames.item(j) == indexData[i].name) { 1.92 + found = true; 1.93 + break; 1.94 + } 1.95 + } 1.96 + is(found, true, "objectStore has our index"); 1.97 + let index = objectStore.index(indexData[i].name); 1.98 + is(index.name, indexData[i].name, "Correct name"); 1.99 + is(index.storeName, objectStore.name, "Correct store name"); 1.100 + is(index.keyPath, indexData[i].keyPath, "Correct keyPath"); 1.101 + is(index.unique, indexData[i].options.unique ? true : false, 1.102 + "Correct unique value"); 1.103 + } 1.104 + 1.105 + ok(true, "Test group 1"); 1.106 + 1.107 + let keyIndex = 0; 1.108 + 1.109 + request = objectStore.index("type").openKeyCursor(); 1.110 + request.onerror = errorHandler; 1.111 + request.onsuccess = function (event) { 1.112 + let cursor = event.target.result; 1.113 + if (cursor) { 1.114 + is(cursor.key, objectStoreDataTypeSort[keyIndex].value.type, 1.115 + "Correct key"); 1.116 + is(cursor.primaryKey, objectStoreDataTypeSort[keyIndex].key, 1.117 + "Correct primary key"); 1.118 + ok(!("value" in cursor), "No value"); 1.119 + 1.120 + cursor.continue(); 1.121 + 1.122 + is(cursor.key, objectStoreDataTypeSort[keyIndex].value.type, 1.123 + "Correct key"); 1.124 + is(cursor.primaryKey, objectStoreDataTypeSort[keyIndex].key, 1.125 + "Correct value"); 1.126 + ok(!("value" in cursor), "No value"); 1.127 + 1.128 + keyIndex++; 1.129 + } 1.130 + else { 1.131 + testGenerator.next(); 1.132 + } 1.133 + } 1.134 + yield undefined; 1.135 + 1.136 + is(keyIndex, objectStoreDataTypeSort.length, "Saw all the expected keys"); 1.137 + 1.138 + ok(true, "Test group 2"); 1.139 + 1.140 + keyIndex = 0; 1.141 + 1.142 + request = objectStore.index("length").openKeyCursor(null, "next"); 1.143 + request.onerror = errorHandler; 1.144 + request.onsuccess = function (event) { 1.145 + let cursor = event.target.result; 1.146 + if (cursor) { 1.147 + is(cursor.key, objectStoreDataLengthSort[keyIndex].value.length, 1.148 + "Correct key"); 1.149 + is(cursor.primaryKey, objectStoreDataLengthSort[keyIndex].key, 1.150 + "Correct value"); 1.151 + 1.152 + cursor.continue(); 1.153 + 1.154 + is(cursor.key, objectStoreDataLengthSort[keyIndex].value.length, 1.155 + "Correct key"); 1.156 + is(cursor.primaryKey, objectStoreDataLengthSort[keyIndex].key, 1.157 + "Correct value"); 1.158 + 1.159 + keyIndex++; 1.160 + } 1.161 + else { 1.162 + testGenerator.next(); 1.163 + } 1.164 + } 1.165 + yield undefined; 1.166 + 1.167 + is(keyIndex, objectStoreDataLengthSort.length, "Saw all the expected keys"); 1.168 + 1.169 + finishTest(); 1.170 + yield undefined; 1.171 +}