| 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 |
goog.provide('goog.storage.RichStorageTest'); |
| 16 |
goog.setTestOnly('goog.storage.RichStorageTest'); |
| 17 |
|
| 18 |
goog.require('goog.storage.ErrorCode'); |
| 19 |
goog.require('goog.storage.RichStorage'); |
| 20 |
goog.require('goog.storage.storageTester'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
goog.require('goog.testing.storage.FakeMechanism'); |
| 23 |
|
| 24 |
function testBasicOperations() { |
| 25 |
var mechanism = new goog.testing.storage.FakeMechanism(); |
| 26 |
var storage = new goog.storage.RichStorage(mechanism); |
| 27 |
goog.storage.storageTester.runBasicTests(storage); |
| 28 |
} |
| 29 |
|
| 30 |
function testWrapping() { |
| 31 |
var mechanism = new goog.testing.storage.FakeMechanism(); |
| 32 |
var storage = new goog.storage.RichStorage(mechanism); |
| 33 |
|
| 34 |
// Some metadata. |
| 35 |
var object = {'a': 97, 'b': 98}; |
| 36 |
var wrapper = new goog.storage.RichStorage.Wrapper(object); |
| 37 |
wrapper['meta'] = 'info'; |
| 38 |
storage.set('first', wrapper); |
| 39 |
assertObjectEquals(object, storage.get('first')); |
| 40 |
assertObjectEquals(wrapper, storage.getWrapper('first')); |
| 41 |
assertEquals('info', storage.getWrapper('first')['meta']); |
| 42 |
|
| 43 |
// Multiple wrappings. |
| 44 |
var wrapper1 = goog.storage.RichStorage.Wrapper.wrapIfNecessary(object); |
| 45 |
wrapper1['some'] = 'meta'; |
| 46 |
var wrapper2 = goog.storage.RichStorage.Wrapper.wrapIfNecessary(wrapper1); |
| 47 |
wrapper2['more'] = 'stuff'; |
| 48 |
storage.set('second', wrapper2); |
| 49 |
assertObjectEquals(object, storage.get('second')); |
| 50 |
assertObjectEquals(wrapper2, storage.getWrapper('second')); |
| 51 |
assertEquals('meta', storage.getWrapper('second')['some']); |
| 52 |
assertEquals('stuff', storage.getWrapper('second')['more']); |
| 53 |
|
| 54 |
// Invalid wrappings. |
| 55 |
mechanism.set('third', 'null'); |
| 56 |
assertEquals(goog.storage.ErrorCode.INVALID_VALUE, assertThrows(function() { |
| 57 |
storage.get('third'); |
| 58 |
})); |
| 59 |
mechanism.set('third', '{"meta": "data"}'); |
| 60 |
assertEquals(goog.storage.ErrorCode.INVALID_VALUE, assertThrows(function() { |
| 61 |
storage.get('third'); |
| 62 |
})); |
| 63 |
|
| 64 |
// Weird values. |
| 65 |
var wrapperA = new goog.storage.RichStorage.Wrapper.wrapIfNecessary(null); |
| 66 |
wrapperA['one'] = 1; |
| 67 |
storage.set('first', wrapperA); |
| 68 |
assertObjectEquals(wrapperA, storage.getWrapper('first')); |
| 69 |
var wrapperB = new goog.storage.RichStorage.Wrapper.wrapIfNecessary(''); |
| 70 |
wrapperA['two'] = []; |
| 71 |
storage.set('second', wrapperB); |
| 72 |
assertObjectEquals(wrapperB, storage.getWrapper('second')); |
| 73 |
|
| 74 |
// Clean up. |
| 75 |
storage.remove('first'); |
| 76 |
storage.remove('second'); |
| 77 |
storage.remove('third'); |
| 78 |
assertUndefined(storage.get('first')); |
| 79 |
assertUndefined(storage.get('second')); |
| 80 |
assertUndefined(storage.get('third')); |
| 81 |
assertNull(mechanism.get('first')); |
| 82 |
assertNull(mechanism.get('second')); |
| 83 |
assertNull(mechanism.get('third')); |
| 84 |
} |