PlatformWebViewQt.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2010 Apple Inc. All rights reserved.
  3. * Copyright (C) 2010 University of Szeged. All rights reserved.
  4. * Copyright (C) 2013 Digia Plc. and/or its subsidiary(-ies).
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "config.h"
  28. #include "PlatformWebView.h"
  29. #include "qquickwebpage_p.h"
  30. #include "qquickwebview_p.h"
  31. #include <WebKit2/WKRetainPtr.h>
  32. #include <QCoreApplication>
  33. #include <QEventLoop>
  34. #include <QQmlProperty>
  35. #include <QtQuick/QQuickView>
  36. #include <qpa/qwindowsysteminterface.h>
  37. namespace TestWebKitAPI {
  38. class WrapperWindow : public QQuickView {
  39. Q_OBJECT
  40. public:
  41. WrapperWindow(QQuickWebView* view)
  42. : QQuickView(QUrl(QStringLiteral("data:text/plain,import QtQuick 2.0\nItem { objectName: 'root' }")))
  43. , m_view(view)
  44. {
  45. if (status() == QQuickView::Ready)
  46. handleStatusChanged(QQuickView::Ready);
  47. else
  48. connect(this, SIGNAL(statusChanged(QQuickView::Status)), SLOT(handleStatusChanged(QQuickView::Status)));
  49. }
  50. private Q_SLOTS:
  51. void handleStatusChanged(QQuickView::Status status)
  52. {
  53. if (status != QQuickView::Ready)
  54. return;
  55. setGeometry(0, 0, 800, 600);
  56. setResizeMode(QQuickView::SizeRootObjectToView);
  57. m_view->setParentItem(rootObject());
  58. QQmlProperty::write(m_view, QStringLiteral("anchors.fill"), qVariantFromValue(rootObject()));
  59. m_view->experimental()->setRenderToOffscreenBuffer(true);
  60. QWindowSystemInterface::handleWindowActivated(this);
  61. m_view->page()->setFocus(true);
  62. }
  63. private:
  64. QQuickWebView* m_view;
  65. };
  66. PlatformWebView::PlatformWebView(WKContextRef contextRef, WKPageGroupRef pageGroupRef)
  67. {
  68. m_view = new QQuickWebView(contextRef, pageGroupRef);
  69. m_view->setAllowAnyHTTPSCertificateForLocalHost(true);
  70. m_view->componentComplete();
  71. m_window = new WrapperWindow(m_view);
  72. }
  73. PlatformWebView::~PlatformWebView()
  74. {
  75. delete m_window;
  76. }
  77. void PlatformWebView::resizeTo(unsigned width, unsigned height)
  78. {
  79. // If we do not have a platform window we will never get the necessary
  80. // resize event, so simulate it in that case to make sure the quickview is
  81. // resized to what the api tests expects.
  82. if (!m_window->handle()) {
  83. QRect newGeometry(m_window->x(), m_window->y(), width, height);
  84. QWindowSystemInterface::handleGeometryChange(m_window, newGeometry);
  85. QWindowSystemInterface::flushWindowSystemEvents();
  86. }
  87. m_window->resize(width, height);
  88. }
  89. WKPageRef PlatformWebView::page() const
  90. {
  91. return m_view->pageRef();
  92. }
  93. void PlatformWebView::focus()
  94. {
  95. m_view->setFocus(true);
  96. }
  97. void PlatformWebView::simulateSpacebarKeyPress()
  98. {
  99. QKeyEvent event(QEvent::KeyPress, Qt::Key_Space, Qt::NoModifier);
  100. QCoreApplication::sendEvent(m_window, &event);
  101. QKeyEvent event2(QEvent::KeyRelease, Qt::Key_Space, Qt::NoModifier);
  102. QCoreApplication::sendEvent(m_window, &event2);
  103. }
  104. void PlatformWebView::simulateAltKeyPress()
  105. {
  106. QKeyEvent event(QEvent::KeyPress, Qt::Key_Alt, Qt::NoModifier);
  107. QCoreApplication::sendEvent(m_window, &event);
  108. QKeyEvent event2(QEvent::KeyRelease, Qt::Key_Alt, Qt::NoModifier);
  109. QCoreApplication::sendEvent(m_window, &event2);
  110. }
  111. void PlatformWebView::simulateMouseMove(unsigned x, unsigned y)
  112. {
  113. QPointF mousePos(x, y);
  114. QMouseEvent event(QEvent::MouseMove, mousePos, Qt::NoButton, Qt::NoButton, Qt::NoModifier);
  115. QCoreApplication::sendEvent(m_window, &event);
  116. }
  117. void PlatformWebView::simulateRightClick(unsigned x, unsigned y)
  118. {
  119. QPointF mousePos(x, y);
  120. QMouseEvent event2(QEvent::MouseButtonPress, mousePos, Qt::RightButton, Qt::RightButton, Qt::NoModifier);
  121. QCoreApplication::sendEvent(m_window, &event2);
  122. QMouseEvent event3(QEvent::MouseButtonRelease, mousePos, Qt::RightButton, Qt::NoButton, Qt::NoModifier);
  123. QCoreApplication::sendEvent(m_window, &event3);
  124. }
  125. } // namespace TestWebKitAPI
  126. #include "PlatformWebViewQt.moc"