summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/toaster/tests/browser/test_toastertable_ui.py
blob: 0780ef9fe45392d574365d8d7c1b4f5898db0eea (plain)
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
#! /usr/bin/env python
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
#
# BitBake Toaster Implementation
#
# Copyright (C) 2013-2016 Intel Corporation
#
# SPDX-License-Identifier: GPL-2.0-only
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from datetime import datetime

from django.core.urlresolvers import reverse
from django.utils import timezone
from tests.browser.selenium_helpers import SeleniumTestCase
from orm.models import BitbakeVersion, Release, Project, Build

class TestToasterTableUI(SeleniumTestCase):
    """
    Tests for the UI elements of ToasterTable (sorting etc.);
    note that the tests cover generic functionality of ToasterTable which
    manifests as UI elements in the browser, and can only be tested via
    Selenium.
    """

    def setUp(self):
        pass

    def _get_orderby_heading(self, table):
        """
        Get the current order by finding the column heading in <table> with
        the sorted class on it.

        table: WebElement for a ToasterTable
        """
        selector = 'thead a.sorted'
        heading = table.find_element_by_css_selector(selector)
        return heading.get_attribute('innerHTML').strip()

    def _get_datetime_from_cell(self, row, selector):
        """
        Return the value in the cell selected by <selector> on <row> as a
        datetime.

        row: <tr> WebElement for a row in the ToasterTable
        selector: CSS selector to use to find the cell containing the date time
        string
        """
        cell = row.find_element_by_css_selector(selector)
        cell_text = cell.get_attribute('innerHTML').strip()
        return datetime.strptime(cell_text, '%d/%m/%y %H:%M')

    def test_revert_orderby(self):
        """
        Test that sort order for a table reverts to the default sort order
        if the current sort column is hidden.
        """
        now = timezone.now()
        later = now + timezone.timedelta(hours=1)
        even_later = later + timezone.timedelta(hours=1)

        bbv = BitbakeVersion.objects.create(name='test bbv', giturl='/tmp/',
                                            branch='master', dirpath='')
        release = Release.objects.create(name='test release',
                                         branch_name='master',
                                         bitbake_version=bbv)

        project = Project.objects.create_project('project', release)

        # set up two builds which will order differently when sorted by
        # started_on or completed_on

        # started first, finished last
        build1 = Build.objects.create(project=project,
                                      started_on=now,
                                      completed_on=even_later,
                                      outcome=Build.SUCCEEDED)

        # started second, finished first
        build2 = Build.objects.create(project=project,
                                      started_on=later,
                                      completed_on=later,
                                      outcome=Build.SUCCEEDED)

        url = reverse('all-builds')
        self.get(url)
        table = self.wait_until_visible('#allbuildstable')

        # check ordering (default is by -completed_on); so build1 should be
        # first as it finished last
        active_heading = self._get_orderby_heading(table)
        self.assertEqual(active_heading, 'Completed on',
            'table should be sorted by "Completed on" by default')

        row_selector = '#allbuildstable tbody tr'
        cell_selector = 'td.completed_on'

        rows = self.find_all(row_selector)
        row1_completed_on = self._get_datetime_from_cell(rows[0], cell_selector)
        row2_completed_on = self._get_datetime_from_cell(rows[1], cell_selector)
        self.assertTrue(row1_completed_on > row2_completed_on,
            'table should be sorted by -completed_on')

        # turn on started_on column
        self.click('#edit-columns-button')
        self.click('#checkbox-started_on')

        # sort by started_on column
        links = table.find_elements_by_css_selector('th.started_on a')
        for link in links:
            if link.get_attribute('innerHTML').strip() == 'Started on':
                link.click()
                break

        # wait for table data to reload in response to new sort
        self.wait_until_visible('#allbuildstable')

        # check ordering; build1 should be first
        active_heading = self._get_orderby_heading(table)
        self.assertEqual(active_heading, 'Started on',
            'table should be sorted by "Started on"')

        cell_selector = 'td.started_on'

        rows = self.find_all(row_selector)
        row1_started_on = self._get_datetime_from_cell(rows[0], cell_selector)
        row2_started_on = self._get_datetime_from_cell(rows[1], cell_selector)
        self.assertTrue(row1_started_on < row2_started_on,
            'table should be sorted by started_on')

        # turn off started_on column
        self.click('#edit-columns-button')
        self.click('#checkbox-started_on')

        # wait for table data to reload in response to new sort
        self.wait_until_visible('#allbuildstable')

        # check ordering (should revert to completed_on); build2 should be first
        active_heading = self._get_orderby_heading(table)
        self.assertEqual(active_heading, 'Completed on',
            'table should be sorted by "Completed on" after hiding sort column')

        cell_selector = 'td.completed_on'

        rows = self.find_all(row_selector)
        row1_completed_on = self._get_datetime_from_cell(rows[0], cell_selector)
        row2_completed_on = self._get_datetime_from_cell(rows[1], cell_selector)
        self.assertTrue(row1_completed_on > row2_completed_on,
            'table should be sorted by -completed_on')