1 # Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
10 # 2. Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following
12 # disclaimer in the documentation and/or other materials
13 # provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
16 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
19 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20 # OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
24 # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
25 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 from webkitpy.common.host import Host
33 from webkitpy.common.system.outputcapture import OutputCapture
34 from webkitpy.common.webkit_finder import WebKitFinder
35 from webkitpy.thirdparty.BeautifulSoup import BeautifulSoup
36 from webkitpy.w3c.test_converter import _W3CTestConverter
38 DUMMY_FILENAME = 'dummy.html'
39 DUMMY_PATH = 'dummy/testharness/path'
41 class W3CTestConverterTest(unittest.TestCase):
43 # FIXME: When we move to using a MockHost, this method should be removed, since
44 # then we can just pass in a dummy dir path
45 def fake_dir_path(self, dirname):
46 filesystem = Host().filesystem
47 webkit_root = WebKitFinder(filesystem).webkit_base()
48 return filesystem.abspath(filesystem.join(webkit_root, "LayoutTests", "css", dirname))
50 def test_read_prefixed_property_list(self):
51 """ Tests that the current list of properties requiring the -webkit- prefix load correctly """
53 # FIXME: We should be passing in a MockHost here ...
54 converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME)
55 prop_list = converter.prefixed_properties
56 self.assertTrue(prop_list, 'No prefixed properties found')
58 def test_convert_for_webkit_nothing_to_convert(self):
59 """ Tests convert_for_webkit() using a basic test that has nothing to convert """
61 test_html = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
62 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
63 <html xmlns="http://www.w3.org/1999/xhtml">
65 <title>CSS Test: DESCRIPTION OF TEST</title>
66 <link rel="author" title="NAME_OF_AUTHOR"
67 href="mailto:EMAIL OR http://CONTACT_PAGE"/>
68 <link rel="help" href="RELEVANT_SPEC_SECTION"/>
69 <meta name="assert" content="TEST ASSERTION"/>
70 <style type="text/css"><![CDATA[
79 converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME)
84 converter.feed(test_html)
86 converted = converter.output()
90 self.verify_no_conversion_happened(converted, test_html)
92 def test_convert_for_webkit_harness_only(self):
93 """ Tests convert_for_webkit() using a basic JS test that uses testharness.js only and has no prefixed properties """
96 <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
97 <script src="/resources/testharness.js"></script>
100 fake_dir_path = self.fake_dir_path("harnessonly")
101 converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME)
102 converter.feed(test_html)
104 converted = converter.output()
106 self.verify_conversion_happened(converted)
107 self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1)
108 self.verify_prefixed_properties(converted, [])
110 def test_convert_for_webkit_properties_only(self):
111 """ Tests convert_for_webkit() using a test that has 2 prefixed properties: 1 in a style block + 1 inline style """
113 test_html = """<html>
115 <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
116 <script src="/resources/testharness.js"></script>
117 <style type="text/css">
119 #block1 { @test0@: propvalue; }
124 <div id="elem1" style="@test1@: propvalue;"></div>
128 fake_dir_path = self.fake_dir_path('harnessandprops')
129 converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME)
130 test_content = self.generate_test_content(converter.prefixed_properties, 1, test_html)
135 converter.feed(test_content[1])
137 converted = converter.output()
141 self.verify_conversion_happened(converted)
142 self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1)
143 self.verify_prefixed_properties(converted, test_content[0])
145 def test_convert_for_webkit_harness_and_properties(self):
146 """ 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 """
148 test_html = """<html>
150 <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
151 <script src="/resources/testharness.js"></script>
152 <style type="text/css">
154 #block1 { @test0@: propvalue; }
155 #block2 { @test1@: propvalue; }
156 #block3 { @test2@: propvalue; }
161 <div id="elem1" style="@test3@: propvalue;"></div>
165 fake_dir_path = self.fake_dir_path('harnessandprops')
166 converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME)
171 test_content = self.generate_test_content(converter.prefixed_properties, 2, test_html)
172 converter.feed(test_content[1])
174 converted = converter.output()
178 self.verify_conversion_happened(converted)
179 self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1)
180 self.verify_prefixed_properties(converted, test_content[0])
182 def test_convert_test_harness_paths(self):
183 """ Tests convert_testharness_paths() with a test that uses all three testharness files """
185 test_html = """<head>
186 <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
187 <script src="/resources/testharness.js"></script>
188 <script src="/resources/testharnessreport.js"></script>
191 fake_dir_path = self.fake_dir_path('testharnesspaths')
192 converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME)
197 converter.feed(test_html)
199 converted = converter.output()
203 self.verify_conversion_happened(converted)
204 self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 2, 1)
206 def test_convert_vendor_prefix_js_paths(self):
207 test_html = """<head>
208 <script src="/common/vendor-prefix.js">
211 fake_dir_path = self.fake_dir_path('adapterjspaths')
212 converter = _W3CTestConverter(fake_dir_path, DUMMY_FILENAME)
217 converter.feed(test_html)
219 converted = converter.output()
223 new_html = BeautifulSoup(converted[1])
225 # Verify the original paths are gone, and the new paths are present.
226 orig_path_pattern = re.compile('\"/common/vendor-prefix.js')
227 self.assertEquals(len(new_html.findAll(src=orig_path_pattern)), 0, 'vendor-prefix.js path was not converted')
229 resources_dir = converter.path_from_webkit_root("LayoutTests", "resources")
230 new_relpath = os.path.relpath(resources_dir, fake_dir_path)
231 relpath_pattern = re.compile(new_relpath)
232 self.assertEquals(len(new_html.findAll(src=relpath_pattern)), 1, 'vendor-prefix.js relative path not correct')
234 def test_convert_prefixed_properties(self):
235 """ Tests convert_prefixed_properties() file that has 20 properties requiring the -webkit- prefix:
236 10 in one style block + 5 in another style
237 block + 5 inline styles, including one with multiple prefixed properties.
238 The properties in the test content are in all sorts of wack formatting.
241 test_html = """<html>
242 <style type="text/css"><![CDATA[
253 .block3{@test1@: propvalue;}
255 .block4 { @test2@:propvalue; }
257 .block5{ @test3@ :propvalue; }
259 #block6 { @test4@ : propvalue; }
266 #block8 { @test6@: propvalue; }
272 @test8@: propvalue propvalue propvalue;;
278 <div id="elem1" style="@test9@: propvalue;"></div>
279 <div id="elem2" style="propname: propvalue; @test10@ : propvalue; propname:propvalue;"></div>
280 <div id="elem2" style="@test11@: propvalue; @test12@ : propvalue; @test13@ :propvalue;"></div>
281 <div id="elem3" style="@test14@:propvalue"></div>
283 <style type="text/css"><![CDATA[
285 .block10{ @test15@: propvalue; }
286 .block11{ @test16@: propvalue; }
287 .block12{ @test17@: propvalue; }
297 converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME)
298 test_content = self.generate_test_content(converter.prefixed_properties, 20, test_html)
303 converter.feed(test_content[1])
305 converted = converter.output()
309 self.verify_conversion_happened(converted)
310 self.verify_prefixed_properties(converted, test_content[0])
312 def test_hides_all_instructions_for_manual_testers(self):
313 test_html = """<body>
314 <h1 class="instructions">Hello manual tester!</h1>
315 <p class="instructions some_other_class">This is how you run this test.</p>
316 <p style="willbeoverwritten" class="instructions">...</p>
317 <doesntmatterwhichtagitis class="some_other_class instructions">...</p>
318 <p>Legit content may contain the instructions string</p>
321 expected_test_html = """<body>
322 <h1 class="instructions" style="display:none">Hello manual tester!</h1>
323 <p class="instructions some_other_class" style="display:none">This is how you run this test.</p>
324 <p class="instructions" style="display:none">...</p>
325 <doesntmatterwhichtagitis class="some_other_class instructions" style="display:none">...</p>
326 <p>Legit content may contain the instructions string</p>
329 converter = _W3CTestConverter(DUMMY_PATH, DUMMY_FILENAME)
334 converter.feed(test_html)
336 converted = converter.output()
340 self.assertEqual(converted[1], expected_test_html)
342 def verify_conversion_happened(self, converted):
343 self.assertTrue(converted, "conversion didn't happen")
345 def verify_no_conversion_happened(self, converted, original):
346 self.assertEqual(converted[1], original, 'test should not have been converted')
348 def verify_test_harness_paths(self, converter, converted, test_path, num_src_paths, num_href_paths):
349 if isinstance(converted, basestring):
350 converted = BeautifulSoup(converted)
352 resources_dir = converter.path_from_webkit_root("LayoutTests", "resources")
354 # Verify the original paths are gone, and the new paths are present.
355 orig_path_pattern = re.compile('\"/resources/testharness')
356 self.assertEquals(len(converted.findAll(src=orig_path_pattern)), 0, 'testharness src path was not converted')
357 self.assertEquals(len(converted.findAll(href=orig_path_pattern)), 0, 'testharness href path was not converted')
359 new_relpath = os.path.relpath(resources_dir, test_path)
360 relpath_pattern = re.compile(new_relpath)
361 self.assertEquals(len(converted.findAll(src=relpath_pattern)), num_src_paths, 'testharness src relative path not correct')
362 self.assertEquals(len(converted.findAll(href=relpath_pattern)), num_href_paths, 'testharness href relative path not correct')
364 def verify_prefixed_properties(self, converted, test_properties):
365 self.assertEqual(len(set(converted[0])), len(set(test_properties)), 'Incorrect number of properties converted')
366 for test_prop in test_properties:
367 self.assertTrue((test_prop in converted[1]), 'Property ' + test_prop + ' not found in converted doc')
369 def generate_test_content(self, full_property_list, num_test_properties, html):
370 """Inserts properties requiring a -webkit- prefix into the content, replacing \'@testXX@\' with a property."""
373 while count < num_test_properties:
374 test_properties.append(full_property_list[count])
377 # Replace the tokens in the testhtml with the test properties. Walk backward
378 # through the list to replace the double-digit tokens first
379 index = len(test_properties) - 1
381 # Use the unprefixed version
382 test_prop = test_properties[index].replace('-webkit-', '')
384 html = html.replace('@test' + str(index) + '@', test_prop)
387 return (test_properties, html)