summaryrefslogtreecommitdiffstats
path: root/media/tools/layout_tests/test_expectations_unittest.py
blob: d7ea03c84347304265e946b7cb2c8c5ff64d2838 (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
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
#!/usr/bin/python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""A unit test module for TestExpectationManager."""

from datetime import date
from datetime import datetime
from datetime import timedelta
import os
import shutil
import time
import unittest

from test_expectations import TestExpectationsManager


class TestTestExpectationsManager(unittest.TestCase):
    """A unit test class for TestExpectationManager."""
    test_dir = u'test_dir'
    cleaned = False

    def setUp(self):
        if not TestTestExpectationsManager.cleaned:
            if os.path.exists(self.test_dir):
                shutil.rmtree(self.test_dir)
            os.mkdir(self.test_dir)
        TestTestExpectationsManager.cleaned = True

    def verify_test_expectaion_format(self, file_name, media):
        """Verify the test expectation format in the file.

        Args:
             file_name: a file name to be verified.
             media: a boolean to indicate whether the test is for media
                 or not.

        Returns:
             a boolean indicating whether the file is in test expectation
                 format.
        """
        file_object = open(file_name, 'r')
        firstline = file_object.readline()
        if ('These are the layout test expectations for the '
            'Chromium port(s) of WebKit.') not in firstline:
            return False
        # Check media related test case file is there.
        verify_media = not media
        if media:
            for line in file_object:
                if 'media' in line:
                    verify_media = True
        file_object.close()
        return verify_media

    def verify_test_expectaion_format_in_csv(self, file_name):
        """Verify the CSV file has information parsed.

        Args:
            file_name: file name to be examined.

        Returns:
            a boolean that indicates that the CSV file contains information
                parsed from test expectation file.
        """
        file_object = open(file_name, 'r')
        firstline = file_object.readline()
        file_object.close()
        return 'TestCase' in firstline

    def test_get_and_save_content(self):
        """Test get_and_save_content method.

        Also, test several other methods.
        """
        # Get all test cases.
        result_file = os.path.join(self.test_dir,
                                   'test_get_and_save_content.txt')

        test_expectations_manager = TestExpectationsManager()
        test_expectations_manager.get_and_save_content(
            test_expectations_manager.DEFAULT_TEST_EXPECTATION_LOCATION,
            result_file)

        self.assertTrue(
            self.verify_test_expectaion_format(result_file, False))

        # Get media test cases from local all test cases.
        result_file_media = os.path.join(self.test_dir,
                                         'test_get_and_save_content_m.txt')

        test_expectations_manager = TestExpectationsManager()
        test_expectations_manager.get_and_save_content_media_only(
            result_file, result_file_media)

        self.assertTrue(
            self.verify_test_expectaion_format(result_file_media, True))

        # Get media test cases from local all test cases.
        result_file_media_csv = os.path.join(
            self.test_dir, 'test_get_and_save_content_m.csv')

        test_expectations_manager.get_and_parse_content(
            result_file, result_file_media_csv, True)

        elements = test_expectations_manager.get_test_case_element(
            'media/restore-from-page-cache.html',
            ['TEXT', 'UNIMPLEMENTED', 'RELEASE'])
        self.assertEqual(
            elements, ['N', 'N', 'N'],
            'returned element of GetTestCaseElement is wrong')

        field_names = ['SKIP', 'UNIMPLEMENTED', 'KNOWNISSUE']
        field_name_indexes = test_expectations_manager.get_column_indexes(
            field_names)
        expected_filed_name_indexes = [6, 22, 23]
        self.assertEqual(field_name_indexes, expected_filed_name_indexes,
                         'incorrect field indexes')

    def test_parse_content_from_svn(self):
        """Test get_and_parse_content method using SVN."""
        self.parse_content_from_svn_test_helper(False)

    def test_parse_content_from_svn_media(self):
        """Test get_and_parse_content method using SVN (only media)."""
        self.parse_content_from_svn_test_helper(True)

    def parse_content_from_svn_test_helper(self, media):
        """Test get_and_parse_content method using SVN.

        Args:
            media: True if this is for media tests.
        """
        media_string = 'all'
        if media:
            media_string = 'media'
        result_file = os.path.join(
            self.test_dir,
            'test_parse_content_from_svn_%s.csv' % media_string)
        test_expectations_manager = TestExpectationsManager()
        test_expectations_manager.get_and_parse_content(
            test_expectations_manager.DEFAULT_TEST_EXPECTATION_LOCATION,
            result_file, media)

        self.assertTrue(test_expectations_manager.testcases > 0)

        self.verify_test_expectaion_format_in_csv(result_file)

    def test_generate_link_for_cbug(self):
        """Test generate_link_for_bug for a chromium bug."""
        self.generate_link_for_bug_helper(
            'BUGCR1234',
            'http://code.google.com/p/chromium/issues/detail?id=1234')

    def test_generate_link_for_wbug(self):
        """Test generate_link_for_bug for a webkit bug."""
        self.generate_link_for_bug_helper(
            'BUGWK1234',
            'https://bugs.webkit.org/show_bug.cgi?id=1234')

    def test_generate_link_for_pbug(self):
        """Test generate_link_for_bug for person."""
        self.generate_link_for_bug_helper('BUGIMASAKI',
                                          'mailto:imasaki@chromium.org')

    def generate_link_for_bug_helper(self, bug_text, expected_link):
        """A helper for generating link test."""
        test_expectations_manager = TestExpectationsManager()
        link = test_expectations_manager.generate_link_for_bug(bug_text)
        self.assertEqual(link, expected_link, 'link generated are not correct')

    def test_te_diff_between_times(self):
        test_expectations_manager = TestExpectationsManager()
        now = time.time()
        past = datetime.now() - timedelta(days=4)
        past = time.mktime(past.timetuple())
        result_list = test_expectations_manager.get_te_diff_between_times(
            test_expectations_manager.DEFAULT_TEST_EXPECTATION_DIR,
            now, past, ['media/audio-repaint.html'], -1, True)
        self.assertTrue(result_list is not None)

    def test_te_diff_between_times_no_changecheck(self):
        test_expectations_manager = TestExpectationsManager()
        now = time.time()
        past = datetime.now() - timedelta(weeks=2)
        past = time.mktime(past.timetuple())
        result_list = test_expectations_manager.get_te_diff_between_times(
            test_expectations_manager.DEFAULT_TEST_EXPECTATION_DIR,
            now, past, ['media/audio-repaint.html'], -1, False)
        self.assertTrue(result_list is not None)

    def test_te_diff_between_times_no_changecheck_with_pattern(self):
        test_expectations_manager = TestExpectationsManager()
        now = time.time()
        past = datetime.now() - timedelta(weeks=2)
        past = time.mktime(past.timetuple())
        result_list = test_expectations_manager.get_te_diff_between_times(
            test_expectations_manager.DEFAULT_TEST_EXPECTATION_DIR,
            now, past, ['media/audio-repaint.html'], -1, False)
        self.assertTrue(result_list is not None)


def main():
    test_suite = unittest.TestLoader().loadTestsFromTestCase(
        TestTestExpectationsManager)

    unittest.TextTestRunner(verbosity=2).run(test_suite)