dom/indexedDB/test/unit/test_add_put.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/test/unit/test_add_put.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,165 @@
     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 +  let openRequest = indexedDB.open(name, 1);
    1.15 +  openRequest.onerror = errorHandler;
    1.16 +  openRequest.onupgradeneeded = grabEventAndContinueHandler;
    1.17 +  openRequest.onsuccess = unexpectedSuccessHandler;
    1.18 +  let event = yield undefined;
    1.19 +  let db = event.target.result;
    1.20 +  let trans = event.target.transaction;
    1.21 +
    1.22 +  for each (let autoincrement in [true, false]) {
    1.23 +    for each (let keypath in [false, true, "missing", "invalid"]) {
    1.24 +      for each (let method in ["put", "add"]) {
    1.25 +        for each (let explicit in [true, false, undefined, "invalid"]) {
    1.26 +          for each (let existing in [true, false]) {
    1.27 +            let speccedNoKey = (keypath == false || keypath == "missing") &&
    1.28 +                               !explicit;
    1.29 +
    1.30 +            // We can't do 'existing' checks if we use autogenerated key
    1.31 +            if (speccedNoKey && autoincrement && existing) {
    1.32 +              continue;
    1.33 +            }
    1.34 +
    1.35 +            // Create store
    1.36 +            if (db.objectStoreNames.contains("mystore"))
    1.37 +              db.deleteObjectStore("mystore");
    1.38 +            let store = db.createObjectStore("mystore",
    1.39 +                                             { autoIncrement: autoincrement,
    1.40 +                                               keyPath: (keypath ? "id" : null) });
    1.41 +
    1.42 +            test = " for test " + JSON.stringify({ autoincrement: autoincrement,
    1.43 +                                                   keypath: keypath,
    1.44 +                                                   method: method,
    1.45 +                                                   explicit: explicit === undefined ? "undefined" : explicit,
    1.46 +                                                   existing: existing });
    1.47 +
    1.48 +            // Insert "existing" data if needed
    1.49 +            if (existing) {
    1.50 +              if (keypath)
    1.51 +                store.add({ existing: "data", id: 5 }).onsuccess = grabEventAndContinueHandler;
    1.52 +              else
    1.53 +                store.add({ existing: "data" }, 5).onsuccess = grabEventAndContinueHandler;
    1.54 +
    1.55 +              let e = yield undefined;
    1.56 +              is(e.type, "success", "success inserting existing" + test);
    1.57 +              is(e.target.result, 5, "inserted correct key" + test);
    1.58 +            }
    1.59 +
    1.60 +            // Set up value to be inserted
    1.61 +            let value = { theObj: true };
    1.62 +            if (keypath === true) {
    1.63 +              value.id = 5;
    1.64 +            }
    1.65 +            else if (keypath === "invalid") {
    1.66 +              value.id = /x/;
    1.67 +            }
    1.68 +
    1.69 +            // Which arguments are passed to function
    1.70 +            args = [value];
    1.71 +            if (explicit === true) {
    1.72 +              args.push(5);
    1.73 +            }
    1.74 +            else if (explicit === undefined) {
    1.75 +              args.push(undefined);
    1.76 +            }
    1.77 +            else if (explicit === "invalid") {
    1.78 +              args.push(/x/);
    1.79 +            }
    1.80 +
    1.81 +            let expected = expectedResult(method, keypath, explicit, autoincrement, existing);
    1.82 +
    1.83 +            let valueJSON = JSON.stringify(value);
    1.84 +
    1.85 +            ok(true, "making call" + test);
    1.86 +
    1.87 +            // Make function call for throwing functions
    1.88 +            if (expected === "throw") {
    1.89 +              try {
    1.90 +                store[method].apply(store, args);
    1.91 +                ok(false, "should have thrown" + test);
    1.92 +              }
    1.93 +              catch (ex) {
    1.94 +                ok(true, "did throw" + test);
    1.95 +                ok(ex instanceof DOMException, "Got a DOMException" + test);
    1.96 +                is(ex.name, "DataError", "expect a DataError" + test);
    1.97 +                is(ex.code, 0, "expect zero" + test);
    1.98 +                is(JSON.stringify(value), valueJSON, "call didn't modify value" + test);
    1.99 +              }
   1.100 +              continue;
   1.101 +            }
   1.102 +
   1.103 +            // Make non-throwing function call
   1.104 +            let req = store[method].apply(store, args);
   1.105 +            is(JSON.stringify(value), valueJSON, "call didn't modify value" + test);
   1.106 +
   1.107 +            req.onsuccess = req.onerror = grabEventAndContinueHandler;
   1.108 +            let e = yield undefined;
   1.109 +
   1.110 +            // Figure out what key we used
   1.111 +            let key = 5;
   1.112 +            if (autoincrement && speccedNoKey) {
   1.113 +              key = 1;
   1.114 +            }
   1.115 +
   1.116 +            // Adjust value if expected
   1.117 +            if (autoincrement && keypath && speccedNoKey) {
   1.118 +              value.id = key;
   1.119 +            }
   1.120 +
   1.121 +            // Check result
   1.122 +            if (expected === "error") {
   1.123 +              is(e.type, "error", "write should fail" + test);
   1.124 +              e.preventDefault();
   1.125 +              e.stopPropagation();
   1.126 +              continue;
   1.127 +            }
   1.128 +
   1.129 +            is(e.type, "success", "write should succeed" + test);
   1.130 +            is(e.target.result, key, "write should return correct key" + test);
   1.131 +
   1.132 +            store.get(key).onsuccess = grabEventAndContinueHandler;
   1.133 +            e = yield undefined;
   1.134 +            is(e.type, "success", "read back should succeed" + test);
   1.135 +            is(JSON.stringify(e.target.result),
   1.136 +               JSON.stringify(value),
   1.137 +               "read back should return correct value" + test);
   1.138 +          }
   1.139 +        }
   1.140 +      }
   1.141 +    }
   1.142 +  }
   1.143 +
   1.144 +  
   1.145 +  function expectedResult(method, keypath, explicit, autoincrement, existing) {
   1.146 +    if (keypath && explicit)
   1.147 +      return "throw";
   1.148 +    if (!keypath && !explicit && !autoincrement)
   1.149 +      return "throw";
   1.150 +    if (keypath == "invalid")
   1.151 +      return "throw";
   1.152 +    if (keypath == "missing" && !autoincrement)
   1.153 +      return "throw";
   1.154 +    if (explicit == "invalid")
   1.155 +      return "throw";
   1.156 +
   1.157 +    if (method == "add" && existing)
   1.158 +      return "error";
   1.159 +
   1.160 +    return "success";
   1.161 +  }
   1.162 +
   1.163 +  openRequest.onsuccess = grabEventAndContinueHandler;
   1.164 +  yield undefined;
   1.165 +
   1.166 +  finishTest();
   1.167 +  yield undefined;
   1.168 +}

mercurial