mechanismtester.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 Unit tests for the abstract storage mechanism interface.
  16. *
  17. * These tests should be included in tests of any class extending
  18. * goog.storage.mechanism.Mechanism.
  19. *
  20. */
  21. goog.provide('goog.storage.mechanism.mechanismTester');
  22. goog.require('goog.storage.mechanism.ErrorCode');
  23. goog.require('goog.testing.asserts');
  24. goog.require('goog.userAgent');
  25. goog.require('goog.userAgent.product');
  26. goog.require('goog.userAgent.product.isVersion');
  27. goog.setTestOnly('goog.storage.mechanism.mechanismTester');
  28. var mechanism = null;
  29. var minimumQuota = 0;
  30. function testSetGet() {
  31. if (!mechanism) {
  32. return;
  33. }
  34. mechanism.set('first', 'one');
  35. assertEquals('one', mechanism.get('first'));
  36. }
  37. function testChange() {
  38. if (!mechanism) {
  39. return;
  40. }
  41. mechanism.set('first', 'one');
  42. mechanism.set('first', 'two');
  43. assertEquals('two', mechanism.get('first'));
  44. }
  45. function testRemove() {
  46. if (!mechanism) {
  47. return;
  48. }
  49. mechanism.set('first', 'one');
  50. mechanism.remove('first');
  51. assertNull(mechanism.get('first'));
  52. }
  53. function testSetRemoveSet() {
  54. if (!mechanism) {
  55. return;
  56. }
  57. mechanism.set('first', 'one');
  58. mechanism.remove('first');
  59. mechanism.set('first', 'one');
  60. assertEquals('one', mechanism.get('first'));
  61. }
  62. function testRemoveRemove() {
  63. if (!mechanism) {
  64. return;
  65. }
  66. mechanism.remove('first');
  67. mechanism.remove('first');
  68. assertNull(mechanism.get('first'));
  69. }
  70. function testSetTwo() {
  71. if (!mechanism) {
  72. return;
  73. }
  74. mechanism.set('first', 'one');
  75. mechanism.set('second', 'two');
  76. assertEquals('one', mechanism.get('first'));
  77. assertEquals('two', mechanism.get('second'));
  78. }
  79. function testChangeTwo() {
  80. if (!mechanism) {
  81. return;
  82. }
  83. mechanism.set('first', 'one');
  84. mechanism.set('second', 'two');
  85. mechanism.set('second', 'three');
  86. mechanism.set('first', 'four');
  87. assertEquals('four', mechanism.get('first'));
  88. assertEquals('three', mechanism.get('second'));
  89. }
  90. function testSetRemoveThree() {
  91. if (!mechanism) {
  92. return;
  93. }
  94. mechanism.set('first', 'one');
  95. mechanism.set('second', 'two');
  96. mechanism.set('third', 'three');
  97. mechanism.remove('second');
  98. assertNull(mechanism.get('second'));
  99. assertEquals('one', mechanism.get('first'));
  100. assertEquals('three', mechanism.get('third'));
  101. mechanism.remove('first');
  102. assertNull(mechanism.get('first'));
  103. assertEquals('three', mechanism.get('third'));
  104. mechanism.remove('third');
  105. assertNull(mechanism.get('third'));
  106. }
  107. function testEmptyValue() {
  108. if (!mechanism) {
  109. return;
  110. }
  111. mechanism.set('third', '');
  112. assertEquals('', mechanism.get('third'));
  113. }
  114. function testWeirdKeys() {
  115. if (!mechanism) {
  116. return;
  117. }
  118. // Some weird keys. We leave out some tests for some browsers where they
  119. // trigger browser bugs, and where the keys are too obscure to prepare a
  120. // workaround.
  121. mechanism.set(' ', 'space');
  122. mechanism.set('=+!@#$%^&*()-_\\|;:\'",./<>?[]{}~`', 'control');
  123. mechanism.set(
  124. '\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\u5341', 'ten');
  125. mechanism.set('\0', 'null');
  126. mechanism.set('\0\0', 'double null');
  127. mechanism.set('\0A', 'null A');
  128. mechanism.set('', 'zero');
  129. assertEquals('space', mechanism.get(' '));
  130. assertEquals('control', mechanism.get('=+!@#$%^&*()-_\\|;:\'",./<>?[]{}~`'));
  131. assertEquals(
  132. 'ten',
  133. mechanism.get(
  134. '\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\u5341'));
  135. if (!goog.userAgent.IE) {
  136. // IE does not properly handle nulls in HTML5 localStorage keys (IE8, IE9).
  137. // https://connect.microsoft.com/IE/feedback/details/667799/
  138. assertEquals('null', mechanism.get('\0'));
  139. assertEquals('double null', mechanism.get('\0\0'));
  140. assertEquals('null A', mechanism.get('\0A'));
  141. }
  142. if (!goog.userAgent.GECKO) {
  143. // Firefox does not properly handle the empty key (FF 3.5, 3.6, 4.0).
  144. // https://bugzilla.mozilla.org/show_bug.cgi?id=510849
  145. assertEquals('zero', mechanism.get(''));
  146. }
  147. }
  148. function testQuota() {
  149. if (!mechanism) {
  150. return;
  151. }
  152. // This test might crash Safari 4, so it is disabled for this version.
  153. // It works fine on Safari 3 and Safari 5.
  154. if (goog.userAgent.product.SAFARI && goog.userAgent.product.isVersion(4) &&
  155. !goog.userAgent.product.isVersion(5)) {
  156. return;
  157. }
  158. var buffer = '\u03ff'; // 2 bytes
  159. var savedBytes = 0;
  160. try {
  161. while (buffer.length < minimumQuota) {
  162. buffer = buffer + buffer;
  163. mechanism.set('foo', buffer);
  164. savedBytes = buffer.length;
  165. }
  166. } catch (ex) {
  167. if (ex != goog.storage.mechanism.ErrorCode.QUOTA_EXCEEDED) {
  168. throw ex;
  169. }
  170. }
  171. mechanism.remove('foo');
  172. assertTrue(savedBytes >= minimumQuota);
  173. }