mockstorage.js 2.7 KB

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