Changeset View
Changeset View
Standalone View
Standalone View
autotests/qml/qmluserscriptapitest.cpp
- This file was added.
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 | * ============================================================ */ | ||||
18 | #include "qmluserscriptapitest.h" | ||||
19 | #include "autotests.h" | ||||
20 | #include "qmltesthelper.h" | ||||
21 | #include "mainapplication.h" | ||||
22 | #include <QWebEngineProfile> | ||||
23 | #include <QWebEngineScript> | ||||
24 | #include <QWebEngineScriptCollection> | ||||
25 | #include "qml/api/userscript/qmluserscript.h" | ||||
26 | #include "qml/api/userscript/qmluserscripts.h" | ||||
27 | | ||||
28 | void QmlUserScriptApiTest::initTestCase() | ||||
29 | { | ||||
30 | } | ||||
31 | | ||||
32 | void QmlUserScriptApiTest::cleanupTestCase() | ||||
33 | { | ||||
34 | } | ||||
35 | | ||||
36 | void QmlUserScriptApiTest::testCount() | ||||
37 | { | ||||
38 | int count = m_testHelper.evaluate("Falkon.UserScripts.count").toInt(); | ||||
39 | QCOMPARE(count, mApp->webProfile()->scripts()->count()); | ||||
40 | } | ||||
41 | | ||||
42 | void QmlUserScriptApiTest::testSize() | ||||
43 | { | ||||
44 | int size = m_testHelper.evaluate("Falkon.UserScripts.size").toInt(); | ||||
45 | QCOMPARE(size, mApp->webProfile()->scripts()->size()); | ||||
46 | } | ||||
47 | | ||||
48 | void QmlUserScriptApiTest::testEmpty() | ||||
49 | { | ||||
50 | bool empty = m_testHelper.evaluate("Falkon.UserScripts.empty").toBool(); | ||||
51 | QCOMPARE(empty, mApp->webProfile()->scripts()->isEmpty()); | ||||
52 | } | ||||
53 | | ||||
54 | void QmlUserScriptApiTest::testContains() | ||||
55 | { | ||||
56 | QWebEngineScript script = mApp->webProfile()->scripts()->toList().at(0); | ||||
57 | QObject *object = m_testHelper.evaluateQObject("Falkon.UserScripts"); | ||||
58 | QmlUserScripts *userScripts = dynamic_cast<QmlUserScripts*>(object); | ||||
59 | QVERIFY(userScripts); | ||||
60 | QmlUserScript *userScript = new QmlUserScript(); | ||||
61 | userScript->setWebEngineScript(script); | ||||
62 | bool contains = userScripts->contains(userScript); | ||||
63 | QCOMPARE(contains, true); | ||||
64 | } | ||||
65 | | ||||
66 | void QmlUserScriptApiTest::testFind() | ||||
67 | { | ||||
68 | QWebEngineScript script = mApp->webProfile()->scripts()->toList().at(0); | ||||
69 | QObject *object = m_testHelper.evaluateQObject("Falkon.UserScripts"); | ||||
70 | QmlUserScripts *userScripts = dynamic_cast<QmlUserScripts*>(object); | ||||
71 | QVERIFY(userScripts); | ||||
72 | QObject *scriptFound = userScripts->findScript(script.name()); | ||||
73 | QVERIFY(scriptFound); | ||||
74 | QCOMPARE(scriptFound->property("name").toString(), script.name()); | ||||
75 | } | ||||
76 | | ||||
77 | void QmlUserScriptApiTest::testInsertRemove() | ||||
78 | { | ||||
79 | int initialCount = m_testHelper.evaluate("Falkon.UserScripts.count").toInt(); | ||||
80 | QObject *object = m_testHelper.evaluateQObject("Falkon.UserScripts"); | ||||
81 | QmlUserScripts *userScripts = dynamic_cast<QmlUserScripts*>(object); | ||||
82 | QVERIFY(userScripts); | ||||
83 | QmlUserScript *userScript = new QmlUserScript(); | ||||
84 | userScript->setProperty("name", "Hello World"); | ||||
85 | userScript->setProperty("sourceCode", "(function() {" | ||||
86 | " alert('Hello World')" | ||||
87 | "})()"); | ||||
88 | userScripts->insert(userScript); | ||||
89 | int finalCount = m_testHelper.evaluate("Falkon.UserScripts.count").toInt(); | ||||
90 | QCOMPARE(finalCount, initialCount + 1); | ||||
91 | | ||||
92 | userScripts->remove(userScript); | ||||
93 | | ||||
94 | int ultimateCount = m_testHelper.evaluate("Falkon.UserScripts.count").toInt(); | ||||
95 | QCOMPARE(ultimateCount, initialCount); | ||||
96 | } | ||||
97 | | ||||
98 | FALKONTEST_MAIN(QmlUserScriptApiTest) |