~pkunal-parmar/ubuntu-calendar-app/ICalImport

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Copyright (C) 2013, 2014 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Calendar app autopilot emulators."""

import logging
from time import sleep

import autopilot.logging
from dateutil import tz

import ubuntuuitoolkit
from ubuntuuitoolkit import (
    emulators as toolkit_emulators,
    pickers
)

from calendar_app import data


logger = logging.getLogger(__name__)


class CalendarException(ubuntuuitoolkit.ToolkitException):

    """Exception raised when there are problems with the Calendar."""


# for now we are borrowing the textfield helper for the textarea
# once the toolkit has a textarea helper this should be removed
# https://bugs.launchpad.net/ubuntu-ui-toolkit/+bug/1327354
class TextArea(toolkit_emulators.TextField):
    """Autopilot helper for the TextArea component."""


class MainView(toolkit_emulators.MainView):

    """An emulator that makes it easy to interact with the calendar-app."""

    @autopilot.logging.log_action(logger.info)
    def go_to_month_view(self):
        """Open the month view.

        :return: The Month View page.

        """
        month_tab = self.select_single('Tab', objectName='monthTab')
        if not month_tab.visible:
            self.switch_to_tab('monthTab')
        else:
            logger.debug('The month View page is already opened.')
        return self.get_month_view(month_tab)

    @autopilot.logging.log_action(logger.info)
    def go_to_week_view(self):
        """Open the week view.

        :return: The Week View page.

        """
        week_tab = self.select_single('Tab', objectName='weekTab')
        if not week_tab.visible:
            self.switch_to_tab('weekTab')
        else:
            logger.debug('The week View page is already opened.')
        return self.get_week_view(week_tab)

    @autopilot.logging.log_action(logger.info)
    def go_to_year_view(self):
        """Open the year view.

        :return: The Year View page.

        """
        year_tab = self.select_single('Tab', objectName='yearTab')
        if not year_tab.visible:
            self.switch_to_tab('yearTab')
        else:
            logger.debug('The Year View page is already opened.')
        return self.get_year_view(year_tab)

    @autopilot.logging.log_action(logger.info)
    def go_to_day_view(self):
        """Open the day view.

        :return: The Day View page.

        """
        day_tab = self.select_single('Tab', objectName='dayTab')
        if not day_tab.visible:
            self.switch_to_tab('dayTab')
        else:
            logger.debug('The Day View page is already opened.')
        return self.get_day_view(day_tab)

    @autopilot.logging.log_action(logger.info)
    def go_to_new_event(self):
        """Open the page to add a new event.

        :return: The New Event page.

        """
        header = self.get_header()
        header.click_action_button('neweventbutton')
        return self.select_single(NewEvent, objectName='newEventPage')

    def set_picker(self, field, mode, value):
        # open picker
        self.pointing_device.click_object(field)
        # valid options are date or time; assume date if invalid/no option
        if mode == 'time':
            mode_value = 'Hours|Minutes'
        else:
            mode_value = 'Years|Months|Days'
        picker = self.wait_select_single(
            pickers.DatePicker, mode=mode_value, visible=True)
        if mode_value == 'Hours|Minutes':
            picker.pick_time(value)
        else:
            picker.pick_date(value)
        # close picker
        self.pointing_device.click_object(field)

    def get_event_view(self, parent_object=None):
        if parent_object is None:
            parent_object = self
        return parent_object.select_single("EventView")

    def get_month_view(self, parent_object=None):
        if parent_object is None:
            parent_object = self
        return parent_object.select_single(MonthView,
                                           objectName='monthViewPage')

    def get_year_view(self, parent_object=None):
        if parent_object is None:
            parent_object = self
        return parent_object.select_single(YearView, objectName='yearViewPage')

    def get_day_view(self, parent_object=None):
        if parent_object is None:
            parent_object = self
        return parent_object.select_single(DayView, objectName='dayViewPage')

    def get_week_view(self, parent_object=None):
        if parent_object is None:
            parent_object = self
        return parent_object.select_single(WeekView, objectName='weekViewPage')

    def get_label_with_text(self, text, root=None):
        if root is None:
            root = self
        labels = root.select_many("Label", text=text)
        if (len(labels) > 0):
            return labels[0]
        else:
            return None

    def get_year(self, component):
        return int(component.wait_select_single(
            "Label", objectName="yearLabel").text)

    def get_month_name(self, component):
        return component.wait_select_single(
            "Label", objectName="monthLabel").text

    def safe_swipe_view(self, direction, view, date):
        """
        direction: direction to swip
        view: the view you are swiping against
        date: a function object of the view
        """
        timeout = 0
        before = date
        # try up to 3 times to swipe
        while timeout < 3 and date == before:
            self._swipe(direction, view)
            # check for up to 3 seconds after swipe for view
            # to have changed before trying again
            for x in range(0, 3):
                if date != before:
                    break
                sleep(1)
            timeout += 1

    def swipe_view(self, direction, view, x_pad=0.15):
        """Swipe the given view to left or right.

        Args:
            direction: if 1 it swipes from right to left, if -1 from
                left right.

        """

        start = (-direction * x_pad) % 1
        stop = (direction * x_pad) % 1

        y_line = view.globalRect[1] + view.globalRect[3] / 2
        x_start = view.globalRect[0] + view.globalRect[2] * start
        x_stop = view.globalRect[0] + view.globalRect[2] * stop

        self.pointing_device.drag(x_start, y_line, x_stop, y_line)

    def to_local_date(self, date):
        utc = date.replace(tzinfo=tz.tzutc())
        local = utc.astimezone(tz.tzlocal())
        return local


class YearView(toolkit_emulators.UbuntuUIToolkitEmulatorBase):

    """Autopilot helper for the Year View page."""


class WeekView(toolkit_emulators.UbuntuUIToolkitEmulatorBase):

    """Autopilot helper for the Week View page."""


class MonthView(toolkit_emulators.UbuntuUIToolkitEmulatorBase):

    """Autopilot helper for the Year View page."""


class DayView(toolkit_emulators.UbuntuUIToolkitEmulatorBase):

    """Autopilot helper for the Day View page."""

    def get_events(self, filter_duplicates=False):
        """Return the events for this day.

        :return: A list with the events. Each event is a tuple with name, start
           time and end time.

        """
        event_bubbles = self._get_selected_day_event_bubbles(filter_duplicates)

        # sort by y, x
        event_bubbles = sorted(
            event_bubbles,
            key=lambda bubble: (bubble.globalRect.y, bubble.globalRect.x))

        events = []
        for event in event_bubbles:
            events.append(event.get_information())

        return events

    def _get_current_day_component(self):
        components = self.select_many('TimeLineBaseComponent')
        for component in components:
            if (self.currentDay.datetime.date() ==
                    component.startDay.datetime.date()):
                return component
        else:
            raise CalendarException(
                'Could not find the current day component.')

    def _get_selected_day_event_bubbles(self, filter_duplicates):
        selected_day = self._get_current_day_component()
        return self._get_event_bubbles(selected_day, filter_duplicates)

    def _get_event_bubbles(self, selected_day, filter_duplicates):
        event_bubbles = selected_day.select_many(EventBubble)
        if filter_duplicates:
            # XXX remove this once bug http://pad.lv/1334833 is fixed.
            # --elopio - 2014-06-26
            separator_id = selected_day.select_single(
                'QQuickRectangle', objectName='separator').id
            event_bubbles = self._remove_duplicate_events(
                separator_id, event_bubbles)
        return event_bubbles

    def _remove_duplicate_events(self, separator_id, event_bubbles):
        events = []
        for bubble in event_bubbles:
            if bubble.id > separator_id:
                events.append(bubble)

        return events

    @autopilot.logging.log_action(logger.info)
    def open_event(self, name, filter_duplicates=False):
        """Open an event.

        :param name: The name of the event to open.
        :return: The Event Details page.

        """
        event_bubbles = self._get_selected_day_event_bubbles(filter_duplicates)
        for bubble in event_bubbles:
            if bubble.get_name() == name:
                return bubble.open_event()
        else:
            raise CalendarException(
                'Could not find event with name {}.'.format(name))

    @autopilot.logging.log_action(logger.info)
    def delete_event(self, name, filter_duplicates=False):
        """Delete an event.

        :param name: The name of the event to delete.
        :return: The Day View page.

        """
        event_details_page = self.open_event(name, filter_duplicates)
        return event_details_page.delete()

    @autopilot.logging.log_action(logger.info)
    def get_day_header(self, day=None):
        """Return the dayheader for a given day. If no day is given,
        return the current day.

        :param day: A datetime object matching the header
        :return: The day header object
        """
        if day:
            headers = self.select_many('TimeLineHeaderComponent')
            for header in headers:
                if header.startDay.datetime == day:
                    day_header = header
                    break
        else:
            # just grab the current day
            day_header = self.wait_select_single(
                'TimeLineHeaderComponent', isCurrentItem=True)

        if not(day_header):
            raise CalendarException('Day Header not found for %s' % day)
        return day_header


class EventBubble(toolkit_emulators.UbuntuUIToolkitEmulatorBase):

    """Autopiot helper for the Event Bubble items."""

    def get_information(self):
        """Return a tuple with the name, start time and end time."""
        name = self.get_name()
        start_time, end_time = self._get_start_and_end_time()
        return name, start_time, end_time

    def _get_start_and_end_time(self):
        """Return a tuple with the start time and end time."""
        time_label = self.select_single('Label', objectName='timeLabel')
        start_time, end_time = time_label.text.split(' - ')
        return start_time, end_time

    def get_name(self):
        """Return the event name."""
        title_label = self.select_single('Label', objectName='titleLabel')
        return title_label.text

    @autopilot.logging.log_action(logger.info)
    def open_event(self):
        """Open the event.

        :return: The Event Details page.

        """
        # If there are too many events, the center of the bubble
        # might be hidden by another event. Click the left side of the
        # bubble.
        left = self.globalRect.x + 5
        center_y = self.globalRect.y + self.globalRect.height // 2
        self.pointing_device.move(left, center_y)
        self.pointing_device.click()
        return self.get_root_instance().select_single(
            EventDetails, objectName='eventDetails')


class NewEvent(toolkit_emulators.UbuntuUIToolkitEmulatorBase):

    """Autopilot helper for the New Event page."""

    @autopilot.logging.log_action(logger.info)
    def add_event(self, event_information):
        """Add a new event.

        :param event_information: Values of the event to fill the form.
        :type event_information: data object with the attributes name,
            description, location and guests.
        :return: The Day View page.

        """
        self._fill_form(event_information)
        self._save()
        return self.get_root_instance().select_single(
            DayView, objectName='dayViewPage')

    @autopilot.logging.log_action(logger.debug)
    def _fill_form(self, event_information):
        """Fill the add event form.

        :param event_information: Values of the event to fill the form.
        :type event_information: data object with the attributes name,
            description, location and guests.

        """
        # TODO fill start date and end date, is all day event, recurrence and
        # reminders. --elopio - 2014-06-26
        if event_information.name is not None:
            self._fill_name(event_information.name)
        if event_information.description is not None:
            self._fill_description(event_information.description)
        if event_information.location is not None:
            self._fill_location(event_information.location)
        if event_information.guests is not None:
            self._fill_guests(event_information.guests)

    def _fill_name(self, value):
        self._ensure_entry_field_visible_and_write('newEventName', value)

    def _ensure_entry_field_visible_and_write(self, object_name, value):
        name_text_field = self._get_new_event_entry_field(object_name)
        self._ensure_visible_and_write(name_text_field, value)

    def _get_new_event_entry_field(self, object_name):
        return self.select_single(NewEventEntryField, objectName=object_name)

    def _ensure_visible_and_write(self, text_field, value):
        text_field.swipe_into_view()
        text_field.write(value)

    def _fill_description(self, value):
        description_text_area = self._get_description_text_area()
        self._ensure_visible_and_write(description_text_area, value)

    def _get_description_text_area(self):
        return self.select_single(TextArea, objectName='eventDescriptionInput')

    def _fill_location(self, value):
        self._ensure_entry_field_visible_and_write('eventLocationInput', value)

    def _fill_guests(self, value):
        if len(value) > 1:
            # See bug http://pad.lv/1295941
            raise CalendarException(
                'It is not yet possible to add more than one guest.')
        self._ensure_entry_field_visible_and_write(
            'eventPeopleInput', value[0])

    def _get_form_values(self):
        # TODO get start date and end date, is all day event, recurrence and
        # reminders. --elopio - 2014-06-26
        name = self._get_new_event_entry_field('newEventName').text
        description = self._get_description_text_area().text
        location = self._get_new_event_entry_field('eventLocationInput').text
        # TODO once bug http://pad.lv/1295941 is fixed, we will have to build
        # the list of guests. --elopio - 2014-06-26
        guests = [self._get_new_event_entry_field('eventPeopleInput').text]
        return data.Event(name, description, location, guests)

    @autopilot.logging.log_action(logger.info)
    def _save(self):
        """Save the new event."""
        save_button = self.select_single('Button', objectName='accept')
        self.pointing_device.click_object(save_button)


class NewEventEntryField(toolkit_emulators.TextField):

    """Autopilot helper for the NewEventEntryField component."""


class EventDetails(toolkit_emulators.UbuntuUIToolkitEmulatorBase):

    """Autopilot helper for the Event Details page."""

    @autopilot.logging.log_action(logger.debug)
    def delete(self):
        """Click the delete button.

        :return: The Day View page.

        """
        root = self.get_root_instance()
        header = root.select_single(MainView).get_header()
        header.click_action_button('delete')

        delete_confirmation_dialog = root.select_single(
            DeleteConfirmationDialog, objectName='deleteConfirmationDialog')
        delete_confirmation_dialog.confirm_deletion()

        return root.select_single(DayView, objectName='dayViewPage')

    def get_event_information(self):
        """Return the information of the event."""
        name = self._get_name()
        description = self._get_description()
        location = self._get_location()
        guests = self._get_guests()
        return data.Event(name, description, location, guests)

    def _get_name(self):
        return self._get_label_text('titleLabel')

    def _get_label_text(self, object_name):
        return self.select_single('Label', objectName=object_name).text

    def _get_description(self):
        return self._get_label_text('descriptionLabel')

    def _get_location(self):
        return self._get_label_text('locationLabel')

    def _get_guests(self):
        guests = []
        contacts_list = self.select_single(
            'QQuickColumn', objectName='contactList')
        guest_labels = contacts_list.select_many('Label')
        for label in guest_labels:
            guests.append(label.text)

        return guests


class DeleteConfirmationDialog(toolkit_emulators.UbuntuUIToolkitEmulatorBase):

    """Autopilot helper for the Delete Confirmation dialog."""

    @autopilot.logging.log_action(logger.debug)
    def confirm_deletion(self):
        """Confirm the deletion of the event."""
        delete_button = self.select_single(
            'Button', objectName='deleteEventButton')
        self.pointing_device.click_object(delete_button)