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 #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 24 namespace content { 25 26 TestInterfaces::TestInterfaces() 27 : accessibility_controller_(new AccessibilityController()), 28 event_sender_(new EventSender(this)), 29 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 43 TestInterfaces::~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); 52 // gamepad_controller_ ignores SetDelegate(0) 53 // text_input_controller_ doesn't depend on WebTestDelegate. 54 test_runner_->SetDelegate(0); 55 } 56 57 void 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 67 void TestInterfaces::SetDelegate(WebTestDelegate* delegate) { 68 accessibility_controller_->SetDelegate(delegate); 69 event_sender_->SetDelegate(delegate); 70 gamepad_controller_ = GamepadController::Create(delegate); 71 // text_input_controller_ doesn't depend on WebTestDelegate. 72 test_runner_->SetDelegate(delegate); 73 delegate_ = delegate; 74 } 75 76 void TestInterfaces::BindTo(blink::WebFrame* frame) { 77 accessibility_controller_->Install(frame); 78 event_sender_->Install(frame); 79 if (gamepad_controller_) 80 gamepad_controller_->Install(frame); 81 text_input_controller_->Install(frame); 82 test_runner_->Install(frame); 83 } 84 85 void TestInterfaces::ResetTestHelperControllers() { 86 accessibility_controller_->Reset(); 87 event_sender_->Reset(); 88 if (gamepad_controller_) 89 gamepad_controller_->Reset(); 90 // text_input_controller_ doesn't have any state to reset. 91 blink::WebCache::clear(); 92 } 93 94 void TestInterfaces::ResetAll() { 95 ResetTestHelperControllers(); 96 test_runner_->Reset(); 97 } 98 99 void TestInterfaces::SetTestIsRunning(bool running) { 100 test_runner_->SetTestIsRunning(running); 101 } 102 103 void TestInterfaces::ConfigureForTestWithURL(const blink::WebURL& test_url, 104 bool generate_pixels) { 105 std::string spec = GURL(test_url).spec(); 106 test_runner_->setShouldGeneratePixelResults(generate_pixels); 107 if (spec.find("loading/") != std::string::npos) 108 test_runner_->setShouldDumpFrameLoadCallbacks(true); 109 if (spec.find("/dumpAsText/") != std::string::npos) { 110 test_runner_->setShouldDumpAsText(true); 111 test_runner_->setShouldGeneratePixelResults(false); 112 } 113 if (spec.find("/inspector/") != std::string::npos || 114 spec.find("/inspector-enabled/") != std::string::npos) 115 test_runner_->ClearDevToolsLocalStorage(); 116 if (spec.find("/inspector/") != std::string::npos) { 117 // Subfolder name determines default panel to open. 118 std::string settings = ""; 119 std::string test_path = spec.substr(spec.find("/inspector/") + 11); 120 size_t slash_index = test_path.find("/"); 121 if (slash_index != std::string::npos) { 122 settings = base::StringPrintf("{\"lastActivePanel\":\"\\\"%s\\\"\"}", 123 test_path.substr(0, slash_index).c_str()); 124 } 125 test_runner_->ShowDevTools(settings, std::string()); 126 } 127 if (spec.find("/viewsource/") != std::string::npos) { 128 test_runner_->setShouldEnableViewSource(true); 129 test_runner_->setShouldGeneratePixelResults(false); 130 test_runner_->setShouldDumpAsMarkup(true); 131 } 132 } 133 134 void TestInterfaces::WindowOpened(WebTestProxyBase* proxy) { 135 window_list_.push_back(proxy); 136 } 137 138 void TestInterfaces::WindowClosed(WebTestProxyBase* proxy) { 139 std::vector<WebTestProxyBase*>::iterator pos = 140 std::find(window_list_.begin(), window_list_.end(), proxy); 141 if (pos == window_list_.end()) { 142 NOTREACHED(); 143 return; 144 } 145 window_list_.erase(pos); 146 } 147 148 AccessibilityController* TestInterfaces::GetAccessibilityController() { 149 return accessibility_controller_.get(); 150 } 151 152 EventSender* TestInterfaces::GetEventSender() { 153 return event_sender_.get(); 154 } 155 156 TestRunner* TestInterfaces::GetTestRunner() { 157 return test_runner_.get(); 158 } 159 160 WebTestDelegate* TestInterfaces::GetDelegate() { 161 return delegate_; 162 } 163 164 WebTestProxyBase* TestInterfaces::GetProxy() { 165 return proxy_; 166 } 167 168 const std::vector<WebTestProxyBase*>& TestInterfaces::GetWindowList() { 169 return window_list_; 170 } 171 172 blink::WebThemeEngine* TestInterfaces::GetThemeEngine() { 173 if (!test_runner_->UseMockTheme()) 174 return 0; 175 #if defined(OS_MACOSX) 176 if (!theme_engine_.get()) 177 theme_engine_.reset(new MockWebThemeEngineMac()); 178 #else 179 if (!theme_engine_.get()) 180 theme_engine_.reset(new MockWebThemeEngine()); 181 #endif 182 return theme_engine_.get(); 183 } 184 185 } // namespace content 186