1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "content/web_test/renderer/web_view_test_proxy.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include "content/web_test/common/web_test_string_util.h"
11 #include "content/web_test/renderer/test_runner.h"
12 #include "content/web_test/renderer/web_frame_test_proxy.h"
13 #include "net/base/filename_util.h"
14 #include "third_party/blink/public/mojom/v8_cache_options.mojom.h"
15 #include "third_party/blink/public/platform/file_path_conversion.h"
16 #include "third_party/blink/public/platform/web_url_request.h"
17 #include "third_party/blink/public/web/web_frame.h"
18 #include "third_party/blink/public/web/web_local_frame.h"
19 #include "third_party/blink/public/web/web_print_params.h"
20 #include "third_party/blink/public/web/web_settings.h"
21 #include "third_party/blink/public/web/web_view.h"
22 
23 namespace content {
24 
WebViewTestProxy(AgentSchedulingGroup & agent_scheduling_group,CompositorDependencies * compositor_deps,const mojom::CreateViewParams & params,TestRunner * test_runner)25 WebViewTestProxy::WebViewTestProxy(AgentSchedulingGroup& agent_scheduling_group,
26                                    CompositorDependencies* compositor_deps,
27                                    const mojom::CreateViewParams& params,
28                                    TestRunner* test_runner)
29     : RenderViewImpl(agent_scheduling_group, compositor_deps, params),
30       test_runner_(test_runner) {
31   test_runner_->AddRenderView(this);
32 }
33 
~WebViewTestProxy()34 WebViewTestProxy::~WebViewTestProxy() {
35   test_runner_->RemoveRenderView(this);
36 }
37 
CreateView(blink::WebLocalFrame * creator,const blink::WebURLRequest & request,const blink::WebWindowFeatures & features,const blink::WebString & frame_name,blink::WebNavigationPolicy policy,network::mojom::WebSandboxFlags sandbox_flags,const blink::FeaturePolicyFeatureState & opener_feature_state,const blink::SessionStorageNamespaceId & session_storage_namespace_id,bool & consumed_user_gesture)38 blink::WebView* WebViewTestProxy::CreateView(
39     blink::WebLocalFrame* creator,
40     const blink::WebURLRequest& request,
41     const blink::WebWindowFeatures& features,
42     const blink::WebString& frame_name,
43     blink::WebNavigationPolicy policy,
44     network::mojom::WebSandboxFlags sandbox_flags,
45     const blink::FeaturePolicyFeatureState& opener_feature_state,
46     const blink::SessionStorageNamespaceId& session_storage_namespace_id,
47     bool& consumed_user_gesture) {
48   if (test_runner_->ShouldDumpNavigationPolicy()) {
49     test_runner_->PrintMessage(
50         "Default policy for createView for '" +
51         web_test_string_util::URLDescription(request.Url()) + "' is '" +
52         web_test_string_util::WebNavigationPolicyToString(policy) + "'\n");
53   }
54 
55   if (!test_runner_->CanOpenWindows())
56     return nullptr;
57 
58   if (test_runner_->ShouldDumpCreateView()) {
59     test_runner_->PrintMessage(
60         std::string("createView(") +
61         web_test_string_util::URLDescription(request.Url()) + ")\n");
62   }
63   return RenderViewImpl::CreateView(creator, request, features, frame_name,
64                                     policy, sandbox_flags, opener_feature_state,
65                                     session_storage_namespace_id,
66                                     consumed_user_gesture);
67 }
68 
PrintPage(blink::WebLocalFrame * frame)69 void WebViewTestProxy::PrintPage(blink::WebLocalFrame* frame) {
70   // This is using the main frame for the size, but maybe it should be using the
71   // frame's size.
72   blink::WebSize page_size_in_pixels =
73       GetMainRenderFrame()->GetLocalRootRenderWidget()->GetWebWidget()->Size();
74   if (page_size_in_pixels.IsEmpty())
75     return;
76   blink::WebPrintParams print_params(page_size_in_pixels);
77   frame->PrintBegin(print_params, blink::WebNode());
78   frame->PrintEnd();
79 }
80 
AcceptLanguages()81 blink::WebString WebViewTestProxy::AcceptLanguages() {
82   return blink::WebString::FromUTF8(test_runner_->GetAcceptLanguages());
83 }
84 
Reset()85 void WebViewTestProxy::Reset() {
86   accessibility_controller_.Reset();
87   // |text_input_controller_| doesn't have any state to reset.
88 
89   // Resets things on the WebView that TestRunnerBindings can modify.
90   test_runner_->ResetWebView(this);
91 }
92 
Install(blink::WebLocalFrame * frame)93 void WebViewTestProxy::Install(blink::WebLocalFrame* frame) {
94   accessibility_controller_.Install(frame);
95   text_input_controller_.Install(frame);
96 }
97 
GetAbsoluteWebStringFromUTF8Path(const std::string & utf8_path)98 blink::WebString WebViewTestProxy::GetAbsoluteWebStringFromUTF8Path(
99     const std::string& utf8_path) {
100   base::FilePath path = base::FilePath::FromUTF8Unsafe(utf8_path);
101   if (!path.IsAbsolute()) {
102     GURL base_url =
103         net::FilePathToFileURL(test_config_.current_working_directory.Append(
104             FILE_PATH_LITERAL("foo")));
105     net::FileURLToFilePath(base_url.Resolve(utf8_path), &path);
106   }
107   return blink::FilePathToWebString(path);
108 }
109 
SetTestConfiguration(mojom::WebTestRunTestConfigurationPtr params,bool starting_test)110 void WebViewTestProxy::SetTestConfiguration(
111     mojom::WebTestRunTestConfigurationPtr params,
112     bool starting_test) {
113   test_config_ = std::move(*params);
114   // Sets this view as being part of the main test window. The main test window
115   // doesn't change, so once set this remains always true.
116   is_main_window_ = true;
117 
118   test_runner_->SetTestIsRunning(true);
119 
120   std::string spec = GURL(test_config_.test_url).spec();
121   size_t path_start = spec.rfind("web_tests/");
122   if (path_start != std::string::npos)
123     spec = spec.substr(path_start);
124 
125   bool is_devtools_test =
126       spec.find("/devtools/") != std::string::npos ||
127       spec.find("/inspector-protocol/") != std::string::npos;
128   if (is_devtools_test)
129     test_runner_->SetDumpConsoleMessages(false);
130 
131   // In protocol mode (see TestInfo::protocol_mode), we dump layout only when
132   // requested by the test. In non-protocol mode, we dump layout by default
133   // because the layout may be the only interesting thing to the user while
134   // we don't dump non-human-readable binary data. In non-protocol mode, we
135   // still generate pixel results (though don't dump them) to let the renderer
136   // execute the same code regardless of the protocol mode, e.g. for ease of
137   // debugging a web test issue.
138   if (!test_config_.protocol_mode)
139     test_runner_->SetShouldDumpAsLayout(true);
140 
141   // For http/tests/loading/, which is served via httpd and becomes /loading/.
142   if (spec.find("/loading/") != std::string::npos)
143     test_runner_->SetShouldDumpFrameLoadCallbacks(true);
144 
145   if (spec.find("/external/wpt/") != std::string::npos ||
146       spec.find("/external/csswg-test/") != std::string::npos ||
147       spec.find("://web-platform.test") != std::string::npos ||
148       spec.find("/harness-tests/wpt/") != std::string::npos)
149     test_runner_->SetIsWebPlatformTestsMode();
150 
151   GetWebView()->GetSettings()->SetV8CacheOptions(
152       is_devtools_test ? blink::mojom::V8CacheOptions::kNone
153                        : blink::mojom::V8CacheOptions::kDefault);
154 
155   if (starting_test) {
156     DCHECK(GetMainRenderFrame());
157     // If focus was in a child frame, it gets lost when we navigate to the next
158     // test, but we want to start with focus in the main frame for every test.
159     // Focus is controlled by the renderer, so we must do the reset here.
160     GetWebView()->SetFocusedFrame(GetMainRenderFrame()->GetWebFrame());
161   }
162 }
163 
164 }  // namespace content
165