test-indexed-db.js 3.9 KB

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