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