Chromium Code Reviews| Index: chrome/test/data/webrtc/indexeddb.js |
| diff --git a/chrome/test/data/webrtc/indexeddb.js b/chrome/test/data/webrtc/indexeddb.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..31f5125d68ada96abcd9b54b6923caca4a7e3a9a |
| --- /dev/null |
| +++ b/chrome/test/data/webrtc/indexeddb.js |
| @@ -0,0 +1,157 @@ |
| +/** |
| + * Copyright 2016 The Chromium Authors. All rights reserved. |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +/** @private */ |
| +var kDatabaseName = 'WebRTC-Database'; |
| + |
| +/** |
| + * The one and only |IDBDatabase| in this page. |
| + * @private |
| + */ |
| +var gDatabase = null; |
| + |
| +/** |
| + * Set by |generateAndCloneCertificate|. |
| + */ |
| +var gCertificate = null; |
| +var gCertificateClone = null; |
| + |
| +// Public interface to tests. These are expected to be called with |
| +// ExecuteJavascript invocations from the browser tests and will return answers |
| +// through the DOM automation controller. |
| + |
| +function openDatabase() { |
| + if (gDatabase !== null) |
| + throw failTest('The database is already open.'); |
| + var reqOpen = indexedDB.open(kDatabaseName); |
| + reqOpen.onupgradeneeded = function() { |
| + // This happens before |onsuccess| if the database is new or its version is |
| + // updated. Create object stores. |
| + var db = reqOpen.result; |
| + var certStore = db.createObjectStore('certificates', { keyPath: 'id' }); |
| + certStore.createIndex('by_id', 'id'); |
|
jsbell
2016/05/09 16:42:39
The store is already keyed by the id property - wh
hbos_chromium
2016/05/09 18:23:32
Ah, no need. Done.
|
| + }; |
| + reqOpen.onsuccess = function() { |
| + if (gDatabase !== null) |
| + throw failTest('The database is already open.'); |
| + gDatabase = reqOpen.result; |
| + returnToTest('ok-database-opened'); |
| + } |
| + reqOpen.onerror = function() { |
| + throw failTest('The database could not be opened. Error: ' + reqOpen.error); |
|
jsbell
2016/05/09 16:42:38
`throw` won't do anything here (exception thrown i
hbos_chromium
2016/05/09 18:23:32
Done.
|
| + } |
| +} |
| + |
| +function closeDatabase() { |
| + if (gDatabase === null) |
| + throw failTest('The database is already closed.'); |
| + gDatabase.close(); |
| + gDatabase = null; |
| + returnToTest('ok-database-closed'); |
| +} |
| + |
| +function deleteDatabase() { |
| + if (gDatabase !== null) |
| + throw failTest('The database should be closed before deleting.'); |
| + var reqDelete = indexedDB.deleteDatabase(kDatabaseName); |
| + reqDelete.onsuccess = function () { |
| + returnToTest('ok-database-deleted'); |
| + }; |
| + reqDelete.onerror = function () { |
| + throw failTest( |
|
jsbell
2016/05/09 16:42:39
Ditto
hbos_chromium
2016/05/09 18:23:32
Done.
|
| + 'The database could not be deleted. Error: ' + reqDelete.error); |
| + }; |
| +} |
| + |
| +/** |
| + * Generates a certificate and clones it by saving and loading it to the |
| + * database (requires database to be open, see |openDatabase|). After returning |
| + * successfully to the test, the global variables |gCertificate| and |
| + * |gCertificateClone| have been set. |
| + * @param {!Object} keygenAlgorithm An |AlgorithmIdentifier| to be used as |
| + * parameter to |RTCPeerConnection.generateCertificate|. The resulting |
| + * certificate will be used by the peer connection. |
| + */ |
| +function generateAndCloneCertificate(keygenAlgorithm) { |
| + RTCPeerConnection.generateCertificate(keygenAlgorithm).then( |
| + function(certificate) { |
| + gCertificate = certificate; |
| + cloneCertificate(gCertificate, |
| + function(clone) { |
| + gCertificateClone = clone; |
| + // TODO(hbos): Verify that |gCertificate| is value-equal to |
| + // |gCertificateClone|. crbug.com/609108 |
| + returnToTest('ok-generated-and-cloned'); |
| + }, |
| + function() { |
| + throw failTest('Error cloning certificate.'); |
| + }); |
| + }, |
| + function() { |
| + failTest('Certificate generation failed. keygenAlgorithm: ' + |
| + JSON.stringify(keygenAlgorithm)); |
| + }); |
| +} |
| + |
| +// Public helper functions to test functions. Expected to indirectly be called |
|
phoglund_chromium
2016/05/09 14:59:57
You mean these are only called from the functions
hbos_chromium
2016/05/09 18:23:32
Done.
|
| +// with ExecuteJavascript from browser tests, but does not do |returnToTest|. |
| + |
| +function saveCertificate(certificate, onsuccess, onerror) { |
|
jsbell
2016/05/09 16:42:39
Code might be more readable returning a Promise ra
hbos_chromium
2016/05/09 18:23:32
Makes sense, changed to promises.
|
| + if (gDatabase === null) |
| + throw failTest('The database is not open.'); |
| + |
| + var certTrans = gDatabase.transaction('certificates', 'readwrite'); |
| + var certStore = certTrans.objectStore('certificates'); |
| + var certPut = certStore.put({ |
| + id:0, |
| + cert:certificate |
| + }); |
| + |
| + certPut.onsuccess = function() { |
| + onsuccess(); |
| + }; |
| + certPut.onerror = function() { |
| + onerror(certPut.error); |
| + }; |
| +} |
| + |
| +function loadCertificate(onsuccess, onerror) { |
| + if (gDatabase === null) |
| + throw failTest('The database is not open.'); |
| + |
| + var certTrans = gDatabase.transaction('certificates', 'readonly'); |
| + var certStore = certTrans.objectStore('certificates'); |
| + var certById = certStore.index('by_id'); |
| + |
| + var reqGet = certById.get(0); |
|
jsbell
2016/05/09 16:42:38
Could just use certStore.get(0) - no need for the
hbos_chromium
2016/05/09 18:23:32
Done.
|
| + reqGet.onsuccess = function() { |
| + var match = reqGet.result; |
| + if (match !== undefined) { |
| + onsuccess(match.cert); |
| + } else { |
| + onsuccess(null); |
| + } |
| + }; |
| + reqGet.onerror = function() { |
| + onerror(reqGet.error); |
| + }; |
| +} |
| + |
| +function cloneCertificate(certificate, onsuccess, onerror) { |
| + saveCertificate(certificate, |
| + function() { |
| + // Certificate saved successfully, now load it. |
| + loadCertificate( |
| + function(clone) { |
| + // Load successful. |
| + if (clone === null) |
| + throw failTest('loadCertificate returned a null certificate.'); |
| + onsuccess(clone); |
| + }, |
| + onerror); |
| + }, |
| + onerror); |
| +} |