dom/tests/mochitest/sessionstorage/test_sessionStorageBaseSessionOnly.html

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

     1 <html xmlns="http://www.w3.org/1999/xhtml">
     2 <head>
     3 <title>sessionStorage basic test, while in sesison only mode</title>
     5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     8 <script type="text/javascript">
    10 var testframe;
    11 function iframeOnload(aValue) {
    12   switch (aValue) {
    13     case 1:
    14       testframe.onload = test1;
    15       break;
    16     case 2:
    17       testframe.onload = test2;
    18       break;
    19     default:
    20       of(false, 'should not be reached');
    21       SimpleTest.finish();
    22       return;
    23   }
    24   /* After every permission change, an iframe has to be reloaded, 
    25      otherwise this test causes failures in b2g (oop) mochitest, because
    26      the permission changes don't seem to be always picked up
    27      by the code that excercises it */
    28   testframe.contentWindow.location.reload();
    29 }
    31 function startTest() {
    32   testframe = document.getElementById('testframe');
    33   SpecialPowers.pushPermissions([{'type': 'cookie', 'allow': SpecialPowers.Ci.nsICookiePermission.ACCESS_SESSION, 'context': document}], function() { iframeOnload(1); });
    34 }
    36 function test1() {
    38   // Initially check the sessionStorage is empty
    39   is(sessionStorage.length, 0, "The storage is empty [1]");
    40   is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
    41   is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
    42   is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
    43   is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())");
    44   is(sessionStorage["nonexisting"], undefined, "Nonexisting item is undefined (array access)");
    45   is(sessionStorage.nonexisting, undefined, "Nonexisting item is undefined (property access)");
    46   sessionStorage.removeItem("nonexisting"); // Just check there is no exception
    48   is(typeof sessionStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object");
    49   is(typeof sessionStorage["nonexisting"], "undefined", "['nonexisting'] is undefined");
    50   is(typeof sessionStorage.nonexisting, "undefined", "nonexisting is undefined");
    51   is(typeof sessionStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object");
    52   is(typeof sessionStorage["nonexisting2"], "undefined", "['nonexisting2'] is undefined");
    53   is(typeof sessionStorage.nonexisting2, "undefined", "nonexisting2 is undefined");
    55   // add an empty-value key
    56   sessionStorage.setItem("empty", "");
    57   is(sessionStorage.getItem("empty"), "", "Empty value (getItem())");
    58   is(sessionStorage["empty"], "", "Empty value (array access)");
    59   is(sessionStorage.empty, "", "Empty value (property access)");
    60   is(typeof sessionStorage.getItem("empty"), "string", "getItem('empty') is string");
    61   is(typeof sessionStorage["empty"], "string", "['empty'] is string");
    62   is(typeof sessionStorage.empty, "string", "empty is string");
    63   sessionStorage.removeItem("empty");
    64   is(sessionStorage.length, 0, "The storage has no keys");
    65   is(sessionStorage.getItem("empty"), null, "empty item is null (getItem())");
    66   is(sessionStorage["empty"], undefined, "empty item is undefined (array access)");
    67   is(sessionStorage.empty, undefined, "empty item is undefined (property access)");
    68   is(typeof sessionStorage.getItem("empty"), "object", "getItem('empty') is object");
    69   is(typeof sessionStorage["empty"], "undefined", "['empty'] is undefined");
    70   is(typeof sessionStorage.empty, "undefined", "empty is undefined");
    72   // add one key, check it is there
    73   sessionStorage.setItem("key1", "value1");
    74   is(sessionStorage.length, 1, "The storage has one key-value pair");
    75   is(sessionStorage.key(0), "key1");
    76   is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
    77   is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
    79   // check all access method give the correct result
    80   // and are of the correct type
    81   is(sessionStorage.getItem("key1"), "value1", "getItem('key1') == value1");
    82   is(sessionStorage["key1"], "value1", "['key1'] == value1");
    83   is(sessionStorage.key1, "value1", "key1 == value1");
    85   is(typeof sessionStorage.getItem("key1"), "string", "getItem('key1') is string");
    86   is(typeof sessionStorage["key1"], "string", "['key1'] is string");
    87   is(typeof sessionStorage.key1, "string", "key1 is string");
    89   // remove the previously added key and check the storage is empty
    90   sessionStorage.removeItem("key1");
    91   is(sessionStorage.length, 0, "The storage is empty [2]");
    92   is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
    93   is(sessionStorage.getItem("key1"), null, "\'key1\' removed");
    95   is(typeof sessionStorage.getItem("key1"), "object", "getItem('key1') is object");
    96   is(typeof sessionStorage["key1"], "undefined", "['key1'] is undefined");
    97   is(typeof sessionStorage.key1, "undefined", "key1 is undefined");
    99   // add one key, check it is there
   100   sessionStorage.setItem("key1", "value1");
   101   is(sessionStorage.length, 1, "The storage has one key-value pair");
   102   is(sessionStorage.key(0), "key1");
   103   is(sessionStorage.getItem("key1"), "value1");
   105   // add a second key
   106   sessionStorage.setItem("key2", "value2");
   107   is(sessionStorage.length, 2, "The storage has two key-value pairs");
   108   is(sessionStorage.getItem("key1"), "value1");
   109   is(sessionStorage.getItem("key2"), "value2");
   110   var firstKey = sessionStorage.key(0);
   111   var secondKey = sessionStorage.key(1);
   112   ok((firstKey == 'key1' && secondKey == 'key2') ||
   113      (firstKey == 'key2' && secondKey == 'key1'),
   114      'key() API works.');
   116   // change the second key
   117   sessionStorage.setItem("key2", "value2-2");
   118   is(sessionStorage.length, 2, "The storage has two key-value pairs");
   119   is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved
   120   is(sessionStorage.key(1), secondKey);
   121   is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
   122   is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access");
   123   is(sessionStorage.getItem("key1"), "value1");
   124   is(sessionStorage.getItem("key2"), "value2-2");
   126   // change the first key
   127   sessionStorage.setItem("key1", "value1-2");
   128   is(sessionStorage.length, 2, "The storage has two key-value pairs");
   129   is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved
   130   is(sessionStorage.key(1), secondKey);
   131   is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
   132   is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access");
   133   is(sessionStorage.getItem("key1"), "value1-2");
   134   is(sessionStorage.getItem("key2"), "value2-2");
   136   // remove the second key
   137   sessionStorage.removeItem("key2");
   138   is(sessionStorage.length, 1, "The storage has one key-value pair");
   139   is(sessionStorage.key(0), "key1");
   140   is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
   141   is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
   142   is(sessionStorage.getItem("key1"), "value1-2");
   144   // JS property test
   145   sessionStorage.testA = "valueA";
   146   is(sessionStorage.testA, "valueA");
   147   is(sessionStorage["testA"], "valueA");
   148   is(sessionStorage.getItem("testA"), "valueA");
   150   sessionStorage.testA = "valueA2";
   151   is(sessionStorage.testA, "valueA2");
   152   is(sessionStorage["testA"], "valueA2");
   153   is(sessionStorage.getItem("testA"), "valueA2");
   155   sessionStorage["testB"] = "valueB";
   156   is(sessionStorage.testB, "valueB");
   157   is(sessionStorage["testB"], "valueB");
   158   is(sessionStorage.getItem("testB"), "valueB");
   160   sessionStorage["testB"] = "valueB2";
   161   is(sessionStorage.testB, "valueB2");
   162   is(sessionStorage["testB"], "valueB2");
   163   is(sessionStorage.getItem("testB"), "valueB2");
   165   sessionStorage.setItem("testC", "valueC");
   166   is(sessionStorage.testC, "valueC");
   167   is(sessionStorage["testC"], "valueC");
   168   is(sessionStorage.getItem("testC"), "valueC");
   170   sessionStorage.setItem("testC", "valueC2");
   171   is(sessionStorage.testC, "valueC2");
   172   is(sessionStorage["testC"], "valueC2");
   173   is(sessionStorage.getItem("testC"), "valueC2");
   175   sessionStorage.setItem("testC", null);
   176   is("testC" in sessionStorage, true);
   177   is(sessionStorage.getItem("testC"), "null");
   178   is(sessionStorage["testC"], "null");
   179   is(sessionStorage.testC, "null");
   181   sessionStorage.removeItem("testC");
   182   sessionStorage["testC"] = null;
   183   is("testC" in sessionStorage, true);
   184   is(sessionStorage.getItem("testC"), "null");
   185   is(sessionStorage["testC"], "null");
   186   is(sessionStorage.testC, "null");
   188   sessionStorage.setItem(null, "test");
   189   is("null" in sessionStorage, true);
   190   is(sessionStorage.getItem("null"), "test");
   191   is(sessionStorage.getItem(null), "test");
   192   is(sessionStorage["null"], "test");
   193   sessionStorage.removeItem(null, "test");
   194   // bug 350023
   195   todo_is("null" in sessionStorage, false);
   197   sessionStorage.setItem(null, "test");
   198   is("null" in sessionStorage, true);
   199   sessionStorage.removeItem("null", "test");
   200   // bug 350023
   201   todo_is("null" in sessionStorage, false);
   203   // Clear the storage
   204   sessionStorage.clear();
   205   is(sessionStorage.length, 0, "The storage is empty [3]");
   206   is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
   207   is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
   208   is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
   209   is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null");
   210   is(sessionStorage.getItem("key1"), null, "key1 removed");
   211   is(sessionStorage.getItem("key2"), null, "key2 removed");
   212   sessionStorage.removeItem("nonexisting"); // Just check there is no exception
   213   sessionStorage.removeItem("key1"); // Just check there is no exception
   214   sessionStorage.removeItem("key2"); // Just check there is no exception
   216   SpecialPowers.pushPermissions([{'type': 'cookie', 'allow': SpecialPowers.Ci.nsICookiePermission.ACCESS_DEFAULT, 'context': document}], function() { iframeOnload(2); });
   217 }
   219 function test2() {
   220   sessionStorage.clear();
   221   SimpleTest.finish();
   222 }
   224 SimpleTest.waitForExplicitFinish();
   226 </script>
   228 </head>
   230 <body onload="startTest();">
   231 <iframe id="testframe" src="data:text/html;charset=utf-8,"></iframe>
   232 </body>
   233 </html>

mercurial