[Qt][Wk2] Add an API test for scroll request from javascript
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / qt / tests / qquickwebview / tst_qquickwebview.cpp
1 /*
2     Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
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 "../testwindow.h"
21 #include "../util.h"
22
23 #include <QScopedPointer>
24 #include <QtTest/QtTest>
25 #include <qquickwebpage_p.h>
26 #include <qquickwebview_p.h>
27
28 class tst_QQuickWebView : public QObject {
29     Q_OBJECT
30 public:
31     tst_QQuickWebView();
32
33 private slots:
34     void init();
35     void cleanup();
36
37     void accessPage();
38     void navigationStatusAtStartup();
39     void stopEnabledAfterLoadStarted();
40     void baseUrl();
41     void loadEmptyUrl();
42     void loadEmptyPageViewVisible();
43     void loadEmptyPageViewHidden();
44     void loadNonexistentFileUrl();
45     void backAndForward();
46     void reload();
47     void stop();
48     void loadProgress();
49     void scrollRequest();
50
51     void show();
52
53 private:
54     inline QQuickWebView* webView() const;
55     QScopedPointer<TestWindow> m_window;
56 };
57
58 tst_QQuickWebView::tst_QQuickWebView()
59 {
60     addQtWebProcessToPath();
61     qRegisterMetaType<QQuickWebPage*>("QQuickWebPage*");
62 }
63
64 void tst_QQuickWebView::init()
65 {
66     m_window.reset(new TestWindow(new QQuickWebView()));
67 }
68
69 void tst_QQuickWebView::cleanup()
70 {
71     m_window.reset();
72 }
73
74 inline QQuickWebView* tst_QQuickWebView::webView() const
75 {
76     return static_cast<QQuickWebView*>(m_window->webView.data());
77 }
78
79 void tst_QQuickWebView::accessPage()
80 {
81     QQuickWebPage* const pageDirectAccess = webView()->page();
82
83     QVariant pagePropertyValue = webView()->property("page");
84     QQuickWebPage* const pagePropertyAccess = pagePropertyValue.value<QQuickWebPage*>();
85     QCOMPARE(pagePropertyAccess, pageDirectAccess);
86 }
87
88 void tst_QQuickWebView::navigationStatusAtStartup()
89 {
90     QCOMPARE(webView()->canGoBack(), false);
91
92     QCOMPARE(webView()->canGoForward(), false);
93
94     QCOMPARE(webView()->canStop(), false);
95
96     QCOMPARE(webView()->canReload(), false);
97 }
98
99 class LoadStartedCatcher : public QObject {
100     Q_OBJECT
101 public:
102     LoadStartedCatcher(QQuickWebView* webView)
103         : m_webView(webView)
104     {
105         connect(m_webView, SIGNAL(loadStarted()), this, SLOT(onLoadStarted()));
106     }
107
108 public slots:
109     void onLoadStarted()
110     {
111         QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
112
113         QCOMPARE(m_webView->canStop(), true);
114     }
115
116 signals:
117     void finished();
118
119 private:
120     QQuickWebView* m_webView;
121 };
122
123 void tst_QQuickWebView::stopEnabledAfterLoadStarted()
124 {
125     QCOMPARE(webView()->canStop(), false);
126
127     LoadStartedCatcher catcher(webView());
128     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html")));
129     waitForSignal(&catcher, SIGNAL(finished()));
130
131     QCOMPARE(webView()->canStop(), true);
132
133     waitForSignal(webView(), SIGNAL(loadSucceeded()));
134 }
135
136 void tst_QQuickWebView::baseUrl()
137 {
138     // Test the url is in a well defined state when instanciating the view, but before loading anything.
139     QVERIFY(webView()->url().isEmpty());
140 }
141
142 void tst_QQuickWebView::loadEmptyUrl()
143 {
144     webView()->load(QUrl());
145     webView()->load(QUrl(QLatin1String("")));
146 }
147
148 void tst_QQuickWebView::loadEmptyPageViewVisible()
149 {
150     m_window->show();
151     loadEmptyPageViewHidden();
152 }
153
154 void tst_QQuickWebView::loadEmptyPageViewHidden()
155 {
156     QSignalSpy loadStartedSpy(webView(), SIGNAL(loadStarted()));
157
158     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html")));
159     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
160
161     QCOMPARE(loadStartedSpy.size(), 1);
162 }
163
164 void tst_QQuickWebView::loadNonexistentFileUrl()
165 {
166     QSignalSpy loadFailedSpy(webView(), SIGNAL(loadStarted()));
167
168     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/file_that_does_not_exist.html")));
169     QVERIFY(waitForSignal(webView(), SIGNAL(loadFailed(QQuickWebView::ErrorType, int, QUrl))));
170
171     QCOMPARE(loadFailedSpy.size(), 1);
172 }
173
174 void tst_QQuickWebView::backAndForward()
175 {
176     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html")));
177     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
178
179     QCOMPARE(webView()->url().path(), QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html"));
180
181     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/basic_page2.html")));
182     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
183
184     QCOMPARE(webView()->url().path(), QLatin1String(TESTS_SOURCE_DIR "/html/basic_page2.html"));
185
186     webView()->goBack();
187     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
188
189     QCOMPARE(webView()->url().path(), QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html"));
190
191     webView()->goForward();
192     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
193
194     QCOMPARE(webView()->url().path(), QLatin1String(TESTS_SOURCE_DIR "/html/basic_page2.html"));
195 }
196
197 void tst_QQuickWebView::reload()
198 {
199     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html")));
200     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
201
202     QCOMPARE(webView()->url().path(), QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html"));
203
204     webView()->reload();
205     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
206
207     QCOMPARE(webView()->url().path(), QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html"));
208 }
209
210 void tst_QQuickWebView::stop()
211 {
212     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html")));
213     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
214
215     QCOMPARE(webView()->url().path(), QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html"));
216
217     // FIXME: This test should be fleshed out. Right now it's just here to make sure we don't crash.
218     webView()->stop();
219 }
220
221 void tst_QQuickWebView::loadProgress()
222 {
223     QCOMPARE(webView()->loadProgress(), 0);
224
225     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html")));
226     QSignalSpy loadProgressChangedSpy(webView(), SIGNAL(loadProgressChanged(int)));
227     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
228
229     QVERIFY(loadProgressChangedSpy.count() >= 1);
230
231     QCOMPARE(webView()->loadProgress(), 100);
232 }
233
234 void tst_QQuickWebView::show()
235 {
236     // This should not crash.
237     m_window->show();
238     QTest::qWait(200);
239     m_window->hide();
240 }
241
242 void tst_QQuickWebView::scrollRequest()
243 {
244     webView()->setSize(QSizeF(300, 400));
245
246     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/scroll.html")));
247     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
248
249     // COMPARE with the position requested in the html
250     int y = -50 * webView()->page()->scale();
251     QVERIFY(webView()->page()->pos().y() == y);
252 }
253
254 QTWEBKIT_API_TEST_MAIN(tst_QQuickWebView)
255
256 #include "tst_qquickwebview.moc"
257