Chromium Code Reviews| Index: chrome/test/data/extensions/platform_apps/web_view/focus/embedder.js |
| diff --git a/chrome/test/data/extensions/platform_apps/web_view/focus/embedder.js b/chrome/test/data/extensions/platform_apps/web_view/focus/embedder.js |
| index b68f2ba392708e6b98cfcd34887027d5deccb27b..6ebd85db4b8f9681a68f40cd041c6c269f5ecb0f 100644 |
| --- a/chrome/test/data/extensions/platform_apps/web_view/focus/embedder.js |
| +++ b/chrome/test/data/extensions/platform_apps/web_view/focus/embedder.js |
| @@ -2,10 +2,12 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +var g_webview = null; |
| var embedder = {}; |
| +var seenFocusCount = 0; |
| embedder.tests = {}; |
| -embedder.triggerNavUrl = |
| - 'data:text/html,<html><body>trigger navigation<body></html>'; |
| +embedder.guestURL = |
| + 'data:text/html,<html><body>Guest<body></html>'; |
| window.runTest = function(testName) { |
| if (!embedder.test.testList[testName]) { |
| @@ -17,8 +19,26 @@ window.runTest = function(testName) { |
| // Run the test. |
| embedder.test.testList[testName](); |
| }; |
| + |
| +window.runCommand = function(command) { |
| + window.console.log('window.runCommand: ' + command); |
| + switch (command) { |
| + case 'POST_testFocusTracksEmbedder': |
| + POST_testFocusTracksEmbedder(); |
| + break; |
| + case 'POST_testAdvanceFocus': |
| + POST_testAdvanceFocus(); |
| + break; |
| + default: |
| + embedder.test.fail(); |
| + } |
| +}; |
| // window.* exported functions end. |
| +var LOG = function(msg) { |
| + window.console.log(msg); |
| +}; |
| + |
| embedder.test = {}; |
| embedder.test.succeed = function() { |
| chrome.test.sendMessage('TEST_PASSED'); |
| @@ -76,11 +96,15 @@ embedder.waitForResponseFromGuest_ = |
| if (response != expectedResponse) { |
| return; |
| } |
| - responseCallback(); |
| + responseCallback(data); |
| window.removeEventListener('message', onPostMessageReceived); |
| }; |
| window.addEventListener('message', onPostMessageReceived); |
| + webview.addEventListener('consolemessage', function(e) { |
| + LOG('g: ' + e.message); |
| + }); |
| + |
| var onWebViewLoadStop = function(e) { |
| console.log('loadstop'); |
| webview.executeScript( |
| @@ -94,7 +118,7 @@ embedder.waitForResponseFromGuest_ = |
| webview.removeEventListener('loadstop', onWebViewLoadStop); |
| }; |
| webview.addEventListener('loadstop', onWebViewLoadStop); |
| - webview.src = embedder.triggerNavUrl; |
| + webview.src = embedder.guestURL; |
| }; |
| // Tests begin. |
| @@ -113,6 +137,32 @@ embedder.testFocus_ = function(channelCreationCallback, |
| responseCallback); |
| }; |
| +// Verifies that if a <webview> is focused before navigation then the guest |
| +// starts off focused. |
| +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.
|
| + var webview = document.createElement('webview'); |
| + document.body.appendChild(webview); |
| + |
| + var onChannelEstablished = function(webview) { |
| + // Query the guest if it has focus. |
| + var msg = ['request-hasFocus']; |
| + webview.contentWindow.postMessage(JSON.stringify(msg), '*'); |
| + }; |
| + |
| + // Focus the <webview> before navigating it. |
| + webview.focus(); |
| + |
| + embedder.waitForResponseFromGuest_( |
| + webview, |
| + onChannelEstablished, |
| + 'response-hasFocus', |
| + function(data) { |
| + LOG('data, hasFocus: ' + data[1]); |
| + embedder.test.assertEq(true, data[1]); |
| + embedder.test.succeed(); |
| + }); |
| +} |
| + |
| function testFocusEvent() { |
| var seenResponse = false; |
| embedder.testFocus_(function(webview) { |
| @@ -142,8 +192,99 @@ function testBlurEvent() { |
| }); |
| } |
| +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.
|
| + var webview = document.createElement('webview'); |
| + g_webview = webview; |
| + document.body.appendChild(webview); |
| + |
| + var onChannelEstablished = function(webview) { |
| + var msg = ['request-waitForFocus']; |
| + webview.contentWindow.postMessage(JSON.stringify(msg), '*'); |
| + }; |
| + |
| + // Focus the <webview> before navigating it. |
| + // This is necessary so that 'blur' event on guest's <input> element fires. |
| + webview.focus(); |
| + |
| + embedder.waitForResponseFromGuest_( |
| + webview, |
| + onChannelEstablished, |
| + 'response-seenFocus', |
| + function(data) { embedder.test.succeed(); }); |
| +} |
| + |
| +function POST_testFocusTracksEmbedder() { |
| + g_webview.contentWindow.postMessage( |
| + JSON.stringify(['request-waitForBlurAfterFocus']), '*'); |
| + |
| + window.addEventListener('message', function(e) { |
| + var data = JSON.parse(e.data); |
| + LOG('send window.message, data: ' + data); |
| + if (data[0] == 'response-seenBlurAfterFocus') { |
| + chrome.test.sendMessage('POST_TEST_PASSED'); |
| + } else { |
| + chrome.test.sendMessage('POST_TEST_FAILED'); |
| + } |
| + }); |
| +} |
| + |
| +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.
|
| + var webview = document.createElement('webview'); |
| + g_webview = webview; |
| + document.body.appendChild(webview); |
| + |
| + webview.addEventListener('consolemessage', function(e) { |
| + LOG('g: ' + e.message); |
| + }); |
| + webview.addEventListener('loadstop', function(e) { |
| + LOG('loadstop'); |
| + |
| + window.addEventListener('message', function(e) { |
| + var data = JSON.parse(e.data); |
| + LOG('message, data: ' + data); |
| + |
| + if (data[0] == 'connected') { |
| + embedder.test.succeed(); |
| + } else if (data[0] == 'button1-focused') { |
| + var focusCount = data[1]; |
| + LOG('focusCount: ' + focusCount); |
| + seenFocusCount++; |
| + if (focusCount == 1) { |
| + chrome.test.sendMessage('button1-focused'); |
| + } else { |
| + chrome.test.sendMessage('button1-advance-focus'); |
| + } |
| + } |
| + }); |
| + |
| + webview.executeScript( |
| + {file: 'inject_advance_focus_test.js'}, |
| + function(results) { |
| + window.console.log('webview.executeScript response'); |
| + if (!results || !results.length) { |
| + LOG('Inject script failure.'); |
| + embedder.test.fail(); |
| + return; |
| + } |
| + webview.contentWindow.postMessage(JSON.stringify(['connect']), '*'); |
| + }); |
| + }); |
| + |
| + webview.src = embedder.guestURL; |
| +} |
| + |
| +function POST_testAdvanceFocus() { |
| + if (seenFocusCount == 1) { |
| + // If we have seen focus before current message loop was run, reply here. |
| + chrome.test.sendMessage('button1-focused'); |
| + } |
| +} |
| + |
| embedder.test.testList = { |
| + 'testAdvanceFocus': testAdvanceFocus, |
| + 'testFocusBeforeNavigation': testFocusBeforeNavigation, |
| 'testFocusEvent': testFocusEvent, |
| + 'testFocusTracksEmbedder': testFocusTracksEmbedder, |
| 'testBlurEvent': testBlurEvent |
| }; |