1// Copyright 2013 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/TestInterfaces.h"
6
7#include <string>
8
9#include "base/logging.h"
10#include "base/command_line.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/text_input_controller.h"
17#include "content/shell/renderer/test_runner/test_runner.h"
18#include "content/shell/renderer/test_runner/web_test_proxy.h"
19#include "third_party/WebKit/public/platform/WebString.h"
20#include "third_party/WebKit/public/platform/WebURL.h"
21#include "third_party/WebKit/public/web/WebCache.h"
22#include "third_party/WebKit/public/web/WebKit.h"
23#include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
24#include "third_party/WebKit/public/web/WebView.h"
25
26using namespace blink;
27using namespace std;
28
29namespace content {
30
31TestInterfaces::TestInterfaces()
32    : m_accessibilityController(new AccessibilityController())
33    , m_eventSender(new EventSender(this))
34    , m_gamepadController(new GamepadController())
35    , m_textInputController(new TextInputController())
36    , m_testRunner(new TestRunner(this))
37    , m_delegate(0)
38{
39    blink::setLayoutTestMode(true);
40    if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableFontAntialiasing))
41        blink::setFontAntialiasingEnabledForTest(true);
42
43    // NOTE: please don't put feature specific enable flags here,
44    // instead add them to RuntimeEnabledFeatures.in
45
46    resetAll();
47}
48
49TestInterfaces::~TestInterfaces()
50{
51    m_accessibilityController->SetWebView(0);
52    m_eventSender->SetWebView(0);
53    // m_gamepadController doesn't depend on WebView.
54    m_textInputController->SetWebView(NULL);
55    m_testRunner->SetWebView(0, 0);
56
57    m_accessibilityController->SetDelegate(0);
58    m_eventSender->SetDelegate(0);
59    m_gamepadController->SetDelegate(0);
60    // m_textInputController doesn't depend on WebTestDelegate.
61    m_testRunner->SetDelegate(0);
62}
63
64void TestInterfaces::setWebView(WebView* webView, WebTestProxyBase* proxy)
65{
66    m_proxy = proxy;
67    m_accessibilityController->SetWebView(webView);
68    m_eventSender->SetWebView(webView);
69    // m_gamepadController doesn't depend on WebView.
70    m_textInputController->SetWebView(webView);
71    m_testRunner->SetWebView(webView, proxy);
72}
73
74void TestInterfaces::setDelegate(WebTestDelegate* delegate)
75{
76    m_accessibilityController->SetDelegate(delegate);
77    m_eventSender->SetDelegate(delegate);
78    m_gamepadController->SetDelegate(delegate);
79    // m_textInputController doesn't depend on WebTestDelegate.
80    m_testRunner->SetDelegate(delegate);
81    m_delegate = delegate;
82}
83
84void TestInterfaces::bindTo(WebFrame* frame)
85{
86    m_accessibilityController->Install(frame);
87    m_eventSender->Install(frame);
88    m_gamepadController->Install(frame);
89    m_textInputController->Install(frame);
90    m_testRunner->Install(frame);
91}
92
93void TestInterfaces::resetTestHelperControllers()
94{
95    m_accessibilityController->Reset();
96    m_eventSender->Reset();
97    m_gamepadController->Reset();
98    // m_textInputController doesn't have any state to reset.
99    WebCache::clear();
100}
101
102void TestInterfaces::resetAll()
103{
104    resetTestHelperControllers();
105    m_testRunner->Reset();
106}
107
108void TestInterfaces::setTestIsRunning(bool running)
109{
110    m_testRunner->SetTestIsRunning(running);
111}
112
113void TestInterfaces::configureForTestWithURL(const WebURL& testURL, bool generatePixels)
114{
115    string spec = GURL(testURL).spec();
116    m_testRunner->setShouldGeneratePixelResults(generatePixels);
117    if (spec.find("loading/") != string::npos)
118        m_testRunner->setShouldDumpFrameLoadCallbacks(true);
119    if (spec.find("/dumpAsText/") != string::npos) {
120        m_testRunner->setShouldDumpAsText(true);
121        m_testRunner->setShouldGeneratePixelResults(false);
122    }
123    if (spec.find("/inspector/") != string::npos
124        || spec.find("/inspector-enabled/") != string::npos)
125        m_testRunner->clearDevToolsLocalStorage();
126    if (spec.find("/inspector/") != string::npos) {
127        // Subfolder name determines default panel to open.
128        string settings = "";
129        string test_path = spec.substr(spec.find("/inspector/") + 11);
130        size_t slash_index = test_path.find("/");
131        if (slash_index != string::npos) {
132            settings = base::StringPrintf(
133                "{\"lastActivePanel\":\"\\\"%s\\\"\"}",
134                test_path.substr(0, slash_index).c_str());
135        }
136        m_testRunner->showDevTools(settings, string());
137    }
138    if (spec.find("/viewsource/") != string::npos) {
139        m_testRunner->setShouldEnableViewSource(true);
140        m_testRunner->setShouldGeneratePixelResults(false);
141        m_testRunner->setShouldDumpAsMarkup(true);
142    }
143}
144
145void TestInterfaces::windowOpened(WebTestProxyBase* proxy)
146{
147    m_windowList.push_back(proxy);
148}
149
150void TestInterfaces::windowClosed(WebTestProxyBase* proxy)
151{
152    vector<WebTestProxyBase*>::iterator pos = find(m_windowList.begin(), m_windowList.end(), proxy);
153    if (pos == m_windowList.end()) {
154        NOTREACHED();
155        return;
156    }
157    m_windowList.erase(pos);
158}
159
160AccessibilityController* TestInterfaces::accessibilityController()
161{
162    return m_accessibilityController.get();
163}
164
165EventSender* TestInterfaces::eventSender()
166{
167    return m_eventSender.get();
168}
169
170TestRunner* TestInterfaces::testRunner()
171{
172    return m_testRunner.get();
173}
174
175WebTestDelegate* TestInterfaces::delegate()
176{
177    return m_delegate;
178}
179
180WebTestProxyBase* TestInterfaces::proxy()
181{
182    return m_proxy;
183}
184
185const vector<WebTestProxyBase*>& TestInterfaces::windowList()
186{
187    return m_windowList;
188}
189
190WebThemeEngine* TestInterfaces::themeEngine()
191{
192    if (!m_testRunner->UseMockTheme())
193        return 0;
194#if defined(__APPLE__)
195    if (!m_themeEngine.get())
196        m_themeEngine.reset(new WebTestThemeEngineMac());
197#else
198    if (!m_themeEngine.get())
199        m_themeEngine.reset(new WebTestThemeEngineMock());
200#endif
201    return m_themeEngine.get();
202}
203
204}  // namespace content
205