Files
KDDockWidgets/tests/tst_common.cpp
2020-10-04 20:37:00 +01:00

135 lines
3.7 KiB
C++

/*
This file is part of KDDockWidgets.
SPDX-FileCopyrightText: 2019-2020 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Sérgio Martins <sergio.martins@kdab.com>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
// We don't care about performance related checks in the tests
// clazy:excludeall=ctor-missing-parent-argument,missing-qobject-macro,range-loop,missing-typeinfo,detaching-member,function-args-by-ref,non-pod-global-static,reserve-candidates,qstring-allocations
#include "Testing.h"
#include "utils.h"
#include "DockWidgetBase.h"
#include "multisplitter/Separator_p.h"
#include "private/MultiSplitter_p.h"
#include <QtTest/QtTest>
#include <QObject>
#include <QApplication>
#ifdef KDDOCKWIDGETS_QTQUICK
# include <QQmlEngine>
# include <QQuickStyle>
#endif
using namespace KDDockWidgets;
using namespace Layouting;
using namespace KDDockWidgets::Tests;
class TestCommon : public QObject
{
Q_OBJECT
public Q_SLOTS:
void initTestCase()
{
qputenv("KDDOCKWIDGETS_SHOW_DEBUG_WINDOW", "");
qApp->setOrganizationName("KDAB");
qApp->setApplicationName("dockwidgets-unit-tests");
Testing::installFatalMessageHandler();
#ifdef KDDOCKWIDGETS_QTQUICK
QQuickStyle::setStyle("Material"); // so we don't load KDE pluginss
KDDockWidgets::Config::self().setQmlEngine(new QQmlEngine(this));
#endif
QTest::qWait(10); // the DND state machine needs the event loop to start, otherwise activeState() is nullptr. (for offscreen QPA)
}
void cleanupTestCase()
{
#ifdef KDDOCKWIDGETS_QTQUICK
delete KDDockWidgets::Config::Config::self().qmlEngine();
#endif
}
private Q_SLOTS:
void tst_simple1();
void tst_doesntHaveNativeTitleBar();
void tst_resizeWindow2();
};
void TestCommon::tst_simple1()
{
// Simply create a MainWindow
EnsureTopLevelsDeleted e;
auto m = createMainWindow();
m->multiSplitter()->checkSanity();
}
void TestCommon::tst_doesntHaveNativeTitleBar()
{
// Tests that a floating window doesn't have a native title bar
// This test is mostly to test a bug that was happening with QtQuick, where the native title bar
// would appear on linux
EnsureTopLevelsDeleted e;
auto dw1 = createDockWidget("dock1");
FloatingWindow *fw = dw1->floatingWindow();
QVERIFY(fw);
QVERIFY(fw->windowFlags() & Qt::Tool);
#if defined(Q_OS_LINUX)
QVERIFY(fw->windowFlags() & Qt::FramelessWindowHint);
#elif defined(Q_OS_WIN)
QVERIFY(!(fw->windowFlags() & Qt::FramelessWindowHint));
#endif
delete dw1->window();
}
void TestCommon::tst_resizeWindow2()
{
// Tests that resizing the width of the main window will never move horizontal anchors
EnsureTopLevelsDeleted e;
auto m = createMainWindow(QSize(501, 500), MainWindowOption_None);
auto dock1 = createDockWidget("1");
auto dock2 = createDockWidget("2");
FloatingWindow *fw1 = dock1->floatingWindow();
FloatingWindow *fw2 = dock2->floatingWindow();
m->addDockWidget(dock1, Location_OnTop);
m->addDockWidget(dock2, Location_OnBottom);
auto layout = m->multiSplitter();
Separator *anchor = layout->separators().at(0);
const int oldPosY = anchor->position();
m->resize(QSize(m->width() + 10, m->height()));
QCOMPARE(anchor->position(), oldPosY);
layout->checkSanity();
delete fw1;
delete fw2;
}
int main(int argc, char *argv[])
{
if (!qpaPassedAsArgument(argc, argv)) {
// Use offscreen by default as it's less annoying, doesn't create visible windows
qputenv("QT_QPA_PLATFORM", "offscreen");
}
QApplication app(argc, argv);
TestCommon test;
return QTest::qExec(&test, argc, argv);
}
#include "tst_common.moc"