summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/storageevent/test_storageNotifications.html
blob: 8d11a5a778273db6d347cc99910ac13614160e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<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;version=1.7">

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 notificaiton");
        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>