11e24d82fcb6a87fb008123e16729a6c909986fc
[WebKit-https.git] / Tools / Scripts / webkitpy / w3c / test_converter_unittest.py
1 #!/usr/bin/env python
2
3 # Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1. Redistributions of source code must retain the above
10 #    copyright notice, this list of conditions and the following
11 #    disclaimer.
12 # 2. Redistributions in binary form must reproduce the above
13 #    copyright notice, this list of conditions and the following
14 #    disclaimer in the documentation and/or other materials
15 #    provided with the distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
18 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22 # OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26 # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 # SUCH DAMAGE.
29
30 import os
31 import re
32 import unittest2 as unittest
33
34 from webkitpy.common.system.outputcapture import OutputCapture
35 from webkitpy.thirdparty.BeautifulSoup import BeautifulSoup
36 from webkitpy.w3c.test_converter import W3CTestConverter
37
38
39 class W3CTestConverterTest(unittest.TestCase):
40
41     def fake_dir_path(self, converter, dirname):
42         return converter.path_from_webkit_root("LayoutTests", "css", dirname)
43
44     def test_read_prefixed_property_list(self):
45         """ Tests that the current list of properties requiring the -webkit- prefix load correctly """
46
47         # FIXME: We should be passing in a MockHost here ...
48         converter = W3CTestConverter()
49         prop_list = converter.prefixed_properties
50         self.assertTrue(prop_list, 'No prefixed properties found')
51         for prop in prop_list:
52             self.assertTrue(prop.startswith('-webkit-'))
53
54     def test_convert_for_webkit_nothing_to_convert(self):
55         """ Tests convert_for_webkit() using a basic test that has nothing to convert """
56
57         test_html = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
58 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
59 <html xmlns="http://www.w3.org/1999/xhtml">
60 <head>
61 <title>CSS Test: DESCRIPTION OF TEST</title>
62 <link rel="author" title="NAME_OF_AUTHOR"
63 href="mailto:EMAIL OR http://CONTACT_PAGE"/>
64 <link rel="help" href="RELEVANT_SPEC_SECTION"/>
65 <meta name="assert" content="TEST ASSERTION"/>
66 <style type="text/css"><![CDATA[
67 CSS FOR TEST
68 ]]></style>
69 </head>
70 <body>
71 CONTENT OF TEST
72 </body>
73 </html>
74 """
75         converter = W3CTestConverter()
76
77         oc = OutputCapture()
78         oc.capture_output()
79         try:
80             converted = converter.convert_html('/nothing/to/convert', test_html)
81         finally:
82             oc.restore_output()
83
84         self.verify_no_conversion_happened(converted)
85
86     def test_convert_for_webkit_harness_only(self):
87         """ Tests convert_for_webkit() using a basic JS test that uses testharness.js only and has no prefixed properties """
88
89         test_html = """<head>
90 <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
91 <script src="/resources/testharness.js"></script>
92 </head>
93 """
94         converter = W3CTestConverter()
95         fake_dir_path = self.fake_dir_path(converter, "harnessonly")
96
97         converted = converter.convert_html(fake_dir_path, test_html)
98
99         self.verify_conversion_happened(converted)
100         self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1)
101         self.verify_prefixed_properties(converted, [])
102
103     def test_convert_for_webkit_properties_only(self):
104         """ Tests convert_for_webkit() using a test that has 2 prefixed properties: 1 in a style block + 1 inline style """
105
106         test_html = """<html>
107 <head>
108 <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
109 <script src="/resources/testharness.js"></script>
110 <style type="text/css">
111
112 #block1 { @test0@: propvalue; }
113
114 </style>
115 </head>
116 <body>
117 <div id="elem1" style="@test1@: propvalue;"></div>
118 </body>
119 </html>
120 """
121         converter = W3CTestConverter()
122         fake_dir_path = self.fake_dir_path(converter, 'harnessandprops')
123         test_content = self.generate_test_content(converter.prefixed_properties, 1, test_html)
124
125         oc = OutputCapture()
126         oc.capture_output()
127         try:
128             converted = converter.convert_html(fake_dir_path, test_content[1])
129         finally:
130             oc.restore_output()
131
132         self.verify_conversion_happened(converted)
133         self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1)
134         self.verify_prefixed_properties(converted, test_content[0])
135
136     def test_convert_for_webkit_harness_and_properties(self):
137         """ Tests convert_for_webkit() using a basic JS test that uses testharness.js and testharness.css and has 4 prefixed properties: 3 in a style block + 1 inline style """
138
139         test_html = """<html>
140 <head>
141 <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
142 <script src="/resources/testharness.js"></script>
143 <style type="text/css">
144
145 #block1 { @test0@: propvalue; }
146 #block2 { @test1@: propvalue; }
147 #block3 { @test2@: propvalue; }
148
149 </style>
150 </head>
151 <body>
152 <div id="elem1" style="@test3@: propvalue;"></div>
153 </body>
154 </html>
155 """
156         converter = W3CTestConverter()
157         fake_dir_path = self.fake_dir_path(converter, 'harnessandprops')
158
159         oc = OutputCapture()
160         oc.capture_output()
161         try:
162             test_content = self.generate_test_content(converter.prefixed_properties, 2, test_html)
163             converted = converter.convert_html(fake_dir_path, test_content[1])
164         finally:
165             oc.restore_output()
166
167         self.verify_conversion_happened(converted)
168         self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1)
169         self.verify_prefixed_properties(converted, test_content[0])
170
171     def test_convert_test_harness_paths(self):
172         """ Tests convert_testharness_paths() with a test that uses all three testharness files """
173
174         test_html = """<head>
175 <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
176 <script src="/resources/testharness.js"></script>
177 <script src="/resources/testharnessreport.js"></script>
178 </head>
179 """
180         converter = W3CTestConverter()
181
182         fake_dir_path = self.fake_dir_path(converter, 'testharnesspaths')
183
184         doc = BeautifulSoup(test_html)
185         oc = OutputCapture()
186         oc.capture_output()
187         try:
188             converted = converter.convert_testharness_paths(doc, fake_dir_path)
189         finally:
190             oc.restore_output()
191
192         self.verify_conversion_happened(converted)
193         self.verify_test_harness_paths(converter, doc, fake_dir_path, 2, 1)
194
195     def test_convert_prefixed_properties(self):
196         """ Tests convert_prefixed_properties() file that has 20 properties requiring the -webkit- prefix:
197         10 in one style block + 5 in another style
198         block + 5 inline styles, including one with multiple prefixed properties.
199         The properties in the test content are in all sorts of wack formatting.
200         """
201
202         test_html = """<html>
203 <style type="text/css"><![CDATA[
204
205 .block1 {
206     width: 300px;
207     height: 300px
208 }
209
210 .block2 {
211     @test0@: propvalue;
212 }
213
214 .block3{@test1@: propvalue;}
215
216 .block4 { @test2@:propvalue; }
217
218 .block5{ @test3@ :propvalue; }
219
220 #block6 {    @test4@   :   propvalue;  }
221
222 #block7
223 {
224     @test5@: propvalue;
225 }
226
227 #block8 { @test6@: propvalue; }
228
229 #block9:pseudo
230 {
231
232     @test7@: propvalue;
233     @test8@:  propvalue propvalue propvalue;;
234 }
235
236 ]]></style>
237 </head>
238 <body>
239     <div id="elem1" style="@test9@: propvalue;"></div>
240     <div id="elem2" style="propname: propvalue; @test10@ : propvalue; propname:propvalue;"></div>
241     <div id="elem2" style="@test11@: propvalue; @test12@ : propvalue; @test13@   :propvalue;"></div>
242     <div id="elem3" style="@test14@:propvalue"></div>
243 </body>
244 <style type="text/css"><![CDATA[
245
246 .block10{ @test15@: propvalue; }
247 .block11{ @test16@: propvalue; }
248 .block12{ @test17@: propvalue; }
249 #block13:pseudo
250 {
251     @test18@: propvalue;
252     @test19@: propvalue;
253 }
254
255 ]]></style>
256 </html>
257 """
258         converter = W3CTestConverter()
259
260         test_content = self.generate_test_content(converter.prefixed_properties, 20, test_html)
261
262         oc = OutputCapture()
263         oc.capture_output()
264         try:
265             converted = converter.convert_prefixed_properties(BeautifulSoup(test_content[1]))
266         finally:
267             oc.restore_output()
268
269         self.verify_conversion_happened(converted)
270         self.verify_prefixed_properties(converted, test_content[0])
271
272     def verify_conversion_happened(self, converted):
273         self.assertTrue(converted, "conversion didn't happen")
274
275     def verify_no_conversion_happened(self, converted):
276         self.assertEqual(converted, None, 'test should not have been converted')
277
278     def verify_test_harness_paths(self, converter, converted, test_path, num_src_paths, num_href_paths):
279         if isinstance(converted, basestring):
280             converted = BeautifulSoup(converted)
281
282         resources_dir = converter.path_from_webkit_root("LayoutTests", "resources")
283
284         # Verify the original paths are gone, and the new paths are present.
285         orig_path_pattern = re.compile('\"/resources/testharness')
286         self.assertEquals(len(converted.findAll(src=orig_path_pattern)), 0, 'testharness src path was not converted')
287         self.assertEquals(len(converted.findAll(href=orig_path_pattern)), 0, 'testharness href path was not converted')
288
289         new_relpath = os.path.relpath(resources_dir, test_path)
290         relpath_pattern = re.compile(new_relpath)
291         self.assertEquals(len(converted.findAll(src=relpath_pattern)), num_src_paths, 'testharness src relative path not correct')
292         self.assertEquals(len(converted.findAll(href=relpath_pattern)), num_href_paths, 'testharness href relative path not correct')
293
294     def verify_prefixed_properties(self, converted, test_properties):
295         self.assertEqual(len(converted[0]), len(test_properties), 'Incorrect number of properties converted')
296         for test_prop in test_properties:
297             self.assertTrue((test_prop in converted[1]), 'Property ' + test_prop + ' not found in converted doc')
298
299     def generate_test_content(self, full_property_list, num_test_properties, html):
300         """Inserts properties requiring a -webkit- prefix into the content, replacing \'@testXX@\' with a property."""
301         test_properties = []
302         count = 0
303         while count < num_test_properties:
304             test_properties.append(full_property_list[count])
305             count += 1
306
307         # Replace the tokens in the testhtml with the test properties. Walk backward
308         # through the list to replace the double-digit tokens first
309         index = len(test_properties) - 1
310         while index >= 0:
311             # Use the unprefixed version
312             test_prop = test_properties[index].replace('-webkit-', '')
313             # Replace the token
314             html = html.replace('@test' + str(index) + '@', test_prop)
315             index -= 1
316
317         return (test_properties, html)