File indexing completed on 2024-05-05 12:13:47

0001 /*
0002  * Copyright 2014 Alex Merry <alex.merry@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see
0019  * <http://www.gnu.org/licenses/>.
0020  */
0021 
0022 #include <QGroupBox>
0023 #include <QMetaObject>
0024 #include <QPluginLoader>
0025 #include <QTest>
0026 
0027 #include <QtUiPlugin/QDesignerCustomWidgetInterface>
0028 
0029 static bool widgetLessThan(QDesignerCustomWidgetInterface* w1, QDesignerCustomWidgetInterface* w2)
0030 {
0031     return w1->name() < w2->name();
0032 }
0033 
0034 class PluginTest : public QObject
0035 {
0036     Q_OBJECT
0037 
0038 private Q_SLOTS:
0039     void initTestCase() {
0040         QPluginLoader loader(QStringLiteral(TEST_PLUGIN));
0041         m_pluginInstance = loader.instance();
0042         QVERIFY2(m_pluginInstance, qPrintable(loader.errorString()));
0043         m_collection = qobject_cast<QDesignerCustomWidgetCollectionInterface*>(m_pluginInstance);
0044         QVERIFY(m_collection);
0045         m_widgets = m_collection->customWidgets();
0046         std::sort(m_widgets.begin(), m_widgets.end(), widgetLessThan);
0047     }
0048 
0049     void testPluginName() {
0050         QCOMPARE(m_pluginInstance->metaObject()->className(),
0051                  "SomeClass");
0052     }
0053 
0054     void testWidgetList() {
0055         QCOMPARE(m_widgets.count(), 4);
0056     }
0057 
0058     void testWidget_data() {
0059         QTest::addColumn<int>("index");
0060         QTest::addColumn<QString>("name");
0061         QTest::addColumn<QString>("implclass");
0062         QTest::addColumn<QString>("group");
0063         QTest::addColumn<QString>("tooltip");
0064         QTest::addColumn<QString>("whatsthis");
0065         QTest::addColumn<QString>("includefile");
0066         QTest::addColumn<QString>("codetemplate");
0067         QTest::addColumn<QString>("domxml");
0068         QTest::addColumn<bool>("iscontainer");
0069         QTest::addColumn<bool>("inittoggles");
0070 
0071         QTest::newRow("QCheckBox")
0072             << 0
0073             << "QCheckBox"
0074             << "QCheckBox"
0075             << "The default group"
0076             << "The check box tooltip"
0077             << "The check box whats this text"
0078             << "qcheckbox.h"
0079             << ""
0080             << "<widget class=\"QCheckBox\" name=\"qcheckbox\"/>"
0081             << false
0082             << true;
0083 
0084         QTest::newRow("QGroupBox")
0085             << 1
0086             << "QGroupBox"
0087             << "QGroupBox"
0088             << "The default group"
0089             << "QGroupBox Widget"
0090             << "QGroupBox Widget"
0091             << "qgroupbox.h"
0092             << ""
0093             << "<widget class=\"QGroupBox\" name=\"qgroupbox\"/>"
0094             << true
0095             << false;
0096 
0097         // 2 is QLabel, but the widget creation will fail
0098 
0099         QTest::newRow("QPushButton")
0100             << 3
0101             << "QPushButton"
0102             << "QCommandLinkButton"
0103             << "A group for QPushButton"
0104             << "A tooltip for QPushButton"
0105             << "A whatsthis for QPushButton"
0106             << "QCommandLinkButton"
0107             << ""
0108             << "<widget class=\"QPushButton\" name=\"qpushbutton\"/>"
0109             << false
0110             << false;
0111     }
0112 
0113     void testWidget() {
0114         QFETCH(int, index);
0115         QFETCH(QString, name);
0116         QFETCH(QString, implclass);
0117         QFETCH(QString, group);
0118         QFETCH(QString, tooltip);
0119         QFETCH(QString, whatsthis);
0120         QFETCH(QString, includefile);
0121         QFETCH(QString, codetemplate);
0122         QFETCH(QString, domxml);
0123         QFETCH(bool, iscontainer);
0124         QFETCH(bool, inittoggles);
0125 
0126         QVERIFY(m_widgets.count() > index);
0127         QDesignerCustomWidgetInterface *wiface = m_widgets.at(index);
0128         QVERIFY(wiface);
0129         QCOMPARE(wiface->name(), name);
0130         QCOMPARE(wiface->group(), group);
0131         QCOMPARE(wiface->toolTip(), tooltip);
0132         QCOMPARE(wiface->whatsThis(), whatsthis);
0133         QCOMPARE(wiface->includeFile(), includefile);
0134         QVERIFY(!wiface->icon().isNull());
0135         QCOMPARE(wiface->isContainer(), iscontainer);
0136         QCOMPARE(wiface->codeTemplate(), codetemplate);
0137         QCOMPARE(wiface->domXml(), domxml);
0138 
0139         QVERIFY(!wiface->isInitialized());
0140         wiface->initialize(nullptr);
0141         QVERIFY(wiface->isInitialized());
0142         wiface->initialize(nullptr);
0143         if (inittoggles) {
0144             QVERIFY(!wiface->isInitialized());
0145         } else {
0146             QVERIFY(wiface->isInitialized());
0147         }
0148 
0149         QWidget *widget = wiface->createWidget(nullptr);
0150         QVERIFY(widget);
0151         QCOMPARE(widget->metaObject()->className(),
0152                  implclass.toLatin1().constData());
0153     }
0154 
0155     void testConstructorArgs() {
0156         // QGroupBox
0157         QVERIFY(m_widgets.count() > 1);
0158         QDesignerCustomWidgetInterface *wiface = m_widgets.at(1);
0159         QVERIFY(wiface);
0160         QGroupBox *box = qobject_cast<QGroupBox*>(wiface->createWidget(nullptr));
0161         QVERIFY(box);
0162         QCOMPARE(box->title(), QString("the title"));
0163     }
0164 
0165     void testCreateWidget() {
0166         // QLabel
0167         QVERIFY(m_widgets.count() > 2);
0168         QDesignerCustomWidgetInterface *wiface = m_widgets.at(2);
0169         QVERIFY(wiface);
0170         QVERIFY(!wiface->createWidget(nullptr));
0171         QWidget widget;
0172         QCOMPARE(wiface->createWidget(&widget), &widget);
0173     }
0174 
0175     void testIcon() {
0176         QVERIFY(m_widgets.count() > 3);
0177 
0178         QDesignerCustomWidgetInterface *checkboxiface = m_widgets.at(0);
0179         QVERIFY(checkboxiface);
0180         QIcon checkboxIcon(":/sth.png");
0181         QCOMPARE(checkboxiface->icon().pixmap(22,22),
0182                  checkboxIcon.pixmap(22,22));
0183 
0184         QDesignerCustomWidgetInterface *pushbuttoniface = m_widgets.at(3);
0185         QVERIFY(pushbuttoniface);
0186         QIcon pushbuttonIcon(":/pics/pushbuttonview.png");
0187         QCOMPARE(pushbuttoniface->icon().pixmap(22,22),
0188                  pushbuttonIcon.pixmap(22,22));
0189     }
0190 
0191 private:
0192     QDesignerCustomWidgetCollectionInterface *m_collection;
0193     QObject *m_pluginInstance;
0194     QList<QDesignerCustomWidgetInterface*> m_widgets;
0195 };
0196 
0197 QTEST_MAIN(PluginTest)
0198 
0199 #include <plugintest.moc>