Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / test / data / webui / cr_test.html
blobb592cac18ffba3e36ef4bf32cd5ff315bf8d74d1
1 <!DOCTYPE html>
2 <html>
3 <body>
5 <script>
7 var EventTarget;
9 function setUp() {
10 EventTarget = cr.EventTarget;
13 function testDefineProperty() {
14 var obj = new EventTarget;
15 cr.defineProperty(obj, 'test');
17 obj.test = 1;
18 assertEquals(1, obj.test);
19 assertEquals(1, obj.test_);
22 function testDefinePropertyOnClass() {
23 function C() {}
24 C.prototype = {
25 __proto__: EventTarget.prototype
28 cr.defineProperty(C, 'test');
30 var obj = new C;
31 assertEquals(undefined, obj.test);
33 obj.test = 1;
34 assertEquals(1, obj.test);
35 assertEquals(1, obj.test_);
38 function testDefinePropertyWithSetter() {
39 var obj = new EventTarget;
41 var hit = false;
42 function onTestSet(value, oldValue) {
43 assertEquals(obj, this);
44 assertEquals(2, this.test);
45 assertEquals(undefined, oldValue);
46 assertEquals(2, value);
47 hit = true;
49 cr.defineProperty(obj, 'test', cr.PropertyKind.JS, onTestSet);
50 obj.test = 2;
51 assertTrue(hit);
54 function testDefinePropertyEvent() {
55 var obj = new EventTarget;
56 cr.defineProperty(obj, 'test');
57 obj.test = 1;
59 var count = 0;
60 function f(e) {
61 assertEquals('testChange', e.type);
62 assertEquals('test', e.propertyName);
63 assertEquals(1, e.oldValue);
64 assertEquals(2, e.newValue);
65 count++;
68 obj.addEventListener('testChange', f);
69 obj.test = 2;
70 assertEquals(2, obj.test);
71 assertEquals(1, count, 'Should have called the property change listener');
73 obj.test = 2;
74 assertEquals(1, count);
77 function testDefinePropertyEventWithDefault() {
78 var obj = new EventTarget;
79 cr.defineProperty(obj, 'test', cr.PropertyKind.JS);
81 var count = 0;
82 function f(e) {
83 assertEquals('testChange', e.type);
84 assertEquals('test', e.propertyName);
85 assertEquals(undefined, e.oldValue);
86 assertEquals(2, e.newValue);
87 count++;
90 obj.addEventListener('testChange', f);
92 obj.test = undefined;
93 assertEquals(0, count, 'Should not have called the property change listener');
95 obj.test = 2;
96 assertEquals(2, obj.test);
97 assertEquals(1, count, 'Should have called the property change listener');
99 obj.test = 2;
100 assertEquals(1, count);
103 function testDefinePropertyAttr() {
104 var obj = document.createElement('div');
105 cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR);
107 obj.test = 'a';
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);
122 obj.test = 'a';
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');
134 var hit = false;
135 function onTestSet(value, oldValue) {
136 assertEquals(obj, this);
137 assertEquals(null, oldValue);
138 assertEquals('b', value);
139 assertEquals('b', this.test);
140 hit = true;
142 cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR, onTestSet);
143 obj.test = 'b';
144 assertTrue(hit);
147 function testDefinePropertyAttrEvent() {
148 var obj = document.createElement('div');
149 cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR);
151 var count = 0;
152 function f(e) {
153 assertEquals('testChange', e.type);
154 assertEquals('test', e.propertyName);
155 assertEquals(null, e.oldValue);
156 assertEquals('b', e.newValue);
157 count++;
160 obj.addEventListener('testChange', f);
162 obj.test = null;
163 assertEquals(0, count, 'Should not have called the property change listener');
165 obj.test = 'b';
166 assertEquals('b', obj.test);
167 assertEquals(1, count, 'Should have called the property change listener');
169 obj.test = 'b';
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'));
180 obj.test = true;
181 assertTrue(obj.test);
182 assertTrue(obj.hasAttribute('test'));
184 obj.test = false;
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);
193 var count = 0;
194 function f(e) {
195 assertEquals('testChange', e.type);
196 assertEquals('test', e.propertyName);
197 assertEquals(false, e.oldValue);
198 assertEquals(true, e.newValue);
199 count++;
202 obj.addEventListener('testChange', f);
203 obj.test = true;
204 assertTrue(obj.test);
205 assertEquals(1, count, 'Should have called the property change listener');
207 obj.test = true;
208 assertEquals(1, count);
211 function testDefinePropertyBoolAttrEvent() {
212 var obj = document.createElement('div');
213 var hit = false;
214 function onTestSet(value, oldValue) {
215 assertEquals(obj, this);
216 assertTrue(this.test);
217 assertFalse(oldValue);
218 assertTrue(value);
219 hit = true;
221 cr.defineProperty(obj, 'test', cr.PropertyKind.BOOL_ATTR, onTestSet);
222 obj.test = true;
223 assertTrue(hit);
226 function testAddSingletonGetter() {
227 function Foo() {};
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');
246 assertNotEqual(x, z,
247 'Should return a different object after clearing for testing');
250 function testDefineWithGetter() {
251 var v = 0;
252 cr.define('foo', function() {
253 return {
254 get v() {
255 return v;
260 assertEquals(0, foo.v);
262 v = 1;
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.
287 window.chrome = {};
288 window.chrome.send = function(method, args) {
289 assertEquals(testMethodName, method);
290 var callbackName = args[0];
291 var id = args[1];
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
305 * handler.
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.
312 window.chrome = {};
313 window.chrome.send = function(method, args) {
314 assertEquals(testMethodName, method);
315 var callbackName = args[0];
316 var id = args[1];
317 var x = args[2];
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'));
348 </script>
350 </body>
351 </html>