decorate_test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.decorateTest');
  15. goog.setTestOnly('goog.ui.decorateTest');
  16. goog.require('goog.testing.jsunit');
  17. goog.require('goog.ui.decorate');
  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 testDecorate() {
  61. var dx = goog.ui.decorate(document.getElementById('x'));
  62. assertTrue(
  63. 'Decorator for element with fake-component-x class must be ' +
  64. 'a FakeComponentX',
  65. dx instanceof FakeComponentX);
  66. assertEquals(
  67. 'Element x must have been decorated', document.getElementById('x'),
  68. dx.element);
  69. var dy = goog.ui.decorate(document.getElementById('y'));
  70. assertTrue(
  71. 'Decorator for element with fake-component-y class must be ' +
  72. 'a FakeComponentY',
  73. dy instanceof FakeComponentY);
  74. assertEquals(
  75. 'Element y must have been decorated', document.getElementById('y'),
  76. dy.element);
  77. var dz = goog.ui.decorate(document.getElementById('z'));
  78. assertNull('Decorator for element with unknown class must be null', dz);
  79. var du = goog.ui.decorate(document.getElementById('u'));
  80. assertNull('Decorator for element without CSS class must be null', du);
  81. }