1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
base.require('ui');
base.unittest.testSuite('ui', function() {
var TestElement = ui.define('div');
TestElement.prototype = {
__proto__: HTMLDivElement.prototype,
decorate: function() {
if (!this.decorateCallCount)
this.decorateCallCount = 0;
this.decorateCallCount++;
}
};
var Base = ui.define('div');
Base.prototype = {
__proto__: HTMLDivElement.prototype,
decorate: function() {
this.decoratedAsBase = true;
},
set baseProperty(v) {
this.basePropertySet = v;
}
};
test('decorateOnceViaNew', function() {
var testElement = new TestElement();
assertEquals(1, testElement.decorateCallCount);
});
test('decorateOnceDirectly', function() {
var testElement = document.createElement('div');
ui.decorate(testElement, TestElement);
assertEquals(1, testElement.decorateCallCount);
});
test('toString', function() {
assertEquals('div', Base.toString());
var Sub = ui.define('Sub', Base);
assertEquals('div::sub', Sub.toString());
var SubSub = ui.define('Marine', Sub);
assertEquals('div::sub::marine', SubSub.toString());
});
test('basicDefines', function() {
var baseInstance = new Base();
assertTrue(baseInstance instanceof Base);
assertTrue(baseInstance.decoratedAsBase);
assertEquals(baseInstance.constructor, Base);
assertEquals(baseInstance.constructor.name, 'div');
baseInstance.basePropertySet = 7;
assertEquals(7, baseInstance.basePropertySet);
});
test('subclassing', function() {
var Sub = ui.define('sub', Base);
Sub.prototype = {
__proto__: Base.prototype,
decorate: function() {
this.decoratedAsSub = true;
}
};
var subInstance = new Sub();
assertTrue(subInstance instanceof Sub);
assertTrue(subInstance.decoratedAsSub);
assertTrue(subInstance instanceof Base);
assertFalse(subInstance.decoratedAsBase);
assertEquals(subInstance.constructor, Sub);
assertEquals(subInstance.constructor.name, 'sub');
subInstance.baseProperty = true;
assertTrue(subInstance.basePropertySet);
});
var NoArgs = ui.define('div');
NoArgs.prototype = {
__proto__: HTMLDivElement.prototype,
decorate: function() {
this.noArgsDecorated_ = true;
},
get noArgsDecorated() {
return this.noArgsDecorated_;
}
};
var Args = ui.define('args', NoArgs);
Args.prototype = {
__proto__: NoArgs.prototype,
decorate: function(first) {
this.first_ = first;
this.argsDecorated_ = true;
},
get first() {
return this.first_;
},
get argsDecorated() {
return this.argsDecorated_;
}
};
var ArgsChild = ui.define('args-child', Args);
ArgsChild.prototype = {
__proto__: Args.prototype,
decorate: function(_, second) {
this.second_ = second;
this.argsChildDecorated_ = true;
},
get second() {
return this.second_;
},
get decorated() {
return this.decorated_;
},
get argsChildDecorated() {
return this.argsChildDecorated_ = true;
}
};
var ArgsDecoratingChild = ui.define('args-decorating-child', Args);
ArgsDecoratingChild.prototype = {
__proto__: Args.prototype,
decorate: function(first, second) {
Args.prototype.decorate.call(this, first);
this.second_ = second;
this.argsDecoratingChildDecorated_ = true;
},
get second() {
return this.second_;
},
get decorated() {
return this.decorated_;
},
get argsDecoratingChildDecorated() {
return this.argsChildDecorated_ = true;
}
};
test('decorate_noArguments', function() {
var noArgs;
assertDoesNotThrow(function() {
noArgs = new NoArgs();
});
assertTrue(noArgs.noArgsDecorated);
});
test('decorate_arguments', function() {
var args = new Args('this is first');
assertEquals('this is first', args.first);
assertTrue(args.argsDecorated);
assertFalse(args.noArgsDecorated);
});
test('decorate_subclassArguments', function() {
var argsChild = new ArgsChild('this is first', 'and second');
assertUndefined(argsChild.first);
assertEquals('and second', argsChild.second);
assertTrue(argsChild.argsChildDecorated);
assertFalse(argsChild.argsDecorated);
assertFalse(argsChild.noArgsDecorated);
});
test('decorate_subClassCallsParentDecorate', function() {
var argsDecoratingChild = new ArgsDecoratingChild(
'this is first', 'and second');
assertEquals('this is first', argsDecoratingChild.first);
assertEquals('and second', argsDecoratingChild.second);
assertTrue(argsDecoratingChild.argsDecoratingChildDecorated);
assertTrue(argsDecoratingChild.argsDecorated);
assertFalse(argsDecoratingChild.noArgsDecorated);
});
});
|