1 /* 2 * Copyright (C) 2012 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "TestInterfaces.h" 32 33 #include "AccessibilityController.h" 34 #include "EventSender.h" 35 #include "GamepadController.h" 36 #include "TestRunner.h" 37 #include "TextInputController.h" 38 #include "public/platform/WebString.h" 39 #include "public/platform/WebURL.h" 40 #include "public/testing/WebTestProxy.h" 41 #include "public/web/WebCache.h" 42 #include "public/web/WebKit.h" 43 #include "public/web/WebRuntimeFeatures.h" 44 #include "public/web/WebView.h" 45 #include <string> 46 47 using namespace blink; 48 using namespace std; 49 50 namespace WebTestRunner { 51 52 TestInterfaces::TestInterfaces() 53 : m_accessibilityController(new AccessibilityController()) 54 , m_eventSender(new EventSender(this)) 55 , m_gamepadController(new GamepadController()) 56 , m_textInputController(new TextInputController()) 57 , m_testRunner(new TestRunner(this)) 58 , m_delegate(0) 59 { 60 blink::setLayoutTestMode(true); 61 62 // NOTE: please don't put feature specific enable flags here, 63 // instead add them to RuntimeEnabledFeatures.in 64 65 resetAll(); 66 } 67 68 TestInterfaces::~TestInterfaces() 69 { 70 m_accessibilityController->setWebView(0); 71 m_eventSender->setWebView(0); 72 // m_gamepadController doesn't depend on WebView. 73 m_textInputController->setWebView(0); 74 m_testRunner->setWebView(0, 0); 75 76 m_accessibilityController->setDelegate(0); 77 m_eventSender->setDelegate(0); 78 m_gamepadController->setDelegate(0); 79 // m_textInputController doesn't depend on WebTestDelegate. 80 m_testRunner->setDelegate(0); 81 } 82 83 void TestInterfaces::setWebView(WebView* webView, WebTestProxyBase* proxy) 84 { 85 m_proxy = proxy; 86 m_accessibilityController->setWebView(webView); 87 m_eventSender->setWebView(webView); 88 // m_gamepadController doesn't depend on WebView. 89 m_textInputController->setWebView(webView); 90 m_testRunner->setWebView(webView, proxy); 91 } 92 93 void TestInterfaces::setDelegate(WebTestDelegate* delegate) 94 { 95 m_accessibilityController->setDelegate(delegate); 96 m_eventSender->setDelegate(delegate); 97 m_gamepadController->setDelegate(delegate); 98 // m_textInputController doesn't depend on WebTestDelegate. 99 m_testRunner->setDelegate(delegate); 100 m_delegate = delegate; 101 } 102 103 void TestInterfaces::bindTo(WebFrame* frame) 104 { 105 m_accessibilityController->bindToJavascript(frame, WebString::fromUTF8("accessibilityController")); 106 m_eventSender->bindToJavascript(frame, WebString::fromUTF8("eventSender")); 107 m_gamepadController->bindToJavascript(frame, WebString::fromUTF8("gamepadController")); 108 m_textInputController->bindToJavascript(frame, WebString::fromUTF8("textInputController")); 109 m_testRunner->bindToJavascript(frame, WebString::fromUTF8("testRunner")); 110 m_testRunner->bindToJavascript(frame, WebString::fromUTF8("layoutTestController")); 111 } 112 113 void TestInterfaces::resetTestHelperControllers() 114 { 115 m_accessibilityController->reset(); 116 m_eventSender->reset(); 117 m_gamepadController->reset(); 118 // m_textInputController doesn't have any state to reset. 119 WebCache::clear(); 120 } 121 122 void TestInterfaces::resetAll() 123 { 124 resetTestHelperControllers(); 125 m_testRunner->reset(); 126 } 127 128 void TestInterfaces::setTestIsRunning(bool running) 129 { 130 m_testRunner->setTestIsRunning(running); 131 } 132 133 void TestInterfaces::configureForTestWithURL(const WebURL& testURL, bool generatePixels) 134 { 135 string spec = GURL(testURL).spec(); 136 m_testRunner->setShouldGeneratePixelResults(generatePixels); 137 if (spec.find("loading/") != string::npos) 138 m_testRunner->setShouldDumpFrameLoadCallbacks(true); 139 if (spec.find("/dumpAsText/") != string::npos) { 140 m_testRunner->setShouldDumpAsText(true); 141 m_testRunner->setShouldGeneratePixelResults(false); 142 } 143 if (spec.find("/inspector/") != string::npos) 144 m_testRunner->showDevTools(); 145 if (spec.find("/viewsource/") != string::npos) { 146 m_testRunner->setShouldEnableViewSource(true); 147 m_testRunner->setShouldGeneratePixelResults(false); 148 m_testRunner->setShouldDumpAsMarkup(true); 149 } 150 } 151 152 void TestInterfaces::windowOpened(WebTestProxyBase* proxy) 153 { 154 m_windowList.push_back(proxy); 155 } 156 157 void TestInterfaces::windowClosed(WebTestProxyBase* proxy) 158 { 159 vector<WebTestProxyBase*>::iterator pos = find(m_windowList.begin(), m_windowList.end(), proxy); 160 if (pos == m_windowList.end()) { 161 BLINK_ASSERT_NOT_REACHED(); 162 return; 163 } 164 m_windowList.erase(pos); 165 } 166 167 AccessibilityController* TestInterfaces::accessibilityController() 168 { 169 return m_accessibilityController.get(); 170 } 171 172 EventSender* TestInterfaces::eventSender() 173 { 174 return m_eventSender.get(); 175 } 176 177 TestRunner* TestInterfaces::testRunner() 178 { 179 return m_testRunner.get(); 180 } 181 182 WebTestDelegate* TestInterfaces::delegate() 183 { 184 return m_delegate; 185 } 186 187 WebTestProxyBase* TestInterfaces::proxy() 188 { 189 return m_proxy; 190 } 191 192 const vector<WebTestProxyBase*>& TestInterfaces::windowList() 193 { 194 return m_windowList; 195 } 196 197 WebThemeEngine* TestInterfaces::themeEngine() 198 { 199 #if defined(USE_DEFAULT_RENDER_THEME) || !(defined(WIN32) || defined(__APPLE__) || defined(ANDROID)) 200 if (!m_themeEngine.get()) 201 m_themeEngine.reset(new WebTestThemeEngineMock()); 202 return m_themeEngine.get(); 203 #elif defined(WIN32) 204 if (!m_themeEngine.get()) 205 m_themeEngine.reset(new WebTestThemeEngineWin()); 206 return m_themeEngine.get(); 207 #elif defined(__APPLE__) 208 if (!m_themeEngine.get()) 209 m_themeEngine.reset(new WebTestThemeEngineMac()); 210 return m_themeEngine.get(); 211 #else 212 return 0; 213 #endif 214 } 215 216 } 217