dom/indexedDB/test/unit/test_cursor_mutation.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     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 objectStoreData = [
    11     // This one will be removed.
    12     { ss: "237-23-7732", name: "Bob" },
    14     // These will always be included.
    15     { ss: "237-23-7733", name: "Ann" },
    16     { ss: "237-23-7734", name: "Ron" },
    17     { ss: "237-23-7735", name: "Sue" },
    18     { ss: "237-23-7736", name: "Joe" },
    20     // This one will be added.
    21     { ss: "237-23-7737", name: "Pat" }
    22   ];
    24   // Post-add and post-remove data ordered by name.
    25   const objectStoreDataNameSort = [ 1, 4, 5, 2, 3 ];
    27   let request = indexedDB.open(this.window ? window.location.pathname : "Splendid Test", 1);
    28   request.onerror = errorHandler;
    29   request.onupgradeneeded = grabEventAndContinueHandler;
    30   let event = yield undefined;
    32   let db = event.target.result;
    33   event.target.onsuccess = continueToNextStep;
    35   let objectStore = db.createObjectStore("foo", { keyPath: "ss" });
    36   objectStore.createIndex("name", "name", { unique: true });
    38   for (let i = 0; i < objectStoreData.length - 1; i++) {
    39     objectStore.add(objectStoreData[i]);
    40   }
    41   yield undefined;
    43   let count = 0;
    45   let sawAdded = false;
    46   let sawRemoved = false;
    48   db.transaction("foo").objectStore("foo").openCursor().onsuccess =
    49     function(event) {
    50       event.target.transaction.oncomplete = continueToNextStep;
    51       let cursor = event.target.result;
    52       if (cursor) {
    53         if (cursor.value.name == objectStoreData[0].name) {
    54           sawRemoved = true;
    55         }
    56         if (cursor.value.name ==
    57             objectStoreData[objectStoreData.length - 1].name) {
    58           sawAdded = true;
    59         }
    60         cursor.continue();
    61         count++;
    62       }
    63     };
    64   yield undefined;
    66   is(count, objectStoreData.length - 1, "Good initial count");
    67   is(sawAdded, false, "Didn't see item that is about to be added");
    68   is(sawRemoved, true, "Saw item that is about to be removed");
    70   count = 0;
    71   sawAdded = false;
    72   sawRemoved = false;
    74   db.transaction("foo", "readwrite").objectStore("foo")
    75     .index("name").openCursor().onsuccess = function(event) {
    76       event.target.transaction.oncomplete = continueToNextStep;
    77       let cursor = event.target.result;
    78       if (cursor) {
    79         if (cursor.value.name == objectStoreData[0].name) {
    80           sawRemoved = true;
    81         }
    82         if (cursor.value.name ==
    83             objectStoreData[objectStoreData.length - 1].name) {
    84           sawAdded = true;
    85         }
    87         is(cursor.value.name,
    88            objectStoreData[objectStoreDataNameSort[count++]].name,
    89            "Correct name");
    91         if (count == 1) {
    92           let objectStore = event.target.transaction.objectStore("foo");
    93           objectStore.delete(objectStoreData[0].ss)
    94                      .onsuccess = function(event) {
    95             objectStore.add(objectStoreData[objectStoreData.length - 1])
    96                        .onsuccess =
    97               function(event) {
    98                 cursor.continue();
    99               };
   100           };
   101         }
   102         else {
   103           cursor.continue();
   104         }
   105       }
   106     };
   107   yield undefined;
   109   is(count, objectStoreData.length - 1, "Good final count");
   110   is(sawAdded, true, "Saw item that was added");
   111   is(sawRemoved, false, "Didn't see item that was removed");
   113   finishTest();
   115   objectStore = null; // Bug 943409 workaround.
   117   yield undefined;
   118 }

mercurial