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