dom/indexedDB/test/unit/test_create_index.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_index.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,121 @@
     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: "a", options: { keyPath: "id", autoIncrement: true } },
    1.16 +    { name: "b", options: { keyPath: "id", autoIncrement: false } },
    1.17 +  ];
    1.18 +  const indexInfo = [
    1.19 +    { name: "1", keyPath: "unique_value", options: { unique: true } },
    1.20 +    { name: "2", keyPath: "value", options: { unique: false } },
    1.21 +    { name: "3", keyPath: "value", options: { unique: false } },
    1.22 +    { name: "", keyPath: "value", options: { unique: false } },
    1.23 +    { name: null, keyPath: "value", options: { unique: false } },
    1.24 +    { name: undefined, keyPath: "value", options: { unique: false } },
    1.25 +  ];
    1.26 +
    1.27 +  let request = indexedDB.open(name, 1);
    1.28 +  request.onerror = errorHandler;
    1.29 +  request.onupgradeneeded = grabEventAndContinueHandler;
    1.30 +  request.onsuccess = unexpectedSuccessHandler;
    1.31 +  let event = yield undefined;
    1.32 +  let db = event.target.result;
    1.33 +
    1.34 +  for (let i = 0; i < objectStoreInfo.length; i++) {
    1.35 +    let info = objectStoreInfo[i];
    1.36 +    let objectStore = info.hasOwnProperty("options") ?
    1.37 +                      db.createObjectStore(info.name, info.options) :
    1.38 +                      db.createObjectStore(info.name);
    1.39 +
    1.40 +    try {
    1.41 +      request = objectStore.createIndex("Hola");
    1.42 +      ok(false, "createIndex with no keyPath should throw");
    1.43 +    }
    1.44 +    catch(e) {
    1.45 +      ok(true, "createIndex with no keyPath should throw");
    1.46 +    }
    1.47 +
    1.48 +    let ex;
    1.49 +    try {
    1.50 +      objectStore.createIndex("Hola", ["foo"], { multiEntry: true });
    1.51 +    }
    1.52 +    catch(e) {
    1.53 +      ex = e;
    1.54 +    }
    1.55 +    ok(ex, "createIndex with array keyPath and multiEntry should throw");
    1.56 +    is(ex.name, "InvalidAccessError", "should throw right exception");
    1.57 +    ok(ex instanceof DOMException, "should throw right exception");
    1.58 +    is(ex.code, DOMException.INVALID_ACCESS_ERR, "should throw right exception");
    1.59 +
    1.60 +    try {
    1.61 +      objectStore.createIndex("foo", "bar", 10);
    1.62 +      ok(false, "createIndex with bad options should throw");
    1.63 +    }
    1.64 +    catch(e) {
    1.65 +      ok(true, "createIndex with bad options threw");
    1.66 +    }
    1.67 +
    1.68 +    ok(objectStore.createIndex("foo", "bar", { foo: "" }),
    1.69 +       "createIndex with unknown options should not throw");
    1.70 +    objectStore.deleteIndex("foo");
    1.71 +
    1.72 +    // Test index creation, and that it ends up in indexNames.
    1.73 +    let objectStoreName = info.name;
    1.74 +    for (let j = 0; j < indexInfo.length; j++) {
    1.75 +      let info = indexInfo[j];
    1.76 +      let count = objectStore.indexNames.length;
    1.77 +      let index = info.hasOwnProperty("options") ?
    1.78 +                  objectStore.createIndex(info.name, info.keyPath,
    1.79 +                                          info.options) :
    1.80 +                  objectStore.createIndex(info.name, info.keyPath);
    1.81 +
    1.82 +      let name = info.name;
    1.83 +      if (name === null) {
    1.84 +        name = "null";
    1.85 +      }
    1.86 +      else if (name === undefined) {
    1.87 +        name = "undefined";
    1.88 +      }
    1.89 +
    1.90 +      is(index.name, name, "correct name");
    1.91 +      is(index.keyPath, info.keyPath, "correct keyPath");
    1.92 +      is(index.unique, info.options.unique, "correct uniqueness");
    1.93 +
    1.94 +      is(objectStore.indexNames.length, count + 1,
    1.95 +         "indexNames grew in size");
    1.96 +      let found = false;
    1.97 +      for (let k = 0; k < objectStore.indexNames.length; k++) {
    1.98 +        if (objectStore.indexNames.item(k) == name) {
    1.99 +          found = true;
   1.100 +          break;
   1.101 +        }
   1.102 +      }
   1.103 +      ok(found, "Name is on objectStore.indexNames");
   1.104 +
   1.105 +      ok(event.target.transaction, "event has a transaction");
   1.106 +      ok(event.target.transaction.db === db,
   1.107 +         "transaction has the right db");
   1.108 +      is(event.target.transaction.mode, "versionchange",
   1.109 +         "transaction has the correct mode");
   1.110 +      is(event.target.transaction.objectStoreNames.length, i + 1,
   1.111 +         "transaction only has one object store");
   1.112 +      ok(event.target.transaction.objectStoreNames.contains(objectStoreName),
   1.113 +         "transaction has the correct object store");
   1.114 +    }
   1.115 +  }
   1.116 +
   1.117 +  request.onsuccess = grabEventAndContinueHandler;
   1.118 +  request.onupgradeneeded = unexpectedSuccessHandler;
   1.119 +
   1.120 +  event = yield undefined;
   1.121 +
   1.122 +  finishTest();
   1.123 +  yield undefined;
   1.124 +}

mercurial