html5webstorage_test.js 3.9 KB

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