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.
5 #include "components/test_runner/test_interfaces.h"
9 #include "base/json/json_writer.h"
10 #include "base/json/string_escape.h"
11 #include "base/logging.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/values.h"
14 #include "components/test_runner/accessibility_controller.h"
15 #include "components/test_runner/app_banner_client.h"
16 #include "components/test_runner/event_sender.h"
17 #include "components/test_runner/gamepad_controller.h"
18 #include "components/test_runner/test_runner.h"
19 #include "components/test_runner/text_input_controller.h"
20 #include "components/test_runner/web_test_proxy.h"
21 #include "third_party/WebKit/public/platform/WebURL.h"
22 #include "third_party/WebKit/public/web/WebCache.h"
23 #include "third_party/WebKit/public/web/WebKit.h"
24 #include "third_party/WebKit/public/web/WebView.h"
26 namespace test_runner
{
28 TestInterfaces::TestInterfaces()
29 : accessibility_controller_(new AccessibilityController()),
30 event_sender_(new EventSender(this)),
31 text_input_controller_(new TextInputController()),
32 test_runner_(new TestRunner(this)),
34 app_banner_client_(nullptr) {
35 blink::setLayoutTestMode(true);
36 // NOTE: please don't put feature specific enable flags here,
37 // instead add them to RuntimeEnabledFeatures.in
42 TestInterfaces::~TestInterfaces() {
43 accessibility_controller_
->SetWebView(0);
44 event_sender_
->SetWebView(0);
45 // gamepad_controller_ doesn't depend on WebView.
46 text_input_controller_
->SetWebView(NULL
);
47 test_runner_
->SetWebView(0, 0);
49 accessibility_controller_
->SetDelegate(0);
50 event_sender_
->SetDelegate(0);
51 // gamepad_controller_ ignores SetDelegate(0)
52 // text_input_controller_ doesn't depend on WebTestDelegate.
53 test_runner_
->SetDelegate(0);
56 void TestInterfaces::SetWebView(blink::WebView
* web_view
,
57 WebTestProxyBase
* proxy
) {
59 accessibility_controller_
->SetWebView(web_view
);
60 event_sender_
->SetWebView(web_view
);
61 // gamepad_controller_ doesn't depend on WebView.
62 text_input_controller_
->SetWebView(web_view
);
63 test_runner_
->SetWebView(web_view
, proxy
);
66 void TestInterfaces::SetDelegate(WebTestDelegate
* delegate
) {
67 accessibility_controller_
->SetDelegate(delegate
);
68 event_sender_
->SetDelegate(delegate
);
69 gamepad_controller_
= GamepadController::Create(delegate
);
70 // text_input_controller_ doesn't depend on WebTestDelegate.
71 test_runner_
->SetDelegate(delegate
);
75 void TestInterfaces::BindTo(blink::WebFrame
* frame
) {
76 accessibility_controller_
->Install(frame
);
77 event_sender_
->Install(frame
);
78 if (gamepad_controller_
)
79 gamepad_controller_
->Install(frame
);
80 text_input_controller_
->Install(frame
);
81 test_runner_
->Install(frame
);
84 void TestInterfaces::ResetTestHelperControllers() {
85 accessibility_controller_
->Reset();
86 event_sender_
->Reset();
87 if (gamepad_controller_
)
88 gamepad_controller_
->Reset();
89 // text_input_controller_ doesn't have any state to reset.
90 blink::WebCache::clear();
93 void TestInterfaces::ResetAll() {
94 ResetTestHelperControllers();
95 test_runner_
->Reset();
98 void TestInterfaces::SetTestIsRunning(bool running
) {
99 test_runner_
->SetTestIsRunning(running
);
102 void TestInterfaces::ConfigureForTestWithURL(const blink::WebURL
& test_url
,
103 bool generate_pixels
) {
104 std::string spec
= GURL(test_url
).spec();
105 size_t path_start
= spec
.rfind("LayoutTests/");
106 if (path_start
!= std::string::npos
)
107 spec
= spec
.substr(path_start
);
108 test_runner_
->setShouldGeneratePixelResults(generate_pixels
);
109 if (spec
.find("loading/") != std::string::npos
)
110 test_runner_
->setShouldDumpFrameLoadCallbacks(true);
111 if (spec
.find("/dumpAsText/") != std::string::npos
) {
112 test_runner_
->setShouldDumpAsText(true);
113 test_runner_
->setShouldGeneratePixelResults(false);
115 if (spec
.find("/inspector/") != std::string::npos
||
116 spec
.find("/inspector-enabled/") != std::string::npos
)
117 test_runner_
->ClearDevToolsLocalStorage();
118 if (spec
.find("/inspector/") != std::string::npos
) {
119 // Subfolder name determines default panel to open.
120 std::string test_path
= spec
.substr(spec
.find("/inspector/") + 11);
121 base::DictionaryValue settings
;
122 settings
.SetString("testPath", base::GetQuotedJSONString(spec
));
123 std::string settings_string
;
124 base::JSONWriter::Write(settings
, &settings_string
);
125 test_runner_
->ShowDevTools(settings_string
, std::string());
127 if (spec
.find("/viewsource/") != std::string::npos
) {
128 test_runner_
->setShouldEnableViewSource(true);
129 test_runner_
->setShouldGeneratePixelResults(false);
130 test_runner_
->setShouldDumpAsMarkup(true);
134 void TestInterfaces::SetAppBannerClient(AppBannerClient
* app_banner_client
) {
135 app_banner_client_
= app_banner_client
;
138 void TestInterfaces::WindowOpened(WebTestProxyBase
* proxy
) {
139 window_list_
.push_back(proxy
);
142 void TestInterfaces::WindowClosed(WebTestProxyBase
* proxy
) {
143 std::vector
<WebTestProxyBase
*>::iterator pos
=
144 std::find(window_list_
.begin(), window_list_
.end(), proxy
);
145 if (pos
== window_list_
.end()) {
149 window_list_
.erase(pos
);
152 AccessibilityController
* TestInterfaces::GetAccessibilityController() {
153 return accessibility_controller_
.get();
156 EventSender
* TestInterfaces::GetEventSender() {
157 return event_sender_
.get();
160 TestRunner
* TestInterfaces::GetTestRunner() {
161 return test_runner_
.get();
164 WebTestDelegate
* TestInterfaces::GetDelegate() {
168 WebTestProxyBase
* TestInterfaces::GetProxy() {
172 const std::vector
<WebTestProxyBase
*>& TestInterfaces::GetWindowList() {
176 blink::WebThemeEngine
* TestInterfaces::GetThemeEngine() {
177 if (!test_runner_
->UseMockTheme())
179 if (!theme_engine_
.get())
180 theme_engine_
.reset(new MockWebThemeEngine());
181 return theme_engine_
.get();
184 AppBannerClient
* TestInterfaces::GetAppBannerClient() {
185 return app_banner_client_
;
188 } // namespace test_runner