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

WebTab: Add addChildTab method

Also fix detaching from parent/child tabs and add autotest.
This commit is contained in:
David Rosca 2018-01-31 10:12:09 +01:00
parent 1b473ac580
commit 67973d704d
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
5 changed files with 145 additions and 13 deletions

View File

@ -21,6 +21,7 @@ falkon_tests(
updatertest
locationbartest
webviewtest
webtabtest
)
set(falkon_autotests_SRCS ${CMAKE_SOURCE_DIR}/tests/modeltest/modeltest.cpp)

75
autotests/webtabtest.cpp Normal file
View File

@ -0,0 +1,75 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 David Rosca <nowrep@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 "webtabtest.h"
#include "autotests.h"
#include "webtab.h"
#include "mainapplication.h"
void WebTabTest::initTestCase()
{
}
void WebTabTest::cleanupTestCase()
{
}
void WebTabTest::parentChildTabsTest()
{
WebTab tab1(mApp->getWindow());
WebTab tab2(mApp->getWindow());
WebTab tab3(mApp->getWindow());
WebTab tab4(mApp->getWindow());
WebTab tab5(mApp->getWindow());
WebTab tab6(mApp->getWindow());
tab1.addChildTab(&tab2);
QCOMPARE(tab1.childTabs(), QVector<WebTab*>{&tab2});
QCOMPARE(tab2.parentTab(), &tab1);
QCOMPARE(tab2.childTabs(), QVector<WebTab*>{});
tab1.addChildTab(&tab3);
QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab2, &tab3}));
QCOMPARE(tab3.parentTab(), &tab1);
QCOMPARE(tab3.childTabs(), QVector<WebTab*>{});
tab1.addChildTab(&tab4, 1);
QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab2, &tab4, &tab3}));
QCOMPARE(tab4.parentTab(), &tab1);
QCOMPARE(tab4.childTabs(), QVector<WebTab*>{});
tab4.addChildTab(&tab5);
tab4.addChildTab(&tab6);
tab4.attach(mApp->getWindow());
tab4.detach();
QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab2, &tab5, &tab6, &tab3}));
QCOMPARE(tab4.parentTab(), nullptr);
QCOMPARE(tab4.childTabs(), QVector<WebTab*>{});
tab3.addChildTab(&tab4);
tab3.setParentTab(nullptr);
tab1.addChildTab(&tab3, 0);
QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab3, &tab2, &tab5, &tab6}));
QCOMPARE(tab3.parentTab(), &tab1);
QCOMPARE(tab3.childTabs(), QVector<WebTab*>{&tab4});
QCOMPARE(tab4.parentTab(), &tab3);
}
FALKONTEST_MAIN(WebTabTest)

31
autotests/webtabtest.h Normal file
View File

@ -0,0 +1,31 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 David Rosca <nowrep@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 WebTabTest : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void cleanupTestCase();
void parentChildTabsTest();
};

View File

@ -279,15 +279,17 @@ void WebTab::detach()
Q_ASSERT(m_window);
Q_ASSERT(m_tabBar);
// Remove parent tab
if (m_parentTab) {
m_parentTab->m_childTabs.removeOne(this);
emit m_parentTab->childTabRemoved(this);
}
// Remove from child tabs
const auto childTabs = m_childTabs;
for (WebTab *child : childTabs) {
// Remove parent tab and reparent children
WebTab *parentTab = m_parentTab;
const int parentIndex = parentTab ? parentTab->m_childTabs.indexOf(this) : -1;
setParentTab(nullptr);
int i = 0;
while (!m_childTabs.isEmpty()) {
WebTab *child = m_childTabs.at(0);
child->setParentTab(nullptr);
if (parentTab) {
parentTab->addChildTab(child, parentIndex + i++);
}
}
// Remove icon from tab
@ -407,20 +409,42 @@ void WebTab::setParentTab(WebTab *tab)
}
if (m_parentTab) {
m_parentTab->m_childTabs.removeOne(this);
emit m_parentTab->childTabRemoved(this);
const int index = m_parentTab->m_childTabs.indexOf(this);
if (index >= 0) {
m_parentTab->m_childTabs.removeAt(index);
emit m_parentTab->childTabRemoved(this, index);
}
}
m_parentTab = tab;
if (m_parentTab) {
m_parentTab->m_childTabs.append(this);
emit m_parentTab->childTabAdded(this);
emit m_parentTab->childTabAdded(this, m_parentTab->m_childTabs.size() - 1);
}
emit parentTabChanged(m_parentTab);
}
void WebTab::addChildTab(WebTab *tab, int index)
{
if (tab->parentTab()) {
tab->setParentTab(nullptr);
}
tab->m_parentTab = this;
if (index < 0 || index > m_childTabs.size()) {
m_childTabs.append(tab);
emit childTabAdded(tab, m_childTabs.size() - 1);
} else {
m_childTabs.insert(index, tab);
emit childTabAdded(tab, index);
}
emit tab->parentTabChanged(this);
}
QVector<WebTab*> WebTab::childTabs() const
{
return m_childTabs;

View File

@ -66,6 +66,7 @@ public:
WebTab *parentTab() const;
void setParentTab(WebTab *tab);
void addChildTab(WebTab *tab, int index = -1);
QVector<WebTab*> childTabs() const;
@ -122,8 +123,8 @@ signals:
void pinnedChanged(bool pinned);
void restoredChanged(bool restored);
void parentTabChanged(WebTab *tab);
void childTabAdded(WebTab *tab);
void childTabRemoved(WebTab *tab);
void childTabAdded(WebTab *tab, int index);
void childTabRemoved(WebTab *tab, int index);
private:
void titleWasChanged(const QString &title);