tst_qgraphicswebview.cpp revision 65f03d4f644ce73618e5f4f50dd694b26f55ae12
1/*
2    Copyright (C) 2009 Jakub Wieczorek <faw217@gmail.com>
3
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17    Boston, MA 02110-1301, USA.
18*/
19
20#include "../util.h"
21#include <QtTest/QtTest>
22#include <QGraphicsSceneMouseEvent>
23#include <QGraphicsView>
24#include <qgraphicswebview.h>
25#include <qwebpage.h>
26#include <qwebframe.h>
27
28class tst_QGraphicsWebView : public QObject
29{
30    Q_OBJECT
31
32private slots:
33    void qgraphicswebview();
34    void crashOnViewlessWebPages();
35    void microFocusCoordinates();
36    void focusInputTypes();
37    void crashOnSetScaleBeforeSetUrl();
38    void widgetsRenderingThroughCache();
39};
40
41void tst_QGraphicsWebView::qgraphicswebview()
42{
43    QGraphicsWebView item;
44    item.url();
45    item.title();
46    item.icon();
47    item.zoomFactor();
48    item.history();
49    item.settings();
50    item.page();
51    item.setPage(0);
52    item.page();
53    item.setUrl(QUrl());
54    item.setZoomFactor(0);
55    item.load(QUrl());
56    item.setHtml(QString());
57    item.setContent(QByteArray());
58    item.isModified();
59}
60
61class WebPage : public QWebPage
62{
63    Q_OBJECT
64
65public:
66    WebPage(QObject* parent = 0): QWebPage(parent)
67    {
68    }
69
70    QGraphicsWebView* webView;
71
72private slots:
73    // Force a webview deletion during the load.
74    // It should not cause WebPage to crash due to
75    // it accessing invalid pageClient pointer.
76    void aborting()
77    {
78        delete webView;
79    }
80};
81
82class GraphicsWebView : public QGraphicsWebView
83{
84    Q_OBJECT
85
86public:
87    GraphicsWebView(QGraphicsItem* parent = 0): QGraphicsWebView(parent)
88    {
89    }
90
91    void fireMouseClick(QPointF point) {
92        QGraphicsSceneMouseEvent presEv(QEvent::GraphicsSceneMousePress);
93        presEv.setPos(point);
94        presEv.setButton(Qt::LeftButton);
95        presEv.setButtons(Qt::LeftButton);
96        QGraphicsSceneMouseEvent relEv(QEvent::GraphicsSceneMouseRelease);
97        relEv.setPos(point);
98        relEv.setButton(Qt::LeftButton);
99        relEv.setButtons(Qt::LeftButton);
100        QGraphicsWebView::sceneEvent(&presEv);
101        QGraphicsWebView::sceneEvent(&relEv);
102    }
103};
104
105void tst_QGraphicsWebView::crashOnViewlessWebPages()
106{
107    QGraphicsScene scene;
108    QGraphicsView view(&scene);
109
110    QGraphicsWebView* webView = new QGraphicsWebView;
111    WebPage* page = new WebPage;
112    webView->setPage(page);
113    page->webView = webView;
114    scene.addItem(webView);
115
116    view.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
117    view.resize(600, 480);
118    webView->resize(view.geometry().size());
119
120    QCoreApplication::processEvents();
121    view.show();
122
123    // Resizing the page will resize and layout the empty "about:blank"
124    // page, so we first connect the signal afterward.
125    connect(page->mainFrame(), SIGNAL(initialLayoutCompleted()), page, SLOT(aborting()));
126
127    page->mainFrame()->load(QUrl("data:text/html,"
128                                 "<frameset cols=\"25%,75%\">"
129                                     "<frame src=\"data:text/html,foo \">"
130                                     "<frame src=\"data:text/html,bar\">"
131                                 "</frameset>"));
132
133    QVERIFY(waitForSignal(page, SIGNAL(loadFinished(bool))));
134    delete page;
135}
136
137void tst_QGraphicsWebView::crashOnSetScaleBeforeSetUrl()
138{
139    QGraphicsWebView* webView = new QGraphicsWebView;
140    webView->setScale(2.0);
141    delete webView;
142}
143
144void tst_QGraphicsWebView::widgetsRenderingThroughCache()
145{
146    // Widgets should be rendered the same way with and without
147    // intermediate cache (tiling for example).
148    // See bug https://bugs.webkit.org/show_bug.cgi?id=47767 where
149    // widget are rendered as disabled when caching is using.
150
151    QGraphicsWebView* webView = new QGraphicsWebView;
152    webView->setHtml(QLatin1String("<body style=\"background-color: white\"><input type=range></input><input type=checkbox></input><input type=radio></input><input type=file></input></body>"));
153
154    QGraphicsView view;
155    view.show();
156    QGraphicsScene* scene = new QGraphicsScene(&view);
157    view.setScene(scene);
158    scene->addItem(webView);
159    view.setGeometry(QRect(0, 0, 500, 500));
160    QWidget *const widget = &view;
161    QTest::qWaitForWindowShown(widget);
162
163    // 1. Reference without tiling.
164    webView->settings()->setAttribute(QWebSettings::TiledBackingStoreEnabled, false);
165    QPixmap referencePixmap(view.size());
166    widget->render(&referencePixmap);
167
168    // 2. With tiling.
169    webView->settings()->setAttribute(QWebSettings::TiledBackingStoreEnabled, true);
170    QPixmap viewWithTiling(view.size());
171    widget->render(&viewWithTiling);
172    QApplication::processEvents();
173    viewWithTiling.fill();
174    widget->render(&viewWithTiling);
175
176    QCOMPARE(referencePixmap.toImage(), viewWithTiling.toImage());
177}
178
179void tst_QGraphicsWebView::microFocusCoordinates()
180{
181    QWebPage* page = new QWebPage;
182    QGraphicsWebView* webView = new QGraphicsWebView;
183    webView->setPage( page );
184    QGraphicsView* view = new QGraphicsView;
185    QGraphicsScene* scene = new QGraphicsScene(view);
186    view->setScene(scene);
187    scene->addItem(webView);
188    view->setGeometry(QRect(0,0,500,500));
189
190    page->mainFrame()->setHtml("<html><body>" \
191        "<input type='text' id='input1' style='font--family: serif' value='' maxlength='20'/><br>" \
192        "<canvas id='canvas1' width='500' height='500'/>" \
193        "<input type='password'/><br>" \
194        "<canvas id='canvas2' width='500' height='500'/>" \
195        "</body></html>");
196
197    page->mainFrame()->setFocus();
198
199    QVariant initialMicroFocus = page->inputMethodQuery(Qt::ImMicroFocus);
200    QVERIFY(initialMicroFocus.isValid());
201
202    page->mainFrame()->scroll(0,300);
203
204    QVariant currentMicroFocus = page->inputMethodQuery(Qt::ImMicroFocus);
205    QVERIFY(currentMicroFocus.isValid());
206
207    QCOMPARE(initialMicroFocus.toRect().translated(QPoint(0,-300)), currentMicroFocus.toRect());
208
209    delete view;
210}
211
212void tst_QGraphicsWebView::focusInputTypes()
213{
214    QWebPage* page = new QWebPage;
215    GraphicsWebView* webView = new GraphicsWebView;
216    webView->setPage( page );
217    QGraphicsView* view = new QGraphicsView;
218    QGraphicsScene* scene = new QGraphicsScene(view);
219    view->setScene(scene);
220    scene->addItem(webView);
221    view->setGeometry(QRect(0,0,500,500));
222    QCoreApplication::processEvents();
223    QUrl url("qrc:///resources/input_types.html");
224    page->mainFrame()->load(url);
225    page->mainFrame()->setFocus();
226
227    QVERIFY(waitForSignal(page, SIGNAL(loadFinished(bool))));
228
229    // 'text' type
230    webView->fireMouseClick(QPointF(20.0, 10.0));
231#if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) || defined(Q_OS_SYMBIAN)
232    QVERIFY(webView->inputMethodHints() & Qt::ImhNoAutoUppercase);
233    QVERIFY(webView->inputMethodHints() & Qt::ImhNoPredictiveText);
234#else
235    QVERIFY(webView->inputMethodHints() == Qt::ImhNone);
236#endif
237
238    // 'password' field
239    webView->fireMouseClick(QPointF(20.0, 60.0));
240    QVERIFY(webView->inputMethodHints() & Qt::ImhHiddenText);
241
242    // 'tel' field
243    webView->fireMouseClick(QPointF(20.0, 110.0));
244    QVERIFY(webView->inputMethodHints() & Qt::ImhDialableCharactersOnly);
245
246    // 'number' field
247    webView->fireMouseClick(QPointF(20.0, 160.0));
248    QVERIFY(webView->inputMethodHints() & Qt::ImhDigitsOnly);
249
250    // 'email' field
251    webView->fireMouseClick(QPointF(20.0, 210.0));
252    QVERIFY(webView->inputMethodHints() & Qt::ImhEmailCharactersOnly);
253
254    // 'url' field
255    webView->fireMouseClick(QPointF(20.0, 260.0));
256    QVERIFY(webView->inputMethodHints() & Qt::ImhUrlCharactersOnly);
257
258    delete webView;
259    delete view;
260}
261
262
263
264QTEST_MAIN(tst_QGraphicsWebView)
265
266#include "tst_qgraphicswebview.moc"
267