1 /*
2  * SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5  */
6 #include "../src/decorationshadow.h"
7 #include <QSignalSpy>
8 #include <QTest>
9 
10 Q_DECLARE_METATYPE(QMargins)
11 
12 class DecorationShadowTest : public QObject
13 {
14     Q_OBJECT
15 private Q_SLOTS:
16     void testPadding_data();
17     void testPadding();
18     void testSizes_data();
19     void testSizes();
20 };
21 
testPadding_data()22 void DecorationShadowTest::testPadding_data()
23 {
24     QTest::addColumn<QByteArray>("propertyName");
25     QTest::addColumn<QMargins>("padding");
26 
27     QTest::newRow("top") << QByteArrayLiteral("paddingTop") << QMargins(0, 10, 0, 0);
28     QTest::newRow("right") << QByteArrayLiteral("paddingRight") << QMargins(0, 0, 10, 0);
29     QTest::newRow("bottom") << QByteArrayLiteral("paddingBottom") << QMargins(0, 0, 0, 10);
30     QTest::newRow("left") << QByteArrayLiteral("paddingLeft") << QMargins(10, 0, 0, 0);
31 }
32 
testPadding()33 void DecorationShadowTest::testPadding()
34 {
35     using namespace KDecoration2;
36     DecorationShadow shadow;
37 
38     QFETCH(QByteArray, propertyName);
39 
40     const int propertyIndex = shadow.metaObject()->indexOfProperty(propertyName.constData());
41     QVERIFY(propertyIndex != -1);
42     QMetaProperty metaProperty = shadow.metaObject()->property(propertyIndex);
43     QCOMPARE(metaProperty.isReadable(), true);
44     QCOMPARE(metaProperty.hasNotifySignal(), true);
45     QCOMPARE(metaProperty.type(), QVariant::Int);
46     QSignalSpy changedSpy(&shadow, &KDecoration2::DecorationShadow::paddingChanged);
47     QVERIFY(changedSpy.isValid());
48 
49     QCOMPARE(shadow.property(propertyName.constData()).isValid(), true);
50     QCOMPARE(shadow.property(propertyName.constData()).toInt(), 0);
51     QFETCH(QMargins, padding);
52     shadow.setPadding(padding);
53     QCOMPARE(shadow.padding(), padding);
54     QCOMPARE(shadow.property(propertyName.constData()).toInt(), 10);
55     QCOMPARE(changedSpy.count(), 1);
56 
57     // trying to set to same value shouldn't emit the signal
58     shadow.setPadding(padding);
59     QCOMPARE(shadow.property(propertyName.constData()).toInt(), 10);
60     QCOMPARE(changedSpy.count(), 1);
61 
62     // changing to different value should emit signal
63     padding += 1;
64     shadow.setPadding(padding);
65     QCOMPARE(shadow.padding(), padding);
66     QCOMPARE(shadow.property(propertyName.constData()).toInt(), 11);
67     QCOMPARE(changedSpy.count(), 2);
68 }
69 
testSizes_data()70 void DecorationShadowTest::testSizes_data()
71 {
72     QTest::addColumn<QByteArray>("propertyName");
73     QTest::addColumn<QRect>("innerShadowRect");
74     QTest::addColumn<QRect>("shadowRect");
75     QTest::addColumn<QSize>("shadowSize");
76 
77     QTest::newRow("topLeft") << QByteArrayLiteral("topLeftGeometry") << QRect(1, 2, 5, 5) << QRect(0, 0, 1, 2) << QSize(6, 7);
78     QTest::newRow("top") << QByteArrayLiteral("topGeometry") << QRect(1, 2, 1, 5) << QRect(1, 0, 1, 2) << QSize(3, 7);
79     QTest::newRow("topRight") << QByteArrayLiteral("topRightGeometry") << QRect(0, 2, 2, 1) << QRect(2, 0, 1, 2) << QSize(3, 3);
80     QTest::newRow("right") << QByteArrayLiteral("rightGeometry") << QRect(0, 0, 1, 2) << QRect(1, 0, 1, 2) << QSize(2, 4);
81     QTest::newRow("bottomRight") << QByteArrayLiteral("bottomRightGeometry") << QRect(0, 0, 1, 4) << QRect(1, 4, 1, 2) << QSize(2, 6);
82     QTest::newRow("bottom") << QByteArrayLiteral("bottomGeometry") << QRect(0, 0, 1, 1) << QRect(0, 1, 1, 2) << QSize(1, 3);
83     QTest::newRow("bottomLeft") << QByteArrayLiteral("bottomLeftGeometry") << QRect(1, 0, 1, 1) << QRect(0, 1, 1, 2) << QSize(2, 3);
84     QTest::newRow("left") << QByteArrayLiteral("leftGeometry") << QRect(1, 0, 1, 2) << QRect(0, 0, 1, 2) << QSize(2, 2);
85 }
86 
testSizes()87 void DecorationShadowTest::testSizes()
88 {
89     using namespace KDecoration2;
90     DecorationShadow shadow;
91 
92     QFETCH(QByteArray, propertyName);
93 
94     const int propertyIndex = shadow.metaObject()->indexOfProperty(propertyName.constData());
95     QVERIFY(propertyIndex != -1);
96     QMetaProperty metaProperty = shadow.metaObject()->property(propertyIndex);
97     QCOMPARE(metaProperty.isReadable(), true);
98     QCOMPARE(metaProperty.hasNotifySignal(), true);
99     QCOMPARE(metaProperty.type(), QVariant::Rect);
100     QSignalSpy changedSpy(&shadow, &KDecoration2::DecorationShadow::innerShadowRectChanged);
101     QVERIFY(changedSpy.isValid());
102 
103     QCOMPARE(shadow.innerShadowRect(), QRect());
104     QCOMPARE(shadow.property(propertyName.constData()).isValid(), true);
105     QCOMPARE(shadow.property(propertyName.constData()).toRect(), QRect());
106     QFETCH(QRect, innerShadowRect);
107     QFETCH(QRect, shadowRect);
108     QFETCH(QSize, shadowSize);
109     shadow.setInnerShadowRect(innerShadowRect);
110     QCOMPARE(shadow.innerShadowRect(), innerShadowRect);
111     // property should still be invalid as the image is not yet set
112     QCOMPARE(shadow.property(propertyName.constData()).toRect(), QRect());
113     shadow.setShadow(QImage(shadowSize, QImage::Format_ARGB32));
114     QCOMPARE(shadow.property(propertyName.constData()).toRect(), shadowRect);
115     QCOMPARE(changedSpy.count(), 1);
116 
117     // trying to set to same value shouldn't emit the signal
118     shadow.setInnerShadowRect(innerShadowRect);
119     QCOMPARE(shadow.property(propertyName.constData()).toRect(), shadowRect);
120     QCOMPARE(changedSpy.count(), 1);
121 
122     // changing to different value should emit signal
123     shadow.setInnerShadowRect(innerShadowRect.adjusted(1, 1, 1, 1));
124     QCOMPARE(changedSpy.count(), 2);
125     QCOMPARE(shadow.innerShadowRect(), innerShadowRect.adjusted(1, 1, 1, 1));
126 }
127 
128 QTEST_MAIN(DecorationShadowTest)
129 #include "shadowtest.moc"
130