michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: let xulApp = require("sdk/system/xul-app"); michael@0: if (xulApp.versionInRange(xulApp.platformVersion, "16.0a1", "*")) { michael@0: new function tests() { michael@0: michael@0: const { indexedDB, IDBKeyRange, DOMException michael@0: } = require("sdk/indexed-db"); michael@0: michael@0: exports["test indexedDB is frozen"] = function(assert){ michael@0: let original = indexedDB.open; michael@0: let f = function(){}; michael@0: assert.throws(function(){indexedDB.open = f}); michael@0: assert.equal(indexedDB.open,original); michael@0: assert.notEqual(indexedDB.open,f); michael@0: michael@0: }; michael@0: michael@0: exports["test db variables"] = function(assert) { michael@0: [ indexedDB, IDBKeyRange, DOMException michael@0: ].forEach(function(value) { michael@0: assert.notEqual(typeof(value), "undefined", "variable is defined"); michael@0: }); michael@0: } michael@0: michael@0: exports["test open"] = function(assert, done) { michael@0: let request = indexedDB.open("MyTestDatabase"); michael@0: request.onerror = function(event) { michael@0: assert.fail("Failed to open indexedDB") michael@0: done(); michael@0: }; michael@0: request.onsuccess = function(event) { michael@0: assert.pass("IndexedDB was open"); michael@0: done(); michael@0: }; michael@0: }; michael@0: michael@0: exports["test dbname is unprefixed"] = function(assert, done) { michael@0: // verify fixes in https://bugzilla.mozilla.org/show_bug.cgi?id=786688 michael@0: let dbName = "dbname-unprefixed"; michael@0: let request = indexedDB.open(dbName); michael@0: request.onerror = function(event) { michael@0: assert.fail("Failed to open db"); michael@0: done(); michael@0: }; michael@0: request.onsuccess = function(event) { michael@0: assert.equal(request.result.name, dbName); michael@0: done(); michael@0: }; michael@0: }; michael@0: michael@0: exports["test structuring the database"] = function(assert, done) { michael@0: // This is what our customer data looks like. michael@0: let customerData = [ michael@0: { ssn: "444-44-4444", name: "Bill", age: 35, email: "bill@company.com" }, michael@0: { ssn: "555-55-5555", name: "Donna", age: 32, email: "donna@home.org" } michael@0: ]; michael@0: let dbName = "the_name"; michael@0: let request = indexedDB.open(dbName, 2); michael@0: request.onerror = function(event) { michael@0: assert.fail("Failed to open db"); michael@0: done(); michael@0: }; michael@0: request.onsuccess = function(event) { michael@0: assert.pass("transaction is complete"); michael@0: testRead(assert, done); michael@0: } michael@0: request.onupgradeneeded = function(event) { michael@0: assert.pass("data base upgrade") michael@0: michael@0: var db = event.target.result; michael@0: michael@0: // Create an objectStore to hold information about our customers. We"re michael@0: // going to use "ssn" as our key path because it"s guaranteed to be michael@0: // unique. michael@0: var objectStore = db.createObjectStore("customers", { keyPath: "ssn" }); michael@0: michael@0: // Create an index to search customers by name. We may have duplicates michael@0: // so we can"t use a unique index. michael@0: objectStore.createIndex("name", "name", { unique: false }); michael@0: michael@0: // Create an index to search customers by email. We want to ensure that michael@0: // no two customers have the same email, so use a unique index. michael@0: objectStore.createIndex("email", "email", { unique: true }); michael@0: michael@0: // Store values in the newly created objectStore. michael@0: customerData.forEach(function(data) { michael@0: objectStore.add(data); michael@0: }); michael@0: assert.pass("data added to object store"); michael@0: }; michael@0: }; michael@0: michael@0: function testRead(assert, done) { michael@0: let dbName = "the_name"; michael@0: let request = indexedDB.open(dbName, 2); michael@0: request.onsuccess = function(event) { michael@0: assert.pass("data opened") michael@0: var db = event.target.result; michael@0: let transaction = db.transaction(["customers"]); michael@0: var objectStore = transaction.objectStore("customers"); michael@0: var request = objectStore.get("444-44-4444"); michael@0: request.onerror = function(event) { michael@0: assert.fail("Failed to retrive data") michael@0: }; michael@0: request.onsuccess = function(event) { michael@0: // Do something with the request.result! michael@0: assert.equal(request.result.name, "Bill", "Name is correct"); michael@0: done(); michael@0: }; michael@0: }; michael@0: request.onerror = function() { michael@0: assert.fail("failed to open db"); michael@0: }; michael@0: }; michael@0: michael@0: } michael@0: } else { michael@0: exports.testDB = function(assert) { michael@0: assert.pass("IndexedDB is not implemented") michael@0: } michael@0: } michael@0: michael@0: require("test").run(exports);