diff --git a/IndexedDB/get-databases.any.js b/IndexedDB/get-databases.any.js index 99b7ae7d4681f0..f054e0fec2f054 100644 --- a/IndexedDB/get-databases.any.js +++ b/IndexedDB/get-databases.any.js @@ -1,50 +1,62 @@ -// META: script=support.js - -async_test( async function(t) { - let made_database_check = t.step_func(async function() { - let idb_databases_promise = await indexedDB.databases(); - assert_true( - idb_databases_promise.some( - e => e.name == "TestDatabase" && e.version == 1), - "Call to databases() did not find database."); - t.done(); - }); - delete_then_open(t, "TestDatabase", ()=>{}, made_database_check); -}, "Report one database test."); - -async_test( function(t) { - let done_making_databases_callback = t.step_func(async function() { - let idb_databases_promise = await indexedDB.databases(); - assert_true( - idb_databases_promise.some( - e => e.name == "TestDatabase1" && e.version == 1), - "Call to databases() did not find database."); - assert_true( - idb_databases_promise.some( - e => e.name == "TestDatabase2" && e.version == 1), - "Call to databases() did not find database."); - assert_true( - idb_databases_promise.some( - e => e.name == "TestDatabase3" && e.version == 1), - "Call to databases() did not find database."); - t.done(); - }); - let make_databases_barrier = create_barrier(done_making_databases_callback); - delete_then_open(t, "TestDatabase1", ()=>{}, make_databases_barrier(t)); - delete_then_open(t, "TestDatabase2", ()=>{}, make_databases_barrier(t)); - delete_then_open(t, "TestDatabase3", ()=>{}, make_databases_barrier(t)); -}, "Report multiple databases test."); - -async_test( function(t) { - let delete_request = indexedDB.deleteDatabase("NonExistentDatabase"); - delete_request.onsuccess = t.step_func(async function() { - let idb_databases_promise = await indexedDB.databases(); - assert_false( - idb_databases_promise.some( - e => e.name == "NonExistentDatabase"), - "Call to databases() found excluded database."); - t.done(); - }); -}, "Don't report nonexistant databases test."); +// META: script=support-promises.js + +promise_test(async testCase => { + // Delete any databases that may not have been cleaned up after + // previous test runs. + await deleteAllDatabases(testCase); + + const db_name = "TestDatabase"; + const db = await createNamedDatabase(testCase, db_name, ()=>{}); + const databases_promise = await indexedDB.databases(); + const expected_result = [ + {"name": db_name, "version": 1}, + ]; + assert_object_equals( + databases_promise, + expected_result, + "Call to databases() did not retrieve the single expected result."); +}, "Enumerate one database."); + +promise_test(async testCase => { + // Delete any databases that may not have been cleaned up after previous test + // runs. + await deleteAllDatabases(testCase); + + const db_name1 = "TestDatabase1"; + const db_name2 = "TestDatabase2"; + const db_name3 = "TestDatabase3"; + const db1 = await createNamedDatabase(testCase, db_name1, ()=>{}); + const db2 = await createNamedDatabase(testCase, db_name2, ()=>{}); + const db3 = await createNamedDatabase(testCase, db_name3, ()=>{}); + const databases_promise = await indexedDB.databases(); + const expected_result = [ + {"name": db_name1, "version": 1}, + {"name": db_name2, "version": 1}, + {"name": db_name3, "version": 1}, + ]; + assert_object_equals( + databases_promise, + expected_result, + "Call to databases() did not retrieve the multiple expected results"); +}, "Enumerate multiple databases."); + +promise_test(async testCase => { + // Add some databases and close their connections. + const db1 = await createNamedDatabase(testCase, "DB1", ()=>{}); + const db2 = await createNamedDatabase(testCase, "DB2", ()=>{}); + db1.onversionchange = () => { db1.close() }; + db2.onversionchange = () => { db2.close() }; + + // Delete any databases that may not have been cleaned up after previous test + // runs as well as the two databases made above. + await deleteAllDatabases(testCase); + + // Make sure the databases are no longer returned. + const databases_promise = await indexedDB.databases(); + assert_object_equals( + databases_promise, + [], + "Call to databases() found database it should not have.") +}, "Make sure an empty list is returned for the case of no databases."); done(); diff --git a/IndexedDB/support-promises.js b/IndexedDB/support-promises.js index afebec56e8d9bf..433af5092701d6 100644 --- a/IndexedDB/support-promises.js +++ b/IndexedDB/support-promises.js @@ -303,3 +303,12 @@ function largeValue(size, seed) { return buffer; } + +async function deleteAllDatabases(testCase) { + const dbs_to_delete = await indexedDB.databases(); + for( const db_info of dbs_to_delete) { + let request = indexedDB.deleteDatabase(db_info.name); + let eventWatcher = requestWatcher(testCase, request); + await eventWatcher.wait_for('success'); + } +} diff --git a/IndexedDB/support.js b/IndexedDB/support.js index 9b3b92218b16bc..5edbdacbcc7d0c 100644 --- a/IndexedDB/support.js +++ b/IndexedDB/support.js @@ -192,49 +192,3 @@ function keep_alive(tx, store_name) { pin = false; }; } - -/** - * Ensures that indexeddb database specified by db_name is deleted, and then - * opens a connection to that database. - * - * @param t: the testing script state - * @param db_name: name of database to delete then create - * @param upgrade_func: function to be called if db_name needs upgrading - * @param body_func: function to be called upon successful deletion - * and creation of db_name - */ -function delete_then_open(t, db_name, upgrade_func, body_func) { - var delete_request = indexedDB.deleteDatabase(db_name); - delete_request.onerror = t.unreached_func('deleteDatabase should not fail'); - delete_request.onsuccess = t.step_func(function(e) { - var open_request = indexedDB.open(db_name); - open_request.onupgradeneeded = t.step_func(function() { - upgrade_func(t, open_request.result, open_request); - }); - open_request.onsuccess = t.step_func(function() { - body_func(t, open_request.result); - }); - }); -} - -/** - * Creates a barrier that one calls - * - * @param callback: function to be called after barrier is passed - */ -function create_barrier(callback) { - var count = 0; - var called = false; - return function(t) { - assert_false(called, "Barrier already used."); - ++count; - return t.step_func(function() { - --count; - if (count === 0) { - assert_false(called, "Barrier already used."); - called = true; - callback(); - } - }); - } -}