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 |
|
28 class tst_QGraphicsWebView : public QObject |
|
29 { |
|
30 Q_OBJECT |
|
31 |
|
32 private slots: |
|
33 void qgraphicswebview(); |
|
34 void crashOnViewlessWebPages(); |
|
35 void microFocusCoordinates(); |
|
36 void focusInputTypes(); |
|
37 void crashOnSetScaleBeforeSetUrl(); |
|
38 }; |
|
39 |
|
40 void tst_QGraphicsWebView::qgraphicswebview() |
|
41 { |
|
42 QGraphicsWebView item; |
|
43 item.url(); |
|
44 item.title(); |
|
45 item.icon(); |
|
46 item.zoomFactor(); |
|
47 item.history(); |
|
48 item.settings(); |
|
49 item.page(); |
|
50 item.setPage(0); |
|
51 item.page(); |
|
52 item.setUrl(QUrl()); |
|
53 item.setZoomFactor(0); |
|
54 item.load(QUrl()); |
|
55 item.setHtml(QString()); |
|
56 item.setContent(QByteArray()); |
|
57 item.isModified(); |
|
58 } |
|
59 |
|
60 class WebPage : public QWebPage |
|
61 { |
|
62 Q_OBJECT |
|
63 |
|
64 public: |
|
65 WebPage(QObject* parent = 0): QWebPage(parent) |
|
66 { |
|
67 } |
|
68 |
|
69 QGraphicsWebView* webView; |
|
70 |
|
71 private slots: |
|
72 // Force a webview deletion during the load. |
|
73 // It should not cause WebPage to crash due to |
|
74 // it accessing invalid pageClient pointer. |
|
75 void aborting() |
|
76 { |
|
77 delete webView; |
|
78 } |
|
79 }; |
|
80 |
|
81 class GraphicsWebView : public QGraphicsWebView |
|
82 { |
|
83 Q_OBJECT |
|
84 |
|
85 public: |
|
86 GraphicsWebView(QGraphicsItem* parent = 0): QGraphicsWebView(parent) |
|
87 { |
|
88 } |
|
89 |
|
90 void fireMouseClick(QPointF point) { |
|
91 QGraphicsSceneMouseEvent presEv(QEvent::GraphicsSceneMousePress); |
|
92 presEv.setPos(point); |
|
93 presEv.setButton(Qt::LeftButton); |
|
94 presEv.setButtons(Qt::LeftButton); |
|
95 QGraphicsSceneMouseEvent relEv(QEvent::GraphicsSceneMouseRelease); |
|
96 relEv.setPos(point); |
|
97 relEv.setButton(Qt::LeftButton); |
|
98 relEv.setButtons(Qt::LeftButton); |
|
99 QGraphicsWebView::sceneEvent(&presEv); |
|
100 QGraphicsWebView::sceneEvent(&relEv); |
|
101 } |
|
102 }; |
|
103 |
|
104 void tst_QGraphicsWebView::crashOnViewlessWebPages() |
|
105 { |
|
106 QGraphicsScene scene; |
|
107 QGraphicsView view(&scene); |
|
108 |
|
109 QGraphicsWebView* webView = new QGraphicsWebView; |
|
110 WebPage* page = new WebPage; |
|
111 webView->setPage(page); |
|
112 page->webView = webView; |
|
113 scene.addItem(webView); |
|
114 |
|
115 view.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
|
116 view.resize(600, 480); |
|
117 webView->resize(view.geometry().size()); |
|
118 |
|
119 QCoreApplication::processEvents(); |
|
120 view.show(); |
|
121 |
|
122 // Resizing the page will resize and layout the empty "about:blank" |
|
123 // page, so we first connect the signal afterward. |
|
124 connect(page->mainFrame(), SIGNAL(initialLayoutCompleted()), page, SLOT(aborting())); |
|
125 |
|
126 page->mainFrame()->setHtml(QString("data:text/html," |
|
127 "<frameset cols=\"25%,75%\">" |
|
128 "<frame src=\"data:text/html,foo \">" |
|
129 "<frame src=\"data:text/html,bar\">" |
|
130 "</frameset>")); |
|
131 |
|
132 QVERIFY(waitForSignal(page, SIGNAL(loadFinished(bool)))); |
|
133 delete page; |
|
134 } |
|
135 |
|
136 void tst_QGraphicsWebView::crashOnSetScaleBeforeSetUrl() |
|
137 { |
|
138 QGraphicsWebView* webView = new QGraphicsWebView; |
|
139 webView->setScale(2.0); |
|
140 delete webView; |
|
141 } |
|
142 |
|
143 void tst_QGraphicsWebView::microFocusCoordinates() |
|
144 { |
|
145 QWebPage* page = new QWebPage; |
|
146 QGraphicsWebView* webView = new QGraphicsWebView; |
|
147 webView->setPage( page ); |
|
148 QGraphicsView* view = new QGraphicsView; |
|
149 QGraphicsScene* scene = new QGraphicsScene(view); |
|
150 view->setScene(scene); |
|
151 scene->addItem(webView); |
|
152 view->setGeometry(QRect(0,0,500,500)); |
|
153 |
|
154 page->mainFrame()->setHtml("<html><body>" \ |
|
155 "<input type='text' id='input1' style='font--family: serif' value='' maxlength='20'/><br>" \ |
|
156 "<canvas id='canvas1' width='500' height='500'/>" \ |
|
157 "<input type='password'/><br>" \ |
|
158 "<canvas id='canvas2' width='500' height='500'/>" \ |
|
159 "</body></html>"); |
|
160 |
|
161 page->mainFrame()->setFocus(); |
|
162 |
|
163 QVariant initialMicroFocus = page->inputMethodQuery(Qt::ImMicroFocus); |
|
164 QVERIFY(initialMicroFocus.isValid()); |
|
165 |
|
166 page->mainFrame()->scroll(0,300); |
|
167 |
|
168 QVariant currentMicroFocus = page->inputMethodQuery(Qt::ImMicroFocus); |
|
169 QVERIFY(currentMicroFocus.isValid()); |
|
170 |
|
171 QCOMPARE(initialMicroFocus.toRect().translated(QPoint(0,-300)), currentMicroFocus.toRect()); |
|
172 |
|
173 delete view; |
|
174 } |
|
175 |
|
176 void tst_QGraphicsWebView::focusInputTypes() |
|
177 { |
|
178 QWebPage* page = new QWebPage; |
|
179 GraphicsWebView* webView = new GraphicsWebView; |
|
180 webView->setPage( page ); |
|
181 QGraphicsView* view = new QGraphicsView; |
|
182 QGraphicsScene* scene = new QGraphicsScene(view); |
|
183 view->setScene(scene); |
|
184 scene->addItem(webView); |
|
185 view->setGeometry(QRect(0,0,500,500)); |
|
186 QCoreApplication::processEvents(); |
|
187 QUrl url("qrc:///resources/input_types.html"); |
|
188 page->mainFrame()->load(url); |
|
189 page->mainFrame()->setFocus(); |
|
190 |
|
191 QVERIFY(waitForSignal(page, SIGNAL(loadFinished(bool)))); |
|
192 |
|
193 // 'text' type |
|
194 webView->fireMouseClick(QPointF(20.0, 10.0)); |
|
195 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) || defined(Q_OS_SYMBIAN) |
|
196 QVERIFY(webView->inputMethodHints() & Qt::ImhNoAutoUppercase); |
|
197 QVERIFY(webView->inputMethodHints() & Qt::ImhNoPredictiveText); |
|
198 #else |
|
199 QVERIFY(webView->inputMethodHints() == Qt::ImhNone); |
|
200 #endif |
|
201 |
|
202 // 'password' field |
|
203 webView->fireMouseClick(QPointF(20.0, 60.0)); |
|
204 QVERIFY(webView->inputMethodHints() & Qt::ImhHiddenText); |
|
205 |
|
206 // 'tel' field |
|
207 webView->fireMouseClick(QPointF(20.0, 110.0)); |
|
208 QVERIFY(webView->inputMethodHints() & Qt::ImhDialableCharactersOnly); |
|
209 |
|
210 // 'number' field |
|
211 webView->fireMouseClick(QPointF(20.0, 160.0)); |
|
212 QVERIFY(webView->inputMethodHints() & Qt::ImhDigitsOnly); |
|
213 |
|
214 // 'email' field |
|
215 webView->fireMouseClick(QPointF(20.0, 210.0)); |
|
216 QVERIFY(webView->inputMethodHints() & Qt::ImhEmailCharactersOnly); |
|
217 |
|
218 // 'url' field |
|
219 webView->fireMouseClick(QPointF(20.0, 260.0)); |
|
220 QVERIFY(webView->inputMethodHints() & Qt::ImhUrlCharactersOnly); |
|
221 |
|
222 delete webView; |
|
223 delete view; |
|
224 } |
|
225 |
|
226 |
|
227 |
|
228 QTEST_MAIN(tst_QGraphicsWebView) |
|
229 |
|
230 #include "tst_qgraphicswebview.moc" |
|