storage_test.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 storage interface.
  16. *
  17. */
  18. goog.provide('goog.storage.storage_test');
  19. goog.setTestOnly('goog.storage.storage_test');
  20. goog.require('goog.functions');
  21. goog.require('goog.storage.ErrorCode');
  22. goog.require('goog.storage.Storage');
  23. goog.require('goog.storage.mechanism.mechanismfactory');
  24. goog.require('goog.storage.storageTester');
  25. goog.require('goog.testing.asserts');
  26. goog.require('goog.testing.jsunit');
  27. goog.require('goog.testing.storage.FakeMechanism');
  28. function testBasicOperations() {
  29. var mechanism = new goog.testing.storage.FakeMechanism();
  30. var storage = new goog.storage.Storage(mechanism);
  31. goog.storage.storageTester.runBasicTests(storage);
  32. }
  33. function testMechanismCommunication() {
  34. var mechanism = new goog.testing.storage.FakeMechanism();
  35. var storage = new goog.storage.Storage(mechanism);
  36. // Invalid JSON.
  37. mechanism.set('first', '');
  38. assertEquals(goog.storage.ErrorCode.INVALID_VALUE, assertThrows(function() {
  39. storage.get('first');
  40. }));
  41. mechanism.set('second', '(');
  42. assertEquals(goog.storage.ErrorCode.INVALID_VALUE, assertThrows(function() {
  43. storage.get('second');
  44. }));
  45. // Cleaning up.
  46. storage.remove('first');
  47. storage.remove('second');
  48. assertUndefined(storage.get('first'));
  49. assertUndefined(storage.get('second'));
  50. assertNull(mechanism.get('first'));
  51. assertNull(mechanism.get('second'));
  52. }
  53. function testMechanismFailsGracefullyOnInvalidValue() {
  54. var mechanism = {
  55. get: goog.functions.error('Invalid value')
  56. };
  57. var storage = new goog.storage.Storage(mechanism);
  58. assertUndefined(storage.get('foobar'));
  59. }