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 "config.h" 32 33 #include "WebInputEventConversion.h" 34 35 #include <gtest/gtest.h> 36 #include "FrameTestHelpers.h" 37 #include "URLTestHelpers.h" 38 #include "WebFrame.h" 39 #include "WebSettings.h" 40 #include "WebViewImpl.h" 41 #include "core/dom/GestureEvent.h" 42 #include "core/dom/KeyboardEvent.h" 43 #include "core/dom/MouseEvent.h" 44 #include "core/dom/Touch.h" 45 #include "core/dom/TouchEvent.h" 46 #include "core/dom/TouchList.h" 47 #include "core/page/Frame.h" 48 #include "core/page/FrameView.h" 49 50 using namespace WebKit; 51 using namespace WebCore; 52 53 namespace { 54 55 PassRefPtr<WebCore::KeyboardEvent> createKeyboardEventWithLocation(WebCore::KeyboardEvent::KeyLocationCode location) 56 { 57 return WebCore::KeyboardEvent::create("keydown", true, true, 0, "", location, false, false, false, false, false); 58 } 59 60 int getModifiersForKeyLocationCode(WebCore::KeyboardEvent::KeyLocationCode location) 61 { 62 RefPtr<WebCore::KeyboardEvent> event = createKeyboardEventWithLocation(location); 63 WebKit::WebKeyboardEventBuilder convertedEvent(*event); 64 return convertedEvent.modifiers; 65 } 66 67 TEST(WebInputEventConversionTest, WebKeyboardEventBuilder) 68 { 69 // Test key location conversion. 70 int modifiers = getModifiersForKeyLocationCode(WebCore::KeyboardEvent::DOM_KEY_LOCATION_STANDARD); 71 EXPECT_FALSE(modifiers & WebInputEvent::IsKeyPad || modifiers & WebInputEvent::IsLeft || modifiers & WebInputEvent::IsRight); 72 73 modifiers = getModifiersForKeyLocationCode(WebCore::KeyboardEvent::DOM_KEY_LOCATION_LEFT); 74 EXPECT_TRUE(modifiers & WebInputEvent::IsLeft); 75 EXPECT_FALSE(modifiers & WebInputEvent::IsKeyPad || modifiers & WebInputEvent::IsRight); 76 77 modifiers = getModifiersForKeyLocationCode(WebCore::KeyboardEvent::DOM_KEY_LOCATION_RIGHT); 78 EXPECT_TRUE(modifiers & WebInputEvent::IsRight); 79 EXPECT_FALSE(modifiers & WebInputEvent::IsKeyPad || modifiers & WebInputEvent::IsLeft); 80 81 modifiers = getModifiersForKeyLocationCode(WebCore::KeyboardEvent::DOM_KEY_LOCATION_NUMPAD); 82 EXPECT_TRUE(modifiers & WebInputEvent::IsKeyPad); 83 EXPECT_FALSE(modifiers & WebInputEvent::IsLeft || modifiers & WebInputEvent::IsRight); 84 } 85 86 TEST(WebInputEventConversionTest, WebTouchEventBuilder) 87 { 88 RefPtr<WebCore::TouchEvent> event = WebCore::TouchEvent::create(); 89 WebMouseEventBuilder mouse(0, 0, *event); 90 EXPECT_EQ(WebInputEvent::Undefined, mouse.type); 91 } 92 93 TEST(WebInputEventConversionTest, InputEventsScaling) 94 { 95 const std::string baseURL("http://www.test.com/"); 96 const std::string fileName("fixed_layout.html"); 97 98 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8("fixed_layout.html")); 99 WebViewImpl* webViewImpl = static_cast<WebViewImpl*>(FrameTestHelpers::createWebViewAndLoad(baseURL + fileName, true)); 100 webViewImpl->enableFixedLayoutMode(true); 101 webViewImpl->settings()->setViewportEnabled(true); 102 int pageWidth = 640; 103 int pageHeight = 480; 104 webViewImpl->resize(WebSize(pageWidth, pageHeight)); 105 webViewImpl->layout(); 106 107 webViewImpl->setPageScaleFactor(2, WebPoint()); 108 109 FrameView* view = webViewImpl->page()->mainFrame()->view(); 110 RefPtr<Document> document = webViewImpl->page()->mainFrame()->document(); 111 DOMWindow* domWindow = webViewImpl->page()->mainFrame()->document()->defaultView(); 112 RenderObject* docRenderer = webViewImpl->page()->mainFrame()->document()->renderer(); 113 114 { 115 WebMouseEvent webMouseEvent; 116 webMouseEvent.type = WebInputEvent::MouseMove; 117 webMouseEvent.x = 10; 118 webMouseEvent.y = 10; 119 webMouseEvent.windowX = 10; 120 webMouseEvent.windowY = 10; 121 webMouseEvent.globalX = 10; 122 webMouseEvent.globalY = 10; 123 webMouseEvent.movementX = 10; 124 webMouseEvent.movementY = 10; 125 126 PlatformMouseEventBuilder platformMouseBuilder(view, webMouseEvent); 127 EXPECT_EQ(5, platformMouseBuilder.position().x()); 128 EXPECT_EQ(5, platformMouseBuilder.position().y()); 129 EXPECT_EQ(10, platformMouseBuilder.globalPosition().x()); 130 EXPECT_EQ(10, platformMouseBuilder.globalPosition().y()); 131 EXPECT_EQ(5, platformMouseBuilder.movementDelta().x()); 132 EXPECT_EQ(5, platformMouseBuilder.movementDelta().y()); 133 } 134 135 { 136 WebGestureEvent webGestureEvent; 137 webGestureEvent.type = WebInputEvent::GestureScrollUpdate; 138 webGestureEvent.x = 10; 139 webGestureEvent.y = 10; 140 webGestureEvent.globalX = 10; 141 webGestureEvent.globalY = 10; 142 webGestureEvent.data.scrollUpdate.deltaX = 10; 143 webGestureEvent.data.scrollUpdate.deltaY = 10; 144 145 PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent); 146 EXPECT_EQ(5, platformGestureBuilder.position().x()); 147 EXPECT_EQ(5, platformGestureBuilder.position().y()); 148 EXPECT_EQ(10, platformGestureBuilder.globalPosition().x()); 149 EXPECT_EQ(10, platformGestureBuilder.globalPosition().y()); 150 EXPECT_EQ(5, platformGestureBuilder.deltaX()); 151 EXPECT_EQ(5, platformGestureBuilder.deltaY()); 152 } 153 154 { 155 WebGestureEvent webGestureEvent; 156 webGestureEvent.type = WebInputEvent::GestureTap; 157 webGestureEvent.data.tap.width = 10; 158 webGestureEvent.data.tap.height = 10; 159 160 PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent); 161 EXPECT_EQ(5, platformGestureBuilder.area().width()); 162 EXPECT_EQ(5, platformGestureBuilder.area().height()); 163 } 164 165 { 166 WebGestureEvent webGestureEvent; 167 webGestureEvent.type = WebInputEvent::GestureTapUnconfirmed; 168 webGestureEvent.data.tap.width = 10; 169 webGestureEvent.data.tap.height = 10; 170 171 PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent); 172 EXPECT_EQ(5, platformGestureBuilder.area().width()); 173 EXPECT_EQ(5, platformGestureBuilder.area().height()); 174 } 175 176 { 177 WebGestureEvent webGestureEvent; 178 webGestureEvent.type = WebInputEvent::GestureTapDown; 179 webGestureEvent.data.tapDown.width = 10; 180 webGestureEvent.data.tapDown.height = 10; 181 182 PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent); 183 EXPECT_EQ(5, platformGestureBuilder.area().width()); 184 EXPECT_EQ(5, platformGestureBuilder.area().height()); 185 } 186 187 { 188 WebGestureEvent webGestureEvent; 189 webGestureEvent.type = WebInputEvent::GestureLongPress; 190 webGestureEvent.data.longPress.width = 10; 191 webGestureEvent.data.longPress.height = 10; 192 193 PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent); 194 EXPECT_EQ(5, platformGestureBuilder.area().width()); 195 EXPECT_EQ(5, platformGestureBuilder.area().height()); 196 } 197 198 { 199 WebGestureEvent webGestureEvent; 200 webGestureEvent.type = WebInputEvent::GestureTwoFingerTap; 201 webGestureEvent.data.twoFingerTap.firstFingerWidth = 10; 202 webGestureEvent.data.twoFingerTap.firstFingerHeight = 10; 203 204 PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent); 205 EXPECT_EQ(5, platformGestureBuilder.area().width()); 206 EXPECT_EQ(5, platformGestureBuilder.area().height()); 207 } 208 209 { 210 WebTouchEvent webTouchEvent; 211 webTouchEvent.type = WebInputEvent::TouchMove; 212 webTouchEvent.touchesLength = 1; 213 webTouchEvent.touches[0].state = WebTouchPoint::StateMoved; 214 webTouchEvent.touches[0].screenPosition.x = 10; 215 webTouchEvent.touches[0].screenPosition.y = 10; 216 webTouchEvent.touches[0].position.x = 10; 217 webTouchEvent.touches[0].position.y = 10; 218 webTouchEvent.touches[0].radiusX = 10; 219 webTouchEvent.touches[0].radiusY = 10; 220 221 PlatformTouchEventBuilder platformTouchBuilder(view, webTouchEvent); 222 EXPECT_EQ(10, platformTouchBuilder.touchPoints()[0].screenPos().x()); 223 EXPECT_EQ(10, platformTouchBuilder.touchPoints()[0].screenPos().y()); 224 EXPECT_EQ(5, platformTouchBuilder.touchPoints()[0].pos().x()); 225 EXPECT_EQ(5, platformTouchBuilder.touchPoints()[0].pos().y()); 226 EXPECT_EQ(5, platformTouchBuilder.touchPoints()[0].radiusX()); 227 EXPECT_EQ(5, platformTouchBuilder.touchPoints()[0].radiusY()); 228 } 229 230 // Reverse builders should *not* go back to physical pixels, as they are used for plugins 231 // which expect CSS pixel coordinates. 232 { 233 PlatformMouseEvent platformMouseEvent(IntPoint(10, 10), IntPoint(10, 10), LeftButton, PlatformEvent::MouseMoved, 1, false, false, false, false, 0); 234 RefPtr<MouseEvent> mouseEvent = MouseEvent::create(WebCore::eventNames().mousemoveEvent, domWindow, platformMouseEvent, 0, document); 235 WebMouseEventBuilder webMouseBuilder(view, docRenderer, *mouseEvent); 236 237 EXPECT_EQ(10, webMouseBuilder.x); 238 EXPECT_EQ(10, webMouseBuilder.y); 239 EXPECT_EQ(10, webMouseBuilder.globalX); 240 EXPECT_EQ(10, webMouseBuilder.globalY); 241 EXPECT_EQ(10, webMouseBuilder.windowX); 242 EXPECT_EQ(10, webMouseBuilder.windowY); 243 } 244 245 { 246 PlatformMouseEvent platformMouseEvent(IntPoint(10, 10), IntPoint(10, 10), NoButton, PlatformEvent::MouseMoved, 1, false, false, false, false, 0); 247 RefPtr<MouseEvent> mouseEvent = MouseEvent::create(WebCore::eventNames().mousemoveEvent, domWindow, platformMouseEvent, 0, document); 248 WebMouseEventBuilder webMouseBuilder(view, docRenderer, *mouseEvent); 249 EXPECT_EQ(WebMouseEvent::ButtonNone, webMouseBuilder.button); 250 } 251 252 { 253 PlatformGestureEvent platformGestureEvent(PlatformEvent::GestureScrollUpdate, IntPoint(10, 10), IntPoint(10, 10), 0, IntSize(10, 10), FloatPoint(10, 10), false, false, false, false); 254 RefPtr<GestureEvent> gestureEvent = GestureEvent::create(domWindow, platformGestureEvent); 255 WebGestureEventBuilder webGestureBuilder(view, docRenderer, *gestureEvent); 256 257 EXPECT_EQ(10, webGestureBuilder.x); 258 EXPECT_EQ(10, webGestureBuilder.y); 259 EXPECT_EQ(10, webGestureBuilder.globalX); 260 EXPECT_EQ(10, webGestureBuilder.globalY); 261 EXPECT_EQ(10, webGestureBuilder.data.scrollUpdate.deltaX); 262 EXPECT_EQ(10, webGestureBuilder.data.scrollUpdate.deltaY); 263 } 264 265 { 266 RefPtr<Touch> touch = Touch::create(webViewImpl->page()->mainFrame(), document.get(), 0, 10, 10, 10, 10, 10, 10, 0, 0); 267 RefPtr<TouchList> touchList = TouchList::create(); 268 touchList->append(touch); 269 RefPtr<TouchEvent> touchEvent = TouchEvent::create(touchList.get(), touchList.get(), touchList.get(), WebCore::eventNames().touchmoveEvent, domWindow, 10, 10, 10, 10, false, false, false, false); 270 271 WebTouchEventBuilder webTouchBuilder(view, docRenderer, *touchEvent); 272 ASSERT_EQ(1u, webTouchBuilder.touchesLength); 273 EXPECT_EQ(10, webTouchBuilder.touches[0].screenPosition.x); 274 EXPECT_EQ(10, webTouchBuilder.touches[0].screenPosition.y); 275 EXPECT_EQ(10, webTouchBuilder.touches[0].position.x); 276 EXPECT_EQ(10, webTouchBuilder.touches[0].position.y); 277 EXPECT_EQ(10, webTouchBuilder.touches[0].radiusX); 278 EXPECT_EQ(10, webTouchBuilder.touches[0].radiusY); 279 } 280 281 webViewImpl->close(); 282 } 283 284 } // anonymous namespace 285