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
);
267 * Executes a function given a potentially namespaced function name, e.g.,
268 * cr.webUIListenerCallback.
269 * @param {string} functionName The name of the function, including any
270 * namespaces, to execute.
272 function executeFunctionByName(functionName
) {
273 var args
= Array
.prototype.slice
.call(arguments
, 1);
274 var func
= (0, eval
)(functionName
);
275 func
.apply(undefined, args
);
279 * Tests that cr.sendWithCallback correctly handles the case where the JS sends
280 * no arguments to the WebUI handler.
282 function testSendWithCallback_PassesJSArgs() {
283 var testMethodName
= 'getFullscreenState';
285 // Mock out chrome.send to emulate a WebUI handler calling back with the
286 // result of a getFullscreenState call.
288 window
.chrome
.send = function(method
, args
) {
289 assertEquals(testMethodName
, method
);
290 var callbackName
= args
[0];
292 executeFunctionByName(callbackName
, id
, /* fullscreen */ true);
295 var callbackResponse
;
296 cr
.sendWithCallback(testMethodName
, undefined, function(fullscreen
) {
297 callbackResponse
= fullscreen
;
300 assertTrue(callbackResponse
);
304 * Tests that cr.sendWithCallback passes arguments from JS to the WebUI
307 function testSendWithCallback_PassesJSArgs() {
308 var testMethodName
= 'getSquareOfX';
310 // Mock out chrome.send to emulate a WebUI handler calling back with the
311 // result of a getSquareOfX call.
313 window
.chrome
.send = function(method
, args
) {
314 assertEquals(testMethodName
, method
);
315 var callbackName
= args
[0];
318 executeFunctionByName(callbackName
, id
, x
* x
);
321 var callbackResponse
;
322 cr
.sendWithCallback(testMethodName
, [5], function(square
) {
323 callbackResponse
= square
;
326 assertEquals(25, callbackResponse
);
330 * Tests that an event fired by a WebUI handler is sent to all listeners.
332 function testAddWebUIListener() {
333 var responses
= new Map();
334 cr
.addWebUIListener('fullscreen-enabled', function(enabled
) {
335 responses
.set('first', enabled
);
337 cr
.addWebUIListener('fullscreen-enabled', function(enabled
) {
338 responses
.set('second', enabled
);
341 executeFunctionByName(
342 'cr.webUIListenerCallback', 'fullscreen-enabled', true);
344 assertTrue(responses
.get('first'));
345 assertTrue(responses
.get('second'));