dom/indexedDB/test/unit/test_advance.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/test/unit/test_advance.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,192 @@
     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 dataCount = 30;
    1.14 +
    1.15 +  let request = indexedDB.open(this.window ? window.location.pathname : "Splendid Test", 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.onerror = errorHandler;
    1.22 +
    1.23 +  event.target.onsuccess = continueToNextStep;
    1.24 +
    1.25 +  let objectStore = db.createObjectStore("", { keyPath: "key" });
    1.26 +  objectStore.createIndex("", "index");
    1.27 +
    1.28 +  for (let i = 0; i < dataCount; i++) {
    1.29 +    objectStore.add({ key: i, index: i });
    1.30 +  }
    1.31 +  yield undefined;
    1.32 +
    1.33 +  function getObjectStore() {
    1.34 +    return db.transaction("").objectStore("");
    1.35 +  }
    1.36 +
    1.37 +  function getIndex() {
    1.38 +    return db.transaction("").objectStore("").index("");
    1.39 +  }
    1.40 +
    1.41 +  let count = 0;
    1.42 +
    1.43 +  getObjectStore().openCursor().onsuccess = function(event) {
    1.44 +    let cursor = event.target.result;
    1.45 +    if (cursor) {
    1.46 +      count++;
    1.47 +      cursor.continue();
    1.48 +    }
    1.49 +    else {
    1.50 +      continueToNextStep();
    1.51 +    }
    1.52 +  };
    1.53 +  yield undefined;
    1.54 +
    1.55 +  is(count, dataCount, "Saw all data");
    1.56 +
    1.57 +  count = 0;
    1.58 +
    1.59 +  getObjectStore().openCursor().onsuccess = function(event) {
    1.60 +    let cursor = event.target.result;
    1.61 +    if (cursor) {
    1.62 +      is(cursor.primaryKey, count, "Got correct object");
    1.63 +      if (count) {
    1.64 +        count++;
    1.65 +        cursor.continue();
    1.66 +      }
    1.67 +      else {
    1.68 +        count = 10;
    1.69 +        cursor.advance(10);
    1.70 +      }
    1.71 +    }
    1.72 +    else {
    1.73 +      continueToNextStep();
    1.74 +    }
    1.75 +  };
    1.76 +  yield undefined;
    1.77 +
    1.78 +  is(count, dataCount, "Saw all data");
    1.79 +
    1.80 +  count = 0;
    1.81 +
    1.82 +  getIndex().openCursor().onsuccess = function(event) {
    1.83 +    let cursor = event.target.result;
    1.84 +    if (cursor) {
    1.85 +      is(cursor.primaryKey, count, "Got correct object");
    1.86 +      if (count) {
    1.87 +        count++;
    1.88 +        cursor.continue();
    1.89 +      }
    1.90 +      else {
    1.91 +        count = 10;
    1.92 +        cursor.advance(10);
    1.93 +      }
    1.94 +    }
    1.95 +    else {
    1.96 +      continueToNextStep();
    1.97 +    }
    1.98 +  };
    1.99 +  yield undefined;
   1.100 +
   1.101 +  is(count, dataCount, "Saw all data");
   1.102 +
   1.103 +  count = 0;
   1.104 +
   1.105 +  getIndex().openKeyCursor().onsuccess = function(event) {
   1.106 +    let cursor = event.target.result;
   1.107 +    if (cursor) {
   1.108 +      is(cursor.primaryKey, count, "Got correct object");
   1.109 +      if (count) {
   1.110 +        count++;
   1.111 +        cursor.continue();
   1.112 +      }
   1.113 +      else {
   1.114 +        count = 10;
   1.115 +        cursor.advance(10);
   1.116 +      }
   1.117 +    }
   1.118 +    else {
   1.119 +      continueToNextStep();
   1.120 +    }
   1.121 +  };
   1.122 +  yield undefined;
   1.123 +
   1.124 +  is(count, dataCount, "Saw all data");
   1.125 +
   1.126 +  count = 0;
   1.127 +
   1.128 +  getObjectStore().openCursor().onsuccess = function(event) {
   1.129 +    let cursor = event.target.result;
   1.130 +    if (cursor) {
   1.131 +      is(cursor.primaryKey, count, "Got correct object");
   1.132 +      if (count == 0) {
   1.133 +        cursor.advance(dataCount + 1);
   1.134 +      }
   1.135 +      else {
   1.136 +        ok(false, "Should never get here!");
   1.137 +        cursor.continue();
   1.138 +      }
   1.139 +    }
   1.140 +    else {
   1.141 +      continueToNextStep();
   1.142 +    }
   1.143 +  };
   1.144 +  yield undefined;
   1.145 +
   1.146 +  is(count, 0, "Saw all data");
   1.147 +
   1.148 +  count = dataCount - 1;
   1.149 +
   1.150 +  getObjectStore().openCursor(null, "prev").onsuccess = function(event) {
   1.151 +    let cursor = event.target.result;
   1.152 +    if (cursor) {
   1.153 +      is(cursor.primaryKey, count, "Got correct object");
   1.154 +      count--;
   1.155 +      if (count == dataCount - 2) {
   1.156 +        cursor.advance(10);
   1.157 +        count -= 9;
   1.158 +      }
   1.159 +      else {
   1.160 +        cursor.continue();
   1.161 +      }
   1.162 +    }
   1.163 +    else {
   1.164 +      continueToNextStep();
   1.165 +    }
   1.166 +  };
   1.167 +  yield undefined;
   1.168 +
   1.169 +  is(count, -1, "Saw all data");
   1.170 +
   1.171 +  count = dataCount - 1;
   1.172 +
   1.173 +  getObjectStore().openCursor(null, "prev").onsuccess = function(event) {
   1.174 +    let cursor = event.target.result;
   1.175 +    if (cursor) {
   1.176 +      is(cursor.primaryKey, count, "Got correct object");
   1.177 +      if (count == dataCount - 1) {
   1.178 +        cursor.advance(dataCount + 1);
   1.179 +      }
   1.180 +      else {
   1.181 +        ok(false, "Should never get here!");
   1.182 +        cursor.continue();
   1.183 +      }
   1.184 +    }
   1.185 +    else {
   1.186 +      continueToNextStep();
   1.187 +    }
   1.188 +  };
   1.189 +  yield undefined;
   1.190 +
   1.191 +  is(count, dataCount - 1, "Saw all data");
   1.192 +
   1.193 +  finishTest();
   1.194 +  yield undefined;
   1.195 +}

mercurial