~brendan-donegan/address-book-app/contacts_dbus_service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import dbus
import dbusmock
import subprocess

from tests.autopilot.address_book_app import (
    ContactsDbusService,
    DBUS_NAME_PIM,
    DBUS_OBJECT_ADDRESS_BOOK,
    DBUS_IFACE_ADDRESS_BOOK,
    DBUS_IFACE_ADDRESS_BOOKVIEW,
)

TEST_VCARD1 = """
BEGIN:VCARD
VERSION:3.0
UID:0123456789abcdef
CLIENTPIDMAP;PID=system-address-book.pas-id-53A8071F00000000:Personal
N;PID=1.1:Ubuntu;;;;
FN;PID=1.1:Ubuntu
X-QTPROJECT-FAVORITE;PID=1.1:false;0
CATEGORIES:UBUNTU
TAG:UBUNTU
END:VCARD
"""

TEST_VCARD2 = """
BEGIN:VCARD
VERSION:3.0
UID:fedcba9876543210
CLIENTPIDMAP;PID=system-address-book.pas-id-53A8071F00000000:Personal
N;PID=1.1:XUbuntu;;;;
FN;PID=1.1:XUbuntu
X-QTPROJECT-FAVORITE;PID=1.1:false;0
CATEGORIES:UBUNTU
TAG:UBUNTU
END:VCARD
"""

TEST_VIEW_OBJECT = '/com/canonical/pim/AddressBookView/1'


class TestContactsDbusService(dbusmock.DBusTestCase):

    @classmethod
    def setUpClass(cls):
        cls.start_session_bus()
        cls.dbus_con = cls.get_dbus()

    def setUp(self):
        self.p_mock = self.spawn_server(DBUS_NAME_PIM,
                                        DBUS_OBJECT_ADDRESS_BOOK,
                                        DBUS_IFACE_ADDRESS_BOOK,
                                        stdout=subprocess.PIPE)
        self.dbus_contacts_mock = dbus.Interface(self.dbus_con.get_object(
            DBUS_NAME_PIM,
            DBUS_OBJECT_ADDRESS_BOOK),
            dbusmock.MOCK_IFACE)
        self.dbus_contacts_mock.AddMethod('', 'createContact', 'ss', 's',
                                          'ret = "CONTACT"')
        self.dbus_contacts_mock.AddMethod('', 'removeContacts', 'as', 'i',
                                          'ret = 1')
        self.dbus_contacts_mock.AddMethod('', 'updateContacts', 'as', 's',
                                          'ret = "UPDATED CONTACT"')
        self.dbus_contacts_mock.AddMethod('', 'query', 'ssas', 's',
                                          'ret = "{}"'.format(
                                              TEST_VIEW_OBJECT))
        self.dbus_contacts_mock.AddObject(TEST_VIEW_OBJECT,
                                          DBUS_IFACE_ADDRESS_BOOKVIEW,
                                          '', '')
        self.dbus_contactsview_mock = dbus.Interface(self.dbus_con.get_object(
                                                     DBUS_NAME_PIM,
                                                     TEST_VIEW_OBJECT),
                                                     dbusmock.MOCK_IFACE)
        self.dbus_contactsview_mock.AddMethod(
            '', 'contactsDetails',
            'asii', 'as', 'ret = ["""{}""", """{}"""]'.format(
                TEST_VCARD1, TEST_VCARD2))
        self.dbus_contactsview_mock.AddMethod('', 'close', '', '', '')
        self.contacts_service = ContactsDbusService()

    def tearDown(self):
        self.p_mock.terminate()
        self.p_mock.wait()

    def test_create_contact(self):
        self.contacts_service.create_contact('VCARD')
        expected_args = ['VCARD', '']
        # Check that createContact was called with the expected arguments
        self.assertEqual(self.dbus_contacts_mock.GetMethodCalls(
            'createContact')[0][1], expected_args)

    def test_delete_contact(self):
        self.contacts_service.delete_contact(['ID'])
        expected_args = [['ID']]
        self.assertEqual(self.dbus_contacts_mock.GetMethodCalls(
            'removeContacts')[0][1], expected_args)

    def test_update_contact(self):
        self.contacts_service.update_contact('VCARD')
        expected_args = [['VCARD']]
        self.assertEqual(self.dbus_contacts_mock.GetMethodCalls(
            'updateContacts')[0][1], expected_args)

    def test_get_vcard_uid(self):
        vcard_uid = self.contacts_service.get_vcard_uid(TEST_VCARD1)
        self.assertEqual(vcard_uid, '0123456789abcdef')

    def test_query_contacts(self):
        contacts = self.contacts_service.query_contacts()
        expected_args = ['', '', []]
        self.assertEqual(self.dbus_contacts_mock.GetMethodCalls(
            'query')[0][1], expected_args)
        self.assertEqual(contacts, [TEST_VCARD1, TEST_VCARD2])

    def test_delete_all_contacts(self):
        expected_uids = [self.contacts_service.get_vcard_uid(TEST_VCARD1),
                         self.contacts_service.get_vcard_uid(TEST_VCARD2)]
        self.contacts_service.delete_all_contacts()
        self.assertCountEqual(self.dbus_contacts_mock.GetMethodCalls(
            'removeContacts')[0][1][0], expected_uids)