Falkon Develop
Cross-platform Qt-based web browser
qmltabsapitest.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* ============================================================ */
18#include "qmltabsapitest.h"
19#include "autotests.h"
20#include "mainapplication.h"
21#include "tabwidget.h"
22
23void QmlTabsApiTest::initTestCase()
24{
25}
26
27void QmlTabsApiTest::cleanupTestCase()
28{
29}
30
31void QmlTabsApiTest::testInitWindowCount()
32{
33 QCOMPARE(mApp->windowCount(), 1);
34 QTRY_COMPARE(mApp->getWindow()->tabCount(), 1);
35}
36
37void QmlTabsApiTest::testTabsAPI()
38{
39 /* Wait until the initial tab (at index 0) in the window is created */
40 QTRY_COMPARE(mApp->getWindow()->tabCount(), 1);
41
42 // Tab Insertion
43 QObject *qmlTabsObject = m_testHelper.evaluateQObject(QSL("Falkon.Tabs"));
44 QVERIFY(qmlTabsObject);
45 QSignalSpy qmlTabsInsertedSpy(qmlTabsObject, SIGNAL(tabInserted(QVariantMap)));
46 m_testHelper.evaluate(QL1S("Falkon.Tabs.addTab({"
47 " url: 'https://example.com/'"
48 "})"));
49 QCOMPARE(qmlTabsInsertedSpy.count(), 1);
50 QVariantMap retMap1 = QVariant(qmlTabsInsertedSpy.at(0).at(0)).toMap();
51 int index1 = retMap1.value(QSL("index"), -1).toInt();
52 int windowId1 = retMap1.value(QSL("windowId"), -1).toInt();
53 QCOMPARE(index1, 1);
54 QCOMPARE(windowId1, 0);
55
56 QObject *qmlTabObject1 = m_testHelper.evaluateQObject(QSL("Falkon.Tabs.get({index: 1})"));
57 QVERIFY(qmlTabObject1);
58 QCOMPARE(qmlTabObject1->property("index").toInt(), 1);
59 QCOMPARE(qmlTabObject1->property("pinned").toBool(), false);
60 QTRY_COMPARE(qmlTabObject1->property("url").toString(), QSL("https://example.com/"));
61
62 m_testHelper.evaluate(QL1S("Falkon.Tabs.addTab({"
63 " url: 'https://another-example.com/'"
64 "})"));
65 QCOMPARE(qmlTabsInsertedSpy.count(), 2);
66 QVariantMap retMap2 = QVariant(qmlTabsInsertedSpy.at(1).at(0)).toMap();
67 int index2 = retMap2.value(QSL("index"), -1).toInt();
68 int windowId2 = retMap2.value(QSL("windowId"), -1).toInt();
69 QCOMPARE(index2, 2);
70 QCOMPARE(windowId2, 0);
71
72 bool pinnedTab = m_testHelper.evaluate(QSL("Falkon.Tabs.pinTab({index: 2})")).toBool();
73 QVERIFY(pinnedTab);
74 QObject *qmlTabObject2 = m_testHelper.evaluateQObject(QSL("Falkon.Tabs.get({index: 0})"));
75 QVERIFY(qmlTabObject2);
76 QCOMPARE(qmlTabObject2->property("index").toInt(), 0);
77 QCOMPARE(qmlTabObject2->property("pinned").toBool(), true);
78 QTRY_COMPARE(qmlTabObject2->property("url").toString(), QSL("https://another-example.com/"));
79
80 bool unpinnedTab = m_testHelper.evaluate(QSL("Falkon.Tabs.unpinTab({index: 0})")).toBool();
81 QVERIFY(unpinnedTab);
82 QObject *qmlTabObject3 = m_testHelper.evaluateQObject(QSL("Falkon.Tabs.get({index: 0})"));
83 QVERIFY(qmlTabObject3);
84 QCOMPARE(qmlTabObject3->property("url").toString(), QSL("https://another-example.com/"));
85 QCOMPARE(qmlTabObject3->property("index").toInt(), 0);
86 QCOMPARE(qmlTabObject3->property("pinned").toBool(), false);
87
88 // Next-Previous-Current
89 QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 0);
90 m_testHelper.evaluate(QSL("Falkon.Tabs.nextTab()"));
91 QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 1);
92 m_testHelper.evaluate(QSL("Falkon.Tabs.nextTab()"));
93 QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 2);
94 m_testHelper.evaluate(QSL("Falkon.Tabs.nextTab()"));
95 QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 0);
96 m_testHelper.evaluate(QSL("Falkon.Tabs.previousTab()"));
97 QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 2);
98 m_testHelper.evaluate(QSL("Falkon.Tabs.previousTab()"));
99 QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 1);
100 m_testHelper.evaluate(QSL("Falkon.Tabs.previousTab()"));
101 QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 0);
102 m_testHelper.evaluate(QSL("Falkon.Tabs.setCurrentIndex({index: 2})"));
103 QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 2);
104 m_testHelper.evaluate(QSL("Falkon.Tabs.setCurrentIndex({index: 1})"));
105 QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 1);
106 m_testHelper.evaluate(QSL("Falkon.Tabs.setCurrentIndex({index: 0})"));
107 QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 0);
108
109 // Move Tab
110 QSignalSpy qmlTabsMovedSpy(qmlTabsObject, SIGNAL(tabMoved(QVariantMap)));
111 m_testHelper.evaluate(QSL("Falkon.Tabs.moveTab({from: 0, to:1, windowId: 0})"));
112 QCOMPARE(qmlTabsMovedSpy.count(), 1);
113
114 // Tab Removal
115 QCOMPARE(mApp->getWindow()->tabCount(), 3);
116 QSignalSpy qmlTabsRemovedSpy(qmlTabsObject, SIGNAL(tabRemoved(QVariantMap)));
117 m_testHelper.evaluate(QSL("Falkon.Tabs.closeTab({index: 0})"));
118 QCOMPARE(qmlTabsRemovedSpy.count(), 1);
119 QCOMPARE(mApp->getWindow()->tabCount(), 2);
120}
121
#define FALKONTEST_MAIN(Test)
Definition: autotests.h:23
QObject * evaluateQObject(const QString &source)
QJSValue evaluate(const QString &source)
#define mApp
#define QL1S(x)
Definition: qzcommon.h:44
#define QSL(x)
Definition: qzcommon.h:40