From 19132ce1ce87227133c1f1da5ca027a1bda9f5e2 Mon Sep 17 00:00:00 2001 From: lfg Date: Fri, 10 Oct 2014 08:46:20 -0700 Subject: [PATCH] Adding webview tests to app_shell_browsertest. This CL adds the following tests to app_shell_browsertest: - TestDialogAlert - TestDialogConfirm - TestDialogConfirmCancel - TestDialogConfirmDefaultCancel - TestDialogConfirmDefaultGCCancel - TestDialogPrompt Also, makes sure that we call content::BrowserTestBase::SetUpCommandLine(...) from AppShellTest. BUG=352293 Review URL: https://codereview.chromium.org/634403003 Cr-Commit-Position: refs/heads/master@{#299121} --- .../guest_view/web_view/web_view_apitest.cc | 30 ++ .../browser/guest_view/web_view/web_view_apitest.h | 1 + extensions/shell/test/shell_test.cc | 1 + extensions/test/data/web_view/dialog/embedder.html | 11 + extensions/test/data/web_view/dialog/embedder.js | 303 +++++++++++++++++++++ .../test/data/web_view/dialog/inject_dialog.js | 55 ++++ extensions/test/data/web_view/dialog/manifest.json | 14 + extensions/test/data/web_view/dialog/test.js | 7 + 8 files changed, 422 insertions(+) create mode 100644 extensions/test/data/web_view/dialog/embedder.html create mode 100644 extensions/test/data/web_view/dialog/embedder.js create mode 100644 extensions/test/data/web_view/dialog/inject_dialog.js create mode 100644 extensions/test/data/web_view/dialog/manifest.json create mode 100644 extensions/test/data/web_view/dialog/test.js diff --git a/extensions/browser/guest_view/web_view/web_view_apitest.cc b/extensions/browser/guest_view/web_view/web_view_apitest.cc index 6672a3303c5c..4fd278ea2a8d 100644 --- a/extensions/browser/guest_view/web_view/web_view_apitest.cc +++ b/extensions/browser/guest_view/web_view/web_view_apitest.cc @@ -9,6 +9,7 @@ #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" #include "content/public/browser/render_process_host.h" +#include "content/public/common/content_switches.h" #include "content/public/test/browser_test_utils.h" #include "content/public/test/test_utils.h" #include "extensions/browser/api/test/test_api.h" @@ -146,6 +147,11 @@ void WebViewAPITest::RunTestOnMainThreadLoop() { GetGuestViewManager()->WaitForAllGuestsDeleted(); } +void WebViewAPITest::SetUpCommandLine(base::CommandLine* command_line) { + AppShellTest::SetUpCommandLine(command_line); + command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc"); +} + void WebViewAPITest::SetUpOnMainThread() { AppShellTest::SetUpOnMainThread(); @@ -280,6 +286,30 @@ IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestDestroyOnEventListener) { RunTest("testDestroyOnEventListener", "web_view/apitest"); } +IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestDialogAlert) { + RunTest("testDialogAlert", "web_view/dialog"); +} + +IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestDialogConfirm) { + RunTest("testDialogConfirm", "web_view/dialog"); +} + +IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestDialogConfirmCancel) { + RunTest("testDialogConfirmCancel", "web_view/dialog"); +} + +IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestDialogConfirmDefaultCancel) { + RunTest("testDialogConfirmDefaultCancel", "web_view/dialog"); +} + +IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestDialogConfirmDefaultGCCancel) { + RunTest("testDialogConfirmDefaultGCCancel", "web_view/dialog"); +} + +IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestDialogPrompt) { + RunTest("testDialogPrompt", "web_view/dialog"); +} + IN_PROC_BROWSER_TEST_F(WebViewAPITest, TestDisplayNoneWebviewLoad) { RunTest("testDisplayNoneWebviewLoad", "web_view/apitest"); } diff --git a/extensions/browser/guest_view/web_view/web_view_apitest.h b/extensions/browser/guest_view/web_view/web_view_apitest.h index a523225f3199..b9beb9ed5446 100644 --- a/extensions/browser/guest_view/web_view/web_view_apitest.h +++ b/extensions/browser/guest_view/web_view/web_view_apitest.h @@ -35,6 +35,7 @@ class WebViewAPITest : public AppShellTest { // content::BrowserTestBase implementation. virtual void RunTestOnMainThreadLoop() override; + virtual void SetUpCommandLine(base::CommandLine* command_line) override; virtual void SetUpOnMainThread() override; virtual void TearDownOnMainThread() override; diff --git a/extensions/shell/test/shell_test.cc b/extensions/shell/test/shell_test.cc index 294fbaa89870..87e543040a69 100644 --- a/extensions/shell/test/shell_test.cc +++ b/extensions/shell/test/shell_test.cc @@ -23,6 +23,7 @@ AppShellTest::~AppShellTest() { void AppShellTest::SetUp() { base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); command_line->AppendSwitchASCII(switches::kTestType, "appshell"); + SetUpCommandLine(command_line); content::BrowserTestBase::SetUp(); } diff --git a/extensions/test/data/web_view/dialog/embedder.html b/extensions/test/data/web_view/dialog/embedder.html new file mode 100644 index 000000000000..d350e46e52c4 --- /dev/null +++ b/extensions/test/data/web_view/dialog/embedder.html @@ -0,0 +1,11 @@ + + + +
+ + + diff --git a/extensions/test/data/web_view/dialog/embedder.js b/extensions/test/data/web_view/dialog/embedder.js new file mode 100644 index 000000000000..2edda30dc855 --- /dev/null +++ b/extensions/test/data/web_view/dialog/embedder.js @@ -0,0 +1,303 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +var embedder = {}; +embedder.tests = {}; +embedder.baseGuestURL = ''; +embedder.guestURL = ''; + +window.runTest = function(testName) { + if (!embedder.test.testList[testName]) { + console.log('Incorrect testName: ' + testName); + embedder.test.fail(); + return; + } + + // Run the test. + embedder.test.testList[testName](); +}; +// window.* exported functions end. + +/** @private */ +embedder.setUpGuest_ = function() { + document.querySelector('#webview-tag-container').innerHTML = + ''; + var webview = document.querySelector('webview'); + if (!webview) { + embedder.test.fail('No element created'); + } + return webview; +}; + +embedder.getHTMLForGuestWithTitle_ = function(title) { + var html = + 'data:text/html,' + + '%s' + + 'hello world' + + ''; + return html.replace('%s', title); +}; + +embedder.test = {}; +embedder.test.succeed = function() { + chrome.test.sendMessage('TEST_PASSED'); +}; + +embedder.test.fail = function() { + chrome.test.sendMessage('TEST_FAILED'); +}; + +embedder.test.assertEq = function(a, b) { + if (a != b) { + console.log('assertion failed: ' + a + ' != ' + b); + embedder.test.fail(); + } +}; + +embedder.test.assertTrue = function(condition) { + if (!condition) { + console.log('assertion failed: true != ' + condition); + embedder.test.fail(); + } +}; + +embedder.test.assertFalse = function(condition) { + if (condition) { + console.log('assertion failed: false != ' + condition); + embedder.test.fail(); + } +}; + +function setUpDialogTest(messageCallback, dialogHandler) { + var guestUrl = 'data:text/html,guest'; + var webview = document.createElement('webview'); + + var onLoadStop = function(e) { + console.log('webview has loaded.'); + webview.executeScript( + {file: 'inject_dialog.js'}, + function(results) { + console.log('Script has been injected into webview.'); + // Establish a communication channel with the guest. + var msg = ['connect']; + webview.contentWindow.postMessage(JSON.stringify(msg), '*'); + }); + }; + webview.addEventListener('loadstop', onLoadStop); + + window.addEventListener('message', function(e) { + var data = JSON.parse(e.data); + if (data[0] == 'connected') { + console.log('A communication channel has been established with webview.'); + } + + messageCallback(webview, data); + }); + + webview.addEventListener('dialog', function(e) { + dialogHandler(e); + }); + + webview.setAttribute('src', guestUrl); + document.body.appendChild(webview); +} + +// Tests begin. + +function testDialogAlert() { + var messageText = '1337h@x0r'; + + var messageCallback = function(webview, data) { + if (data[0] == 'connected') { + console.log('The alert dialog test has started.'); + var msg = ['start-alert-dialog-test', messageText]; + webview.contentWindow.postMessage(JSON.stringify(msg), '*'); + return; + } + + if (data[0] == 'alert-dialog-done') { + console.log( + 'webview has been unblocked after requesting an alert dialog.'); + embedder.test.succeed(); + return; + } + }; + + var dialogHandler = function(e) { + console.log('webview has requested a dialog.'); + embedder.test.assertEq('alert', e.messageType); + embedder.test.assertEq(messageText, e.messageText); + e.dialog.ok(); + console.log('The app has responded to the dialog request.'); + }; + + setUpDialogTest(messageCallback, dialogHandler); +} + +function testDialogConfirm() { + var messageText = 'foobar'; + + var messageCallback = function(webview, data) { + if (data[0] == 'connected') { + console.log('The confirm dialog test has started.'); + var msg = ['start-confirm-dialog-test', messageText]; + webview.contentWindow.postMessage(JSON.stringify(msg), '*'); + return; + } + + if (data[0] == 'confirm-dialog-result') { + console.log('webview has reported a result for its confirm dialog.'); + embedder.test.assertEq(true, data[1]); + embedder.test.succeed(); + return; + } + }; + + var dialogHandler = function(e) { + console.log('webview has requested a dialog.'); + embedder.test.assertEq('confirm', e.messageType); + embedder.test.assertEq(messageText, e.messageText); + e.dialog.ok(); + console.log('The app has responded to the dialog request.'); + }; + + setUpDialogTest(messageCallback, dialogHandler); +} + +function testDialogConfirmCancel() { + var messageText = 'foobar'; + + var messageCallback = function(webview, data) { + if (data[0] == 'connected') { + console.log('The confirm dialog test has started.'); + var msg = ['start-confirm-dialog-test', messageText]; + webview.contentWindow.postMessage(JSON.stringify(msg), '*'); + return; + } + + if (data[0] == 'confirm-dialog-result') { + console.log('webview has reported a result for its confirm dialog.'); + embedder.test.assertEq(false, data[1]); + embedder.test.succeed(); + return; + } + }; + + var dialogHandler = function(e) { + console.log('webview has requested a dialog.'); + embedder.test.assertEq('confirm', e.messageType); + embedder.test.assertEq(messageText, e.messageText); + e.dialog.cancel(); + console.log('The app has responded to the dialog request.'); + }; + + setUpDialogTest(messageCallback, dialogHandler); +} + +function testDialogConfirmDefaultCancel() { + var messageText = 'foobar'; + + var messageCallback = function(webview, data) { + if (data[0] == 'connected') { + console.log('The confirm dialog test has started.'); + var msg = ['start-confirm-dialog-test', messageText]; + webview.contentWindow.postMessage(JSON.stringify(msg), '*'); + return; + } + + if (data[0] == 'confirm-dialog-result') { + console.log('webview has reported a result for its confirm dialog.'); + embedder.test.assertEq(false, data[1]); + embedder.test.succeed(); + return; + } + }; + + var dialogHandler = function(e) { + console.log('webview has requested a dialog.'); + embedder.test.assertEq('confirm', e.messageType); + embedder.test.assertEq(messageText, e.messageText); + }; + + setUpDialogTest(messageCallback, dialogHandler); +} + +function testDialogConfirmDefaultGCCancel() { + var messageText = 'foobar'; + + var messageCallback = function(webview, data) { + if (data[0] == 'connected') { + console.log('The confirm dialog test has started.'); + var msg = ['start-confirm-dialog-test', messageText]; + webview.contentWindow.postMessage(JSON.stringify(msg), '*'); + return; + } + + if (data[0] == 'confirm-dialog-result') { + console.log('webview has reported a result for its confirm dialog.'); + embedder.test.assertEq(false, data[1]); + embedder.test.succeed(); + return; + } + }; + + var dialogHandler = function(e) { + console.log('webview has requested a dialog.'); + embedder.test.assertEq('confirm', e.messageType); + embedder.test.assertEq(messageText, e.messageText); + // Prevent default to leave cleanup in the GC's hands. + e.preventDefault(); + window.setTimeout(function() { window.gc(); }); + }; + + setUpDialogTest(messageCallback, dialogHandler); +} + +function testDialogPrompt() { + var messageText = 'bleep'; + var defaultPromptText = 'bloop'; + var returnPromptText = 'blah'; + + var messageCallback = function(webview, data) { + if (data[0] == 'connected') { + console.log('The prompt dialog test has started.'); + var msg = ['start-prompt-dialog-test', messageText, defaultPromptText]; + webview.contentWindow.postMessage(JSON.stringify(msg), '*'); + return; + } + + if (data[0] == 'prompt-dialog-result') { + console.log('webview has reported a result for its prompt dialog.'); + embedder.test.assertEq(returnPromptText, data[1]); + embedder.test.succeed(); + return; + } + }; + + var dialogHandler = function(e) { + console.log('webview has requested a dialog.'); + embedder.test.assertEq('prompt', e.messageType); + embedder.test.assertEq(messageText, e.messageText); + embedder.test.assertEq(defaultPromptText, e.defaultPromptText); + e.dialog.ok(returnPromptText); + console.log('The app has responded to the dialog request.'); + }; + + setUpDialogTest(messageCallback, dialogHandler); +} + +embedder.test.testList = { + 'testDialogAlert': testDialogAlert, + 'testDialogConfirm': testDialogConfirm, + 'testDialogConfirmCancel': testDialogConfirmCancel, + 'testDialogConfirmDefaultCancel': testDialogConfirmDefaultCancel, + 'testDialogConfirmDefaultGCCancel': testDialogConfirmDefaultGCCancel, + 'testDialogPrompt': testDialogPrompt +}; + +onload = function() { + chrome.test.getConfig(function(config) { + chrome.test.sendMessage('LAUNCHED'); + }); +}; diff --git a/extensions/test/data/web_view/dialog/inject_dialog.js b/extensions/test/data/web_view/dialog/inject_dialog.js new file mode 100644 index 000000000000..5d7a31eafa0d --- /dev/null +++ b/extensions/test/data/web_view/dialog/inject_dialog.js @@ -0,0 +1,55 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +var embedder = null; + +function reportConnected() { + var msg = ['connected']; + embedder.postMessage(JSON.stringify(msg), '*'); +} + +function reportAlertCompletion(messageText) { + window.alert(messageText); + var msg = ['alert-dialog-done']; + embedder.postMessage(JSON.stringify(msg), '*'); +} + +function reportConfirmDialogResult(messageText) { + var result = window.confirm(messageText); + var msg = ['confirm-dialog-result', result]; + embedder.postMessage(JSON.stringify(msg), '*'); +} + +function reportPromptDialogResult(messageText, defaultPromptText) { + var result = window.prompt(messageText, defaultPromptText); + var msg = ['prompt-dialog-result', result]; + embedder.postMessage(JSON.stringify(msg), '*'); +} + +window.addEventListener('message', function(e) { + embedder = e.source; + var data = JSON.parse(e.data); + switch (data[0]) { + case 'connect': { + reportConnected(); + break; + } + case 'start-confirm-dialog-test': { + var messageText = data[1]; + reportConfirmDialogResult(messageText); + break; + } + case 'start-alert-dialog-test': { + var messageText = data[1]; + reportAlertCompletion(messageText); + break; + } + case 'start-prompt-dialog-test': { + var messageText = data[1]; + var defaultPromptText = data[2]; + reportPromptDialogResult(messageText, defaultPromptText); + break; + } + } +}); diff --git a/extensions/test/data/web_view/dialog/manifest.json b/extensions/test/data/web_view/dialog/manifest.json new file mode 100644 index 000000000000..21dd6473b553 --- /dev/null +++ b/extensions/test/data/web_view/dialog/manifest.json @@ -0,0 +1,14 @@ +{ + "name": "Packaged App Test: JavaScript Dialogs", + "description": "Verifies that the Dialog API works as expected.", + "version": "2", + "manifest_version": 2, + "permissions": [ + "webview" + ], + "app": { + "background": { + "scripts": ["test.js"] + } + } +} diff --git a/extensions/test/data/web_view/dialog/test.js b/extensions/test/data/web_view/dialog/test.js new file mode 100644 index 000000000000..1886432c3055 --- /dev/null +++ b/extensions/test/data/web_view/dialog/test.js @@ -0,0 +1,7 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +chrome.app.runtime.onLaunched.addListener(function() { + chrome.app.window.create('embedder.html', {}, function () {}); +}); -- 2.11.4.GIT