File indexing completed on 2024-11-24 04:43:03

0001 /*
0002    SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "mergecontactwidgettest.h"
0008 #include "../manualmerge/mergecontactwidget.h"
0009 #include <Akonadi/EntityTreeModel>
0010 
0011 #include <QListWidget>
0012 #include <QPushButton>
0013 #include <QSignalSpy>
0014 #include <QSplitter>
0015 #include <QStandardItemModel>
0016 #include <QTest>
0017 #include <qtestmouse.h>
0018 
0019 #include "widgets/mergecontactinfowidget.h"
0020 #include "widgets/mergecontactloseinformationwarning.h"
0021 using namespace KABMergeContacts;
0022 
0023 namespace KABMergeContacts
0024 {
0025 extern KADDRESSBOOKMERGELIB_EXPORT QAbstractItemModel *_k_mergeStubModel;
0026 }
0027 
0028 MergeContactWidgetTest::MergeContactWidgetTest()
0029 {
0030     qRegisterMetaType<Akonadi::Item::List>();
0031     qRegisterMetaType<Akonadi::Item>();
0032     qRegisterMetaType<Akonadi::Collection>();
0033 
0034     auto model = new QStandardItemModel;
0035     for (int id = 42; id < 51; ++id) {
0036         Akonadi::Collection collection(id);
0037         collection.setRights(Akonadi::Collection::AllRights);
0038         collection.setName(QString::number(id));
0039         collection.setContentMimeTypes(QStringList() << KContacts::Addressee::mimeType());
0040 
0041         auto item = new QStandardItem(collection.name());
0042         item->setData(QVariant::fromValue(collection), Akonadi::EntityTreeModel::CollectionRole);
0043         item->setData(QVariant::fromValue(collection.id()), Akonadi::EntityTreeModel::CollectionIdRole);
0044 
0045         model->appendRow(item);
0046     }
0047     KABMergeContacts::_k_mergeStubModel = model;
0048 }
0049 
0050 Akonadi::Item::List MergeContactWidgetTest::createItems()
0051 {
0052     Akonadi::Item::List lst;
0053     lst.reserve(10);
0054     for (int i = 0; i < 10; ++i) {
0055         Akonadi::Item item(i);
0056         KContacts::Addressee address;
0057         item.setPayload(address);
0058         lst.append(item);
0059     }
0060     return lst;
0061 }
0062 
0063 void MergeContactWidgetTest::shouldHaveDefaultValueOnCreation()
0064 {
0065     MergeContactWidget mergeWidget;
0066     auto listWidget = mergeWidget.findChild<QListWidget *>(QStringLiteral("listcontact"));
0067     QVERIFY(listWidget);
0068     QCOMPARE(listWidget->count(), 0);
0069     auto button = mergeWidget.findChild<QPushButton *>(QStringLiteral("mergebutton"));
0070     QVERIFY(button);
0071     QCOMPARE(button->isEnabled(), false);
0072     auto warningWidget = mergeWidget.findChild<MergeContactLoseInformationWarning *>(QStringLiteral("mergecontactwarning"));
0073     QVERIFY(warningWidget);
0074     QVERIFY(warningWidget->isHidden());
0075 
0076     auto splitter = mergeWidget.findChild<QSplitter *>(QStringLiteral("splitter"));
0077     QVERIFY(splitter);
0078     QVERIFY(!splitter->childrenCollapsible());
0079     for (int i = 0; i < splitter->count(); ++i) {
0080         const QString objName = splitter->widget(i)->objectName();
0081 
0082         const bool hasName = (objName == QLatin1StringView("selectcontactwidget")) || (objName == QLatin1StringView("mergecontactinfowidget"));
0083         QVERIFY(hasName);
0084     }
0085 }
0086 
0087 void MergeContactWidgetTest::shouldFillList()
0088 {
0089     MergeContactWidget mergeWidget;
0090     mergeWidget.setContacts(createItems());
0091     auto listWidget = mergeWidget.findChild<QListWidget *>(QStringLiteral("listcontact"));
0092     QCOMPARE(listWidget->count(), 10);
0093     QCOMPARE(listWidget->selectedItems().count(), 0);
0094     auto button = mergeWidget.findChild<QPushButton *>(QStringLiteral("mergebutton"));
0095     QCOMPARE(button->isEnabled(), false);
0096 }
0097 
0098 void MergeContactWidgetTest::shouldFillListWithValidItem()
0099 {
0100     Akonadi::Item::List lst = createItems();
0101     QCOMPARE(lst.count(), 10);
0102     lst.append(Akonadi::Item(25));
0103     MergeContactWidget mergeWidget;
0104     mergeWidget.setContacts(lst);
0105     auto listWidget = mergeWidget.findChild<QListWidget *>(QStringLiteral("listcontact"));
0106     QCOMPARE(listWidget->count(), 10);
0107 }
0108 
0109 void MergeContactWidgetTest::shouldEnableButton()
0110 {
0111     MergeContactWidget mergeWidget;
0112     mergeWidget.setContacts(createItems());
0113     auto listWidget = mergeWidget.findChild<QListWidget *>(QStringLiteral("listcontact"));
0114     auto button = mergeWidget.findChild<QPushButton *>(QStringLiteral("mergebutton"));
0115     mergeWidget.show();
0116     QVERIFY(QTest::qWaitForWindowExposed(&mergeWidget));
0117     listWidget->item(0)->setCheckState(Qt::Checked);
0118     listWidget->item(1)->setCheckState(Qt::Checked);
0119     QCOMPARE(button->isEnabled(), true);
0120 }
0121 
0122 void MergeContactWidgetTest::shouldEmitSignalsWhenThereIsElementSelected()
0123 {
0124 #if 0 // FIXME
0125     MergeContactWidget mergeWidget;
0126     mergeWidget.setContacts(createItems());
0127     QListWidget *listWidget = mergeWidget.findChild<QListWidget *>(QStringLiteral("listcontact"));
0128     QPushButton *button = mergeWidget.findChild<QPushButton *>(QStringLiteral("mergebutton"));
0129     mergeWidget.show();
0130     QVERIFY(QTest::qWaitForWindowExposed(&mergeWidget));
0131     listWidget->item(0)->setCheckState(Qt::Checked);
0132     listWidget->item(1)->setCheckState(Qt::Checked);
0133     QSignalSpy spy(&mergeWidget, SIGNAL(mergeContact(Akonadi::Item::List,Akonadi::Collection)));
0134     QTest::mouseClick(button, Qt::LeftButton);
0135     QCOMPARE(spy.count(), 1);
0136     listWidget->item(1)->setCheckState(Qt::Unchecked);
0137     QTest::mouseClick(button, Qt::LeftButton);
0138     QCOMPARE(spy.count(), 1); //No new signal emitted when we are not 2 items checked
0139 #endif
0140 }
0141 
0142 void MergeContactWidgetTest::shouldEmitSignalsWhenThereIsTwoElementsSelected()
0143 {
0144 #if 0 // FIXME
0145     MergeContactWidget mergeWidget;
0146     mergeWidget.setContacts(createItems());
0147     QListWidget *listWidget = mergeWidget.findChild<QListWidget *>(QStringLiteral("listcontact"));
0148     QPushButton *button = mergeWidget.findChild<QPushButton *>(QStringLiteral("mergebutton"));
0149     mergeWidget.show();
0150     QVERIFY(QTest::qWaitForWindowExposed(&mergeWidget));
0151     listWidget->item(0)->setCheckState(Qt::Checked);
0152     QSignalSpy spy(&mergeWidget, SIGNAL(mergeContact(Akonadi::Item::List,Akonadi::Collection)));
0153     QTest::mouseClick(button, Qt::LeftButton);
0154     QCOMPARE(spy.count(), 0);
0155     listWidget->item(1)->setCheckState(Qt::Checked);
0156     QTest::mouseClick(button, Qt::LeftButton);
0157     QCOMPARE(spy.count(), 1);
0158 #endif
0159 }
0160 
0161 void MergeContactWidgetTest::shouldEmitSignalsWhenSelectContact()
0162 {
0163 #if 0 // FIXME
0164     MergeContactWidget mergeWidget;
0165     mergeWidget.setContacts(createItems());
0166     QListWidget *listWidget = mergeWidget.findChild<QListWidget *>(QStringLiteral("listcontact"));
0167     mergeWidget.show();
0168     QSignalSpy spy(&mergeWidget, SIGNAL(contactSelected(Akonadi::Item)));
0169     listWidget->item(1)->setSelected(true);
0170     QCOMPARE(spy.count(), 1);
0171 
0172     listWidget->clearSelection();
0173     QCOMPARE(spy.count(), 2);
0174 #endif
0175 }
0176 
0177 QTEST_MAIN(MergeContactWidgetTest)
0178 
0179 #include "moc_mergecontactwidgettest.cpp"