File indexing completed on 2024-12-22 04:45:34
0001 /* 0002 SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "administratorcustomemojicreateorupdatewidgettest.h" 0008 #include "administratordialog/customemoji/administratorcustomemojicreateorupdatewidget.h" 0009 0010 #include <KUrlRequester> 0011 #include <QFormLayout> 0012 #include <QLabel> 0013 #include <QLineEdit> 0014 #include <QSignalSpy> 0015 #include <QTest> 0016 0017 QTEST_MAIN(AdministratorCustomEmojiCreateOrUpdateWidgetTest) 0018 AdministratorCustomEmojiCreateOrUpdateWidgetTest::AdministratorCustomEmojiCreateOrUpdateWidgetTest(QObject *parent) 0019 : QObject(parent) 0020 { 0021 } 0022 0023 void AdministratorCustomEmojiCreateOrUpdateWidgetTest::shouldHaveDefaultValues() 0024 { 0025 AdministratorCustomEmojiCreateOrUpdateWidget w; 0026 auto mainLayout = w.findChild<QFormLayout *>(QStringLiteral("mainLayout")); 0027 QVERIFY(mainLayout); 0028 QCOMPARE(mainLayout->contentsMargins(), QMargins{}); 0029 0030 auto mName = w.findChild<QLineEdit *>(QStringLiteral("mName")); 0031 QVERIFY(mName); 0032 QVERIFY(mName->text().isEmpty()); 0033 QVERIFY(mName->isClearButtonEnabled()); 0034 0035 auto mAlias = w.findChild<QLineEdit *>(QStringLiteral("mAlias")); 0036 QVERIFY(mAlias); 0037 QVERIFY(mAlias->text().isEmpty()); 0038 QVERIFY(mAlias->isClearButtonEnabled()); 0039 0040 auto mSelectFile = w.findChild<KUrlRequester *>(QStringLiteral("mSelectFile")); 0041 QVERIFY(mSelectFile); 0042 0043 auto mWarningLabel = w.findChild<QLabel *>(QStringLiteral("mWarningLabel")); 0044 QVERIFY(mWarningLabel); 0045 QVERIFY(mWarningLabel->isHidden()); 0046 QCOMPARE(w.type(), AdministratorCustomEmojiCreateOrUpdateWidget::AdministratorCustomEmojiCreateOrUpdateType::Create); 0047 0048 auto mIconLabel = w.findChild<QLabel *>(QStringLiteral("mIconLabel")); 0049 QVERIFY(mIconLabel); 0050 QVERIFY(mIconLabel->text().isEmpty()); 0051 } 0052 0053 void AdministratorCustomEmojiCreateOrUpdateWidgetTest::shouldEmitSignal() 0054 { 0055 AdministratorCustomEmojiCreateOrUpdateWidget w; 0056 QSignalSpy updateOkButtonChanged(&w, &AdministratorCustomEmojiCreateOrUpdateWidget::updateOkButton); 0057 0058 auto mName = w.findChild<QLineEdit *>(QStringLiteral("mName")); 0059 auto mAlias = w.findChild<QLineEdit *>(QStringLiteral("mAlias")); 0060 auto mSelectFile = w.findChild<KUrlRequester *>(QStringLiteral("mSelectFile")); 0061 auto mWarningLabel = w.findChild<QLabel *>(QStringLiteral("mWarningLabel")); 0062 0063 const QString name = QStringLiteral("bla"); 0064 mName->setText(name); 0065 QCOMPARE(updateOkButtonChanged.count(), 1); 0066 QVERIFY(!updateOkButtonChanged.at(0).at(0).toBool()); 0067 updateOkButtonChanged.clear(); 0068 QVERIFY(mWarningLabel->isHidden()); 0069 0070 mAlias->setText(QStringLiteral("bli")); 0071 QCOMPARE(updateOkButtonChanged.count(), 1); 0072 QVERIFY(!updateOkButtonChanged.at(0).at(0).toBool()); 0073 updateOkButtonChanged.clear(); 0074 QVERIFY(mWarningLabel->isHidden()); 0075 0076 // Valid 0077 mSelectFile->setUrl(QUrl::fromLocalFile(QStringLiteral("/tmp/bla"))); 0078 QCOMPARE(updateOkButtonChanged.count(), 1); 0079 QVERIFY(updateOkButtonChanged.at(0).at(0).toBool()); 0080 updateOkButtonChanged.clear(); 0081 0082 mAlias->clear(); 0083 QCOMPARE(updateOkButtonChanged.count(), 1); 0084 // Always valid when we clear alias 0085 QVERIFY(updateOkButtonChanged.at(0).at(0).toBool()); 0086 updateOkButtonChanged.clear(); 0087 0088 // Valid 0089 mSelectFile->setUrl(QUrl::fromLocalFile(QStringLiteral("/tmp/bla2"))); 0090 QCOMPARE(updateOkButtonChanged.count(), 1); 0091 QVERIFY(updateOkButtonChanged.at(0).at(0).toBool()); 0092 updateOkButtonChanged.clear(); 0093 0094 // same name and alias => disable ok button 0095 mAlias->setText(name); 0096 QCOMPARE(updateOkButtonChanged.count(), 1); 0097 QVERIFY(!updateOkButtonChanged.at(0).at(0).toBool()); 0098 updateOkButtonChanged.clear(); 0099 QVERIFY(!mWarningLabel->isHidden()); 0100 } 0101 0102 #include "moc_administratorcustomemojicreateorupdatewidgettest.cpp"