dom/indexedDB/test/unit/test_indexes_bad_values.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:9ceb5f7afbc3
1 /**
2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
4 */
5
6 var testGenerator = testSteps();
7
8 function testSteps()
9 {
10 const name = this.window ? window.location.pathname : "Splendid Test";
11
12 const objectStoreName = "People";
13
14 const objectStoreData = [
15 { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } },
16 { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } },
17 { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } },
18 { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } },
19 { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } },
20 { key: "237-23-7737", value: { name: "Pat", height: 65 } },
21 { key: "237-23-7738", value: { name: "Mel", height: 66, weight: {} } }
22 ];
23
24 const badObjectStoreData = [
25 { key: "237-23-7739", value: { name: "Rob", height: 65 } },
26 { key: "237-23-7740", value: { name: "Jen", height: 66, weight: {} } }
27 ];
28
29 const indexData = [
30 { name: "weight", keyPath: "weight", options: { unique: false } }
31 ];
32
33 const objectStoreDataWeightSort = [
34 { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } },
35 { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } },
36 { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } },
37 { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } },
38 { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } }
39 ];
40
41 let request = indexedDB.open(name, 1);
42 request.onerror = errorHandler;
43 request.onupgradeneeded = grabEventAndContinueHandler;
44 request.onsuccess = grabEventAndContinueHandler;
45 let event = yield undefined;
46
47 let db = event.target.result;
48
49 let objectStore = db.createObjectStore(objectStoreName, { } );
50
51 let addedData = 0;
52 for (let i in objectStoreData) {
53 request = objectStore.add(objectStoreData[i].value,
54 objectStoreData[i].key);
55 request.onerror = errorHandler;
56 request.onsuccess = function(event) {
57 if (++addedData == objectStoreData.length) {
58 testGenerator.send(event);
59 }
60 }
61 }
62 event = yield undefined;
63
64 for (let i in indexData) {
65 objectStore.createIndex(indexData[i].name, indexData[i].keyPath,
66 indexData[i].options);
67 }
68
69 addedData = 0;
70 for (let i in badObjectStoreData) {
71 request = objectStore.add(badObjectStoreData[i].value,
72 badObjectStoreData[i].key);
73 request.onerror = errorHandler;
74 request.onsuccess = function(event) {
75 if (++addedData == badObjectStoreData.length) {
76 executeSoon(function() { testGenerator.next() });
77 }
78 }
79 }
80 yield undefined;
81 yield undefined;
82
83 objectStore = db.transaction(objectStoreName)
84 .objectStore(objectStoreName);
85
86 let keyIndex = 0;
87
88 request = objectStore.index("weight").openKeyCursor();
89 request.onerror = errorHandler;
90 request.onsuccess = function (event) {
91 let cursor = event.target.result;
92 if (cursor) {
93 is(cursor.key, objectStoreDataWeightSort[keyIndex].value.weight,
94 "Correct key");
95 is(cursor.primaryKey, objectStoreDataWeightSort[keyIndex].key,
96 "Correct value");
97 keyIndex++;
98
99 cursor.continue();
100 }
101 else {
102 testGenerator.next();
103 }
104 }
105 yield undefined;
106
107 is(keyIndex, objectStoreDataWeightSort.length, "Saw all weights");
108
109 keyIndex = 0;
110
111 request = objectStore.openCursor();
112 request.onerror = errorHandler;
113 request.onsuccess = function (event) {
114 let cursor = event.target.result;
115 if (cursor) {
116 keyIndex++;
117 cursor.continue();
118 }
119 else {
120 testGenerator.next();
121 }
122 }
123 yield undefined;
124
125 is(keyIndex, objectStoreData.length + badObjectStoreData.length,
126 "Saw all people");
127
128 finishTest();
129 yield undefined;
130 }

mercurial