Chromium Code Reviews| Index: third_party/WebKit/LayoutTests/storage/indexeddb/resources/observer-changes.js |
| diff --git a/third_party/WebKit/LayoutTests/storage/indexeddb/resources/observer-changes.js b/third_party/WebKit/LayoutTests/storage/indexeddb/resources/observer-changes.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..62dddba2328837d254946570f2b1c70c757b96d1 |
| --- /dev/null |
| +++ b/third_party/WebKit/LayoutTests/storage/indexeddb/resources/observer-changes.js |
| @@ -0,0 +1,257 @@ |
| +if (this.importScripts) { |
| + importScripts('../../../resources/testharness.js'); |
| + importScripts('../../../resources/generic-idb-operations.js'); |
| +} |
| + |
| +(function() { |
| + var pathname = location.pathname + ' - ' + 'changes'; |
| + var callback_count = Array(6).fill(0); |
| + var dbname = []; |
| + for(var i = 1; i <= 6; i++) |
| + dbname.push(pathname + i.toString()); |
| + |
| + callback_count.fill(0); |
| + var putOps = [{type: 'put', key : 1, value: 1 }, |
| + {type: 'put', key : 2, value: 2 }, |
| + {type: 'put', key : 3, value: 3 },]; |
| + var deleteOps = [ {type: 'kDelete', key: {lower : 1, upper : 2} }, {type: 'kDelete', key: {lower : 3, upper : 3} }]; |
| + var addOps = [ {type: 'add', key : 3, value: 3 } ]; |
| + var clearOps = [ {type: 'clear'} ]; |
| + var put_records = { 'store': putOps }; |
| + |
| + var ops0 = putOps.concat(deleteOps, addOps, clearOps); |
| + var records0 = { 'store' : ops0 }; |
| + var observed_records0 = { 'store' : ops0 }; |
| + var observed_changes0 = { dbName: dbname[0], records: observed_records0 }; |
| + |
| + function callback0(changes) { |
| + compareChanges(changes, observed_changes0); |
| + callback_count[0]++; |
| + }; |
| + |
| + async_test(function(t) { |
| + var openRequest = indexedDB.open(dbname[0]); |
| + // TODO(palakj): operationType should be 'delete' instead of 'kDelete', once fixed. Issue crbug.com/609934. |
| + var obs = new IDBObserver(t.step_func(callback0), { operationTypes: [ 'clear', 'put', 'add', 'kDelete' ] }); |
| + openRequest.onupgradeneeded = t.step_func(function() { |
| + createDatabase(openRequest.result, ['store']); |
| + }); |
| + openRequest.onsuccess = t.step_func(function() { |
|
cmumford
2016/07/21 15:00:55
Add onerror to avoid having to wait for test to ti
|
| + var db = openRequest.result; |
| + var tx1 = db.transaction('store', 'readwrite'); |
| + var tx2 = db.transaction('store', 'readwrite'); |
| + obs.observe(db, tx1); |
| + operateOnTx(tx2, records0); |
| + |
| + tx1.oncomplete = t.step_func(function() { |
| + countCallbacks(callback_count[0], 0); |
| + }); |
| + tx1.onerror = t.unreached_func('transaction 1 should not fail') |
| + tx2.oncomplete = t.step_func(function() { |
| + countCallbacks(callback_count[0], 1); |
| + t.done(); |
| + }); |
| + tx2.onerror = t.unreached_func('transaction 2 should not fail') |
| + }); |
| + }, 'Observer: Record all operations'); |
| + |
| + var ops1 = putOps.concat(deleteOps, addOps, clearOps); |
| + var records1 = { 'store' : ops1 }; |
| + var observed_ops1 = putOps.concat(clearOps); |
| + var observed_records1 = { 'store' : observed_ops1 }; |
| + var observed_changes1 = { dbName: dbname[1], records: observed_records1 }; |
| + |
| + function callback1(changes) { |
| + compareChanges(changes, observed_changes1); |
| + callback_count[1]++; |
| + }; |
| + |
| + async_test(function(t) { |
| + var openRequest = indexedDB.open(dbname[1]); |
| + var cnt2 = 0; |
| + var obs1 = new IDBObserver(t.step_func(callback1), { operationTypes: ['clear', 'put'] }); |
| + var obs2 = new IDBObserver(t.step_func(function(){ |
| + cnt2++; |
| + })); |
| + |
| + openRequest.onupgradeneeded = t.step_func(function() { |
| + createDatabase(openRequest.result, ['store']); |
|
cmumford
2016/07/21 15:00:55
I know |createDatabase| landed in a prior CL, but
|
| + }); |
| + openRequest.onsuccess = t.step_func(function() { |
| + var db = openRequest.result; |
| + var tx1 = db.transaction('store', 'readwrite'); |
| + var tx2 = db.transaction('store', 'readwrite'); |
| + obs1.observe(db, tx1); |
| + obs2.observe(db, tx1); |
| + operateOnTx(tx2, records1); |
| + |
| + tx1.oncomplete = t.step_func(function() { |
| + countCallbacks(callback_count[1], 0); |
| + countCallbacks(cnt2, 0); |
| + |
| + }); |
| + tx2.oncomplete = t.step_func(function() { |
| + countCallbacks(callback_count[1], 1); |
| + countCallbacks(cnt2, 0); |
| + t.done(); |
| + }); |
| + }); |
| + }, 'Observer: Operation type filtering'); |
| + |
| + var records2 = { 'store' : putOps, 'store2' : addOps }; |
| + var observed_records2 = { 'store' : putOps }; |
| + var observed_changes2 = { dbName: dbname[2], records: observed_records2 }; |
| + |
| + function callback2(changes){ |
| + compareChanges(changes, observed_changes2); |
| + callback_count[2]++; |
| + } |
| + var stores = ['store', 'store2'] |
| + |
| + async_test(function(t) { |
| + var openRequest = indexedDB.open(dbname[2]); |
| + var obs = new IDBObserver(t.step_func(callback2), { operationTypes: ['put', 'add'] }); |
| + |
| + openRequest.onupgradeneeded = t.step_func(function() { |
| + createDatabase(openRequest.result, stores); |
| + }); |
| + openRequest.onsuccess = t.step_func(function() { |
| + var db = openRequest.result; |
| + var tx1 = db.transaction(stores[0], 'readwrite'); |
| + var tx2 = db.transaction(stores, 'readwrite'); |
| + obs.observe(db, tx1); |
| + operateOnTx(tx2, records2); |
| + |
| + tx1.oncomplete = t.step_func(function() { |
| + countCallbacks(callback_count[2], 0); |
| + }); |
| + tx2.oncomplete = t.step_func(function() { |
| + countCallbacks(callback_count[2], 1); |
| + t.done(); |
| + }); |
| + }); |
| + }, 'Observer: ObjectStore filtering'); |
| + |
| + var records3 = { 'store' : putOps }; |
| + var put_records = { 'store' : putOps }; |
| + var observed_changes3 = { dbName: dbname[3], records: put_records }; |
| + |
| + function callback3(changes){ |
| + compareChanges(changes, observed_changes3); |
| + callback_count[3]++; |
| + } |
| + async_test(function(t) { |
| + var openRequest = indexedDB.open(dbname[3]); |
| + var obs = new IDBObserver(t.step_func(callback3), { operationTypes: ['put'] }); |
| + |
| + openRequest.onupgradeneeded = t.step_func(function() { |
| + createDatabase(openRequest.result, ['store']); |
| + }); |
| + openRequest.onsuccess = t.step_func(function() { |
| + var db = openRequest.result; |
| + var tx1 = db.transaction('store', 'readwrite'); |
| + // External transaction on same database connection. |
| + operateOnDb(db, records3); |
| + var tx2 = db.transaction('store', 'readwrite'); |
| + operateOnTx(tx2, records3); |
| + obs.observe(db, tx1); |
| + |
| + tx1.oncomplete = t.step_func(function() { |
| + countCallbacks(callback_count[3], 0); |
| + }); |
| + tx2.oncomplete = t.step_func(function() { |
| + countCallbacks(callback_count[3], 2); |
| + t.done(); |
| + }); |
| + tx2.onerror = t.unreached_func('tx should not have error'); |
| + }); |
| + }, 'Observer : Records external transaction'); |
| + |
| + var observed_changes4 = { dbName: dbname[4], records: put_records }; |
| + |
| + function callback4(changes){ |
| + compareChanges(changes, observed_changes4); |
| + callback_count[4]++; |
| + } |
| + |
| + async_test(function(t) { |
| + var openRequest = indexedDB.open(dbname[4]); |
| + var obs = new IDBObserver(t.step_func(callback4), { operationTypes: ['put'] }); |
| + |
| + openRequest.onupgradeneeded = t.step_func(function() { |
| + createDatabase(openRequest.result, ['store']); |
| + }); |
| + openRequest.onsuccess = t.step_func(function() { |
| + var db = openRequest.result; |
| + var tx1 = db.transaction('store', 'readwrite'); |
| + obs.observe(db, tx1); |
| + tx1.oncomplete = t.step_func(function() { |
| + countCallbacks(callback_count[4], 0); |
| + }); |
| + tx1.onerror = t.unreached_func('transaction should not fail'); |
| + |
| + var openRequest2 = indexedDB.open(dbname[4]); |
| + openRequest2.onsuccess = t.step_func(function(){ |
| + var db2 = openRequest2.result; |
| + var tx2 = db2.transaction('store', 'readwrite'); |
| + operateOnTx(tx2, put_records); |
| + tx2.oncomplete = function(){ |
| + countCallbacks(callback_count[4], 1); |
| + t.done(); |
| + } |
| + tx2.onerror = t.unreached_func('transaction should not fail'); |
| + }); |
| + }); |
| + }, 'Observer : Record changes on another connection'); |
| + |
| + var records5_2 = {'store2' : putOps} |
| + var observed_changes5_1 = { dbName: dbname[5], records: put_records }; |
| + var observed_changes5_2 = { dbName: dbname[5], records: records5_2 }; |
| + |
| + function callback5(changes){ |
| + assert_true(callback_count[5] < 2); |
| + if(callback_count[5] == 0) |
| + compareChanges(changes, observed_changes5_1); |
| + else |
| + compareChanges(changes, observed_changes5_2); |
| + callback_count[5]++; |
| + } |
| + |
| + async_test(function(t) { |
| + var openRequest = indexedDB.open(dbname[5]); |
| + var obs = new IDBObserver(t.step_func(callback5), { operationTypes: ['put'] }); |
| + |
| + openRequest.onupgradeneeded = t.step_func(function() { |
| + createDatabase(openRequest.result, [ 'store', 'store2' ]); |
| + }); |
| + openRequest.onsuccess = t.step_func(function() { |
| + var db = openRequest.result; |
| + var tx1 = db.transaction([ 'store', 'store2'], 'readwrite'); |
| + var tx2 = db.transaction('store', 'readwrite'); |
| + |
| + obs.observe(db, tx1); |
| + tx1.objectStore('store').get(1); |
| + t.step_func(operateOnTx(tx2, put_records)); |
| + |
| + tx1.oncomplete = t.step_func(function() { |
| + countCallbacks(callback_count[5], 0); |
| + }); |
| + tx1.onerror = t.unreached_func('transaction should not fail'); |
| + tx2.oncomplete = t.step_func(function(){ |
| + countCallbacks(callback_count[5], 1); |
| + var tx3 = db.transaction('store2', 'readwrite'); |
| + t.step_func(operateOnTx(tx3, records5_2)); |
| + |
| + tx3.oncomplete = t.step_func(function(){ |
| + countCallbacks(callback_count[5], 2); |
| + t.done(); |
| + }); |
| + tx3.onerror = t.unreached_func('transaction should not fail'); |
| + }); |
| + tx2.onerror = t.unreached_func('transaction should not fail'); |
| + |
| + }); |
| + }, 'Observer : Record multiple transactions'); |
| + |
| + done(); |
| +})(); |