Debian Codegraph @

closure-library/closure/goog/storage/mechanism/html5webstorage_test.js

raw
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 goog.setTestOnly('goog.storage.mechanism.HTML5WebStorageTest');
16 goog.provide('goog.storage.mechanism.HTML5MockStorage');
17 goog.provide('goog.storage.mechanism.HTML5WebStorageTest');
18 goog.provide('goog.storage.mechanism.MockThrowableStorage');
19
20 goog.require('goog.storage.mechanism.ErrorCode');
21 goog.require('goog.storage.mechanism.HTML5WebStorage');
22 goog.require('goog.testing.jsunit');
23
24
25
26 /**
27 * A minimal WebStorage implementation that throws exceptions for disabled
28 * storage. Since we cannot have unit tests running in Safari private mode to
29 * test this, we need to mock an exception throwing when trying to set a value.
30 *
31 * @param {boolean=} opt_isStorageDisabled If true, throws exceptions emulating
32 * Private browsing mode. If false, storage quota will be marked as
33 * exceeded.
34 * @constructor
35 */
36 goog.storage.mechanism.MockThrowableStorage = function(opt_isStorageDisabled) {
37 this.isStorageDisabled_ = !!opt_isStorageDisabled;
38 this.length = opt_isStorageDisabled ? 0 : 1;
39 };
40
41
42 /** @override */
43 goog.storage.mechanism.MockThrowableStorage.prototype.setItem = function(
44 key, value) {
45 if (this.isStorageDisabled_) {
46 throw goog.storage.mechanism.ErrorCode.STORAGE_DISABLED;
47 } else {
48 throw goog.storage.mechanism.ErrorCode.QUOTA_EXCEEDED;
49 }
50 };
51
52
53 /** @override */
54 goog.storage.mechanism.MockThrowableStorage.prototype.removeItem = function(
55 key) {};
56
57
58 /**
59 * A very simple, dummy implementation of key(), merely to verify that calls to
60 * HTML5WebStorage#key are proxied through.
61 * @param {number} index A key index.
62 * @return {string} The key associated with that index.
63 */
64 goog.storage.mechanism.MockThrowableStorage.prototype.key = function(index) {
65 return 'dummyKey';
66 };
67
68
69
70 /**
71 * Provides an HTML5WebStorage wrapper for MockThrowableStorage.
72 *
73 * @constructor
74 * @extends {goog.storage.mechanism.HTML5WebStorage}
75 */
76 goog.storage.mechanism.HTML5MockStorage = function(opt_isStorageDisabled) {
77 goog.storage.mechanism.HTML5MockStorage.base(
78 this, 'constructor',
79 new goog.storage.mechanism.MockThrowableStorage(opt_isStorageDisabled));
80 };
81 goog.inherits(
82 goog.storage.mechanism.HTML5MockStorage,
83 goog.storage.mechanism.HTML5WebStorage);
84
85
86 function testIsNotAvailableWhenQuotaExceeded() {
87 var storage = new goog.storage.mechanism.HTML5MockStorage(false);
88 assertFalse(storage.isAvailable());
89 }
90
91 function testIsNotAvailableWhenStorageDisabled() {
92 var storage = new goog.storage.mechanism.HTML5MockStorage(true);
93 assertFalse(storage.isAvailable());
94 }
95
96 function testSetThrowsExceptionWhenQuotaExceeded() {
97 var storage = new goog.storage.mechanism.HTML5MockStorage(false);
98 var isQuotaExceeded = false;
99 try {
100 storage.set('foobar', '1');
101 } catch (e) {
102 isQuotaExceeded = e == goog.storage.mechanism.ErrorCode.QUOTA_EXCEEDED;
103 }
104 assertTrue(isQuotaExceeded);
105 }
106
107 function testSetThrowsExceptionWhenStorageDisabled() {
108 var storage = new goog.storage.mechanism.HTML5MockStorage(true);
109 var isStorageDisabled = false;
110 try {
111 storage.set('foobar', '1');
112 } catch (e) {
113 isStorageDisabled = e == goog.storage.mechanism.ErrorCode.STORAGE_DISABLED;
114 }
115 assertTrue(isStorageDisabled);
116 }
117
118 function testKeyIterationWithKeyMethod() {
119 var storage = new goog.storage.mechanism.HTML5MockStorage(true);
120 assertEquals('dummyKey', storage.key(1));
121 }