Skip to content

Commit

Permalink
Rip apart TestInterfaces.
Browse files Browse the repository at this point in the history
Pull most of TestInterfaces out into TestRunner so that we don't need
to keep around both classes which overlap in responsibilities.

Stop plumbing through TestInterfaces to get to TestRunner, expose it
from the same places that currently expose TestInterfaces.

Add a list of main frames to TestRunner, and use that for windowCount()
which changes a couple of test expectations, as they were counting the
proxied RenderViewHost of a cross-site window. The problem is they would
also count same-site fragments of windows opened by other renderer
processes.

R=avi@chromium.org

Bug: 866140
Change-Id: I065916b4e5184a0fbb95981a2a69b6b5a90b0023
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2281067
Commit-Queue: danakj <danakj@chromium.org>
Auto-Submit: danakj <danakj@chromium.org>
Reviewed-by: Avi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#785822}
  • Loading branch information
danakj authored and Commit Bot committed Jul 7, 2020
1 parent 44d0462 commit aa87f00
Show file tree
Hide file tree
Showing 20 changed files with 199 additions and 358 deletions.
185 changes: 64 additions & 121 deletions content/shell/renderer/web_test/blink_test_runner.cc
Original file line number Diff line number Diff line change
@@ -1,101 +1,26 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Copyright 2012 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.

#include "content/shell/renderer/web_test/blink_test_runner.h"

#include <stddef.h>

#include <algorithm>
#include <clocale>
#include <cmath>
#include <memory>
#include <string>
#include <utility>

#include "base/base64.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/debug/debugger.h"
#include "base/files/file_path.h"
#include "base/hash/md5.h"
#include "base/location.h"
#include "base/macros.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "build/build_config.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/service_names.mojom.h"
#include "content/public/common/url_constants.h"
#include "content/public/common/use_zoom_for_dsf_policy.h"
#include "content/public/common/web_preferences.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/render_view.h"
#include "content/shell/common/shell_switches.h"
#include "content/shell/renderer/web_test/blink_test_helpers.h"
#include "content/shell/renderer/web_test/gamepad_controller.h"
#include "content/shell/renderer/web_test/pixel_dump.h"
#include "content/shell/renderer/web_test/test_interfaces.h"
#include "content/shell/renderer/web_test/test_runner.h"
#include "content/shell/renderer/web_test/web_test_render_thread_observer.h"
#include "content/shell/renderer/web_test/web_frame_test_proxy.h"
#include "content/shell/renderer/web_test/web_view_test_proxy.h"
#include "ipc/ipc_sync_channel.h"
#include "media/base/audio_capturer_source.h"
#include "media/base/audio_parameters.h"
#include "media/capture/video_capturer_source.h"
#include "media/media_buildflags.h"
#include "net/base/filename_util.h"
#include "net/base/net_errors.h"
#include "services/service_manager/public/cpp/binder_registry.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/common/input/web_input_event.h"
#include "third_party/blink/public/platform/file_path_conversion.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h"
#include "third_party/blink/public/platform/web_rect.h"
#include "third_party/blink/public/platform/web_size.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/platform/web_url.h"
#include "third_party/blink/public/platform/web_url_error.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/public/platform/web_url_response.h"
#include "third_party/blink/public/web/blink.h"
#include "third_party/blink/public/web/web_context_menu_data.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_element.h"
#include "third_party/blink/public/web/web_frame.h"
#include "third_party/blink/public/web/web_frame_widget.h"
#include "third_party/blink/public/web/web_history_item.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_navigation_params.h"
#include "third_party/blink/public/web/web_script_source.h"
#include "third_party/blink/public/web/web_view.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/icc_profile.h"

using blink::Platform;
using blink::WebContextMenuData;
using blink::WebElement;
using blink::WebFrame;
using blink::WebHistoryItem;
using blink::WebLocalFrame;
using blink::WebRect;
using blink::WebScriptSource;
using blink::WebSize;
using blink::WebString;
using blink::WebURL;
using blink::WebURLError;
using blink::WebURLRequest;
using blink::WebVector;
using blink::WebView;

namespace content {

Expand All @@ -105,7 +30,7 @@ BlinkTestRunner::BlinkTestRunner(WebViewTestProxy* web_view_test_proxy)

BlinkTestRunner::~BlinkTestRunner() = default;

WebString BlinkTestRunner::GetAbsoluteWebStringFromUTF8Path(
blink::WebString BlinkTestRunner::GetAbsoluteWebStringFromUTF8Path(
const std::string& utf8_path) {
base::FilePath path = base::FilePath::FromUTF8Unsafe(utf8_path);
if (!path.IsAbsolute()) {
Expand All @@ -117,20 +42,14 @@ WebString BlinkTestRunner::GetAbsoluteWebStringFromUTF8Path(
return blink::FilePathToWebString(path);
}

void BlinkTestRunner::SetBluetoothFakeAdapter(const std::string& adapter_name,
base::OnceClosure callback) {
GetBluetoothFakeAdapterSetter().Set(adapter_name, std::move(callback));
}

void BlinkTestRunner::TestFinished() {
TestInterfaces* interfaces = web_view_test_proxy_->test_interfaces();
TestRunner* test_runner = interfaces->GetTestRunner();
TestRunner* test_runner = web_view_test_proxy_->GetTestRunner();

// We might get multiple TestFinished calls, ensure to only process the dump
// once.
if (!interfaces->TestIsRunning())
if (!test_runner->TestIsRunning())
return;
interfaces->SetTestIsRunning(false);
test_runner->SetTestIsRunning(false);

// If we're not in the main frame, then ask the browser to redirect the call
// to the main frame instead.
Expand All @@ -142,8 +61,7 @@ void BlinkTestRunner::TestFinished() {
// Now we know that we're in the main frame, we should generate dump results.
// Clean out the lifecycle if needed before capturing the web tree
// dump and pixels from the compositor.
auto* web_frame =
web_view_test_proxy_->GetWebView()->MainFrame()->ToWebLocalFrame();
auto* web_frame = web_view_test_proxy_->GetMainRenderFrame()->GetWebFrame();
web_frame->FrameWidget()->UpdateAllLifecyclePhases(
blink::DocumentUpdateReason::kTest);

Expand Down Expand Up @@ -193,15 +111,14 @@ void BlinkTestRunner::TestFinished() {

void BlinkTestRunner::CaptureLocalAudioDump() {
TRACE_EVENT0("shell", "BlinkTestRunner::CaptureLocalAudioDump");
TestInterfaces* interfaces = web_view_test_proxy_->test_interfaces();
TestRunner* test_runner = web_view_test_proxy_->GetTestRunner();
dump_result_->audio.emplace();
interfaces->GetTestRunner()->GetAudioData(&*dump_result_->audio);
test_runner->GetAudioData(&*dump_result_->audio);
}

void BlinkTestRunner::CaptureLocalLayoutDump() {
TRACE_EVENT0("shell", "BlinkTestRunner::CaptureLocalLayoutDump");
TestInterfaces* interfaces = web_view_test_proxy_->test_interfaces();
TestRunner* test_runner = interfaces->GetTestRunner();
TestRunner* test_runner = web_view_test_proxy_->GetTestRunner();
std::string layout;
if (test_runner->HasCustomTextDump(&layout)) {
dump_result_->layout.emplace(layout + "\n");
Expand All @@ -221,12 +138,12 @@ void BlinkTestRunner::CaptureLocalPixelsDump() {

// Test finish should only be processed in the BlinkTestRunner associated
// with the current, non-swapped-out RenderView.
DCHECK(web_view_test_proxy_->GetWebView()->MainFrame()->IsWebLocalFrame());
DCHECK(web_view_test_proxy_->GetMainRenderFrame());

waiting_for_pixels_dump_result_ = true;

TestInterfaces* interfaces = web_view_test_proxy_->test_interfaces();
interfaces->GetTestRunner()->DumpPixelsAsync(
TestRunner* test_runner = web_view_test_proxy_->GetTestRunner();
test_runner->DumpPixelsAsync(
web_view_test_proxy_,
base::BindOnce(&BlinkTestRunner::OnPixelsDumpCompleted,
base::Unretained(this)));
Expand Down Expand Up @@ -297,63 +214,89 @@ void BlinkTestRunner::DidCommitNavigationInMainFrame() {
if (!waiting_for_reset_navigation_to_about_blank_)
return;

WebFrame* main_frame = web_view_test_proxy_->GetWebView()->MainFrame();
DCHECK(main_frame->IsWebLocalFrame());
TestRunner* test_runner = web_view_test_proxy_->GetTestRunner();
RenderFrameImpl* main_frame = web_view_test_proxy_->GetMainRenderFrame();
DCHECK(main_frame);

// This would mean some other navigation was already happening when the test
// ended, the about:blank should still be coming.
GURL url = main_frame->ToWebLocalFrame()->GetDocumentLoader()->GetUrl();
GURL url = main_frame->GetWebFrame()->GetDocumentLoader()->GetUrl();
if (!url.IsAboutBlank())
return;

waiting_for_reset_navigation_to_about_blank_ = false;
web_view_test_proxy_->test_interfaces()->ResetAll();
test_runner->Reset(static_cast<WebFrameTestProxy*>(main_frame));
// Ack to the browser (this could converted to be a mojo reply).
GetWebTestControlHostRemote()->ResetRendererAfterWebTestDone();
}

mojom::WebTestBluetoothFakeAdapterSetter&
BlinkTestRunner::GetBluetoothFakeAdapterSetter() {
if (!bluetooth_fake_adapter_setter_) {
RenderThread::Get()->BindHostReceiver(
bluetooth_fake_adapter_setter_.BindNewPipeAndPassReceiver());
}
return *bluetooth_fake_adapter_setter_;
}

mojo::AssociatedRemote<mojom::WebTestControlHost>&
BlinkTestRunner::GetWebTestControlHostRemote() {
TestInterfaces* interfaces = web_view_test_proxy_->test_interfaces();
TestRunner* test_runner = interfaces->GetTestRunner();
TestRunner* test_runner = web_view_test_proxy_->GetTestRunner();
return test_runner->GetWebTestControlHostRemote();
}

mojo::AssociatedRemote<mojom::WebTestClient>&
BlinkTestRunner::GetWebTestClientRemote() {
TestInterfaces* interfaces = web_view_test_proxy_->test_interfaces();
TestRunner* test_runner = interfaces->GetTestRunner();
TestRunner* test_runner = web_view_test_proxy_->GetTestRunner();
return test_runner->GetWebTestClientRemote();
}

void BlinkTestRunner::OnSetupRendererProcessForNonTestWindow() {
DCHECK(!is_main_window_);

TestInterfaces* interfaces = web_view_test_proxy_->test_interfaces();
TestRunner* test_runner = web_view_test_proxy_->GetTestRunner();
// Allows the window to receive replicated WebTestRuntimeFlags and to
// control or end the test.
interfaces->SetTestIsRunning(true);
test_runner->SetTestIsRunning(true);
}

void BlinkTestRunner::ApplyTestConfiguration(
mojom::WebTestRunTestConfigurationPtr params) {
TestInterfaces* interfaces = web_view_test_proxy_->test_interfaces();
TestRunner* test_runner = web_view_test_proxy_->GetTestRunner();

test_config_ = params.Clone();

is_main_window_ = true;
interfaces->SetMainView(web_view_test_proxy_->GetWebView());
// TODO(danakj): These should not differ, so we should SetDelegate() here.
test_runner->SetMainView(web_view_test_proxy_->GetWebView());
test_runner->SetTestIsRunning(true);

std::string spec = GURL(params->test_url).spec();
size_t path_start = spec.rfind("web_tests/");
if (path_start != std::string::npos)
spec = spec.substr(path_start);

bool is_devtools_test =
spec.find("/devtools/") != std::string::npos ||
spec.find("/inspector-protocol/") != std::string::npos;
if (is_devtools_test)
test_runner->SetDumpConsoleMessages(false);

// In protocol mode (see TestInfo::protocol_mode), we dump layout only when
// requested by the test. In non-protocol mode, we dump layout by default
// because the layout may be the only interesting thing to the user while
// we don't dump non-human-readable binary data. In non-protocol mode, we
// still generate pixel results (though don't dump them) to let the renderer
// execute the same code regardless of the protocol mode, e.g. for ease of
// debugging a web test issue.
if (!params->protocol_mode)
test_runner->SetShouldDumpAsLayout(true);

// For http/tests/loading/, which is served via httpd and becomes /loading/.
if (spec.find("/loading/") != std::string::npos)
test_runner->SetShouldDumpFrameLoadCallbacks(true);
if (spec.find("/dumpAsText/") != std::string::npos) {
test_runner->SetShouldDumpAsText(true);
test_runner->SetShouldGeneratePixelResults(false);
}
test_runner->SetV8CacheDisabled(is_devtools_test);

interfaces->SetTestIsRunning(true);
interfaces->ConfigureForTestWithURL(params->test_url, params->protocol_mode);
if (spec.find("/external/wpt/") != std::string::npos ||
spec.find("/external/csswg-test/") != std::string::npos ||
spec.find("://web-platform.test") != std::string::npos ||
spec.find("/harness-tests/wpt/") != std::string::npos)
test_runner->SetIsWebPlatformTestsMode();
}

void BlinkTestRunner::OnReplicateTestConfiguration(
Expand Down Expand Up @@ -401,8 +344,8 @@ void BlinkTestRunner::OnFinishTestInMainWindow() {

// Avoid a situation where TestFinished is called twice, because
// of a racey test finish in 2 secondary renderers.
TestInterfaces* interfaces = web_view_test_proxy_->test_interfaces();
if (!interfaces->TestIsRunning())
TestRunner* test_runner = web_view_test_proxy_->GetTestRunner();
if (!test_runner->TestIsRunning())
return;

TestFinished();
Expand Down
10 changes: 0 additions & 10 deletions content/shell/renderer/web_test/blink_test_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "base/strings/string16.h"
#include "content/public/common/page_state.h"
#include "content/shell/common/web_test/web_test.mojom.h"
#include "content/shell/common/web_test/web_test_bluetooth_fake_adapter_setter.mojom.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/blink/public/platform/web_string.h"
Expand All @@ -43,11 +42,6 @@ class BlinkTestRunner {
// Convert the provided relative path into an absolute path.
blink::WebString GetAbsoluteWebStringFromUTF8Path(const std::string& path);

// Set the bluetooth adapter while running a web test, uses Mojo to
// communicate with the browser.
void SetBluetoothFakeAdapter(const std::string& adapter_name,
base::OnceClosure callback);

// Invoked when the test finished.
void TestFinished();

Expand Down Expand Up @@ -89,10 +83,6 @@ class BlinkTestRunner {
void CaptureLocalLayoutDump();
void CaptureLocalPixelsDump();

mojom::WebTestBluetoothFakeAdapterSetter& GetBluetoothFakeAdapterSetter();
mojo::Remote<mojom::WebTestBluetoothFakeAdapterSetter>
bluetooth_fake_adapter_setter_;

mojo::AssociatedRemote<mojom::WebTestControlHost>&
GetWebTestControlHostRemote();
mojo::AssociatedRemote<mojom::WebTestClient>& GetWebTestClientRemote();
Expand Down

0 comments on commit aa87f00

Please sign in to comment.