addon-sdk/source/test/test-indexed-db.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 let xulApp = require("sdk/system/xul-app");
michael@0 8 if (xulApp.versionInRange(xulApp.platformVersion, "16.0a1", "*")) {
michael@0 9 new function tests() {
michael@0 10
michael@0 11 const { indexedDB, IDBKeyRange, DOMException
michael@0 12 } = require("sdk/indexed-db");
michael@0 13
michael@0 14 exports["test indexedDB is frozen"] = function(assert){
michael@0 15 let original = indexedDB.open;
michael@0 16 let f = function(){};
michael@0 17 assert.throws(function(){indexedDB.open = f});
michael@0 18 assert.equal(indexedDB.open,original);
michael@0 19 assert.notEqual(indexedDB.open,f);
michael@0 20
michael@0 21 };
michael@0 22
michael@0 23 exports["test db variables"] = function(assert) {
michael@0 24 [ indexedDB, IDBKeyRange, DOMException
michael@0 25 ].forEach(function(value) {
michael@0 26 assert.notEqual(typeof(value), "undefined", "variable is defined");
michael@0 27 });
michael@0 28 }
michael@0 29
michael@0 30 exports["test open"] = function(assert, done) {
michael@0 31 let request = indexedDB.open("MyTestDatabase");
michael@0 32 request.onerror = function(event) {
michael@0 33 assert.fail("Failed to open indexedDB")
michael@0 34 done();
michael@0 35 };
michael@0 36 request.onsuccess = function(event) {
michael@0 37 assert.pass("IndexedDB was open");
michael@0 38 done();
michael@0 39 };
michael@0 40 };
michael@0 41
michael@0 42 exports["test dbname is unprefixed"] = function(assert, done) {
michael@0 43 // verify fixes in https://bugzilla.mozilla.org/show_bug.cgi?id=786688
michael@0 44 let dbName = "dbname-unprefixed";
michael@0 45 let request = indexedDB.open(dbName);
michael@0 46 request.onerror = function(event) {
michael@0 47 assert.fail("Failed to open db");
michael@0 48 done();
michael@0 49 };
michael@0 50 request.onsuccess = function(event) {
michael@0 51 assert.equal(request.result.name, dbName);
michael@0 52 done();
michael@0 53 };
michael@0 54 };
michael@0 55
michael@0 56 exports["test structuring the database"] = function(assert, done) {
michael@0 57 // This is what our customer data looks like.
michael@0 58 let customerData = [
michael@0 59 { ssn: "444-44-4444", name: "Bill", age: 35, email: "bill@company.com" },
michael@0 60 { ssn: "555-55-5555", name: "Donna", age: 32, email: "donna@home.org" }
michael@0 61 ];
michael@0 62 let dbName = "the_name";
michael@0 63 let request = indexedDB.open(dbName, 2);
michael@0 64 request.onerror = function(event) {
michael@0 65 assert.fail("Failed to open db");
michael@0 66 done();
michael@0 67 };
michael@0 68 request.onsuccess = function(event) {
michael@0 69 assert.pass("transaction is complete");
michael@0 70 testRead(assert, done);
michael@0 71 }
michael@0 72 request.onupgradeneeded = function(event) {
michael@0 73 assert.pass("data base upgrade")
michael@0 74
michael@0 75 var db = event.target.result;
michael@0 76
michael@0 77 // Create an objectStore to hold information about our customers. We"re
michael@0 78 // going to use "ssn" as our key path because it"s guaranteed to be
michael@0 79 // unique.
michael@0 80 var objectStore = db.createObjectStore("customers", { keyPath: "ssn" });
michael@0 81
michael@0 82 // Create an index to search customers by name. We may have duplicates
michael@0 83 // so we can"t use a unique index.
michael@0 84 objectStore.createIndex("name", "name", { unique: false });
michael@0 85
michael@0 86 // Create an index to search customers by email. We want to ensure that
michael@0 87 // no two customers have the same email, so use a unique index.
michael@0 88 objectStore.createIndex("email", "email", { unique: true });
michael@0 89
michael@0 90 // Store values in the newly created objectStore.
michael@0 91 customerData.forEach(function(data) {
michael@0 92 objectStore.add(data);
michael@0 93 });
michael@0 94 assert.pass("data added to object store");
michael@0 95 };
michael@0 96 };
michael@0 97
michael@0 98 function testRead(assert, done) {
michael@0 99 let dbName = "the_name";
michael@0 100 let request = indexedDB.open(dbName, 2);
michael@0 101 request.onsuccess = function(event) {
michael@0 102 assert.pass("data opened")
michael@0 103 var db = event.target.result;
michael@0 104 let transaction = db.transaction(["customers"]);
michael@0 105 var objectStore = transaction.objectStore("customers");
michael@0 106 var request = objectStore.get("444-44-4444");
michael@0 107 request.onerror = function(event) {
michael@0 108 assert.fail("Failed to retrive data")
michael@0 109 };
michael@0 110 request.onsuccess = function(event) {
michael@0 111 // Do something with the request.result!
michael@0 112 assert.equal(request.result.name, "Bill", "Name is correct");
michael@0 113 done();
michael@0 114 };
michael@0 115 };
michael@0 116 request.onerror = function() {
michael@0 117 assert.fail("failed to open db");
michael@0 118 };
michael@0 119 };
michael@0 120
michael@0 121 }
michael@0 122 } else {
michael@0 123 exports.testDB = function(assert) {
michael@0 124 assert.pass("IndexedDB is not implemented")
michael@0 125 }
michael@0 126 }
michael@0 127
michael@0 128 require("test").run(exports);

mercurial