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

Side by Side 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 test coverage for the (slightly incorrect) behavior in strict mode when our flag is not enabl… 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Returns an IndexedDB database name likely to be unique to the test case.
2 const databaseName = (testCase) => {
3 return 'db' + self.location.pathname + '-' + testCase.name;
4 };
5
6 // Creates an EventWatcher covering all the events that can be issued by
7 // IndexedDB requests and transactions.
8 const requestWatcher = (testCase, request) => {
9 return new EventWatcher(testCase, request,
10 ['error', 'success', 'upgradeneeded']);
11 };
12
13 // Migrates an IndexedDB database whose name is unique for the test case.
14 //
15 // newVersion must be greater than the database's current version.
16 //
17 // migrationCallback will be called during a versionchange transaction and will
18 // be given the created database and the versionchange transaction.
19 //
20 // Returns a promise. If the versionchange transaction goes through, the promise
21 // resolves to an IndexedDB database that must be closed by the caller. If the
22 // versionchange transaction is aborted, the promise resolves to an error.
23 const migrateDatabase = (testCase, newVersion, migrationCallback) => {
24 // We cannot use eventWatcher.wait_for('upgradeneeded') here, because
25 // the versionchange transaction auto-commits before the Promise's then
26 // callback gets called.
27 return new Promise((resolve, reject) => {
28 const request = indexedDB.open(databaseName(testCase), newVersion);
29 request.onupgradeneeded = testCase.step_func(event => {
30 const database = event.target.result;
31 const transaction = event.target.transaction;
32 let abortCalled = false;
33
34 // We wrap IDBTransaction.abort so we can set up the correct event
35 // listeners and expectations if the test chooses to abort the
36 // versionchange transaction.
37 const transactionAbort = transaction.abort.bind(transaction);
38 transaction.abort = () => {
39 request.onerror = event => {
40 event.preventDefault();
41 resolve(event);
42 };
43 request.onsuccess = () => reject(new Error(
44 'indexedDB.open should not succeed after the ' +
45 'versionchange transaction is aborted'));
46 transactionAbort();
47 abortCalled = true;
48 }
49
50 migrationCallback(database, transaction);
51 if (!abortCalled) {
52 request.onsuccess = null;
53 resolve(requestWatcher(testCase, request).wait_for('success'));
54 }
55 });
56 request.onerror = event => reject(event.target.error);
57 request.onsuccess = () => reject(new Error(
58 'indexedDB.open should not succeed without creating a ' +
59 'versionchange transaction'));
60 }).then(event => event.target.result || event.target.error);
61 };
62
63 // Creates an IndexedDB database whose name is unique for the test case.
64 //
65 // setupCallback will be called during a versionchange transaction, and will be
66 // given the created database and the versionchange transaction.
67 //
68 // Returns a promise that resolves to an IndexedDB database. The caller must
69 // close the database.
70 const createDatabase = (testCase, setupCallback) => {
71 const request = indexedDB.deleteDatabase(databaseName(testCase));
72 const eventWatcher = requestWatcher(testCase, request);
73
74 return eventWatcher.wait_for('success').then(event =>
75 migrateDatabase(testCase, 1, setupCallback));
76 };
77
78 // The data in the 'books' object store records in the first example of the
79 // IndexedDB specification.
80 const BOOKS_RECORD_DATA = [
81 { title: 'Quarry Memories', author: 'Fred', isbn: 123456 },
82 { title: 'Water Buffaloes', author: 'Fred', isbn: 234567 },
83 { title: 'Bedrock Nights', author: 'Barney', isbn: 345678 },
84 ];
85
86 // Creates a 'books' object store whose contents closely resembles the first
87 // example in the IndexedDB specification.
88 const createBooksStore = (testCase, database) => {
89 const store = database.createObjectStore('books',
90 { keyPath: 'isbn', autoIncrement: true });
91 store.createIndex('by_author', 'author');
92 store.createIndex('by_title', 'title', { unique: true });
93 for (let record of BOOKS_RECORD_DATA)
94 store.put(record);
95 return store;
96 };
97
98 // Creates a 'not_books' object store used to test renaming into existing or
99 // deleted store names.
100 const createNotBooksStore = (testCase, database) => {
101 const store = database.createObjectStore('not_books');
102 store.createIndex('not_by_author', 'author');
103 store.createIndex('not_by_title', 'title', { unique: true });
104 return store;
105 };
106
107 // Verifies that an object store's indexes match the indexes used to create the
108 // books store in the test database's version 1.
109 //
110 // The errorMessage is used if the assertions fail. It can state that the
111 // IndexedDB implementation being tested is incorrect, or that the testing code
112 // is using it incorrectly.
113 const checkStoreIndexes = (testCase, store, errorMessage) => {
114 assert_array_equals(
115 store.indexNames, ['by_author', 'by_title'], errorMessage);
116 const authorIndex = store.index('by_author');
117 const titleIndex = store.index('by_title');
118 return Promise.all([
119 checkAuthorIndexContents(testCase, authorIndex, errorMessage),
120 checkTitleIndexContents(testCase, titleIndex, errorMessage),
121 ]);
122 };
123
124 // Verifies that an object store's key generator is in the same state as the
125 // key generator created for the books store in the test database's version 1.
126 //
127 // The errorMessage is used if the assertions fail. It can state that the
128 // IndexedDB implementation being tested is incorrect, or that the testing code
129 // is using it incorrectly.
130 const checkStoreGenerator = (testCase, store, expectedKey, errorMessage) => {
131 const request = store.put(
132 { title: 'Bedrock Nights ' + expectedKey, author: 'Barney' });
133 const eventWatcher = requestWatcher(testCase, request);
134 return eventWatcher.wait_for('success').then(() => {
135 const result = request.result;
136 assert_equals(result, expectedKey, errorMessage);
137 });
138 };
139
140 // Verifies that an object store's contents matches the contents used to create
141 // the books store in the test database's version 1.
142 //
143 // The errorMessage is used if the assertions fail. It can state that the
144 // IndexedDB implementation being tested is incorrect, or that the testing code
145 // is using it incorrectly.
146 const checkStoreContents = (testCase, store, errorMessage) => {
147 const request = store.get(123456);
148 const eventWatcher = requestWatcher(testCase, request);
149 return eventWatcher.wait_for('success').then(() => {
150 const result = request.result;
151 assert_equals(result.isbn, BOOKS_RECORD_DATA[0].isbn, errorMessage);
152 assert_equals(result.author, BOOKS_RECORD_DATA[0].author, errorMessage);
153 assert_equals(result.title, BOOKS_RECORD_DATA[0].title, errorMessage);
154 });
155 };
156
157 // Verifies that index matches the 'by_author' index used to create the
158 // by_author books store in the test database's version 1.
159 //
160 // The errorMessage is used if the assertions fail. It can state that the
161 // IndexedDB implementation being tested is incorrect, or that the testing code
162 // is using it incorrectly.
163 const checkAuthorIndexContents = (testCase, index, errorMessage) => {
164 const request = index.get(BOOKS_RECORD_DATA[2].author);
165 const eventWatcher = requestWatcher(testCase, request);
166 return eventWatcher.wait_for('success').then(() => {
167 const result = request.result;
168 assert_equals(result.isbn, BOOKS_RECORD_DATA[2].isbn, errorMessage);
169 assert_equals(result.title, BOOKS_RECORD_DATA[2].title, errorMessage);
170 });
171 };
172
173 // Verifies that an index matches the 'by_title' index used to create the books
174 // store in the test database's version 1.
175 //
176 // The errorMessage is used if the assertions fail. It can state that the
177 // IndexedDB implementation being tested is incorrect, or that the testing code
178 // is using it incorrectly.
179 const checkTitleIndexContents = (testCase, index, errorMessage) => {
180 const request = index.get(BOOKS_RECORD_DATA[2].title);
181 const eventWatcher = requestWatcher(testCase, request);
182 return eventWatcher.wait_for('success').then(() => {
183 const result = request.result;
184 assert_equals(result.isbn, BOOKS_RECORD_DATA[2].isbn, errorMessage);
185 assert_equals(result.author, BOOKS_RECORD_DATA[2].author, errorMessage);
186 });
187 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698