tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_advance.js (3985B)


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