get-databases.any.js (5418B)
1 // META: script=resources/support-promises.js 2 'use strict'; 3 4 promise_test(async testCase => { 5 let result = indexedDB.databases(); 6 assert_true(result instanceof Promise, 7 "databases() should return a promise."); 8 result.catch(() => {}); 9 }, "Ensure that databases() returns a promise."); 10 11 promise_test(async testCase => { 12 // Delete any databases that may not have been cleaned up after previous test 13 // runs. 14 await deleteAllDatabases(testCase); 15 16 const db_name = "TestDatabase"; 17 const db = await createNamedDatabase(testCase, db_name, ()=>{}); 18 const databases_result = await indexedDB.databases(); 19 db.close(); 20 const expected_result = {"name": db_name, "version": 1}; 21 assert_equals( 22 databases_result.length, 23 1, 24 "The result of databases() should contain one result per database."); 25 assert_true( 26 databases_result[0].name === expected_result.name 27 && databases_result[0].version === expected_result.version, 28 "The result of databases() should be a sequence of the correct names " 29 + "and versions of all databases for the origin."); 30 }, "Enumerate one database."); 31 32 promise_test(async testCase => { 33 // Delete any databases that may not have been cleaned up after previous test 34 // runs. 35 await deleteAllDatabases(testCase); 36 37 const db_name1 = "TestDatabase1"; 38 const db_name2 = "TestDatabase2"; 39 const db_name3 = "TestDatabase3"; 40 const db1 = await createNamedDatabase(testCase, db_name1, ()=>{}); 41 const db2 = await createNamedDatabase(testCase, db_name2, ()=>{}); 42 const db3 = await createNamedDatabase(testCase, db_name3, ()=>{}); 43 db1.close(); 44 db2.close(); 45 db3.close(); 46 const version_promise = 47 await migrateNamedDatabase(testCase, db_name2, 2, () => {}); 48 const databases_result = await indexedDB.databases(); 49 const expected_result = [ 50 {"name": db_name1, "version": 1}, 51 {"name": db_name2, "version": 2}, 52 {"name": db_name3, "version": 1}, 53 ]; 54 assert_equals( 55 databases_result.length, 56 expected_result.length, 57 "The result of databases() should contain one result per database."); 58 for ( let i = 0; i < expected_result.length; i += 1 ) { 59 const result = expected_result[i]; 60 assert_true( 61 databases_result.some( 62 e => e.name === result.name && e.version === result.version), 63 "The result of databases() should be a sequence of the correct names " 64 + "and versions of all databases for the origin."); 65 } 66 }, "Enumerate multiple databases."); 67 68 promise_test(async testCase => { 69 // Add some databases and close their connections. 70 const db1 = await createNamedDatabase(testCase, "DB1", () => {}); 71 const db2 = await createNamedDatabase(testCase, "DB2", () => {}); 72 db1.close(); 73 db2.close(); 74 75 // Delete any databases that may not have been cleaned up after previous test 76 // runs as well as the two databases made above. 77 await deleteAllDatabases(testCase); 78 79 // Make sure the databases are no longer returned. 80 const databases_result = await indexedDB.databases(); 81 assert_equals( 82 databases_result.length, 83 0, 84 "The result of databases() should be an empty sequence for the case of " 85 + "no databases for the origin."); 86 }, "Make sure an empty list is returned for the case of no databases."); 87 88 promise_test(async testCase => { 89 function sleep_sync(msec) { 90 const start = new Date().getTime(); 91 while (new Date().getTime() - start < msec) {} 92 } 93 94 // Delete any databases that may not have been cleaned up after previous test 95 // runs as well as the two databases made above. 96 await deleteAllDatabases(testCase); 97 98 const db1 = await createNamedDatabase(testCase, "DB1", ()=>{}); 99 let databases_promise1; 100 const db2 = await createNamedDatabase(testCase, "DB2", async () => { 101 databases_promise1 = indexedDB.databases(); 102 103 // Give databases() operation a chance to fetch all current info about 104 // existing databases. This must be a sync sleep since await would trigger 105 // auto commit of the upgrade transaction. 106 sleep_sync(1000); 107 }); 108 const databases_result1 = await databases_promise1; 109 assert_equals( 110 databases_result1.length, 111 1, 112 "The result of databases() should be only those databases which have " 113 + "been created at the time of calling, regardless of versionchange " 114 + "transactions currently running."); 115 db1.close(); 116 db2.close(); 117 const databases_result2 = await indexedDB.databases(); 118 assert_equals( 119 databases_result2.length, 120 2, 121 "The result of databases() should include all databases which have " 122 + "been created at the time of calling."); 123 let databases_promise3; 124 await migrateNamedDatabase(testCase, "DB2", 2, async () => { 125 databases_promise3 = indexedDB.databases(); 126 127 // Give databases() operation a chance to fetch all current info about 128 // existing databases. This must be a sync sleep since await would trigger 129 // auto commit of the upgrade transaction. 130 sleep_sync(1000); 131 }); 132 const databases_result3 = await databases_promise3; 133 assert_true( 134 databases_result3[0].version === 1 135 && databases_result3[1].version === 1, 136 "The result of databases() should contain the versions of databases " 137 + "at the time of calling, regardless of versionchange transactions " 138 + "currently running."); 139 }, "Ensure that databases() doesn't pick up changes that haven't commited.");