Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / platform_apps / window_api_interactive / test.js
1 // Copyright 2013 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 function testWindowGetsFocus(win) {
8   // If the window is already focused, we are done.
9   if (win.contentWindow.document.hasFocus()) {
10     chrome.test.assertTrue(win.contentWindow.document.hasFocus(),
11                            "window has been focused");
12     win.close();
13     return;
14   }
15
16   // Otherwise, we wait for the focus event.
17   win.contentWindow.onfocus = callbackPass(function() {
18     chrome.test.assertTrue(win.contentWindow.document.hasFocus(),
19                            "window has been focused");
20     win.close();
21   });
22 }
23
24 function testWindowNeverGetsFocus(win) {
25   win.contentWindow.onfocus = function() {
26     chrome.test.assertFalse(win.contentWindow.document.hasFocus(),
27                             "window should not be focused");
28     win.close();
29   };
30
31   if (win.contentWindow.document.hasFocus()) {
32     chrome.test.assertFalse(win.contentWindow.document.hasFocus(),
33                             "window should not be focused");
34     win.close();
35     return;
36   };
37
38   if (win.contentWindow.document.readyState == 'complete') {
39     chrome.test.assertFalse(win.contentWindow.document.hasFocus(),
40                             "window should not be focused");
41     win.close();
42     return;
43   }
44
45   win.contentWindow.onload = callbackPass(function() {
46     chrome.test.assertFalse(win.contentWindow.document.hasFocus(),
47                             "window should not be focused");
48     win.close();
49   });
50 }
51
52 function testCreate() {
53   chrome.test.runTests([
54     function createUnfocusedWindow() {
55       chrome.app.window.create('test.html', {
56         innerBounds: { width: 200, height: 200 },
57         focused: false
58       }, callbackPass(testWindowNeverGetsFocus));
59     },
60     function createTwiceUnfocused() {
61       chrome.app.window.create('test.html', {
62         id: 'createTwiceUnfocused', focused: false,
63         innerBounds: { width: 200, height: 200 }
64       }, callbackPass(function(win) {
65         win.contentWindow.onload = callbackPass(function() {
66           chrome.app.window.create('test.html', {
67             id: 'createTwiceUnfocused', focused: false,
68             innerBounds: { width: 200, height: 200 }
69           }, callbackPass(testWindowNeverGetsFocus));
70         });
71       }));
72     },
73     function createFocusedWindow() {
74       chrome.app.window.create('test.html', {
75         innerBounds: { width: 200, height: 200 },
76         focused: true
77       }, callbackPass(testWindowGetsFocus));
78     },
79     function createDefaultFocusStateWindow() {
80       chrome.app.window.create('test.html', {
81         innerBounds: { width: 200, height: 200 },
82       }, callbackPass(testWindowGetsFocus));
83     },
84     function createTwiceFocusUnfocus() {
85       chrome.app.window.create('test.html', {
86         id: 'createTwiceFocusUnfocus', focused: true,
87         innerBounds: { width: 200, height: 200 }
88       }, callbackPass(function(win) {
89         win.contentWindow.onload = callbackPass(function() {
90           chrome.app.window.create('test.html', {
91             id: 'createTwiceFocusUnfocus', focused: false,
92             innerBounds: { width: 200, height: 200 }
93           }, callbackPass(testWindowGetsFocus));
94         });
95       }));
96     },
97     function createTwiceUnfocusFocus() {
98       chrome.app.window.create('test.html', {
99         id: 'createTwiceUnfocusFocus', focused: false,
100         innerBounds: { width: 200, height: 200 }
101       }, callbackPass(function(win) {
102         win.contentWindow.onload = callbackPass(function() {
103           chrome.app.window.create('test.html', {
104             id: 'createTwiceUnfocusFocus', focused: true,
105             innerBounds: { width: 200, height: 200 }
106           }, callbackPass(function() {
107             // This test fails on Linux GTK, see http://crbug.com/325219
108             // And none of those tests run on Linux Aura, see
109             // http://crbug.com/325142
110             // We remove this and disable the entire test for Linux GTK when the
111             // test will run on other platforms, see http://crbug.com/328829
112             if (navigator.platform.indexOf('Linux') != 0)
113               testWindowGetsFocus(win);
114           }));
115         });
116       }));
117     },
118   ]);
119 }
120
121 function testShow() {
122   chrome.test.runTests([
123     function createUnfocusThenShow() {
124       chrome.app.window.create('test.html', {
125         id: 'createUnfocusThenShow', focused: false,
126         innerBounds: { width: 200, height: 200 }
127       }, callbackPass(function(win) {
128         win.contentWindow.onload = callbackPass(function() {
129           win.show();
130           // This test fails on Linux GTK, see http://crbug.com/325219
131           // And none of those tests run on Linux Aura, see
132           // http://crbug.com/325142
133           // We remove this and disable the entire test for Linux GTK when the
134           // test will run on other platforms, see http://crbug.com/328829
135           if (navigator.platform.indexOf('Linux') != 0)
136             testWindowGetsFocus(win);
137         });
138       }));
139     },
140     function createUnfocusThenShowUnfocused() {
141       chrome.app.window.create('test.html', {
142         id: 'createUnfocusThenShowUnfocused', focused: false,
143         innerBounds: { width: 200, height: 200 }
144       }, callbackPass(function(win) {
145         win.contentWindow.onload = callbackPass(function() {
146           win.show(false);
147           testWindowNeverGetsFocus(win);
148         });
149       }));
150     },
151     function createUnfocusThenShowFocusedThenShowUnfocused() {
152       chrome.app.window.create('test.html', {
153         id: 'createUnfocusThenShowFocusedThenShowUnfocused', focused: false,
154         innerBounds: { width: 200, height: 200 }
155       }, callbackPass(function(win) {
156         win.contentWindow.onload = callbackPass(function() {
157           win.show(true);
158           win.show(false);
159           // This test fails on Linux GTK, see http://crbug.com/325219
160           // And none of those tests run on Linux Aura, see
161           // http://crbug.com/325142
162           // We remove this and disable the entire test for Linux GTK when the
163           // test will run on other platforms, see http://crbug.com/328829
164           if (navigator.platform.indexOf('Linux') != 0)
165             testWindowGetsFocus(win);
166         });
167       }));
168     },
169   ]);
170 }
171
172 chrome.app.runtime.onLaunched.addListener(function() {
173   chrome.test.sendMessage('Launched', function(reply) {
174     window[reply]();
175   });
176 });