tweakui_test.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.TweakUiTest');
  15. goog.setTestOnly('goog.tweak.TweakUiTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.string');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.tweak');
  21. goog.require('goog.tweak.TweakUi');
  22. /** @suppress {extraRequire} needed for createRegistryEntries. */
  23. goog.require('goog.tweak.testhelpers');
  24. var root;
  25. var registry;
  26. var EXPECTED_ENTRIES_COUNT = 14;
  27. function setUp() {
  28. root = document.getElementById('root');
  29. // Make both test cases use the same entries in order to be able to test that
  30. // having two UIs on the same page does not cause trouble.
  31. createRegistryEntries('');
  32. registry = goog.tweak.getRegistry();
  33. }
  34. function tearDown() {
  35. goog.tweak.activeBooleanGroup_ = null;
  36. // When debugging a single test, don't clear out the DOM.
  37. if (window.location.search.indexOf('runTests') == -1) {
  38. goog.dom.removeChildren(root);
  39. }
  40. }
  41. function tearDownPage() {
  42. // When debugging a single test, don't clear out the DOM.
  43. if (window.location.search.indexOf('runTests') != -1) {
  44. return;
  45. }
  46. // Create both registries for interactive testing.
  47. createRegistryEntries('');
  48. registry = goog.tweak.getRegistry();
  49. // Add an extra tweak for testing the creation of tweaks after the UI has
  50. // already been rendered.
  51. var entryCounter = 0;
  52. goog.tweak.registerButton(
  53. 'CreateNewTweak', 'Creates a new tweak. Meant ' +
  54. 'to simulate a tweak being registered in a lazy-loaded module.',
  55. function() {
  56. goog.tweak.registerBoolean(
  57. 'Lazy' + ++entryCounter, 'Lazy-loaded tweak.');
  58. });
  59. goog.tweak.registerButton(
  60. 'CreateNewTweakInNamespace1',
  61. 'Creates a new tweak within a namespace. Meant to simulate a tweak ' +
  62. 'being registered in a lazy-loaded module.',
  63. function() {
  64. goog.tweak.registerString(
  65. 'foo.bar.Lazy' + ++entryCounter, 'Lazy-loaded tweak.');
  66. });
  67. goog.tweak.registerButton(
  68. 'CreateNewTweakInNamespace2',
  69. 'Creates a new tweak within a namespace. Meant to simulate a tweak ' +
  70. 'being registered in a lazy-loaded module.',
  71. function() {
  72. goog.tweak.registerNumber(
  73. 'foo.bar.baz.Lazy' + ++entryCounter, 'Lazy combo', 3,
  74. {validValues: [1, 2, 3], label: 'Lazy!'});
  75. });
  76. var label = document.createElement('h3');
  77. goog.dom.setTextContent(label, 'TweakUi:');
  78. root.appendChild(label);
  79. createUi(false);
  80. label = document.createElement('h3');
  81. goog.dom.setTextContent(label, 'Collapsible:');
  82. root.appendChild(label);
  83. createUi(true);
  84. }
  85. function createUi(collapsible) {
  86. var tweakUiElem = collapsible ? goog.tweak.TweakUi.createCollapsible() :
  87. goog.tweak.TweakUi.create();
  88. root.appendChild(tweakUiElem);
  89. }
  90. function getAllEntryDivs() {
  91. return goog.dom.getElementsByTagNameAndClass(
  92. goog.dom.TagName.DIV, goog.tweak.TweakUi.ENTRY_CSS_CLASS_);
  93. }
  94. function getEntryDiv(entry) {
  95. var label = goog.tweak.TweakUi.getNamespacedLabel_(entry);
  96. var allDivs = getAllEntryDivs();
  97. var ret;
  98. for (var i = 0, div; div = allDivs[i]; i++) {
  99. var divText = goog.dom.getTextContent(div);
  100. if (goog.string.startsWith(divText, label) &&
  101. goog.string.contains(divText, entry.description)) {
  102. assertFalse('Found multiple divs matching entry ' + entry.getId(), !!ret);
  103. ret = div;
  104. }
  105. }
  106. assertTrue('getEntryDiv failed for ' + entry.getId(), !!ret);
  107. return ret;
  108. }
  109. function getEntryInput(entry) {
  110. var div = getEntryDiv(entry);
  111. return goog.dom.getElementsByTagName(goog.dom.TagName.INPUT, div)[0] ||
  112. goog.dom.getElementsByTagName(goog.dom.TagName.SELECT, div)[0];
  113. }
  114. function testCreate() {
  115. createUi(false);
  116. assertEquals(
  117. 'Wrong number of entry divs.', EXPECTED_ENTRIES_COUNT,
  118. getAllEntryDivs().length);
  119. assertFalse(
  120. 'checkbox should not be checked 1', getEntryInput(boolEntry).checked);
  121. assertTrue('checkbox should be checked 2', getEntryInput(boolEntry2).checked);
  122. // Enusre custom labels are being used.
  123. var html =
  124. goog.dom.getElementsByTagName(goog.dom.TagName.BUTTON)[0].innerHTML;
  125. assertTrue('Button label is wrong', html.indexOf('<btn>') > -1);
  126. html = getEntryDiv(numEnumEntry).innerHTML;
  127. assertTrue('Enum2 label is wrong', html.indexOf('second&') > -1);
  128. }
  129. function testToggleBooleanSetting() {
  130. boolEntry.setValue(true);
  131. createUi(false);
  132. assertTrue('checkbox should be checked', getEntryInput(boolEntry).checked);
  133. boolEntry.setValue(false);
  134. assertFalse(
  135. 'checkbox should not be checked 1', getEntryInput(boolEntry).checked);
  136. }
  137. function testToggleStringSetting() {
  138. strEntry.setValue('val1');
  139. createUi(false);
  140. assertEquals(
  141. 'Textbox has wrong value 1', 'val1', getEntryInput(strEntry).value);
  142. strEntry.setValue('val2');
  143. assertEquals(
  144. 'Textbox has wrong value 2', 'val2', getEntryInput(strEntry).value);
  145. }
  146. function testToggleStringEnumSetting() {
  147. strEnumEntry.setValue('B');
  148. createUi(false);
  149. assertEquals('wrong value 1', 'B', getEntryInput(strEnumEntry).value);
  150. strEnumEntry.setValue('C');
  151. assertEquals('wrong value 2', 'C', getEntryInput(strEnumEntry).value);
  152. }
  153. function testToggleNumericSetting() {
  154. numEntry.setValue(3);
  155. createUi(false);
  156. assertEquals('wrong value 1', '3', getEntryInput(numEntry).value);
  157. numEntry.setValue(4);
  158. assertEquals('wrong value 2', '4', getEntryInput(numEntry).value);
  159. }
  160. function testToggleNumericEnumSetting() {
  161. numEnumEntry.setValue(2);
  162. createUi(false);
  163. assertEquals('wrong value 1', '2', getEntryInput(numEnumEntry).value);
  164. numEnumEntry.setValue(3);
  165. assertEquals('wrong value 2', '3', getEntryInput(numEnumEntry).value);
  166. }
  167. function testClickBooleanSetting() {
  168. createUi(false);
  169. var input = getEntryInput(boolEntry);
  170. input.checked = true;
  171. input.onchange();
  172. assertTrue('setting should be true', boolEntry.getNewValue());
  173. input.checked = false;
  174. input.onchange();
  175. assertFalse('setting should be false', boolEntry.getNewValue());
  176. }
  177. function testToggleDescriptions() {
  178. createUi(false);
  179. var toggleLink = goog.dom.getElementsByTagName(goog.dom.TagName.A, root)[0];
  180. var heightBefore = root.offsetHeight;
  181. toggleLink.onclick();
  182. assertTrue(
  183. 'Expected div height to grow from toggle descriptions.',
  184. root.offsetHeight > heightBefore);
  185. toggleLink.onclick();
  186. assertEquals(
  187. 'Expected div height to revert from toggle descriptions.', heightBefore,
  188. root.offsetHeight);
  189. }
  190. function assertEntryOrder(entryId1, entryId2) {
  191. var entry1 = registry.getEntry(entryId1);
  192. var entry2 = registry.getEntry(entryId2);
  193. var div1 = getEntryDiv(entry1);
  194. var div2 = getEntryDiv(entry2);
  195. var order = goog.dom.compareNodeOrder(div1, div2);
  196. assertTrue(entry1.getId() + ' should be before ' + entry2.getId(), order < 0);
  197. }
  198. function testAddEntry() {
  199. createUi(false);
  200. goog.tweak.registerBoolean('Lazy1', 'Lazy-loaded tweak.');
  201. goog.tweak.registerBoolean(
  202. 'Lazy2', 'Lazy-loaded tweak.',
  203. /* defaultValue */ false, {restartRequired: false});
  204. goog.tweak.beginBooleanGroup('LazyGroup', 'Lazy-loaded tweak.');
  205. goog.tweak.registerBoolean('Lazy3', 'Lazy-loaded tweak.');
  206. goog.tweak.endBooleanGroup();
  207. assertEquals(
  208. 'Wrong number of entry divs.', EXPECTED_ENTRIES_COUNT + 4,
  209. getAllEntryDivs().length);
  210. assertEntryOrder('Enum2', 'Lazy1');
  211. assertEntryOrder('Lazy1', 'Lazy2');
  212. assertEntryOrder('Lazy2', 'Num');
  213. assertEntryOrder('BoolGroup', 'Lazy3');
  214. }
  215. function testAddNamespacedEntries() {
  216. createUi(false);
  217. goog.tweak.beginBooleanGroup('NS.LazyGroup', 'Lazy-loaded tweak.');
  218. goog.tweak.registerBoolean('NS.InGroup', 'Lazy-loaded tweak.');
  219. goog.tweak.endBooleanGroup();
  220. goog.tweak.registerBoolean('NS.Banana', 'Lazy-loaded tweak.');
  221. goog.tweak.registerBoolean('NS.Apple', 'Lazy-loaded tweak.');
  222. assertEquals(
  223. 'Wrong number of entry divs.', EXPECTED_ENTRIES_COUNT + 5,
  224. getAllEntryDivs().length);
  225. assertEntryOrder('Enum2', 'NS.Apple');
  226. assertEntryOrder('NS.Apple', 'NS.Banana');
  227. assertEntryOrder('NS.Banana', 'NS.InGroup');
  228. }
  229. function testCollapsibleIsLazy() {
  230. if (document.createEvent) {
  231. createUi(true);
  232. assertEquals('Expected no entry divs.', 0, getAllEntryDivs().length);
  233. var showLink = goog.dom.getElementsByTagName(goog.dom.TagName.A, root)[0];
  234. var event = document.createEvent('MouseEvents');
  235. event.initMouseEvent(
  236. 'click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false,
  237. 0, null);
  238. showLink.dispatchEvent(event);
  239. assertEquals(
  240. 'Wrong number of entry divs.', EXPECTED_ENTRIES_COUNT,
  241. getAllEntryDivs().length);
  242. }
  243. }