3 Copyright (c) 2014 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
7 <link rel="import" href="/tvcm/ui.html">
11 tvcm.unittest.testSuite(function() {
12 var TestElement = tvcm.ui.define('div');
13 TestElement.prototype = {
14 __proto__: HTMLDivElement.prototype,
16 decorate: function() {
17 if (!this.decorateCallCount)
18 this.decorateCallCount = 0;
19 this.decorateCallCount++;
23 var Base = tvcm.ui.define('div');
25 __proto__: HTMLDivElement.prototype,
26 decorate: function() {
27 this.decoratedAsBase = true;
30 this.basePropertySet = v;
34 test('decorateOnceViaNew', function() {
35 var testElement = new TestElement();
36 assertEquals(1, testElement.decorateCallCount);
39 test('decorateOnceDirectly', function() {
40 var testElement = document.createElement('div');
41 tvcm.ui.decorate(testElement, TestElement);
42 assertEquals(1, testElement.decorateCallCount);
45 test('componentToString', function() {
46 assertEquals('div', Base.toString());
48 var Sub = tvcm.ui.define('Sub', Base);
49 assertEquals('div::sub', Sub.toString());
51 var SubSub = tvcm.ui.define('Marine', Sub);
52 assertEquals('div::sub::marine', SubSub.toString());
55 test('basicDefines', function() {
56 var baseInstance = new Base();
57 assertTrue(baseInstance instanceof Base);
58 assertTrue(baseInstance.decoratedAsBase);
60 assertEquals(baseInstance.constructor, Base);
61 assertEquals(baseInstance.constructor.toString(), 'div');
63 baseInstance.basePropertySet = 7;
64 assertEquals(7, baseInstance.basePropertySet);
67 test('subclassing', function() {
68 var Sub = tvcm.ui.define('sub', Base);
70 __proto__: Base.prototype,
71 decorate: function() {
72 this.decoratedAsSub = true;
76 var subInstance = new Sub();
77 assertTrue(subInstance instanceof Sub);
78 assertTrue(subInstance.decoratedAsSub);
80 assertTrue(subInstance instanceof Base);
81 assertFalse(subInstance.decoratedAsBase);
83 assertEquals(subInstance.constructor, Sub);
84 assertEquals(subInstance.constructor.toString(), 'div::sub');
86 subInstance.baseProperty = true;
87 assertTrue(subInstance.basePropertySet);
90 var NoArgs = tvcm.ui.define('div');
92 __proto__: HTMLDivElement.prototype,
93 decorate: function() {
94 this.noArgsDecorated_ = true;
96 get noArgsDecorated() {
97 return this.noArgsDecorated_;
101 var Args = tvcm.ui.define('args', NoArgs);
103 __proto__: NoArgs.prototype,
104 decorate: function(first) {
106 this.argsDecorated_ = true;
111 get argsDecorated() {
112 return this.argsDecorated_;
116 var ArgsChild = tvcm.ui.define('args-child', Args);
117 ArgsChild.prototype = {
118 __proto__: Args.prototype,
119 decorate: function(_, second) {
120 this.second_ = second;
121 this.argsChildDecorated_ = true;
127 return this.decorated_;
129 get argsChildDecorated() {
130 return this.argsChildDecorated_ = true;
134 var ArgsDecoratingChild = tvcm.ui.define('args-decorating-child', Args);
135 ArgsDecoratingChild.prototype = {
136 __proto__: Args.prototype,
137 decorate: function(first, second) {
138 Args.prototype.decorate.call(this, first);
139 this.second_ = second;
140 this.argsDecoratingChildDecorated_ = true;
146 return this.decorated_;
148 get argsDecoratingChildDecorated() {
149 return this.argsChildDecorated_ = true;
153 test('decorate_noArguments', function() {
155 assertDoesNotThrow(function() {
156 noArgs = new NoArgs();
158 assertTrue(noArgs.noArgsDecorated);
161 test('decorate_arguments', function() {
162 var args = new Args('this is first');
163 assertEquals('this is first', args.first);
164 assertTrue(args.argsDecorated);
165 assertFalse(args.noArgsDecorated);
168 test('decorate_subclassArguments', function() {
169 var argsChild = new ArgsChild('this is first', 'and second');
170 assertUndefined(argsChild.first);
171 assertEquals('and second', argsChild.second);
173 assertTrue(argsChild.argsChildDecorated);
174 assertFalse(argsChild.argsDecorated);
175 assertFalse(argsChild.noArgsDecorated);
178 test('decorate_subClassCallsParentDecorate', function() {
179 var argsDecoratingChild = new ArgsDecoratingChild(
180 'this is first', 'and second');
181 assertEquals('this is first', argsDecoratingChild.first);
182 assertEquals('and second', argsDecoratingChild.second);
183 assertTrue(argsDecoratingChild.argsDecoratingChildDecorated);
184 assertTrue(argsDecoratingChild.argsDecorated);
185 assertFalse(argsDecoratingChild.noArgsDecorated);
188 test('defineWithNamespace', function() {
189 var svgNS = 'http://www.w3.org/2000/svg';
190 var cls = tvcm.ui.define('svg', undefined, svgNS);
192 __proto__: HTMLUnknownElement.prototype,
194 decorate: function() {
195 this.setAttribute('width', 200);
196 this.setAttribute('height', 200);
197 this.setAttribute('viewPort', '0 0 200 200');
198 var rectEl = document.createElementNS(svgNS, 'rect');
199 rectEl.setAttribute('x', 10);
200 rectEl.setAttribute('y', 10);
201 rectEl.setAttribute('width', 180);
202 rectEl.setAttribute('height', 180);
203 this.appendChild(rectEl);
207 assertEquals('svg', el.tagName);
208 assertEquals(svgNS, el.namespaceURI);
209 this.addHTMLOutput(el);
212 test('defineSubclassWithNamespace', function() {
213 var svgNS = 'http://www.w3.org/2000/svg';
214 var cls = tvcm.ui.define('svg', undefined, svgNS);
216 __proto__: HTMLUnknownElement.prototype,
218 decorate: function() {
219 this.setAttribute('width', 200);
220 this.setAttribute('height', 200);
221 this.setAttribute('viewPort', '0 0 200 200');
222 var rectEl = document.createElementNS(svgNS, 'rect');
223 rectEl.setAttribute('x', 10);
224 rectEl.setAttribute('y', 10);
225 rectEl.setAttribute('width', 180);
226 rectEl.setAttribute('height', 180);
227 this.appendChild(rectEl);
231 var subCls = tvcm.ui.define('sub', cls);
233 __proto__: cls.prototype
235 assertEquals('svg::sub', subCls.toString());
237 var el = new subCls();
238 this.addHTMLOutput(el);
239 assertEquals('svg', el.tagName);
240 assertEquals(svgNS, el.namespaceURI);