EventSenderProxyQt.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "EventSenderProxy.h"
  27. #include "PlatformWebView.h"
  28. #include "TestController.h"
  29. #include <QGraphicsSceneMouseEvent>
  30. #include <QKeyEvent>
  31. #include <QtTest/QtTest>
  32. #include <WebKit2/WKPagePrivate.h>
  33. #include <WebKit2/WKStringQt.h>
  34. #include <qpa/qwindowsysteminterface.h>
  35. namespace WTR {
  36. #define KEYCODE_DEL 127
  37. #define KEYCODE_BACKSPACE 8
  38. #define KEYCODE_LEFTARROW 0xf702
  39. #define KEYCODE_RIGHTARROW 0xf703
  40. #define KEYCODE_UPARROW 0xf700
  41. #define KEYCODE_DOWNARROW 0xf701
  42. struct WTREventQueue {
  43. QEvent* m_event;
  44. int m_delay;
  45. };
  46. static WTREventQueue eventQueue[1024];
  47. static unsigned endOfQueue;
  48. static bool isReplayingEvents;
  49. EventSenderProxy::EventSenderProxy(TestController* testController)
  50. : m_testController(testController)
  51. , m_time(0)
  52. , m_position()
  53. , m_leftMouseButtonDown(false)
  54. , m_clickCount(0)
  55. , m_clickTime(0)
  56. , m_clickPosition()
  57. , m_clickButton(kWKEventMouseButtonNoButton)
  58. , m_mouseButtons(0)
  59. #if ENABLE(TOUCH_EVENTS)
  60. , m_touchActive(false)
  61. #endif
  62. {
  63. memset(eventQueue, 0, sizeof(eventQueue));
  64. endOfQueue = 0;
  65. isReplayingEvents = false;
  66. }
  67. EventSenderProxy::~EventSenderProxy()
  68. {
  69. }
  70. static Qt::MouseButton getMouseButton(unsigned button)
  71. {
  72. Qt::MouseButton mouseButton;
  73. switch (button) {
  74. case 0:
  75. mouseButton = Qt::LeftButton;
  76. break;
  77. case 1:
  78. mouseButton = Qt::MidButton;
  79. break;
  80. case 2:
  81. mouseButton = Qt::RightButton;
  82. break;
  83. case 3:
  84. // fast/events/mouse-click-events expects the 4th button to be treated as the middle button
  85. mouseButton = Qt::MidButton;
  86. break;
  87. default:
  88. mouseButton = Qt::LeftButton;
  89. break;
  90. }
  91. return mouseButton;
  92. }
  93. static Qt::KeyboardModifiers getModifiers(WKEventModifiers modifiersRef)
  94. {
  95. Qt::KeyboardModifiers modifiers = 0;
  96. if (modifiersRef & kWKEventModifiersControlKey)
  97. modifiers |= Qt::ControlModifier;
  98. if (modifiersRef & kWKEventModifiersShiftKey)
  99. modifiers |= Qt::ShiftModifier;
  100. if (modifiersRef & kWKEventModifiersAltKey)
  101. modifiers |= Qt::AltModifier;
  102. if (modifiersRef & kWKEventModifiersMetaKey)
  103. modifiers |= Qt::MetaModifier;
  104. return modifiers;
  105. }
  106. void EventSenderProxy::keyDown(WKStringRef keyRef, WKEventModifiers modifiersRef, unsigned location)
  107. {
  108. const QString key = WKStringCopyQString(keyRef);
  109. QString keyText = key;
  110. Qt::KeyboardModifiers modifiers = getModifiers(modifiersRef);
  111. if (location == 3)
  112. modifiers |= Qt::KeypadModifier;
  113. int code = 0;
  114. if (key.length() == 1) {
  115. code = key.unicode()->unicode();
  116. // map special keycodes used by the tests to something that works for Qt/X11
  117. if (code == '\r') {
  118. code = Qt::Key_Return;
  119. } else if (code == '\t') {
  120. code = Qt::Key_Tab;
  121. if (modifiers == Qt::ShiftModifier)
  122. code = Qt::Key_Backtab;
  123. keyText = QStringLiteral("\t");
  124. } else if (code == KEYCODE_DEL || code == KEYCODE_BACKSPACE) {
  125. code = Qt::Key_Backspace;
  126. if (modifiers == Qt::AltModifier)
  127. modifiers = Qt::ControlModifier;
  128. keyText = QString();
  129. } else if (code == 'o' && modifiers == Qt::ControlModifier) {
  130. // Mimic the emacs ctrl-o binding on Mac by inserting a paragraph
  131. // separator and then putting the cursor back to its original
  132. // position. Allows us to pass emacs-ctrl-o.html
  133. keyText = QLatin1String("\n");
  134. code = '\n';
  135. modifiers = 0;
  136. QKeyEvent event(QEvent::KeyPress, code, modifiers, keyText);
  137. m_testController->mainWebView()->sendEvent(&event);
  138. QKeyEvent event2(QEvent::KeyRelease, code, modifiers, keyText);
  139. m_testController->mainWebView()->sendEvent(&event2);
  140. keyText = QString();
  141. code = Qt::Key_Left;
  142. } else if (code == 'y' && modifiers == Qt::ControlModifier) {
  143. keyText = QLatin1String("c");
  144. code = 'c';
  145. } else if (code == 'k' && modifiers == Qt::ControlModifier) {
  146. keyText = QLatin1String("x");
  147. code = 'x';
  148. } else if (code == 'a' && modifiers == Qt::ControlModifier) {
  149. keyText = QString();
  150. code = Qt::Key_Home;
  151. modifiers = 0;
  152. } else if (code == KEYCODE_LEFTARROW) {
  153. keyText = QString();
  154. code = Qt::Key_Left;
  155. if (modifiers & Qt::MetaModifier) {
  156. code = Qt::Key_Home;
  157. modifiers &= ~Qt::MetaModifier;
  158. }
  159. } else if (code == KEYCODE_RIGHTARROW) {
  160. keyText = QString();
  161. code = Qt::Key_Right;
  162. if (modifiers & Qt::MetaModifier) {
  163. code = Qt::Key_End;
  164. modifiers &= ~Qt::MetaModifier;
  165. }
  166. } else if (code == KEYCODE_UPARROW) {
  167. keyText = QString();
  168. code = Qt::Key_Up;
  169. if (modifiers & Qt::MetaModifier) {
  170. code = Qt::Key_PageUp;
  171. modifiers &= ~Qt::MetaModifier;
  172. }
  173. } else if (code == KEYCODE_DOWNARROW) {
  174. keyText = QString();
  175. code = Qt::Key_Down;
  176. if (modifiers & Qt::MetaModifier) {
  177. code = Qt::Key_PageDown;
  178. modifiers &= ~Qt::MetaModifier;
  179. }
  180. } else
  181. code = key.unicode()->toUpper().unicode();
  182. } else {
  183. if (key.startsWith(QLatin1Char('F')) && key.count() <= 3) {
  184. keyText = keyText.mid(1);
  185. int functionKey = keyText.toInt();
  186. Q_ASSERT(functionKey >= 1 && functionKey <= 35);
  187. code = Qt::Key_F1 + (functionKey - 1);
  188. // map special keycode strings used by the tests to something that works for Qt/X11
  189. } else if (key == QLatin1String("leftArrow")) {
  190. keyText = QString();
  191. code = Qt::Key_Left;
  192. } else if (key == QLatin1String("rightArrow")) {
  193. keyText = QString();
  194. code = Qt::Key_Right;
  195. } else if (key == QLatin1String("upArrow")) {
  196. keyText = QString();
  197. code = Qt::Key_Up;
  198. } else if (key == QLatin1String("downArrow")) {
  199. keyText = QString();
  200. code = Qt::Key_Down;
  201. } else if (key == QLatin1String("pageUp")) {
  202. keyText = QString();
  203. code = Qt::Key_PageUp;
  204. } else if (key == QLatin1String("pageDown")) {
  205. keyText = QString();
  206. code = Qt::Key_PageDown;
  207. } else if (key == QLatin1String("home")) {
  208. keyText = QString();
  209. code = Qt::Key_Home;
  210. } else if (key == QLatin1String("end")) {
  211. keyText = QString();
  212. code = Qt::Key_End;
  213. } else if (key == QLatin1String("insert")) {
  214. keyText = QString();
  215. code = Qt::Key_Insert;
  216. } else if (key == QLatin1String("delete")) {
  217. keyText = QString();
  218. code = Qt::Key_Delete;
  219. } else if (key == QLatin1String("printScreen")) {
  220. keyText = QString();
  221. code = Qt::Key_Print;
  222. } else if (key == QLatin1String("menu")) {
  223. keyText = QString();
  224. code = Qt::Key_Menu;
  225. }
  226. }
  227. QKeyEvent* pressEvent = new QKeyEvent(QEvent::KeyPress, code, modifiers, keyText);
  228. sendOrQueueEvent(pressEvent);
  229. QKeyEvent* releaseEvent = new QKeyEvent(QEvent::KeyRelease, code, modifiers, keyText);
  230. sendOrQueueEvent(releaseEvent);
  231. }
  232. void EventSenderProxy::updateClickCountForButton(int button)
  233. {
  234. if (m_time - m_clickTime < QApplication::doubleClickInterval() && m_position == m_clickPosition && button == m_clickButton) {
  235. m_clickTime = m_time;
  236. return;
  237. }
  238. m_clickTime = m_time;
  239. m_clickPosition = m_position;
  240. m_clickButton = button;
  241. }
  242. void EventSenderProxy::mouseDown(unsigned button, WKEventModifiers wkModifiers)
  243. {
  244. Qt::KeyboardModifiers modifiers = getModifiers(wkModifiers);
  245. Qt::MouseButton mouseButton = getMouseButton(button);
  246. updateClickCountForButton(button);
  247. m_mouseButtons |= mouseButton;
  248. QPoint mousePos(m_position.x, m_position.y);
  249. QMouseEvent* event = new QMouseEvent(QEvent::MouseButtonPress,
  250. mousePos, mousePos, mouseButton, m_mouseButtons, modifiers);
  251. // We aren't generating MouseButtonDblClick events as they aren't used.
  252. sendOrQueueEvent(event);
  253. }
  254. void EventSenderProxy::mouseUp(unsigned button, WKEventModifiers)
  255. {
  256. Qt::MouseButton mouseButton = getMouseButton(button);
  257. m_mouseButtons &= ~mouseButton;
  258. QPoint mousePos(m_position.x, m_position.y);
  259. QMouseEvent* event = new QMouseEvent(QEvent::MouseButtonRelease,
  260. mousePos, mousePos, mouseButton, m_mouseButtons, Qt::NoModifier);
  261. sendOrQueueEvent(event);
  262. }
  263. void EventSenderProxy::mouseMoveTo(double x, double y)
  264. {
  265. m_position.x = x;
  266. m_position.y = y;
  267. QPoint mousePos(m_position.x, m_position.y);
  268. QMouseEvent* event = new QMouseEvent(QEvent::MouseMove,
  269. mousePos, mousePos, Qt::NoButton, m_mouseButtons, Qt::NoModifier);
  270. sendOrQueueEvent(event);
  271. }
  272. void EventSenderProxy::mouseScrollBy(int, int)
  273. {
  274. // FIXME: Implement this.
  275. }
  276. void EventSenderProxy::continuousMouseScrollBy(int, int, bool)
  277. {
  278. // FIXME: Implement this.
  279. }
  280. void EventSenderProxy::leapForward(int ms)
  281. {
  282. eventQueue[endOfQueue].m_delay = ms;
  283. m_time += ms;
  284. }
  285. #if ENABLE(TOUCH_EVENTS)
  286. void EventSenderProxy::addTouchPoint(int x, int y)
  287. {
  288. const int id = m_touchPoints.isEmpty() ? 0 : m_touchPoints.last().id() + 1;
  289. const QPointF pos(x, y);
  290. QTouchEvent::TouchPoint point(id);
  291. point.setPos(pos);
  292. point.setStartPos(pos);
  293. point.setState(Qt::TouchPointPressed);
  294. if (!m_touchPointRadius.isNull())
  295. point.setRect(QRectF(pos - m_touchPointRadius, pos + m_touchPointRadius));
  296. m_touchPoints.append(point);
  297. }
  298. void EventSenderProxy::updateTouchPoint(int index, int x, int y)
  299. {
  300. ASSERT(index >= 0 && index < m_touchPoints.count());
  301. QPointF pos(x, y);
  302. QTouchEvent::TouchPoint &p = m_touchPoints[index];
  303. p.setPos(pos);
  304. p.setState(Qt::TouchPointMoved);
  305. if (!m_touchPointRadius.isNull())
  306. p.setRect(QRectF(pos - m_touchPointRadius, pos + m_touchPointRadius));
  307. }
  308. void EventSenderProxy::setTouchModifier(WKEventModifiers modifier, bool enable)
  309. {
  310. Qt::KeyboardModifiers mod = getModifiers(modifier);
  311. if (enable)
  312. m_touchModifiers |= mod;
  313. else
  314. m_touchModifiers &= ~mod;
  315. }
  316. void EventSenderProxy::setTouchPointRadius(int radiusX, int radiusY)
  317. {
  318. m_touchPointRadius = QPoint(radiusX, radiusY);
  319. }
  320. void EventSenderProxy::touchStart()
  321. {
  322. if (!m_touchActive) {
  323. sendTouchEvent(QEvent::TouchBegin);
  324. m_touchActive = true;
  325. } else
  326. sendTouchEvent(QEvent::TouchUpdate);
  327. }
  328. void EventSenderProxy::touchMove()
  329. {
  330. sendTouchEvent(QEvent::TouchUpdate);
  331. }
  332. void EventSenderProxy::touchEnd()
  333. {
  334. for (int i = 0; i < m_touchPoints.count(); ++i) {
  335. if (m_touchPoints[i].state() != Qt::TouchPointReleased) {
  336. sendTouchEvent(QEvent::TouchUpdate);
  337. return;
  338. }
  339. }
  340. sendTouchEvent(QEvent::TouchEnd);
  341. m_touchActive = false;
  342. }
  343. void EventSenderProxy::touchCancel()
  344. {
  345. sendTouchEvent(QEvent::TouchCancel);
  346. m_touchActive = false;
  347. }
  348. void EventSenderProxy::clearTouchPoints()
  349. {
  350. m_touchPoints.clear();
  351. m_touchModifiers = Qt::KeyboardModifiers();
  352. m_touchActive = false;
  353. m_touchPointRadius = QPoint();
  354. }
  355. void EventSenderProxy::releaseTouchPoint(int index)
  356. {
  357. if (index < 0 || index >= m_touchPoints.count())
  358. return;
  359. m_touchPoints[index].setState(Qt::TouchPointReleased);
  360. }
  361. void EventSenderProxy::cancelTouchPoint(int index)
  362. {
  363. // FIXME: No cancellation state in Qt 5, mapped to release instead.
  364. // PlatformTouchEvent conversion later will map all touch points to
  365. // cancelled.
  366. releaseTouchPoint(index);
  367. }
  368. void EventSenderProxy::sendTouchEvent(QEvent::Type type)
  369. {
  370. static QTouchDevice* device = 0;
  371. if (!device) {
  372. device = new QTouchDevice;
  373. device->setType(QTouchDevice::TouchScreen);
  374. QWindowSystemInterface::registerTouchDevice(device);
  375. }
  376. Qt::TouchPointStates eventStates;
  377. for (int i = 0; i < m_touchPoints.count(); i++)
  378. eventStates |= m_touchPoints[i].state();
  379. QTouchEvent event(type, device, m_touchModifiers, eventStates);
  380. event.setTouchPoints(m_touchPoints);
  381. m_testController->mainWebView()->sendEvent(&event);
  382. QList<QTouchEvent::TouchPoint>::Iterator it = m_touchPoints.begin();
  383. while (it != m_touchPoints.end()) {
  384. if (it->state() == Qt::TouchPointReleased)
  385. it = m_touchPoints.erase(it);
  386. else {
  387. it->setState(Qt::TouchPointStationary);
  388. ++it;
  389. }
  390. }
  391. }
  392. #endif
  393. void EventSenderProxy::sendOrQueueEvent(QEvent* event)
  394. {
  395. if (!endOfQueue && !eventQueue[endOfQueue].m_delay) {
  396. m_testController->mainWebView()->sendEvent(event);
  397. delete event;
  398. return;
  399. }
  400. eventQueue[endOfQueue++].m_event = event;
  401. replaySavedEvents();
  402. }
  403. void EventSenderProxy::replaySavedEvents()
  404. {
  405. if (isReplayingEvents)
  406. return;
  407. isReplayingEvents = true;
  408. int i = 0;
  409. while (i < endOfQueue) {
  410. WTREventQueue& ev = eventQueue[i];
  411. if (ev.m_delay)
  412. QTest::qWait(ev.m_delay);
  413. i++;
  414. m_testController->mainWebView()->sendEvent(ev.m_event);
  415. delete ev.m_event;
  416. ev.m_delay = 0;
  417. }
  418. endOfQueue = 0;
  419. isReplayingEvents = false;
  420. }
  421. } // namespace WTR