OLD | NEW |
| 1 /* This test checks the following |
| 2 * Create observer during version change |
| 3 * Ignore observe call during version change |
| 4 * All operations {add, put, delete, clear} recorded |
| 5 * Filtering object stores |
| 6 * Add observer with an empty transaction. |
| 7 * Observer Callback fired before transaction oncomplete. |
| 8 * Multiple observer callbacks. |
| 9 */ |
| 10 |
1 if (this.importScripts) { | 11 if (this.importScripts) { |
2 importScripts('../../../resources/testharness.js'); | 12 importScripts('../../../resources/testharness.js'); |
3 } | 13 } |
4 | 14 |
5 function callback(){}; | 15 (function(){ |
| 16 var callback1_count = 0, callback2_count = 0; |
| 17 var dbname = location.pathname + ' - ' + 'changes'; |
| 18 var operations = [{type: 'put', key : {lower : 1, upper : 1} }, |
| 19 {type: 'put', key : {lower : 2, upper : 2} }, |
| 20 {type: 'put', key : {lower : 3, upper : 3} }, |
| 21 {type: 'put', key : {lower : 4, upper : 4} }, |
| 22 {type: 'kDelete', key: {lower : 1, upper : 3} }, |
| 23 {type: 'add', key : {lower : 1, upper : 1} }, |
| 24 {type: 'clear'} ]; |
6 | 25 |
7 async_test(function(t) { | 26 function observation_equals(actual, expected){ |
8 var description = 'observer addition and removal test'; | 27 assert_equals(actual.type, expected.type); |
9 var dbname = location.pathname + ' - ' + description; | 28 if(actual.type == 'clear') { |
10 var openRequest = indexedDB.open(dbname); | 29 assert_equals(actual.key, undefined, 'clear operation has no key'); |
11 var obs1 = new IDBObserver(callback, {transaction: true, values: true}); | 30 assert_equals(actual.value, null, 'clear operation has no value'); |
12 var obs2 = new IDBObserver(callback, {transaction: true, values: true}); | 31 return; |
| 32 } |
| 33 assert_equals(actual.key.lower, expected.key.lower, 'key lower bound'); |
| 34 assert_equals(actual.key.upper, expected.key.upper, 'key upper bound'); |
| 35 assert_equals(actual.key.lower_open, expected.key.lower_open, 'key lower o
pen'); |
| 36 assert_equals(actual.key.upper_open, expected.key.upper_open, 'key upper o
pen'); |
| 37 if(actual.type == 'kDelete') { |
| 38 assert_equals(actual.value, null, 'delete operation has no value'); |
| 39 return; |
| 40 } |
| 41 assert_equals(actual.value, null , 'observation value'); |
| 42 } |
13 | 43 |
14 openRequest.onupgradeneeded = t.step_func(function() { | 44 function callback1(changes){ |
| 45 assert_equals(changes.database.name, dbname, 'database'); |
| 46 assert_equals(changes.records.size, 1, 'observer records #objstore'); |
| 47 assert_true(changes.records.has('store')); |
| 48 |
| 49 var obsv = changes.records.get('store'); |
| 50 if(callback1_count == 0) |
| 51 assert_equals(obsv.length, 7, '#observations'); |
| 52 else |
| 53 assert_equals(obsv.length, 1, '#observations'); |
| 54 for(i in obsv) |
| 55 observation_equals(obsv[i], operations[i]); |
| 56 |
| 57 callback1_count++; |
| 58 }; |
| 59 |
| 60 function callback2(changes) { |
| 61 assert_equals(changes.database.name, dbname, 'database'); |
| 62 assert_equals(changes.records.size, 2, 'observer records #objstores'); |
| 63 assert_true(changes.records.has('store')); |
| 64 assert_true(changes.records.has('store2')); |
| 65 |
| 66 var obsv1 = changes.records.get('store'); |
| 67 assert_equals(obsv1.length, 1, '#observations'); |
| 68 observation_equals(obsv1[0], operations[0]); |
| 69 |
| 70 var obsv2 = changes.records.get('store2'); |
| 71 assert_equals(obsv2.length, 1, '#observations'); |
| 72 observation_equals(obsv2[0], operations[0]); |
| 73 |
| 74 callback2_count++; |
| 75 }; |
| 76 |
| 77 async_test(function(t) { |
| 78 var description = 'observer changes test'; |
| 79 var openRequest = indexedDB.open(dbname); |
| 80 var obs1 = new IDBObserver(t.step_func(callback1)); |
| 81 openRequest.onupgradeneeded = t.step_func(function() { |
| 82 var db = openRequest.result; |
| 83 db.createObjectStore('store'); |
| 84 db.createObjectStore('store2'); |
| 85 var obs2 = new IDBObserver(t.step_func(callback2)); |
| 86 // observe call in version change transaction is ignored |
| 87 obs2.observe(db, openRequest.transaction); |
| 88 }); |
| 89 openRequest.onsuccess = t.step_func(function() { |
15 var db = openRequest.result; | 90 var db = openRequest.result; |
16 db.createObjectStore('store'); | 91 var tx = db.transaction('store', 'readwrite'); // Empty transaction to add
the obs1. |
17 }); | 92 var tx2 = db.transaction(['store', 'store2'], 'readwrite'); |
18 openRequest.onsuccess = t.step_func(function() { | 93 obs1.observe(db, tx); |
19 var db = openRequest.result; | 94 obs2.observe(db, tx2); |
20 var tx = db.transaction('store', 'readwrite'); | 95 |
21 var store = tx.objectStore('store'); | 96 var store = tx2.objectStore('store'); |
22 var put_request = store.put(1,1); | 97 var store2 = tx2.objectStore('store2'); |
23 obs1.observe(db, tx); | 98 for(i = 1; i <= 4; i++) |
24 obs1.unobserve(db); | 99 store.put(i,i); |
25 obs1.observe(db, tx); | 100 store.delete(IDBKeyRange.bound(1,3)); |
26 obs2.observe(db, tx); | 101 store.add(1,1); |
27 tx.oncomplete = t.step_func(function(){ | 102 store.clear(); |
28 obs1.unobserve(db); | 103 |
29 t.done(); | 104 // This operation would not be observed. |
30 }); | 105 store2.put(2,2); |
| 106 tx.oncomplete = t.step_func(function(){ |
| 107 assert_equals(callback1_count, 0, 'callback1 count'); |
| 108 assert_equals(callback2_count, 0, 'callback1 count'); |
31 }); | 109 }); |
32 }, 'observer addition and removal test'); | 110 tx2.oncomplete = t.step_func(function(){ |
| 111 assert_equals(callback1_count, 1, 'callback1 count'); |
| 112 assert_equals(callback2_count, 0, 'callback1 count'); |
| 113 }); |
| 114 |
| 115 var tx3 = db.transaction(['store', 'store2'], 'readwrite'); |
| 116 var store3 = tx3.objectStore('store', 'readwrite'); |
| 117 var store4 = tx3.objectStore('store2', 'readwrite'); |
| 118 store3.put(1,1); |
| 119 store4.put(1,1); |
| 120 |
| 121 tx3.oncomplete = t.step_func(function(){ |
| 122 assert_equals(callback1_count, 2, 'callback1 count'); |
| 123 assert_equals(callback2_count, 1, 'callback1 count'); |
| 124 t.done(); |
| 125 }); |
| 126 }); |
| 127 }, 'observer changes test'); |
| 128 })(); |
33 | 129 |
34 done(); | |
35 | 130 |
OLD | NEW |