1#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import copy
7from datetime import datetime
8import os
9import pickle
10import time
11import unittest
12
13
14import layouttest_analyzer_helpers
15
16
17class TestLayoutTestAnalyzerHelpers(unittest.TestCase):
18
19  def testFindLatestTime(self):
20    time_list = ['2011-08-18-19', '2011-08-18-22', '2011-08-18-21',
21                 '2012-01-11-21', '.foo']
22    self.assertEquals(layouttest_analyzer_helpers.FindLatestTime(time_list),
23                      '2012-01-11-21')
24
25  def testFindLatestTimeWithEmptyList(self):
26    time_list = []
27    self.assertEquals(layouttest_analyzer_helpers.FindLatestTime(time_list),
28                      None)
29
30  def testFindLatestTimeWithNoValidStringInList(self):
31    time_list = ['.foo1', '232232']
32    self.assertEquals(layouttest_analyzer_helpers.FindLatestTime(time_list),
33                      None)
34
35  def GenerateTestDataWholeAndSkip(self):
36    """You should call this method if you want to generate test data."""
37    file_path = os.path.join('test_data', 'base')
38    analyzerResultMapBase = (
39        layouttest_analyzer_helpers.AnalyzerResultMap.Load(file_path))
40    # Remove this first part
41    m = analyzerResultMapBase.result_map['whole']
42    del m['media/video-source-type.html']
43    m = analyzerResultMapBase.result_map['skip']
44    del m['media/track/track-webvtt-tc004-magicheader.html']
45
46    file_path = os.path.join('test_data', 'less')
47    analyzerResultMapBase.Save(file_path)
48
49    file_path = os.path.join('test_data', 'base')
50    analyzerResultMapBase = AnalyzerResultMap.Load(file_path)
51
52    analyzerResultMapBase.result_map['whole']['add1.html'] = True
53    analyzerResultMapBase.result_map['skip']['add2.html'] = True
54
55    file_path = os.path.join('test_data', 'more')
56    analyzerResultMapBase.Save(file_path)
57
58  def GenerateTestDataNonSkip(self):
59    """You should call this method if you want to generate test data."""
60    file_path = os.path.join('test_data', 'base')
61    analyzerResultMapBase = AnalyzerResultMap.Load(file_path)
62    m = analyzerResultMapBase.result_map['nonskip']
63    ex = m['media/media-document-audio-repaint.html']
64    te_info_map1 = ex['te_info'][0]
65    te_info_map2 = copy.copy(te_info_map1)
66    te_info_map2['NEWADDED'] = True
67    ex['te_info'].append(te_info_map2)
68    m = analyzerResultMapBase.result_map['nonskip']
69
70    file_path = os.path.join('test_data', 'more_te_info')
71    analyzerResultMapBase.Save(file_path)
72
73  def testCompareResultMapsWholeAndSkip(self):
74    file_path = os.path.join('test_data', 'base')
75    analyzerResultMapBase = (
76        layouttest_analyzer_helpers.AnalyzerResultMap.Load(file_path))
77
78    file_path = os.path.join('test_data', 'less')
79    analyzerResultMapLess = (
80        layouttest_analyzer_helpers.AnalyzerResultMap.Load(file_path))
81
82    diff = analyzerResultMapBase.CompareToOtherResultMap(analyzerResultMapLess)
83    self.assertEquals(diff['skip'][0][0][0],
84                      'media/track/track-webvtt-tc004-magicheader.html')
85    self.assertEquals(diff['whole'][0][0][0],
86                      'media/video-source-type.html')
87    file_path = os.path.join('test_data', 'more')
88    analyzerResultMapMore = (
89        layouttest_analyzer_helpers.AnalyzerResultMap.Load(file_path))
90    diff = analyzerResultMapBase.CompareToOtherResultMap(analyzerResultMapMore)
91    self.assertEquals(diff['whole'][1][0][0], 'add1.html')
92    self.assertEquals(diff['skip'][1][0][0], 'add2.html')
93
94  def testCompareResultMapsNonSkip(self):
95    file_path = os.path.join('test_data', 'base')
96    analyzerResultMapBase = (
97        layouttest_analyzer_helpers.AnalyzerResultMap.Load(file_path))
98    file_path = os.path.join('test_data', 'more_te_info')
99    analyzerResultMapMoreTEInfo = (
100        layouttest_analyzer_helpers.AnalyzerResultMap.Load(file_path))
101    m = analyzerResultMapBase.CompareToOtherResultMap(
102        analyzerResultMapMoreTEInfo)
103    self.assertTrue('NEWADDED' in m['nonskip'][1][0][1][0])
104
105  def testGetListOfBugsForNonSkippedTests(self):
106    file_path = os.path.join('test_data', 'base')
107    analyzerResultMapBase = (
108        layouttest_analyzer_helpers.AnalyzerResultMap.Load(file_path))
109    self.assertEquals(
110        len(analyzerResultMapBase.GetListOfBugsForNonSkippedTests().keys()),
111        10)
112
113  def RunTestGetRevisionString(self, current_time_str, prev_time_str,
114                               expected_rev_str, expected_simple_rev_str,
115                               expected_rev_number, expected_rev_date,
116                               testname, diff_map_none=False):
117    current_time = datetime.strptime(current_time_str, '%Y-%m-%d-%H')
118    current_time = time.mktime(current_time.timetuple())
119    prev_time = datetime.strptime(prev_time_str, '%Y-%m-%d-%H')
120    prev_time = time.mktime(prev_time.timetuple())
121    if diff_map_none:
122      diff_map = None
123    else:
124      diff_map = {
125          'whole': [[], []],
126          'skip': [[(testname, 'te_info1')], []],
127          'nonskip': [[], []],
128      }
129    (rev_str, simple_rev_str, rev_number, rev_date) = (
130        layouttest_analyzer_helpers.GetRevisionString(prev_time, current_time,
131                                                      diff_map))
132    self.assertEquals(rev_str, expected_rev_str)
133    self.assertEquals(simple_rev_str, expected_simple_rev_str)
134    self.assertEquals(rev_number, expected_rev_number)
135    self.assertEquals(rev_date, expected_rev_date)
136
137  def testGetRevisionString(self):
138    expected_rev_str = ('<ul><a href="http://trac.webkit.org/changeset?'
139                        'new=94377@trunk/LayoutTests/platform/chromium/'
140                        'test_expectations.txt&old=94366@trunk/LayoutTests/'
141                        'platform/chromium/test_expectations.txt">94366->'
142                        '94377</a>\n'
143                        '<li>jamesr@google.com</li>\n'
144                        '<li>2011-09-01 18:00:23</li>\n'
145                        '<ul><li>-<a href="http://webkit.org/b/63878">'
146                        'BUGWK63878</a> : <a href="http://test-results.'
147                        'appspot.com/dashboards/flakiness_dashboard.html#'
148                        'tests=fast/dom/dom-constructors.html">fast/dom/'
149                        'dom-constructors.html</a> = TEXT</li>\n</ul></ul>')
150    expected_simple_rev_str = ('<a href="http://trac.webkit.org/changeset?'
151                               'new=94377@trunk/LayoutTests/platform/chromium/'
152                               'test_expectations.txt&old=94366@trunk/'
153                               'LayoutTests/platform/chromium/'
154                               'test_expectations.txt">94366->94377</a>,')
155    self.RunTestGetRevisionString('2011-09-02-00', '2011-09-01-00',
156                                  expected_rev_str, expected_simple_rev_str,
157                                  94377, '2011-09-01 18:00:23',
158                                  'fast/dom/dom-constructors.html')
159
160  def testGetRevisionStringNoneDiffMap(self):
161    self.RunTestGetRevisionString('2011-09-02-00', '2011-09-01-00', '', '',
162                                  '', '', '', diff_map_none=True)
163
164  def testGetRevisionStringNoMatchingTest(self):
165    self.RunTestGetRevisionString('2011-09-01-00', '2011-09-02-00', '', '',
166                                  '', '', 'foo1.html')
167
168  def testReplaceLineInFile(self):
169    file_path = os.path.join('test_data', 'inplace.txt')
170    f = open(file_path, 'w')
171    f.write('Hello')
172    f.close()
173    layouttest_analyzer_helpers.ReplaceLineInFile(
174        file_path, 'Hello', 'Goodbye')
175    f = open(file_path, 'r')
176    self.assertEquals(f.readline(), 'Goodbye')
177    f.close()
178    layouttest_analyzer_helpers.ReplaceLineInFile(
179        file_path, 'Bye', 'Hello')
180    f = open(file_path, 'r')
181    self.assertEquals(f.readline(), 'Goodbye')
182    f.close()
183
184  def testFindLatestResultWithNoData(self):
185    self.assertFalse(
186        layouttest_analyzer_helpers.FindLatestResult('test_data'))
187
188  def testConvertToCSVText(self):
189    file_path = os.path.join('test_data', 'base')
190    analyzerResultMapBase = (
191        layouttest_analyzer_helpers.AnalyzerResultMap.Load(file_path))
192    data, issues_txt = analyzerResultMapBase.ConvertToCSVText('11-10-10-2011')
193    self.assertEquals(data, '11-10-10-2011,204,36,10,95')
194    expected_issues_txt = """\
195BUGWK,66310,TEXT PASS,media/media-blocked-by-beforeload.html,DEBUG TEXT PASS,\
196media/video-source-error.html,
197BUGCR,86714,GPU IMAGE CRASH MAC,media/video-zoom.html,GPU IMAGE CRASH MAC,\
198media/video-controls-rendering.html,
199BUGCR,74102,GPU IMAGE PASS LINUX,media/video-controls-rendering.html,
200BUGWK,55718,TEXT IMAGE IMAGE+TEXT,media/media-document-audio-repaint.html,
201BUGCR,78376,TIMEOUT,http/tests/media/video-play-stall-seek.html,
202BUGCR,59415,WIN TEXT TIMEOUT PASS,media/video-loop.html,
203BUGCR,72223,IMAGE PASS,media/video-frame-accurate-seek.html,
204BUGCR,75354,TEXT IMAGE IMAGE+TEXT,media/media-document-audio-repaint.html,
205BUGCR,73609,TEXT,http/tests/media/video-play-stall.html,
206BUGWK,64003,DEBUG TEXT MAC PASS,media/video-delay-load-event.html,
207"""
208    self.assertEquals(issues_txt, expected_issues_txt)
209
210
211if __name__ == '__main__':
212  unittest.main()
213