1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

Added qml tabs api tests and improved qmll tabs api

This commit is contained in:
Anmol Gautam 2018-06-03 11:43:39 +05:30
parent 28f639a203
commit a9a65be17a
5 changed files with 204 additions and 32 deletions

View File

@ -59,4 +59,5 @@ falkon_qml_tests(
qmlhistoryapitest
qmlcookiesapitest
qmlclipboardapitest
qmltabsapitest
)

View File

@ -0,0 +1,117 @@
/* ============================================================
* 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 "qmltabsapitest.h"
#include "autotests.h"
#include "qmltesthelper.h"
#include "mainapplication.h"
#include "tabwidget.h"
void QmlTabsApiTest::initTestCase()
{
}
void QmlTabsApiTest::cleanupTestCase()
{
}
void QmlTabsApiTest::testInitWindowCount()
{
QCOMPARE(mApp->windowCount(), 1);
QCOMPARE(mApp->getWindow()->tabCount(), 0);
}
void QmlTabsApiTest::testTabsAPI()
{
// Tab Insertion
QmlTestHelper qmlTest;
QObject *qmlTabsObject = qmlTest.evaluateQObject("Falkon.Tabs");
QVERIFY(qmlTabsObject);
QSignalSpy qmlTabsInsertedSpy(qmlTabsObject, SIGNAL(tabInserted(QVariantMap)));
qmlTest.evaluate("Falkon.Tabs.addTab({"
" url: 'https://example.com/'"
"})");
QCOMPARE(qmlTabsInsertedSpy.count(), 1);
QVariantMap retMap1 = QVariant(qmlTabsInsertedSpy.at(0).at(0)).toMap();
int index1 = retMap1.value(QSL("index"), -1).toInt();
int windowId1 = retMap1.value(QSL("windowId"), -1).toInt();
QCOMPARE(index1, 0);
QCOMPARE(windowId1, 0);
QObject *qmlTabObject1 = qmlTest.evaluateQObject("Falkon.Tabs.get({index: 0})");
QVERIFY(qmlTabObject1);
QCOMPARE(qmlTabObject1->property("url").toString(), "https://example.com/");
QCOMPARE(qmlTabObject1->property("index").toInt(), 0);
QCOMPARE(qmlTabObject1->property("pinned").toBool(), false);
qmlTest.evaluate("Falkon.Tabs.addTab({"
" url: 'https://another-example.com/',"
"})");
QCOMPARE(qmlTabsInsertedSpy.count(), 2);
QVariantMap retMap2 = QVariant(qmlTabsInsertedSpy.at(1).at(0)).toMap();
int index2 = retMap2.value(QSL("index"), -1).toInt();
int windowId2 = retMap2.value(QSL("windowId"), -1).toInt();
QCOMPARE(index2, 1);
QCOMPARE(windowId2, 0);
bool pinnedTab = qmlTest.evaluate("Falkon.Tabs.pinTab({index: 1})").toBool();
QVERIFY(pinnedTab);
QObject *qmlTabObject2 = qmlTest.evaluateQObject("Falkon.Tabs.get({index: 0})");
QVERIFY(qmlTabObject2);
QCOMPARE(qmlTabObject2->property("url").toString(), "https://another-example.com/");
QCOMPARE(qmlTabObject2->property("index").toInt(), 0);
QCOMPARE(qmlTabObject2->property("pinned").toBool(), true);
bool unpinnedTab = qmlTest.evaluate("Falkon.Tabs.unpinTab({index: 0})").toBool();
QVERIFY(unpinnedTab);
QObject *qmlTabObject3 = qmlTest.evaluateQObject("Falkon.Tabs.get({index: 0})");
QVERIFY(qmlTabObject3);
QCOMPARE(qmlTabObject3->property("url").toString(), "https://another-example.com/");
QCOMPARE(qmlTabObject3->property("index").toInt(), 0);
QCOMPARE(qmlTabObject3->property("pinned").toBool(), false);
// TODO: when the windowId is different from current window
// Next-Previous-Current
QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 0);
qmlTest.evaluate("Falkon.Tabs.nextTab()");
QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 1);
qmlTest.evaluate("Falkon.Tabs.nextTab()");
QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 0);
qmlTest.evaluate("Falkon.Tabs.previousTab()");
QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 1);
qmlTest.evaluate("Falkon.Tabs.previousTab()");
QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 0);
qmlTest.evaluate("Falkon.Tabs.setCurrentIndex({index: 1})");
QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 1);
qmlTest.evaluate("Falkon.Tabs.setCurrentIndex({index: 0})");
QCOMPARE(mApp->getWindow()->tabWidget()->currentIndex(), 0);
// Move Tab
QSignalSpy qmlTabsMovedSpy(qmlTabsObject, SIGNAL(tabMoved(QVariantMap)));
qmlTest.evaluate("Falkon.Tabs.moveTab({from: 0, to:1})");
QCOMPARE(qmlTabsMovedSpy.count(), 1);
// Tab Removal
QCOMPARE(mApp->getWindow()->tabCount(), 2);
QSignalSpy qmlTabsRemovedSpy(qmlTabsObject, SIGNAL(tabRemoved(QVariantMap)));
qmlTest.evaluate("Falkon.Tabs.closeTab({index: 0})");
QCOMPARE(qmlTabsRemovedSpy.count(), 1);
QCOMPARE(mApp->getWindow()->tabCount(), 1);
}
FALKONTEST_MAIN(QmlTabsApiTest)

View File

@ -0,0 +1,32 @@
/* ============================================================
* 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 QmlTabsApiTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void testInitWindowCount();
void testTabsAPI();
};

View File

@ -22,35 +22,11 @@
QmlTabs::QmlTabs(QObject *parent)
: QObject(parent)
{
connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, [this](BrowserWindow *window){
// FIXME: make it more efficient
for (int i = 0; i < mApp->windowCount(); i++) {
windowIdHash[mApp->windows().at(i)] = i;
}
for (BrowserWindow *window : mApp->windows()) {
windowCreated(window);
}
connect(window->tabWidget(), &TabWidget::changed, this, [this, window]{
QVariantMap map;
int windowId = windowIdHash.value(window);
map.insert(QSL("windowId"), windowId);
emit changed(map);
});
connect(window->tabWidget(), &TabWidget::tabInserted, this, [this, window](int index){
QVariantMap map;
int windowId = windowIdHash.value(window);
map.insert(QSL("windowId"), windowId);
map.insert(QSL("index"), index);
emit tabInserted(map);
});
connect(window->tabWidget(), &TabWidget::tabRemoved, this, [this, window](int index){
QVariantMap map;
int windowId = windowIdHash.value(window);
map.insert(QSL("windowId"), windowId);
map.insert(QSL("index"), index);
emit tabRemoved(map);
});
});
connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, &QmlTabs::windowCreated);
connect(mApp->plugins(), &PluginProxy::mainWindowDeleted, this, [this](BrowserWindow *window){
// FIXME: make it more efficient
@ -143,11 +119,13 @@ bool QmlTabs::pinTab(const QVariantMap &map)
return false;
}
if (window->tabWidget()->webTab(index)->isPinned()) {
WebTab *webTab = window->tabWidget()->webTab(index);
if (webTab->isPinned()) {
return false;
}
window->tabWidget()->pinUnPinTab(index);
webTab->togglePinned();
return true;
}
@ -165,11 +143,13 @@ bool QmlTabs::unpinTab(const QVariantMap &map)
return false;
}
if (!window->tabWidget()->webTab(index)->isPinned()) {
WebTab *webTab = window->tabWidget()->webTab(index);
if (!webTab->isPinned()) {
return false;
}
window->tabWidget()->pinUnPinTab(index);
webTab->togglePinned();
return true;
}
@ -360,3 +340,43 @@ BrowserWindow *QmlTabs::getWindow(const QVariantMap &map) const
return window;
}
void QmlTabs::windowCreated(BrowserWindow *window)
{
// FIXME: make it more efficient
for (int i = 0; i < mApp->windowCount(); i++) {
windowIdHash[mApp->windows().at(i)] = i;
}
connect(window->tabWidget(), &TabWidget::changed, this, [this, window]{
QVariantMap map;
int windowId = windowIdHash.value(window);
map.insert(QSL("windowId"), windowId);
emit changed(map);
});
connect(window->tabWidget(), &TabWidget::tabInserted, this, [this, window](int index){
QVariantMap map;
int windowId = windowIdHash.value(window);
map.insert(QSL("windowId"), windowId);
map.insert(QSL("index"), index);
emit tabInserted(map);
});
connect(window->tabWidget(), &TabWidget::tabRemoved, this, [this, window](int index){
QVariantMap map;
int windowId = windowIdHash.value(window);
map.insert(QSL("windowId"), windowId);
map.insert(QSL("index"), index);
emit tabRemoved(map);
});
connect(window->tabWidget(), &TabWidget::tabMoved, this, [this, window](int from, int to){
QVariantMap map;
int windowId = windowIdHash.value(window);
map.insert(QSL("windowId"), windowId);
map.insert(QSL("from"), from);
map.insert(QSL("to"), to);
emit tabMoved(map);
});
}

View File

@ -47,7 +47,9 @@ Q_SIGNALS:
void changed(const QVariantMap &map);
void tabInserted(const QVariantMap &map);
void tabRemoved(const QVariantMap &map);
void tabMoved(const QVariantMap &map);
private:
BrowserWindow *getWindow(const QVariantMap &map) const;
void windowCreated(BrowserWindow *window);
QHash<BrowserWindow*, int> windowIdHash;
};