1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/indexedDB/test/unit/test_index_getAllObjects.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,233 @@ 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 + const name = this.window ? window.location.pathname : "Splendid Test"; 1.14 + const objectStoreName = "People"; 1.15 + 1.16 + const objectStoreData = [ 1.17 + { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, 1.18 + { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, 1.19 + { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } }, 1.20 + { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } }, 1.21 + { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, 1.22 + { key: "237-23-7737", value: { name: "Pat", height: 65 } } 1.23 + ]; 1.24 + 1.25 + const indexData = [ 1.26 + { name: "name", keyPath: "name", options: { unique: true } }, 1.27 + { name: "height", keyPath: "height", options: { unique: false } }, 1.28 + { name: "weight", keyPath: "weight", options: { unique: false } } 1.29 + ]; 1.30 + 1.31 + const objectStoreDataNameSort = [ 1.32 + { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, 1.33 + { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, 1.34 + { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, 1.35 + { key: "237-23-7737", value: { name: "Pat", height: 65 } }, 1.36 + { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } }, 1.37 + { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } } 1.38 + ]; 1.39 + 1.40 + const objectStoreDataWeightSort = [ 1.41 + { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, 1.42 + { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, 1.43 + { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } }, 1.44 + { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, 1.45 + { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } } 1.46 + ]; 1.47 + 1.48 + const objectStoreDataHeightSort = [ 1.49 + { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, 1.50 + { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } }, 1.51 + { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, 1.52 + { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, 1.53 + { key: "237-23-7737", value: { name: "Pat", height: 65 } }, 1.54 + { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } } 1.55 + ]; 1.56 + 1.57 + let request = indexedDB.open(name, 1); 1.58 + request.onerror = errorHandler; 1.59 + request.onupgradeneeded = grabEventAndContinueHandler; 1.60 + request.onsuccess = grabEventAndContinueHandler; 1.61 + let event = yield undefined; 1.62 + 1.63 + let db = event.target.result; 1.64 + 1.65 + let objectStore = db.createObjectStore(objectStoreName, {}); 1.66 + 1.67 + // First, add all our data to the object store. 1.68 + let addedData = 0; 1.69 + for (let i in objectStoreData) { 1.70 + request = objectStore.add(objectStoreData[i].value, 1.71 + objectStoreData[i].key); 1.72 + request.onerror = errorHandler; 1.73 + request.onsuccess = function(event) { 1.74 + if (++addedData == objectStoreData.length) { 1.75 + testGenerator.send(event); 1.76 + } 1.77 + } 1.78 + } 1.79 + event = yield undefined; 1.80 + 1.81 + // Now create the indexes. 1.82 + for (let i in indexData) { 1.83 + objectStore.createIndex(indexData[i].name, indexData[i].keyPath, 1.84 + indexData[i].options); 1.85 + } 1.86 + 1.87 + is(objectStore.indexNames.length, indexData.length, "Good index count"); 1.88 + yield undefined; 1.89 + 1.90 + objectStore = db.transaction(objectStoreName) 1.91 + .objectStore(objectStoreName); 1.92 + 1.93 + request = objectStore.index("height").mozGetAll(65); 1.94 + request.onerror = errorHandler; 1.95 + request.onsuccess = grabEventAndContinueHandler; 1.96 + event = yield undefined; 1.97 + 1.98 + is(event.target.result instanceof Array, true, "Got an array object"); 1.99 + is(event.target.result.length, 2, "Correct length"); 1.100 + 1.101 + for (let i in event.target.result) { 1.102 + let result = event.target.result[i]; 1.103 + let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; 1.104 + 1.105 + is(result.name, testObj.name, "Correct name"); 1.106 + is(result.height, testObj.height, "Correct height"); 1.107 + 1.108 + if (testObj.hasOwnProperty("weight")) { 1.109 + is(result.weight, testObj.weight, "Correct weight"); 1.110 + } 1.111 + } 1.112 + 1.113 + request = objectStore.index("height").mozGetAll(65, 0); 1.114 + request.onerror = errorHandler; 1.115 + request.onsuccess = grabEventAndContinueHandler; 1.116 + event = yield undefined; 1.117 + 1.118 + is(event.target.result instanceof Array, true, "Got an array object"); 1.119 + is(event.target.result.length, 2, "Correct length"); 1.120 + 1.121 + for (let i in event.target.result) { 1.122 + let result = event.target.result[i]; 1.123 + let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; 1.124 + 1.125 + is(result.name, testObj.name, "Correct name"); 1.126 + is(result.height, testObj.height, "Correct height"); 1.127 + 1.128 + if (testObj.hasOwnProperty("weight")) { 1.129 + is(result.weight, testObj.weight, "Correct weight"); 1.130 + } 1.131 + } 1.132 + 1.133 + request = objectStore.index("height").mozGetAll(65, null); 1.134 + request.onerror = errorHandler; 1.135 + request.onsuccess = grabEventAndContinueHandler; 1.136 + event = yield undefined; 1.137 + 1.138 + is(event.target.result instanceof Array, true, "Got an array object"); 1.139 + is(event.target.result.length, 2, "Correct length"); 1.140 + 1.141 + for (let i in event.target.result) { 1.142 + let result = event.target.result[i]; 1.143 + let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; 1.144 + 1.145 + is(result.name, testObj.name, "Correct name"); 1.146 + is(result.height, testObj.height, "Correct height"); 1.147 + 1.148 + if (testObj.hasOwnProperty("weight")) { 1.149 + is(result.weight, testObj.weight, "Correct weight"); 1.150 + } 1.151 + } 1.152 + 1.153 + request = objectStore.index("height").mozGetAll(65, undefined); 1.154 + request.onerror = errorHandler; 1.155 + request.onsuccess = grabEventAndContinueHandler; 1.156 + event = yield undefined; 1.157 + 1.158 + is(event.target.result instanceof Array, true, "Got an array object"); 1.159 + is(event.target.result.length, 2, "Correct length"); 1.160 + 1.161 + for (let i in event.target.result) { 1.162 + let result = event.target.result[i]; 1.163 + let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; 1.164 + 1.165 + is(result.name, testObj.name, "Correct name"); 1.166 + is(result.height, testObj.height, "Correct height"); 1.167 + 1.168 + if (testObj.hasOwnProperty("weight")) { 1.169 + is(result.weight, testObj.weight, "Correct weight"); 1.170 + } 1.171 + } 1.172 + 1.173 + request = objectStore.index("height").mozGetAll(); 1.174 + request.onerror = errorHandler; 1.175 + request.onsuccess = grabEventAndContinueHandler; 1.176 + event = yield undefined; 1.177 + 1.178 + is(event.target.result instanceof Array, true, "Got an array object"); 1.179 + is(event.target.result.length, objectStoreDataHeightSort.length, 1.180 + "Correct length"); 1.181 + 1.182 + for (let i in event.target.result) { 1.183 + let result = event.target.result[i]; 1.184 + let testObj = objectStoreDataHeightSort[i].value; 1.185 + 1.186 + is(result.name, testObj.name, "Correct name"); 1.187 + is(result.height, testObj.height, "Correct height"); 1.188 + 1.189 + if (testObj.hasOwnProperty("weight")) { 1.190 + is(result.weight, testObj.weight, "Correct weight"); 1.191 + } 1.192 + } 1.193 + 1.194 + request = objectStore.index("height").mozGetAll(null, 4); 1.195 + request.onerror = errorHandler; 1.196 + request.onsuccess = grabEventAndContinueHandler; 1.197 + event = yield undefined; 1.198 + 1.199 + is(event.target.result instanceof Array, true, "Got an array object"); 1.200 + is(event.target.result.length, 4, "Correct length"); 1.201 + 1.202 + for (let i in event.target.result) { 1.203 + let result = event.target.result[i]; 1.204 + let testObj = objectStoreDataHeightSort[i].value; 1.205 + 1.206 + is(result.name, testObj.name, "Correct name"); 1.207 + is(result.height, testObj.height, "Correct height"); 1.208 + 1.209 + if (testObj.hasOwnProperty("weight")) { 1.210 + is(result.weight, testObj.weight, "Correct weight"); 1.211 + } 1.212 + } 1.213 + 1.214 + request = objectStore.index("height").mozGetAll(65, 1); 1.215 + request.onerror = errorHandler; 1.216 + request.onsuccess = grabEventAndContinueHandler; 1.217 + event = yield undefined; 1.218 + 1.219 + is(event.target.result instanceof Array, true, "Got an array object"); 1.220 + is(event.target.result.length, 1, "Correct length"); 1.221 + 1.222 + for (let i in event.target.result) { 1.223 + let result = event.target.result[i]; 1.224 + let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; 1.225 + 1.226 + is(result.name, testObj.name, "Correct name"); 1.227 + is(result.height, testObj.height, "Correct height"); 1.228 + 1.229 + if (testObj.hasOwnProperty("weight")) { 1.230 + is(result.weight, testObj.weight, "Correct weight"); 1.231 + } 1.232 + } 1.233 + 1.234 + finishTest(); 1.235 + yield undefined; 1.236 +}