Debian Codegraph @

closure-library/closure/goog/ui/decorate_test.js

raw
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
15 goog.provide('goog.ui.decorateTest');
16 goog.setTestOnly('goog.ui.decorateTest');
17
18 goog.require('goog.testing.jsunit');
19 goog.require('goog.ui.decorate');
20 goog.require('goog.ui.registry');
21 // Fake component and renderer implementations, for testing only.
22
23 // UnknownComponent has no default renderer or decorator registered.
24 function UnknownComponent() {}
25
26 // FakeComponentX's default renderer is FakeRenderer. It also has a
27 // decorator.
28 function FakeComponentX() {
29 this.element = null;
30 }
31
32 FakeComponentX.prototype.decorate = function(element) {
33 this.element = element;
34 };
35
36 // FakeComponentY doesn't have an explicitly registered default
37 // renderer; it should inherit the default renderer from its superclass.
38 // It does have a decorator registered.
39 function FakeComponentY() {
40 FakeComponentX.call(this);
41 }
42 goog.inherits(FakeComponentY, FakeComponentX);
43
44 // FakeComponentZ is just another component. Its default renderer is
45 // FakeSingletonRenderer, but it has no decorator registered.
46 function FakeComponentZ() {}
47
48 // FakeRenderer is a stateful renderer.
49 function FakeRenderer() {}
50
51 // FakeSingletonRenderer is a stateless renderer that can be used as a
52 // singleton.
53 function FakeSingletonRenderer() {}
54
55 FakeSingletonRenderer.instance_ = new FakeSingletonRenderer();
56
57 FakeSingletonRenderer.getInstance = function() {
58 return FakeSingletonRenderer.instance_;
59 };
60
61 function setUp() {
62 goog.ui.registry.setDefaultRenderer(FakeComponentX, FakeRenderer);
63 goog.ui.registry.setDefaultRenderer(FakeComponentZ, FakeSingletonRenderer);
64
65 goog.ui.registry.setDecoratorByClassName(
66 'fake-component-x', function() { return new FakeComponentX(); });
67 goog.ui.registry.setDecoratorByClassName(
68 'fake-component-y', function() { return new FakeComponentY(); });
69 }
70
71 function tearDown() {
72 goog.ui.registry.reset();
73 }
74
75 function testDecorate() {
76 var dx = goog.ui.decorate(document.getElementById('x'));
77 assertTrue(
78 'Decorator for element with fake-component-x class must be ' +
79 'a FakeComponentX',
80 dx instanceof FakeComponentX);
81 assertEquals(
82 'Element x must have been decorated', document.getElementById('x'),
83 dx.element);
84
85 var dy = goog.ui.decorate(document.getElementById('y'));
86 assertTrue(
87 'Decorator for element with fake-component-y class must be ' +
88 'a FakeComponentY',
89 dy instanceof FakeComponentY);
90 assertEquals(
91 'Element y must have been decorated', document.getElementById('y'),
92 dy.element);
93
94 var dz = goog.ui.decorate(document.getElementById('z'));
95 assertNull('Decorator for element with unknown class must be null', dz);
96
97 var du = goog.ui.decorate(document.getElementById('u'));
98 assertNull('Decorator for element without CSS class must be null', du);
99 }