File indexing completed on 2024-11-10 04:55:54

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #include "scene/itemgeometry.h"
0010 #include <QTest>
0011 
0012 Q_DECLARE_METATYPE(KWin::WindowQuadList)
0013 
0014 class WindowQuadListTest : public QObject
0015 {
0016     Q_OBJECT
0017 private Q_SLOTS:
0018     void testMakeGrid_data();
0019     void testMakeGrid();
0020     void testMakeRegularGrid_data();
0021     void testMakeRegularGrid();
0022 
0023 private:
0024     KWin::WindowQuad makeQuad(const QRectF &rect);
0025 };
0026 
0027 KWin::WindowQuad WindowQuadListTest::makeQuad(const QRectF &r)
0028 {
0029     KWin::WindowQuad quad;
0030     quad[0] = KWin::WindowVertex(r.x(), r.y(), r.x(), r.y());
0031     quad[1] = KWin::WindowVertex(r.x() + r.width(), r.y(), r.x() + r.width(), r.y());
0032     quad[2] = KWin::WindowVertex(r.x() + r.width(), r.y() + r.height(), r.x() + r.width(), r.y() + r.height());
0033     quad[3] = KWin::WindowVertex(r.x(), r.y() + r.height(), r.x(), r.y() + r.height());
0034     return quad;
0035 }
0036 
0037 void WindowQuadListTest::testMakeGrid_data()
0038 {
0039     QTest::addColumn<KWin::WindowQuadList>("orig");
0040     QTest::addColumn<int>("quadSize");
0041     QTest::addColumn<int>("expectedCount");
0042     QTest::addColumn<KWin::WindowQuadList>("expected");
0043 
0044     KWin::WindowQuadList orig;
0045     KWin::WindowQuadList expected;
0046 
0047     QTest::newRow("empty") << orig << 10 << 0 << expected;
0048 
0049     orig.append(makeQuad(QRectF(0, 0, 10, 10)));
0050     expected.append(makeQuad(QRectF(0, 0, 10, 10)));
0051     QTest::newRow("quadSizeTooLarge") << orig << 10 << 1 << expected;
0052 
0053     expected.clear();
0054     expected.append(makeQuad(QRectF(0, 0, 5, 5)));
0055     expected.append(makeQuad(QRectF(0, 5, 5, 5)));
0056     expected.append(makeQuad(QRectF(5, 0, 5, 5)));
0057     expected.append(makeQuad(QRectF(5, 5, 5, 5)));
0058     QTest::newRow("regularGrid") << orig << 5 << 4 << expected;
0059 
0060     expected.clear();
0061     expected.append(makeQuad(QRectF(0, 0, 9, 9)));
0062     expected.append(makeQuad(QRectF(0, 9, 9, 1)));
0063     expected.append(makeQuad(QRectF(9, 0, 1, 9)));
0064     expected.append(makeQuad(QRectF(9, 9, 1, 1)));
0065     QTest::newRow("irregularGrid") << orig << 9 << 4 << expected;
0066 
0067     orig.append(makeQuad(QRectF(0, 10, 4, 3)));
0068     expected.clear();
0069     expected.append(makeQuad(QRectF(0, 0, 4, 4)));
0070     expected.append(makeQuad(QRectF(0, 4, 4, 4)));
0071     expected.append(makeQuad(QRectF(0, 8, 4, 2)));
0072     expected.append(makeQuad(QRectF(0, 10, 4, 2)));
0073     expected.append(makeQuad(QRectF(0, 12, 4, 1)));
0074     expected.append(makeQuad(QRectF(4, 0, 4, 4)));
0075     expected.append(makeQuad(QRectF(4, 4, 4, 4)));
0076     expected.append(makeQuad(QRectF(4, 8, 4, 2)));
0077     expected.append(makeQuad(QRectF(8, 0, 2, 4)));
0078     expected.append(makeQuad(QRectF(8, 4, 2, 4)));
0079     expected.append(makeQuad(QRectF(8, 8, 2, 2)));
0080     QTest::newRow("irregularGrid2") << orig << 4 << 11 << expected;
0081 }
0082 
0083 void WindowQuadListTest::testMakeGrid()
0084 {
0085     QFETCH(KWin::WindowQuadList, orig);
0086     QFETCH(int, quadSize);
0087     QFETCH(int, expectedCount);
0088     KWin::WindowQuadList actual = orig.makeGrid(quadSize);
0089     QCOMPARE(actual.count(), expectedCount);
0090 
0091     QFETCH(KWin::WindowQuadList, expected);
0092     for (auto it = actual.constBegin(); it != actual.constEnd(); ++it) {
0093         bool found = false;
0094         const KWin::WindowQuad &actualQuad = (*it);
0095         for (auto it2 = expected.constBegin(); it2 != expected.constEnd(); ++it2) {
0096             const KWin::WindowQuad &expectedQuad = (*it2);
0097             auto vertexTest = [actualQuad, expectedQuad](int index) {
0098                 const KWin::WindowVertex &actualVertex = actualQuad[index];
0099                 const KWin::WindowVertex &expectedVertex = expectedQuad[index];
0100                 if (actualVertex.x() != expectedVertex.x()) {
0101                     return false;
0102                 }
0103                 if (actualVertex.y() != expectedVertex.y()) {
0104                     return false;
0105                 }
0106                 if (!qFuzzyIsNull(actualVertex.u() - expectedVertex.u())) {
0107                     return false;
0108                 }
0109                 if (!qFuzzyIsNull(actualVertex.v() - expectedVertex.v())) {
0110                     return false;
0111                 }
0112                 return true;
0113             };
0114             found = vertexTest(0) && vertexTest(1) && vertexTest(2) && vertexTest(3);
0115             if (found) {
0116                 break;
0117             }
0118         }
0119         QVERIFY2(found, qPrintable(QStringLiteral("%0, %1 / %2, %3").arg(QString::number(actualQuad.left()), QString::number(actualQuad.top()), QString::number(actualQuad.right()), QString::number(actualQuad.bottom()))));
0120     }
0121 }
0122 
0123 void WindowQuadListTest::testMakeRegularGrid_data()
0124 {
0125     QTest::addColumn<KWin::WindowQuadList>("orig");
0126     QTest::addColumn<int>("xSubdivisions");
0127     QTest::addColumn<int>("ySubdivisions");
0128     QTest::addColumn<int>("expectedCount");
0129     QTest::addColumn<KWin::WindowQuadList>("expected");
0130 
0131     KWin::WindowQuadList orig;
0132     KWin::WindowQuadList expected;
0133 
0134     QTest::newRow("empty") << orig << 1 << 1 << 0 << expected;
0135 
0136     orig.append(makeQuad(QRectF(0, 0, 10, 10)));
0137     expected.append(makeQuad(QRectF(0, 0, 10, 10)));
0138     QTest::newRow("noSplit") << orig << 1 << 1 << 1 << expected;
0139 
0140     expected.clear();
0141     expected.append(makeQuad(QRectF(0, 0, 5, 10)));
0142     expected.append(makeQuad(QRectF(5, 0, 5, 10)));
0143     QTest::newRow("xSplit") << orig << 2 << 1 << 2 << expected;
0144 
0145     expected.clear();
0146     expected.append(makeQuad(QRectF(0, 0, 10, 5)));
0147     expected.append(makeQuad(QRectF(0, 5, 10, 5)));
0148     QTest::newRow("ySplit") << orig << 1 << 2 << 2 << expected;
0149 
0150     expected.clear();
0151     expected.append(makeQuad(QRectF(0, 0, 5, 5)));
0152     expected.append(makeQuad(QRectF(5, 0, 5, 5)));
0153     expected.append(makeQuad(QRectF(0, 5, 5, 5)));
0154     expected.append(makeQuad(QRectF(5, 5, 5, 5)));
0155     QTest::newRow("xySplit") << orig << 2 << 2 << 4 << expected;
0156 
0157     orig.append(makeQuad(QRectF(0, 10, 4, 2)));
0158     expected.clear();
0159     expected.append(makeQuad(QRectF(0, 0, 5, 3)));
0160     expected.append(makeQuad(QRectF(5, 0, 5, 3)));
0161     expected.append(makeQuad(QRectF(0, 3, 5, 3)));
0162     expected.append(makeQuad(QRectF(5, 3, 5, 3)));
0163     expected.append(makeQuad(QRectF(0, 6, 5, 3)));
0164     expected.append(makeQuad(QRectF(5, 6, 5, 3)));
0165     expected.append(makeQuad(QRectF(0, 9, 5, 1)));
0166     expected.append(makeQuad(QRectF(0, 10, 4, 2)));
0167     expected.append(makeQuad(QRectF(5, 9, 5, 1)));
0168     QTest::newRow("multipleQuads") << orig << 2 << 4 << 9 << expected;
0169 }
0170 
0171 void WindowQuadListTest::testMakeRegularGrid()
0172 {
0173     QFETCH(KWin::WindowQuadList, orig);
0174     QFETCH(int, xSubdivisions);
0175     QFETCH(int, ySubdivisions);
0176     QFETCH(int, expectedCount);
0177     KWin::WindowQuadList actual = orig.makeRegularGrid(xSubdivisions, ySubdivisions);
0178     QCOMPARE(actual.count(), expectedCount);
0179 
0180     QFETCH(KWin::WindowQuadList, expected);
0181     for (auto it = actual.constBegin(); it != actual.constEnd(); ++it) {
0182         bool found = false;
0183         const KWin::WindowQuad &actualQuad = (*it);
0184         for (auto it2 = expected.constBegin(); it2 != expected.constEnd(); ++it2) {
0185             const KWin::WindowQuad &expectedQuad = (*it2);
0186             auto vertexTest = [actualQuad, expectedQuad](int index) {
0187                 const KWin::WindowVertex &actualVertex = actualQuad[index];
0188                 const KWin::WindowVertex &expectedVertex = expectedQuad[index];
0189                 if (actualVertex.x() != expectedVertex.x()) {
0190                     return false;
0191                 }
0192                 if (actualVertex.y() != expectedVertex.y()) {
0193                     return false;
0194                 }
0195                 if (!qFuzzyIsNull(actualVertex.u() - expectedVertex.u())) {
0196                     return false;
0197                 }
0198                 if (!qFuzzyIsNull(actualVertex.v() - expectedVertex.v())) {
0199                     return false;
0200                 }
0201                 return true;
0202             };
0203             found = vertexTest(0) && vertexTest(1) && vertexTest(2) && vertexTest(3);
0204             if (found) {
0205                 break;
0206             }
0207         }
0208         QVERIFY2(found, qPrintable(QStringLiteral("%0, %1 / %2, %3").arg(QString::number(actualQuad.left()), QString::number(actualQuad.top()), QString::number(actualQuad.right()), QString::number(actualQuad.bottom()))));
0209     }
0210 }
0211 
0212 QTEST_MAIN(WindowQuadListTest)
0213 
0214 #include "windowquadlisttest.moc"