File indexing completed on 2025-02-16 04:49:25
0001 /* 0002 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "markdowncreateimagewidgettest.h" 0008 #include "markdowncreateimagewidget.h" 0009 #include <QCheckBox> 0010 #include <QFormLayout> 0011 #include <QLabel> 0012 #include <QLineEdit> 0013 #include <QSignalSpy> 0014 #include <QSpinBox> 0015 0016 #include <QTest> 0017 QTEST_MAIN(MarkdownCreateImageWidgetTest) 0018 0019 MarkdownCreateImageWidgetTest::MarkdownCreateImageWidgetTest(QObject *parent) 0020 : QObject(parent) 0021 { 0022 } 0023 0024 void MarkdownCreateImageWidgetTest::shouldHaveDefaultValue() 0025 { 0026 MarkdownCreateImageWidget w; 0027 0028 auto mainLayout = w.findChild<QFormLayout *>(QStringLiteral("mainlayout")); 0029 QVERIFY(mainLayout); 0030 0031 QCOMPARE(mainLayout->contentsMargins(), QMargins()); 0032 0033 auto mTitle = w.findChild<QLineEdit *>(QStringLiteral("title")); 0034 QVERIFY(mTitle); 0035 QVERIFY(mTitle->text().isEmpty()); 0036 QVERIFY(mTitle->isClearButtonEnabled()); 0037 0038 auto mLink = w.findChild<QLineEdit *>(QStringLiteral("image")); 0039 QVERIFY(mLink); 0040 QVERIFY(mLink->text().isEmpty()); 0041 QVERIFY(mLink->isClearButtonEnabled()); 0042 0043 auto mAlternateText = w.findChild<QLineEdit *>(QStringLiteral("alternatetext")); 0044 QVERIFY(mAlternateText); 0045 QVERIFY(mAlternateText->text().isEmpty()); 0046 QVERIFY(mAlternateText->isClearButtonEnabled()); 0047 0048 auto mKeepOriginalSize = w.findChild<QCheckBox *>(QStringLiteral("keeporiginalsize")); 0049 QVERIFY(mKeepOriginalSize); 0050 QVERIFY(mKeepOriginalSize->isChecked()); 0051 0052 auto mLabWidth = w.findChild<QLabel *>(QStringLiteral("labwidth")); 0053 QVERIFY(mLabWidth); 0054 QVERIFY(!mLabWidth->text().isEmpty()); 0055 0056 auto mWidth = w.findChild<QSpinBox *>(QStringLiteral("mwidth")); 0057 QCOMPARE(mWidth->minimum(), 1); 0058 QCOMPARE(mWidth->maximum(), 999); 0059 QVERIFY(!mWidth->isEnabled()); 0060 0061 auto mLabHeight = w.findChild<QLabel *>(QStringLiteral("labheight")); 0062 QVERIFY(mLabHeight); 0063 QVERIFY(!mLabHeight->text().isEmpty()); 0064 0065 auto mHeight = w.findChild<QSpinBox *>(QStringLiteral("mheight")); 0066 QCOMPARE(mHeight->minimum(), 1); 0067 QCOMPARE(mHeight->maximum(), 999); 0068 QVERIFY(!mHeight->isEnabled()); 0069 0070 auto sizeWidget = w.findChild<QWidget *>(QStringLiteral("sizeWidget")); 0071 QVERIFY(sizeWidget); 0072 0073 auto sizeWidgetLayout = w.findChild<QHBoxLayout *>(QStringLiteral("sizeWidgetLayout")); 0074 QVERIFY(sizeWidgetLayout); 0075 QCOMPARE(sizeWidgetLayout->contentsMargins(), QMargins()); 0076 } 0077 0078 void MarkdownCreateImageWidgetTest::shouldGenerateLink() 0079 { 0080 MarkdownCreateImageWidget w; 0081 auto mTitle = w.findChild<QLineEdit *>(QStringLiteral("title")); 0082 auto mLink = w.findChild<QLineEdit *>(QStringLiteral("image")); 0083 auto mAlternateText = w.findChild<QLineEdit *>(QStringLiteral("alternatetext")); 0084 mLink->setText(QStringLiteral("http://www.kde.org")); 0085 mTitle->setText(QStringLiteral("TITLE")); 0086 QCOMPARE(w.linkStr(), QStringLiteral("")); 0087 0088 mAlternateText->setText(QStringLiteral("alternate")); 0089 QCOMPARE(w.linkStr(), QStringLiteral("")); 0090 } 0091 0092 void MarkdownCreateImageWidgetTest::shouldChangeState() 0093 { 0094 MarkdownCreateImageWidget w; 0095 auto mKeepOriginalSize = w.findChild<QCheckBox *>(QStringLiteral("keeporiginalsize")); 0096 auto mLabWidth = w.findChild<QLabel *>(QStringLiteral("labwidth")); 0097 auto mWidth = w.findChild<QSpinBox *>(QStringLiteral("mwidth")); 0098 auto mLabHeight = w.findChild<QLabel *>(QStringLiteral("labheight")); 0099 auto mHeight = w.findChild<QSpinBox *>(QStringLiteral("mheight")); 0100 0101 mKeepOriginalSize->setChecked(true); 0102 QVERIFY(!mLabWidth->isEnabled()); 0103 QVERIFY(!mWidth->isEnabled()); 0104 QVERIFY(!mLabHeight->isEnabled()); 0105 QVERIFY(!mHeight->isEnabled()); 0106 0107 mKeepOriginalSize->setChecked(false); 0108 QVERIFY(mLabWidth->isEnabled()); 0109 QVERIFY(mWidth->isEnabled()); 0110 QVERIFY(mLabHeight->isEnabled()); 0111 QVERIFY(mHeight->isEnabled()); 0112 } 0113 0114 void MarkdownCreateImageWidgetTest::shouldAddSize() 0115 { 0116 MarkdownCreateImageWidget w; 0117 auto mTitle = w.findChild<QLineEdit *>(QStringLiteral("title")); 0118 auto mLink = w.findChild<QLineEdit *>(QStringLiteral("image")); 0119 auto mAlternateText = w.findChild<QLineEdit *>(QStringLiteral("alternatetext")); 0120 mLink->setText(QStringLiteral("http://www.kde.org")); 0121 mTitle->setText(QStringLiteral("TITLE")); 0122 0123 mAlternateText->setText(QStringLiteral("alternate")); 0124 auto mKeepOriginalSize = w.findChild<QCheckBox *>(QStringLiteral("keeporiginalsize")); 0125 auto mWidth = w.findChild<QSpinBox *>(QStringLiteral("mwidth")); 0126 auto mHeight = w.findChild<QSpinBox *>(QStringLiteral("mheight")); 0127 mKeepOriginalSize->setChecked(false); 0128 mWidth->setValue(45); 0129 mHeight->setValue(70); 0130 0131 QCOMPARE(w.linkStr(), QStringLiteral("")); 0132 mAlternateText->setText(QString()); 0133 QCOMPARE(w.linkStr(), QStringLiteral("")); 0134 } 0135 0136 void MarkdownCreateImageWidgetTest::shouldEmitSignal() 0137 { 0138 MarkdownCreateImageWidget w; 0139 QSignalSpy spy(&w, &MarkdownCreateImageWidget::enabledOkButton); 0140 auto mTitle = w.findChild<QLineEdit *>(QStringLiteral("title")); 0141 auto mLink = w.findChild<QLineEdit *>(QStringLiteral("image")); 0142 auto mAlternateText = w.findChild<QLineEdit *>(QStringLiteral("alternatetext")); 0143 mTitle->setText(QStringLiteral("foo")); 0144 QCOMPARE(spy.count(), 1); 0145 QCOMPARE(spy.at(0).at(0).value<bool>(), false); 0146 spy.clear(); 0147 0148 mAlternateText->setText(QStringLiteral("dd")); 0149 QCOMPARE(spy.count(), 0); 0150 spy.clear(); 0151 0152 mLink->setText(QStringLiteral("dd")); 0153 QCOMPARE(spy.count(), 1); 0154 QCOMPARE(spy.at(0).at(0).value<bool>(), true); 0155 spy.clear(); 0156 mTitle->clear(); 0157 QCOMPARE(spy.at(0).at(0).value<bool>(), false); 0158 spy.clear(); 0159 } 0160 0161 #include "moc_markdowncreateimagewidgettest.cpp"