| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "components/test_runner/web_view_test_client.h" |
| 6 |
| 7 #include "base/i18n/rtl.h" |
| 8 #include "base/strings/string16.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/time/time.h" |
| 11 #include "components/test_runner/event_sender.h" |
| 12 #include "components/test_runner/mock_web_speech_recognizer.h" |
| 13 #include "components/test_runner/test_common.h" |
| 14 #include "components/test_runner/test_runner.h" |
| 15 #include "components/test_runner/web_task.h" |
| 16 #include "components/test_runner/web_test_delegate.h" |
| 17 #include "components/test_runner/web_test_proxy.h" |
| 18 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 19 #include "third_party/WebKit/public/web/WebFrame.h" |
| 20 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 21 #include "third_party/WebKit/public/web/WebPagePopup.h" |
| 22 #include "third_party/WebKit/public/web/WebPrintParams.h" |
| 23 #include "third_party/WebKit/public/web/WebView.h" |
| 24 #include "third_party/WebKit/public/web/WebWidget.h" |
| 25 |
| 26 namespace test_runner { |
| 27 |
| 28 namespace { |
| 29 |
| 30 class HostMethodTask : public WebMethodTask<WebViewTestClient> { |
| 31 public: |
| 32 typedef void (WebViewTestClient::*CallbackMethodType)(); |
| 33 HostMethodTask(WebViewTestClient* object, CallbackMethodType callback) |
| 34 : WebMethodTask<WebViewTestClient>(object), callback_(callback) {} |
| 35 |
| 36 void RunIfValid() override { (object_->*callback_)(); } |
| 37 |
| 38 private: |
| 39 CallbackMethodType callback_; |
| 40 }; |
| 41 |
| 42 } // namespace |
| 43 |
| 44 WebViewTestClient::WebViewTestClient(TestRunner* test_runner, |
| 45 WebTestDelegate* delegate, |
| 46 EventSender* event_sender, |
| 47 WebTestProxyBase* web_test_proxy_base) |
| 48 : test_runner_(test_runner), |
| 49 delegate_(delegate), |
| 50 event_sender_(event_sender), |
| 51 web_test_proxy_base_(web_test_proxy_base), |
| 52 animation_scheduled_(false) { |
| 53 DCHECK(test_runner); |
| 54 DCHECK(delegate); |
| 55 DCHECK(event_sender); |
| 56 DCHECK(web_test_proxy_base); |
| 57 } |
| 58 |
| 59 WebViewTestClient::~WebViewTestClient() {} |
| 60 |
| 61 void WebViewTestClient::scheduleAnimation() { |
| 62 if (!test_runner_->TestIsRunning()) |
| 63 return; |
| 64 |
| 65 if (!animation_scheduled_) { |
| 66 animation_scheduled_ = true; |
| 67 test_runner_->OnAnimationScheduled(web_test_proxy_base_->web_view()); |
| 68 |
| 69 delegate_->PostDelayedTask( |
| 70 new HostMethodTask(this, &WebViewTestClient::AnimateNow), 1); |
| 71 } |
| 72 } |
| 73 |
| 74 void WebViewTestClient::AnimateNow() { |
| 75 if (animation_scheduled_) { |
| 76 blink::WebWidget* web_widget = web_test_proxy_base_->web_widget(); |
| 77 animation_scheduled_ = false; |
| 78 test_runner_->OnAnimationBegun(web_test_proxy_base_->web_view()); |
| 79 |
| 80 base::TimeDelta animate_time = base::TimeTicks::Now() - base::TimeTicks(); |
| 81 web_widget->beginFrame(animate_time.InSecondsF()); |
| 82 web_widget->updateAllLifecyclePhases(); |
| 83 if (blink::WebPagePopup* popup = web_widget->pagePopup()) { |
| 84 popup->beginFrame(animate_time.InSecondsF()); |
| 85 popup->updateAllLifecyclePhases(); |
| 86 } |
| 87 } |
| 88 } |
| 89 |
| 90 void WebViewTestClient::startDragging(blink::WebLocalFrame* frame, |
| 91 const blink::WebDragData& data, |
| 92 blink::WebDragOperationsMask mask, |
| 93 const blink::WebImage& image, |
| 94 const blink::WebPoint& point) { |
| 95 test_runner_->setDragImage(image); |
| 96 |
| 97 // When running a test, we need to fake a drag drop operation otherwise |
| 98 // Windows waits for real mouse events to know when the drag is over. |
| 99 event_sender_->DoDragDrop(data, mask); |
| 100 } |
| 101 |
| 102 // The output from these methods in layout test mode should match that |
| 103 // expected by the layout tests. See EditingDelegate.m in DumpRenderTree. |
| 104 |
| 105 void WebViewTestClient::didChangeContents() { |
| 106 if (test_runner_->shouldDumpEditingCallbacks()) |
| 107 delegate_->PrintMessage( |
| 108 "EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification\n"); |
| 109 } |
| 110 |
| 111 blink::WebView* WebViewTestClient::createView( |
| 112 blink::WebLocalFrame* frame, |
| 113 const blink::WebURLRequest& request, |
| 114 const blink::WebWindowFeatures& features, |
| 115 const blink::WebString& frame_name, |
| 116 blink::WebNavigationPolicy policy, |
| 117 bool suppress_opener) { |
| 118 if (test_runner_->shouldDumpNavigationPolicy()) { |
| 119 delegate_->PrintMessage("Default policy for createView for '" + |
| 120 URLDescription(request.url()) + "' is '" + |
| 121 WebNavigationPolicyToString(policy) + "'\n"); |
| 122 } |
| 123 |
| 124 if (!test_runner_->canOpenWindows()) |
| 125 return nullptr; |
| 126 if (test_runner_->shouldDumpCreateView()) |
| 127 delegate_->PrintMessage(std::string("createView(") + |
| 128 URLDescription(request.url()) + ")\n"); |
| 129 |
| 130 // The return value below is used to communicate to WebTestProxy whether it |
| 131 // should forward the createView request to RenderViewImpl or not. The |
| 132 // somewhat ugly cast is used to do this while fitting into the existing |
| 133 // WebViewClient interface. |
| 134 return reinterpret_cast<blink::WebView*>(0xdeadbeef); |
| 135 } |
| 136 |
| 137 void WebViewTestClient::setStatusText(const blink::WebString& text) { |
| 138 if (!test_runner_->shouldDumpStatusCallbacks()) |
| 139 return; |
| 140 delegate_->PrintMessage( |
| 141 std::string("UI DELEGATE STATUS CALLBACK: setStatusText:") + |
| 142 text.utf8().data() + "\n"); |
| 143 } |
| 144 |
| 145 // Simulate a print by going into print mode and then exit straight away. |
| 146 void WebViewTestClient::printPage(blink::WebLocalFrame* frame) { |
| 147 blink::WebSize page_size_in_pixels = frame->view()->size(); |
| 148 if (page_size_in_pixels.isEmpty()) |
| 149 return; |
| 150 blink::WebPrintParams printParams(page_size_in_pixels); |
| 151 frame->printBegin(printParams); |
| 152 frame->printEnd(); |
| 153 } |
| 154 |
| 155 bool WebViewTestClient::runFileChooser( |
| 156 const blink::WebFileChooserParams& params, |
| 157 blink::WebFileChooserCompletion* completion) { |
| 158 delegate_->PrintMessage("Mock: Opening a file chooser.\n"); |
| 159 // FIXME: Add ability to set file names to a file upload control. |
| 160 return false; |
| 161 } |
| 162 |
| 163 void WebViewTestClient::showValidationMessage( |
| 164 const blink::WebRect& anchor_in_root_view, |
| 165 const blink::WebString& main_message, |
| 166 blink::WebTextDirection main_message_hint, |
| 167 const blink::WebString& sub_message, |
| 168 blink::WebTextDirection sub_message_hint) { |
| 169 base::string16 wrapped_main_text = main_message; |
| 170 base::string16 wrapped_sub_text = sub_message; |
| 171 |
| 172 if (main_message_hint == blink::WebTextDirectionLeftToRight) { |
| 173 wrapped_main_text = |
| 174 base::i18n::GetDisplayStringInLTRDirectionality(wrapped_main_text); |
| 175 } else if (main_message_hint == blink::WebTextDirectionRightToLeft && |
| 176 !base::i18n::IsRTL()) { |
| 177 base::i18n::WrapStringWithRTLFormatting(&wrapped_main_text); |
| 178 } |
| 179 |
| 180 if (!wrapped_sub_text.empty()) { |
| 181 if (sub_message_hint == blink::WebTextDirectionLeftToRight) { |
| 182 wrapped_sub_text = |
| 183 base::i18n::GetDisplayStringInLTRDirectionality(wrapped_sub_text); |
| 184 } else if (sub_message_hint == blink::WebTextDirectionRightToLeft) { |
| 185 base::i18n::WrapStringWithRTLFormatting(&wrapped_sub_text); |
| 186 } |
| 187 } |
| 188 delegate_->PrintMessage("ValidationMessageClient: main-message=" + |
| 189 base::UTF16ToUTF8(wrapped_main_text) + |
| 190 " sub-message=" + |
| 191 base::UTF16ToUTF8(wrapped_sub_text) + "\n"); |
| 192 } |
| 193 |
| 194 blink::WebSpeechRecognizer* WebViewTestClient::speechRecognizer() { |
| 195 return test_runner_->getMockWebSpeechRecognizer(); |
| 196 } |
| 197 |
| 198 bool WebViewTestClient::requestPointerLock() { |
| 199 return test_runner_->RequestPointerLock(); |
| 200 } |
| 201 |
| 202 void WebViewTestClient::requestPointerUnlock() { |
| 203 test_runner_->RequestPointerUnlock(); |
| 204 } |
| 205 |
| 206 bool WebViewTestClient::isPointerLocked() { |
| 207 return test_runner_->isPointerLocked(); |
| 208 } |
| 209 |
| 210 void WebViewTestClient::didFocus() { |
| 211 delegate_->SetFocus(web_test_proxy_base_->web_view(), true); |
| 212 } |
| 213 |
| 214 void WebViewTestClient::setToolTipText(const blink::WebString& text, |
| 215 blink::WebTextDirection direction) { |
| 216 test_runner_->setToolTipText(text); |
| 217 } |
| 218 |
| 219 void WebViewTestClient::resetInputMethod() { |
| 220 // If a composition text exists, then we need to let the browser process |
| 221 // to cancel the input method's ongoing composition session. |
| 222 if (web_test_proxy_base_) |
| 223 web_test_proxy_base_->web_widget()->confirmComposition(); |
| 224 } |
| 225 |
| 226 blink::WebString WebViewTestClient::acceptLanguages() { |
| 227 return blink::WebString::fromUTF8(test_runner_->GetAcceptLanguages()); |
| 228 } |
| 229 |
| 230 } // namespace test_runner |
| OLD | NEW |