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 #ifndef CONTENT_WEB_TEST_RENDERER_WEB_VIEW_TEST_PROXY_H_
6 #define CONTENT_WEB_TEST_RENDERER_WEB_VIEW_TEST_PROXY_H_
7 
8 #include <memory>
9 #include <string>
10 #include <utility>
11 
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "build/build_config.h"
15 #include "content/renderer/agent_scheduling_group.h"
16 #include "content/renderer/render_view_impl.h"
17 #include "content/web_test/common/web_test.mojom.h"
18 #include "content/web_test/renderer/accessibility_controller.h"
19 #include "content/web_test/renderer/text_input_controller.h"
20 #include "third_party/blink/public/common/page/drag_operation.h"
21 #include "third_party/blink/public/platform/web_rect.h"
22 #include "third_party/blink/public/platform/web_url_error.h"
23 #include "third_party/blink/public/platform/web_url_request.h"
24 #include "third_party/blink/public/web/web_dom_message_event.h"
25 #include "third_party/blink/public/web/web_history_commit_type.h"
26 #include "third_party/blink/public/web/web_navigation_policy.h"
27 #include "third_party/blink/public/web/web_view_client.h"
28 #include "third_party/blink/public/web/web_widget_client.h"
29 
30 namespace blink {
31 class WebLocalFrame;
32 class WebString;
33 class WebView;
34 struct WebWindowFeatures;
35 }  // namespace blink
36 
37 namespace content {
38 class AccessibilityController;
39 class TestRunner;
40 class TextInputController;
41 
42 // WebViewTestProxy is used to run web tests. This class is a partial fake
43 // implementation of RenderViewImpl that overrides the minimal necessary
44 // portions of RenderViewImpl to allow for use in web tests.
45 //
46 // This method of injecting test functionality is an outgrowth of legacy.
47 // In particular, classic dependency injection does not work easily
48 // because the RenderWidget class is too large with too much entangled
49 // state, making it hard to factor out creation points for injection.
50 //
51 // While implementing a fake via partial overriding of a class leads to
52 // a fragile base class problem and implicit coupling of the test code
53 // and production code, it is the most viable mechanism available without
54 // a huge refactor.
55 //
56 // Historically, the overridden functionality has been small enough to not
57 // cause too much trouble. If that changes, then this entire testing
58 // architecture should be revisited.
59 class WebViewTestProxy : public RenderViewImpl {
60  public:
61   explicit WebViewTestProxy(AgentSchedulingGroup& agent_scheduling_group,
62                             CompositorDependencies* compositor_deps,
63                             const mojom::CreateViewParams& params,
64                             TestRunner* test_runner);
65 
66   // WebViewClient implementation.
67   blink::WebView* CreateView(
68       blink::WebLocalFrame* creator,
69       const blink::WebURLRequest& request,
70       const blink::WebWindowFeatures& features,
71       const blink::WebString& frame_name,
72       blink::WebNavigationPolicy policy,
73       network::mojom::WebSandboxFlags sandbox_flags,
74       const blink::FeaturePolicyFeatureState&,
75       const blink::SessionStorageNamespaceId& session_storage_namespace_id,
76       bool& consumed_user_gesture) override;
77   void PrintPage(blink::WebLocalFrame* frame) override;
78   blink::WebString AcceptLanguages() override;
79 
GetTestRunner()80   TestRunner* GetTestRunner() { return test_runner_; }
accessibility_controller()81   AccessibilityController* accessibility_controller() {
82     return &accessibility_controller_;
83   }
84 
85   void Reset();
86   void Install(blink::WebLocalFrame* frame);
87 
88   // Convert the provided relative path into an absolute path.
89   blink::WebString GetAbsoluteWebStringFromUTF8Path(const std::string& path);
90 
91   // Called on each RenderView that is part of the main test window, to give
92   // the test configuration, indicate to the RenderView that it is part of the
93   // main window, as well as to start the test.
94   void SetTestConfiguration(mojom::WebTestRunTestConfigurationPtr params,
95                             bool starting_test);
96 
97   // True if the RenderView is hosting a frame tree fragment that is part of the
98   // web test harness' main window.
is_main_window()99   bool is_main_window() const { return is_main_window_; }
test_config()100   const mojom::WebTestRunTestConfiguration& test_config() const {
101     return test_config_;
102   }
103 
104  private:
105   // RenderViewImpl has no public destructor.
106   ~WebViewTestProxy() override;
107 
108   TestRunner* const test_runner_;
109 
110   // True if the RenderView is hosting a frame tree fragment that is part of the
111   // web test harness' main window.
112   bool is_main_window_ = false;
113   mojom::WebTestRunTestConfiguration test_config_;
114 
115   AccessibilityController accessibility_controller_{this};
116   TextInputController text_input_controller_{this};
117 
118   DISALLOW_COPY_AND_ASSIGN(WebViewTestProxy);
119 };
120 
121 }  // namespace content
122 
123 #endif  // CONTENT_WEB_TEST_RENDERER_WEB_VIEW_TEST_PROXY_H_
124