OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var g_webview = null; | |
5 var embedder = {}; | 6 var embedder = {}; |
7 var seenFocusCount = 0; | |
6 embedder.tests = {}; | 8 embedder.tests = {}; |
7 embedder.triggerNavUrl = | 9 embedder.guestURL = |
8 'data:text/html,<html><body>trigger navigation<body></html>'; | 10 'data:text/html,<html><body>Guest<body></html>'; |
9 | 11 |
10 window.runTest = function(testName) { | 12 window.runTest = function(testName) { |
11 if (!embedder.test.testList[testName]) { | 13 if (!embedder.test.testList[testName]) { |
12 console.log('Incorrect testName: ' + testName); | 14 console.log('Incorrect testName: ' + testName); |
13 embedder.test.fail(); | 15 embedder.test.fail(); |
14 return; | 16 return; |
15 } | 17 } |
16 | 18 |
17 // Run the test. | 19 // Run the test. |
18 embedder.test.testList[testName](); | 20 embedder.test.testList[testName](); |
19 }; | 21 }; |
22 | |
23 window.runCommand = function(command) { | |
24 window.console.log('window.runCommand: ' + command); | |
25 switch (command) { | |
26 case 'POST_testFocusTracksEmbedder': | |
27 POST_testFocusTracksEmbedder(); | |
28 break; | |
29 case 'POST_testAdvanceFocus': | |
30 POST_testAdvanceFocus(); | |
31 break; | |
32 default: | |
33 embedder.test.fail(); | |
34 } | |
35 }; | |
20 // window.* exported functions end. | 36 // window.* exported functions end. |
21 | 37 |
38 var LOG = function(msg) { | |
39 window.console.log(msg); | |
40 }; | |
41 | |
22 embedder.test = {}; | 42 embedder.test = {}; |
23 embedder.test.succeed = function() { | 43 embedder.test.succeed = function() { |
24 chrome.test.sendMessage('TEST_PASSED'); | 44 chrome.test.sendMessage('TEST_PASSED'); |
25 }; | 45 }; |
26 | 46 |
27 embedder.test.fail = function() { | 47 embedder.test.fail = function() { |
28 chrome.test.sendMessage('TEST_FAILED'); | 48 chrome.test.sendMessage('TEST_FAILED'); |
29 }; | 49 }; |
30 | 50 |
31 embedder.test.assertEq = function(a, b) { | 51 embedder.test.assertEq = function(a, b) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 var onPostMessageReceived = function(e) { | 89 var onPostMessageReceived = function(e) { |
70 var data = JSON.parse(e.data); | 90 var data = JSON.parse(e.data); |
71 var response = data[0]; | 91 var response = data[0]; |
72 if (response == 'connected') { | 92 if (response == 'connected') { |
73 channelCreationCallback(webview); | 93 channelCreationCallback(webview); |
74 return; | 94 return; |
75 } | 95 } |
76 if (response != expectedResponse) { | 96 if (response != expectedResponse) { |
77 return; | 97 return; |
78 } | 98 } |
79 responseCallback(); | 99 responseCallback(data); |
80 window.removeEventListener('message', onPostMessageReceived); | 100 window.removeEventListener('message', onPostMessageReceived); |
81 }; | 101 }; |
82 window.addEventListener('message', onPostMessageReceived); | 102 window.addEventListener('message', onPostMessageReceived); |
83 | 103 |
104 webview.addEventListener('consolemessage', function(e) { | |
105 LOG('g: ' + e.message); | |
106 }); | |
107 | |
84 var onWebViewLoadStop = function(e) { | 108 var onWebViewLoadStop = function(e) { |
85 console.log('loadstop'); | 109 console.log('loadstop'); |
86 webview.executeScript( | 110 webview.executeScript( |
87 {file: 'inject_focus.js'}, | 111 {file: 'inject_focus.js'}, |
88 function(results) { | 112 function(results) { |
89 console.log('Injected script into webview.'); | 113 console.log('Injected script into webview.'); |
90 // Establish a communication channel with the webview1's guest. | 114 // Establish a communication channel with the webview1's guest. |
91 var msg = ['connect']; | 115 var msg = ['connect']; |
92 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); | 116 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); |
93 }); | 117 }); |
94 webview.removeEventListener('loadstop', onWebViewLoadStop); | 118 webview.removeEventListener('loadstop', onWebViewLoadStop); |
95 }; | 119 }; |
96 webview.addEventListener('loadstop', onWebViewLoadStop); | 120 webview.addEventListener('loadstop', onWebViewLoadStop); |
97 webview.src = embedder.triggerNavUrl; | 121 webview.src = embedder.guestURL; |
98 }; | 122 }; |
99 | 123 |
100 // Tests begin. | 124 // Tests begin. |
101 | 125 |
102 // The embedder has to initiate a post message so that the guest can get a | 126 // The embedder has to initiate a post message so that the guest can get a |
103 // reference to embedder to send the reply back. | 127 // reference to embedder to send the reply back. |
104 | 128 |
105 embedder.testFocus_ = function(channelCreationCallback, | 129 embedder.testFocus_ = function(channelCreationCallback, |
106 expectedResponse, | 130 expectedResponse, |
107 responseCallback) { | 131 responseCallback) { |
108 var webview = embedder.setUpGuest_(); | 132 var webview = embedder.setUpGuest_(); |
109 | 133 |
110 embedder.waitForResponseFromGuest_(webview, | 134 embedder.waitForResponseFromGuest_(webview, |
111 channelCreationCallback, | 135 channelCreationCallback, |
112 expectedResponse, | 136 expectedResponse, |
113 responseCallback); | 137 responseCallback); |
114 }; | 138 }; |
115 | 139 |
140 // Verifies that if a <webview> is focused before navigation then the guest | |
141 // starts off focused. | |
142 function testFocusBeforeNavigation() { | |
Fady Samuel
2014/05/20 15:37:20
There's a lot of code here and it's a lot easier f
lazyboy
2014/05/20 16:46:51
Done.
| |
143 var webview = document.createElement('webview'); | |
144 document.body.appendChild(webview); | |
145 | |
146 var onChannelEstablished = function(webview) { | |
147 // Query the guest if it has focus. | |
148 var msg = ['request-hasFocus']; | |
149 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); | |
150 }; | |
151 | |
152 // Focus the <webview> before navigating it. | |
153 webview.focus(); | |
154 | |
155 embedder.waitForResponseFromGuest_( | |
156 webview, | |
157 onChannelEstablished, | |
158 'response-hasFocus', | |
159 function(data) { | |
160 LOG('data, hasFocus: ' + data[1]); | |
161 embedder.test.assertEq(true, data[1]); | |
162 embedder.test.succeed(); | |
163 }); | |
164 } | |
165 | |
116 function testFocusEvent() { | 166 function testFocusEvent() { |
117 var seenResponse = false; | 167 var seenResponse = false; |
118 embedder.testFocus_(function(webview) { | 168 embedder.testFocus_(function(webview) { |
119 webview.focus(); | 169 webview.focus(); |
120 }, 'focused', function() { | 170 }, 'focused', function() { |
121 // The focus event fires three times on first focus. We only care about | 171 // The focus event fires three times on first focus. We only care about |
122 // the first focus. | 172 // the first focus. |
123 if (seenResponse) { | 173 if (seenResponse) { |
124 return; | 174 return; |
125 } | 175 } |
126 seenResponse = true; | 176 seenResponse = true; |
127 embedder.test.succeed(); | 177 embedder.test.succeed(); |
128 }); | 178 }); |
129 } | 179 } |
130 | 180 |
131 function testBlurEvent() { | 181 function testBlurEvent() { |
132 var seenResponse = false; | 182 var seenResponse = false; |
133 embedder.testFocus_(function(webview) { | 183 embedder.testFocus_(function(webview) { |
134 webview.focus(); | 184 webview.focus(); |
135 webview.blur(); | 185 webview.blur(); |
136 }, 'blurred', function() { | 186 }, 'blurred', function() { |
137 if (seenResponse) { | 187 if (seenResponse) { |
138 return; | 188 return; |
139 } | 189 } |
140 seenResponse = true; | 190 seenResponse = true; |
141 embedder.test.succeed(); | 191 embedder.test.succeed(); |
142 }); | 192 }); |
143 } | 193 } |
144 | 194 |
195 function testFocusTracksEmbedder() { | |
Fady Samuel
2014/05/20 15:37:20
Description of what this test does please.
lazyboy
2014/05/20 16:46:51
Done.
| |
196 var webview = document.createElement('webview'); | |
197 g_webview = webview; | |
198 document.body.appendChild(webview); | |
199 | |
200 var onChannelEstablished = function(webview) { | |
201 var msg = ['request-waitForFocus']; | |
202 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); | |
203 }; | |
204 | |
205 // Focus the <webview> before navigating it. | |
206 // This is necessary so that 'blur' event on guest's <input> element fires. | |
207 webview.focus(); | |
208 | |
209 embedder.waitForResponseFromGuest_( | |
210 webview, | |
211 onChannelEstablished, | |
212 'response-seenFocus', | |
213 function(data) { embedder.test.succeed(); }); | |
214 } | |
215 | |
216 function POST_testFocusTracksEmbedder() { | |
217 g_webview.contentWindow.postMessage( | |
218 JSON.stringify(['request-waitForBlurAfterFocus']), '*'); | |
219 | |
220 window.addEventListener('message', function(e) { | |
221 var data = JSON.parse(e.data); | |
222 LOG('send window.message, data: ' + data); | |
223 if (data[0] == 'response-seenBlurAfterFocus') { | |
224 chrome.test.sendMessage('POST_TEST_PASSED'); | |
225 } else { | |
226 chrome.test.sendMessage('POST_TEST_FAILED'); | |
227 } | |
228 }); | |
229 } | |
230 | |
231 function testAdvanceFocus() { | |
Fady Samuel
2014/05/20 15:37:20
There's a lot of code here and it's a lot easier f
lazyboy
2014/05/20 16:46:51
Done.
| |
232 var webview = document.createElement('webview'); | |
233 g_webview = webview; | |
234 document.body.appendChild(webview); | |
235 | |
236 webview.addEventListener('consolemessage', function(e) { | |
237 LOG('g: ' + e.message); | |
238 }); | |
239 webview.addEventListener('loadstop', function(e) { | |
240 LOG('loadstop'); | |
241 | |
242 window.addEventListener('message', function(e) { | |
243 var data = JSON.parse(e.data); | |
244 LOG('message, data: ' + data); | |
245 | |
246 if (data[0] == 'connected') { | |
247 embedder.test.succeed(); | |
248 } else if (data[0] == 'button1-focused') { | |
249 var focusCount = data[1]; | |
250 LOG('focusCount: ' + focusCount); | |
251 seenFocusCount++; | |
252 if (focusCount == 1) { | |
253 chrome.test.sendMessage('button1-focused'); | |
254 } else { | |
255 chrome.test.sendMessage('button1-advance-focus'); | |
256 } | |
257 } | |
258 }); | |
259 | |
260 webview.executeScript( | |
261 {file: 'inject_advance_focus_test.js'}, | |
262 function(results) { | |
263 window.console.log('webview.executeScript response'); | |
264 if (!results || !results.length) { | |
265 LOG('Inject script failure.'); | |
266 embedder.test.fail(); | |
267 return; | |
268 } | |
269 webview.contentWindow.postMessage(JSON.stringify(['connect']), '*'); | |
270 }); | |
271 }); | |
272 | |
273 webview.src = embedder.guestURL; | |
274 } | |
275 | |
276 function POST_testAdvanceFocus() { | |
277 if (seenFocusCount == 1) { | |
278 // If we have seen focus before current message loop was run, reply here. | |
279 chrome.test.sendMessage('button1-focused'); | |
280 } | |
281 } | |
282 | |
145 embedder.test.testList = { | 283 embedder.test.testList = { |
284 'testAdvanceFocus': testAdvanceFocus, | |
285 'testFocusBeforeNavigation': testFocusBeforeNavigation, | |
146 'testFocusEvent': testFocusEvent, | 286 'testFocusEvent': testFocusEvent, |
287 'testFocusTracksEmbedder': testFocusTracksEmbedder, | |
147 'testBlurEvent': testBlurEvent | 288 'testBlurEvent': testBlurEvent |
148 }; | 289 }; |
149 | 290 |
150 onload = function() { | 291 onload = function() { |
151 chrome.test.getConfig(function(config) { | 292 chrome.test.getConfig(function(config) { |
152 chrome.test.sendMessage('Launched'); | 293 chrome.test.sendMessage('Launched'); |
153 }); | 294 }); |
154 }; | 295 }; |
OLD | NEW |