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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
</style>
<script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script>
<script src="cr.js"></script>
<script src="cr/event_target.js"></script>
<script>
goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>
const EventTarget = cr.EventTarget;
function testDefineProperty() {
var obj = new EventTarget;
cr.defineProperty(obj, 'test');
obj.test = 1;
assertEquals(1, obj.test);
assertEquals(1, obj.test_);
}
function testDefinePropertyOnClass() {
function C() {}
C.prototype = {
__proto__: EventTarget.prototype
};
cr.defineProperty(C, 'test');
var obj = new C;
obj.test = 1;
assertEquals(1, obj.test);
assertEquals(1, obj.test_);
}
function testDefinePropertyWithDefault() {
var obj = new EventTarget;
cr.defineProperty(obj, 'test', null, 1);
assertEquals(1, obj.test);
assertEquals(1, obj.test_);
obj.test = 2;
assertEquals(2, obj.test);
assertEquals(2, obj.test_);
}
function testDefinePropertyEvent() {
var obj = new EventTarget;
cr.defineProperty(obj, 'test');
obj.test = 1;
var count = 0;
function f(e) {
assertEquals('testChange', e.type);
assertEquals('test', e.propertyName);
assertEquals(1, e.oldValue);
assertEquals(2, e.newValue);
count++;
}
obj.addEventListener('testChange', f);
obj.test = 2;
assertEquals(2, obj.test);
assertEquals('Should have called the property change listener', 1, count);
obj.test = 2;
assertEquals(1, count);
}
function testDefinePropertyEventWithDefault() {
var obj = new EventTarget;
cr.defineProperty(obj, 'test', cr.PropertyKind.JS, 1);
var count = 0;
function f(e) {
assertEquals('testChange', e.type);
assertEquals('test', e.propertyName);
assertEquals(1, e.oldValue);
assertEquals(2, e.newValue);
count++;
}
obj.addEventListener('testChange', f);
obj.test = 1;
assertEquals('Should not have called the property change listener', 0, count);
obj.test = 2;
assertEquals(2, obj.test);
assertEquals('Should have called the property change listener', 1, count);
obj.test = 2;
assertEquals(1, count);
}
function testDefinePropertyAttr() {
var obj = document.createElement('div');
cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR);
obj.test = 'a';
assertEquals('a', obj.test);
assertEquals('a', obj.getAttribute('test'));
}
function testDefinePropertyAttrOnClass() {
var obj = document.createElement('button');
cr.defineProperty(HTMLButtonElement, 'test', cr.PropertyKind.ATTR);
obj.test = 'a';
assertEquals('a', obj.test);
assertEquals('a', obj.getAttribute('test'));
}
function testDefinePropertyAttrWithDefault() {
var obj = document.createElement('div');
cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR, 'a');
assertEquals('a', obj.test);
assertFalse(obj.hasAttribute('test'));
obj.test = 'b';
assertEquals('b', obj.test);
assertEquals('b', obj.getAttribute('test'));
}
function testDefinePropertyAttrEvent() {
var obj = document.createElement('div');
cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR);
obj.test = 'a';
var count = 0;
function f(e) {
assertEquals('testChange', e.type);
assertEquals('test', e.propertyName);
assertEquals('a', e.oldValue);
assertEquals('b', e.newValue);
count++;
}
obj.addEventListener('testChange', f);
obj.test = 'b';
assertEquals('b', obj.test);
assertEquals('Should have called the property change listener', 1, count);
obj.test = 'b';
assertEquals(1, count);
}
function testDefinePropertyAttrEventWithDefault() {
var obj = document.createElement('div');
cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR, 'a');
var count = 0;
function f(e) {
assertEquals('testChange', e.type);
assertEquals('test', e.propertyName);
assertEquals('a', e.oldValue);
assertEquals('b', e.newValue);
count++;
}
obj.addEventListener('testChange', f);
obj.test = 'a';
assertEquals('Should not have called the property change listener', 0, count);
obj.test = 'b';
assertEquals('b', obj.test);
assertEquals('Should have called the property change listener', 1, count);
obj.test = 'b';
assertEquals(1, count);
}
function testDefinePropertyBoolAttr() {
var obj = document.createElement('div');
cr.defineProperty(obj, 'test', cr.PropertyKind.BOOL_ATTR);
assertFalse(obj.test);
assertFalse(obj.hasAttribute('test'));
obj.test = true;
assertTrue(obj.test);
assertTrue(obj.hasAttribute('test'));
obj.test = false;
assertFalse(obj.test);
assertFalse(obj.hasAttribute('test'));
}
function testDefinePropertyBoolAttrEvent() {
var obj = document.createElement('div');
cr.defineProperty(obj, 'test', cr.PropertyKind.BOOL_ATTR);
var count = 0;
function f(e) {
assertEquals('testChange', e.type);
assertEquals('test', e.propertyName);
assertEquals(false, e.oldValue);
assertEquals(true, e.newValue);
count++;
}
obj.addEventListener('testChange', f);
obj.test = true;
assertTrue(obj.test);
assertEquals('Should have called the property change listener', 1, count);
obj.test = true;
assertEquals(1, count);
}
function testAddSingletonGetter() {
function Foo() {};
cr.addSingletonGetter(Foo);
assertNotNull('Should add get instance function', Foo.getInstance);
var x = Foo.getInstance();
assertNotNull('Should successfully create an object', x);
var y = Foo.getInstance();
assertEquals('Should return the same object', x, y);
delete Foo.instance_;
var z = Foo.getInstance();
assertNotNull('Should work after clearing for testing', z);
assertNotEquals('Should return a different object after clearing for testing',
x, z);
}
</script>
</body>
</html>
|