dom/indexedDB/test/unit/test_key_requirements.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/test/unit/test_key_requirements.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,281 @@
     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 +
    1.15 +  let request = indexedDB.open(name, 1);
    1.16 +  request.onerror = errorHandler;
    1.17 +  request.onupgradeneeded = grabEventAndContinueHandler;
    1.18 +  let event = yield undefined;
    1.19 +
    1.20 +  let db = event.target.result;
    1.21 +  db.addEventListener("error", function(event) {
    1.22 +    event.preventDefault();
    1.23 +  }, false);
    1.24 +
    1.25 +  let objectStore = db.createObjectStore("foo", { autoIncrement: true });
    1.26 +
    1.27 +  request = objectStore.add({});
    1.28 +  request.onerror = errorHandler;
    1.29 +  request.onsuccess = grabEventAndContinueHandler;
    1.30 +  event = yield undefined;
    1.31 +
    1.32 +  let key1 = event.target.result;
    1.33 +
    1.34 +  request = objectStore.put({}, key1);
    1.35 +  request.onerror = errorHandler;
    1.36 +  request.onsuccess = grabEventAndContinueHandler;
    1.37 +  event = yield undefined;
    1.38 +
    1.39 +  is(event.target.result, key1, "put gave the same key back");
    1.40 +
    1.41 +  let key2 = 10;
    1.42 +
    1.43 +  request = objectStore.put({}, key2);
    1.44 +  request.onerror = errorHandler;
    1.45 +  request.onsuccess = grabEventAndContinueHandler;
    1.46 +  event = yield undefined;
    1.47 +
    1.48 +  is(event.target.result, key2, "put gave the same key back");
    1.49 +
    1.50 +  key2 = 100;
    1.51 +
    1.52 +  request = objectStore.add({}, key2);
    1.53 +  request.onerror = errorHandler;
    1.54 +  request.onsuccess = grabEventAndContinueHandler;
    1.55 +  event = yield undefined;
    1.56 +
    1.57 +  is(event.target.result, key2, "put gave the same key back");
    1.58 +
    1.59 +  try {
    1.60 +    objectStore.put({});
    1.61 +    ok(true, "put with no key should not throw with autoIncrement!");
    1.62 +  }
    1.63 +  catch (e) {
    1.64 +    ok(false, "put with no key threw with autoIncrement");
    1.65 +  }
    1.66 +
    1.67 +  try {
    1.68 +    objectStore.put({});
    1.69 +    ok(true, "put with no key should not throw with autoIncrement!");
    1.70 +  }
    1.71 +  catch (e) {
    1.72 +    ok(false, "put with no key threw with autoIncrement");
    1.73 +  }
    1.74 +
    1.75 +  try {
    1.76 +    objectStore.delete();
    1.77 +    ok(false, "remove with no key should throw!");
    1.78 +  }
    1.79 +  catch (e) {
    1.80 +    ok(true, "remove with no key threw");
    1.81 +  }
    1.82 +
    1.83 +  objectStore = db.createObjectStore("bar");
    1.84 +
    1.85 +  try {
    1.86 +    objectStore.add({});
    1.87 +    ok(false, "add with no key should throw!");
    1.88 +  }
    1.89 +  catch (e) {
    1.90 +    ok(true, "add with no key threw");
    1.91 +  }
    1.92 +
    1.93 +  try {
    1.94 +    objectStore.put({});
    1.95 +    ok(false, "put with no key should throw!");
    1.96 +  }
    1.97 +  catch (e) {
    1.98 +    ok(true, "put with no key threw");
    1.99 +  }
   1.100 +
   1.101 +  try {
   1.102 +    objectStore.put({});
   1.103 +    ok(false, "put with no key should throw!");
   1.104 +  }
   1.105 +  catch (e) {
   1.106 +    ok(true, "put with no key threw");
   1.107 +  }
   1.108 +
   1.109 +  try {
   1.110 +    objectStore.delete();
   1.111 +    ok(false, "remove with no key should throw!");
   1.112 +  }
   1.113 +  catch (e) {
   1.114 +    ok(true, "remove with no key threw");
   1.115 +  }
   1.116 +
   1.117 +  objectStore = db.createObjectStore("baz", { keyPath: "id" });
   1.118 +
   1.119 +  try {
   1.120 +    objectStore.add({});
   1.121 +    ok(false, "add with no key should throw!");
   1.122 +  }
   1.123 +  catch (e) {
   1.124 +    ok(true, "add with no key threw");
   1.125 +  }
   1.126 +
   1.127 +  try {
   1.128 +    objectStore.add({id:5}, 5);
   1.129 +    ok(false, "add with inline key and passed key should throw!");
   1.130 +  }
   1.131 +  catch (e) {
   1.132 +    ok(true, "add with inline key and passed key threw");
   1.133 +  }
   1.134 +
   1.135 +  try {
   1.136 +    objectStore.put({});
   1.137 +    ok(false, "put with no key should throw!");
   1.138 +  }
   1.139 +  catch (e) {
   1.140 +    ok(true, "put with no key threw");
   1.141 +  }
   1.142 +
   1.143 +  try {
   1.144 +    objectStore.put({});
   1.145 +    ok(false, "put with no key should throw!");
   1.146 +  }
   1.147 +  catch (e) {
   1.148 +    ok(true, "put with no key threw");
   1.149 +  }
   1.150 +
   1.151 +  try {
   1.152 +    objectStore.delete();
   1.153 +    ok(false, "remove with no key should throw!");
   1.154 +  }
   1.155 +  catch (e) {
   1.156 +    ok(true, "remove with no key threw");
   1.157 +  }
   1.158 +
   1.159 +  key1 = 10;
   1.160 +
   1.161 +  request = objectStore.add({id:key1});
   1.162 +  request.onerror = errorHandler;
   1.163 +  request.onsuccess = grabEventAndContinueHandler;
   1.164 +  event = yield undefined;
   1.165 +
   1.166 +  is(event.target.result, key1, "add gave back the same key");
   1.167 +
   1.168 +  request = objectStore.put({id:10});
   1.169 +  request.onerror = errorHandler;
   1.170 +  request.onsuccess = grabEventAndContinueHandler;
   1.171 +  event = yield undefined;
   1.172 +
   1.173 +  is(event.target.result, key1, "put gave back the same key");
   1.174 +
   1.175 +  request = objectStore.put({id:10});
   1.176 +  request.onerror = errorHandler;
   1.177 +  request.onsuccess = grabEventAndContinueHandler;
   1.178 +  event = yield undefined;
   1.179 +
   1.180 +  is(event.target.result, key1, "put gave back the same key");
   1.181 +
   1.182 +  request = objectStore.add({id:10});
   1.183 +  request.addEventListener("error", new ExpectError("ConstraintError", true));
   1.184 +  request.onsuccess = unexpectedSuccessHandler;
   1.185 +  event = yield undefined;
   1.186 +
   1.187 +  try {
   1.188 +    objectStore.add({}, null);
   1.189 +    ok(false, "add with null key should throw!");
   1.190 +  }
   1.191 +  catch (e) {
   1.192 +    ok(true, "add with null key threw");
   1.193 +  }
   1.194 +
   1.195 +  try {
   1.196 +    objectStore.put({}, null);
   1.197 +    ok(false, "put with null key should throw!");
   1.198 +  }
   1.199 +  catch (e) {
   1.200 +    ok(true, "put with null key threw");
   1.201 +  }
   1.202 +
   1.203 +  try {
   1.204 +    objectStore.put({}, null);
   1.205 +    ok(false, "put with null key should throw!");
   1.206 +  }
   1.207 +  catch (e) {
   1.208 +    ok(true, "put with null key threw");
   1.209 +  }
   1.210 +
   1.211 +  try {
   1.212 +    objectStore.delete({}, null);
   1.213 +    ok(false, "remove with null key should throw!");
   1.214 +  }
   1.215 +  catch (e) {
   1.216 +    ok(true, "remove with null key threw");
   1.217 +  }
   1.218 +
   1.219 +  objectStore = db.createObjectStore("bazing", { keyPath: "id",
   1.220 +                                                 autoIncrement: true });
   1.221 +
   1.222 +  request = objectStore.add({});
   1.223 +  request.onerror = errorHandler;
   1.224 +  request.onsuccess = grabEventAndContinueHandler;
   1.225 +  event = yield undefined;
   1.226 +
   1.227 +  key1 = event.target.result;
   1.228 +
   1.229 +  request = objectStore.put({id:key1});
   1.230 +  request.onerror = errorHandler;
   1.231 +  request.onsuccess = grabEventAndContinueHandler;
   1.232 +  event = yield undefined;
   1.233 +
   1.234 +  is(event.target.result, key1, "put gave the same key back");
   1.235 +
   1.236 +  key2 = 10;
   1.237 +
   1.238 +  request = objectStore.put({id:key2});
   1.239 +  request.onerror = errorHandler;
   1.240 +  request.onsuccess = grabEventAndContinueHandler;
   1.241 +  event = yield undefined;
   1.242 +
   1.243 +  is(event.target.result, key2, "put gave the same key back");
   1.244 +
   1.245 +  try {
   1.246 +    objectStore.put({});
   1.247 +    ok(true, "put with no key should not throw with autoIncrement!");
   1.248 +  }
   1.249 +  catch (e) {
   1.250 +    ok(false, "put with no key threw with autoIncrement");
   1.251 +  }
   1.252 +
   1.253 +  try {
   1.254 +    objectStore.put({});
   1.255 +    ok(true, "put with no key should not throw with autoIncrement!");
   1.256 +  }
   1.257 +  catch (e) {
   1.258 +    ok(false, "put with no key threw with autoIncrement");
   1.259 +  }
   1.260 +
   1.261 +  try {
   1.262 +    objectStore.delete();
   1.263 +    ok(false, "remove with no key should throw!");
   1.264 +  }
   1.265 +  catch (e) {
   1.266 +    ok(true, "remove with no key threw");
   1.267 +  }
   1.268 +
   1.269 +  try {
   1.270 +    objectStore.add({id:5}, 5);
   1.271 +    ok(false, "add with inline key and passed key should throw!");
   1.272 +  }
   1.273 +  catch (e) {
   1.274 +    ok(true, "add with inline key and passed key threw");
   1.275 +  }
   1.276 +
   1.277 +  request = objectStore.delete(key2);
   1.278 +  request.onerror = errorHandler;
   1.279 +  request.onsuccess = grabEventAndContinueHandler;
   1.280 +  event = yield undefined;
   1.281 +
   1.282 +  finishTest();
   1.283 +  yield undefined;
   1.284 +}
   1.285 \ No newline at end of file

mercurial