e43e665894b733f9b4ec20f2d8794ec294fd456e
[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
50     void show();
51
52 private:
53     inline QQuickWebView* webView() const;
54     QScopedPointer<TestWindow> m_window;
55 };
56
57 tst_QQuickWebView::tst_QQuickWebView()
58 {
59     addQtWebProcessToPath();
60     qRegisterMetaType<QQuickWebPage*>("QQuickWebPage*");
61 }
62
63 void tst_QQuickWebView::init()
64 {
65     m_window.reset(new TestWindow(new QQuickWebView()));
66 }
67
68 void tst_QQuickWebView::cleanup()
69 {
70     m_window.reset();
71 }
72
73 inline QQuickWebView* tst_QQuickWebView::webView() const
74 {
75     return static_cast<QQuickWebView*>(m_window->webView.data());
76 }
77
78 void tst_QQuickWebView::accessPage()
79 {
80     QQuickWebPage* const pageDirectAccess = webView()->page();
81
82     QVariant pagePropertyValue = webView()->property("page");
83     QQuickWebPage* const pagePropertyAccess = pagePropertyValue.value<QQuickWebPage*>();
84     QCOMPARE(pagePropertyAccess, pageDirectAccess);
85 }
86
87 void tst_QQuickWebView::navigationStatusAtStartup()
88 {
89     QCOMPARE(webView()->canGoBack(), false);
90
91     QCOMPARE(webView()->canGoForward(), false);
92
93     QCOMPARE(webView()->canStop(), false);
94
95     QCOMPARE(webView()->canReload(), false);
96 }
97
98 class LoadStartedCatcher : public QObject {
99     Q_OBJECT
100 public:
101     LoadStartedCatcher(QQuickWebView* webView)
102         : m_webView(webView)
103     {
104         connect(m_webView, SIGNAL(loadStarted()), this, SLOT(onLoadStarted()));
105     }
106
107 public slots:
108     void onLoadStarted()
109     {
110         QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
111
112         QCOMPARE(m_webView->canStop(), true);
113     }
114
115 signals:
116     void finished();
117
118 private:
119     QQuickWebView* m_webView;
120 };
121
122 void tst_QQuickWebView::stopEnabledAfterLoadStarted()
123 {
124     QCOMPARE(webView()->canStop(), false);
125
126     LoadStartedCatcher catcher(webView());
127     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html")));
128     waitForSignal(&catcher, SIGNAL(finished()));
129
130     QCOMPARE(webView()->canStop(), true);
131
132     waitForSignal(webView(), SIGNAL(loadSucceeded()));
133 }
134
135 void tst_QQuickWebView::baseUrl()
136 {
137     // Test the url is in a well defined state when instanciating the view, but before loading anything.
138     QVERIFY(webView()->url().isEmpty());
139 }
140
141 void tst_QQuickWebView::loadEmptyUrl()
142 {
143     webView()->load(QUrl());
144     webView()->load(QUrl(QLatin1String("")));
145 }
146
147 void tst_QQuickWebView::loadEmptyPageViewVisible()
148 {
149     m_window->show();
150     loadEmptyPageViewHidden();
151 }
152
153 void tst_QQuickWebView::loadEmptyPageViewHidden()
154 {
155     QSignalSpy loadStartedSpy(webView(), SIGNAL(loadStarted()));
156
157     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html")));
158     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
159
160     QCOMPARE(loadStartedSpy.size(), 1);
161 }
162
163 void tst_QQuickWebView::loadNonexistentFileUrl()
164 {
165     QSignalSpy loadFailedSpy(webView(), SIGNAL(loadStarted()));
166
167     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/file_that_does_not_exist.html")));
168     QVERIFY(waitForSignal(webView(), SIGNAL(loadFailed(QQuickWebView::ErrorType, int, QUrl))));
169
170     QCOMPARE(loadFailedSpy.size(), 1);
171 }
172
173 void tst_QQuickWebView::backAndForward()
174 {
175     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html")));
176     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
177
178     QCOMPARE(webView()->url().path(), QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html"));
179
180     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/basic_page2.html")));
181     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
182
183     QCOMPARE(webView()->url().path(), QLatin1String(TESTS_SOURCE_DIR "/html/basic_page2.html"));
184
185     webView()->goBack();
186     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
187
188     QCOMPARE(webView()->url().path(), QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html"));
189
190     webView()->goForward();
191     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
192
193     QCOMPARE(webView()->url().path(), QLatin1String(TESTS_SOURCE_DIR "/html/basic_page2.html"));
194 }
195
196 void tst_QQuickWebView::reload()
197 {
198     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html")));
199     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
200
201     QCOMPARE(webView()->url().path(), QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html"));
202
203     webView()->reload();
204     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
205
206     QCOMPARE(webView()->url().path(), QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html"));
207 }
208
209 void tst_QQuickWebView::stop()
210 {
211     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html")));
212     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
213
214     QCOMPARE(webView()->url().path(), QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html"));
215
216     // FIXME: This test should be fleshed out. Right now it's just here to make sure we don't crash.
217     webView()->stop();
218 }
219
220 void tst_QQuickWebView::loadProgress()
221 {
222     QCOMPARE(webView()->loadProgress(), 0);
223
224     webView()->load(QUrl::fromLocalFile(QLatin1String(TESTS_SOURCE_DIR "/html/basic_page.html")));
225     QSignalSpy loadProgressChangedSpy(webView(), SIGNAL(loadProgressChanged(int)));
226     QVERIFY(waitForSignal(webView(), SIGNAL(loadSucceeded())));
227
228     QVERIFY(loadProgressChangedSpy.count() >= 1);
229
230     QCOMPARE(webView()->loadProgress(), 100);
231 }
232
233 void tst_QQuickWebView::show()
234 {
235     // This should not crash.
236     m_window->show();
237     QTest::qWait(200);
238     m_window->hide();
239 }
240
241 QTWEBKIT_API_TEST_MAIN(tst_QQuickWebView)
242
243 #include "tst_qquickwebview.moc"
244