| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Unit tests for the abstract storage mechanism interface. |
| 17 |
* |
| 18 |
* These tests should be included in tests of any class extending |
| 19 |
* goog.storage.mechanism.Mechanism. |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
goog.provide('goog.storage.mechanism.mechanismTester'); |
| 24 |
|
| 25 |
goog.require('goog.storage.mechanism.ErrorCode'); |
| 26 |
goog.require('goog.testing.asserts'); |
| 27 |
goog.require('goog.userAgent'); |
| 28 |
goog.require('goog.userAgent.product'); |
| 29 |
goog.require('goog.userAgent.product.isVersion'); |
| 30 |
goog.setTestOnly('goog.storage.mechanism.mechanismTester'); |
| 31 |
|
| 32 |
|
| 33 |
var mechanism = null; |
| 34 |
var minimumQuota = 0; |
| 35 |
|
| 36 |
|
| 37 |
function testSetGet() { |
| 38 |
if (!mechanism) { |
| 39 |
return; |
| 40 |
} |
| 41 |
mechanism.set('first', 'one'); |
| 42 |
assertEquals('one', mechanism.get('first')); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
function testChange() { |
| 47 |
if (!mechanism) { |
| 48 |
return; |
| 49 |
} |
| 50 |
mechanism.set('first', 'one'); |
| 51 |
mechanism.set('first', 'two'); |
| 52 |
assertEquals('two', mechanism.get('first')); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
function testRemove() { |
| 57 |
if (!mechanism) { |
| 58 |
return; |
| 59 |
} |
| 60 |
mechanism.set('first', 'one'); |
| 61 |
mechanism.remove('first'); |
| 62 |
assertNull(mechanism.get('first')); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
function testSetRemoveSet() { |
| 67 |
if (!mechanism) { |
| 68 |
return; |
| 69 |
} |
| 70 |
mechanism.set('first', 'one'); |
| 71 |
mechanism.remove('first'); |
| 72 |
mechanism.set('first', 'one'); |
| 73 |
assertEquals('one', mechanism.get('first')); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
function testRemoveRemove() { |
| 78 |
if (!mechanism) { |
| 79 |
return; |
| 80 |
} |
| 81 |
mechanism.remove('first'); |
| 82 |
mechanism.remove('first'); |
| 83 |
assertNull(mechanism.get('first')); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
function testSetTwo() { |
| 88 |
if (!mechanism) { |
| 89 |
return; |
| 90 |
} |
| 91 |
mechanism.set('first', 'one'); |
| 92 |
mechanism.set('second', 'two'); |
| 93 |
assertEquals('one', mechanism.get('first')); |
| 94 |
assertEquals('two', mechanism.get('second')); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
function testChangeTwo() { |
| 99 |
if (!mechanism) { |
| 100 |
return; |
| 101 |
} |
| 102 |
mechanism.set('first', 'one'); |
| 103 |
mechanism.set('second', 'two'); |
| 104 |
mechanism.set('second', 'three'); |
| 105 |
mechanism.set('first', 'four'); |
| 106 |
assertEquals('four', mechanism.get('first')); |
| 107 |
assertEquals('three', mechanism.get('second')); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
function testSetRemoveThree() { |
| 112 |
if (!mechanism) { |
| 113 |
return; |
| 114 |
} |
| 115 |
mechanism.set('first', 'one'); |
| 116 |
mechanism.set('second', 'two'); |
| 117 |
mechanism.set('third', 'three'); |
| 118 |
mechanism.remove('second'); |
| 119 |
assertNull(mechanism.get('second')); |
| 120 |
assertEquals('one', mechanism.get('first')); |
| 121 |
assertEquals('three', mechanism.get('third')); |
| 122 |
mechanism.remove('first'); |
| 123 |
assertNull(mechanism.get('first')); |
| 124 |
assertEquals('three', mechanism.get('third')); |
| 125 |
mechanism.remove('third'); |
| 126 |
assertNull(mechanism.get('third')); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
function testEmptyValue() { |
| 131 |
if (!mechanism) { |
| 132 |
return; |
| 133 |
} |
| 134 |
mechanism.set('third', ''); |
| 135 |
assertEquals('', mechanism.get('third')); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
function testWeirdKeys() { |
| 140 |
if (!mechanism) { |
| 141 |
return; |
| 142 |
} |
| 143 |
// Some weird keys. We leave out some tests for some browsers where they |
| 144 |
// trigger browser bugs, and where the keys are too obscure to prepare a |
| 145 |
// workaround. |
| 146 |
mechanism.set(' ', 'space'); |
| 147 |
mechanism.set('=+!@#$%^&*()-_\\|;:\'",./<>?[]{}~`', 'control'); |
| 148 |
mechanism.set( |
| 149 |
'\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\u5341', 'ten'); |
| 150 |
mechanism.set('\0', 'null'); |
| 151 |
mechanism.set('\0\0', 'double null'); |
| 152 |
mechanism.set('\0A', 'null A'); |
| 153 |
mechanism.set('', 'zero'); |
| 154 |
assertEquals('space', mechanism.get(' ')); |
| 155 |
assertEquals('control', mechanism.get('=+!@#$%^&*()-_\\|;:\'",./<>?[]{}~`')); |
| 156 |
assertEquals( |
| 157 |
'ten', |
| 158 |
mechanism.get( |
| 159 |
'\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\u5341')); |
| 160 |
if (!goog.userAgent.IE) { |
| 161 |
// IE does not properly handle nulls in HTML5 localStorage keys (IE8, IE9). |
| 162 |
// https://connect.microsoft.com/IE/feedback/details/667799/ |
| 163 |
assertEquals('null', mechanism.get('\0')); |
| 164 |
assertEquals('double null', mechanism.get('\0\0')); |
| 165 |
assertEquals('null A', mechanism.get('\0A')); |
| 166 |
} |
| 167 |
if (!goog.userAgent.GECKO) { |
| 168 |
// Firefox does not properly handle the empty key (FF 3.5, 3.6, 4.0). |
| 169 |
// https://bugzilla.mozilla.org/show_bug.cgi?id=510849 |
| 170 |
assertEquals('zero', mechanism.get('')); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
function testQuota() { |
| 176 |
if (!mechanism) { |
| 177 |
return; |
| 178 |
} |
| 179 |
// This test might crash Safari 4, so it is disabled for this version. |
| 180 |
// It works fine on Safari 3 and Safari 5. |
| 181 |
if (goog.userAgent.product.SAFARI && goog.userAgent.product.isVersion(4) && |
| 182 |
!goog.userAgent.product.isVersion(5)) { |
| 183 |
return; |
| 184 |
} |
| 185 |
var buffer = '\u03ff'; // 2 bytes |
| 186 |
var savedBytes = 0; |
| 187 |
try { |
| 188 |
while (buffer.length < minimumQuota) { |
| 189 |
buffer = buffer + buffer; |
| 190 |
mechanism.set('foo', buffer); |
| 191 |
savedBytes = buffer.length; |
| 192 |
} |
| 193 |
} catch (ex) { |
| 194 |
if (ex != goog.storage.mechanism.ErrorCode.QUOTA_EXCEEDED) { |
| 195 |
throw ex; |
| 196 |
} |
| 197 |
} |
| 198 |
mechanism.remove('foo'); |
| 199 |
assertTrue(savedBytes >= minimumQuota); |
| 200 |
} |