dom/indexedDB/test/unit/test_keys.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 dbname = this.window ? window.location.pathname : "Splendid Test";
michael@0 11 const RW = "readwrite"
michael@0 12 let c1 = 1;
michael@0 13 let c2 = 1;
michael@0 14
michael@0 15 let openRequest = indexedDB.open(dbname, 1);
michael@0 16 openRequest.onerror = errorHandler;
michael@0 17 openRequest.onupgradeneeded = grabEventAndContinueHandler;
michael@0 18 openRequest.onsuccess = unexpectedSuccessHandler;
michael@0 19 let event = yield undefined;
michael@0 20 let db = event.target.result;
michael@0 21 let trans = event.target.transaction;
michael@0 22
michael@0 23 // Create test stores
michael@0 24 let store = db.createObjectStore("store");
michael@0 25
michael@0 26 // Test simple inserts
michael@0 27 var keys = [
michael@0 28 -1/0,
michael@0 29 -1.7e308,
michael@0 30 -10000,
michael@0 31 -2,
michael@0 32 -1.5,
michael@0 33 -1,
michael@0 34 -1.00001e-200,
michael@0 35 -1e-200,
michael@0 36 0,
michael@0 37 1e-200,
michael@0 38 1.00001e-200,
michael@0 39 1,
michael@0 40 2,
michael@0 41 10000,
michael@0 42 1.7e308,
michael@0 43 1/0,
michael@0 44 new Date("1750-01-02"),
michael@0 45 new Date("1800-12-31T12:34:56.001"),
michael@0 46 new Date(-1000),
michael@0 47 new Date(-10),
michael@0 48 new Date(-1),
michael@0 49 new Date(0),
michael@0 50 new Date(1),
michael@0 51 new Date(2),
michael@0 52 new Date(1000),
michael@0 53 new Date("1971-01-01"),
michael@0 54 new Date("1971-01-01T01:01:01Z"),
michael@0 55 new Date("1971-01-01T01:01:01.001Z"),
michael@0 56 new Date("1971-01-01T01:01:01.01Z"),
michael@0 57 new Date("1971-01-01T01:01:01.1Z"),
michael@0 58 new Date("1980-02-02"),
michael@0 59 new Date("3333-03-19T03:33:33.333"),
michael@0 60 "",
michael@0 61 "\x00",
michael@0 62 "\x00\x00",
michael@0 63 "\x00\x01",
michael@0 64 "\x01",
michael@0 65 "\x02",
michael@0 66 "\x03",
michael@0 67 "\x04",
michael@0 68 "\x07",
michael@0 69 "\x08",
michael@0 70 "\x0F",
michael@0 71 "\x10",
michael@0 72 "\x1F",
michael@0 73 "\x20",
michael@0 74 "01234",
michael@0 75 "\x3F",
michael@0 76 "\x40",
michael@0 77 "A",
michael@0 78 "A\x00",
michael@0 79 "A1",
michael@0 80 "ZZZZ",
michael@0 81 "a",
michael@0 82 "a\x00",
michael@0 83 "aa",
michael@0 84 "azz",
michael@0 85 "}",
michael@0 86 "\x7E",
michael@0 87 "\x7F",
michael@0 88 "\x80",
michael@0 89 "\xFF",
michael@0 90 "\u0100",
michael@0 91 "\u01FF",
michael@0 92 "\u0200",
michael@0 93 "\u03FF",
michael@0 94 "\u0400",
michael@0 95 "\u07FF",
michael@0 96 "\u0800",
michael@0 97 "\u0FFF",
michael@0 98 "\u1000",
michael@0 99 "\u1FFF",
michael@0 100 "\u2000",
michael@0 101 "\u3FFF",
michael@0 102 "\u4000",
michael@0 103 "\u7FFF",
michael@0 104 "\u8000",
michael@0 105 "\uD800",
michael@0 106 "\uD800a",
michael@0 107 "\uD800\uDC01",
michael@0 108 "\uDBFF",
michael@0 109 "\uDC00",
michael@0 110 "\uDFFF\uD800",
michael@0 111 "\uFFFE",
michael@0 112 "\uFFFF",
michael@0 113 "\uFFFF\x00",
michael@0 114 "\uFFFFZZZ",
michael@0 115 [],
michael@0 116 [-1/0],
michael@0 117 [-1],
michael@0 118 [0],
michael@0 119 [1],
michael@0 120 [1, "a"],
michael@0 121 [1, []],
michael@0 122 [1, [""]],
michael@0 123 [2, 3],
michael@0 124 [2, 3.0000000000001],
michael@0 125 [12, [[]]],
michael@0 126 [12, [[[]]]],
michael@0 127 [12, [[[""]]]],
michael@0 128 [12, [[["foo"]]]],
michael@0 129 [12, [[[[[3]]]]]],
michael@0 130 [12, [[[[[[3]]]]]]],
michael@0 131 [new Date(-1)],
michael@0 132 [new Date(1)],
michael@0 133 [""],
michael@0 134 ["", [[]]],
michael@0 135 ["", [[[]]]],
michael@0 136 ["abc"],
michael@0 137 ["abc", "def"],
michael@0 138 ["abc\x00"],
michael@0 139 ["abc\x00", "\x00\x01"],
michael@0 140 ["abc\x00", "\x00def"],
michael@0 141 ["abc\x00\x00def"],
michael@0 142 ["x", [[]]],
michael@0 143 ["x", [[[]]]],
michael@0 144 [[]],
michael@0 145 [[],"foo"],
michael@0 146 [[],[]],
michael@0 147 [[[]]],
michael@0 148 [[[]], []],
michael@0 149 [[[]], [[]]],
michael@0 150 [[[]], [[1]]],
michael@0 151 [[[]], [[[]]]],
michael@0 152 [[[1]]],
michael@0 153 [[[[]], []]],
michael@0 154 ];
michael@0 155
michael@0 156 for (var i = 0; i < keys.length; ++i) {
michael@0 157 let keyI = keys[i];
michael@0 158 is(indexedDB.cmp(keyI, keyI), 0, i + " compared to self");
michael@0 159
michael@0 160 function doCompare(keyI) {
michael@0 161 for (var j = i-1; j >= i-10 && j >= 0; --j) {
michael@0 162 is(indexedDB.cmp(keyI, keys[j]), 1, i + " compared to " + j);
michael@0 163 is(indexedDB.cmp(keys[j], keyI), -1, j + " compared to " + i);
michael@0 164 }
michael@0 165 }
michael@0 166
michael@0 167 doCompare(keyI);
michael@0 168 store.add(i, keyI).onsuccess = function(e) {
michael@0 169 is(indexedDB.cmp(e.target.result, keyI), 0,
michael@0 170 "Returned key should cmp as equal");
michael@0 171 ok(compareKeys(e.target.result, keyI),
michael@0 172 "Returned key should actually be equal");
michael@0 173 };
michael@0 174
michael@0 175 // Test that -0 compares the same as 0
michael@0 176 if (keyI === 0) {
michael@0 177 doCompare(-0);
michael@0 178 let req = store.add(i, -0);
michael@0 179 req.addEventListener("error", new ExpectError("ConstraintError", true));
michael@0 180 req.onsuccess = unexpectedSuccessHandler;
michael@0 181 yield undefined;
michael@0 182 }
michael@0 183 else if (Array.isArray(keyI) && keyI.length === 1 && keyI[0] === 0) {
michael@0 184 doCompare([-0]);
michael@0 185 let req = store.add(i, [-0]);
michael@0 186 req.addEventListener("error", new ExpectError("ConstraintError", true));
michael@0 187 req.onsuccess = unexpectedSuccessHandler;
michael@0 188 yield undefined;
michael@0 189 }
michael@0 190 }
michael@0 191
michael@0 192 store.openCursor().onsuccess = grabEventAndContinueHandler;
michael@0 193 for (i = 0; i < keys.length; ++i) {
michael@0 194 event = yield undefined;
michael@0 195 let cursor = event.target.result;
michael@0 196 is(indexedDB.cmp(cursor.key, keys[i]), 0,
michael@0 197 "Read back key should cmp as equal");
michael@0 198 ok(compareKeys(cursor.key, keys[i]),
michael@0 199 "Read back key should actually be equal");
michael@0 200 is(cursor.value, i, "Stored with right value");
michael@0 201
michael@0 202 cursor.continue();
michael@0 203 }
michael@0 204 event = yield undefined;
michael@0 205 is(event.target.result, undefined, "no more results expected");
michael@0 206
michael@0 207 var nan = 0/0;
michael@0 208 var invalidKeys = [
michael@0 209 nan,
michael@0 210 undefined,
michael@0 211 null,
michael@0 212 /x/,
michael@0 213 {},
michael@0 214 new Date(NaN),
michael@0 215 new Date("foopy"),
michael@0 216 [nan],
michael@0 217 [undefined],
michael@0 218 [null],
michael@0 219 [/x/],
michael@0 220 [{}],
michael@0 221 [new Date(NaN)],
michael@0 222 [1, nan],
michael@0 223 [1, undefined],
michael@0 224 [1, null],
michael@0 225 [1, /x/],
michael@0 226 [1, {}],
michael@0 227 [1, [nan]],
michael@0 228 [1, [undefined]],
michael@0 229 [1, [null]],
michael@0 230 [1, [/x/]],
michael@0 231 [1, [{}]],
michael@0 232 ];
michael@0 233
michael@0 234 for (i = 0; i < invalidKeys.length; ++i) {
michael@0 235 try {
michael@0 236 indexedDB.cmp(invalidKeys[i], 1);
michael@0 237 ok(false, "didn't throw");
michael@0 238 }
michael@0 239 catch(ex) {
michael@0 240 ok(ex instanceof DOMException, "Threw DOMException");
michael@0 241 is(ex.name, "DataError", "Threw right DOMException");
michael@0 242 is(ex.code, 0, "Threw with right code");
michael@0 243 }
michael@0 244 try {
michael@0 245 indexedDB.cmp(1, invalidKeys[i]);
michael@0 246 ok(false, "didn't throw2");
michael@0 247 }
michael@0 248 catch(ex) {
michael@0 249 ok(ex instanceof DOMException, "Threw DOMException2");
michael@0 250 is(ex.name, "DataError", "Threw right DOMException2");
michael@0 251 is(ex.code, 0, "Threw with right code2");
michael@0 252 }
michael@0 253 try {
michael@0 254 store.put(1, invalidKeys[i]);
michael@0 255 ok(false, "didn't throw3");
michael@0 256 }
michael@0 257 catch(ex) {
michael@0 258 ok(ex instanceof DOMException, "Threw DOMException3");
michael@0 259 is(ex.name, "DataError", "Threw right DOMException3");
michael@0 260 is(ex.code, 0, "Threw with right code3");
michael@0 261 }
michael@0 262 }
michael@0 263
michael@0 264 openRequest.onsuccess = grabEventAndContinueHandler;
michael@0 265 yield undefined;
michael@0 266
michael@0 267 finishTest();
michael@0 268 yield undefined;
michael@0 269 }

mercurial