Falkon Develop
Cross-platform Qt-based web browser
qmluserscriptapitest.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
19#include "autotests.h"
20#include "mainapplication.h"
21#include <QWebEngineProfile>
22#include <QWebEngineScript>
23#include <QWebEngineScriptCollection>
26
27void QmlUserScriptApiTest::initTestCase()
28{
29}
30
31void QmlUserScriptApiTest::cleanupTestCase()
32{
33}
34
35void QmlUserScriptApiTest::testCount()
36{
37 int count = m_testHelper.evaluate(QSL("Falkon.UserScripts.count")).toInt();
38 QCOMPARE(count, mApp->webProfile()->scripts()->count());
39}
40
41void QmlUserScriptApiTest::testSize()
42{
43 int size = m_testHelper.evaluate(QSL("Falkon.UserScripts.size")).toInt();
44 QCOMPARE(size, mApp->webProfile()->scripts()->count());
45}
46
47void QmlUserScriptApiTest::testEmpty()
48{
49 bool empty = m_testHelper.evaluate(QSL("Falkon.UserScripts.empty")).toBool();
50 QCOMPARE(empty, mApp->webProfile()->scripts()->isEmpty());
51}
52
53void QmlUserScriptApiTest::testContains()
54{
55 QWebEngineScript script = mApp->webProfile()->scripts()->toList().at(0);
56 QObject *object = m_testHelper.evaluateQObject(QSL("Falkon.UserScripts"));
57 auto *userScripts = dynamic_cast<QmlUserScripts*>(object);
58 QVERIFY(userScripts);
59 auto *userScript = new QmlUserScript();
60 userScript->setWebEngineScript(script);
61 bool contains = userScripts->contains(userScript);
62 QCOMPARE(contains, true);
63}
64
65void QmlUserScriptApiTest::testFind()
66{
67 QWebEngineScript script = mApp->webProfile()->scripts()->toList().at(0);
68 QObject *object = m_testHelper.evaluateQObject(QSL("Falkon.UserScripts"));
69 auto *userScripts = dynamic_cast<QmlUserScripts*>(object);
70 QVERIFY(userScripts);
71 QObject *scriptFound = userScripts->findScript(script.name());
72 QVERIFY(scriptFound);
73 QCOMPARE(scriptFound->property("name").toString(), script.name());
74}
75
76void QmlUserScriptApiTest::testInsertRemove()
77{
78 int initialCount = m_testHelper.evaluate(QSL("Falkon.UserScripts.count")).toInt();
79 QObject *object = m_testHelper.evaluateQObject(QSL("Falkon.UserScripts"));
80 auto *userScripts = dynamic_cast<QmlUserScripts*>(object);
81 QVERIFY(userScripts);
82 auto *userScript = new QmlUserScript();
83 userScript->setProperty("name", QSL("Hello World"));
84 userScript->setProperty("sourceCode", QL1S("(function() {"
85 " alert('Hello World')"
86 "})()"));
87 userScripts->insert(userScript);
88 int finalCount = m_testHelper.evaluate(QSL("Falkon.UserScripts.count")).toInt();
89 QCOMPARE(finalCount, initialCount + 1);
90
91 userScripts->remove(userScript);
92
93 int ultimateCount = m_testHelper.evaluate(QSL("Falkon.UserScripts.count")).toInt();
94 QCOMPARE(ultimateCount, initialCount);
95}
96
#define FALKONTEST_MAIN(Test)
Definition: autotests.h:23
QObject * evaluateQObject(const QString &source)
QJSValue evaluate(const QString &source)
The class exposing QWebEngineScript to QML.
Definition: qmluserscript.h:30
The class exposing QWebEngineScriptCollection to QML.
#define mApp
#define QL1S(x)
Definition: qzcommon.h:44
#define QSL(x)
Definition: qzcommon.h:40