PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/qt/qtwebkit/Tools/Scripts/webkitpy/w3c/test_converter_unittest.py

https://gitlab.com/x33n/phantomjs
Python | 319 lines | 278 code | 11 blank | 30 comment | 1 complexity | 60f3009dd63ef847b6af751d249aa64e MD5 | raw file
  1. #!/usr/bin/env python
  2. # Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions
  6. # are met:
  7. #
  8. # 1. Redistributions of source code must retain the above
  9. # copyright notice, this list of conditions and the following
  10. # disclaimer.
  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.
  15. #
  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
  27. # SUCH DAMAGE.
  28. import os
  29. import re
  30. import unittest2 as unittest
  31. from webkitpy.common.system.outputcapture import OutputCapture
  32. from webkitpy.thirdparty.BeautifulSoup import BeautifulSoup
  33. from webkitpy.w3c.test_converter import W3CTestConverter
  34. DUMMY_FILENAME = 'dummy.html'
  35. class W3CTestConverterTest(unittest.TestCase):
  36. def fake_dir_path(self, converter, dirname):
  37. return converter.path_from_webkit_root("LayoutTests", "css", dirname)
  38. def test_read_prefixed_property_list(self):
  39. """ Tests that the current list of properties requiring the -webkit- prefix load correctly """
  40. # FIXME: We should be passing in a MockHost here ...
  41. converter = W3CTestConverter()
  42. prop_list = converter.prefixed_properties
  43. self.assertTrue(prop_list, 'No prefixed properties found')
  44. for prop in prop_list:
  45. self.assertTrue(prop.startswith('-webkit-'))
  46. def test_convert_for_webkit_nothing_to_convert(self):
  47. """ Tests convert_for_webkit() using a basic test that has nothing to convert """
  48. test_html = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  49. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  50. <html xmlns="http://www.w3.org/1999/xhtml">
  51. <head>
  52. <title>CSS Test: DESCRIPTION OF TEST</title>
  53. <link rel="author" title="NAME_OF_AUTHOR"
  54. href="mailto:EMAIL OR http://CONTACT_PAGE"/>
  55. <link rel="help" href="RELEVANT_SPEC_SECTION"/>
  56. <meta name="assert" content="TEST ASSERTION"/>
  57. <style type="text/css"><![CDATA[
  58. CSS FOR TEST
  59. ]]></style>
  60. </head>
  61. <body>
  62. CONTENT OF TEST
  63. </body>
  64. </html>
  65. """
  66. converter = W3CTestConverter()
  67. oc = OutputCapture()
  68. oc.capture_output()
  69. try:
  70. converted = converter.convert_html('/nothing/to/convert', test_html, DUMMY_FILENAME)
  71. finally:
  72. oc.restore_output()
  73. self.verify_no_conversion_happened(converted)
  74. def test_convert_for_webkit_harness_only(self):
  75. """ Tests convert_for_webkit() using a basic JS test that uses testharness.js only and has no prefixed properties """
  76. test_html = """<head>
  77. <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
  78. <script src="/resources/testharness.js"></script>
  79. </head>
  80. """
  81. converter = W3CTestConverter()
  82. fake_dir_path = self.fake_dir_path(converter, "harnessonly")
  83. converted = converter.convert_html(fake_dir_path, test_html, DUMMY_FILENAME)
  84. self.verify_conversion_happened(converted)
  85. self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1)
  86. self.verify_prefixed_properties(converted, [])
  87. def test_convert_for_webkit_properties_only(self):
  88. """ Tests convert_for_webkit() using a test that has 2 prefixed properties: 1 in a style block + 1 inline style """
  89. test_html = """<html>
  90. <head>
  91. <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
  92. <script src="/resources/testharness.js"></script>
  93. <style type="text/css">
  94. #block1 { @test0@: propvalue; }
  95. </style>
  96. </head>
  97. <body>
  98. <div id="elem1" style="@test1@: propvalue;"></div>
  99. </body>
  100. </html>
  101. """
  102. converter = W3CTestConverter()
  103. fake_dir_path = self.fake_dir_path(converter, 'harnessandprops')
  104. test_content = self.generate_test_content(converter.prefixed_properties, 1, test_html)
  105. oc = OutputCapture()
  106. oc.capture_output()
  107. try:
  108. converted = converter.convert_html(fake_dir_path, test_content[1], DUMMY_FILENAME)
  109. finally:
  110. oc.restore_output()
  111. self.verify_conversion_happened(converted)
  112. self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1)
  113. self.verify_prefixed_properties(converted, test_content[0])
  114. def test_convert_for_webkit_harness_and_properties(self):
  115. """ 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 """
  116. test_html = """<html>
  117. <head>
  118. <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
  119. <script src="/resources/testharness.js"></script>
  120. <style type="text/css">
  121. #block1 { @test0@: propvalue; }
  122. #block2 { @test1@: propvalue; }
  123. #block3 { @test2@: propvalue; }
  124. </style>
  125. </head>
  126. <body>
  127. <div id="elem1" style="@test3@: propvalue;"></div>
  128. </body>
  129. </html>
  130. """
  131. converter = W3CTestConverter()
  132. fake_dir_path = self.fake_dir_path(converter, 'harnessandprops')
  133. oc = OutputCapture()
  134. oc.capture_output()
  135. try:
  136. test_content = self.generate_test_content(converter.prefixed_properties, 2, test_html)
  137. converted = converter.convert_html(fake_dir_path, test_content[1], DUMMY_FILENAME)
  138. finally:
  139. oc.restore_output()
  140. self.verify_conversion_happened(converted)
  141. self.verify_test_harness_paths(converter, converted[1], fake_dir_path, 1, 1)
  142. self.verify_prefixed_properties(converted, test_content[0])
  143. def test_convert_test_harness_paths(self):
  144. """ Tests convert_testharness_paths() with a test that uses all three testharness files """
  145. test_html = """<head>
  146. <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
  147. <script src="/resources/testharness.js"></script>
  148. <script src="/resources/testharnessreport.js"></script>
  149. </head>
  150. """
  151. converter = W3CTestConverter()
  152. fake_dir_path = self.fake_dir_path(converter, 'testharnesspaths')
  153. doc = BeautifulSoup(test_html)
  154. oc = OutputCapture()
  155. oc.capture_output()
  156. try:
  157. converted = converter.convert_testharness_paths(doc, fake_dir_path, DUMMY_FILENAME)
  158. finally:
  159. oc.restore_output()
  160. self.verify_conversion_happened(converted)
  161. self.verify_test_harness_paths(converter, doc, fake_dir_path, 2, 1)
  162. def test_convert_prefixed_properties(self):
  163. """ Tests convert_prefixed_properties() file that has 20 properties requiring the -webkit- prefix:
  164. 10 in one style block + 5 in another style
  165. block + 5 inline styles, including one with multiple prefixed properties.
  166. The properties in the test content are in all sorts of wack formatting.
  167. """
  168. test_html = """<html>
  169. <style type="text/css"><![CDATA[
  170. .block1 {
  171. width: 300px;
  172. height: 300px
  173. }
  174. .block2 {
  175. @test0@: propvalue;
  176. }
  177. .block3{@test1@: propvalue;}
  178. .block4 { @test2@:propvalue; }
  179. .block5{ @test3@ :propvalue; }
  180. #block6 { @test4@ : propvalue; }
  181. #block7
  182. {
  183. @test5@: propvalue;
  184. }
  185. #block8 { @test6@: propvalue; }
  186. #block9:pseudo
  187. {
  188. @test7@: propvalue;
  189. @test8@: propvalue propvalue propvalue;;
  190. }
  191. ]]></style>
  192. </head>
  193. <body>
  194. <div id="elem1" style="@test9@: propvalue;"></div>
  195. <div id="elem2" style="propname: propvalue; @test10@ : propvalue; propname:propvalue;"></div>
  196. <div id="elem2" style="@test11@: propvalue; @test12@ : propvalue; @test13@ :propvalue;"></div>
  197. <div id="elem3" style="@test14@:propvalue"></div>
  198. </body>
  199. <style type="text/css"><![CDATA[
  200. .block10{ @test15@: propvalue; }
  201. .block11{ @test16@: propvalue; }
  202. .block12{ @test17@: propvalue; }
  203. #block13:pseudo
  204. {
  205. @test18@: propvalue;
  206. @test19@: propvalue;
  207. }
  208. ]]></style>
  209. </html>
  210. """
  211. converter = W3CTestConverter()
  212. test_content = self.generate_test_content(converter.prefixed_properties, 20, test_html)
  213. oc = OutputCapture()
  214. oc.capture_output()
  215. try:
  216. converted = converter.convert_prefixed_properties(BeautifulSoup(test_content[1]), DUMMY_FILENAME)
  217. finally:
  218. oc.restore_output()
  219. self.verify_conversion_happened(converted)
  220. self.verify_prefixed_properties(converted, test_content[0])
  221. def verify_conversion_happened(self, converted):
  222. self.assertTrue(converted, "conversion didn't happen")
  223. def verify_no_conversion_happened(self, converted):
  224. self.assertEqual(converted, None, 'test should not have been converted')
  225. def verify_test_harness_paths(self, converter, converted, test_path, num_src_paths, num_href_paths):
  226. if isinstance(converted, basestring):
  227. converted = BeautifulSoup(converted)
  228. resources_dir = converter.path_from_webkit_root("LayoutTests", "resources")
  229. # Verify the original paths are gone, and the new paths are present.
  230. orig_path_pattern = re.compile('\"/resources/testharness')
  231. self.assertEquals(len(converted.findAll(src=orig_path_pattern)), 0, 'testharness src path was not converted')
  232. self.assertEquals(len(converted.findAll(href=orig_path_pattern)), 0, 'testharness href path was not converted')
  233. new_relpath = os.path.relpath(resources_dir, test_path)
  234. relpath_pattern = re.compile(new_relpath)
  235. self.assertEquals(len(converted.findAll(src=relpath_pattern)), num_src_paths, 'testharness src relative path not correct')
  236. self.assertEquals(len(converted.findAll(href=relpath_pattern)), num_href_paths, 'testharness href relative path not correct')
  237. def verify_prefixed_properties(self, converted, test_properties):
  238. self.assertEqual(len(converted[0]), len(test_properties), 'Incorrect number of properties converted')
  239. for test_prop in test_properties:
  240. self.assertTrue((test_prop in converted[1]), 'Property ' + test_prop + ' not found in converted doc')
  241. def generate_test_content(self, full_property_list, num_test_properties, html):
  242. """Inserts properties requiring a -webkit- prefix into the content, replacing \'@testXX@\' with a property."""
  243. test_properties = []
  244. count = 0
  245. while count < num_test_properties:
  246. test_properties.append(full_property_list[count])
  247. count += 1
  248. # Replace the tokens in the testhtml with the test properties. Walk backward
  249. # through the list to replace the double-digit tokens first
  250. index = len(test_properties) - 1
  251. while index >= 0:
  252. # Use the unprefixed version
  253. test_prop = test_properties[index].replace('-webkit-', '')
  254. # Replace the token
  255. html = html.replace('@test' + str(index) + '@', test_prop)
  256. index -= 1
  257. return (test_properties, html)