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

0001 // SPDX-FileCopyrightText: (C) 2023 Aakarsh MJ <mj.akarsh@gmail.com>
0002 // SPDX-License-Identifier: BSD-2-Clause
0003 
0004 #include "../phonemodel.h"
0005 #include <KContacts/PhoneNumber>
0006 #include <KLocalizedString>
0007 #include <QObject>
0008 #include <QTest>
0009 
0010 class PhoneModelTest : public QObject
0011 {
0012     Q_OBJECT
0013 
0014 private Q_SLOTS:
0015     void initTestCase()
0016     {
0017     }
0018 
0019     void testReading()
0020     {
0021         KContacts::Addressee addressee;
0022         KContacts::PhoneNumber::List phonenumbers;
0023         phonenumbers.append(KContacts::PhoneNumber(QStringLiteral("+49 721 605605-0")));
0024         phonenumbers.append(KContacts::PhoneNumber(QStringLiteral("+91 80 2361 1974")));
0025 
0026         // Creating the phoneModel and loading the initial data
0027         addressee.setPhoneNumbers(phonenumbers);
0028         PhoneModel phoneModel;
0029         phoneModel.loadContact(addressee);
0030         QModelIndex zeroIndex = phoneModel.index(0, 0);
0031         int role = PhoneModel::PhoneNumberRole;
0032         QString phoneNumber1 = phoneModel.data(zeroIndex, role).toString();
0033 
0034         QCOMPARE(phoneModel.rowCount(), 2);
0035         QCOMPARE(phoneNumber1, QStringLiteral("+49 721 605605-0"));
0036 
0037         // Adding a new phone number
0038         phonenumbers.append(KContacts::PhoneNumber(QStringLiteral("+34 691 86 06 75")));
0039         addressee.setPhoneNumbers(phonenumbers);
0040         phoneModel.loadContact(addressee);
0041         QModelIndex secondIndex = phoneModel.index(2, 0);
0042         QString phoneNumber3 = phoneModel.data(secondIndex, role).toString();
0043 
0044         QCOMPARE(phoneModel.rowCount(), 3);
0045         QCOMPARE(phoneNumber3, QStringLiteral("+34 691 86 06 75"));
0046 
0047         // Deleting a phone number
0048         phonenumbers.remove(1);
0049         addressee.setPhoneNumbers(phonenumbers);
0050         phoneModel.loadContact(addressee);
0051         QModelIndex firstIndex = phoneModel.index(1, 0);
0052         QString phoneNumber2 = phoneModel.data(firstIndex, role).toString();
0053 
0054         QCOMPARE(phoneModel.rowCount(), 2);
0055         QCOMPARE(phoneNumber1, QStringLiteral("+49 721 605605-0"));
0056         QCOMPARE(phoneNumber2, QStringLiteral("+34 691 86 06 75"));
0057 
0058         // Modifying a phone number
0059         phonenumbers.replace(0, QStringLiteral("+44 203 514 2299"));
0060         addressee.setPhoneNumbers(phonenumbers);
0061         phoneModel.loadContact(addressee);
0062         phoneNumber1 = phoneModel.data(zeroIndex, role).toString();
0063 
0064         QCOMPARE(phoneNumber1, QStringLiteral("+44 203 514 2299"));
0065     }
0066 };
0067 
0068 QTEST_MAIN(PhoneModelTest)
0069 #include "phonemodeltest.moc"