dom/indexedDB/test/unit/test_cursor_mutation.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/test/unit/test_cursor_mutation.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     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 objectStoreData = [
    1.14 +    // This one will be removed.
    1.15 +    { ss: "237-23-7732", name: "Bob" },
    1.16 +
    1.17 +    // These will always be included.
    1.18 +    { ss: "237-23-7733", name: "Ann" },
    1.19 +    { ss: "237-23-7734", name: "Ron" },
    1.20 +    { ss: "237-23-7735", name: "Sue" },
    1.21 +    { ss: "237-23-7736", name: "Joe" },
    1.22 +
    1.23 +    // This one will be added.
    1.24 +    { ss: "237-23-7737", name: "Pat" }
    1.25 +  ];
    1.26 +
    1.27 +  // Post-add and post-remove data ordered by name.
    1.28 +  const objectStoreDataNameSort = [ 1, 4, 5, 2, 3 ];
    1.29 +
    1.30 +  let request = indexedDB.open(this.window ? window.location.pathname : "Splendid Test", 1);
    1.31 +  request.onerror = errorHandler;
    1.32 +  request.onupgradeneeded = grabEventAndContinueHandler;
    1.33 +  let event = yield undefined;
    1.34 +
    1.35 +  let db = event.target.result;
    1.36 +  event.target.onsuccess = continueToNextStep;
    1.37 +
    1.38 +  let objectStore = db.createObjectStore("foo", { keyPath: "ss" });
    1.39 +  objectStore.createIndex("name", "name", { unique: true });
    1.40 +
    1.41 +  for (let i = 0; i < objectStoreData.length - 1; i++) {
    1.42 +    objectStore.add(objectStoreData[i]);
    1.43 +  }
    1.44 +  yield undefined;
    1.45 +
    1.46 +  let count = 0;
    1.47 +
    1.48 +  let sawAdded = false;
    1.49 +  let sawRemoved = false;
    1.50 +
    1.51 +  db.transaction("foo").objectStore("foo").openCursor().onsuccess =
    1.52 +    function(event) {
    1.53 +      event.target.transaction.oncomplete = continueToNextStep;
    1.54 +      let cursor = event.target.result;
    1.55 +      if (cursor) {
    1.56 +        if (cursor.value.name == objectStoreData[0].name) {
    1.57 +          sawRemoved = true;
    1.58 +        }
    1.59 +        if (cursor.value.name ==
    1.60 +            objectStoreData[objectStoreData.length - 1].name) {
    1.61 +          sawAdded = true;
    1.62 +        }
    1.63 +        cursor.continue();
    1.64 +        count++;
    1.65 +      }
    1.66 +    };
    1.67 +  yield undefined;
    1.68 +
    1.69 +  is(count, objectStoreData.length - 1, "Good initial count");
    1.70 +  is(sawAdded, false, "Didn't see item that is about to be added");
    1.71 +  is(sawRemoved, true, "Saw item that is about to be removed");
    1.72 +
    1.73 +  count = 0;
    1.74 +  sawAdded = false;
    1.75 +  sawRemoved = false;
    1.76 +
    1.77 +  db.transaction("foo", "readwrite").objectStore("foo")
    1.78 +    .index("name").openCursor().onsuccess = function(event) {
    1.79 +      event.target.transaction.oncomplete = continueToNextStep;
    1.80 +      let cursor = event.target.result;
    1.81 +      if (cursor) {
    1.82 +        if (cursor.value.name == objectStoreData[0].name) {
    1.83 +          sawRemoved = true;
    1.84 +        }
    1.85 +        if (cursor.value.name ==
    1.86 +            objectStoreData[objectStoreData.length - 1].name) {
    1.87 +          sawAdded = true;
    1.88 +        }
    1.89 +
    1.90 +        is(cursor.value.name,
    1.91 +           objectStoreData[objectStoreDataNameSort[count++]].name,
    1.92 +           "Correct name");
    1.93 +
    1.94 +        if (count == 1) {
    1.95 +          let objectStore = event.target.transaction.objectStore("foo");
    1.96 +          objectStore.delete(objectStoreData[0].ss)
    1.97 +                     .onsuccess = function(event) {
    1.98 +            objectStore.add(objectStoreData[objectStoreData.length - 1])
    1.99 +                       .onsuccess =
   1.100 +              function(event) {
   1.101 +                cursor.continue();
   1.102 +              };
   1.103 +          };
   1.104 +        }
   1.105 +        else {
   1.106 +          cursor.continue();
   1.107 +        }
   1.108 +      }
   1.109 +    };
   1.110 +  yield undefined;
   1.111 +
   1.112 +  is(count, objectStoreData.length - 1, "Good final count");
   1.113 +  is(sawAdded, true, "Saw item that was added");
   1.114 +  is(sawRemoved, false, "Didn't see item that was removed");
   1.115 +
   1.116 +  finishTest();
   1.117 +
   1.118 +  objectStore = null; // Bug 943409 workaround.
   1.119 +
   1.120 +  yield undefined;
   1.121 +}

mercurial