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

« back to all changes in this revision

Viewing changes to tests/autopilot/address_book_app/__init__.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:
16
16
 
17
17
"""address-book-app autopilot tests and emulators - top level package."""
18
18
 
 
19
import dbus
19
20
import logging
20
 
 
 
21
import re
21
22
 
22
23
logging.basicConfig(filename='warning.log', level=logging.WARNING)
23
24
 
24
25
 
25
26
import autopilot.logging
26
27
import ubuntuuitoolkit
27
 
from autopilot.introspection import dbus
 
28
from autopilot.introspection import dbus as ap_dbus
28
29
 
29
30
from address_book_app import pages
30
31
 
66
67
        for p in contact_editor_pages:
67
68
            if p.active:
68
69
                return p
69
 
        raise dbus.StateNotFoundError('contactEditorPage not found')
 
70
        raise ap_dbus.StateNotFoundError('contactEditorPage not found')
70
71
        return None
71
72
 
72
 
 
73
73
    def get_contact_view_page(self):
74
74
        return self.wait_select_single("ContactView",
75
75
                                       objectName="contactViewPage")
148
148
        bottom_swipe_page = self.get_contact_list_page()
149
149
        bottom_swipe_page.revel_bottom_edge_page()
150
150
        return self.get_contact_edit_page()
 
151
 
 
152
 
 
153
DBUS_NAME_PIM = 'com.canonical.pim'
 
154
DBUS_IFACE_ADDRESS_BOOK = 'com.canonical.pim.AddressBook'
 
155
DBUS_IFACE_ADDRESS_BOOKVIEW = 'com.canonical.pim.AddressBookView'
 
156
DBUS_OBJECT_ADDRESS_BOOK = '/com/canonical/pim/AddressBook'
 
157
 
 
158
 
 
159
class ContactsDbusService(object):
 
160
    def __init__(self):
 
161
        self.bus = None
 
162
        self.addr = None
 
163
        self.addr_iface = None
 
164
        self.bus = dbus.SessionBus()
 
165
        self.addr = self.bus.get_object(
 
166
            DBUS_NAME_PIM, DBUS_OBJECT_ADDRESS_BOOK)
 
167
        self.addr_iface = dbus.Interface(
 
168
            self.addr, dbus_interface=DBUS_IFACE_ADDRESS_BOOK)
 
169
 
 
170
    def query_contacts(self, fields='', query='', sources=[]):
 
171
        view_path = self.addr_iface.query(fields, query, [])
 
172
        view = self.bus.get_object(
 
173
            DBUS_NAME_PIM, view_path)
 
174
        view_iface = dbus.Interface(
 
175
            view, dbus_interface=DBUS_IFACE_ADDRESS_BOOKVIEW)
 
176
        contacts = view_iface.contactsDetails([], 0, -1)
 
177
        view.close()
 
178
        return contacts
 
179
 
 
180
    def update_contact(self, vcard):
 
181
        return self.addr_iface.updateContacts([vcard])
 
182
 
 
183
    def create_contact(self, vcard):
 
184
        return self.addr_iface.createContact(vcard, "")
 
185
 
 
186
    def delete_contact(self, ids):
 
187
        return self.addr_iface.removeContacts(ids)
 
188
 
 
189
    def get_vcard_uid(self, vcard):
 
190
        """
 
191
        Return the UID of the provided vcard
 
192
        :param vcard: vcard formatted data
 
193
        :return: uid value from vcard, else None
 
194
        """
 
195
        uid_pattern = re.compile(r'^UID:(?P<uid>\S*)\r?$',
 
196
                                 re.MULTILINE | re.IGNORECASE)
 
197
        uid = None
 
198
        match = uid_pattern.search(vcard)
 
199
        if match:
 
200
            uid = match.group('uid')
 
201
        return uid
 
202
 
 
203
    def get_vcard_cell(self, vcard):
 
204
        """
 
205
        Return the mobile number of the provided vcard
 
206
        :param vcard: vcard formatted data
 
207
        :return: mobile number from vcard, else None
 
208
        """
 
209
        cell_pattern = re.compile(r'^.*TYPE=CELL,HOME:(?P<cell>\S*)\r?$',
 
210
                                  re.MULTILINE | re.IGNORECASE)
 
211
        cell = None
 
212
        match = cell_pattern.search(vcard)
 
213
        if match:
 
214
            cell = match.group('cell')
 
215
        return cell
 
216
 
 
217
    def delete_all_contacts(self):
 
218
        """
 
219
        Delete all contacts using the dbus service
 
220
        """
 
221
        uids = []
 
222
        contacts = self.query_contacts()
 
223
        for contact in contacts:
 
224
            uid = self.get_vcard_uid(contact)
 
225
            if uid:
 
226
                uids.append(uid)
 
227
        self.delete_contact(uids)