test_parser_unittest.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 unittest2 as unittest
  30. from webkitpy.common.system.outputcapture import OutputCapture
  31. from webkitpy.w3c.test_parser import TestParser
  32. options = {'all': False, 'no_overwrite': False}
  33. class TestParserTest(unittest.TestCase):
  34. def test_analyze_test_reftest_one_match(self):
  35. test_html = """<head>
  36. <link rel="match" href="green-box-ref.xht" />
  37. </head>
  38. """
  39. test_path = '/some/madeup/path/'
  40. parser = TestParser(options, test_path + 'somefile.html')
  41. test_info = parser.analyze_test(test_contents=test_html)
  42. self.assertNotEqual(test_info, None, 'did not find a test')
  43. self.assertTrue('test' in test_info.keys(), 'did not find a test file')
  44. self.assertTrue('reference' in test_info.keys(), 'did not find a reference file')
  45. self.assertTrue(test_info['reference'].startswith(test_path), 'reference path is not correct')
  46. self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
  47. self.assertFalse('jstest' in test_info.keys(), 'test should not have been analyzed as a jstest')
  48. def test_analyze_test_reftest_multiple_matches(self):
  49. test_html = """<head>
  50. <link rel="match" href="green-box-ref.xht" />
  51. <link rel="match" href="blue-box-ref.xht" />
  52. <link rel="match" href="orange-box-ref.xht" />
  53. </head>
  54. """
  55. oc = OutputCapture()
  56. oc.capture_output()
  57. try:
  58. test_path = '/some/madeup/path/'
  59. parser = TestParser(options, test_path + 'somefile.html')
  60. test_info = parser.analyze_test(test_contents=test_html)
  61. finally:
  62. output, _, _ = oc.restore_output()
  63. self.assertNotEqual(test_info, None, 'did not find a test')
  64. self.assertTrue('test' in test_info.keys(), 'did not find a test file')
  65. self.assertTrue('reference' in test_info.keys(), 'did not find a reference file')
  66. self.assertTrue(test_info['reference'].startswith(test_path), 'reference path is not correct')
  67. self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
  68. self.assertFalse('jstest' in test_info.keys(), 'test should not have been analyzed as a jstest')
  69. self.assertTrue(output.startswith('Warning'), 'no warning about multiple matches')
  70. def test_analyze_test_reftest_match_and_mismatch(self):
  71. test_html = """<head>
  72. <link rel="match" href="green-box-ref.xht" />
  73. <link rel="match" href="blue-box-ref.xht" />
  74. <link rel="mismatch" href="orange-box-notref.xht" />
  75. </head>
  76. """
  77. oc = OutputCapture()
  78. oc.capture_output()
  79. try:
  80. test_path = '/some/madeup/path/'
  81. parser = TestParser(options, test_path + 'somefile.html')
  82. test_info = parser.analyze_test(test_contents=test_html)
  83. finally:
  84. output, _, _ = oc.restore_output()
  85. self.assertNotEqual(test_info, None, 'did not find a test')
  86. self.assertTrue('test' in test_info.keys(), 'did not find a test file')
  87. self.assertTrue('reference' in test_info.keys(), 'did not find a reference file')
  88. self.assertTrue(test_info['reference'].startswith(test_path), 'reference path is not correct')
  89. self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
  90. self.assertFalse('jstest' in test_info.keys(), 'test should not have been analyzed as a jstest')
  91. warnings = output.splitlines()
  92. self.assertEquals(warnings, ["Warning: Webkit does not support multiple references. Importing the first ref defined in somefile.html"])
  93. def test_analyze_test_reftest_with_ref_support_Files(self):
  94. """ 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 """
  95. test_html = """<html>
  96. <head>
  97. <link rel="match" href="../reference/green-box-ref.xht" />
  98. </head>
  99. """
  100. ref_html = """<head>
  101. <link href="support/css/ref-stylesheet.css" rel="stylesheet" type="text/css">
  102. <style type="text/css">
  103. background-image: url("../../support/some-image.png")
  104. </style>
  105. </head>
  106. <body>
  107. <div><img src="../support/black96x96.png" alt="Image download support must be enabled" /></div>
  108. </body>
  109. </html>
  110. """
  111. test_path = '/some/madeup/path/'
  112. parser = TestParser(options, test_path + 'somefile.html')
  113. test_info = parser.analyze_test(test_contents=test_html, ref_contents=ref_html)
  114. self.assertNotEqual(test_info, None, 'did not find a test')
  115. self.assertTrue('test' in test_info.keys(), 'did not find a test file')
  116. self.assertTrue('reference' in test_info.keys(), 'did not find a reference file')
  117. self.assertTrue(test_info['reference'].startswith(test_path), 'reference path is not correct')
  118. self.assertTrue('refsupport' in test_info.keys(), 'there should be refsupport files for this test')
  119. self.assertEquals(len(test_info['refsupport']), 3, 'there should be 3 support files in this reference')
  120. self.assertFalse('jstest' in test_info.keys(), 'test should not have been analyzed as a jstest')
  121. def test_analyze_jstest(self):
  122. """ Tests analyze_test() using a jstest """
  123. test_html = """<head>
  124. <link href="/resources/testharness.css" rel="stylesheet" type="text/css">
  125. <script src="/resources/testharness.js"></script>
  126. </head>
  127. """
  128. test_path = '/some/madeup/path/'
  129. parser = TestParser(options, test_path + 'somefile.html')
  130. test_info = parser.analyze_test(test_contents=test_html)
  131. self.assertNotEqual(test_info, None, 'test_info is None')
  132. self.assertTrue('test' in test_info.keys(), 'did not find a test file')
  133. self.assertFalse('reference' in test_info.keys(), 'shold not have found a reference file')
  134. self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
  135. self.assertTrue('jstest' in test_info.keys(), 'test should be a jstest')
  136. def test_analyze_pixel_test_all_true(self):
  137. """ Tests analyze_test() using a test that is neither a reftest or jstest with all=False """
  138. test_html = """<html>
  139. <head>
  140. <title>CSS Test: DESCRIPTION OF TEST</title>
  141. <link rel="author" title="NAME_OF_AUTHOR" />
  142. <style type="text/css"><![CDATA[
  143. CSS FOR TEST
  144. ]]></style>
  145. </head>
  146. <body>
  147. CONTENT OF TEST
  148. </body>
  149. </html>
  150. """
  151. # Set options to 'all' so this gets found
  152. options['all'] = True
  153. test_path = '/some/madeup/path/'
  154. parser = TestParser(options, test_path + 'somefile.html')
  155. test_info = parser.analyze_test(test_contents=test_html)
  156. self.assertNotEqual(test_info, None, 'test_info is None')
  157. self.assertTrue('test' in test_info.keys(), 'did not find a test file')
  158. self.assertFalse('reference' in test_info.keys(), 'shold not have found a reference file')
  159. self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
  160. self.assertFalse('jstest' in test_info.keys(), 'test should not be a jstest')
  161. def test_analyze_pixel_test_all_false(self):
  162. """ Tests analyze_test() using a test that is neither a reftest or jstest, with -all=False """
  163. test_html = """<html>
  164. <head>
  165. <title>CSS Test: DESCRIPTION OF TEST</title>
  166. <link rel="author" title="NAME_OF_AUTHOR" />
  167. <style type="text/css"><![CDATA[
  168. CSS FOR TEST
  169. ]]></style>
  170. </head>
  171. <body>
  172. CONTENT OF TEST
  173. </body>
  174. </html>
  175. """
  176. # Set all to false so this gets skipped
  177. options['all'] = False
  178. test_path = '/some/madeup/path/'
  179. parser = TestParser(options, test_path + 'somefile.html')
  180. test_info = parser.analyze_test(test_contents=test_html)
  181. self.assertEqual(test_info, None, 'test should have been skipped')
  182. def test_analyze_non_html_file(self):
  183. """ Tests analyze_test() with a file that has no html"""
  184. # FIXME: use a mock filesystem
  185. parser = TestParser(options, os.path.join(os.path.dirname(__file__), 'test_parser.py'))
  186. test_info = parser.analyze_test()
  187. self.assertEqual(test_info, None, 'no tests should have been found in this file')