registry_test.js 5.2 KB

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