File indexing completed on 2024-04-28 16:44:32

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 #include "../src/decorationsettings.h"
0007 #include "mockbridge.h"
0008 #include "mockclient.h"
0009 #include "mockdecoration.h"
0010 #include "mocksettings.h"
0011 #include <QSignalSpy>
0012 #include <QTest>
0013 #include <QVariant>
0014 
0015 class DecorationTest : public QObject
0016 {
0017     Q_OBJECT
0018 private Q_SLOTS:
0019     void testCreate();
0020     void testOpaque();
0021     void testSection_data();
0022     void testSection();
0023 };
0024 
0025 #ifdef _MSC_VER
0026 QMap<QString, QVariant> makeMap(const QString &key, const QVariant &value);
0027 #endif
0028 void DecorationTest::testCreate()
0029 {
0030     // just test that creating the Decoration doesn't crash
0031     MockBridge bridge;
0032     const QString bridgeKey = QStringLiteral("bridge");
0033     MockDecoration deco1(nullptr,
0034                          QVariantList({
0035 #ifdef _MSC_VER
0036                              makeMap(bridgeKey, QVariant::fromValue(5)),
0037 #else
0038                              QVariantMap({{bridgeKey, QVariant::fromValue(5)}}),
0039 #endif
0040                              QVariant::fromValue(bridgeKey),
0041                              QVariantMap(),
0042 #ifdef _MSC_VER
0043                              makeMap(bridgeKey, QVariant::fromValue(&bridge)),
0044 #else
0045                              QVariantMap({{bridgeKey, QVariant::fromValue(&bridge)}})
0046 #endif
0047                          }));
0048     QVERIFY(!deco1.client().isNull());
0049 }
0050 
0051 void DecorationTest::testOpaque()
0052 {
0053     MockBridge bridge;
0054     MockDecoration deco(&bridge);
0055     QSignalSpy opaqueChangedSpy(&deco, &KDecoration2::Decoration::opaqueChanged);
0056     QVERIFY(opaqueChangedSpy.isValid());
0057     QCOMPARE(deco.isOpaque(), false);
0058     deco.setOpaque(false);
0059     QVERIFY(opaqueChangedSpy.isEmpty());
0060     deco.setOpaque(true);
0061     QCOMPARE(opaqueChangedSpy.count(), 1);
0062     QCOMPARE(opaqueChangedSpy.first().first().toBool(), true);
0063     QCOMPARE(deco.isOpaque(), true);
0064     deco.setOpaque(true);
0065     QCOMPARE(opaqueChangedSpy.count(), 1);
0066     deco.setOpaque(false);
0067     QCOMPARE(opaqueChangedSpy.count(), 2);
0068     QCOMPARE(opaqueChangedSpy.first().first().toBool(), true);
0069     QCOMPARE(opaqueChangedSpy.last().first().toBool(), false);
0070     QCOMPARE(deco.isOpaque(), false);
0071 }
0072 
0073 Q_DECLARE_METATYPE(QMargins)
0074 Q_DECLARE_METATYPE(Qt::WindowFrameSection)
0075 
0076 void DecorationTest::testSection_data()
0077 {
0078     QTest::addColumn<QRect>("titleBar");
0079     QTest::addColumn<QMargins>("margins");
0080     QTest::addColumn<QPoint>("pos");
0081     QTest::addColumn<Qt::WindowFrameSection>("expected");
0082 
0083     QRect r(1, 1, 98, 8);
0084     QMargins m(1, 10, 1, 1);
0085     QTest::newRow("topLeft") << r << m << QPoint(0, 0) << Qt::TopLeftSection;
0086     QTest::newRow("top@Left") << r << m << QPoint(1, 0) << Qt::TopSection;
0087     QTest::newRow("top@Right") << r << m << QPoint(100, 0) << Qt::TopSection;
0088     QTest::newRow("topRight") << r << m << QPoint(101, 0) << Qt::TopRightSection;
0089     QTest::newRow("right@top") << r << m << QPoint(101, 1) << Qt::RightSection;
0090     QTest::newRow("right@bottom") << r << m << QPoint(101, 109) << Qt::RightSection;
0091     QTest::newRow("bottomRight") << r << m << QPoint(101, 110) << Qt::BottomRightSection;
0092     QTest::newRow("bottom@right") << r << m << QPoint(100, 110) << Qt::BottomSection;
0093     QTest::newRow("bottom@left") << r << m << QPoint(1, 110) << Qt::BottomSection;
0094     QTest::newRow("bottomLeft") << r << m << QPoint(0, 110) << Qt::BottomLeftSection;
0095     QTest::newRow("left@Top") << r << m << QPoint(0, 1) << Qt::LeftSection;
0096     QTest::newRow("left@Bottom") << r << m << QPoint(0, 109) << Qt::LeftSection;
0097     QTest::newRow("title") << r << m << QPoint(1, 1) << Qt::TitleBarArea;
0098 }
0099 
0100 void DecorationTest::testSection()
0101 {
0102     MockBridge bridge;
0103     auto decoSettings = QSharedPointer<KDecoration2::DecorationSettings>::create(&bridge);
0104     MockDecoration deco(&bridge);
0105     deco.setSettings(decoSettings);
0106 
0107     MockSettings *settings = bridge.lastCreatedSettings();
0108     settings->setLargeSpacing(0);
0109 
0110     MockClient *client = bridge.lastCreatedClient();
0111     client->setWidth(100);
0112     client->setHeight(100);
0113     QCOMPARE(deco.size(), QSize(100, 100));
0114     QCOMPARE(deco.borderLeft(), 0);
0115     QCOMPARE(deco.borderTop(), 0);
0116     QCOMPARE(deco.borderRight(), 0);
0117     QCOMPARE(deco.borderBottom(), 0);
0118     QCOMPARE(deco.titleBar(), QRect());
0119     QCOMPARE(deco.sectionUnderMouse(), Qt::NoSection);
0120 
0121     QFETCH(QRect, titleBar);
0122     QFETCH(QMargins, margins);
0123     deco.setBorders(margins);
0124     QCOMPARE(deco.borderLeft(), margins.left());
0125     QCOMPARE(deco.borderTop(), margins.top());
0126     QCOMPARE(deco.borderRight(), margins.right());
0127     QCOMPARE(deco.borderBottom(), margins.bottom());
0128     deco.setTitleBar(titleBar);
0129     QCOMPARE(deco.titleBar(), titleBar);
0130     QCOMPARE(deco.size(), QSize(100 + deco.borderLeft() + deco.borderRight(), 100 + deco.borderTop() + deco.borderBottom()));
0131 
0132     QSignalSpy spy(&deco, &KDecoration2::Decoration::sectionUnderMouseChanged);
0133     QVERIFY(spy.isValid());
0134     QFETCH(QPoint, pos);
0135     QHoverEvent event(QEvent::HoverMove, QPointF(pos), QPointF(pos));
0136     QCoreApplication::sendEvent(&deco, &event);
0137     QFETCH(Qt::WindowFrameSection, expected);
0138     QCOMPARE(deco.sectionUnderMouse(), expected);
0139     QCOMPARE(spy.count(), 1);
0140     QCOMPARE(spy.first().first().value<Qt::WindowFrameSection>(), expected);
0141 
0142     QHoverEvent event2(QEvent::HoverMove, QPointF(50, 50), QPointF(50, 50));
0143     QCoreApplication::sendEvent(&deco, &event2);
0144     QCOMPARE(deco.sectionUnderMouse(), Qt::NoSection);
0145     QCOMPARE(spy.count(), 2);
0146     QCOMPARE(spy.first().first().value<Qt::WindowFrameSection>(), expected);
0147     QCOMPARE(spy.last().first().value<Qt::WindowFrameSection>(), Qt::NoSection);
0148 }
0149 
0150 QTEST_MAIN(DecorationTest)
0151 #include "decorationtest.moc"