10 EventTarget
= cr
.EventTarget
;
13 function testDefineProperty() {
14 var obj
= new EventTarget
;
15 cr
.defineProperty(obj
, 'test');
18 assertEquals(1, obj
.test
);
19 assertEquals(1, obj
.test_
);
22 function testDefinePropertyOnClass() {
25 __proto__
: EventTarget
.prototype
28 cr
.defineProperty(C
, 'test');
31 assertEquals(undefined, obj
.test
);
34 assertEquals(1, obj
.test
);
35 assertEquals(1, obj
.test_
);
38 function testDefinePropertyWithSetter() {
39 var obj
= new EventTarget
;
42 function onTestSet(value
, oldValue
) {
43 assertEquals(obj
, this);
44 assertEquals(2, this.test
);
45 assertEquals(undefined, oldValue
);
46 assertEquals(2, value
);
49 cr
.defineProperty(obj
, 'test', cr
.PropertyKind
.JS
, onTestSet
);
54 function testDefinePropertyEvent() {
55 var obj
= new EventTarget
;
56 cr
.defineProperty(obj
, 'test');
61 assertEquals('testChange', e
.type
);
62 assertEquals('test', e
.propertyName
);
63 assertEquals(1, e
.oldValue
);
64 assertEquals(2, e
.newValue
);
68 obj
.addEventListener('testChange', f
);
70 assertEquals(2, obj
.test
);
71 assertEquals(1, count
, 'Should have called the property change listener');
74 assertEquals(1, count
);
77 function testDefinePropertyEventWithDefault() {
78 var obj
= new EventTarget
;
79 cr
.defineProperty(obj
, 'test', cr
.PropertyKind
.JS
);
83 assertEquals('testChange', e
.type
);
84 assertEquals('test', e
.propertyName
);
85 assertEquals(undefined, e
.oldValue
);
86 assertEquals(2, e
.newValue
);
90 obj
.addEventListener('testChange', f
);
93 assertEquals(0, count
, 'Should not have called the property change listener');
96 assertEquals(2, obj
.test
);
97 assertEquals(1, count
, 'Should have called the property change listener');
100 assertEquals(1, count
);
103 function testDefinePropertyAttr() {
104 var obj
= document
.createElement('div');
105 cr
.defineProperty(obj
, 'test', cr
.PropertyKind
.ATTR
);
108 assertEquals('a', obj
.test
);
109 assertEquals('a', obj
.getAttribute('test'));
111 obj
.test
= undefined;
112 assertEquals(null, obj
.test
);
113 assertFalse(obj
.hasAttribute('test'));
116 function testDefinePropertyAttrOnClass() {
117 var obj
= document
.createElement('button');
118 cr
.defineProperty(HTMLButtonElement
, 'test', cr
.PropertyKind
.ATTR
);
120 assertEquals(null, obj
.test
);
123 assertEquals('a', obj
.test
);
124 assertEquals('a', obj
.getAttribute('test'));
126 obj
.test
= undefined;
127 assertEquals(null, obj
.test
);
128 assertFalse(obj
.hasAttribute('test'));
131 function testDefinePropertyAttrWithSetter() {
132 var obj
= document
.createElement('div');
135 function onTestSet(value
, oldValue
) {
136 assertEquals(obj
, this);
137 assertEquals(null, oldValue
);
138 assertEquals('b', value
);
139 assertEquals('b', this.test
);
142 cr
.defineProperty(obj
, 'test', cr
.PropertyKind
.ATTR
, onTestSet
);
147 function testDefinePropertyAttrEvent() {
148 var obj
= document
.createElement('div');
149 cr
.defineProperty(obj
, 'test', cr
.PropertyKind
.ATTR
);
153 assertEquals('testChange', e
.type
);
154 assertEquals('test', e
.propertyName
);
155 assertEquals(null, e
.oldValue
);
156 assertEquals('b', e
.newValue
);
160 obj
.addEventListener('testChange', f
);
163 assertEquals(0, count
, 'Should not have called the property change listener');
166 assertEquals('b', obj
.test
);
167 assertEquals(1, count
, 'Should have called the property change listener');
170 assertEquals(1, count
);
173 function testDefinePropertyBoolAttr() {
174 var obj
= document
.createElement('div');
175 cr
.defineProperty(obj
, 'test', cr
.PropertyKind
.BOOL_ATTR
);
177 assertFalse(obj
.test
);
178 assertFalse(obj
.hasAttribute('test'));
181 assertTrue(obj
.test
);
182 assertTrue(obj
.hasAttribute('test'));
185 assertFalse(obj
.test
);
186 assertFalse(obj
.hasAttribute('test'));
189 function testDefinePropertyBoolAttrEvent() {
190 var obj
= document
.createElement('div');
191 cr
.defineProperty(obj
, 'test', cr
.PropertyKind
.BOOL_ATTR
);
195 assertEquals('testChange', e
.type
);
196 assertEquals('test', e
.propertyName
);
197 assertEquals(false, e
.oldValue
);
198 assertEquals(true, e
.newValue
);
202 obj
.addEventListener('testChange', f
);
204 assertTrue(obj
.test
);
205 assertEquals(1, count
, 'Should have called the property change listener');
208 assertEquals(1, count
);
211 function testDefinePropertyBoolAttrEvent() {
212 var obj
= document
.createElement('div');
214 function onTestSet(value
, oldValue
) {
215 assertEquals(obj
, this);
216 assertTrue(this.test
);
217 assertFalse(oldValue
);
221 cr
.defineProperty(obj
, 'test', cr
.PropertyKind
.BOOL_ATTR
, onTestSet
);
226 function testAddSingletonGetter() {
228 cr
.addSingletonGetter(Foo
);
230 assertEquals('function', typeof Foo
.getInstance
,
231 'Should add get instance function');
233 var x
= Foo
.getInstance();
234 assertEquals('object', typeof x
, 'Should successfully create an object');
235 assertNotEqual(null, x
, 'Created object should not be null');
237 var y
= Foo
.getInstance();
238 assertEquals(x
, y
, 'Should return the same object');
240 delete Foo
.instance_
;
242 var z
= Foo
.getInstance();
243 assertEquals('object', typeof z
, 'Should work after clearing for testing');
244 assertNotEqual(null, z
, 'Created object should not be null');
247 'Should return a different object after clearing for testing');
250 function testDefineWithGetter() {
252 cr
.define('foo', function() {
260 assertEquals(0, foo
.v
);
263 assertEquals(1, foo
.v
);