Debian Codegraph @

closure-library/closure/goog/testing/mockstorage.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 /**
16 * @fileoverview Provides a JS storage class implementing the HTML5 Storage
17 * interface.
18 */
19
20
21 goog.setTestOnly('goog.testing.MockStorage');
22 goog.provide('goog.testing.MockStorage');
23
24
25 goog.require('goog.structs.Map');
26
27
28
29 /**
30 * A JS storage instance, implementing the HTML5 Storage interface.
31 * See http://www.w3.org/TR/webstorage/ for details.
32 *
33 * @constructor
34 * @implements {Storage}
35 * @final
36 */
37 goog.testing.MockStorage = function() {
38 /**
39 * The underlying storage object.
40 * @type {goog.structs.Map}
41 * @private
42 */
43 this.store_ = new goog.structs.Map();
44
45 /**
46 * The number of elements in the storage.
47 * @type {number}
48 */
49 this.length = 0;
50 };
51
52
53 /**
54 * Sets an item to the storage.
55 * @param {string} key Storage key.
56 * @param {*} value Storage value. Must be convertible to string.
57 * @override
58 */
59 goog.testing.MockStorage.prototype.setItem = function(key, value) {
60 this.store_.set(key, String(value));
61 this.length = this.store_.getCount();
62 };
63
64
65 /**
66 * Gets an item from the storage. The item returned is the "structured clone"
67 * of the value from setItem. In practice this means it's the value cast to a
68 * string.
69 * @param {string} key Storage key.
70 * @return {?string} Storage value for key; null if does not exist.
71 * @override
72 */
73 goog.testing.MockStorage.prototype.getItem = function(key) {
74 var val = this.store_.get(key);
75 // Enforce that getItem returns string values.
76 return (val != null) ? /** @type {string} */ (val) : null;
77 };
78
79
80 /**
81 * Removes and item from the storage.
82 * @param {string} key Storage key.
83 * @override
84 */
85 goog.testing.MockStorage.prototype.removeItem = function(key) {
86 this.store_.remove(key);
87 this.length = this.store_.getCount();
88 };
89
90
91 /**
92 * Clears the storage.
93 * @override
94 */
95 goog.testing.MockStorage.prototype.clear = function() {
96 this.store_.clear();
97 this.length = 0;
98 };
99
100
101 /**
102 * Returns the key at the given index.
103 * @param {number} index The index for the key.
104 * @return {?string} Key at the given index, null if not found.
105 * @override
106 */
107 goog.testing.MockStorage.prototype.key = function(index) {
108 return this.store_.getKeys()[index] || null;
109 };