dom/tests/mochitest/storageevent/test_storageNotifications.html
author Sebastian Hengst <archaeopteryx@coole-files.de>
Fri, 14 Apr 2017 23:39:22 +0200
changeset 401420 d523c49136775a7187486dbc96858a2cb0383bfb
parent 401401 322fde2d53bf49266dbc293321e6b36a4c789c12
child 401430 56bf52d4ddeb1dc7019c4f9e23961670d0a31414
permissions -rw-r--r--
Backed out changeset 322fde2d53bf (bug 1356569) so bug 1355161 can be backed out. r=backout

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>sessionStorage basic test</title>

<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />

<script type="application/javascript">

var expectedTypes = [
  "localStorage",
  "localStorage",
  "sessionStorage",
  "localStorage",
  "sessionStorage",
  "sessionStorage",
  "localStorage",
  "sessionStorage",
  "localStorage",
  "sessionStorage",
  "localStorage",
  "sessionStorage",
  "sessionStorage",
  "localStorage",
  "sessionStorage",
  "localStorage",
];

var tests = Tests();
function setup() {
  sessionStorage.clear();
  SimpleTest.executeSoon(function() {
    tests.next();
  });
}

function* Tests()
{
  // Initially check the both storages are empty
  is(sessionStorage.length, 0, "Session storage is empty [1]");
  is(localStorage.length, 0, "Local storage is empty [1]");

  var onStorageChanged = {
    observe: function(subject, topic, type) {
      if (topic == "dom-storage2-changed") {
        ok(expectedTypes.length > 0, "Not more then expected events encountered");
        is(type, expectedTypes.shift(), "Expected type of the storage notification");
        tests.next();
      }
    }
  }

  // Listen for dom-storage2-changed notification
  SpecialPowers.Services.obs.addObserver(onStorageChanged,
    "dom-storage2-changed", false);

  // add an empty-value key
  localStorage.setItem("empty", "");
  yield undefined;

  localStorage.setItem("empty", "value-1");
  yield undefined;

  sessionStorage.setItem("empty", "");
  yield undefined;

  localStorage.removeItem("empty");
  yield undefined;

  sessionStorage.setItem("empty", "value-1");
  yield undefined;

  sessionStorage.removeItem("empty");
  yield undefined;

  localStorage.setItem("key1", "value-1");
  yield undefined;

  sessionStorage.setItem("key2", "value-2");
  yield undefined;

  localStorage.setItem("key1", "value-1-2");
  yield undefined;

  sessionStorage.setItem("key2", "value-2-2");
  yield undefined;

  localStorage.setItem("key3", "value-3");
  yield undefined;

  sessionStorage.setItem("key4", "value-4");
  yield undefined;

  sessionStorage.removeItem("key4");
  yield undefined;

  localStorage.setItem("key4", "value-4");
  yield undefined;

  sessionStorage.clear();
  yield undefined;

  localStorage.clear();
  yield undefined;

  SimpleTest.executeSoon(function () {
    SpecialPowers.Services.obs.removeObserver(onStorageChanged,
      "dom-storage2-changed", false);
    is(expectedTypes.length, 0, "received the correct number of events");

    sessionStorage.clear();
    localStorage.clear();
    tests = null;
    SimpleTest.finish();
  });
}

SimpleTest.waitForExplicitFinish();

</script>

</head>

<body onload="setup();">

</body>
</html>