summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/sessionstorage/test_sessionStorageBaseSessionOnly.html
blob: 34169decc4f600b230a8177b26174f318845718f (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>sessionStorage basic test, while in sesison only mode</title>

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

<script type="text/javascript">

var testframe;
function iframeOnload(aValue) {
  switch (aValue) {
    case 1:
      testframe.onload = test1;
      break;
    case 2:
      testframe.onload = test2;
      break;
    default:
      of(false, 'should not be reached');
      SimpleTest.finish();
      return;
  }
  /* After every permission change, an iframe has to be reloaded, 
     otherwise this test causes failures in b2g (oop) mochitest, because
     the permission changes don't seem to be always picked up
     by the code that excercises it */
  testframe.contentWindow.location.reload();
}

function startTest() {
  testframe = document.getElementById('testframe');
  SpecialPowers.pushPermissions([{'type': 'cookie', 'allow': SpecialPowers.Ci.nsICookiePermission.ACCESS_SESSION, 'context': document}], function() { iframeOnload(1); });
}

function test1() {

  // Initially check the sessionStorage is empty
  is(sessionStorage.length, 0, "The storage is empty [1]");
  is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
  is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())");
  is(sessionStorage.nonexisting, undefined, "Nonexisting item is undefined (array access)");
  is(sessionStorage.nonexisting, undefined, "Nonexisting item is undefined (property access)");
  sessionStorage.removeItem("nonexisting"); // Just check there is no exception

  is(typeof sessionStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object");
  is(typeof sessionStorage.nonexisting, "undefined", "['nonexisting'] is undefined");
  is(typeof sessionStorage.nonexisting, "undefined", "nonexisting is undefined");
  is(typeof sessionStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object");
  is(typeof sessionStorage.nonexisting2, "undefined", "['nonexisting2'] is undefined");
  is(typeof sessionStorage.nonexisting2, "undefined", "nonexisting2 is undefined");

  // add an empty-value key
  sessionStorage.setItem("empty", "");
  is(sessionStorage.getItem("empty"), "", "Empty value (getItem())");
  is(sessionStorage.empty, "", "Empty value (array access)");
  is(sessionStorage.empty, "", "Empty value (property access)");
  is(typeof sessionStorage.getItem("empty"), "string", "getItem('empty') is string");
  is(typeof sessionStorage.empty, "string", "['empty'] is string");
  is(typeof sessionStorage.empty, "string", "empty is string");
  sessionStorage.removeItem("empty");
  is(sessionStorage.length, 0, "The storage has no keys");
  is(sessionStorage.getItem("empty"), null, "empty item is null (getItem())");
  is(sessionStorage.empty, undefined, "empty item is undefined (array access)");
  is(sessionStorage.empty, undefined, "empty item is undefined (property access)");
  is(typeof sessionStorage.getItem("empty"), "object", "getItem('empty') is object");
  is(typeof sessionStorage.empty, "undefined", "['empty'] is undefined");
  is(typeof sessionStorage.empty, "undefined", "empty is undefined");

  // add one key, check it is there
  sessionStorage.setItem("key1", "value1");
  is(sessionStorage.length, 1, "The storage has one key-value pair");
  is(sessionStorage.key(0), "key1");
  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");

  // check all access method give the correct result
  // and are of the correct type
  is(sessionStorage.getItem("key1"), "value1", "getItem('key1') == value1");
  is(sessionStorage.key1, "value1", "['key1'] == value1");
  is(sessionStorage.key1, "value1", "key1 == value1");

  is(typeof sessionStorage.getItem("key1"), "string", "getItem('key1') is string");
  is(typeof sessionStorage.key1, "string", "['key1'] is string");
  is(typeof sessionStorage.key1, "string", "key1 is string");

  // remove the previously added key and check the storage is empty
  sessionStorage.removeItem("key1");
  is(sessionStorage.length, 0, "The storage is empty [2]");
  is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
  is(sessionStorage.getItem("key1"), null, "\'key1\' removed");

  is(typeof sessionStorage.getItem("key1"), "object", "getItem('key1') is object");
  is(typeof sessionStorage.key1, "undefined", "['key1'] is undefined");
  is(typeof sessionStorage.key1, "undefined", "key1 is undefined");

  // add one key, check it is there
  sessionStorage.setItem("key1", "value1");
  is(sessionStorage.length, 1, "The storage has one key-value pair");
  is(sessionStorage.key(0), "key1");
  is(sessionStorage.getItem("key1"), "value1");

  // add a second key
  sessionStorage.setItem("key2", "value2");
  is(sessionStorage.length, 2, "The storage has two key-value pairs");
  is(sessionStorage.getItem("key1"), "value1");
  is(sessionStorage.getItem("key2"), "value2");
  var firstKey = sessionStorage.key(0);
  var secondKey = sessionStorage.key(1);
  ok((firstKey == 'key1' && secondKey == 'key2') ||
     (firstKey == 'key2' && secondKey == 'key1'),
     'key() API works.');

  // change the second key
  sessionStorage.setItem("key2", "value2-2");
  is(sessionStorage.length, 2, "The storage has two key-value pairs");
  is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved
  is(sessionStorage.key(1), secondKey);
  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
  is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access");
  is(sessionStorage.getItem("key1"), "value1");
  is(sessionStorage.getItem("key2"), "value2-2");

  // change the first key
  sessionStorage.setItem("key1", "value1-2");
  is(sessionStorage.length, 2, "The storage has two key-value pairs");
  is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved
  is(sessionStorage.key(1), secondKey);
  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
  is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access");
  is(sessionStorage.getItem("key1"), "value1-2");
  is(sessionStorage.getItem("key2"), "value2-2");

  // remove the second key
  sessionStorage.removeItem("key2");
  is(sessionStorage.length, 1, "The storage has one key-value pair");
  is(sessionStorage.key(0), "key1");
  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
  is(sessionStorage.getItem("key1"), "value1-2");

  // JS property test
  sessionStorage.testA = "valueA";
  is(sessionStorage.testA, "valueA");
  is(sessionStorage.testA, "valueA");
  is(sessionStorage.getItem("testA"), "valueA");

  sessionStorage.testA = "valueA2";
  is(sessionStorage.testA, "valueA2");
  is(sessionStorage.testA, "valueA2");
  is(sessionStorage.getItem("testA"), "valueA2");

  sessionStorage.testB = "valueB";
  is(sessionStorage.testB, "valueB");
  is(sessionStorage.testB, "valueB");
  is(sessionStorage.getItem("testB"), "valueB");

  sessionStorage.testB = "valueB2";
  is(sessionStorage.testB, "valueB2");
  is(sessionStorage.testB, "valueB2");
  is(sessionStorage.getItem("testB"), "valueB2");

  sessionStorage.setItem("testC", "valueC");
  is(sessionStorage.testC, "valueC");
  is(sessionStorage.testC, "valueC");
  is(sessionStorage.getItem("testC"), "valueC");

  sessionStorage.setItem("testC", "valueC2");
  is(sessionStorage.testC, "valueC2");
  is(sessionStorage.testC, "valueC2");
  is(sessionStorage.getItem("testC"), "valueC2");

  sessionStorage.setItem("testC", null);
  is("testC" in sessionStorage, true);
  is(sessionStorage.getItem("testC"), "null");
  is(sessionStorage.testC, "null");
  is(sessionStorage.testC, "null");

  sessionStorage.removeItem("testC");
  sessionStorage.testC = null;
  is("testC" in sessionStorage, true);
  is(sessionStorage.getItem("testC"), "null");
  is(sessionStorage.testC, "null");
  is(sessionStorage.testC, "null");

  sessionStorage.setItem(null, "test");
  is("null" in sessionStorage, true);
  is(sessionStorage.getItem("null"), "test");
  is(sessionStorage.getItem(null), "test");
  is(sessionStorage.null, "test");
  sessionStorage.removeItem(null, "test");
  is("null" in sessionStorage, false);

  sessionStorage.setItem(null, "test");
  is("null" in sessionStorage, true);
  sessionStorage.removeItem("null", "test");
  is("null" in sessionStorage, false);

  // Clear the storage
  sessionStorage.clear();
  is(sessionStorage.length, 0, "The storage is empty [3]");
  is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
  is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null");
  is(sessionStorage.getItem("key1"), null, "key1 removed");
  is(sessionStorage.getItem("key2"), null, "key2 removed");
  sessionStorage.removeItem("nonexisting"); // Just check there is no exception
  sessionStorage.removeItem("key1"); // Just check there is no exception
  sessionStorage.removeItem("key2"); // Just check there is no exception

  SpecialPowers.pushPermissions([{'type': 'cookie', 'allow': SpecialPowers.Ci.nsICookiePermission.ACCESS_DEFAULT, 'context': document}], function() { iframeOnload(2); });
}

function test2() {
  sessionStorage.clear();
  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();

</script>

</head>

<body onload="startTest();">
<iframe id="testframe" srcdoc="<meta charset=utf-8>"></iframe>
</body>
</html>