File indexing completed on 2024-05-05 17:36:13

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 
0010 // KWin
0011 #include "tabbox/desktopchain.h"
0012 
0013 #include <QTest>
0014 
0015 using namespace KWin::TabBox;
0016 
0017 class TestDesktopChain : public QObject
0018 {
0019     Q_OBJECT
0020 private Q_SLOTS:
0021     void chainInit_data();
0022     void chainInit();
0023     void chainAdd_data();
0024     void chainAdd();
0025     void resize_data();
0026     void resize();
0027     void resizeAdd();
0028     void useChain();
0029 };
0030 
0031 void TestDesktopChain::chainInit_data()
0032 {
0033     QTest::addColumn<uint>("size");
0034     QTest::addColumn<uint>("next");
0035     QTest::addColumn<uint>("result");
0036 
0037     QTest::newRow("0/1") << (uint)0 << (uint)1 << (uint)1;
0038     QTest::newRow("0/5") << (uint)0 << (uint)5 << (uint)1;
0039     QTest::newRow("1/1") << (uint)1 << (uint)1 << (uint)1;
0040     QTest::newRow("1/2") << (uint)1 << (uint)2 << (uint)1;
0041     QTest::newRow("4/1") << (uint)4 << (uint)1 << (uint)2;
0042     QTest::newRow("4/2") << (uint)4 << (uint)2 << (uint)3;
0043     QTest::newRow("4/3") << (uint)4 << (uint)3 << (uint)4;
0044     QTest::newRow("4/4") << (uint)4 << (uint)4 << (uint)1;
0045     QTest::newRow("4/5") << (uint)4 << (uint)5 << (uint)1;
0046     QTest::newRow("4/7") << (uint)4 << (uint)7 << (uint)1;
0047 }
0048 
0049 void TestDesktopChain::chainInit()
0050 {
0051     QFETCH(uint, size);
0052     QFETCH(uint, next);
0053     DesktopChain chain(size);
0054     QTEST(chain.next(next), "result");
0055 
0056     DesktopChainManager manager(this);
0057     manager.resize(0, size);
0058     QTEST(manager.next(next), "result");
0059 }
0060 
0061 void TestDesktopChain::chainAdd_data()
0062 {
0063     QTest::addColumn<uint>("size");
0064     QTest::addColumn<uint>("add");
0065     QTest::addColumn<uint>("next");
0066     QTest::addColumn<uint>("result");
0067 
0068     // invalid size, should not crash
0069     QTest::newRow("0/1/1/1") << (uint)0 << (uint)1 << (uint)1 << (uint)1;
0070     // moving first element to the front, shouldn't change the chain
0071     QTest::newRow("4/1/1/2") << (uint)4 << (uint)1 << (uint)1 << (uint)2;
0072     QTest::newRow("4/1/2/3") << (uint)4 << (uint)1 << (uint)2 << (uint)3;
0073     QTest::newRow("4/1/3/4") << (uint)4 << (uint)1 << (uint)3 << (uint)4;
0074     QTest::newRow("4/1/4/1") << (uint)4 << (uint)1 << (uint)4 << (uint)1;
0075     // moving an element from middle to front, should reorder
0076     QTest::newRow("4/3/1/1") << (uint)4 << (uint)3 << (uint)1 << (uint)2;
0077     QTest::newRow("4/3/2/4") << (uint)4 << (uint)3 << (uint)2 << (uint)4;
0078     QTest::newRow("4/3/3/1") << (uint)4 << (uint)3 << (uint)3 << (uint)1;
0079     QTest::newRow("4/3/4/3") << (uint)4 << (uint)3 << (uint)4 << (uint)3;
0080     // adding an element which does not exist - should leave the chain untouched
0081     QTest::newRow("4/5/1/2") << (uint)4 << (uint)5 << (uint)1 << (uint)2;
0082     QTest::newRow("4/5/2/3") << (uint)4 << (uint)5 << (uint)2 << (uint)3;
0083     QTest::newRow("4/5/3/4") << (uint)4 << (uint)5 << (uint)3 << (uint)4;
0084     QTest::newRow("4/5/4/1") << (uint)4 << (uint)5 << (uint)4 << (uint)1;
0085 }
0086 
0087 void TestDesktopChain::chainAdd()
0088 {
0089     QFETCH(uint, size);
0090     QFETCH(uint, add);
0091     QFETCH(uint, next);
0092     DesktopChain chain(size);
0093     chain.add(add);
0094     QTEST(chain.next(next), "result");
0095 
0096     DesktopChainManager manager(this);
0097     manager.resize(0, size);
0098     manager.addDesktop(0, add);
0099     QTEST(manager.next(next), "result");
0100 }
0101 
0102 void TestDesktopChain::resize_data()
0103 {
0104     QTest::addColumn<uint>("size");
0105     QTest::addColumn<uint>("add");
0106     QTest::addColumn<uint>("newSize");
0107     QTest::addColumn<uint>("next");
0108     QTest::addColumn<uint>("result");
0109 
0110     // basic test - increment by one
0111     QTest::newRow("1->2/1") << (uint)1 << (uint)1 << (uint)2 << (uint)1 << (uint)2;
0112     QTest::newRow("1->2/2") << (uint)1 << (uint)1 << (uint)2 << (uint)2 << (uint)1;
0113     // more complex test - increment by three, keep chain untouched
0114     QTest::newRow("3->6/1") << (uint)3 << (uint)1 << (uint)6 << (uint)1 << (uint)2;
0115     QTest::newRow("3->6/2") << (uint)3 << (uint)1 << (uint)6 << (uint)2 << (uint)3;
0116     QTest::newRow("3->6/3") << (uint)3 << (uint)1 << (uint)6 << (uint)3 << (uint)4;
0117     QTest::newRow("3->6/4") << (uint)3 << (uint)1 << (uint)6 << (uint)4 << (uint)5;
0118     QTest::newRow("3->6/5") << (uint)3 << (uint)1 << (uint)6 << (uint)5 << (uint)6;
0119     QTest::newRow("3->6/6") << (uint)3 << (uint)1 << (uint)6 << (uint)6 << (uint)1;
0120     // increment by three, but change it before
0121     QTest::newRow("3->6/3/1") << (uint)3 << (uint)3 << (uint)6 << (uint)1 << (uint)2;
0122     QTest::newRow("3->6/3/2") << (uint)3 << (uint)3 << (uint)6 << (uint)2 << (uint)4;
0123     QTest::newRow("3->6/3/3") << (uint)3 << (uint)3 << (uint)6 << (uint)3 << (uint)1;
0124     QTest::newRow("3->6/3/4") << (uint)3 << (uint)3 << (uint)6 << (uint)4 << (uint)5;
0125     QTest::newRow("3->6/3/5") << (uint)3 << (uint)3 << (uint)6 << (uint)5 << (uint)6;
0126     QTest::newRow("3->6/3/6") << (uint)3 << (uint)3 << (uint)6 << (uint)6 << (uint)3;
0127 
0128     // basic test - decrement by one
0129     QTest::newRow("2->1/1") << (uint)2 << (uint)1 << (uint)1 << (uint)1 << (uint)1;
0130     QTest::newRow("2->1/2") << (uint)2 << (uint)2 << (uint)1 << (uint)1 << (uint)1;
0131     // more complex test - decrement by three, keep chain untouched
0132     QTest::newRow("6->3/1") << (uint)6 << (uint)1 << (uint)3 << (uint)1 << (uint)2;
0133     QTest::newRow("6->3/2") << (uint)6 << (uint)1 << (uint)3 << (uint)2 << (uint)3;
0134     QTest::newRow("6->3/3") << (uint)6 << (uint)1 << (uint)3 << (uint)3 << (uint)1;
0135     // more complex test - decrement by three, move element to front
0136     QTest::newRow("6->3/6/1") << (uint)6 << (uint)6 << (uint)3 << (uint)1 << (uint)2;
0137     QTest::newRow("6->3/6/2") << (uint)6 << (uint)6 << (uint)3 << (uint)2 << (uint)3;
0138     QTest::newRow("6->3/6/3") << (uint)6 << (uint)6 << (uint)3 << (uint)3 << (uint)1;
0139 }
0140 
0141 void TestDesktopChain::resize()
0142 {
0143     QFETCH(uint, size);
0144     DesktopChain chain(size);
0145     QFETCH(uint, add);
0146     chain.add(add);
0147     QFETCH(uint, newSize);
0148     chain.resize(size, newSize);
0149     QFETCH(uint, next);
0150     QTEST(chain.next(next), "result");
0151 
0152     DesktopChainManager manager(this);
0153     manager.resize(0, size);
0154     manager.addDesktop(0, add);
0155     manager.resize(size, newSize);
0156     QTEST(manager.next(next), "result");
0157 }
0158 
0159 void TestDesktopChain::resizeAdd()
0160 {
0161     // test that verifies that add works after shrinking the chain
0162     DesktopChain chain(6);
0163     DesktopChainManager manager(this);
0164     manager.resize(0, 6);
0165     chain.add(4);
0166     manager.addDesktop(0, 4);
0167     chain.add(5);
0168     manager.addDesktop(4, 5);
0169     chain.add(6);
0170     manager.addDesktop(5, 6);
0171     QCOMPARE(chain.next(6), (uint)5);
0172     QCOMPARE(manager.next(6), (uint)5);
0173     QCOMPARE(chain.next(5), (uint)4);
0174     QCOMPARE(manager.next(5), (uint)4);
0175     QCOMPARE(chain.next(4), (uint)1);
0176     QCOMPARE(manager.next(4), (uint)1);
0177     chain.resize(6, 3);
0178     manager.resize(6, 3);
0179     QCOMPARE(chain.next(3), (uint)3);
0180     QCOMPARE(manager.next(3), (uint)3);
0181     QCOMPARE(chain.next(1), (uint)3);
0182     QCOMPARE(manager.next(1), (uint)3);
0183     QCOMPARE(chain.next(2), (uint)3);
0184     QCOMPARE(manager.next(2), (uint)3);
0185     // add
0186     chain.add(1);
0187     manager.addDesktop(3, 1);
0188     QCOMPARE(chain.next(3), (uint)3);
0189     QCOMPARE(manager.next(3), (uint)3);
0190     QCOMPARE(chain.next(1), (uint)3);
0191     QCOMPARE(manager.next(1), (uint)3);
0192     chain.add(2);
0193     manager.addDesktop(1, 2);
0194     QCOMPARE(chain.next(1), (uint)3);
0195     QCOMPARE(manager.next(1), (uint)3);
0196     QCOMPARE(chain.next(2), (uint)1);
0197     QCOMPARE(manager.next(2), (uint)1);
0198     QCOMPARE(chain.next(3), (uint)2);
0199     QCOMPARE(manager.next(3), (uint)2);
0200 }
0201 
0202 void TestDesktopChain::useChain()
0203 {
0204     DesktopChainManager manager(this);
0205     manager.resize(0, 4);
0206     manager.addDesktop(0, 3);
0207     // creating the first chain, should keep it unchanged
0208     manager.useChain(QStringLiteral("test"));
0209     QCOMPARE(manager.next(3), (uint)1);
0210     QCOMPARE(manager.next(1), (uint)2);
0211     QCOMPARE(manager.next(2), (uint)4);
0212     QCOMPARE(manager.next(4), (uint)3);
0213     // but creating a second chain, should create an empty one
0214     manager.useChain(QStringLiteral("second chain"));
0215     QCOMPARE(manager.next(1), (uint)2);
0216     QCOMPARE(manager.next(2), (uint)3);
0217     QCOMPARE(manager.next(3), (uint)4);
0218     QCOMPARE(manager.next(4), (uint)1);
0219     // adding a desktop should only affect the currently used one
0220     manager.addDesktop(3, 2);
0221     QCOMPARE(manager.next(1), (uint)3);
0222     QCOMPARE(manager.next(2), (uint)1);
0223     QCOMPARE(manager.next(3), (uint)4);
0224     QCOMPARE(manager.next(4), (uint)2);
0225     // verify by switching back
0226     manager.useChain(QStringLiteral("test"));
0227     QCOMPARE(manager.next(3), (uint)1);
0228     QCOMPARE(manager.next(1), (uint)2);
0229     QCOMPARE(manager.next(2), (uint)4);
0230     QCOMPARE(manager.next(4), (uint)3);
0231     manager.addDesktop(3, 1);
0232     // use second chain again and put 4th desktop to front
0233     manager.useChain(QStringLiteral("second chain"));
0234     manager.addDesktop(3, 4);
0235     // just for the fun a third chain, and let's shrink it
0236     manager.useChain(QStringLiteral("third chain"));
0237     manager.resize(4, 3);
0238     QCOMPARE(manager.next(1), (uint)2);
0239     QCOMPARE(manager.next(2), (uint)3);
0240     // it must have affected all chains
0241     manager.useChain(QStringLiteral("test"));
0242     QCOMPARE(manager.next(1), (uint)3);
0243     QCOMPARE(manager.next(3), (uint)2);
0244     QCOMPARE(manager.next(2), (uint)1);
0245     manager.useChain(QStringLiteral("second chain"));
0246     QCOMPARE(manager.next(3), (uint)2);
0247     QCOMPARE(manager.next(1), (uint)3);
0248     QCOMPARE(manager.next(2), (uint)1);
0249 }
0250 
0251 QTEST_MAIN(TestDesktopChain)
0252 #include "test_desktopchain.moc"