dom/indexedDB/test/unit/test_advance.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /**
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/publicdomain/zero/1.0/
     4  */
     6 var testGenerator = testSteps();
     8 function testSteps()
     9 {
    10   const dataCount = 30;
    12   let request = indexedDB.open(this.window ? window.location.pathname : "Splendid Test", 1);
    13   request.onerror = errorHandler;
    14   request.onupgradeneeded = grabEventAndContinueHandler;
    15   let event = yield undefined;
    17   let db = event.target.result;
    18   db.onerror = errorHandler;
    20   event.target.onsuccess = continueToNextStep;
    22   let objectStore = db.createObjectStore("", { keyPath: "key" });
    23   objectStore.createIndex("", "index");
    25   for (let i = 0; i < dataCount; i++) {
    26     objectStore.add({ key: i, index: i });
    27   }
    28   yield undefined;
    30   function getObjectStore() {
    31     return db.transaction("").objectStore("");
    32   }
    34   function getIndex() {
    35     return db.transaction("").objectStore("").index("");
    36   }
    38   let count = 0;
    40   getObjectStore().openCursor().onsuccess = function(event) {
    41     let cursor = event.target.result;
    42     if (cursor) {
    43       count++;
    44       cursor.continue();
    45     }
    46     else {
    47       continueToNextStep();
    48     }
    49   };
    50   yield undefined;
    52   is(count, dataCount, "Saw all data");
    54   count = 0;
    56   getObjectStore().openCursor().onsuccess = function(event) {
    57     let cursor = event.target.result;
    58     if (cursor) {
    59       is(cursor.primaryKey, count, "Got correct object");
    60       if (count) {
    61         count++;
    62         cursor.continue();
    63       }
    64       else {
    65         count = 10;
    66         cursor.advance(10);
    67       }
    68     }
    69     else {
    70       continueToNextStep();
    71     }
    72   };
    73   yield undefined;
    75   is(count, dataCount, "Saw all data");
    77   count = 0;
    79   getIndex().openCursor().onsuccess = function(event) {
    80     let cursor = event.target.result;
    81     if (cursor) {
    82       is(cursor.primaryKey, count, "Got correct object");
    83       if (count) {
    84         count++;
    85         cursor.continue();
    86       }
    87       else {
    88         count = 10;
    89         cursor.advance(10);
    90       }
    91     }
    92     else {
    93       continueToNextStep();
    94     }
    95   };
    96   yield undefined;
    98   is(count, dataCount, "Saw all data");
   100   count = 0;
   102   getIndex().openKeyCursor().onsuccess = function(event) {
   103     let cursor = event.target.result;
   104     if (cursor) {
   105       is(cursor.primaryKey, count, "Got correct object");
   106       if (count) {
   107         count++;
   108         cursor.continue();
   109       }
   110       else {
   111         count = 10;
   112         cursor.advance(10);
   113       }
   114     }
   115     else {
   116       continueToNextStep();
   117     }
   118   };
   119   yield undefined;
   121   is(count, dataCount, "Saw all data");
   123   count = 0;
   125   getObjectStore().openCursor().onsuccess = function(event) {
   126     let cursor = event.target.result;
   127     if (cursor) {
   128       is(cursor.primaryKey, count, "Got correct object");
   129       if (count == 0) {
   130         cursor.advance(dataCount + 1);
   131       }
   132       else {
   133         ok(false, "Should never get here!");
   134         cursor.continue();
   135       }
   136     }
   137     else {
   138       continueToNextStep();
   139     }
   140   };
   141   yield undefined;
   143   is(count, 0, "Saw all data");
   145   count = dataCount - 1;
   147   getObjectStore().openCursor(null, "prev").onsuccess = function(event) {
   148     let cursor = event.target.result;
   149     if (cursor) {
   150       is(cursor.primaryKey, count, "Got correct object");
   151       count--;
   152       if (count == dataCount - 2) {
   153         cursor.advance(10);
   154         count -= 9;
   155       }
   156       else {
   157         cursor.continue();
   158       }
   159     }
   160     else {
   161       continueToNextStep();
   162     }
   163   };
   164   yield undefined;
   166   is(count, -1, "Saw all data");
   168   count = dataCount - 1;
   170   getObjectStore().openCursor(null, "prev").onsuccess = function(event) {
   171     let cursor = event.target.result;
   172     if (cursor) {
   173       is(cursor.primaryKey, count, "Got correct object");
   174       if (count == dataCount - 1) {
   175         cursor.advance(dataCount + 1);
   176       }
   177       else {
   178         ok(false, "Should never get here!");
   179         cursor.continue();
   180       }
   181     }
   182     else {
   183       continueToNextStep();
   184     }
   185   };
   186   yield undefined;
   188   is(count, dataCount - 1, "Saw all data");
   190   finishTest();
   191   yield undefined;
   192 }

mercurial