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

« back to all changes in this revision

Viewing changes to tests/autopilot/calendar_app/tests/test_weekview.py

  • Committer: Tarmac
  • Author(s): nskaggs
  • Date: 2015-03-05 01:29:36 UTC
  • mfrom: (590.1.9 ubuntu-calendar-app)
  • Revision ID: tarmac-20150305012936-4otnfmj15yr3md03
Fix inifinite loop in weekview tests; add testing stubs. Fixes: https://bugs.launchpad.net/bugs/1423582, https://bugs.launchpad.net/bugs/1426183.

Approved by Ubuntu Phone Apps Jenkins Bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
import datetime
28
28
from autopilot.matchers import Eventually
29
 
from testtools.matchers import Equals, NotEquals
 
29
from testtools.matchers import Equals
 
30
from random import randint, randrange
30
31
 
31
32
from calendar_app.tests import CalendarAppTestCase
32
33
import logging
40
41
        super(TestWeekView, self).setUp()
41
42
        self.week_view = self.app.main_view.go_to_week_view()
42
43
 
43
 
    def _change_week(self, direction):
44
 
        first_dow = self._get_first_day_of_week()
45
 
 
46
 
        # prevent timing issues with swiping
47
 
        old_day = self.app.main_view.to_local_date(
48
 
            self.week_view.dayStart.datetime)
49
 
 
50
 
        pathView = self.week_view.select_single("PathViewBase")
51
 
        timeLineBase = pathView.select_single("TimeLineBaseComponent",
52
 
                                              isActive=True)
53
 
        timelineview = timeLineBase.select_single(objectName="timelineview")
54
 
        val = 0
55
 
        if direction == 1:
56
 
            val = timelineview.contentWidth - timelineview.width
57
 
 
58
 
        while timelineview.contentX != val:
59
 
            self.app.main_view.swipe_view(direction, self.week_view)
60
 
 
61
 
        self.app.main_view.swipe_view(direction, self.week_view)
62
 
        self.assertThat(lambda:
63
 
                        self.app.main_view.to_local_date(
64
 
                            self.week_view.dayStart.datetime),
65
 
                        Eventually(NotEquals(old_day)))
66
 
 
67
 
        new_day_start = self.app.main_view.to_local_date(
68
 
            self.week_view.dayStart.datetime)
69
 
 
70
 
        expected_day_start = first_dow + datetime.timedelta(
71
 
            days=(7 * direction))
72
 
 
73
 
        self.assertThat(new_day_start.day, Equals(expected_day_start.day))
74
 
 
75
 
    def _get_days_of_week(self):
76
 
        # sort based on text value of the day
77
 
        days = sorted(self._get_date_label_headers(),
78
 
                      key=lambda label: label.text)
79
 
        days = [int(item.text) for item in days]
80
 
 
81
 
        # resort so beginning of next month comes after the end
82
 
        # need to support overlapping months 28,30,31 -> 1
83
 
        sorteddays = []
84
 
        for day in days:
85
 
            inserted = 0
86
 
            for index, sortday in enumerate(sorteddays):
87
 
                if day - sorteddays[index] == 1:
88
 
                    sorteddays.insert(index + 1, day)
89
 
                    inserted = 1
90
 
                    break
91
 
            if inserted == 0:
92
 
                sorteddays.insert(0, day)
93
 
        return sorteddays
94
 
 
95
 
    def _get_date_label_headers(self):
96
 
        pathView = self.week_view.select_single("PathViewBase")
97
 
        timeLineBase = pathView.select_single("TimeLineBaseComponent",
98
 
                                              isActive=True)
99
 
        viewHeader = timeLineBase.select_single(objectName="viewHeader")
100
 
        timeLineHeader = viewHeader.select_single(objectName="timelineHeader")
101
 
        dateLabels = timeLineHeader.select_many("Label",
102
 
                                                objectName="dateLabel")
103
 
        return dateLabels
104
 
 
105
 
    def _get_first_day_of_week(self):
106
 
        date = self.app.main_view.to_local_date(
107
 
            self.week_view.dayStart.datetime)
108
 
        firstDay = self.app.main_view.to_local_date(
109
 
            self.week_view.firstDay.datetime)
110
 
 
111
 
        # sunday
112
 
        if firstDay.weekday() == 6:
113
 
            logger.debug("Locale has Sunday as first day of week")
114
 
            weekday = date.weekday()
115
 
            diff = datetime.timedelta(days=weekday + 1)
116
 
        # saturday
117
 
        elif firstDay.weekday() == 5:
118
 
            logger.debug("Locale has Saturday as first day of week")
119
 
            weekday = date.weekday()
120
 
            diff = datetime.timedelta(days=weekday + 2)
121
 
        # monday
122
 
        else:
123
 
            logger.debug("Locale has Monday as first day of week")
124
 
            weekday = date.weekday()
125
 
            diff = datetime.timedelta(days=weekday)
126
 
 
127
 
        # set the start of week
128
 
        if date.day != firstDay.day:
129
 
            day_start = date - diff
130
 
            logger.debug("Setting day_start to %s" % firstDay.day)
131
 
        else:
132
 
            day_start = date
133
 
            logger.debug("Using today as day_start %s" % date)
134
 
        return day_start
135
 
 
136
 
    def test_current_month_and_year_is_selected(self):
137
 
        """By default, the week view shows the current month and year."""
 
44
    def _assert_week_delta(self, original_week, delta):
 
45
        current_week = self.week_view.get_current_weeknumber()
 
46
        expected_week = original_week + delta
 
47
 
 
48
        if expected_week < 0:
 
49
            expected_week += 53
 
50
        elif expected_week > 52:
 
51
            expected_week -= 53
 
52
 
 
53
        self.assertEquals(current_week, expected_week)
 
54
 
 
55
    def test_default_view(self):
 
56
        """By default, the week view shows the current week.
 
57
        It also displays the current year and month"""
138
58
 
139
59
        now = datetime.datetime.now()
140
 
 
141
60
        expected_month_name_year = now.strftime("%B %Y")
142
 
 
143
61
        self.assertThat(self.app.main_view.get_month_year(self.week_view),
144
62
                        Equals(expected_month_name_year))
145
63
 
146
 
    def test_current_week_is_selected(self):
147
 
        """By default, the week view shows the current week."""
148
 
 
149
 
        now = datetime.datetime.now()
150
 
        days = self._get_days_of_week()
151
 
        day_headers = self._get_date_label_headers()
152
 
 
153
 
        first_dow = self._get_first_day_of_week()
154
 
 
155
 
        for i in range(7):
156
 
            current_day = days[i]
157
 
            expected_day = (first_dow + datetime.timedelta(days=i)).day
158
 
 
159
 
            self.assertThat(current_day, Equals(expected_day))
160
 
 
161
 
            # current day is highlighted in white.
162
 
            # days returned by AP are out of order, so check header and today
163
 
            color = day_headers[i].color
164
 
            label_color = (color[0], color[1], color[2], color[3])
165
 
            if label_color == (255, 255, 255, 255):
166
 
                self.assertThat(int(day_headers[i].text), Equals(now.day))
167
 
 
168
 
    def test_show_next_weeks(self):
169
 
        """It must be possible to show next weeks by swiping the view."""
170
 
        for i in range(6):
171
 
            self._change_week(1)
172
 
 
173
 
    def test_show_previous_weeks(self):
174
 
        """It must be possible to show previous weeks by swiping the view."""
175
 
        for i in range(6):
176
 
            self._change_week(-1)
 
64
        # TODO: check current day is highlighted
 
65
 
 
66
    # These testing stubs need completed
 
67
    # def test_scroll_week_must_scroll_within_week(self):
 
68
        # """Scrolling inside the timeline should scroll the weekdays"""
 
69
        # pass
 
70
 
 
71
    # def test_change_week_across_month(self):
 
72
        # """Changing week across months should update the month"""
 
73
        # pass
 
74
 
 
75
    # def test_change_week_across_year(self):
 
76
        # """Changing week across years should update the year"""
 
77
        # pass
 
78
 
 
79
    # def test_month_to_week(self):
 
80
        # """Changing from a month to weekview should
 
81
        # start weekview on the first week of the month"""
 
82
        # pass
 
83
 
 
84
    # def test_day_to_week(self):
 
85
        # """Changing from a day to weekview should
 
86
        # start weekview on the same week as the day"""
 
87
        # pass
 
88
 
 
89
    def test_change_week(self):
 
90
        """It must be possible to change weeks by swiping the timeline"""
 
91
        weeks = randint(1, 6)
 
92
        direction = randrange(-1, 1, 2)
 
93
        delta = weeks * direction
 
94
        original_week = self.week_view.get_current_weeknumber()
 
95
 
 
96
        self.week_view.change_week(delta)
 
97
        self._assert_week_delta(original_week, delta)
177
98
 
178
99
    def test_selecting_a_day_switches_to_day_view(self):
179
100
        """It must be possible to show a single day by clicking on it."""
180
 
        first_day_date = self.week_view.firstDay
181
 
        # expected_day = first_day_date.day
182
 
        expected_month = first_day_date.month
183
 
        expected_year = first_day_date.year
184
 
 
185
 
        days = self._get_days_of_week()
 
101
        days = self.week_view.get_days_of_week()
186
102
        day_to_select = self.app.main_view.get_label_with_text(days[0])
 
103
        expected_day = days[0]
 
104
        dayStart = self.week_view.dayStart
 
105
        expected_month = dayStart.month
 
106
        expected_year = dayStart.year
187
107
 
188
108
        self.app.pointing_device.click_object(day_to_select)
189
109
 
192
112
        self.assertThat(day_view.visible, Eventually(Equals(True)))
193
113
 
194
114
        # Check that the 'Day' view is on the correct/selected day.
195
 
        selected_date = datetime.datetime.strptime(
196
 
            self.app.main_view.get_month_year(day_view),
197
 
            '%B %Y')
198
 
        # self.assertThat(expected_day, Equals(selected_date.day))
 
115
        selected_date = \
 
116
            self.app.main_view.get_day_view().get_selected_day().startDay
 
117
        self.assertThat(expected_day, Equals(selected_date.day))
199
118
        self.assertThat(expected_month, Equals(selected_date.month))
200
119
        self.assertThat(expected_year, Equals(selected_date.year))