| OLD | NEW |
| (Empty) | |
| 1 if (this.importScripts) { |
| 2 importScripts('../../../resources/testharness.js'); |
| 3 importScripts('generic-idb-operations.js'); |
| 4 } |
| 5 |
| 6 window=this; |
| 7 var isWorker = window.document === undefined; |
| 8 |
| 9 var runTests = function() { |
| 10 var numTestTransactions = 17; |
| 11 var numTestTransactionsReady = 0; |
| 12 var testTransactionReadyForChanges = function() { |
| 13 numTestTransactionsReady = numTestTransactionsReady + 1; |
| 14 if (numTestTransactionsReady === numTestTransactions) { |
| 15 if (isWorker) { |
| 16 postMessage(200); |
| 17 } else { |
| 18 postMessage(200, location.origin); |
| 19 } |
| 20 } else if (numTestTransactionsReady > numTestTransactions) { |
| 21 assert_unreached("Test added without incrementing test count."); |
| 22 } |
| 23 }; |
| 24 |
| 25 var openDB = function(t, dbName, openFunc) { |
| 26 var openRequest = indexedDB.open(dbName); |
| 27 openRequest.onupgradeneeded = t.unreached_func('upgrade should not be needed
'); |
| 28 openRequest.onsuccess = t.step_func(function() { |
| 29 openFunc(openRequest.result); |
| 30 }); |
| 31 openRequest.onerror = t.unreached_func('opening database should not fail'); |
| 32 }; |
| 33 |
| 34 async_test(function(t) { |
| 35 var expectedChanges = { |
| 36 dbName: 'observersDB1', |
| 37 records: { |
| 38 'store1': [{type: 'delete', key: {lower: 'a', upper: 'b'}}, |
| 39 {type: 'put', key: 'a'}], |
| 40 'store2': [{type: 'add', key: 'z'}] |
| 41 } |
| 42 }; |
| 43 |
| 44 var connection = null; |
| 45 var observeFunction = function(changes) { |
| 46 compareChanges(changes, expectedChanges); |
| 47 assert_true(connection != null); |
| 48 obs.unobserve(connection); |
| 49 t.done(); |
| 50 }; |
| 51 |
| 52 var obs = new IDBObserver( |
| 53 t.step_func(observeFunction), |
| 54 { operationTypes: ['clear', 'put', 'add', 'delete'] }); |
| 55 |
| 56 openDB(t, "observersDB1", t.step_func(function(db) { |
| 57 connection = db; |
| 58 var txn = db.transaction(['store1', 'store2'], 'readwrite'); |
| 59 |
| 60 // Verify initial state. |
| 61 var os1 = txn.objectStore('store1'); |
| 62 var readReq1 = os1.get('a'); |
| 63 readReq1.onsuccess = t.step_func(function() { |
| 64 assert_equals(readReq1.result, 'b', 'Initial state incorrect.'); |
| 65 }); |
| 66 var os2 = txn.objectStore('store2'); |
| 67 var readReq2 = os2.get('x'); |
| 68 readReq2.onsuccess = t.step_func(function() { |
| 69 assert_equals(readReq2.result, 'y', 'Initial state incorrect.'); |
| 70 }); |
| 71 |
| 72 // Start observing! |
| 73 obs.observe(db, txn); |
| 74 |
| 75 txn.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 76 txn.onerror = t.step_func(function() { |
| 77 console.log('got error'); |
| 78 console.log(txn.error); |
| 79 t.fail(txn.error); |
| 80 }); |
| 81 })); |
| 82 }, 'IDB Observers: Verify initial state and record all operations.'); |
| 83 |
| 84 async_test(function(t) { |
| 85 var expectedChanges = { |
| 86 dbName: 'observersDB1', |
| 87 records: { |
| 88 'store2': [{type: 'add', key: 'z'}] |
| 89 } |
| 90 }; |
| 91 |
| 92 var connection = null; |
| 93 var observeFunction = function(changes) { |
| 94 compareChanges(changes, expectedChanges); |
| 95 assert_true(connection != null); |
| 96 obs.unobserve(connection); |
| 97 t.done(); |
| 98 }; |
| 99 |
| 100 var obs = new IDBObserver( |
| 101 t.step_func(observeFunction), |
| 102 { operationTypes: ['add'] }); |
| 103 |
| 104 openDB(t, "observersDB1", t.step_func(function(db) { |
| 105 connection = db; |
| 106 var txn = db.transaction(['store2'], 'readonly'); |
| 107 obs.observe(db, txn); |
| 108 txn.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 109 txn.onerror = t.unreached_func('transaction should not fail') |
| 110 })); |
| 111 }, 'IDB Observers: Operation filtering.'); |
| 112 |
| 113 async_test(function(t) { |
| 114 var expectedChanges = { |
| 115 dbName: 'observersDB1', |
| 116 records: { |
| 117 'store2': [{type: 'add', key: 'z'}] |
| 118 } |
| 119 }; |
| 120 |
| 121 var connection = null; |
| 122 var observeFunction = function(changes) { |
| 123 compareChanges(changes, expectedChanges); |
| 124 assert_true(connection != null); |
| 125 obs.unobserve(connection); |
| 126 t.done(); |
| 127 }; |
| 128 |
| 129 var obs = new IDBObserver( |
| 130 t.step_func(observeFunction), |
| 131 { operationTypes: ['clear', 'put', 'add', 'delete'] }); |
| 132 |
| 133 openDB(t, "observersDB1", t.step_func(function(db) { |
| 134 connection = db; |
| 135 var txn = db.transaction(['store2'], 'readonly'); |
| 136 obs.observe(db, txn); |
| 137 txn.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 138 txn.onerror = t.unreached_func('transaction should not fail') |
| 139 })); |
| 140 }, 'IDB Observers: Object store filtering.'); |
| 141 |
| 142 async_test(function(t) { |
| 143 var expectedChanges1 = { |
| 144 dbName: 'observersDB1', |
| 145 records: { |
| 146 'store1': [{type: 'delete', key: {lower: 'a', upper: 'b'}}, |
| 147 {type: 'put', key: 'a'}], |
| 148 'store2': [{type: 'add', key: 'z'}] |
| 149 } |
| 150 }; |
| 151 |
| 152 var expectedChanges2 = { |
| 153 dbName: 'observersDB2', |
| 154 records: { |
| 155 'store3': [{type: 'put', key: 'c'}], |
| 156 'store4': [{type: 'add', key: 'z'}] |
| 157 } |
| 158 }; |
| 159 |
| 160 var connection1 = null; |
| 161 var connection2 = null; |
| 162 var pendingObserves = 2; |
| 163 var observeFunction = function(changes) { |
| 164 pendingObserves = pendingObserves - 1; |
| 165 if (changes.database.name == 'observersDB1') { |
| 166 compareChanges(changes, expectedChanges1); |
| 167 assert_true(connection1 != null); |
| 168 obs.unobserve(connection1); |
| 169 } else if (changes.database.name == 'observersDB2') { |
| 170 compareChanges(changes, expectedChanges2); |
| 171 assert_true(connection2 != null); |
| 172 obs.unobserve(connection2); |
| 173 } |
| 174 if (pendingObserves == 0) { |
| 175 t.done(); |
| 176 } else if (pendingObserves < 0) { |
| 177 assert_unreached("incorrect pendingObserves"); |
| 178 } |
| 179 }; |
| 180 |
| 181 var obs = new IDBObserver( |
| 182 t.step_func(observeFunction), |
| 183 { operationTypes: ['clear', 'put', 'add', 'delete'] }); |
| 184 |
| 185 openDB(t, "observersDB1", t.step_func(function(db) { |
| 186 connection1 = db; |
| 187 var txn = db.transaction(['store1', 'store2'], 'readonly'); |
| 188 obs.observe(db, txn); |
| 189 txn.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 190 txn.onerror = t.unreached_func('transaction should not fail') |
| 191 })); |
| 192 openDB(t, "observersDB2", t.step_func(function(db) { |
| 193 connection2 = db; |
| 194 var txn = db.transaction(['store3', 'store4'], 'readonly'); |
| 195 obs.observe(db, txn); |
| 196 txn.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 197 txn.onerror = t.unreached_func('transaction should not fail') |
| 198 })); |
| 199 }, 'IDB Observers: Multiple connections'); |
| 200 |
| 201 async_test(function(t) { |
| 202 var expectedChanges = { |
| 203 dbName: 'observersDB1', |
| 204 records: { |
| 205 'store1': [{type: 'delete', key: {lower: 'a', upper: 'b'}}, |
| 206 {type: 'put', key: 'a'}], |
| 207 'store2': [{type: 'add', key: 'z'}] |
| 208 } |
| 209 }; |
| 210 |
| 211 var connection = null; |
| 212 var pendingObserves = 2; |
| 213 var observeFunction = function(changes) { |
| 214 compareChanges(changes, expectedChanges); |
| 215 pendingObserves = pendingObserves - 1; |
| 216 if (pendingObserves == 0) { |
| 217 assert_true(connection != null); |
| 218 obs.unobserve(connection); |
| 219 t.done(); |
| 220 } else if (pendingObserves < 0) { |
| 221 assert_unreached("incorrect pendingObserves"); |
| 222 } |
| 223 }; |
| 224 |
| 225 var obs = new IDBObserver( |
| 226 t.step_func(observeFunction), |
| 227 { operationTypes: ['clear', 'put', 'add', 'delete'] }); |
| 228 |
| 229 openDB(t, "observersDB1", t.step_func(function(db) { |
| 230 connection = db; |
| 231 var txn = db.transaction(['store1', 'store2'], 'readonly'); |
| 232 obs.observe(db, txn); |
| 233 obs.observe(db, txn); |
| 234 txn.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 235 txn.onerror = t.unreached_func('transaction should not fail'); |
| 236 })); |
| 237 }, 'IDB Observers: Multiple observer calls.'); |
| 238 |
| 239 async_test(function(t) { |
| 240 var partOneChanges1 = { |
| 241 dbName: 'observersDB1', |
| 242 records: { |
| 243 'store1': [{type: 'delete', key: {lower: 'a', upper: 'b'}}, |
| 244 {type: 'put', key: 'a'}] |
| 245 } |
| 246 }; |
| 247 |
| 248 var partOneChanges2 = { |
| 249 dbName: 'observersDB1', |
| 250 records: { |
| 251 'store2': [{type: 'add', key: 'z'}] |
| 252 } |
| 253 }; |
| 254 |
| 255 var partOneChanges3 = { |
| 256 dbName: 'observersDB2', |
| 257 records: { |
| 258 'store3': [{type: 'put', key: 'c'}], |
| 259 'store4': [{type: 'add', key: 'z'}] |
| 260 } |
| 261 }; |
| 262 |
| 263 var partTwoChanges2 = { |
| 264 dbName: 'observersDB1', |
| 265 records: { |
| 266 'store2': [{type: 'put', key: 'z'}] |
| 267 } |
| 268 }; |
| 269 |
| 270 var connection1 = null; |
| 271 var connection2 = null; |
| 272 var connection3 = null; |
| 273 |
| 274 var pendingObserves = 4; |
| 275 var observeFunction = function(changes) { |
| 276 pendingObserves = pendingObserves - 1; |
| 277 var firstRound = pendingObserves != 0; |
| 278 |
| 279 if (changes.database == connection1) { |
| 280 assert_true(firstRound, "connection 1 should have been unobserved"); |
| 281 compareChanges(changes, partOneChanges1); |
| 282 obs.unobserve(connection1); |
| 283 } else if (changes.database == connection2) { |
| 284 if (firstRound) |
| 285 compareChanges(changes, partOneChanges2); |
| 286 else |
| 287 compareChanges(changes, partTwoChanges2); |
| 288 } else if (changes.database == connection3) { |
| 289 assert_true(firstRound, "connection 3 should have been unobserved "); |
| 290 compareChanges(changes, partOneChanges3); |
| 291 obs.unobserve(connection3); |
| 292 } else { |
| 293 assert_unreached('Unknown connection supplied with changes.'); |
| 294 } |
| 295 if (pendingObserves == 0) { |
| 296 t.done(); |
| 297 } else if (pendingObserves < 0) { |
| 298 assert_unreached("incorrect pendingObserves"); |
| 299 } |
| 300 }; |
| 301 |
| 302 var obs = new IDBObserver( |
| 303 t.step_func(observeFunction), |
| 304 { operationTypes: ['clear', 'put', 'add', 'delete'] }); |
| 305 |
| 306 openDB(t, "observersDB1", t.step_func(function(db) { |
| 307 connection1 = db; |
| 308 var txn = db.transaction(['store1'], 'readonly'); |
| 309 obs.observe(db, txn); |
| 310 txn.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 311 txn.onerror = t.unreached_func('transaction should not fail'); |
| 312 })); |
| 313 openDB(t, "observersDB1", t.step_func(function(db) { |
| 314 connection2 = db; |
| 315 var txn = db.transaction(['store2'], 'readonly'); |
| 316 obs.observe(db, txn); |
| 317 txn.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 318 txn.onerror = t.unreached_func('transaction should not fail'); |
| 319 })); |
| 320 openDB(t, "observersDB2", t.step_func(function(db) { |
| 321 connection3 = db; |
| 322 var txn = db.transaction(['store3', 'store4'], 'readonly'); |
| 323 obs.observe(db, txn); |
| 324 txn.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 325 txn.onerror = t.unreached_func('transaction should not fail') |
| 326 })); |
| 327 }, 'IDB Observers: Three connections, unobserve two.'); |
| 328 |
| 329 async_test(function(t) { |
| 330 var connection1 = null; |
| 331 var connection2 = null; |
| 332 |
| 333 var observeFunction = function(changes) { |
| 334 assert_equals(changes.database, connection2); |
| 335 obs.unobserve(connection2); |
| 336 t.done(); |
| 337 }; |
| 338 |
| 339 var obs = new IDBObserver( |
| 340 t.step_func(observeFunction), |
| 341 { operationTypes: ['clear', 'put', 'add', 'delete'] }); |
| 342 |
| 343 openDB(t, "observersDB1", t.step_func(function(db) { |
| 344 connection1 = db; |
| 345 var txn = db.transaction(['store1'], 'readonly'); |
| 346 obs.observe(db, txn); |
| 347 obs.unobserve(db); |
| 348 txn.oncomplete = t.step_func(function() { |
| 349 openDB(t, "observersDB1", t.step_func(function(db) { |
| 350 connection2 = db; |
| 351 var txn = db.transaction(['store2'], 'readonly'); |
| 352 obs.observe(db, txn); |
| 353 txn.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 354 txn.onerror = t.unreached_func('transaction should not fail'); |
| 355 })); |
| 356 }); |
| 357 txn.onerror = t.unreached_func('transaction should not fail'); |
| 358 })); |
| 359 }, 'IDB Observers: Unobserve immediately.'); |
| 360 |
| 361 async_test(function(t) { |
| 362 var connection1 = null; |
| 363 var connection2 = null; |
| 364 |
| 365 var observeFunction = function(changes) { |
| 366 assert_equals(changes.database, connection2); |
| 367 obs.unobserve(connection2); |
| 368 t.done(); |
| 369 }; |
| 370 |
| 371 var obs = new IDBObserver( |
| 372 t.step_func(observeFunction), |
| 373 { operationTypes: ['clear', 'put', 'add', 'delete'] }); |
| 374 |
| 375 openDB(t, "observersDB1", t.step_func(function(db) { |
| 376 connection1 = db; |
| 377 var txn = db.transaction(['store1'], 'readonly'); |
| 378 obs.observe(db, txn); |
| 379 obs.observe(db, txn); |
| 380 obs.observe(db, txn); |
| 381 obs.unobserve(db); |
| 382 txn.oncomplete = t.step_func(function() { |
| 383 openDB(t, "observersDB1", t.step_func(function(db) { |
| 384 connection2 = db; |
| 385 var txn = db.transaction(['store2'], 'readonly'); |
| 386 obs.observe(db, txn); |
| 387 txn.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 388 txn.onerror = t.unreached_func('transaction should not fail'); |
| 389 })); |
| 390 }); |
| 391 txn.onerror = t.unreached_func('transaction should not fail'); |
| 392 })); |
| 393 }, 'IDB Observers: Unobserve immediately on multiple.'); |
| 394 |
| 395 async_test(function(t) { |
| 396 var expectedChanges1 = { |
| 397 dbName: 'observersDB1', |
| 398 records: { |
| 399 'store1': [{type: 'delete', key: {lower: 'a', upper: 'b'}}, |
| 400 {type: 'put', key: 'a'}] |
| 401 } |
| 402 }; |
| 403 var expectedChanges2 = { |
| 404 dbName: 'observersDB1', |
| 405 records: { |
| 406 'store2': [{type: 'add', key: 'z'}] |
| 407 } |
| 408 }; |
| 409 |
| 410 var connection1 = null; |
| 411 var connection2 = null; |
| 412 var connection1Observes = 0; |
| 413 var connection2Observes = 0; |
| 414 var observeFunction = function(changes) { |
| 415 if (changes.database == connection1) { |
| 416 connection1Observes = connection1Observes + 1; |
| 417 assert_true(connection1Observes <= 2); |
| 418 if (changes.records.has('store1')) { |
| 419 compareChanges(changes, expectedChanges1); |
| 420 } else if (changes.records.has('store2')) { |
| 421 compareChanges(changes, expectedChanges2); |
| 422 } else { |
| 423 assert_unreached("unknown changes"); |
| 424 } |
| 425 if (connection1Observes == 2) { |
| 426 obs.unobserve(connection1); |
| 427 } |
| 428 } else if (changes.database == connection2) { |
| 429 connection2Observes = connection2Observes + 1; |
| 430 if (connection2Observes > 1) { |
| 431 t.done(); |
| 432 } |
| 433 } else { |
| 434 assert_unreached("unknown changes"); |
| 435 } |
| 436 }; |
| 437 |
| 438 var obs = new IDBObserver( |
| 439 t.step_func(observeFunction), |
| 440 { operationTypes: ['clear', 'put', 'add', 'delete'] }); |
| 441 |
| 442 openDB(t, "observersDB1", t.step_func(function(db) { |
| 443 connection1 = db; |
| 444 var txn1 = db.transaction(['store1'], 'readonly'); |
| 445 var txn2 = db.transaction(['store2'], 'readonly'); |
| 446 // Start observing! |
| 447 obs.observe(db, txn1); |
| 448 obs.observe(db, txn2); |
| 449 |
| 450 txn1.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 451 txn2.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 452 txn1.onerror = t.unreached_func('transaction should not fail'); |
| 453 txn2.onerror = t.unreached_func('transaction should not fail'); |
| 454 })); |
| 455 openDB(t, "observersDB2", t.step_func(function(db) { |
| 456 connection2 = db; |
| 457 var txn = db.transaction(['store3'], 'readonly'); |
| 458 obs.observe(db, txn); |
| 459 txn.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 460 txn.onerror = t.unreached_func('transaction should not fail'); |
| 461 })); |
| 462 }, 'IDB Observers: Unobserve connection removes observers.'); |
| 463 |
| 464 async_test(function(t) { |
| 465 var expectedChanges1 = { |
| 466 dbName: 'observersDB1', |
| 467 records: { |
| 468 'store1': [{type: 'delete', key: {lower: 'a', upper: 'b'}}, |
| 469 {type: 'put', key: 'a'}] |
| 470 } |
| 471 }; |
| 472 var expectedChanges2 = { |
| 473 dbName: 'observersDB1', |
| 474 records: { |
| 475 'store2': [{type: 'add', key: 'z'}] |
| 476 } |
| 477 }; |
| 478 |
| 479 var connection1 = null; |
| 480 var connection2 = null; |
| 481 var connection1Observes = 0; |
| 482 var connection2Observes = 0; |
| 483 var observeFunction = function(changes) { |
| 484 if (changes.database == connection1) { |
| 485 connection1Observes = connection1Observes + 1; |
| 486 assert_true(connection1Observes <= 2); |
| 487 if (changes.records.has('store1')) { |
| 488 compareChanges(changes, expectedChanges1); |
| 489 } else if (changes.records.has('store2')) { |
| 490 compareChanges(changes, expectedChanges2); |
| 491 } else { |
| 492 assert_unreached("unknown changes"); |
| 493 } |
| 494 if (connection1Observes == 2) { |
| 495 connection1.close(); |
| 496 } |
| 497 } else if (changes.database == connection2) { |
| 498 connection2Observes = connection2Observes + 1; |
| 499 if (connection2Observes > 1) { |
| 500 t.done(); |
| 501 } |
| 502 } else { |
| 503 assert_unreached("unknown changes"); |
| 504 } |
| 505 }; |
| 506 |
| 507 var obs = new IDBObserver( |
| 508 t.step_func(observeFunction), |
| 509 { operationTypes: ['clear', 'put', 'add', 'delete'] }); |
| 510 |
| 511 openDB(t, "observersDB1", t.step_func(function(db) { |
| 512 connection1 = db; |
| 513 var txn1 = db.transaction(['store1'], 'readonly'); |
| 514 var txn2 = db.transaction(['store2'], 'readonly'); |
| 515 // Start observing! |
| 516 obs.observe(db, txn1); |
| 517 obs.observe(db, txn2); |
| 518 |
| 519 txn1.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 520 txn2.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 521 txn1.onerror = t.unreached_func('transaction should not fail'); |
| 522 txn2.onerror = t.unreached_func('transaction should not fail'); |
| 523 })); |
| 524 openDB(t, "observersDB2", t.step_func(function(db) { |
| 525 connection2 = db; |
| 526 var txn = db.transaction(['store3'], 'readonly'); |
| 527 obs.observe(db, txn); |
| 528 txn.oncomplete = t.step_func(testTransactionReadyForChanges); |
| 529 txn.onerror = t.unreached_func('transaction should not fail'); |
| 530 })); |
| 531 }, 'IDB Observers: Close connection removes observers.'); |
| 532 } |
| 533 |
| 534 if (isWorker) { |
| 535 runTests(); |
| 536 } |
| OLD | NEW |