1 # Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
2 # Copyright (C) 2015 Apple Inc. All Rights Reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
8 # 1. Redistributions of source code must retain the above
9 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
17 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 # OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25 # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 from webkitpy.common.system.outputcapture import OutputCapture
33 from webkitpy.w3c.test_parser import TestParser
36 options = {'all': False, 'no_overwrite': False}
39 class TestParserTest(unittest.TestCase):
41 def test_analyze_test_reftest_one_match(self):
43 <link rel="match" href="green-box-ref.xht" />
46 test_path = os.path.join(os.path.sep, 'some', 'madeup', 'path')
47 parser = TestParser(options, os.path.join(test_path, 'somefile.html'))
48 test_info = parser.analyze_test(test_contents=test_html)
50 self.assertNotEqual(test_info, None, 'did not find a test')
51 self.assertTrue('test' in test_info.keys(), 'did not find a test file')
52 self.assertTrue('reference' in test_info.keys(), 'did not find a reference file')
53 self.assertTrue(test_info['reference'].startswith(test_path), 'reference path is not correct')
54 self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
55 self.assertFalse('jstest' in test_info.keys(), 'test should not have been analyzed as a jstest')
57 def test_analyze_test_reftest_multiple_matches(self):
59 <link rel="match" href="green-box-ref.xht" />
60 <link rel="match" href="blue-box-ref.xht" />
61 <link rel="match" href="orange-box-ref.xht" />
67 test_path = os.path.join(os.path.sep, 'some', 'madeup', 'path')
68 parser = TestParser(options, os.path.join(test_path, 'somefile.html'))
69 test_info = parser.analyze_test(test_contents=test_html)
71 _, _, logs = oc.restore_output()
73 self.assertNotEqual(test_info, None, 'did not find a test')
74 self.assertTrue('test' in test_info.keys(), 'did not find a test file')
75 self.assertTrue('reference' in test_info.keys(), 'did not find a reference file')
76 self.assertTrue(test_info['reference'].startswith(test_path), 'reference path is not correct')
77 self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
78 self.assertFalse('jstest' in test_info.keys(), 'test should not have been analyzed as a jstest')
80 self.assertEqual(logs, 'Multiple references are not supported. Importing the first ref defined in somefile.html\n')
82 def test_analyze_test_reftest_match_and_mismatch(self):
84 <link rel="match" href="green-box-ref.xht" />
85 <link rel="match" href="blue-box-ref.xht" />
86 <link rel="mismatch" href="orange-box-notref.xht" />
93 test_path = os.path.join(os.path.sep, 'some', 'madeup', 'path')
94 parser = TestParser(options, os.path.join(test_path, 'somefile.html'))
95 test_info = parser.analyze_test(test_contents=test_html)
97 _, _, logs = oc.restore_output()
99 self.assertNotEqual(test_info, None, 'did not find a test')
100 self.assertTrue('test' in test_info.keys(), 'did not find a test file')
101 self.assertTrue('reference' in test_info.keys(), 'did not find a reference file')
102 self.assertTrue(test_info['reference'].startswith(test_path), 'reference path is not correct')
103 self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
104 self.assertFalse('jstest' in test_info.keys(), 'test should not have been analyzed as a jstest')
106 self.assertEqual(logs, 'Multiple references are not supported. Importing the first ref defined in somefile.html\n')
108 def test_analyze_test_reftest_with_ref_support_Files(self):
109 """ Tests analyze_test() using a reftest that has refers to a reference file outside of the tests directory and the reference file has paths to other support files """
111 test_html = """<html>
113 <link rel="match" href="../reference/green-box-ref.xht" />
117 <link href="support/css/ref-stylesheet.css" rel="stylesheet" type="text/css">
118 <style type="text/css">
119 background-image: url("../../support/some-image.png")
123 <div><img src="../support/black96x96.png" alt="Image download support must be enabled" /></div>
127 test_path = os.path.join(os.path.sep, 'some', 'madeup', 'path')
128 parser = TestParser(options, os.path.join(test_path, 'somefile.html'))
129 test_info = parser.analyze_test(test_contents=test_html, ref_contents=ref_html)
131 self.assertNotEqual(test_info, None, 'did not find a test')
132 self.assertTrue('test' in test_info.keys(), 'did not find a test file')
133 self.assertTrue('reference' in test_info.keys(), 'did not find a reference file')
134 self.assertTrue(test_info['reference'].startswith(test_path), 'reference path is not correct')
135 self.assertTrue('reference_support_info' in test_info.keys(), 'there should be reference_support_info for this test')
136 self.assertEquals(len(test_info['reference_support_info']['files']), 3, 'there should be 3 support files in this reference')
137 self.assertFalse('jstest' in test_info.keys(), 'test should not have been analyzed as a jstest')
139 def test_analyze_jstest(self):
140 """ Tests analyze_test() using a jstest """
142 test_html = """<head>
143 <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
144 <script src="/resources/testharness.js"></script>
147 test_path = os.path.join(os.path.sep, 'some', 'madeup', 'path')
148 parser = TestParser(options, os.path.join(test_path, 'somefile.html'))
149 test_info = parser.analyze_test(test_contents=test_html)
151 self.assertNotEqual(test_info, None, 'test_info is None')
152 self.assertTrue('test' in test_info.keys(), 'did not find a test file')
153 self.assertFalse('reference' in test_info.keys(), 'shold not have found a reference file')
154 self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
155 self.assertTrue('jstest' in test_info.keys(), 'test should be a jstest')
157 def test_analyze_manual_wpt_test(self):
158 """ Tests analyze_test() using a manual jstest """
160 test_html = """<head>
161 <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
162 <script src="/resources/testharness.js"></script>
165 test_path = os.path.join(os.path.sep, 'some', 'madeup', 'path')
166 parser = TestParser(options, os.path.join(test_path, 'somefile-manual.html'))
167 test_info = parser.analyze_test(test_contents=test_html)
169 self.assertTrue(test_info['manualtest'], 'test_info is None')
171 def test_analyze_pixel_test_all_true(self):
172 """ Tests analyze_test() using a test that is neither a reftest or jstest with all=False """
174 test_html = """<html>
176 <title>CSS Test: DESCRIPTION OF TEST</title>
177 <link rel="author" title="NAME_OF_AUTHOR" />
178 <style type="text/css"><![CDATA[
187 # Set options to 'all' so this gets found
188 options['all'] = True
190 test_path = os.path.join(os.path.sep, 'some', 'madeup', 'path')
191 parser = TestParser(options, os.path.join(test_path, 'somefile.html'))
192 test_info = parser.analyze_test(test_contents=test_html)
194 self.assertNotEqual(test_info, None, 'test_info is None')
195 self.assertTrue('test' in test_info.keys(), 'did not find a test file')
196 self.assertFalse('reference' in test_info.keys(), 'shold not have found a reference file')
197 self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
198 self.assertFalse('jstest' in test_info.keys(), 'test should not be a jstest')
200 def test_analyze_pixel_test_all_false(self):
201 """ Tests analyze_test() using a test that is neither a reftest or jstest, with -all=False """
203 test_html = """<html>
205 <title>CSS Test: DESCRIPTION OF TEST</title>
206 <link rel="author" title="NAME_OF_AUTHOR" />
207 <style type="text/css"><![CDATA[
216 # Set all to false so this gets skipped
217 options['all'] = False
219 test_path = os.path.join(os.path.sep, 'some', 'madeup', 'path')
220 parser = TestParser(options, os.path.join(test_path, 'somefile.html'))
221 test_info = parser.analyze_test(test_contents=test_html)
223 self.assertEqual(test_info, None, 'test should have been skipped')
225 def test_analyze_non_html_file(self):
226 """ Tests analyze_test() with a file that has no html"""
227 # FIXME: use a mock filesystem
228 parser = TestParser(options, os.path.join(os.path.dirname(__file__), 'test_parser.py'))
229 test_info = parser.analyze_test()
230 self.assertEqual(test_info, None, 'no tests should have been found in this file')
232 def test_reference_test(self):
233 """ Tests analyze_test() using a test that is a reference file having a <link rel="match"> tag"""
235 test_html = """<html>
237 <title>CSS Test: DESCRIPTION OF TEST</title>
238 <link rel="match" href="test-ref.html" />
239 <link rel="author" title="NAME_OF_AUTHOR" />
240 <style type="text/css"><![CDATA[
249 test_path = os.path.join(os.path.sep, 'some', 'madeup', 'path')
250 parser = TestParser(options, os.path.join(test_path, 'test-ref.html'))
251 test_info = parser.analyze_test(test_contents=test_html)
253 self.assertTrue('referencefile' in test_info, 'test should be detected as reference file')