1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 tvcm.require('tvcm.ui');
9 tvcm.unittest.testSuite('tvcm.ui.ui_test', function() {
10 var TestElement = tvcm.ui.define('div');
11 TestElement.prototype = {
12 __proto__: HTMLDivElement.prototype,
14 decorate: function() {
15 if (!this.decorateCallCount)
16 this.decorateCallCount = 0;
17 this.decorateCallCount++;
21 var Base = tvcm.ui.define('div');
23 __proto__: HTMLDivElement.prototype,
24 decorate: function() {
25 this.decoratedAsBase = true;
28 this.basePropertySet = v;
32 test('decorateOnceViaNew', function() {
33 var testElement = new TestElement();
34 assertEquals(1, testElement.decorateCallCount);
37 test('decorateOnceDirectly', function() {
38 var testElement = document.createElement('div');
39 tvcm.ui.decorate(testElement, TestElement);
40 assertEquals(1, testElement.decorateCallCount);
43 test('componentToString', function() {
44 assertEquals('div', Base.toString());
46 var Sub = tvcm.ui.define('Sub', Base);
47 assertEquals('div::sub', Sub.toString());
49 var SubSub = tvcm.ui.define('Marine', Sub);
50 assertEquals('div::sub::marine', SubSub.toString());
53 test('basicDefines', function() {
54 var baseInstance = new Base();
55 assertTrue(baseInstance instanceof Base);
56 assertTrue(baseInstance.decoratedAsBase);
58 assertEquals(baseInstance.constructor, Base);
59 assertEquals(baseInstance.constructor.name, 'div');
61 baseInstance.basePropertySet = 7;
62 assertEquals(7, baseInstance.basePropertySet);
65 test('subclassing', function() {
66 var Sub = tvcm.ui.define('sub', Base);
68 __proto__: Base.prototype,
69 decorate: function() {
70 this.decoratedAsSub = true;
74 var subInstance = new Sub();
75 assertTrue(subInstance instanceof Sub);
76 assertTrue(subInstance.decoratedAsSub);
78 assertTrue(subInstance instanceof Base);
79 assertFalse(subInstance.decoratedAsBase);
81 assertEquals(subInstance.constructor, Sub);
82 assertEquals(subInstance.constructor.name, 'sub');
84 subInstance.baseProperty = true;
85 assertTrue(subInstance.basePropertySet);
88 var NoArgs = tvcm.ui.define('div');
90 __proto__: HTMLDivElement.prototype,
91 decorate: function() {
92 this.noArgsDecorated_ = true;
94 get noArgsDecorated() {
95 return this.noArgsDecorated_;
99 var Args = tvcm.ui.define('args', NoArgs);
101 __proto__: NoArgs.prototype,
102 decorate: function(first) {
104 this.argsDecorated_ = true;
109 get argsDecorated() {
110 return this.argsDecorated_;
114 var ArgsChild = tvcm.ui.define('args-child', Args);
115 ArgsChild.prototype = {
116 __proto__: Args.prototype,
117 decorate: function(_, second) {
118 this.second_ = second;
119 this.argsChildDecorated_ = true;
125 return this.decorated_;
127 get argsChildDecorated() {
128 return this.argsChildDecorated_ = true;
132 var ArgsDecoratingChild = tvcm.ui.define('args-decorating-child', Args);
133 ArgsDecoratingChild.prototype = {
134 __proto__: Args.prototype,
135 decorate: function(first, second) {
136 Args.prototype.decorate.call(this, first);
137 this.second_ = second;
138 this.argsDecoratingChildDecorated_ = true;
144 return this.decorated_;
146 get argsDecoratingChildDecorated() {
147 return this.argsChildDecorated_ = true;
151 test('decorate_noArguments', function() {
153 assertDoesNotThrow(function() {
154 noArgs = new NoArgs();
156 assertTrue(noArgs.noArgsDecorated);
159 test('decorate_arguments', function() {
160 var args = new Args('this is first');
161 assertEquals('this is first', args.first);
162 assertTrue(args.argsDecorated);
163 assertFalse(args.noArgsDecorated);
166 test('decorate_subclassArguments', function() {
167 var argsChild = new ArgsChild('this is first', 'and second');
168 assertUndefined(argsChild.first);
169 assertEquals('and second', argsChild.second);
171 assertTrue(argsChild.argsChildDecorated);
172 assertFalse(argsChild.argsDecorated);
173 assertFalse(argsChild.noArgsDecorated);
176 test('decorate_subClassCallsParentDecorate', function() {
177 var argsDecoratingChild = new ArgsDecoratingChild(
178 'this is first', 'and second');
179 assertEquals('this is first', argsDecoratingChild.first);
180 assertEquals('and second', argsDecoratingChild.second);
181 assertTrue(argsDecoratingChild.argsDecoratingChildDecorated);
182 assertTrue(argsDecoratingChild.argsDecorated);
183 assertFalse(argsDecoratingChild.noArgsDecorated);