blob: 5f62c15c81503a042fbc34ad2682626b85ffd356 [file] [log] [blame]
[email protected]8ed8bf432014-08-11 19:47:551// 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/shell/renderer/test_runner/test_interfaces.h"
6
7#include <string>
8
9#include "base/command_line.h"
10#include "base/logging.h"
11#include "base/strings/stringprintf.h"
12#include "content/shell/common/shell_switches.h"
13#include "content/shell/renderer/test_runner/accessibility_controller.h"
14#include "content/shell/renderer/test_runner/event_sender.h"
15#include "content/shell/renderer/test_runner/gamepad_controller.h"
16#include "content/shell/renderer/test_runner/test_runner.h"
17#include "content/shell/renderer/test_runner/text_input_controller.h"
18#include "content/shell/renderer/test_runner/web_test_proxy.h"
19#include "third_party/WebKit/public/platform/WebURL.h"
20#include "third_party/WebKit/public/web/WebCache.h"
21#include "third_party/WebKit/public/web/WebKit.h"
22#include "third_party/WebKit/public/web/WebView.h"
23
24namespace content {
25
26TestInterfaces::TestInterfaces()
27 : accessibility_controller_(new AccessibilityController()),
28 event_sender_(new EventSender(this)),
[email protected]8ed8bf432014-08-11 19:47:5529 text_input_controller_(new TextInputController()),
30 test_runner_(new TestRunner(this)),
31 delegate_(0) {
32 blink::setLayoutTestMode(true);
33 if (CommandLine::ForCurrentProcess()->HasSwitch(
34 switches::kEnableFontAntialiasing))
35 blink::setFontAntialiasingEnabledForTest(true);
36
37 // NOTE: please don't put feature specific enable flags here,
38 // instead add them to RuntimeEnabledFeatures.in
39
40 ResetAll();
41}
42
43TestInterfaces::~TestInterfaces() {
44 accessibility_controller_->SetWebView(0);
45 event_sender_->SetWebView(0);
46 // gamepad_controller_ doesn't depend on WebView.
47 text_input_controller_->SetWebView(NULL);
48 test_runner_->SetWebView(0, 0);
49
50 accessibility_controller_->SetDelegate(0);
51 event_sender_->SetDelegate(0);
[email protected]9c41b462014-08-19 15:51:3452 // gamepad_controller_ ignores SetDelegate(0)
[email protected]8ed8bf432014-08-11 19:47:5553 // text_input_controller_ doesn't depend on WebTestDelegate.
54 test_runner_->SetDelegate(0);
55}
56
57void TestInterfaces::SetWebView(blink::WebView* web_view,
58 WebTestProxyBase* proxy) {
59 proxy_ = proxy;
60 accessibility_controller_->SetWebView(web_view);
61 event_sender_->SetWebView(web_view);
62 // gamepad_controller_ doesn't depend on WebView.
63 text_input_controller_->SetWebView(web_view);
64 test_runner_->SetWebView(web_view, proxy);
65}
66
67void TestInterfaces::SetDelegate(WebTestDelegate* delegate) {
68 accessibility_controller_->SetDelegate(delegate);
69 event_sender_->SetDelegate(delegate);
[email protected]9c41b462014-08-19 15:51:3470 gamepad_controller_ = GamepadController::Create(delegate);
[email protected]8ed8bf432014-08-11 19:47:5571 // text_input_controller_ doesn't depend on WebTestDelegate.
72 test_runner_->SetDelegate(delegate);
73 delegate_ = delegate;
74}
75
76void TestInterfaces::BindTo(blink::WebFrame* frame) {
77 accessibility_controller_->Install(frame);
78 event_sender_->Install(frame);
[email protected]9c41b462014-08-19 15:51:3479 if (gamepad_controller_)
80 gamepad_controller_->Install(frame);
[email protected]8ed8bf432014-08-11 19:47:5581 text_input_controller_->Install(frame);
82 test_runner_->Install(frame);
83}
84
85void TestInterfaces::ResetTestHelperControllers() {
86 accessibility_controller_->Reset();
87 event_sender_->Reset();
[email protected]9c41b462014-08-19 15:51:3488 if (gamepad_controller_)
89 gamepad_controller_->Reset();
[email protected]8ed8bf432014-08-11 19:47:5590 // text_input_controller_ doesn't have any state to reset.
91 blink::WebCache::clear();
92}
93
94void TestInterfaces::ResetAll() {
95 ResetTestHelperControllers();
96 test_runner_->Reset();
97}
98
99void TestInterfaces::SetTestIsRunning(bool running) {
100 test_runner_->SetTestIsRunning(running);
101}
102
103void TestInterfaces::ConfigureForTestWithURL(const blink::WebURL& test_url,
104 bool generate_pixels) {
105 std::string spec = GURL(test_url).spec();
yurysda0493f2014-10-01 07:55:23106 size_t path_start = spec.rfind("LayoutTests/");
107 if (path_start != std::string::npos)
108 spec = spec.substr(path_start);
[email protected]8ed8bf432014-08-11 19:47:55109 test_runner_->setShouldGeneratePixelResults(generate_pixels);
110 if (spec.find("loading/") != std::string::npos)
111 test_runner_->setShouldDumpFrameLoadCallbacks(true);
112 if (spec.find("/dumpAsText/") != std::string::npos) {
113 test_runner_->setShouldDumpAsText(true);
114 test_runner_->setShouldGeneratePixelResults(false);
115 }
116 if (spec.find("/inspector/") != std::string::npos ||
117 spec.find("/inspector-enabled/") != std::string::npos)
abhishek.a21ca9b5602014-09-19 07:33:33118 test_runner_->ClearDevToolsLocalStorage();
[email protected]8ed8bf432014-08-11 19:47:55119 if (spec.find("/inspector/") != std::string::npos) {
120 // Subfolder name determines default panel to open.
121 std::string settings = "";
122 std::string test_path = spec.substr(spec.find("/inspector/") + 11);
123 size_t slash_index = test_path.find("/");
pfeldmanfeed35d22014-09-30 14:52:38124 std::string test_path_setting = base::StringPrintf(
125 "\"testPath\":\"\\\"%s\\\"\"", spec.c_str());
126
127 // TODO(pfeldman): remove once migrated to testPath.
128 std::string last_active_panel;
[email protected]8ed8bf432014-08-11 19:47:55129 if (slash_index != std::string::npos) {
pfeldmanfeed35d22014-09-30 14:52:38130 last_active_panel = base::StringPrintf(
131 ",\"lastActivePanel\":\"\\\"%s\\\"\"",
132 test_path.substr(0, slash_index).c_str());
[email protected]8ed8bf432014-08-11 19:47:55133 }
pfeldmanfeed35d22014-09-30 14:52:38134
135 test_runner_->ShowDevTools(base::StringPrintf("{%s%s}",
136 test_path_setting.c_str(), last_active_panel.c_str()), std::string());
[email protected]8ed8bf432014-08-11 19:47:55137 }
138 if (spec.find("/viewsource/") != std::string::npos) {
139 test_runner_->setShouldEnableViewSource(true);
140 test_runner_->setShouldGeneratePixelResults(false);
141 test_runner_->setShouldDumpAsMarkup(true);
142 }
143}
144
145void TestInterfaces::WindowOpened(WebTestProxyBase* proxy) {
146 window_list_.push_back(proxy);
147}
148
149void TestInterfaces::WindowClosed(WebTestProxyBase* proxy) {
150 std::vector<WebTestProxyBase*>::iterator pos =
151 std::find(window_list_.begin(), window_list_.end(), proxy);
152 if (pos == window_list_.end()) {
153 NOTREACHED();
154 return;
155 }
156 window_list_.erase(pos);
157}
158
159AccessibilityController* TestInterfaces::GetAccessibilityController() {
160 return accessibility_controller_.get();
161}
162
163EventSender* TestInterfaces::GetEventSender() {
164 return event_sender_.get();
165}
166
167TestRunner* TestInterfaces::GetTestRunner() {
168 return test_runner_.get();
169}
170
171WebTestDelegate* TestInterfaces::GetDelegate() {
172 return delegate_;
173}
174
175WebTestProxyBase* TestInterfaces::GetProxy() {
176 return proxy_;
177}
178
179const std::vector<WebTestProxyBase*>& TestInterfaces::GetWindowList() {
180 return window_list_;
181}
182
183blink::WebThemeEngine* TestInterfaces::GetThemeEngine() {
184 if (!test_runner_->UseMockTheme())
185 return 0;
186#if defined(OS_MACOSX)
187 if (!theme_engine_.get())
188 theme_engine_.reset(new MockWebThemeEngineMac());
189#else
190 if (!theme_engine_.get())
191 theme_engine_.reset(new MockWebThemeEngine());
192#endif
193 return theme_engine_.get();
194}
195
196} // namespace content