Commit 783645d2 authored by David Dorwin's avatar David Dorwin Committed by Commit Bot

[fuchsia] Extract common Frame setup from WebEngineIntegrationTests

Also breaks out the multiple cases in the *UserAgent tests into
individual WebEngineIntegrationUserAgentTests.

Change-Id: I4aab782d55ff327456d6a2b638739b0c4ec86204
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2421655
Commit-Queue: David Dorwin <ddorwin@chromium.org>
Reviewed-by: default avatarSergey Ulanov <sergeyu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808955}
parent 9e1c9457
......@@ -9,6 +9,7 @@
#include <lib/sys/cpp/component_context.h>
#include <zircon/processargs.h>
#include "base/check.h"
#include "base/command_line.h"
#include "base/files/file_enumerator.h"
#include "base/fuchsia/file_utils.h"
......@@ -137,6 +138,8 @@ class WebEngineIntegrationTestBase : public testing::Test {
return create_params;
}
// Returns the same CreateContextParams as ContextParamsWithAudio() plus the
// testdata content directory.
fuchsia::web::CreateContextParams ContextParamsWithAudioAndTestData() {
fuchsia::web::CreateContextParams create_params = ContextParamsWithAudio();
create_params.mutable_content_directories()->push_back(
......@@ -144,33 +147,90 @@ class WebEngineIntegrationTestBase : public testing::Test {
return create_params;
}
void CreateNavigationListener() {
// Attach a navigation listener, to monitor the state of the Frame.
// Populates |navigation_listener_| with a TestNavigationListener and adds it
// to |frame|, enabling tests to monitor the state of the Frame.
// May only be called once.
void CreateNavigationListener(fuchsia::web::FramePtr* frame) {
DCHECK(!navigation_listener_);
navigation_listener_ =
std::make_unique<cr_fuchsia::TestNavigationListener>();
navigation_listener_binding_ =
std::make_unique<fidl::Binding<fuchsia::web::NavigationEventListener>>(
navigation_listener_.get());
frame_->SetNavigationEventListener(
(*frame)->SetNavigationEventListener(
navigation_listener_binding_->NewBinding());
}
void CreateContextAndFrame(fuchsia::web::CreateContextParams params) {
web_context_provider_->Create(std::move(params), context_.NewRequest());
context_.set_error_handler([](zx_status_t status) { ADD_FAILURE(); });
// Populates |navigation_controller_| with a NavigationController for |frame|.
// May only be called once.
void AddNavigationControllerAndListenerToFrame(
fuchsia::web::FramePtr* frame) {
DCHECK(!navigation_controller_);
context_->CreateFrame(frame_.NewRequest());
frame_.set_error_handler([](zx_status_t status) { ADD_FAILURE(); });
frame_->GetNavigationController(navigation_controller_.NewRequest());
(*frame)->GetNavigationController(navigation_controller_.NewRequest());
navigation_controller_.set_error_handler(
[](zx_status_t status) { ADD_FAILURE(); });
CreateNavigationListener();
CreateNavigationListener(frame);
}
// Populates |context_| with a Context with |params|.
void CreateContext(fuchsia::web::CreateContextParams context_params) {
DCHECK(!context_);
web_context_provider_->Create(std::move(context_params),
context_.NewRequest());
context_.set_error_handler([](zx_status_t status) { ADD_FAILURE(); });
}
// Returns a new Frame created from |context_|.
fuchsia::web::FramePtr CreateFrame() {
DCHECK(context_);
fuchsia::web::FramePtr frame;
context_->CreateFrame(frame.NewRequest());
frame.set_error_handler([](zx_status_t status) { ADD_FAILURE(); });
return frame;
}
// Returns a new Frame with |frame_params| created from |context_|.
fuchsia::web::FramePtr CreateFrameWithParams(
fuchsia::web::CreateFrameParams frame_params) {
DCHECK(context_);
fuchsia::web::FramePtr frame;
context_->CreateFrameWithParams(std::move(frame_params),
frame.NewRequest());
frame.set_error_handler([](zx_status_t status) { ADD_FAILURE(); });
return frame;
}
// Populates |context_| with a Context with |context_params|, |frame_| with a
// new Frame, |navigation_controller_| with a NavigationController request for
// |frame_|, and navigation_listener_| with a TestNavigationListener that is
// added to |frame|.
void CreateContextAndFrame(fuchsia::web::CreateContextParams context_params) {
ASSERT_FALSE(frame_);
CreateContext(std::move(context_params));
frame_ = CreateFrame();
AddNavigationControllerAndListenerToFrame(&frame_);
}
// Same as CreateContextAndFrame() but uses |frame_params| to create the
// Frame.
void CreateContextAndFrameWithParams(
fuchsia::web::CreateContextParams context_params,
fuchsia::web::CreateFrameParams frame_params) {
ASSERT_FALSE(frame_);
CreateContext(std::move(context_params));
frame_ = CreateFrameWithParams(std::move(frame_params));
AddNavigationControllerAndListenerToFrame(&frame_);
}
void CreateContextAndExpectError(fuchsia::web::CreateContextParams params,
zx_status_t expected_error) {
ASSERT_FALSE(context_);
web_context_provider_->Create(std::move(params), context_.NewRequest());
base::RunLoop run_loop;
context_.set_error_handler([&run_loop, expected_error](zx_status_t status) {
......@@ -266,17 +326,21 @@ class WebEngineIntegrationTest : public WebEngineIntegrationTestBase {
}
};
TEST_F(WebEngineIntegrationTest, ValidUserAgent) {
const std::string kEchoHeaderPath =
class WebEngineIntegrationUserAgentTest : public WebEngineIntegrationTest {
protected:
GURL GetEchoUserAgentUrl() {
static std::string echo_user_agent_header_path =
std::string("/echoheader?") + net::HttpRequestHeaders::kUserAgent;
const GURL kEchoUserAgentUrl(embedded_test_server_.GetURL(kEchoHeaderPath));
return embedded_test_server_.GetURL(echo_user_agent_header_path);
}
};
{
TEST_F(WebEngineIntegrationUserAgentTest, ValidProductOnly) {
// Create a Context with just an embedder product specified.
fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_product(kValidUserAgentProduct);
CreateContextAndFrameAndLoadUrl(std::move(create_params),
kEchoUserAgentUrl);
GetEchoUserAgentUrl());
// Query & verify that the header echoed into the document body contains
// the product tag.
......@@ -287,15 +351,15 @@ TEST_F(WebEngineIntegrationTest, ValidUserAgent) {
// Query & verify that the navigator.userAgent contains the product tag.
result = ExecuteJavaScriptWithStringResult("navigator.userAgent;");
EXPECT_TRUE(result.find(kValidUserAgentProduct) != std::string::npos);
}
}
{
TEST_F(WebEngineIntegrationUserAgentTest, ValidProductAndVersion) {
// Create a Context with both product and version specified.
fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_product(kValidUserAgentProduct);
create_params.set_user_agent_version(kValidUserAgentVersion);
CreateContextAndFrameAndLoadUrl(std::move(create_params),
kEchoUserAgentUrl);
GetEchoUserAgentUrl());
// Query & verify that the header echoed into the document body contains
// both product & version.
......@@ -308,35 +372,28 @@ TEST_F(WebEngineIntegrationTest, ValidUserAgent) {
result = ExecuteJavaScriptWithStringResult("navigator.userAgent;");
EXPECT_TRUE(result.find(kValidUserAgentProductAndVersion) !=
std::string::npos);
}
}
TEST_F(WebEngineIntegrationTest, InvalidUserAgent) {
const std::string kEchoHeaderPath =
std::string("/echoheader?") + net::HttpRequestHeaders::kUserAgent;
const GURL kEchoUserAgentUrl(embedded_test_server_.GetURL(kEchoHeaderPath));
{
TEST_F(WebEngineIntegrationUserAgentTest, InvalidProduct) {
// Try to create a Context with an invalid embedder product tag.
fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_product(kInvalidUserAgentProduct);
CreateContextAndExpectError(std::move(create_params), ZX_ERR_INVALID_ARGS);
}
}
{
TEST_F(WebEngineIntegrationUserAgentTest, VersionOnly) {
// Try to create a Context with an embedder version but no product.
fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_version(kValidUserAgentVersion);
CreateContextAndExpectError(std::move(create_params), ZX_ERR_INVALID_ARGS);
}
}
{
TEST_F(WebEngineIntegrationUserAgentTest, ValidProductAndInvalidVersion) {
// Try to create a Context with valid product tag, but invalid version.
fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_product(kValidUserAgentProduct);
create_params.set_user_agent_version(kInvalidUserAgentVersion);
CreateContextAndExpectError(std::move(create_params), ZX_ERR_INVALID_ARGS);
}
}
// Check that if the CreateContextParams has |remote_debugging_port| set then:
......@@ -344,38 +401,21 @@ TEST_F(WebEngineIntegrationTest, InvalidUserAgent) {
// - DevTools closes when the last debuggable Frame is closed.
TEST_F(WebEngineIntegrationTest, RemoteDebuggingPort) {
// Create a Context with remote debugging enabled via an ephemeral port.
fuchsia::web::CreateContextParams create_params;
auto directory = base::fuchsia::OpenDirectory(
base::FilePath(base::fuchsia::kServiceDirectoryPath));
ASSERT_TRUE(directory.is_valid());
create_params.set_service_directory(std::move(directory));
fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_remote_debugging_port(0);
fuchsia::web::ContextPtr web_context;
web_context_provider_->Create(std::move(create_params),
web_context.NewRequest());
web_context.set_error_handler([](zx_status_t status) {
ZX_LOG(ERROR, status);
ADD_FAILURE();
});
// Create a Frame with remote debugging enabled.
fuchsia::web::FramePtr web_frame;
fuchsia::web::CreateFrameParams create_frame_params;
create_frame_params.set_enable_remote_debugging(true);
web_context->CreateFrameWithParams(std::move(create_frame_params),
web_frame.NewRequest());
web_frame.set_error_handler([](zx_status_t status) {
ZX_LOG(ERROR, status);
ADD_FAILURE();
});
CreateContextAndFrameWithParams(std::move(create_params),
std::move(create_frame_params));
// Expect to receive a notification of the selected DevTools port.
base::RunLoop run_loop;
cr_fuchsia::ResultReceiver<
fuchsia::web::Context_GetRemoteDebuggingPort_Result>
port_receiver(run_loop.QuitClosure());
web_context->GetRemoteDebuggingPort(
context_->GetRemoteDebuggingPort(
cr_fuchsia::CallbackToFitFunction(port_receiver.GetReceiveCallback()));
run_loop.Run();
......@@ -383,20 +423,11 @@ TEST_F(WebEngineIntegrationTest, RemoteDebuggingPort) {
uint16_t remote_debugging_port = port_receiver->response().port;
ASSERT_TRUE(remote_debugging_port != 0);
// Navigate the Frame to a URL, and verify the URL via DevTools.
fuchsia::web::NavigationControllerPtr nav_controller;
web_frame->GetNavigationController(nav_controller.NewRequest());
cr_fuchsia::TestNavigationListener navigation_listener;
fidl::Binding<fuchsia::web::NavigationEventListener> listener_binding(
&navigation_listener);
web_frame->SetNavigationEventListener(listener_binding.NewBinding());
// Navigate to a URL.
GURL url = embedded_test_server_.GetURL("/defaultresponse");
ASSERT_TRUE(cr_fuchsia::LoadUrlAndExpectResponse(
nav_controller.get(), fuchsia::web::LoadUrlParams(), url.spec()));
navigation_listener.RunUntilUrlEquals(url);
navigation_controller_.get(), fuchsia::web::LoadUrlParams(), url.spec()));
navigation_listener_->RunUntilUrlEquals(url);
base::Value devtools_list =
cr_fuchsia::GetDevToolsListFromPort(remote_debugging_port);
......@@ -409,9 +440,7 @@ TEST_F(WebEngineIntegrationTest, RemoteDebuggingPort) {
// Create a second frame, without remote debugging enabled. The remote
// debugging service should still report a single Frame is present.
fuchsia::web::FramePtr web_frame2;
web_context->CreateFrame(web_frame2.NewRequest());
web_frame2.set_error_handler([](zx_status_t status) { ADD_FAILURE(); });
fuchsia::web::FramePtr web_frame2 = CreateFrame();
devtools_list = cr_fuchsia::GetDevToolsListFromPort(remote_debugging_port);
ASSERT_TRUE(devtools_list.is_list());
......@@ -424,9 +453,9 @@ TEST_F(WebEngineIntegrationTest, RemoteDebuggingPort) {
// Tear down the debuggable Frame. The remote debugging service should have
// shut down.
base::RunLoop controller_run_loop;
nav_controller.set_error_handler(
navigation_controller_.set_error_handler(
[&controller_run_loop](zx_status_t) { controller_run_loop.Quit(); });
web_frame.Unbind();
frame_.Unbind();
// Wait until the NavigationController shuts down to ensure WebEngine has
// handled the Frame tear down.
......@@ -463,21 +492,13 @@ TEST_F(WebEngineIntegrationTest, RemoteDebuggingPort) {
// Check that remote debugging requests for Frames in non-debuggable Contexts
// cause an error to be reported.
TEST_F(WebEngineIntegrationTest, RequestDebuggableFrameInNonDebuggableContext) {
fuchsia::web::CreateContextParams create_params = DefaultContextParams();
fuchsia::web::ContextPtr web_context;
web_context_provider_->Create(std::move(create_params),
web_context.NewRequest());
web_context.set_error_handler([](zx_status_t status) { ADD_FAILURE(); });
fuchsia::web::FramePtr web_frame;
fuchsia::web::CreateFrameParams create_frame_params;
create_frame_params.set_enable_remote_debugging(true);
web_context->CreateFrameWithParams(std::move(create_frame_params),
web_frame.NewRequest());
CreateContextAndFrameWithParams(DefaultContextParams(),
std::move(create_frame_params));
base::RunLoop loop;
web_frame.set_error_handler(
frame_.set_error_handler(
[quit_loop = loop.QuitClosure()](zx_status_t status) {
EXPECT_EQ(status, ZX_ERR_INVALID_ARGS);
quit_loop.Run();
......@@ -490,10 +511,7 @@ TEST_F(WebEngineIntegrationTest, ContentDirectoryProvider) {
const GURL kUrl("fuchsia-dir://testdata/title1.html");
constexpr char kTitle[] = "title 1";
fuchsia::web::CreateContextParams create_params =
DefaultContextParamsWithTestData();
CreateContextAndFrame(std::move(create_params));
CreateContextAndFrame(DefaultContextParamsWithTestData());
// Navigate to test1.html and verify that the resource was correctly
// downloaded and interpreted by inspecting the document title.
......@@ -560,9 +578,7 @@ TEST_F(WebEngineIntegrationTest, PlayVideo) {
}
void WebEngineIntegrationTestBase::RunPermissionTest(bool grant) {
fuchsia::web::CreateContextParams create_params =
DefaultContextParamsWithTestData();
CreateContextAndFrame(std::move(create_params));
CreateContextAndFrame(DefaultContextParamsWithTestData());
if (grant) {
GrantPermission(fuchsia::web::PermissionType::MICROPHONE,
......@@ -655,8 +671,7 @@ TEST_F(WebEngineIntegrationTest, SetBlockMediaLoading_SetBlockedAfterLoading) {
}
TEST_F(WebEngineIntegrationTest, WebGLContextAbsentWithoutVulkanFeature) {
fuchsia::web::CreateContextParams create_params = DefaultContextParams();
CreateContextAndFrame(std::move(create_params));
CreateContextAndFrame(DefaultContextParams());
EXPECT_TRUE(cr_fuchsia::LoadUrlAndExpectResponse(
navigation_controller_.get(), fuchsia::web::LoadUrlParams(),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment