File indexing completed on 2025-01-05 04:58:20

0001 /*
0002   SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "recentaddresswidgettest.h"
0009 #include "addressline/recentaddress/recentaddresswidget.h"
0010 #include <QLineEdit>
0011 #include <QListWidget>
0012 #include <QTest>
0013 #include <QToolButton>
0014 #include <qtestmouse.h>
0015 
0016 RecentAddressWidgetTest::RecentAddressWidgetTest(QObject *parent)
0017     : QObject(parent)
0018 {
0019 }
0020 
0021 RecentAddressWidgetTest::~RecentAddressWidgetTest() = default;
0022 
0023 void RecentAddressWidgetTest::shouldHaveDefaultValue()
0024 {
0025     PimCommon::RecentAddressWidget w;
0026     auto lineedit = w.findChild<QLineEdit *>(QStringLiteral("line_edit"));
0027     QVERIFY(lineedit);
0028     QVERIFY(lineedit->isClearButtonEnabled());
0029     QVERIFY(lineedit->text().isEmpty());
0030 
0031     auto newButton = w.findChild<QToolButton *>(QStringLiteral("new_button"));
0032     QVERIFY(newButton);
0033 
0034     auto removeButton = w.findChild<QToolButton *>(QStringLiteral("remove_button"));
0035     QVERIFY(removeButton);
0036     QVERIFY(!removeButton->isEnabled());
0037 
0038     auto listview = w.findChild<QListWidget *>(QStringLiteral("list_view"));
0039     QVERIFY(listview);
0040     QCOMPARE(listview->count(), 0);
0041     QCOMPARE(listview->contextMenuPolicy(), Qt::CustomContextMenu);
0042 }
0043 
0044 void RecentAddressWidgetTest::shouldAddAddresses()
0045 {
0046     PimCommon::RecentAddressWidget w;
0047     auto listview = w.findChild<QListWidget *>(QStringLiteral("list_view"));
0048     QCOMPARE(listview->count(), 0);
0049     QStringList lst;
0050     lst << QStringLiteral("foo");
0051     lst << QStringLiteral("foo1");
0052     lst << QStringLiteral("foo2");
0053     w.setAddresses(lst);
0054     QCOMPARE(listview->count(), lst.count());
0055     // Clear list before to add
0056     w.setAddresses(lst);
0057     QCOMPARE(listview->count(), lst.count());
0058 }
0059 
0060 void RecentAddressWidgetTest::shouldInformThatItWasChanged()
0061 {
0062     PimCommon::RecentAddressWidget w;
0063     QVERIFY(!w.wasChanged());
0064     auto lineedit = w.findChild<QLineEdit *>(QStringLiteral("line_edit"));
0065     lineedit->setText(QStringLiteral("foo"));
0066     auto newButton = w.findChild<QToolButton *>(QStringLiteral("new_button"));
0067     QVERIFY(newButton);
0068     QVERIFY(newButton->isEnabled());
0069     QTest::mouseClick(newButton, Qt::LeftButton);
0070     QVERIFY(w.wasChanged());
0071     auto listview = w.findChild<QListWidget *>(QStringLiteral("list_view"));
0072     QCOMPARE(listview->count(), 1);
0073 }
0074 
0075 void RecentAddressWidgetTest::shouldNotAddMultiSameLine()
0076 {
0077     PimCommon::RecentAddressWidget w;
0078     auto lineedit = w.findChild<QLineEdit *>(QStringLiteral("line_edit"));
0079     auto newButton = w.findChild<QToolButton *>(QStringLiteral("new_button"));
0080 
0081     auto listview = w.findChild<QListWidget *>(QStringLiteral("list_view"));
0082     QCOMPARE(listview->count(), 0);
0083 
0084     lineedit->setText(QStringLiteral("foo"));
0085     QVERIFY(newButton->isEnabled());
0086     QTest::mouseClick(newButton, Qt::LeftButton);
0087     QCOMPARE(listview->count(), 1);
0088 
0089     QTest::mouseClick(newButton, Qt::LeftButton);
0090     QCOMPARE(listview->count(), 1);
0091 }
0092 
0093 void RecentAddressWidgetTest::shouldNotAddEmptyLine()
0094 {
0095     PimCommon::RecentAddressWidget w;
0096     auto lineedit = w.findChild<QLineEdit *>(QStringLiteral("line_edit"));
0097     auto newButton = w.findChild<QToolButton *>(QStringLiteral("new_button"));
0098     auto listview = w.findChild<QListWidget *>(QStringLiteral("list_view"));
0099     QCOMPARE(listview->count(), 0);
0100     QVERIFY(lineedit->text().isEmpty());
0101     QVERIFY(!newButton->isEnabled());
0102 
0103     QTest::mouseClick(newButton, Qt::LeftButton);
0104     QCOMPARE(listview->count(), 0);
0105 
0106     QTest::mouseClick(newButton, Qt::LeftButton);
0107     QCOMPARE(listview->count(), 0);
0108 
0109     lineedit->setText(QStringLiteral("foo"));
0110     QTest::mouseClick(newButton, Qt::LeftButton);
0111     QCOMPARE(listview->count(), 1);
0112 }
0113 
0114 void RecentAddressWidgetTest::shouldDisableAddButton()
0115 {
0116     PimCommon::RecentAddressWidget w;
0117     auto lineedit = w.findChild<QLineEdit *>(QStringLiteral("line_edit"));
0118     auto newButton = w.findChild<QToolButton *>(QStringLiteral("new_button"));
0119 
0120     lineedit->setText(QStringLiteral("foo"));
0121     QVERIFY(newButton->isEnabled());
0122     lineedit->clear();
0123     QVERIFY(!newButton->isEnabled());
0124 }
0125 
0126 QTEST_MAIN(RecentAddressWidgetTest)
0127 
0128 #include "moc_recentaddresswidgettest.cpp"