Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: chrome/test/data/extensions/platform_apps/web_view/focus/embedder.js

Issue 281833003: BrowserPluginHostTest: Rewrite 3 focus related tests and 1 visibility test to browser_tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sync Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 //
143 // We create a <webview> element and make it focused before navigating it.
144 // Then we load a URL in it and make sure document.hasFocus() returns true
145 // for the <webview>.
146 function testFocusBeforeNavigation() {
147 var webview = document.createElement('webview');
148 document.body.appendChild(webview);
149
150 var onChannelEstablished = function(webview) {
151 // Query the guest if it has focus.
152 var msg = ['request-hasFocus'];
153 webview.contentWindow.postMessage(JSON.stringify(msg), '*');
154 };
155
156 // Focus the <webview> before navigating it.
157 webview.focus();
158
159 embedder.waitForResponseFromGuest_(
160 webview,
161 onChannelEstablished,
162 'response-hasFocus',
163 function(data) {
164 LOG('data, hasFocus: ' + data[1]);
165 embedder.test.assertEq(true, data[1]);
166 embedder.test.succeed();
167 });
168 }
169
116 function testFocusEvent() { 170 function testFocusEvent() {
117 var seenResponse = false; 171 var seenResponse = false;
118 embedder.testFocus_(function(webview) { 172 embedder.testFocus_(function(webview) {
119 webview.focus(); 173 webview.focus();
120 }, 'focused', function() { 174 }, 'focused', function() {
121 // The focus event fires three times on first focus. We only care about 175 // The focus event fires three times on first focus. We only care about
122 // the first focus. 176 // the first focus.
123 if (seenResponse) { 177 if (seenResponse) {
124 return; 178 return;
125 } 179 }
126 seenResponse = true; 180 seenResponse = true;
127 embedder.test.succeed(); 181 embedder.test.succeed();
128 }); 182 });
129 } 183 }
130 184
131 function testBlurEvent() { 185 function testBlurEvent() {
132 var seenResponse = false; 186 var seenResponse = false;
133 embedder.testFocus_(function(webview) { 187 embedder.testFocus_(function(webview) {
134 webview.focus(); 188 webview.focus();
135 webview.blur(); 189 webview.blur();
136 }, 'blurred', function() { 190 }, 'blurred', function() {
137 if (seenResponse) { 191 if (seenResponse) {
138 return; 192 return;
139 } 193 }
140 seenResponse = true; 194 seenResponse = true;
141 embedder.test.succeed(); 195 embedder.test.succeed();
142 }); 196 });
143 } 197 }
144 198
199 // Tests that if we focus/blur the embedder, it also gets reflected in the
200 // guest.
201 //
202 // This test has two steps:
203 // 1) testFocusTracksEmbedder(), in this step we create a <webview> and
204 // focus it before navigating. After navigating it to a URL, we focus an input
205 // element inside the <webview>, and wait for its 'focus' event to fire.
206 // 2) POST_testFocusTracksEmbedder(), in this step, we have already called
207 // Blur() on the embedder's RVH (see WebViewTest.Focus_FocusTracksEmbedder),
208 // we make sure we see a 'blur' event on the <webview>'s input element.
209 function testFocusTracksEmbedder() {
210 var webview = document.createElement('webview');
211 g_webview = webview;
212 document.body.appendChild(webview);
213
214 var onChannelEstablished = function(webview) {
215 var msg = ['request-waitForFocus'];
216 webview.contentWindow.postMessage(JSON.stringify(msg), '*');
217 };
218
219 // Focus the <webview> before navigating it.
220 // This is necessary so that 'blur' event on guest's <input> element fires.
221 webview.focus();
222
223 embedder.waitForResponseFromGuest_(
224 webview,
225 onChannelEstablished,
226 'response-seenFocus',
227 function(data) { embedder.test.succeed(); });
228 }
229
230 // Runs the second step for testFocusTracksEmbedder().
231 // See WebViewTest.Focus_FocusTracksEmbedder() to see how this is invoked.
232 function POST_testFocusTracksEmbedder() {
233 g_webview.contentWindow.postMessage(
234 JSON.stringify(['request-waitForBlurAfterFocus']), '*');
235
236 window.addEventListener('message', function(e) {
237 var data = JSON.parse(e.data);
238 LOG('send window.message, data: ' + data);
239 if (data[0] == 'response-seenBlurAfterFocus') {
240 chrome.test.sendMessage('POST_TEST_PASSED');
241 } else {
242 chrome.test.sendMessage('POST_TEST_FAILED');
243 }
244 });
245 }
246
247 // Tests that <webview> sees advanceFocus() call when we cycle through the
248 // elements inside it using tab key.
249 //
250 // This test has two steps:
251 // 1) testAdvanceFocus(), in this step, we focus the embedder and press a
252 // tab key, we expect the input element inside the <webview> to be focused.
253 // 2) POST_testAdvanceFocus(), in this step we send additional tab keypress
254 // to the embedder/app (from WebViewInteractiveTest.Focus_AdvanceFocus), this
255 // would cycle the focus within the elements and will bring focus back to
256 // the input element present in the <webview> mentioned in step 1.
257 function testAdvanceFocus() {
258 var webview = document.createElement('webview');
259 g_webview = webview;
260 document.body.appendChild(webview);
261
262 webview.addEventListener('consolemessage', function(e) {
263 LOG('g: ' + e.message);
264 });
265 webview.addEventListener('loadstop', function(e) {
266 LOG('loadstop');
267
268 window.addEventListener('message', function(e) {
269 var data = JSON.parse(e.data);
270 LOG('message, data: ' + data);
271
272 if (data[0] == 'connected') {
273 embedder.test.succeed();
274 } else if (data[0] == 'button1-focused') {
275 var focusCount = data[1];
276 LOG('focusCount: ' + focusCount);
277 seenFocusCount++;
278 if (focusCount == 1) {
279 chrome.test.sendMessage('button1-focused');
280 } else {
281 chrome.test.sendMessage('button1-advance-focus');
282 }
283 }
284 });
285
286 webview.executeScript(
287 {file: 'inject_advance_focus_test.js'},
288 function(results) {
289 window.console.log('webview.executeScript response');
290 if (!results || !results.length) {
291 LOG('Inject script failure.');
292 embedder.test.fail();
293 return;
294 }
295 webview.contentWindow.postMessage(JSON.stringify(['connect']), '*');
296 });
297 });
298
299 webview.src = embedder.guestURL;
300 }
301
302 // Second step for testAdvanceFocus().
303 // See WebViewInteractiveTest.Focus_AdvanceFocus() to see how this is invoked.
304 function POST_testAdvanceFocus() {
305 if (seenFocusCount == 1) {
306 // If we have seen focus before current message loop was run, reply here.
307 chrome.test.sendMessage('button1-focused');
308 }
309 }
310
145 embedder.test.testList = { 311 embedder.test.testList = {
312 'testAdvanceFocus': testAdvanceFocus,
313 'testFocusBeforeNavigation': testFocusBeforeNavigation,
146 'testFocusEvent': testFocusEvent, 314 'testFocusEvent': testFocusEvent,
315 'testFocusTracksEmbedder': testFocusTracksEmbedder,
147 'testBlurEvent': testBlurEvent 316 'testBlurEvent': testBlurEvent
148 }; 317 };
149 318
150 onload = function() { 319 onload = function() {
151 chrome.test.getConfig(function(config) { 320 chrome.test.getConfig(function(config) {
152 chrome.test.sendMessage('Launched'); 321 chrome.test.sendMessage('Launched');
153 }); 322 });
154 }; 323 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698