registry_test.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright 2008 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.ui.registryTest');
  15. goog.setTestOnly('goog.ui.registryTest');
  16. goog.require('goog.object');
  17. goog.require('goog.testing.jsunit');
  18. goog.require('goog.ui.registry');
  19. // Fake component and renderer implementations, for testing only.
  20. // UnknownComponent has no default renderer or decorator registered.
  21. function UnknownComponent() {}
  22. // FakeComponentX's default renderer is FakeRenderer. It also has a
  23. // decorator.
  24. function FakeComponentX() {
  25. this.element = null;
  26. }
  27. FakeComponentX.prototype.decorate = function(element) {
  28. this.element = element;
  29. };
  30. // FakeComponentY doesn't have an explicitly registered default
  31. // renderer; it should inherit the default renderer from its superclass.
  32. // It does have a decorator registered.
  33. function FakeComponentY() {
  34. FakeComponentX.call(this);
  35. }
  36. goog.inherits(FakeComponentY, FakeComponentX);
  37. // FakeComponentZ is just another component. Its default renderer is
  38. // FakeSingletonRenderer, but it has no decorator registered.
  39. function FakeComponentZ() {}
  40. // FakeRenderer is a stateful renderer.
  41. function FakeRenderer() {}
  42. // FakeSingletonRenderer is a stateless renderer that can be used as a
  43. // singleton.
  44. function FakeSingletonRenderer() {}
  45. FakeSingletonRenderer.instance_ = new FakeSingletonRenderer();
  46. FakeSingletonRenderer.getInstance = function() {
  47. return FakeSingletonRenderer.instance_;
  48. };
  49. function setUp() {
  50. goog.ui.registry.setDefaultRenderer(FakeComponentX, FakeRenderer);
  51. goog.ui.registry.setDefaultRenderer(FakeComponentZ, FakeSingletonRenderer);
  52. goog.ui.registry.setDecoratorByClassName(
  53. 'fake-component-x', function() { return new FakeComponentX(); });
  54. goog.ui.registry.setDecoratorByClassName(
  55. 'fake-component-y', function() { return new FakeComponentY(); });
  56. }
  57. function tearDown() {
  58. goog.ui.registry.reset();
  59. }
  60. function testGetDefaultRenderer() {
  61. var rx1 = goog.ui.registry.getDefaultRenderer(FakeComponentX);
  62. var rx2 = goog.ui.registry.getDefaultRenderer(FakeComponentX);
  63. assertTrue(
  64. 'FakeComponentX\'s default renderer must be a FakeRenderer',
  65. rx1 instanceof FakeRenderer);
  66. assertNotEquals(
  67. 'Each call to getDefaultRenderer must create a new ' +
  68. 'FakeRenderer',
  69. rx1, rx2);
  70. var ry = goog.ui.registry.getDefaultRenderer(FakeComponentY);
  71. assertTrue(
  72. 'FakeComponentY must inherit its default renderer from ' +
  73. 'its superclass',
  74. ry instanceof FakeRenderer);
  75. var rz1 = goog.ui.registry.getDefaultRenderer(FakeComponentZ);
  76. var rz2 = goog.ui.registry.getDefaultRenderer(FakeComponentZ);
  77. assertTrue(
  78. 'FakeComponentZ\' default renderer must be a ' +
  79. 'FakeSingletonRenderer',
  80. rz1 instanceof FakeSingletonRenderer);
  81. assertEquals(
  82. 'Each call to getDefaultRenderer must return the ' +
  83. 'singleton instance of FakeSingletonRenderer',
  84. rz1, rz2);
  85. assertNull(
  86. 'getDefaultRenderer must return null for unknown component',
  87. goog.ui.registry.getDefaultRenderer(UnknownComponent));
  88. }
  89. function testSetDefaultRenderer() {
  90. var rx1 = goog.ui.registry.getDefaultRenderer(FakeComponentX);
  91. assertTrue(
  92. 'FakeComponentX\'s renderer must be FakeRenderer',
  93. rx1 instanceof FakeRenderer);
  94. var ry1 = goog.ui.registry.getDefaultRenderer(FakeComponentY);
  95. assertTrue(
  96. 'FakeComponentY must inherit its default renderer from ' +
  97. 'its superclass',
  98. ry1 instanceof FakeRenderer);
  99. goog.ui.registry.setDefaultRenderer(FakeComponentX, FakeSingletonRenderer);
  100. var rx2 = goog.ui.registry.getDefaultRenderer(FakeComponentX);
  101. assertEquals(
  102. 'FakeComponentX\'s renderer must be FakeSingletonRenderer',
  103. FakeSingletonRenderer.getInstance(), rx2);
  104. var ry2 = goog.ui.registry.getDefaultRenderer(FakeComponentY);
  105. assertEquals(
  106. 'FakeComponentY must inherit the new default renderer ' +
  107. 'from its superclass',
  108. FakeSingletonRenderer.getInstance(), ry2);
  109. goog.ui.registry.setDefaultRenderer(FakeComponentY, FakeRenderer);
  110. var rx3 = goog.ui.registry.getDefaultRenderer(FakeComponentX);
  111. assertEquals(
  112. 'FakeComponentX\'s renderer must be unchanged',
  113. FakeSingletonRenderer.getInstance(), rx3);
  114. var ry3 = goog.ui.registry.getDefaultRenderer(FakeComponentY);
  115. assertTrue(
  116. 'FakeComponentY must now have its own default renderer',
  117. ry3 instanceof FakeRenderer);
  118. assertThrows(
  119. 'Calling setDefaultRenderer with non-function component ' +
  120. 'must throw error',
  121. function() {
  122. goog.ui.registry.setDefaultRenderer('Not function', FakeRenderer);
  123. });
  124. assertThrows(
  125. 'Calling setDefaultRenderer with non-function renderer ' +
  126. 'must throw error',
  127. function() {
  128. goog.ui.registry.setDefaultRenderer(FakeComponentX, 'Not function');
  129. });
  130. }
  131. function testGetDecoratorByClassName() {
  132. var dx1 = goog.ui.registry.getDecoratorByClassName('fake-component-x');
  133. var dx2 = goog.ui.registry.getDecoratorByClassName('fake-component-x');
  134. assertTrue(
  135. 'fake-component-x must be decorated by a FakeComponentX',
  136. dx1 instanceof FakeComponentX);
  137. assertNotEquals(
  138. 'Each call to getDecoratorByClassName must return a ' +
  139. 'new FakeComponentX instance',
  140. dx1, dx2);
  141. var dy1 = goog.ui.registry.getDecoratorByClassName('fake-component-y');
  142. var dy2 = goog.ui.registry.getDecoratorByClassName('fake-component-y');
  143. assertTrue(
  144. 'fake-component-y must be decorated by a FakeComponentY',
  145. dy1 instanceof FakeComponentY);
  146. assertNotEquals(
  147. 'Each call to getDecoratorByClassName must return a ' +
  148. 'new FakeComponentY instance',
  149. dy1, dy2);
  150. assertNull(
  151. 'getDecoratorByClassName must return null for unknown class',
  152. goog.ui.registry.getDecoratorByClassName('fake-component-z'));
  153. assertNull(
  154. 'getDecoratorByClassName must return null for empty string',
  155. goog.ui.registry.getDecoratorByClassName(''));
  156. }
  157. function testSetDecoratorByClassName() {
  158. var dx1, dx2;
  159. dx1 = goog.ui.registry.getDecoratorByClassName('fake-component-x');
  160. assertTrue(
  161. 'fake-component-x must be decorated by a FakeComponentX',
  162. dx1 instanceof FakeComponentX);
  163. goog.ui.registry.setDecoratorByClassName(
  164. 'fake-component-x', function() { return new UnknownComponent(); });
  165. dx2 = goog.ui.registry.getDecoratorByClassName('fake-component-x');
  166. assertTrue(
  167. 'fake-component-x must now be decorated by UnknownComponent',
  168. dx2 instanceof UnknownComponent);
  169. assertThrows(
  170. 'Calling setDecoratorByClassName with invalid class name ' +
  171. 'must throw error',
  172. function() {
  173. goog.ui.registry.setDecoratorByClassName(
  174. '', function() { return new UnknownComponent(); });
  175. });
  176. assertThrows(
  177. 'Calling setDecoratorByClassName with non-function ' +
  178. 'decorator must throw error',
  179. function() {
  180. goog.ui.registry.setDecoratorByClassName(
  181. 'fake-component-x', 'Not function');
  182. });
  183. }
  184. function testGetDecorator() {
  185. var dx = goog.ui.registry.getDecorator(document.getElementById('x'));
  186. assertTrue(
  187. 'Decorator for element with fake-component-x class must be ' +
  188. 'a FakeComponentX',
  189. dx instanceof FakeComponentX);
  190. var dy = goog.ui.registry.getDecorator(document.getElementById('y'));
  191. assertTrue(
  192. 'Decorator for element with fake-component-y class must be ' +
  193. 'a FakeComponentY',
  194. dy instanceof FakeComponentY);
  195. var dz = goog.ui.registry.getDecorator(document.getElementById('z'));
  196. assertNull('Decorator for element with unknown class must be null', dz);
  197. var du = goog.ui.registry.getDecorator(document.getElementById('u'));
  198. assertNull('Decorator for element without CSS class must be null', du);
  199. }
  200. function testReset() {
  201. assertNotEquals(
  202. 'Some renderers must be registered', 0,
  203. goog.object.getCount(goog.ui.registry.defaultRenderers_));
  204. assertNotEquals(
  205. 'Some decorators must be registered', 0,
  206. goog.object.getCount(goog.ui.registry.decoratorFunctions_));
  207. goog.ui.registry.reset();
  208. assertTrue(
  209. 'No renderers must be registered',
  210. goog.object.isEmpty(goog.ui.registry.defaultRenderers_));
  211. assertTrue(
  212. 'No decorators must be registered',
  213. goog.object.isEmpty(goog.ui.registry.decoratorFunctions_));
  214. }