File indexing completed on 2024-11-24 05:00:54

0001 /*  This file is part of the KDE libraries
0002     SPDX-FileCopyrightText: 2014 Dominik Haumann <dhaumann@kde.org>
0003     SPDX-FileCopyrightText: 2015 David Rosca <nowrep@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include <KFileWidget>
0009 #include <QQmlComponent>
0010 #include <QQmlEngine>
0011 #include <QTest>
0012 
0013 class KFileDialogQml_UnitTest : public QObject
0014 {
0015     Q_OBJECT
0016 
0017 private Q_SLOTS:
0018     void initTestCase()
0019     {
0020         m_engine = new QQmlEngine;
0021     }
0022 
0023     void cleanupTestCase()
0024     {
0025         delete m_engine;
0026     }
0027 
0028     void testShowDialogParentless()
0029     {
0030         KFileWidget *fw;
0031         {
0032             QQmlComponent component(m_engine);
0033             component.loadUrl(QUrl::fromLocalFile(QFINDTESTDATA("qml/filedialog_parentless.qml")));
0034             QScopedPointer<QObject> object(component.create());
0035             QVERIFY(!object.isNull());
0036 
0037             fw = findFileWidget();
0038             QVERIFY(fw);
0039             QCOMPARE(fw->isVisible(), true);
0040             fw->slotCancel();
0041         }
0042     }
0043 
0044     void testShowDialogWithParent()
0045     {
0046         KFileWidget *fw;
0047         {
0048             QQmlComponent component(m_engine);
0049             component.loadUrl(QUrl::fromLocalFile(QFINDTESTDATA("qml/filedialog_withparent.qml")));
0050             QScopedPointer<QObject> object(component.create());
0051             QVERIFY(!object.isNull());
0052 
0053             fw = findFileWidget();
0054             QVERIFY(fw);
0055             QCOMPARE(fw->isVisible(), true);
0056             fw->slotCancel();
0057         }
0058     }
0059 
0060 private:
0061     static KFileWidget *findFileWidget()
0062     {
0063         QList<KFileWidget *> widgets;
0064         foreach (QWidget *widget, QApplication::topLevelWidgets()) {
0065             KFileWidget *fw = widget->findChild<KFileWidget *>();
0066             if (fw) {
0067                 widgets.append(fw);
0068             }
0069         }
0070         Q_ASSERT(widgets.count() == 1);
0071         return (widgets.count() == 1) ? widgets.first() : nullptr;
0072     }
0073 
0074     QQmlEngine *m_engine = nullptr;
0075 };
0076 
0077 QTEST_MAIN(KFileDialogQml_UnitTest)
0078 
0079 #include "kfiledialogqml_unittest.moc"