| 1 |
// Copyright 2013 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.tweak.RegistryTest'); |
| 16 |
goog.setTestOnly('goog.tweak.RegistryTest'); |
| 17 |
|
| 18 |
goog.require('goog.asserts.AssertionError'); |
| 19 |
goog.require('goog.testing.jsunit'); |
| 20 |
goog.require('goog.tweak'); |
| 21 |
/** @suppress {extraRequire} needed for createRegistryEntries. */ |
| 22 |
goog.require('goog.tweak.testhelpers'); |
| 23 |
|
| 24 |
var registry; |
| 25 |
|
| 26 |
function setUp() { |
| 27 |
createRegistryEntries(''); |
| 28 |
registry = goog.tweak.getRegistry(); |
| 29 |
} |
| 30 |
|
| 31 |
function tearDown() { |
| 32 |
goog.tweak.registry_ = null; |
| 33 |
} |
| 34 |
|
| 35 |
function testGetBaseEntry() { |
| 36 |
// Initial values |
| 37 |
assertEquals( |
| 38 |
'wrong bool1 object', boolEntry, registry.getBooleanSetting('Bool')); |
| 39 |
assertEquals( |
| 40 |
'wrong string object', strEntry, registry.getStringSetting('Str')); |
| 41 |
assertEquals( |
| 42 |
'wrong numeric object', numEntry, registry.getNumericSetting('Num')); |
| 43 |
assertEquals('wrong button object', buttonEntry, registry.getEntry('Button')); |
| 44 |
assertEquals( |
| 45 |
'wrong button object', boolGroup, registry.getEntry('BoolGroup')); |
| 46 |
} |
| 47 |
|
| 48 |
function testInitializeFromQueryParams() { |
| 49 |
var testCase = 0; |
| 50 |
function assertQuery( |
| 51 |
queryStr, boolValue, enumValue, strValue, subBoolValue, subBoolValue2) { |
| 52 |
createRegistryEntries(queryStr); |
| 53 |
assertEquals( |
| 54 |
'Wrong bool value for query: ' + queryStr, boolValue, |
| 55 |
boolEntry.getValue()); |
| 56 |
assertEquals( |
| 57 |
'Wrong enum value for query: ' + queryStr, enumValue, |
| 58 |
strEnumEntry.getValue()); |
| 59 |
assertEquals( |
| 60 |
'Wrong str value for query: ' + queryStr, strValue, |
| 61 |
strEntry.getValue()); |
| 62 |
assertEquals( |
| 63 |
'Wrong BoolOne value for query: ' + queryStr, subBoolValue, |
| 64 |
boolOneEntry.getValue()); |
| 65 |
assertEquals( |
| 66 |
'Wrong BoolTwo value for query: ' + queryStr, subBoolValue2, |
| 67 |
boolTwoEntry.getValue()); |
| 68 |
} |
| 69 |
assertQuery('?dummy=1&bool=&enum=&s=', false, '', '', false, true); |
| 70 |
assertQuery('?bool=0&enum=A&s=a', false, 'A', 'a', false, true); |
| 71 |
assertQuery('?bool=1&enum=A', true, 'A', '', false, true); |
| 72 |
assertQuery('?bool=true&enum=B&s=as+df', true, 'B', 'as df', false, true); |
| 73 |
assertQuery('?enum=C', false, 'C', '', false, true); |
| 74 |
assertQuery('?enum=C&boolgroup=-booltwo', false, 'C', '', false, false); |
| 75 |
assertQuery('?enum=C&boolgroup=b1,-booltwo', false, 'C', '', true, false); |
| 76 |
assertQuery('?enum=C&boolgroup=b1', false, 'C', '', true, true); |
| 77 |
assertQuery('?s=a+b%20c%26', false, 'A', 'a b c&', false, true); |
| 78 |
} |
| 79 |
|
| 80 |
function testMakeUrlQuery() { |
| 81 |
assertEquals('All values are default.', '', registry.makeUrlQuery('')); |
| 82 |
assertEquals( |
| 83 |
'All values are default - with existing params.', '?super=pudu', |
| 84 |
registry.makeUrlQuery('?super=pudu')); |
| 85 |
|
| 86 |
boolEntry.setValue(true); |
| 87 |
numEnumEntry.setValue(2); |
| 88 |
strEntry.setValue('f o&o'); |
| 89 |
assertEquals( |
| 90 |
'Wrong query string 1.', '?bool=1&enum2=2&s=f+o%26o', |
| 91 |
registry.makeUrlQuery('?bool=1')); |
| 92 |
assertEquals( |
| 93 |
'Wrong query string 1 - with existing params.', |
| 94 |
'?super=pudu&bool=1&enum2=2&s=f+o%26o', |
| 95 |
registry.makeUrlQuery('?bool=0&s=g&super=pudu')); |
| 96 |
|
| 97 |
boolOneEntry.setValue(true); |
| 98 |
assertEquals( |
| 99 |
'Wrong query string 2.', '?bool=1&boolgroup=B1&enum2=2&s=f+o%26o', |
| 100 |
registry.makeUrlQuery('')); |
| 101 |
|
| 102 |
boolTwoEntry.setValue(false); |
| 103 |
assertEquals( |
| 104 |
'Wrong query string 3.', |
| 105 |
'?bool=1&boolgroup=B1,-booltwo&enum2=2&s=f+o%26o', |
| 106 |
registry.makeUrlQuery('')); |
| 107 |
} |
| 108 |
|
| 109 |
function testOverrideDefaultValue_calledBefore() { |
| 110 |
registry.overrideDefaultValue('b', false); |
| 111 |
registry.overrideDefaultValue('b', true); |
| 112 |
goog.tweak.registerBoolean('b', 'b desc'); |
| 113 |
var bEntry = registry.getEntry('b'); |
| 114 |
assertTrue('Default value should be true.', bEntry.getDefaultValue()); |
| 115 |
assertTrue('Value should be true.', bEntry.getValue()); |
| 116 |
} |
| 117 |
|
| 118 |
function testOverrideDefaultValue_calledAfter() { |
| 119 |
var exception = assertThrows('Should assert.', function() { |
| 120 |
registry.overrideDefaultValue('Bool2', false); |
| 121 |
}); |
| 122 |
assertTrue( |
| 123 |
'Wrong exception', exception instanceof goog.asserts.AssertionError); |
| 124 |
} |
| 125 |
|
| 126 |
function testCompilerOverrideDefaultValue() { |
| 127 |
createRegistryEntries('', {'b': true}); |
| 128 |
registry = goog.tweak.getRegistry(); |
| 129 |
goog.tweak.registerBoolean('b', 'b desc'); |
| 130 |
var bEntry = registry.getEntry('b'); |
| 131 |
assertTrue('Default value should be true.', bEntry.getDefaultValue()); |
| 132 |
assertTrue('Value should be true.', bEntry.getValue()); |
| 133 |
} |
| 134 |
|
| 135 |
function testCompilerAndJsOverrideDefaultValue() { |
| 136 |
createRegistryEntries('', {'b': false}); |
| 137 |
registry = goog.tweak.getRegistry(); |
| 138 |
registry.overrideDefaultValue('b', true); |
| 139 |
goog.tweak.registerBoolean('b', 'b desc', true); |
| 140 |
var bEntry = registry.getEntry('b'); |
| 141 |
assertFalse('Default value should be false.', bEntry.getDefaultValue()); |
| 142 |
assertFalse('Value should be false.', bEntry.getValue()); |
| 143 |
} |