| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/test_runner/web_test_proxy.h" | 5 #include "components/test_runner/web_test_proxy.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <cctype> | 10 #include <cctype> |
| 11 | 11 |
| 12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/i18n/rtl.h" | |
| 15 #include "base/logging.h" | 14 #include "base/logging.h" |
| 16 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | |
| 18 #include "base/strings/utf_string_conversions.h" | |
| 19 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 20 #include "base/time/time.h" | |
| 21 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
| 22 #include "components/test_runner/event_sender.h" | |
| 23 #include "components/test_runner/mock_credential_manager_client.h" | 18 #include "components/test_runner/mock_credential_manager_client.h" |
| 24 #include "components/test_runner/mock_screen_orientation_client.h" | 19 #include "components/test_runner/mock_screen_orientation_client.h" |
| 25 #include "components/test_runner/mock_web_speech_recognizer.h" | |
| 26 #include "components/test_runner/spell_check_client.h" | 20 #include "components/test_runner/spell_check_client.h" |
| 27 #include "components/test_runner/test_common.h" | 21 #include "components/test_runner/test_common.h" |
| 28 #include "components/test_runner/test_interfaces.h" | 22 #include "components/test_runner/test_interfaces.h" |
| 29 #include "components/test_runner/test_plugin.h" | 23 #include "components/test_runner/test_plugin.h" |
| 30 #include "components/test_runner/test_runner.h" | 24 #include "components/test_runner/test_runner.h" |
| 31 #include "components/test_runner/web_test_delegate.h" | 25 #include "components/test_runner/web_test_delegate.h" |
| 32 #include "components/test_runner/web_test_interfaces.h" | 26 #include "components/test_runner/web_test_interfaces.h" |
| 33 #include "components/test_runner/web_test_runner.h" | 27 #include "components/test_runner/web_test_runner.h" |
| 34 #include "third_party/WebKit/public/platform/WebLayoutAndPaintAsyncCallback.h" | 28 #include "third_party/WebKit/public/platform/WebLayoutAndPaintAsyncCallback.h" |
| 35 #include "third_party/WebKit/public/platform/WebURLError.h" | 29 #include "third_party/WebKit/public/platform/WebURLError.h" |
| 36 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 30 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 37 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 31 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 38 #include "third_party/WebKit/public/web/WebAXEnums.h" | 32 #include "third_party/WebKit/public/web/WebAXEnums.h" |
| 39 #include "third_party/WebKit/public/web/WebAXObject.h" | 33 #include "third_party/WebKit/public/web/WebAXObject.h" |
| 40 #include "third_party/WebKit/public/web/WebDocument.h" | 34 #include "third_party/WebKit/public/web/WebDocument.h" |
| 41 #include "third_party/WebKit/public/web/WebElement.h" | 35 #include "third_party/WebKit/public/web/WebElement.h" |
| 42 #include "third_party/WebKit/public/web/WebHistoryItem.h" | 36 #include "third_party/WebKit/public/web/WebHistoryItem.h" |
| 43 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 37 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 44 #include "third_party/WebKit/public/web/WebNode.h" | 38 #include "third_party/WebKit/public/web/WebNode.h" |
| 45 #include "third_party/WebKit/public/web/WebPagePopup.h" | 39 #include "third_party/WebKit/public/web/WebPagePopup.h" |
| 46 #include "third_party/WebKit/public/web/WebPluginParams.h" | 40 #include "third_party/WebKit/public/web/WebPluginParams.h" |
| 47 #include "third_party/WebKit/public/web/WebPrintParams.h" | 41 #include "third_party/WebKit/public/web/WebPrintParams.h" |
| 48 #include "third_party/WebKit/public/web/WebRange.h" | 42 #include "third_party/WebKit/public/web/WebRange.h" |
| 49 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | 43 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
| 50 #include "third_party/WebKit/public/web/WebView.h" | 44 #include "third_party/WebKit/public/web/WebView.h" |
| 51 #include "third_party/WebKit/public/web/WebWidgetClient.h" | |
| 52 | 45 |
| 53 namespace test_runner { | 46 namespace test_runner { |
| 54 | 47 |
| 55 namespace { | 48 namespace { |
| 56 | 49 |
| 57 class LayoutAndPaintCallback : public blink::WebLayoutAndPaintAsyncCallback { | 50 class LayoutAndPaintCallback : public blink::WebLayoutAndPaintAsyncCallback { |
| 58 public: | 51 public: |
| 59 LayoutAndPaintCallback(const base::Closure& callback) | 52 LayoutAndPaintCallback(const base::Closure& callback) |
| 60 : callback_(callback), wait_for_popup_(false) { | 53 : callback_(callback), wait_for_popup_(false) { |
| 61 } | 54 } |
| 62 virtual ~LayoutAndPaintCallback() { | 55 virtual ~LayoutAndPaintCallback() { |
| 63 } | 56 } |
| 64 | 57 |
| 65 void set_wait_for_popup(bool wait) { wait_for_popup_ = wait; } | 58 void set_wait_for_popup(bool wait) { wait_for_popup_ = wait; } |
| 66 | 59 |
| 67 // WebLayoutAndPaintAsyncCallback implementation. | 60 // WebLayoutAndPaintAsyncCallback implementation. |
| 68 void didLayoutAndPaint() override; | 61 void didLayoutAndPaint() override; |
| 69 | 62 |
| 70 private: | 63 private: |
| 71 base::Closure callback_; | 64 base::Closure callback_; |
| 72 bool wait_for_popup_; | 65 bool wait_for_popup_; |
| 73 }; | 66 }; |
| 74 | 67 |
| 75 class HostMethodTask : public WebMethodTask<WebTestProxyBase> { | |
| 76 public: | |
| 77 typedef void (WebTestProxyBase::*CallbackMethodType)(); | |
| 78 HostMethodTask(WebTestProxyBase* object, CallbackMethodType callback) | |
| 79 : WebMethodTask<WebTestProxyBase>(object), callback_(callback) {} | |
| 80 | |
| 81 void RunIfValid() override { (object_->*callback_)(); } | |
| 82 | |
| 83 private: | |
| 84 CallbackMethodType callback_; | |
| 85 }; | |
| 86 | |
| 87 std::string DumpAllBackForwardLists(TestInterfaces* interfaces, | 68 std::string DumpAllBackForwardLists(TestInterfaces* interfaces, |
| 88 WebTestDelegate* delegate) { | 69 WebTestDelegate* delegate) { |
| 89 std::string result; | 70 std::string result; |
| 90 const std::vector<WebTestProxyBase*>& window_list = | 71 const std::vector<WebTestProxyBase*>& window_list = |
| 91 interfaces->GetWindowList(); | 72 interfaces->GetWindowList(); |
| 92 for (size_t i = 0; i < window_list.size(); ++i) | 73 for (size_t i = 0; i < window_list.size(); ++i) |
| 93 result.append(delegate->DumpHistoryForWindow(window_list.at(i))); | 74 result.append(delegate->DumpHistoryForWindow(window_list.at(i))); |
| 94 return result; | 75 return result; |
| 95 } | 76 } |
| 96 } | 77 |
| 78 } // namespace |
| 97 | 79 |
| 98 WebTestProxyBase::WebTestProxyBase() | 80 WebTestProxyBase::WebTestProxyBase() |
| 99 : test_interfaces_(NULL), | 81 : test_interfaces_(nullptr), |
| 100 delegate_(NULL), | 82 delegate_(nullptr), |
| 101 web_widget_(NULL), | 83 web_view_(nullptr), |
| 102 spellcheck_(new SpellCheckClient(this)) { | 84 web_widget_(nullptr), |
| 103 Reset(); | 85 spellcheck_(new SpellCheckClient(this)) {} |
| 104 } | |
| 105 | 86 |
| 106 WebTestProxyBase::~WebTestProxyBase() { | 87 WebTestProxyBase::~WebTestProxyBase() { |
| 107 test_interfaces_->WindowClosed(this); | 88 test_interfaces_->WindowClosed(this); |
| 108 } | 89 } |
| 109 | 90 |
| 110 void WebTestProxyBase::SetInterfaces(WebTestInterfaces* interfaces) { | 91 void WebTestProxyBase::SetInterfaces(WebTestInterfaces* interfaces) { |
| 111 test_interfaces_ = interfaces->GetTestInterfaces(); | 92 test_interfaces_ = interfaces->GetTestInterfaces(); |
| 112 test_interfaces_->WindowOpened(this); | 93 test_interfaces_->WindowOpened(this); |
| 113 } | 94 } |
| 114 | 95 |
| 115 void WebTestProxyBase::SetDelegate(WebTestDelegate* delegate) { | 96 void WebTestProxyBase::SetDelegate(WebTestDelegate* delegate) { |
| 116 delegate_ = delegate; | 97 delegate_ = delegate; |
| 117 spellcheck_->SetDelegate(delegate); | 98 spellcheck_->SetDelegate(delegate); |
| 118 if (speech_recognizer_.get()) | |
| 119 speech_recognizer_->SetDelegate(delegate); | |
| 120 } | |
| 121 | |
| 122 blink::WebView* WebTestProxyBase::GetWebView() const { | |
| 123 DCHECK(web_widget_); | |
| 124 // TestRunner does not support popup widgets. So |web_widget|_ is always a | |
| 125 // WebView. | |
| 126 return static_cast<blink::WebView*>(web_widget_); | |
| 127 } | |
| 128 | |
| 129 void WebTestProxyBase::Reset() { | |
| 130 animate_scheduled_ = false; | |
| 131 } | 99 } |
| 132 | 100 |
| 133 blink::WebSpellCheckClient* WebTestProxyBase::GetSpellCheckClient() const { | 101 blink::WebSpellCheckClient* WebTestProxyBase::GetSpellCheckClient() const { |
| 134 return spellcheck_.get(); | 102 return spellcheck_.get(); |
| 135 } | 103 } |
| 136 | 104 |
| 137 bool WebTestProxyBase::RunFileChooser( | |
| 138 const blink::WebFileChooserParams& params, | |
| 139 blink::WebFileChooserCompletion* completion) { | |
| 140 delegate_->PrintMessage("Mock: Opening a file chooser.\n"); | |
| 141 // FIXME: Add ability to set file names to a file upload control. | |
| 142 return false; | |
| 143 } | |
| 144 | |
| 145 void WebTestProxyBase::ShowValidationMessage( | |
| 146 const blink::WebString& main_message, | |
| 147 blink::WebTextDirection main_message_hint, | |
| 148 const blink::WebString& sub_message, | |
| 149 blink::WebTextDirection sub_message_hint) { | |
| 150 base::string16 wrapped_main_text = main_message; | |
| 151 base::string16 wrapped_sub_text = sub_message; | |
| 152 | |
| 153 if (main_message_hint == blink::WebTextDirectionLeftToRight) { | |
| 154 wrapped_main_text = | |
| 155 base::i18n::GetDisplayStringInLTRDirectionality(wrapped_main_text); | |
| 156 } else if (main_message_hint == blink::WebTextDirectionRightToLeft && | |
| 157 !base::i18n::IsRTL()) { | |
| 158 base::i18n::WrapStringWithRTLFormatting(&wrapped_main_text); | |
| 159 } | |
| 160 | |
| 161 if (!wrapped_sub_text.empty()) { | |
| 162 if (sub_message_hint == blink::WebTextDirectionLeftToRight) { | |
| 163 wrapped_sub_text = | |
| 164 base::i18n::GetDisplayStringInLTRDirectionality(wrapped_sub_text); | |
| 165 } else if (sub_message_hint == blink::WebTextDirectionRightToLeft) { | |
| 166 base::i18n::WrapStringWithRTLFormatting(&wrapped_sub_text); | |
| 167 } | |
| 168 } | |
| 169 delegate_->PrintMessage("ValidationMessageClient: main-message=" + | |
| 170 base::UTF16ToUTF8(wrapped_main_text) + | |
| 171 " sub-message=" + | |
| 172 base::UTF16ToUTF8(wrapped_sub_text) + "\n"); | |
| 173 } | |
| 174 | |
| 175 std::string WebTestProxyBase::DumpBackForwardLists() { | 105 std::string WebTestProxyBase::DumpBackForwardLists() { |
| 176 return DumpAllBackForwardLists(test_interfaces_, delegate_); | 106 return DumpAllBackForwardLists(test_interfaces_, delegate_); |
| 177 } | 107 } |
| 178 | 108 |
| 179 void LayoutAndPaintCallback::didLayoutAndPaint() { | 109 void LayoutAndPaintCallback::didLayoutAndPaint() { |
| 180 TRACE_EVENT0("shell", "LayoutAndPaintCallback::didLayoutAndPaint"); | 110 TRACE_EVENT0("shell", "LayoutAndPaintCallback::didLayoutAndPaint"); |
| 181 if (wait_for_popup_) { | 111 if (wait_for_popup_) { |
| 182 wait_for_popup_ = false; | 112 wait_for_popup_ = false; |
| 183 return; | 113 return; |
| 184 } | 114 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 204 blink::WebScreenInfo& screen_info) { | 134 blink::WebScreenInfo& screen_info) { |
| 205 MockScreenOrientationClient* mock_client = | 135 MockScreenOrientationClient* mock_client = |
| 206 test_interfaces_->GetTestRunner()->getMockScreenOrientationClient(); | 136 test_interfaces_->GetTestRunner()->getMockScreenOrientationClient(); |
| 207 if (mock_client->IsDisabled()) | 137 if (mock_client->IsDisabled()) |
| 208 return; | 138 return; |
| 209 // Override screen orientation information with mock data. | 139 // Override screen orientation information with mock data. |
| 210 screen_info.orientationType = mock_client->CurrentOrientationType(); | 140 screen_info.orientationType = mock_client->CurrentOrientationType(); |
| 211 screen_info.orientationAngle = mock_client->CurrentOrientationAngle(); | 141 screen_info.orientationAngle = mock_client->CurrentOrientationAngle(); |
| 212 } | 142 } |
| 213 | 143 |
| 214 MockWebSpeechRecognizer* WebTestProxyBase::GetSpeechRecognizerMock() { | |
| 215 if (!speech_recognizer_.get()) { | |
| 216 speech_recognizer_.reset(new MockWebSpeechRecognizer()); | |
| 217 speech_recognizer_->SetDelegate(delegate_); | |
| 218 } | |
| 219 return speech_recognizer_.get(); | |
| 220 } | |
| 221 | |
| 222 MockCredentialManagerClient* | 144 MockCredentialManagerClient* |
| 223 WebTestProxyBase::GetCredentialManagerClientMock() { | 145 WebTestProxyBase::GetCredentialManagerClientMock() { |
| 224 if (!credential_manager_client_.get()) | 146 if (!credential_manager_client_.get()) |
| 225 credential_manager_client_.reset(new MockCredentialManagerClient()); | 147 credential_manager_client_.reset(new MockCredentialManagerClient()); |
| 226 return credential_manager_client_.get(); | 148 return credential_manager_client_.get(); |
| 227 } | 149 } |
| 228 | 150 |
| 229 void WebTestProxyBase::ScheduleAnimation() { | |
| 230 if (!test_interfaces_->GetTestRunner()->TestIsRunning()) | |
| 231 return; | |
| 232 | |
| 233 if (!animate_scheduled_) { | |
| 234 animate_scheduled_ = true; | |
| 235 delegate_->PostDelayedTask( | |
| 236 new HostMethodTask(this, &WebTestProxyBase::AnimateNow), 1); | |
| 237 } | |
| 238 } | |
| 239 | |
| 240 void WebTestProxyBase::AnimateNow() { | |
| 241 if (animate_scheduled_) { | |
| 242 base::TimeDelta animate_time = base::TimeTicks::Now() - base::TimeTicks(); | |
| 243 animate_scheduled_ = false; | |
| 244 web_widget_->beginFrame(animate_time.InSecondsF()); | |
| 245 web_widget_->updateAllLifecyclePhases(); | |
| 246 if (blink::WebPagePopup* popup = web_widget_->pagePopup()) { | |
| 247 popup->beginFrame(animate_time.InSecondsF()); | |
| 248 popup->updateAllLifecyclePhases(); | |
| 249 } | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 void WebTestProxyBase::StartDragging(blink::WebLocalFrame* frame, | |
| 254 const blink::WebDragData& data, | |
| 255 blink::WebDragOperationsMask mask, | |
| 256 const blink::WebImage& image, | |
| 257 const blink::WebPoint& point) { | |
| 258 test_interfaces_->GetTestRunner()->setDragImage(image); | |
| 259 | |
| 260 // When running a test, we need to fake a drag drop operation otherwise | |
| 261 // Windows waits for real mouse events to know when the drag is over. | |
| 262 test_interfaces_->GetEventSender()->DoDragDrop(data, mask); | |
| 263 } | |
| 264 | |
| 265 // The output from these methods in layout test mode should match that | |
| 266 // expected by the layout tests. See EditingDelegate.m in DumpRenderTree. | |
| 267 | |
| 268 void WebTestProxyBase::DidChangeContents() { | |
| 269 if (test_interfaces_->GetTestRunner()->shouldDumpEditingCallbacks()) | |
| 270 delegate_->PrintMessage( | |
| 271 "EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification\n"); | |
| 272 } | |
| 273 | |
| 274 bool WebTestProxyBase::CreateView(blink::WebLocalFrame* frame, | |
| 275 const blink::WebURLRequest& request, | |
| 276 const blink::WebWindowFeatures& features, | |
| 277 const blink::WebString& frame_name, | |
| 278 blink::WebNavigationPolicy policy, | |
| 279 bool suppress_opener) { | |
| 280 if (test_interfaces_->GetTestRunner()->shouldDumpNavigationPolicy()) { | |
| 281 delegate_->PrintMessage("Default policy for createView for '" + | |
| 282 URLDescription(request.url()) + "' is '" + | |
| 283 WebNavigationPolicyToString(policy) + "'\n"); | |
| 284 } | |
| 285 | |
| 286 if (!test_interfaces_->GetTestRunner()->canOpenWindows()) | |
| 287 return false; | |
| 288 if (test_interfaces_->GetTestRunner()->shouldDumpCreateView()) | |
| 289 delegate_->PrintMessage(std::string("createView(") + | |
| 290 URLDescription(request.url()) + ")\n"); | |
| 291 return true; | |
| 292 } | |
| 293 | |
| 294 void WebTestProxyBase::SetStatusText(const blink::WebString& text) { | |
| 295 if (!test_interfaces_->GetTestRunner()->shouldDumpStatusCallbacks()) | |
| 296 return; | |
| 297 delegate_->PrintMessage( | |
| 298 std::string("UI DELEGATE STATUS CALLBACK: setStatusText:") + | |
| 299 text.utf8().data() + "\n"); | |
| 300 } | |
| 301 | |
| 302 // Simulate a print by going into print mode and then exit straight away. | |
| 303 void WebTestProxyBase::PrintPage(blink::WebLocalFrame* frame) { | |
| 304 blink::WebSize page_size_in_pixels = web_widget_->size(); | |
| 305 if (page_size_in_pixels.isEmpty()) | |
| 306 return; | |
| 307 blink::WebPrintParams printParams(page_size_in_pixels); | |
| 308 frame->printBegin(printParams); | |
| 309 frame->printEnd(); | |
| 310 } | |
| 311 | |
| 312 blink::WebSpeechRecognizer* WebTestProxyBase::GetSpeechRecognizer() { | |
| 313 return GetSpeechRecognizerMock(); | |
| 314 } | |
| 315 | |
| 316 bool WebTestProxyBase::RequestPointerLock() { | |
| 317 return test_interfaces_->GetTestRunner()->RequestPointerLock(); | |
| 318 } | |
| 319 | |
| 320 void WebTestProxyBase::RequestPointerUnlock() { | |
| 321 test_interfaces_->GetTestRunner()->RequestPointerUnlock(); | |
| 322 } | |
| 323 | |
| 324 bool WebTestProxyBase::IsPointerLocked() { | |
| 325 return test_interfaces_->GetTestRunner()->isPointerLocked(); | |
| 326 } | |
| 327 | |
| 328 void WebTestProxyBase::DidFocus() { | |
| 329 delegate_->SetFocus(this, true); | |
| 330 } | |
| 331 | |
| 332 void WebTestProxyBase::SetToolTipText(const blink::WebString& text, | |
| 333 blink::WebTextDirection direction) { | |
| 334 test_interfaces_->GetTestRunner()->setToolTipText(text); | |
| 335 } | |
| 336 | |
| 337 void WebTestProxyBase::PostSpellCheckEvent(const blink::WebString& event_name) { | 151 void WebTestProxyBase::PostSpellCheckEvent(const blink::WebString& event_name) { |
| 338 if (test_interfaces_->GetTestRunner()->shouldDumpSpellCheckCallbacks()) { | 152 if (test_interfaces_->GetTestRunner()->shouldDumpSpellCheckCallbacks()) { |
| 339 delegate_->PrintMessage(std::string("SpellCheckEvent: ") + | 153 delegate_->PrintMessage(std::string("SpellCheckEvent: ") + |
| 340 event_name.utf8().data() + "\n"); | 154 event_name.utf8().data() + "\n"); |
| 341 } | 155 } |
| 342 } | 156 } |
| 343 | 157 |
| 344 void WebTestProxyBase::ResetInputMethod() { | |
| 345 // If a composition text exists, then we need to let the browser process | |
| 346 // to cancel the input method's ongoing composition session. | |
| 347 if (web_widget_) | |
| 348 web_widget_->confirmComposition(); | |
| 349 } | |
| 350 | |
| 351 blink::WebString WebTestProxyBase::acceptLanguages() { | |
| 352 return blink::WebString::fromUTF8( | |
| 353 test_interfaces_->GetTestRunner()->GetAcceptLanguages()); | |
| 354 } | |
| 355 | |
| 356 } // namespace test_runner | 158 } // namespace test_runner |
| OLD | NEW |