- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / platform_apps / windows_api / test.js
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 var callbackPass = chrome.test.callbackPass;
6
7 chrome.app.runtime.onLaunched.addListener(function() {
8   chrome.test.runTests([
9    function testCreateWindow() {
10      chrome.app.window.create('test.html',
11                               {id: 'testId'},
12                               callbackPass(function(win) {
13        chrome.test.assertEq(typeof win.contentWindow.window, 'object');
14        chrome.test.assertTrue(
15          typeof win.contentWindow.document !== 'undefined');
16        chrome.test.assertFalse(
17          'about:blank' === win.contentWindow.location.href);
18        var cw = win.contentWindow.chrome.app.window.current();
19        chrome.test.assertEq(cw, win);
20        chrome.test.assertEq('testId', cw.id);
21        win.contentWindow.close();
22      }));
23    },
24
25    function testCreateBadWindow() {
26      chrome.app.window.create('404.html', callbackPass(function(win) {
27        chrome.test.assertTrue(typeof win === 'undefined');
28      }));
29    },
30
31    function testOnLoad() {
32      chrome.app.window.create('test.html', callbackPass(function(win) {
33        win.contentWindow.onload = callbackPass(function() {
34          chrome.test.assertEq(document.readyState, 'complete');
35          win.contentWindow.close();
36        });
37      }));
38    },
39
40    function testCreateMultiWindow() {
41      chrome.test.assertTrue(null === chrome.app.window.current());
42      chrome.app.window.create('test.html',
43                               {id: 'testId1'},
44                               callbackPass(function(win1) {
45        chrome.app.window.create('test.html',
46                                 {id: 'testId2'},
47                                 callbackPass(function(win2) {
48          var cw1 = win1.contentWindow.chrome.app.window.current();
49          var cw2 = win2.contentWindow.chrome.app.window.current();
50          chrome.test.assertEq('testId1', cw1.id);
51          chrome.test.assertEq('testId2', cw2.id);
52          chrome.test.assertTrue(cw1 === win1);
53          chrome.test.assertTrue(cw2 === win2);
54          chrome.test.assertFalse(cw1 === cw2);
55          win1.contentWindow.close();
56          win2.contentWindow.close();
57        }));
58      }));
59    },
60
61    function testCreateWindowContentSize() {
62      chrome.app.window.create('test.html',
63          { bounds: { width: 250, height: 200 } }, callbackPass(function(win) {
64        chrome.test.assertEq(250, win.contentWindow.innerWidth);
65        chrome.test.assertEq(200, win.contentWindow.innerHeight);
66        win.close();
67      }));
68    },
69
70    function testSetBoundsContentSize() {
71      chrome.app.window.create('test.html',
72          { bounds: { width: 250, height: 200 } }, callbackPass(function(win) {
73        var b = win.getBounds();
74        win.setBounds({width: 400, height: 450})
75        // Listen to onresize here rather than win.onBoundsChanged, because
76        // onBoundsChanged is fired before the web contents are resized.
77        win.contentWindow.onresize = callbackPass(function() {
78          win.contentWindow.onresize = undefined;
79          chrome.test.assertEq(400, win.contentWindow.innerWidth);
80          chrome.test.assertEq(450, win.contentWindow.innerHeight);
81          win.close();
82        });
83      }));
84    },
85
86    function testOnClosedEvent() {
87      chrome.app.window.create('test.html', callbackPass(function(win) {
88        win.onClosed.addListener(callbackPass(function() {
89          // Mission accomplished.
90        }));
91        win.contentWindow.close();
92      }));
93    },
94
95    function testMinSize() {
96      chrome.app.window.create('test.html', {
97        bounds: { width: 250, height: 250 },
98        minWidth: 400, minHeight: 450
99      }, callbackPass(function(win) {
100        var w = win.contentWindow;
101        chrome.test.assertEq(400, w.innerWidth);
102        chrome.test.assertEq(450, w.innerHeight);
103        w.close();
104      }));
105    },
106
107    function testMaxSize() {
108      chrome.app.window.create('test.html', {
109        bounds: { width: 250, height: 250 },
110        maxWidth: 200, maxHeight: 150
111      }, callbackPass(function(win) {
112        var w = win.contentWindow;
113        chrome.test.assertEq(200, w.innerWidth);
114        chrome.test.assertEq(150, w.innerHeight);
115        w.close();
116      }));
117    },
118
119    function testMinAndMaxSize() {
120      chrome.app.window.create('test.html', {
121        bounds: { width: 250, height: 250 },
122        minWidth: 400, minHeight: 450,
123        maxWidth: 200, maxHeight: 150
124      }, callbackPass(function(win) {
125        var w = win.contentWindow;
126        chrome.test.assertEq(400, w.innerWidth);
127        chrome.test.assertEq(450, w.innerHeight);
128        w.close();
129      }));
130    },
131
132    function testUndefinedMinAndMaxSize() {
133      chrome.app.window.create('test.html', {
134        bounds: { width: 250, height: 250 }
135      }, callbackPass(function(win) {
136        chrome.test.assertEq(undefined, win.getMinWidth());
137        chrome.test.assertEq(undefined, win.getMinHeight());
138        chrome.test.assertEq(undefined, win.getMaxWidth());
139        chrome.test.assertEq(undefined, win.getMaxHeight());
140        win.close();
141      }));
142    },
143
144    function testSetUndefinedMinAndMaxSize() {
145      chrome.app.window.create('test.html', {
146        bounds: { width: 102, height: 103 },
147        minWidth: 100, minHeight: 101,
148        maxWidth: 104, maxHeight: 105
149      }, callbackPass(function(win) {
150        chrome.test.assertEq(100, win.getMinWidth());
151        chrome.test.assertEq(101, win.getMinHeight());
152        chrome.test.assertEq(104, win.getMaxWidth());
153        chrome.test.assertEq(105, win.getMaxHeight());
154        win.setMinWidth(null);
155        win.setMinHeight(null);
156        win.setMaxWidth(null);
157        win.setMaxHeight(null);
158        // We need to wait for an onBoundsChanged to ensure the size has been
159        // updated.
160        win.setBounds({ width: 103, height: 102 });
161        var cb;
162        win.onBoundsChanged.addListener(cb = callbackPass(function() {
163          chrome.test.assertEq(undefined, win.getMinWidth());
164          chrome.test.assertEq(undefined, win.getMinHeight());
165          chrome.test.assertEq(undefined, win.getMaxWidth());
166          chrome.test.assertEq(undefined, win.getMaxHeight());
167          win.close();
168        }));
169      }));
170    },
171
172    function testChangingMinAndMaxSize() {
173      chrome.app.window.create('test.html', {
174        bounds: { width: 102, height: 103 },
175        minWidth: 100, minHeight: 101,
176        maxWidth: 104, maxHeight: 105
177      }, callbackPass(function(win) {
178        chrome.test.assertEq(100, win.getMinWidth());
179        chrome.test.assertEq(101, win.getMinHeight());
180        chrome.test.assertEq(104, win.getMaxWidth());
181        chrome.test.assertEq(105, win.getMaxHeight());
182        win.setMinWidth(98);
183        win.setMinHeight(99);
184        win.setMaxWidth(106);
185        win.setMaxHeight(107);
186        // We need to wait for an onBoundsChanged to ensure the size has been
187        // updated.
188        win.setBounds({ width: 103, height: 102 });
189        var cb;
190        win.onBoundsChanged.addListener(cb = callbackPass(function() {
191          chrome.test.assertEq(98, win.getMinWidth());
192          chrome.test.assertEq(99, win.getMinHeight());
193          chrome.test.assertEq(106, win.getMaxWidth());
194          chrome.test.assertEq(107, win.getMaxHeight());
195          win.close();
196        }));
197      }));
198    },
199
200    function testMinWidthLargerThanMaxWidth() {
201      chrome.app.window.create('test.html', {
202        bounds: { width: 102, height: 103 },
203        minWidth: 100, minHeight: 101,
204        maxWidth: 104, maxHeight: 105
205      }, callbackPass(function(win) {
206        win.setMinWidth(200);
207        // An onBoundsChanged will be fired because this resizes the window.
208        var cb;
209        win.onBoundsChanged.addListener(cb = callbackPass(function() {
210          chrome.test.assertEq(200, win.getMinWidth());
211          chrome.test.assertEq(101, win.getMinHeight());
212          chrome.test.assertEq(200, win.getMaxWidth());
213          chrome.test.assertEq(105, win.getMaxHeight());
214          win.close();
215        }));
216      }));
217    },
218
219    function testMinHeightLargerThanMaxHeight() {
220      chrome.app.window.create('test.html', {
221        bounds: { width: 102, height: 103 },
222        minWidth: 100, minHeight: 101,
223        maxWidth: 104, maxHeight: 105
224      }, callbackPass(function(win) {
225        win.setMinHeight(200);
226        // An onBoundsChanged will be fired because this resizes the window.
227        var cb;
228        win.onBoundsChanged.addListener(cb = callbackPass(function() {
229          chrome.test.assertEq(100, win.getMinWidth());
230          chrome.test.assertEq(200, win.getMinHeight());
231          chrome.test.assertEq(104, win.getMaxWidth());
232          chrome.test.assertEq(200, win.getMaxHeight());
233          win.close();
234        }));
235      }));
236    },
237
238    function testMaxWidthSmallerThanMinWidth() {
239      chrome.app.window.create('test.html', {
240        bounds: { width: 102, height: 103 },
241        minWidth: 100, minHeight: 101,
242        maxWidth: 104, maxHeight: 105
243      }, callbackPass(function(win) {
244        win.setMaxWidth(50);
245        // An onBoundsChanged will be fired because this resizes the window.
246        var cb;
247        win.onBoundsChanged.addListener(cb = callbackPass(function() {
248          chrome.test.assertEq(100, win.getMinWidth());
249          chrome.test.assertEq(101, win.getMinHeight());
250          chrome.test.assertEq(100, win.getMaxWidth());
251          chrome.test.assertEq(105, win.getMaxHeight());
252          win.close();
253        }));
254      }));
255    },
256
257    function testMaxHeightSmallerThanMinHeight() {
258      chrome.app.window.create('test.html', {
259        bounds: { width: 102, height: 103 },
260        minWidth: 100, minHeight: 101,
261        maxWidth: 104, maxHeight: 105
262      }, callbackPass(function(win) {
263        win.setMaxHeight(50);
264        // An onBoundsChanged will be fired because this resizes the window.
265        var cb;
266        win.onBoundsChanged.addListener(cb = callbackPass(function() {
267          chrome.test.assertEq(100, win.getMinWidth());
268          chrome.test.assertEq(101, win.getMinHeight());
269          chrome.test.assertEq(104, win.getMaxWidth());
270          chrome.test.assertEq(101, win.getMaxHeight());
271          win.close();
272        }));
273      }));
274    },
275
276    function testMinSizeRestore() {
277      chrome.app.window.create('test.html', {
278        bounds: { width: 100, height: 150 },
279        minWidth: 200, minHeight: 250,
280        maxWidth: 200, maxHeight: 250,
281        id: 'test-id', singleton: false
282      }, callbackPass(function(win) {
283        var w = win.contentWindow;
284        chrome.test.assertEq(200, w.innerWidth);
285        chrome.test.assertEq(250, w.innerHeight);
286        w.close();
287
288        chrome.app.window.create('test.html', {
289          bounds: { width: 500, height: 550 },
290          minWidth: 400, minHeight: 450,
291          maxWidth: 600, maxHeight: 650,
292          id: 'test-id', singleton: false
293        }, callbackPass(function(win) {
294          var w = win.contentWindow;
295          chrome.test.assertEq(400, w.innerWidth);
296          chrome.test.assertEq(450, w.innerHeight);
297          w.close();
298        }));
299      }));
300    },
301
302    function testSingleton() {
303      chrome.app.window.create('test.html', {
304        id: 'singleton-id'
305      }, callbackPass(function(win) {
306        var w = win.contentWindow;
307
308        chrome.app.window.create('test.html', {
309          id: 'singleton-id'
310        }, callbackPass(function(win) {
311          var w2 = win.contentWindow;
312
313          chrome.test.assertTrue(w === w2);
314
315          chrome.app.window.create('test.html', {
316            id: 'singleton-id', singleton: false
317          }, callbackPass(function(win) {
318            var w3 = win.contentWindow;
319
320            chrome.test.assertFalse(w === w3);
321
322            w.close();
323            w2.close();
324            w3.close();
325          }));
326        }));
327      }));
328    },
329
330    function testPartialSetBounds() {
331      chrome.app.window.create('test.html', {
332        bounds: { width: 512, height: 256 }
333      }, callbackPass(function(win) {
334        win.setBounds({ height: 348 });
335        var cb;
336        win.onBoundsChanged.addListener(cb = callbackPass(function() {
337          chrome.test.assertEq(348, win.getBounds().height);
338          win.close();
339        }));
340      }));
341    },
342
343    /*function testMaximize() {
344      chrome.app.window.create('test.html', {width: 200, height: 200},
345          callbackPass(function(win) {
346            win.onresize = callbackPass(function(e) {
347              // Crude test to check we're somewhat maximized.
348              chrome.test.assertTrue(
349                  win.outerHeight > screen.availHeight * 0.8);
350              chrome.test.assertTrue(
351                  win.outerWidth > screen.availWidth * 0.8);
352            });
353            win.chrome.app.window.maximize();
354          }));
355    },*/
356
357    /*function testRestore() {
358      chrome.app.window.create('test.html', {width: 200, height: 200},
359          callbackPass(function(win) {
360            var oldWidth = win.innerWidth;
361            var oldHeight = win.innerHeight;
362            win.onresize = callbackPass(function() {
363              chrome.test.assertTrue(win.innerWidth != oldWidth);
364              chrome.test.assertTrue(win.innerHeight != oldHeight);
365              // Seems like every time we resize, we get two resize events.
366              // See http://crbug.com/133869.
367              win.onresize = callbackPass(function() {
368                // Ignore the immediately following resize, as it's a clone of
369                // the one we just got.
370                win.onresize = callbackPass(function() {
371                  chrome.test.assertEq(oldWidth, win.innerWidth);
372                  chrome.test.assertEq(oldHeight, win.innerHeight);
373                });
374              })
375              win.chrome.app.window.restore();
376            });
377            win.chrome.app.window.maximize();
378          }));
379    },*/
380   ]);
381 });