File indexing completed on 2024-04-28 15:19:33

0001 /*
0002     SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "kwindowstatesaver.h"
0007 #include "kconfiggroup.h"
0008 #include "ksharedconfig.h"
0009 
0010 #include <QSignalSpy>
0011 #include <QStandardPaths>
0012 #include <QTest>
0013 
0014 #include <QFontDialog>
0015 
0016 class KWindowStateSaverTest : public QObject
0017 {
0018     Q_OBJECT
0019 private Q_SLOTS:
0020     void initTestCase();
0021     void testTopLevelDialog();
0022     void testSubDialog();
0023 };
0024 
0025 void KWindowStateSaverTest::initTestCase()
0026 {
0027     QStandardPaths::setTestModeEnabled(true);
0028 }
0029 
0030 void KWindowStateSaverTest::testTopLevelDialog()
0031 {
0032     auto cfg = KSharedConfig::openStateConfig();
0033     cfg->deleteGroup("topLevelDialogTest");
0034     QSize dlgSize(720, 720);
0035 
0036     {
0037         QFontDialog dlg;
0038         new KWindowStateSaver(&dlg, "topLevelDialogTest");
0039         dlg.show();
0040         QTest::qWait(10); // give the window time to show up, so we simulate a user-triggered resize
0041         dlg.resize(dlgSize);
0042         QTest::qWait(500); // give the state saver time to trigger
0043         QCOMPARE(dlg.size(), dlgSize);
0044     }
0045 
0046     QVERIFY(cfg->hasGroup("topLevelDialogTest"));
0047 
0048     {
0049         QFontDialog dlg;
0050         new KWindowStateSaver(&dlg, "topLevelDialogTest");
0051         dlg.show();
0052         QTest::qWait(100); // give the window time to show up properly
0053         QCOMPARE(dlg.size(), dlgSize);
0054     }
0055 }
0056 
0057 void KWindowStateSaverTest::testSubDialog()
0058 {
0059     QWidget mainWindow;
0060     mainWindow.show();
0061     QTest::qWait(10);
0062 
0063     auto cfg = KSharedConfig::openStateConfig();
0064     cfg->deleteGroup("subDialogTest");
0065     QSize dlgSize(700, 500);
0066 
0067     {
0068         auto dlg = new QFontDialog(&mainWindow);
0069         new KWindowStateSaver(dlg, "subDialogTest");
0070         dlg->show();
0071         QTest::qWait(10); // give the window time to show up, so we simulate a user-triggered resize
0072         dlg->resize(dlgSize);
0073         QTest::qWait(500); // give the state saver time to trigger
0074         QCOMPARE(dlg->size(), dlgSize);
0075         delete dlg;
0076     }
0077 
0078     QVERIFY(cfg->hasGroup("subDialogTest"));
0079 
0080     {
0081         auto dlg = new QFontDialog(&mainWindow);
0082         new KWindowStateSaver(dlg, "subDialogTest");
0083         dlg->show();
0084         QTest::qWait(100); // give the window time to show up properly
0085         QCOMPARE(dlg->size(), dlgSize);
0086     }
0087 }
0088 
0089 QTEST_MAIN(KWindowStateSaverTest)
0090 #include "kwindowstatesavertest.moc"