mirror of
https://invent.kde.org/network/falkon.git
synced 2024-11-11 01:22:10 +01:00
Added autotests for UserScript API
This commit is contained in:
parent
d01609cb05
commit
68ba798597
|
@ -61,4 +61,5 @@ falkon_qml_tests(
|
||||||
qmlclipboardapitest
|
qmlclipboardapitest
|
||||||
qmltabsapitest
|
qmltabsapitest
|
||||||
qmlwindowsapitest
|
qmlwindowsapitest
|
||||||
|
qmluserscriptapitest
|
||||||
)
|
)
|
||||||
|
|
104
autotests/qml/qmluserscriptapitest.cpp
Normal file
104
autotests/qml/qmluserscriptapitest.cpp
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
/* ============================================================
|
||||||
|
* Falkon - Qt web browser
|
||||||
|
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program 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 General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* ============================================================ */
|
||||||
|
#include "qmluserscriptapitest.h"
|
||||||
|
#include "autotests.h"
|
||||||
|
#include "qmltesthelper.h"
|
||||||
|
#include "mainapplication.h"
|
||||||
|
#include <QWebEngineProfile>
|
||||||
|
#include <QWebEngineScript>
|
||||||
|
#include <QWebEngineScriptCollection>
|
||||||
|
#include "qml/api/userscript/qmluserscript.h"
|
||||||
|
#include "qml/api/userscript/qmluserscripts.h"
|
||||||
|
|
||||||
|
void QmlUserScriptApiTest::initTestCase()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlUserScriptApiTest::cleanupTestCase()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlUserScriptApiTest::testCount()
|
||||||
|
{
|
||||||
|
QmlTestHelper qmlTest;
|
||||||
|
int count = qmlTest.evaluate("Falkon.UserScripts.count").toInt();
|
||||||
|
QCOMPARE(count, mApp->webProfile()->scripts()->count());
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlUserScriptApiTest::testSize()
|
||||||
|
{
|
||||||
|
QmlTestHelper qmlTest;
|
||||||
|
int size = qmlTest.evaluate("Falkon.UserScripts.size").toInt();
|
||||||
|
QCOMPARE(size, mApp->webProfile()->scripts()->size());
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlUserScriptApiTest::testEmpty()
|
||||||
|
{
|
||||||
|
QmlTestHelper qmlTest;
|
||||||
|
bool empty = qmlTest.evaluate("Falkon.UserScripts.empty").toBool();
|
||||||
|
QCOMPARE(empty, mApp->webProfile()->scripts()->isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlUserScriptApiTest::testContains()
|
||||||
|
{
|
||||||
|
QmlTestHelper qmlTest;
|
||||||
|
QWebEngineScript script = mApp->webProfile()->scripts()->toList().at(0);
|
||||||
|
QObject *object = qmlTest.evaluateQObject("Falkon.UserScripts");
|
||||||
|
QmlUserScripts *userScripts = dynamic_cast<QmlUserScripts*>(object);
|
||||||
|
QVERIFY(userScripts);
|
||||||
|
QmlUserScript *userScript = new QmlUserScript();
|
||||||
|
userScript->setWebEngineScript(script);
|
||||||
|
bool contains = userScripts->contains(userScript);
|
||||||
|
QCOMPARE(contains, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlUserScriptApiTest::testFind()
|
||||||
|
{
|
||||||
|
QmlTestHelper qmlTest;
|
||||||
|
QWebEngineScript script = mApp->webProfile()->scripts()->toList().at(0);
|
||||||
|
QObject *object = qmlTest.evaluateQObject("Falkon.UserScripts");
|
||||||
|
QmlUserScripts *userScripts = dynamic_cast<QmlUserScripts*>(object);
|
||||||
|
QVERIFY(userScripts);
|
||||||
|
QObject *scriptFound = userScripts->findScript(script.name());
|
||||||
|
QVERIFY(scriptFound);
|
||||||
|
QCOMPARE(scriptFound->property("name").toString(), script.name());
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlUserScriptApiTest::testInsertRemove()
|
||||||
|
{
|
||||||
|
QmlTestHelper qmlTest;
|
||||||
|
int initialCount = qmlTest.evaluate("Falkon.UserScripts.count").toInt();
|
||||||
|
QObject *object = qmlTest.evaluateQObject("Falkon.UserScripts");
|
||||||
|
QmlUserScripts *userScripts = dynamic_cast<QmlUserScripts*>(object);
|
||||||
|
QVERIFY(userScripts);
|
||||||
|
QmlUserScript *userScript = new QmlUserScript();
|
||||||
|
userScript->setProperty("name", "Hello World");
|
||||||
|
userScript->setProperty("sourceCode", "(function() {"
|
||||||
|
" alert('Hello World')"
|
||||||
|
"})()");
|
||||||
|
userScripts->insert(userScript);
|
||||||
|
int finalCount = qmlTest.evaluate("Falkon.UserScripts.count").toInt();
|
||||||
|
QCOMPARE(finalCount, initialCount + 1);
|
||||||
|
|
||||||
|
userScripts->remove(userScript);
|
||||||
|
|
||||||
|
int ultimateCount = qmlTest.evaluate("Falkon.UserScripts.count").toInt();
|
||||||
|
QCOMPARE(ultimateCount, initialCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
FALKONTEST_MAIN(QmlUserScriptApiTest)
|
36
autotests/qml/qmluserscriptapitest.h
Normal file
36
autotests/qml/qmluserscriptapitest.h
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
/* ============================================================
|
||||||
|
* Falkon - Qt web browser
|
||||||
|
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program 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 General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* ============================================================ */
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
class QmlUserScriptApiTest : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private Q_SLOTS:
|
||||||
|
void initTestCase();
|
||||||
|
void cleanupTestCase();
|
||||||
|
|
||||||
|
void testCount();
|
||||||
|
void testSize();
|
||||||
|
void testEmpty();
|
||||||
|
void testContains();
|
||||||
|
void testFind();
|
||||||
|
void testInsertRemove();
|
||||||
|
};
|
|
@ -17,10 +17,12 @@
|
||||||
* ============================================================ */
|
* ============================================================ */
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "qzcommon.h"
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QWebEngineScript>
|
#include <QWebEngineScript>
|
||||||
|
|
||||||
class QmlUserScript : public QObject
|
class FALKON_EXPORT QmlUserScript : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY(bool null READ null CONSTANT)
|
Q_PROPERTY(bool null READ null CONSTANT)
|
||||||
|
|
|
@ -61,7 +61,7 @@ bool QmlUserScripts::contains(QObject *object) const
|
||||||
return mApp->webProfile()->scripts()->contains(webEngineScript);
|
return mApp->webProfile()->scripts()->contains(webEngineScript);
|
||||||
}
|
}
|
||||||
|
|
||||||
QmlUserScript *QmlUserScripts::findScript(const QString &name) const
|
QObject *QmlUserScripts::findScript(const QString &name) const
|
||||||
{
|
{
|
||||||
QWebEngineScript webEngineScript = mApp->webProfile()->scripts()->findScript(name);
|
QWebEngineScript webEngineScript = mApp->webProfile()->scripts()->findScript(name);
|
||||||
QmlUserScript *qmlUserScript = new QmlUserScript();
|
QmlUserScript *qmlUserScript = new QmlUserScript();
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
#include "qmluserscript.h"
|
#include "qmluserscript.h"
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
class QmlUserScripts : public QObject
|
class FALKON_EXPORT QmlUserScripts : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY(int count READ count CONSTANT)
|
Q_PROPERTY(int count READ count CONSTANT)
|
||||||
|
@ -29,7 +29,7 @@ class QmlUserScripts : public QObject
|
||||||
public:
|
public:
|
||||||
explicit QmlUserScripts(QObject *parent = nullptr);
|
explicit QmlUserScripts(QObject *parent = nullptr);
|
||||||
Q_INVOKABLE bool contains(QObject *object) const;
|
Q_INVOKABLE bool contains(QObject *object) const;
|
||||||
Q_INVOKABLE QmlUserScript *findScript(const QString &name) const;
|
Q_INVOKABLE QObject *findScript(const QString &name) const;
|
||||||
Q_INVOKABLE QList<QObject *> findScripts(const QString &name) const;
|
Q_INVOKABLE QList<QObject *> findScripts(const QString &name) const;
|
||||||
Q_INVOKABLE void insert(QObject *object) const;
|
Q_INVOKABLE void insert(QObject *object) const;
|
||||||
Q_INVOKABLE void insert(const QList<QObject*> &list) const;
|
Q_INVOKABLE void insert(const QList<QObject*> &list) const;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user