Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / src / tvcm / ui / ui_test.js
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.
4
5 'use strict';
6
7 tvcm.require('tvcm.ui');
8
9 tvcm.unittest.testSuite('tvcm.ui.ui_test', function() {
10   var TestElement = tvcm.ui.define('div');
11   TestElement.prototype = {
12     __proto__: HTMLDivElement.prototype,
13
14     decorate: function() {
15       if (!this.decorateCallCount)
16         this.decorateCallCount = 0;
17       this.decorateCallCount++;
18     }
19   };
20
21   var Base = tvcm.ui.define('div');
22   Base.prototype = {
23     __proto__: HTMLDivElement.prototype,
24     decorate: function() {
25       this.decoratedAsBase = true;
26     },
27     set baseProperty(v) {
28       this.basePropertySet = v;
29     }
30   };
31
32   test('decorateOnceViaNew', function() {
33     var testElement = new TestElement();
34     assertEquals(1, testElement.decorateCallCount);
35   });
36
37   test('decorateOnceDirectly', function() {
38     var testElement = document.createElement('div');
39     tvcm.ui.decorate(testElement, TestElement);
40     assertEquals(1, testElement.decorateCallCount);
41   });
42
43   test('componentToString', function() {
44     assertEquals('div', Base.toString());
45
46     var Sub = tvcm.ui.define('Sub', Base);
47     assertEquals('div::sub', Sub.toString());
48
49     var SubSub = tvcm.ui.define('Marine', Sub);
50     assertEquals('div::sub::marine', SubSub.toString());
51   });
52
53   test('basicDefines', function() {
54     var baseInstance = new Base();
55     assertTrue(baseInstance instanceof Base);
56     assertTrue(baseInstance.decoratedAsBase);
57
58     assertEquals(baseInstance.constructor, Base);
59     assertEquals(baseInstance.constructor.name, 'div');
60
61     baseInstance.basePropertySet = 7;
62     assertEquals(7, baseInstance.basePropertySet);
63   });
64
65   test('subclassing', function() {
66     var Sub = tvcm.ui.define('sub', Base);
67     Sub.prototype = {
68       __proto__: Base.prototype,
69       decorate: function() {
70         this.decoratedAsSub = true;
71       }
72     };
73
74     var subInstance = new Sub();
75     assertTrue(subInstance instanceof Sub);
76     assertTrue(subInstance.decoratedAsSub);
77
78     assertTrue(subInstance instanceof Base);
79     assertFalse(subInstance.decoratedAsBase);
80
81     assertEquals(subInstance.constructor, Sub);
82     assertEquals(subInstance.constructor.name, 'sub');
83
84     subInstance.baseProperty = true;
85     assertTrue(subInstance.basePropertySet);
86   });
87
88   var NoArgs = tvcm.ui.define('div');
89   NoArgs.prototype = {
90     __proto__: HTMLDivElement.prototype,
91     decorate: function() {
92       this.noArgsDecorated_ = true;
93     },
94     get noArgsDecorated() {
95       return this.noArgsDecorated_;
96     }
97   };
98
99   var Args = tvcm.ui.define('args', NoArgs);
100   Args.prototype = {
101     __proto__: NoArgs.prototype,
102     decorate: function(first) {
103       this.first_ = first;
104       this.argsDecorated_ = true;
105     },
106     get first() {
107       return this.first_;
108     },
109     get argsDecorated() {
110       return this.argsDecorated_;
111     }
112   };
113
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;
120     },
121     get second() {
122       return this.second_;
123     },
124     get decorated() {
125       return this.decorated_;
126     },
127     get argsChildDecorated() {
128       return this.argsChildDecorated_ = true;
129     }
130   };
131
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;
139     },
140     get second() {
141       return this.second_;
142     },
143     get decorated() {
144       return this.decorated_;
145     },
146     get argsDecoratingChildDecorated() {
147       return this.argsChildDecorated_ = true;
148     }
149   };
150
151   test('decorate_noArguments', function() {
152     var noArgs;
153     assertDoesNotThrow(function() {
154       noArgs = new NoArgs();
155     });
156     assertTrue(noArgs.noArgsDecorated);
157   });
158
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);
164   });
165
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);
170
171     assertTrue(argsChild.argsChildDecorated);
172     assertFalse(argsChild.argsDecorated);
173     assertFalse(argsChild.noArgsDecorated);
174   });
175
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);
184   });
185 });