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 "searchduplicateresultwidgettest.h"
0008 #include "../searchduplicate/resultduplicatetreewidget.h"
0009 #include "../searchduplicate/searchduplicateresultwidget.h"
0010 #include "../widgets/mergecontactloseinformationwarning.h"
0011 #include <Akonadi/CollectionComboBox>
0012 #include <Akonadi/EntityTreeModel>
0013 #include <Akonadi/GrantleeContactViewer>
0014 #include <KContacts/Addressee>
0015 #include <QLabel>
0016 #include <QLayout>
0017 #include <QPushButton>
0018 #include <QSplitter>
0019 #include <QStandardItemModel>
0020 #include <QTest>
0021 
0022 using namespace KContacts;
0023 namespace KABMergeContacts
0024 {
0025 QAbstractItemModel *_k_searchDuplicateResultStubModel = nullptr;
0026 }
0027 
0028 SearchDuplicateResultWidgetTest::SearchDuplicateResultWidgetTest(QObject *parent)
0029     : QObject(parent)
0030 {
0031     auto model = new QStandardItemModel;
0032     for (int id = 42; id < 51; ++id) {
0033         Akonadi::Collection collection(id);
0034         collection.setRights(Akonadi::Collection::AllRights);
0035         collection.setName(QString::number(id));
0036         collection.setContentMimeTypes(QStringList() << Addressee::mimeType());
0037 
0038         auto item = new QStandardItem(collection.name());
0039         item->setData(QVariant::fromValue(collection), Akonadi::EntityTreeModel::CollectionRole);
0040         item->setData(QVariant::fromValue(collection.id()), Akonadi::EntityTreeModel::CollectionIdRole);
0041 
0042         model->appendRow(item);
0043     }
0044     KABMergeContacts::_k_searchDuplicateResultStubModel = model;
0045 }
0046 
0047 SearchDuplicateResultWidgetTest::~SearchDuplicateResultWidgetTest() = default;
0048 
0049 void SearchDuplicateResultWidgetTest::shouldHaveDefaultValue()
0050 {
0051     KABMergeContacts::SearchDuplicateResultWidget w;
0052     QCOMPARE(w.layout()->contentsMargins(), QMargins());
0053 
0054     auto tree = w.findChild<KABMergeContacts::ResultDuplicateTreeWidget *>(QStringLiteral("result_treewidget"));
0055     QVERIFY(tree);
0056     QCOMPARE(tree->topLevelItemCount(), 0);
0057     auto splitter = w.findChild<QSplitter *>(QStringLiteral("splitter"));
0058     QVERIFY(splitter);
0059     QVERIFY(!splitter->childrenCollapsible());
0060     auto viewer = w.findChild<KAddressBookGrantlee::GrantleeContactViewer *>(QStringLiteral("contact_viewer"));
0061     QVERIFY(viewer);
0062     auto lab = w.findChild<QLabel *>(QStringLiteral("select_addressbook_label"));
0063     lab->setObjectName(QLatin1StringView("select_addressbook_label"));
0064     auto pushButton = w.findChild<QPushButton *>(QStringLiteral("merge_contact_button"));
0065     QVERIFY(pushButton);
0066     QVERIFY(!pushButton->isEnabled());
0067 
0068     auto warning = w.findChild<KABMergeContacts::MergeContactLoseInformationWarning *>(QStringLiteral("mergecontactwarning"));
0069     QVERIFY(warning);
0070 
0071     auto combobox = w.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
0072     QVERIFY(combobox);
0073 }
0074 
0075 void SearchDuplicateResultWidgetTest::shouldHaveMergeButtonEnabled()
0076 {
0077     KABMergeContacts::SearchDuplicateResultWidget w;
0078     auto tree = w.findChild<KABMergeContacts::ResultDuplicateTreeWidget *>(QStringLiteral("result_treewidget"));
0079     QVERIFY(tree);
0080     QCOMPARE(tree->topLevelItemCount(), 0);
0081     Akonadi::Item::List lst;
0082     lst << Akonadi::Item(42);
0083     lst << Akonadi::Item(43);
0084     lst << Akonadi::Item(44);
0085     // QList<Akonadi::Item::List> itemLst;
0086 #if 0 // FIXME
0087     tree->setContacts(itemLst);
0088     QVERIFY(tree->topLevelItemCount() > 0);
0089 
0090     QPushButton *pushButton = w.findChild<QPushButton *>(QStringLiteral("merge_contact_button"));
0091     QVERIFY(pushButton);
0092     QVERIFY(!pushButton->isEnabled());
0093 
0094     Akonadi::CollectionComboBox *combobox = w.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
0095     QVERIFY(combobox);
0096 #endif
0097 }
0098 
0099 QTEST_MAIN(SearchDuplicateResultWidgetTest)
0100 
0101 #include "moc_searchduplicateresultwidgettest.cpp"