dom/indexedDB/test/unit/test_create_objectStore.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/test/unit/test_create_objectStore.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,135 @@
     1.4 +/**
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.7 + */
     1.8 +
     1.9 +var testGenerator = testSteps();
    1.10 +
    1.11 +function testSteps()
    1.12 +{
    1.13 +  const name = this.window ? window.location.pathname : "Splendid Test";
    1.14 +  const objectStoreInfo = [
    1.15 +    { name: "1", options: { keyPath: null } },
    1.16 +    { name: "2", options: { keyPath: null, autoIncrement: true } },
    1.17 +    { name: "3", options: { keyPath: null, autoIncrement: false } },
    1.18 +    { name: "4", options: { keyPath: null } },
    1.19 +    { name: "5", options: { keyPath: "foo" } },
    1.20 +    { name: "6" },
    1.21 +    { name: "7", options: null },
    1.22 +    { name: "8", options: { autoIncrement: true } },
    1.23 +    { name: "9", options: { autoIncrement: false } },
    1.24 +    { name: "10", options: { keyPath: "foo", autoIncrement: false } },
    1.25 +    { name: "11", options: { keyPath: "foo", autoIncrement: true } },
    1.26 +    { name: "" },
    1.27 +    { name: null },
    1.28 +    { name: undefined }
    1.29 +  ];
    1.30 +
    1.31 +  let request = indexedDB.open(name, 1);
    1.32 +  request.onerror = errorHandler;
    1.33 +  request.onupgradeneeded = grabEventAndContinueHandler;
    1.34 +  request.onsuccess = unexpectedSuccessHandler;
    1.35 +  let event = yield undefined;
    1.36 +
    1.37 +  let db = event.target.result;
    1.38 +
    1.39 +  let count = db.objectStoreNames.length;
    1.40 +  is(count, 0, "correct objectStoreNames length");
    1.41 +
    1.42 +  try {
    1.43 +    db.createObjectStore("foo", "bar");
    1.44 +    ok(false, "createObjectStore with bad options should throw");
    1.45 +  }
    1.46 +  catch(e) {
    1.47 +    ok(true, "createObjectStore with bad options");
    1.48 +  }
    1.49 +
    1.50 +  ok(db.createObjectStore("foo", { foo: "" }),
    1.51 +     "createObjectStore with unknown options should not throw");
    1.52 +  db.deleteObjectStore("foo");
    1.53 +
    1.54 +  for (let index in objectStoreInfo) {
    1.55 +    index = parseInt(index);
    1.56 +    const info = objectStoreInfo[index];
    1.57 +
    1.58 +    let objectStore = info.hasOwnProperty("options") ?
    1.59 +                      db.createObjectStore(info.name, info.options) :
    1.60 +                      db.createObjectStore(info.name);
    1.61 +
    1.62 +    is(db.objectStoreNames.length, index + 1,
    1.63 +       "updated objectStoreNames list");
    1.64 +
    1.65 +    let name = info.name;
    1.66 +    if (name === null) {
    1.67 +      name = "null";
    1.68 +    }
    1.69 +    else if (name === undefined) {
    1.70 +      name = "undefined";
    1.71 +    }
    1.72 +
    1.73 +    let found = false;
    1.74 +    for (let i = 0; i <= index; i++) {
    1.75 +      if (db.objectStoreNames.item(i) == name) {
    1.76 +        found = true;
    1.77 +        break;
    1.78 +      }
    1.79 +    }
    1.80 +    is(found, true, "objectStoreNames contains name");
    1.81 +
    1.82 +    is(objectStore.name, name, "Bad name");
    1.83 +    is(objectStore.keyPath, info.options && info.options.keyPath ?
    1.84 +                            info.options.keyPath : null,
    1.85 +       "Bad keyPath");
    1.86 +    if(objectStore.indexNames.length, 0, "Bad indexNames");
    1.87 +
    1.88 +    ok(event.target.transaction, "event has a transaction");
    1.89 +    ok(event.target.transaction.db === db, "transaction has the right db");
    1.90 +    is(event.target.transaction.mode, "versionchange",
    1.91 +       "transaction has the correct mode");
    1.92 +    is(event.target.transaction.objectStoreNames.length, index + 1,
    1.93 +       "transaction has correct objectStoreNames list");
    1.94 +    found = false;
    1.95 +    for (let j = 0; j < event.target.transaction.objectStoreNames.length;
    1.96 +         j++) {
    1.97 +      if (event.target.transaction.objectStoreNames.item(j) == name) {
    1.98 +        found = true;
    1.99 +        break;
   1.100 +      }
   1.101 +    }
   1.102 +    is(found, true, "transaction has correct objectStoreNames list");
   1.103 +  }
   1.104 +
   1.105 +  // Can't handle autoincrement and empty keypath
   1.106 +  let ex;
   1.107 +  try {
   1.108 +    db.createObjectStore("storefail", { keyPath: "", autoIncrement: true });
   1.109 +  }
   1.110 +  catch(e) {
   1.111 +    ex = e;
   1.112 +  }
   1.113 +  ok(ex, "createObjectStore with empty keyPath and autoIncrement should throw");
   1.114 +  is(ex.name, "InvalidAccessError", "should throw right exception");
   1.115 +  ok(ex instanceof DOMException, "should throw right exception");
   1.116 +  is(ex.code, DOMException.INVALID_ACCESS_ERR, "should throw right exception");
   1.117 +
   1.118 +  // Can't handle autoincrement and array keypath
   1.119 +  let ex;
   1.120 +  try {
   1.121 +    db.createObjectStore("storefail", { keyPath: ["a"], autoIncrement: true });
   1.122 +  }
   1.123 +  catch(e) {
   1.124 +    ex = e;
   1.125 +  }
   1.126 +  ok(ex, "createObjectStore with array keyPath and autoIncrement should throw");
   1.127 +  is(ex.name, "InvalidAccessError", "should throw right exception");
   1.128 +  ok(ex instanceof DOMException, "should throw right exception");
   1.129 +  is(ex.code, DOMException.INVALID_ACCESS_ERR, "should throw right exception");
   1.130 +
   1.131 +  request.onsuccess = grabEventAndContinueHandler;
   1.132 +  request.onupgradeneeded = unexpectedSuccessHandler;
   1.133 +
   1.134 +  event = yield undefined;
   1.135 +
   1.136 +  finishTest();
   1.137 +  yield undefined;
   1.138 +}

mercurial