Index: third_party/WebKit/LayoutTests/storage/indexeddb/resources/observer-removal.js |
diff --git a/third_party/WebKit/LayoutTests/storage/indexeddb/resources/observer-removal.js b/third_party/WebKit/LayoutTests/storage/indexeddb/resources/observer-removal.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c7178ba3ec4ff4a707263a3ef667f1b3545eaf18 |
--- /dev/null |
+++ b/third_party/WebKit/LayoutTests/storage/indexeddb/resources/observer-removal.js |
@@ -0,0 +1,150 @@ |
+if (this.importScripts) { |
+ importScripts('../../../resources/testharness.js'); |
+ importScripts('../../../resources/generic-idb-operations.js'); |
+} |
+ |
+async_test(function(t) { |
+ var dbname = location.pathname + ' - ' + 'unobserve before tx completes'; |
+ var openRequest = indexedDB.open(dbname); |
+ var obs = new IDBObserver(t.unreached_func('Observer callback should not be fired'), { operationTypes: ['put'] }); |
+ |
+ openRequest.onupgradeneeded = t.step_func(function() { |
+ createObjectStores(openRequest.result, ['store']); |
+ }); |
+ openRequest.onsuccess = t.step_func(function() { |
+ var db = openRequest.result; |
+ var tx1 = db.transaction('store'); |
+ var tx2 = db.transaction('store', 'readwrite'); |
+ tx1.objectStore('store').get(1); |
+ tx2.objectStore('store').put(1, 1); |
+ obs.observe(db, tx1); |
+ |
+ tx1.onerror = t.unreached_func('transaction should not fail'); |
+ tx2.oncomplete = t.step_func(function() { |
+ t.done(); |
+ }); |
+ tx2.onerror = t.unreached_func('transaction should not fail') |
+ obs.unobserve(db); |
+ }); |
+ openRequest.onerror = t.unreached_func('opening database should not fail'); |
+}, 'Unobserve before associated transaction completes'); |
+ |
+async_test(function(t) { |
+ var dbname = location.pathname + ' - ' + 'unobserve after tx completes'; |
+ var openRequest = indexedDB.open(dbname); |
+ var obs = new IDBObserver(t.unreached_func('Observer callback should not be fired'), { operationTypes: ['put'] }); |
+ |
+ openRequest.onupgradeneeded = t.step_func(function() { |
+ createObjectStores(openRequest.result, ['store']); |
+ }); |
+ openRequest.onsuccess = t.step_func(function() { |
+ var db = openRequest.result; |
+ var tx1 = db.transaction('store'); |
+ var tx2 = db.transaction('store', 'readwrite'); |
+ tx1.objectStore('store').get(1); |
+ tx2.objectStore('store').put(1, 1); |
+ obs.observe(db, tx1); |
+ |
+ tx1.oncomplete = t.step_func(function() { |
+ obs.unobserve(db); |
+ }); |
+ tx1.onerror = t.unreached_func('transaction should not fail'); |
+ tx2.oncomplete = t.step_func(function() { |
+ t.done(); |
+ }); |
+ tx2.onerror = t.unreached_func('transaction should not fail'); |
+ }); |
+ openRequest.onerror = t.unreached_func('opening database should not fail'); |
+}, 'Unobserve after associated transaction completes'); |
+ |
+async_test(function(t) { |
+ var dbname = location.pathname + ' - ' + 'unobserve multiple observers'; |
+ var openRequest = indexedDB.open(dbname); |
+ var obs = new IDBObserver(t.unreached_func('Observer callback should not be fired'), { operationTypes: ['put'] }); |
+ openRequest.onupgradeneeded = t.step_func(function() { |
+ createObjectStores(openRequest.result, ['store']); |
+ }); |
+ openRequest.onsuccess = t.step_func(function() { |
+ var db = openRequest.result; |
+ var tx1 = db.transaction('store'); |
+ var tx2 = db.transaction('store', 'readwrite'); |
+ |
+ tx2.objectStore('store').put(1, 1); |
+ obs.observe(db, tx1); |
+ obs.observe(db, tx1); // Multiple observe calls registered. |
+ obs.observe(db, tx2); // Multiple observe calls registered. |
+ obs.unobserve(db); |
+ |
+ tx1.onerror = t.unreached_func('transaction should not fail'); |
+ tx2.onerror = t.unreached_func('transaction should not fail'); |
+ tx2.oncomplete = t.step_func(function() { |
+ t.done(); |
+ }) |
+ }); |
+ openRequest.onerror = t.unreached_func('opening database should not fail'); |
+}, 'Unobserve multiple observe calls on same database'); |
+ |
+async_test(function(t) { |
+ var dbname = location.pathname + ' - ' + 'unobserve non existant observers'; |
+ var openRequest = indexedDB.open(dbname); |
+ var obs = new IDBObserver(t.unreached_func('Observer callback should not be fired'), { operationTypes: ['put'] }); |
+ openRequest.onupgradeneeded = t.step_func(function() { |
+ createObjectStores(openRequest.result, ['store']); |
+ }); |
+ openRequest.onsuccess = t.step_func(function() { |
+ var db = openRequest.result; |
+ obs.unobserve(db); |
+ t.done(); |
+ }); |
+ openRequest.onerror = t.unreached_func('opening database should not fail'); |
+}, 'Unobserve non existant observer'); |
+ |
+async_test(function(t) { |
+ var dbname = location.pathname + ' - ' + 'connection close'; |
+ var cnt1 = 0, cnt2 = 0; |
+ var openRequest1 = indexedDB.open(dbname); |
+ var obs1 = new IDBObserver(t.step_func(function() { cnt1++; }), { operationTypes: ['put'] }); |
+ var obs2 = new IDBObserver(t.step_func(function() { cnt2++; }), { operationTypes: ['put'] }); |
+ |
+ openRequest1.onupgradeneeded = t.step_func(function() { |
+ createObjectStores(openRequest1.result, ['store']); |
+ }); |
+ openRequest1.onsuccess = t.step_func(function() { |
+ var db1 = openRequest1.result; |
+ var tx1 = db1.transaction('store', 'readwrite'); |
+ var tx2 = db1.transaction('store', 'readwrite'); |
+ tx1.objectStore('store').get(1); |
+ tx2.objectStore('store').put(1, 1); |
+ obs1.observe(db1, tx1); |
+ obs2.observe(db1, tx2); |
+ |
+ tx1.oncomplete = t.step_func(function() { |
+ countCallbacks(cnt1, 0); |
+ countCallbacks(cnt2, 0); |
+ }) |
+ tx2.oncomplete = t.step_func(function() { |
+ countCallbacks(cnt1, 1); |
+ countCallbacks(cnt2, 0); |
+ |
+ db1.close(); |
+ var openRequest2 = indexedDB.open(dbname); |
+ openRequest2.onsuccess = t.step_func(function() { |
+ var db2 = openRequest2.result; |
+ var tx3 = db2.transaction('store', 'readwrite'); |
+ tx3.objectStore('store').put(1, 1); |
+ tx3.oncomplete = t.step_func(function() { |
+ countCallbacks(cnt1, 1); |
+ countCallbacks(cnt2, 0); |
+ t.done(); |
+ }); |
+ tx3.onerror = t.unreached_func('transaction should not fail'); |
+ }); |
+ openRequest2.onerror = t.unreached_func('opening database should not fail'); |
+ }); |
+ tx1.onerror = t.unreached_func('transaction should not fail'); |
+ tx2.onerror = t.unreached_func('transaction should not fail'); |
+ }); |
+ openRequest1.onerror = t.unreached_func('opening database should not fail'); |
+}, 'Remove observers on database close'); |
+ |
+done(); |