dom/indexedDB/test/unit/test_transaction_lifetimes.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/test/unit/test_transaction_lifetimes.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,91 @@
     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 +  let request = indexedDB.open(this.window ? window.location.pathname : "Splendid Test", 1);
    1.14 +  request.onerror = errorHandler;
    1.15 +  request.onupgradeneeded = grabEventAndContinueHandler;
    1.16 +  request.onsuccess = unexpectedSuccessHandler;
    1.17 +  let event = yield undefined;
    1.18 +
    1.19 +  let db = event.target.result;
    1.20 +  db.onerror = errorHandler;
    1.21 +
    1.22 +  event.target.transaction.onerror = errorHandler;
    1.23 +  event.target.transaction.oncomplete = grabEventAndContinueHandler;
    1.24 +
    1.25 +  let os = db.createObjectStore("foo", { autoIncrement: true });
    1.26 +  let index = os.createIndex("bar", "foo.bar");
    1.27 +  event = yield undefined;
    1.28 +
    1.29 +  is(request.transaction, event.target,
    1.30 +     "request.transaction should still be set");
    1.31 +
    1.32 +  request.onsuccess = grabEventAndContinueHandler;
    1.33 +  event = yield undefined;
    1.34 +
    1.35 +  is(request.transaction, null, "request.transaction should be cleared");
    1.36 +
    1.37 +  let transaction = db.transaction("foo", "readwrite");
    1.38 +
    1.39 +  os = transaction.objectStore("foo");
    1.40 +  // Place a request to keep the transaction alive long enough for our
    1.41 +  // executeSoon.
    1.42 +  let requestComplete = false;
    1.43 +
    1.44 +  let wasAbleToGrabObjectStoreOutsideOfCallback = false;
    1.45 +  let wasAbleToGrabIndexOutsideOfCallback = false;
    1.46 +  executeSoon(function() {
    1.47 +    ok(!requestComplete, "Ordering is correct.");
    1.48 +    wasAbleToGrabObjectStoreOutsideOfCallback = !!transaction.objectStore("foo");
    1.49 +    wasAbleToGrabIndexOutsideOfCallback =
    1.50 +      !!transaction.objectStore("foo").index("bar");
    1.51 +  });
    1.52 +
    1.53 +  request = os.add({});
    1.54 +  request.onerror = errorHandler;
    1.55 +  request.onsuccess = grabEventAndContinueHandler;
    1.56 +
    1.57 +  event = yield undefined;
    1.58 +
    1.59 +  requestComplete = true;
    1.60 +
    1.61 +  ok(wasAbleToGrabObjectStoreOutsideOfCallback,
    1.62 +     "Should be able to get objectStore");
    1.63 +  ok(wasAbleToGrabIndexOutsideOfCallback,
    1.64 +     "Should be able to get index");
    1.65 +
    1.66 +  transaction.oncomplete = grabEventAndContinueHandler;
    1.67 +  yield undefined;
    1.68 +
    1.69 +  try {
    1.70 +    transaction.objectStore("foo");
    1.71 +    ok(false, "Should have thrown!");
    1.72 +  }
    1.73 +  catch (e) {
    1.74 +    ok(e instanceof DOMException, "Got database exception.");
    1.75 +    is(e.name, "InvalidStateError", "Good error.");
    1.76 +    is(e.code, DOMException.INVALID_STATE_ERR, "Good error code.");
    1.77 +  }
    1.78 +
    1.79 +  continueToNextStep();
    1.80 +  yield undefined;
    1.81 +
    1.82 +  try {
    1.83 +    transaction.objectStore("foo");
    1.84 +    ok(false, "Should have thrown!");
    1.85 +  }
    1.86 +  catch (e) {
    1.87 +    ok(e instanceof DOMException, "Got database exception.");
    1.88 +    is(e.name, "InvalidStateError", "Good error.");
    1.89 +    is(e.code, DOMException.INVALID_STATE_ERR, "Good error code.");
    1.90 +  }
    1.91 +
    1.92 +  finishTest();
    1.93 +  yield undefined;
    1.94 +}

mercurial