| 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');
|
| + };
|
| + 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);
|
| + }
|
| +}
|
| +
|
| +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(
|
| + '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
|
| +// with ExecuteJavascript from browser tests, but does not do |returnToTest|.
|
| +
|
| +function saveCertificate(certificate, onsuccess, onerror) {
|
| + 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);
|
| + 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);
|
| +}
|
|
|