1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
/*
Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "testwindow.h"
#include "util.h"
#include <QScopedPointer>
#include <QtQml/QQmlEngine>
#include <QtTest/QtTest>
#include <QtWebViewQuick/private/qquickwebview_p.h>
#include <QtCore/qfile.h>
#include <QtCore/qstandardpaths.h>
#include <QtWebView/qtwebviewfunctions.h>
QUrl getTestFilePath(const QString &testFile)
{
const QString tempTestFile = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/" + testFile;
const bool exists = QFile::exists(tempTestFile);
if (exists)
return QUrl::fromLocalFile(tempTestFile);
QFile tf(QString(":/") + testFile);
const bool copied = tf.copy(tempTestFile);
return QUrl::fromLocalFile(copied ? tempTestFile : testFile);
}
class tst_QQuickWebView : public QObject {
Q_OBJECT
public:
tst_QQuickWebView();
private Q_SLOTS:
void init();
void cleanup();
void navigationStatusAtStartup();
void stopEnabledAfterLoadStarted();
void baseUrl();
void loadEmptyUrl();
void loadEmptyPageViewVisible();
void loadEmptyPageViewHidden();
void loadNonexistentFileUrl();
void backAndForward();
void reload();
void stop();
void loadProgress();
void show();
void showWebView();
void removeFromCanvas();
void multipleWebViewWindows();
void multipleWebViews();
void titleUpdate();
void changeUserAgent();
void setAndDeleteCookies();
private:
inline QQuickWebView *newWebView();
inline QQuickWebView *webView() const;
void runJavaScript(const QString &script);
QScopedPointer<TestWindow> m_window;
QScopedPointer<QQmlComponent> m_component;
QQmlEngine *engine = nullptr;
};
tst_QQuickWebView::tst_QQuickWebView()
{
engine = new QQmlEngine(this);
m_component.reset(new QQmlComponent(engine, this));
m_component->setData(QByteArrayLiteral("import QtQuick 2.0\n"
"import QtWebView 1.1\n"
"WebView {}")
, QUrl());
}
QQuickWebView *tst_QQuickWebView::newWebView()
{
QObject *viewInstance = m_component->create();
QQuickWebView *webView = qobject_cast<QQuickWebView*>(viewInstance);
return webView;
}
void tst_QQuickWebView::init()
{
QtWebView::initialize();
m_window.reset(new TestWindow(newWebView()));
}
void tst_QQuickWebView::cleanup()
{
m_window.reset();
}
inline QQuickWebView *tst_QQuickWebView::webView() const
{
return static_cast<QQuickWebView*>(m_window->webView.data());
}
void tst_QQuickWebView::runJavaScript(const QString &script)
{
webView()->runJavaScript(script);
}
void tst_QQuickWebView::navigationStatusAtStartup()
{
QCOMPARE(webView()->canGoBack(), false);
QCOMPARE(webView()->canGoForward(), false);
QCOMPARE(webView()->isLoading(), false);
}
void tst_QQuickWebView::stopEnabledAfterLoadStarted()
{
QCOMPARE(webView()->isLoading(), false);
LoadStartedCatcher catcher(webView());
webView()->setUrl(getTestFilePath("basic_page.html"));
waitForSignal(&catcher, SIGNAL(finished()));
QCOMPARE(webView()->isLoading(), true);
QVERIFY(waitForLoadSucceeded(webView()));
}
void tst_QQuickWebView::baseUrl()
{
// Test the url is in a well defined state when instanciating the view, but before loading anything.
QVERIFY(webView()->url().isEmpty());
}
void tst_QQuickWebView::loadEmptyUrl()
{
webView()->setUrl(QUrl());
webView()->setUrl(QUrl(QLatin1String("")));
}
void tst_QQuickWebView::loadEmptyPageViewVisible()
{
m_window->show();
loadEmptyPageViewHidden();
}
void tst_QQuickWebView::loadEmptyPageViewHidden()
{
QSignalSpy loadSpy(webView(), SIGNAL(loadingChanged(QQuickWebViewLoadRequest*)));
webView()->setUrl(getTestFilePath("basic_page.html"));
QVERIFY(waitForLoadSucceeded(webView()));
QCOMPARE(loadSpy.size(), 2);
}
void tst_QQuickWebView::loadNonexistentFileUrl()
{
QSignalSpy loadSpy(webView(), SIGNAL(loadingChanged(QQuickWebViewLoadRequest*)));
webView()->setUrl(getTestFilePath("file_that_does_not_exist.html"));
QVERIFY(waitForLoadFailed(webView()));
QCOMPARE(loadSpy.size(), 2);
}
void tst_QQuickWebView::backAndForward()
{
const QUrl basicPage = getTestFilePath("basic_page.html");
const QUrl basicPage2 = getTestFilePath("basic_page2.html");
webView()->setUrl(basicPage);
QVERIFY(waitForLoadSucceeded(webView()));
QCOMPARE(webView()->url(), basicPage);
webView()->setUrl(basicPage2);
QVERIFY(waitForLoadSucceeded(webView()));
QCOMPARE(webView()->url(), basicPage2);
webView()->goBack();
QVERIFY(waitForLoadSucceeded(webView()));
QCOMPARE(webView()->url(), basicPage);
webView()->goForward();
QVERIFY(waitForLoadSucceeded(webView()));
QCOMPARE(webView()->url(), basicPage2);
}
void tst_QQuickWebView::reload()
{
webView()->setUrl(getTestFilePath("basic_page.html"));
QVERIFY(waitForLoadSucceeded(webView()));
QCOMPARE(webView()->url(), getTestFilePath("basic_page.html"));
webView()->reload();
QVERIFY(waitForLoadSucceeded(webView()));
QCOMPARE(webView()->url(), getTestFilePath("basic_page.html"));
}
void tst_QQuickWebView::stop()
{
webView()->setUrl(QUrl(getTestFilePath("basic_page.html")));
QVERIFY(waitForLoadSucceeded(webView()));
QCOMPARE(webView()->url(), getTestFilePath("basic_page.html"));
webView()->stop();
}
void tst_QQuickWebView::loadProgress()
{
QCOMPARE(webView()->loadProgress(), 0);
webView()->setUrl(getTestFilePath("basic_page.html"));
QSignalSpy loadProgressChangedSpy(webView(), SIGNAL(loadProgressChanged()));
QVERIFY(waitForLoadSucceeded(webView()));
QVERIFY(loadProgressChangedSpy.size() >= 1);
QCOMPARE(webView()->loadProgress(), 100);
}
void tst_QQuickWebView::show()
{
// This should not crash.
m_window->show();
QTest::qWait(200);
m_window->hide();
}
void tst_QQuickWebView::showWebView()
{
webView()->setUrl(getTestFilePath("direct-image-compositing.html"));
QVERIFY(waitForLoadSucceeded(webView()));
m_window->show();
// This should not crash.
webView()->setVisible(true);
QTest::qWait(200);
webView()->setVisible(false);
QTest::qWait(200);
}
void tst_QQuickWebView::removeFromCanvas()
{
showWebView();
// This should not crash.
QQuickItem *parent = webView()->parentItem();
QQuickItem noCanvasItem;
webView()->setParentItem(&noCanvasItem);
QTest::qWait(200);
webView()->setParentItem(parent);
webView()->setVisible(true);
QTest::qWait(200);
}
void tst_QQuickWebView::multipleWebViewWindows()
{
showWebView();
// This should not crash.
QQuickWebView *webView1 = newWebView();
QScopedPointer<TestWindow> window1(new TestWindow(webView1));
QQuickWebView *webView2 = newWebView();
QScopedPointer<TestWindow> window2(new TestWindow(webView2));
webView1->setUrl(getTestFilePath("scroll.html"));
QVERIFY(waitForLoadSucceeded(webView1));
window1->show();
webView1->setVisible(true);
webView2->setUrl(getTestFilePath("basic_page.html"));
QVERIFY(waitForLoadSucceeded(webView2));
window2->show();
webView2->setVisible(true);
QTest::qWait(200);
}
void tst_QQuickWebView::multipleWebViews()
{
showWebView();
// This should not crash.
QScopedPointer<QQuickWebView> webView1(newWebView());
webView1->setParentItem(m_window->contentItem());
QScopedPointer<QQuickWebView> webView2(newWebView());
webView2->setParentItem(m_window->contentItem());
webView1->setSize(QSizeF(300, 400));
webView1->setUrl(getTestFilePath("scroll.html"));
QVERIFY(waitForLoadSucceeded(webView1.data()));
webView1->setVisible(true);
webView2->setSize(QSizeF(300, 400));
webView2->setUrl(getTestFilePath("basic_page.html"));
QVERIFY(waitForLoadSucceeded(webView2.data()));
webView2->setVisible(true);
QTest::qWait(200);
}
void tst_QQuickWebView::titleUpdate()
{
QSignalSpy titleSpy(webView(), SIGNAL(titleChanged()));
// Load page with no title
webView()->setUrl(getTestFilePath("basic_page2.html"));
QVERIFY(waitForLoadSucceeded(webView()));
#ifdef QT_WEBVIEW_WEBENGINE_BACKEND
// webengine emits titleChanged even if there is no title
// QTBUG-94151
QCOMPARE(titleSpy.size(), 1);
#else
QCOMPARE(titleSpy.size(), 0);
#endif
titleSpy.clear();
// No titleChanged signal for failed load
webView()->setUrl(getTestFilePath("file_that_does_not_exist.html"));
QVERIFY(waitForLoadFailed(webView()));
QCOMPARE(titleSpy.size(), 0);
}
void tst_QQuickWebView::changeUserAgent()
{
QQmlComponent userAgentWebView(engine, this);
userAgentWebView.setData(QByteArrayLiteral("import QtQuick 2.0\n"
"import QtWebView 1.14\n"
"WebView {\n"
"httpUserAgent: \"dummy\"\n"
"}"),
QUrl());
QObject *viewInstance = userAgentWebView.create();
QQuickWebView *webView = qobject_cast<QQuickWebView *>(viewInstance);
QCOMPARE(webView->httpUserAgent(), "dummy");
}
void tst_QQuickWebView::setAndDeleteCookies()
{
QSignalSpy cookieAddedSpy(webView(), SIGNAL(cookieAdded(const QString &, const QString &)));
QSignalSpy cookieRemovedSpy(webView(), SIGNAL(cookieRemoved(const QString &, const QString &)));
#ifdef QT_WEBVIEW_WEBENGINE_BACKEND
webView()->setUrl(QUrl("qrc:///cookies.html"));
QVERIFY(waitForLoadSucceeded(webView()));
QTRY_COMPARE(cookieAddedSpy.size(), 2);
webView()->deleteAllCookies();
QTRY_COMPARE(cookieRemovedSpy.size(), 2);
cookieAddedSpy.clear();
cookieRemovedSpy.clear();
#endif
Cookie::List cookies { {".example.com", "TestCookie", "testValue"},
{".example2.com", "TestCookie2", "testValue2"},
{".example3.com", "TestCookie3", "testValue3"} };
for (const auto &cookie : cookies)
webView()->setCookie(cookie.domain, cookie.name, cookie.value);
QTRY_COMPARE(cookieAddedSpy.size(), cookies.size());
QVERIFY(Cookie::testSignalValues(cookies, cookieAddedSpy));
auto removedCookie = cookies.takeLast();
webView()->deleteCookie(removedCookie.domain, removedCookie.name);
QTRY_COMPARE(cookieRemovedSpy.size(), 1);
{
const auto &first = cookieRemovedSpy.first();
Cookie::SigArg sigArg{ first.at(0).toString(), first.at(1).toString() };
QCOMPARE(removedCookie, sigArg);
}
// deleting a cookie using a name that has not been set
webView()->deleteCookie(".example.com", "NewCookieName");
QTRY_COMPARE(cookieRemovedSpy.size(), 1);
// deleting a cookie using a domain that has not been set
webView()->deleteCookie(".new.domain.com", "TestCookie2");
QTRY_COMPARE(cookieRemovedSpy.size(), 1);
webView()->deleteAllCookies();
#ifdef Q_OS_ANDROID
QEXPECT_FAIL("", "Notification for deleteAllCookies() is not implemented on Android, yet!", Continue);
#endif
QTRY_COMPARE(cookieRemovedSpy.size(), 3);
}
QTEST_MAIN(tst_QQuickWebView)
#include "tst_qquickwebview.moc"
|