Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Unified Diff: third_party/WebKit/LayoutTests/storage/indexeddb/resources/rename-common.js

Issue 2276593002: Support renaming of IndexedDB indexes and object stores. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added tests for create rename in the same aborted transaction. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/storage/indexeddb/resources/rename-common.js
diff --git a/third_party/WebKit/LayoutTests/storage/indexeddb/resources/rename-common.js b/third_party/WebKit/LayoutTests/storage/indexeddb/resources/rename-common.js
new file mode 100644
index 0000000000000000000000000000000000000000..a2ee3a8a1ce39ed9b134a68488a438624751c7f8
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/storage/indexeddb/resources/rename-common.js
@@ -0,0 +1,162 @@
+
+// Returns an IndexedDB database name likely to be unique to the test case.
+const databaseName = function(testCase) {
+ return 'db' + self.location.pathname + '-' + testCase.name;
+};
+
+// Creates an EventWatcher covering all the events that can be issued by
+// IndexedDB requests and transactions.
+const requestWatcher = function(testCase, request) {
+ return new EventWatcher(testCase, request,
+ ['error', 'success', 'upgradeneeded']);
+};
+
+// Migrates an IndexedDB database whose name is unique for the test case.
+//
+// setupCallback will be called during a versionchange transaction, and will be
+// given the created database and the versionchange transaction.
+//
+// Returns a promise that resolves to an IndexedDB database. The caller must
+// close the database.
+const migrateDatabase = function(testCase, newVersion, setupCallback) {
+ // We cannot use eventWatcher.wait_for('upgradeneeded') here, because
+ // the versionchange transaction auto-commits before the Promise's then
+ // callback gets called.
+ return new Promise((resolve, reject) => {
+ const request = indexedDB.open(databaseName(testCase), newVersion);
+ request.onupgradeneeded = event => {
+ const eventWatcher = requestWatcher(testCase, request);
+ const database = event.target.result;
+ const transaction = event.target.transaction;
+ setupCallback(database, transaction);
+ resolve(eventWatcher.wait_for('success'));
+ };
+ request.onerror = event => reject(event.target.error);
+ }).then(event => event.target.result);
+};
+
+// Creates an IndexedDB database whose name is unique for the test case.
+//
+// setupCallback will be called during a versionchange transaction, and will be
+// given the created database and the versionchange transaction.
+//
+// Returns a promise that resolves to an IndexedDB database. The caller must
+// close the database.
+const createDatabase = function(testCase, setupCallback) {
+ const request = indexedDB.deleteDatabase(databaseName(testCase));
+ const eventWatcher = requestWatcher(testCase, request);
+
+ return eventWatcher.wait_for('success').then(event =>
+ migrateDatabase(testCase, 1, setupCallback));
+};
+
+// The data in the 'books' object store records in the first example of the
+// IndexedDB specification.
+const BOOKS_RECORD_DATA = [
+ { title: 'Quarry Memories', author: 'Fred', isbn: 123456 },
+ { title: 'Water Buffaloes', author: 'Fred', isbn: 234567 },
+ { title: 'Bedrock Nights', author: 'Barney', isbn: 345678 },
+];
+
+// Creates a 'books' object store whose contents closely resembles the first
+// example in the IndexedDB specification.
+const createBooksStore = function(testCase, database) {
+ const store = database.createObjectStore('books',
+ { keyPath: 'isbn', autoIncrement: true });
+ store.createIndex('by_author', 'author');
+ store.createIndex('by_title', 'title', { unique: true });
+ for (let record of BOOKS_RECORD_DATA)
+ store.put(record);
+ return store;
+};
+
+// Creates a 'not_books' object store used to test renaming into existing or
+// deleted store names.
+const createNotBooksStore = function(testCase, database) {
+ return database.createObjectStore('not_books');
+};
+
+// Verifies that an object store's indexes match the indexes used to create the
+// books store in the test database's version 1.
+//
+// The errorMessage is used if the assertions fail. It can state that the
+// IndexedDB implementation being tested is incorrect, or that the testing code
+// is using it incorrectly.
+const checkStoreIndexes = function(testCase, store, errorMessage) {
+ assert_array_equals(
+ store.indexNames, ['by_author', 'by_title'], errorMessage);
+ const authorIndex = store.index('by_author');
+ const titleIndex = store.index('by_title');
+ return Promise.all([
+ checkAuthorIndexContents(testCase, authorIndex, errorMessage),
+ checkTitleIndexContents(testCase, titleIndex, errorMessage),
+ ]);
+};
+
+// Verifies that an object store's key generator is in the same state as the
+// key generator created for the books store in the test database's version 1.
+//
+// The errorMessage is used if the assertions fail. It can state that the
+// IndexedDB implementation being tested is incorrect, or that the testing code
+// is using it incorrectly.
+const checkStoreGenerator = function(testCase,
+ store,
+ expectedKey,
+ errorMessage) {
+ const request = store.put(
+ { title: 'Bedrock Nights ' + expectedKey, author: 'Barney' });
+ const eventWatcher = requestWatcher(testCase, request);
+ return eventWatcher.wait_for('success').then(() => {
+ const result = request.result;
+ assert_equals(result, expectedKey, errorMessage);
+ });
+};
+
+// Verifies that an object store's contents matches the contents used to create
+// the books store in the test database's version 1.
+//
+// The errorMessage is used if the assertions fail. It can state that the
+// IndexedDB implementation being tested is incorrect, or that the testing code
+// is using it incorrectly.
+const checkStoreContents = function(testCase, store, errorMessage) {
+ const request = store.get(123456);
+ const eventWatcher = requestWatcher(testCase, request);
+ return eventWatcher.wait_for('success').then(() => {
+ const result = request.result;
+ assert_equals(result.isbn, BOOKS_RECORD_DATA[0].isbn, errorMessage);
+ assert_equals(result.author, BOOKS_RECORD_DATA[0].author, errorMessage);
+ assert_equals(result.title, BOOKS_RECORD_DATA[0].title, errorMessage);
+ });
+};
+
+// Verifies that index matches the 'by_author' index used to create the
+// by_author books store in the test database's version 1.
+//
+// The errorMessage is used if the assertions fail. It can state that the
+// IndexedDB implementation being tested is incorrect, or that the testing code
+// is using it incorrectly.
+const checkAuthorIndexContents = function(testCase, index, errorMessage) {
+ const request = index.get(BOOKS_RECORD_DATA[2].author);
+ const eventWatcher = requestWatcher(testCase, request);
+ return eventWatcher.wait_for('success').then(() => {
+ const result = request.result;
+ assert_equals(result.isbn, BOOKS_RECORD_DATA[2].isbn, errorMessage);
+ assert_equals(result.title, BOOKS_RECORD_DATA[2].title, errorMessage);
+ });
+};
+
+// Verifies that an index matches the 'by_title' index used to create the books
+// store in the test database's version 1.
+//
+// The errorMessage is used if the assertions fail. It can state that the
+// IndexedDB implementation being tested is incorrect, or that the testing code
+// is using it incorrectly.
+const checkTitleIndexContents = function(testCase, index, errorMessage) {
+ const request = index.get(BOOKS_RECORD_DATA[2].title);
+ const eventWatcher = requestWatcher(testCase, request);
+ return eventWatcher.wait_for('success').then(() => {
+ const result = request.result;
+ assert_equals(result.isbn, BOOKS_RECORD_DATA[2].isbn, errorMessage);
+ assert_equals(result.author, BOOKS_RECORD_DATA[2].author, errorMessage);
+ });
+};

Powered by Google App Engine
This is Rietveld 408576698