File indexing completed on 2024-11-24 04:50:40

0001 // SPDX-FileCopyrightText: (C) 2023 Anant Verma <vermainfinite@gmail.com>
0002 // SPDX-License-Identifier: BSD-2-Clause
0003 
0004 #include "../addressmodel.h"
0005 #include <KContacts/Address>
0006 #include <KLocalizedString>
0007 #include <QObject>
0008 #include <QTest>
0009 
0010 class AddressModelTest : public QObject
0011 {
0012     Q_OBJECT
0013 
0014 private Q_SLOTS:
0015     void initTestCase()
0016     {
0017     }
0018 
0019     void testReading()
0020     {
0021         // Create some addresses
0022         KContacts::Address address1;
0023         address1.setCountry(QStringLiteral("India"));
0024         address1.setLabel(QStringLiteral("India"));
0025 
0026         KContacts::Address address2;
0027         KContacts::Geo geo;
0028         geo.setLatitude(35.6762);
0029         geo.setLongitude(139.6503);
0030         address2.setCountry(QStringLiteral("Japan"));
0031         address2.setLocality(QStringLiteral("Tokyo"));
0032         address2.setLabel(QStringLiteral("Tokyo"));
0033         address2.setGeo(geo);
0034 
0035         // append to addressList
0036         KContacts::Address::List addressList;
0037 
0038         addressList.append(address1);
0039         addressList.append(address2);
0040 
0041         // store it in AddressModel
0042         AddressModel addressModel;
0043         addressModel.setAddresses(addressList);
0044 
0045         // run tests
0046         QCOMPARE(addressModel.rowCount(), 2);
0047         QCOMPARE(addressModel.data(addressModel.index(0, 0), AddressModel::CountryRole).toString(), QStringLiteral("India"));
0048         QCOMPARE(addressModel.data(addressModel.index(0, 0), AddressModel::IsEmptyRole).toBool(), false);
0049         QCOMPARE(addressModel.data(addressModel.index(0, 0), AddressModel::ExtendedRole), QString());
0050         QCOMPARE(addressModel.data(addressModel.index(0, 0), AddressModel::PostalCodeRole), QString());
0051 
0052         QCOMPARE(addressModel.data(addressModel.index(1, 0), AddressModel::FormattedAddressRole), QStringLiteral("Tokyo\nJapan"));
0053         QCOMPARE(addressModel.data(addressModel.index(1, 0), AddressModel::LongitudeRole).toInt(), 140);
0054         QCOMPARE(addressModel.data(addressModel.index(1, 0), AddressModel::IsEmptyRole).toBool(), false);
0055     }
0056 };
0057 
0058 QTEST_MAIN(AddressModelTest)
0059 #include "addressmodeltest.moc"