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

« back to all changes in this revision

Viewing changes to tests/autopilot/unittests/test_contacts_service.py

  • Committer: Brendan Donegan
  • Date: 2014-07-11 14:29:37 UTC
  • Revision ID: brendan.donegan@canonical.com-20140711142937-xu3qwmvniho20yng
Add ContactsDbusService and associated tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import dbus
 
2
import dbusmock
 
3
import subprocess
 
4
 
 
5
from tests.autopilot.address_book_app import (
 
6
    ContactsDbusService,
 
7
    DBUS_NAME_PIM,
 
8
    DBUS_OBJECT_ADDRESS_BOOK,
 
9
    DBUS_IFACE_ADDRESS_BOOK,
 
10
    DBUS_IFACE_ADDRESS_BOOKVIEW,
 
11
)
 
12
 
 
13
TEST_VCARD1 = """
 
14
BEGIN:VCARD
 
15
VERSION:3.0
 
16
UID:0123456789abcdef
 
17
CLIENTPIDMAP;PID=system-address-book.pas-id-53A8071F00000000:Personal
 
18
N;PID=1.1:Ubuntu;;;;
 
19
FN;PID=1.1:Ubuntu
 
20
X-QTPROJECT-FAVORITE;PID=1.1:false;0
 
21
CATEGORIES:UBUNTU
 
22
TAG:UBUNTU
 
23
END:VCARD
 
24
"""
 
25
 
 
26
TEST_VCARD2 = """
 
27
BEGIN:VCARD
 
28
VERSION:3.0
 
29
UID:fedcba9876543210
 
30
CLIENTPIDMAP;PID=system-address-book.pas-id-53A8071F00000000:Personal
 
31
N;PID=1.1:XUbuntu;;;;
 
32
FN;PID=1.1:XUbuntu
 
33
X-QTPROJECT-FAVORITE;PID=1.1:false;0
 
34
CATEGORIES:UBUNTU
 
35
TAG:UBUNTU
 
36
END:VCARD
 
37
"""
 
38
 
 
39
TEST_VIEW_OBJECT = '/com/canonical/pim/AddressBookView/1'
 
40
 
 
41
 
 
42
class TestContactsDbusService(dbusmock.DBusTestCase):
 
43
 
 
44
    @classmethod
 
45
    def setUpClass(cls):
 
46
        cls.start_session_bus()
 
47
        cls.dbus_con = cls.get_dbus()
 
48
 
 
49
    def setUp(self):
 
50
        self.p_mock = self.spawn_server(DBUS_NAME_PIM,
 
51
                                        DBUS_OBJECT_ADDRESS_BOOK,
 
52
                                        DBUS_IFACE_ADDRESS_BOOK,
 
53
                                        stdout=subprocess.PIPE)
 
54
        self.dbus_contacts_mock = dbus.Interface(self.dbus_con.get_object(
 
55
            DBUS_NAME_PIM,
 
56
            DBUS_OBJECT_ADDRESS_BOOK),
 
57
            dbusmock.MOCK_IFACE)
 
58
        self.dbus_contacts_mock.AddMethod('', 'createContact', 'ss', 's',
 
59
                                          'ret = "CONTACT"')
 
60
        self.dbus_contacts_mock.AddMethod('', 'removeContacts', 'as', 'i',
 
61
                                          'ret = 1')
 
62
        self.dbus_contacts_mock.AddMethod('', 'updateContacts', 'as', 's',
 
63
                                          'ret = "UPDATED CONTACT"')
 
64
        self.dbus_contacts_mock.AddMethod('', 'query', 'ssas', 's',
 
65
                                          'ret = "{}"'.format(
 
66
                                              TEST_VIEW_OBJECT))
 
67
        self.dbus_contacts_mock.AddObject(TEST_VIEW_OBJECT,
 
68
                                          DBUS_IFACE_ADDRESS_BOOKVIEW,
 
69
                                          '', '')
 
70
        self.dbus_contactsview_mock = dbus.Interface(self.dbus_con.get_object(
 
71
                                                     DBUS_NAME_PIM,
 
72
                                                     TEST_VIEW_OBJECT),
 
73
                                                     dbusmock.MOCK_IFACE)
 
74
        self.dbus_contactsview_mock.AddMethod(
 
75
            '', 'contactsDetails',
 
76
            'asii', 'as', 'ret = ["""{}""", """{}"""]'.format(
 
77
                TEST_VCARD1, TEST_VCARD2))
 
78
        self.dbus_contactsview_mock.AddMethod('', 'close', '', '', '')
 
79
        self.contacts_service = ContactsDbusService()
 
80
 
 
81
    def tearDown(self):
 
82
        self.p_mock.terminate()
 
83
        self.p_mock.wait()
 
84
 
 
85
    def test_create_contact(self):
 
86
        self.contacts_service.create_contact('VCARD')
 
87
        expected_args = ['VCARD', '']
 
88
        # Check that createContact was called with the expected arguments
 
89
        self.assertEqual(self.dbus_contacts_mock.GetMethodCalls(
 
90
            'createContact')[0][1], expected_args)
 
91
 
 
92
    def test_delete_contact(self):
 
93
        self.contacts_service.delete_contact(['ID'])
 
94
        expected_args = [['ID']]
 
95
        self.assertEqual(self.dbus_contacts_mock.GetMethodCalls(
 
96
            'removeContacts')[0][1], expected_args)
 
97
 
 
98
    def test_update_contact(self):
 
99
        self.contacts_service.update_contact('VCARD')
 
100
        expected_args = [['VCARD']]
 
101
        self.assertEqual(self.dbus_contacts_mock.GetMethodCalls(
 
102
            'updateContacts')[0][1], expected_args)
 
103
 
 
104
    def test_get_vcard_uid(self):
 
105
        vcard_uid = self.contacts_service.get_vcard_uid(TEST_VCARD1)
 
106
        self.assertEqual(vcard_uid, '0123456789abcdef')
 
107
 
 
108
    def test_query_contacts(self):
 
109
        contacts = self.contacts_service.query_contacts()
 
110
        expected_args = ['', '', []]
 
111
        self.assertEqual(self.dbus_contacts_mock.GetMethodCalls(
 
112
            'query')[0][1], expected_args)
 
113
        self.assertEqual(contacts, [TEST_VCARD1, TEST_VCARD2])
 
114
 
 
115
    def test_delete_all_contacts(self):
 
116
        expected_uids = [self.contacts_service.get_vcard_uid(TEST_VCARD1),
 
117
                         self.contacts_service.get_vcard_uid(TEST_VCARD2)]
 
118
        self.contacts_service.delete_all_contacts()
 
119
        self.assertCountEqual(self.dbus_contacts_mock.GetMethodCalls(
 
120
            'removeContacts')[0][1][0], expected_uids)