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 default: |
| 30 embedder.test.fail(); |
| 31 } |
| 32 }; |
20 // window.* exported functions end. | 33 // window.* exported functions end. |
21 | 34 |
| 35 var LOG = function(msg) { |
| 36 window.console.log(msg); |
| 37 }; |
| 38 |
22 embedder.test = {}; | 39 embedder.test = {}; |
23 embedder.test.succeed = function() { | 40 embedder.test.succeed = function() { |
24 chrome.test.sendMessage('TEST_PASSED'); | 41 chrome.test.sendMessage('TEST_PASSED'); |
25 }; | 42 }; |
26 | 43 |
27 embedder.test.fail = function() { | 44 embedder.test.fail = function() { |
28 chrome.test.sendMessage('TEST_FAILED'); | 45 chrome.test.sendMessage('TEST_FAILED'); |
29 }; | 46 }; |
30 | 47 |
31 embedder.test.assertEq = function(a, b) { | 48 embedder.test.assertEq = function(a, b) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 var onPostMessageReceived = function(e) { | 86 var onPostMessageReceived = function(e) { |
70 var data = JSON.parse(e.data); | 87 var data = JSON.parse(e.data); |
71 var response = data[0]; | 88 var response = data[0]; |
72 if (response == 'connected') { | 89 if (response == 'connected') { |
73 channelCreationCallback(webview); | 90 channelCreationCallback(webview); |
74 return; | 91 return; |
75 } | 92 } |
76 if (response != expectedResponse) { | 93 if (response != expectedResponse) { |
77 return; | 94 return; |
78 } | 95 } |
79 responseCallback(); | 96 responseCallback(data); |
80 window.removeEventListener('message', onPostMessageReceived); | 97 window.removeEventListener('message', onPostMessageReceived); |
81 }; | 98 }; |
82 window.addEventListener('message', onPostMessageReceived); | 99 window.addEventListener('message', onPostMessageReceived); |
83 | 100 |
| 101 webview.addEventListener('consolemessage', function(e) { |
| 102 LOG('g: ' + e.message); |
| 103 }); |
| 104 |
84 var onWebViewLoadStop = function(e) { | 105 var onWebViewLoadStop = function(e) { |
85 console.log('loadstop'); | 106 console.log('loadstop'); |
86 webview.executeScript( | 107 webview.executeScript( |
87 {file: 'inject_focus.js'}, | 108 {file: 'inject_focus.js'}, |
88 function(results) { | 109 function(results) { |
89 console.log('Injected script into webview.'); | 110 console.log('Injected script into webview.'); |
90 // Establish a communication channel with the webview1's guest. | 111 // Establish a communication channel with the webview1's guest. |
91 var msg = ['connect']; | 112 var msg = ['connect']; |
92 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); | 113 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); |
93 }); | 114 }); |
94 webview.removeEventListener('loadstop', onWebViewLoadStop); | 115 webview.removeEventListener('loadstop', onWebViewLoadStop); |
95 }; | 116 }; |
96 webview.addEventListener('loadstop', onWebViewLoadStop); | 117 webview.addEventListener('loadstop', onWebViewLoadStop); |
97 webview.src = embedder.triggerNavUrl; | 118 webview.src = embedder.guestURL; |
98 }; | 119 }; |
99 | 120 |
100 // Tests begin. | 121 // Tests begin. |
101 | 122 |
102 // The embedder has to initiate a post message so that the guest can get a | 123 // The embedder has to initiate a post message so that the guest can get a |
103 // reference to embedder to send the reply back. | 124 // reference to embedder to send the reply back. |
104 | 125 |
105 embedder.testFocus_ = function(channelCreationCallback, | 126 embedder.testFocus_ = function(channelCreationCallback, |
106 expectedResponse, | 127 expectedResponse, |
107 responseCallback) { | 128 responseCallback) { |
108 var webview = embedder.setUpGuest_(); | 129 var webview = embedder.setUpGuest_(); |
109 | 130 |
110 embedder.waitForResponseFromGuest_(webview, | 131 embedder.waitForResponseFromGuest_(webview, |
111 channelCreationCallback, | 132 channelCreationCallback, |
112 expectedResponse, | 133 expectedResponse, |
113 responseCallback); | 134 responseCallback); |
114 }; | 135 }; |
115 | 136 |
| 137 // Verifies that if a <webview> is focused before navigation then the guest |
| 138 // starts off focused. |
| 139 // |
| 140 // We create a <webview> element and make it focused before navigating it. |
| 141 // Then we load a URL in it and make sure document.hasFocus() returns true |
| 142 // for the <webview>. |
| 143 function testFocusBeforeNavigation() { |
| 144 var webview = document.createElement('webview'); |
| 145 document.body.appendChild(webview); |
| 146 |
| 147 var onChannelEstablished = function(webview) { |
| 148 // Query the guest if it has focus. |
| 149 var msg = ['request-hasFocus']; |
| 150 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); |
| 151 }; |
| 152 |
| 153 // Focus the <webview> before navigating it. |
| 154 webview.focus(); |
| 155 |
| 156 embedder.waitForResponseFromGuest_( |
| 157 webview, |
| 158 onChannelEstablished, |
| 159 'response-hasFocus', |
| 160 function(data) { |
| 161 LOG('data, hasFocus: ' + data[1]); |
| 162 embedder.test.assertEq(true, data[1]); |
| 163 embedder.test.succeed(); |
| 164 }); |
| 165 } |
| 166 |
116 function testFocusEvent() { | 167 function testFocusEvent() { |
117 var seenResponse = false; | 168 var seenResponse = false; |
118 embedder.testFocus_(function(webview) { | 169 embedder.testFocus_(function(webview) { |
119 webview.focus(); | 170 webview.focus(); |
120 }, 'focused', function() { | 171 }, 'focused', function() { |
121 // The focus event fires three times on first focus. We only care about | 172 // The focus event fires three times on first focus. We only care about |
122 // the first focus. | 173 // the first focus. |
123 if (seenResponse) { | 174 if (seenResponse) { |
124 return; | 175 return; |
125 } | 176 } |
126 seenResponse = true; | 177 seenResponse = true; |
127 embedder.test.succeed(); | 178 embedder.test.succeed(); |
128 }); | 179 }); |
129 } | 180 } |
130 | 181 |
131 function testBlurEvent() { | 182 function testBlurEvent() { |
132 var seenResponse = false; | 183 var seenResponse = false; |
133 embedder.testFocus_(function(webview) { | 184 embedder.testFocus_(function(webview) { |
134 webview.focus(); | 185 webview.focus(); |
135 webview.blur(); | 186 webview.blur(); |
136 }, 'blurred', function() { | 187 }, 'blurred', function() { |
137 if (seenResponse) { | 188 if (seenResponse) { |
138 return; | 189 return; |
139 } | 190 } |
140 seenResponse = true; | 191 seenResponse = true; |
141 embedder.test.succeed(); | 192 embedder.test.succeed(); |
142 }); | 193 }); |
143 } | 194 } |
144 | 195 |
| 196 // Tests that if we focus/blur the embedder, it also gets reflected in the |
| 197 // guest. |
| 198 // |
| 199 // This test has two steps: |
| 200 // 1) testFocusTracksEmbedder(), in this step we create a <webview> and |
| 201 // focus it before navigating. After navigating it to a URL, we focus an input |
| 202 // element inside the <webview>, and wait for its 'focus' event to fire. |
| 203 // 2) POST_testFocusTracksEmbedder(), in this step, we have already called |
| 204 // Blur() on the embedder's RVH (see WebViewTest.Focus_FocusTracksEmbedder), |
| 205 // we make sure we see a 'blur' event on the <webview>'s input element. |
| 206 function testFocusTracksEmbedder() { |
| 207 var webview = document.createElement('webview'); |
| 208 g_webview = webview; |
| 209 document.body.appendChild(webview); |
| 210 |
| 211 var onChannelEstablished = function(webview) { |
| 212 var msg = ['request-waitForFocus']; |
| 213 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); |
| 214 }; |
| 215 |
| 216 // Focus the <webview> before navigating it. |
| 217 // This is necessary so that 'blur' event on guest's <input> element fires. |
| 218 webview.focus(); |
| 219 |
| 220 embedder.waitForResponseFromGuest_( |
| 221 webview, |
| 222 onChannelEstablished, |
| 223 'response-seenFocus', |
| 224 function(data) { embedder.test.succeed(); }); |
| 225 } |
| 226 |
| 227 // Runs the second step for testFocusTracksEmbedder(). |
| 228 // See WebViewTest.Focus_FocusTracksEmbedder() to see how this is invoked. |
| 229 function POST_testFocusTracksEmbedder() { |
| 230 g_webview.contentWindow.postMessage( |
| 231 JSON.stringify(['request-waitForBlurAfterFocus']), '*'); |
| 232 |
| 233 window.addEventListener('message', function(e) { |
| 234 var data = JSON.parse(e.data); |
| 235 LOG('send window.message, data: ' + data); |
| 236 if (data[0] == 'response-seenBlurAfterFocus') { |
| 237 chrome.test.sendMessage('POST_TEST_PASSED'); |
| 238 } else { |
| 239 chrome.test.sendMessage('POST_TEST_FAILED'); |
| 240 } |
| 241 }); |
| 242 } |
| 243 |
| 244 // Tests that <webview> sees advanceFocus() call when we cycle through the |
| 245 // elements inside it using tab key. |
| 246 // |
| 247 // This test has two steps: |
| 248 // 1) testAdvanceFocus(), in this step, we focus the embedder and press a |
| 249 // tab key, we expect the input element inside the <webview> to be focused. |
| 250 // 2) POST_testAdvanceFocus(), in this step we send additional tab keypress |
| 251 // to the embedder/app (from WebViewInteractiveTest.Focus_AdvanceFocus), this |
| 252 // would cycle the focus within the elements and will bring focus back to |
| 253 // the input element present in the <webview> mentioned in step 1. |
| 254 function testAdvanceFocus() { |
| 255 var webview = document.createElement('webview'); |
| 256 g_webview = webview; |
| 257 document.body.appendChild(webview); |
| 258 |
| 259 webview.addEventListener('consolemessage', function(e) { |
| 260 LOG('g: ' + e.message); |
| 261 }); |
| 262 webview.addEventListener('loadstop', function(e) { |
| 263 LOG('loadstop'); |
| 264 |
| 265 window.addEventListener('message', function(e) { |
| 266 var data = JSON.parse(e.data); |
| 267 LOG('message, data: ' + data); |
| 268 |
| 269 if (data[0] == 'connected') { |
| 270 embedder.test.succeed(); |
| 271 } else if (data[0] == 'button1-focused') { |
| 272 var focusCount = data[1]; |
| 273 LOG('focusCount: ' + focusCount); |
| 274 seenFocusCount++; |
| 275 if (focusCount == 1) { |
| 276 chrome.test.sendMessage('button1-focused'); |
| 277 } else { |
| 278 chrome.test.sendMessage('button1-advance-focus'); |
| 279 } |
| 280 } |
| 281 }); |
| 282 |
| 283 webview.executeScript( |
| 284 {file: 'inject_advance_focus_test.js'}, |
| 285 function(results) { |
| 286 window.console.log('webview.executeScript response'); |
| 287 if (!results || !results.length) { |
| 288 LOG('Inject script failure.'); |
| 289 embedder.test.fail(); |
| 290 return; |
| 291 } |
| 292 webview.contentWindow.postMessage(JSON.stringify(['connect']), '*'); |
| 293 }); |
| 294 }); |
| 295 |
| 296 webview.src = embedder.guestURL; |
| 297 } |
| 298 |
145 embedder.test.testList = { | 299 embedder.test.testList = { |
| 300 'testAdvanceFocus': testAdvanceFocus, |
| 301 'testFocusBeforeNavigation': testFocusBeforeNavigation, |
146 'testFocusEvent': testFocusEvent, | 302 'testFocusEvent': testFocusEvent, |
| 303 'testFocusTracksEmbedder': testFocusTracksEmbedder, |
147 'testBlurEvent': testBlurEvent | 304 'testBlurEvent': testBlurEvent |
148 }; | 305 }; |
149 | 306 |
150 onload = function() { | 307 onload = function() { |
151 chrome.test.getConfig(function(config) { | 308 chrome.test.getConfig(function(config) { |
152 chrome.test.sendMessage('Launched'); | 309 chrome.test.sendMessage('Launched'); |
153 }); | 310 }); |
154 }; | 311 }; |
OLD | NEW |