mock_drt_unittest.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # Copyright (C) 2011 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the name of Google Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. """Unit tests for MockDRT."""
  29. import sys
  30. import unittest2 as unittest
  31. from webkitpy.common import newstringio
  32. from webkitpy.common.system.systemhost_mock import MockSystemHost
  33. from webkitpy.port import mock_drt
  34. from webkitpy.port import port_testcase
  35. from webkitpy.port import test
  36. from webkitpy.port.factory import PortFactory
  37. from webkitpy.tool import mocktool
  38. mock_options = mocktool.MockOptions(configuration='Release')
  39. class MockDRTPortTest(port_testcase.PortTestCase):
  40. def make_port(self, options=mock_options):
  41. host = MockSystemHost()
  42. test.add_unit_tests_to_mock_filesystem(host.filesystem)
  43. return mock_drt.MockDRTPort(host, port_name='mock-mac-lion', options=options)
  44. def test_port_name_in_constructor(self):
  45. self.assertTrue(mock_drt.MockDRTPort(MockSystemHost(), port_name='mock-test'))
  46. def test_check_sys_deps(self):
  47. pass
  48. def test_diff_image(self):
  49. pass
  50. def test_diff_image_crashed(self):
  51. pass
  52. def test_uses_apache(self):
  53. pass
  54. def integration_test_http_lock(self):
  55. pass
  56. def integration_test_start_helper(self):
  57. pass
  58. def integration_test_http_server__normal(self):
  59. pass
  60. def integration_test_http_server__fails(self):
  61. pass
  62. def integration_test_websocket_server__normal(self):
  63. pass
  64. def integration_test_websocket_server__fails(self):
  65. pass
  66. def integration_test_helper(self):
  67. pass
  68. def test_get_crash_log(self):
  69. pass
  70. def test_check_build(self):
  71. pass
  72. class MockDRTTest(unittest.TestCase):
  73. def input_line(self, port, test_name, checksum=None):
  74. url = port.create_driver(0).test_to_uri(test_name)
  75. if url.startswith('file://'):
  76. url = url[len('file://'):]
  77. if checksum:
  78. return url + "'" + checksum + '\n'
  79. return url + '\n'
  80. def extra_args(self, pixel_tests):
  81. if pixel_tests:
  82. return ['--pixel-tests', '-']
  83. return ['-']
  84. def make_drt(self, options, args, host, stdin, stdout, stderr):
  85. return mock_drt.MockDRT(options, args, host, stdin, stdout, stderr)
  86. def make_input_output(self, port, test_name, pixel_tests,
  87. expected_checksum, drt_output, drt_input=None, expected_text=None):
  88. if pixel_tests:
  89. if not expected_checksum:
  90. expected_checksum = port.expected_checksum(test_name)
  91. if not drt_input:
  92. drt_input = self.input_line(port, test_name, expected_checksum)
  93. text_output = expected_text or port.expected_text(test_name) or ''
  94. if not drt_output:
  95. drt_output = self.expected_output(port, test_name, pixel_tests,
  96. text_output, expected_checksum)
  97. return (drt_input, drt_output)
  98. def expected_output(self, port, test_name, pixel_tests, text_output, expected_checksum):
  99. output = ['Content-Type: text/plain\n']
  100. if text_output:
  101. output.append(text_output)
  102. output.append('#EOF\n')
  103. if pixel_tests and expected_checksum:
  104. output.extend(['\n',
  105. 'ActualHash: %s\n' % expected_checksum,
  106. 'ExpectedHash: %s\n' % expected_checksum])
  107. output.append('#EOF\n')
  108. return output
  109. def assertTest(self, test_name, pixel_tests, expected_checksum=None, drt_output=None, host=None, expected_text=None):
  110. port_name = 'test'
  111. host = host or MockSystemHost()
  112. test.add_unit_tests_to_mock_filesystem(host.filesystem)
  113. port = PortFactory(host).get(port_name)
  114. drt_input, drt_output = self.make_input_output(port, test_name,
  115. pixel_tests, expected_checksum, drt_output, drt_input=None, expected_text=expected_text)
  116. args = ['--platform', port_name] + self.extra_args(pixel_tests)
  117. stdin = newstringio.StringIO(drt_input)
  118. stdout = newstringio.StringIO()
  119. stderr = newstringio.StringIO()
  120. options, args = mock_drt.parse_options(args)
  121. drt = self.make_drt(options, args, host, stdin, stdout, stderr)
  122. res = drt.run()
  123. self.assertEqual(res, 0)
  124. # We use the StringIO.buflist here instead of getvalue() because
  125. # the StringIO might be a mix of unicode/ascii and 8-bit strings.
  126. self.assertEqual(stdout.buflist, drt_output)
  127. self.assertEqual(stderr.getvalue(), '' if options.test_shell else '#EOF\n')
  128. def test_main(self):
  129. host = MockSystemHost()
  130. test.add_unit_tests_to_mock_filesystem(host.filesystem)
  131. stdin = newstringio.StringIO()
  132. stdout = newstringio.StringIO()
  133. stderr = newstringio.StringIO()
  134. res = mock_drt.main(['--platform', 'test'] + self.extra_args(False),
  135. host, stdin, stdout, stderr)
  136. self.assertEqual(res, 0)
  137. self.assertEqual(stdout.getvalue(), '')
  138. self.assertEqual(stderr.getvalue(), '')
  139. self.assertEqual(host.filesystem.written_files, {})
  140. def test_pixeltest_passes(self):
  141. # This also tests that we handle HTTP: test URLs properly.
  142. self.assertTest('http/tests/passes/text.html', True)
  143. def test_pixeltest__fails(self):
  144. self.assertTest('failures/expected/image_checksum.html', pixel_tests=True,
  145. expected_checksum='image_checksum-checksum',
  146. drt_output=['Content-Type: text/plain\n',
  147. 'image_checksum-txt',
  148. '#EOF\n',
  149. '\n',
  150. 'ActualHash: image_checksum-checksum\n',
  151. 'ExpectedHash: image_checksum-checksum\n',
  152. '#EOF\n'])
  153. def test_textonly(self):
  154. self.assertTest('passes/image.html', False)
  155. def test_checksum_in_png(self):
  156. self.assertTest('passes/checksum_in_image.html', True)
  157. def test_missing_image(self):
  158. self.assertTest('failures/expected/missing_image.html', True)
  159. def test_missing_text(self):
  160. self.assertTest('failures/expected/missing_text.html', True)
  161. def test_reftest_match(self):
  162. self.assertTest('passes/reftest.html', False, expected_checksum='mock-checksum', expected_text='reference text\n')
  163. self.assertTest('passes/reftest.html', True, expected_checksum='mock-checksum', expected_text='reference text\n')
  164. def test_reftest_mismatch(self):
  165. self.assertTest('passes/mismatch.html', False, expected_checksum='mock-checksum', expected_text='reference text\n')
  166. self.assertTest('passes/mismatch.html', True, expected_checksum='mock-checksum', expected_text='reference text\n')
  167. class MockTestShellTest(MockDRTTest):
  168. def extra_args(self, pixel_tests):
  169. if pixel_tests:
  170. return ['--pixel-tests=/tmp/png_result0.png']
  171. return []
  172. def make_drt(self, options, args, host, stdin, stdout, stderr):
  173. options.test_shell = True
  174. # We have to set these by hand because --platform test won't trigger
  175. # the Chromium code paths.
  176. options.pixel_path = '/tmp/png_result0.png'
  177. options.pixel_tests = True
  178. return mock_drt.MockTestShell(options, args, host, stdin, stdout, stderr)
  179. def input_line(self, port, test_name, checksum=None):
  180. url = port.create_driver(0).test_to_uri(test_name)
  181. if checksum:
  182. return url + ' 6000 ' + checksum + '\n'
  183. return url + ' 6000\n'
  184. def expected_output(self, port, test_name, pixel_tests, text_output, expected_checksum):
  185. url = port.create_driver(0).test_to_uri(test_name)
  186. output = ['#URL:%s\n' % url]
  187. if expected_checksum:
  188. output.append('#MD5:%s\n' % expected_checksum)
  189. if text_output:
  190. output.append(text_output)
  191. if not text_output.endswith('\n'):
  192. output.append('\n')
  193. output.append('#EOF\n')
  194. return output
  195. def test_pixeltest__fails(self):
  196. host = MockSystemHost()
  197. url = '#URL:file://'
  198. url = url + '%s/failures/expected/image_checksum.html' % PortFactory(host).get('test').layout_tests_dir()
  199. self.assertTest('failures/expected/image_checksum.html', pixel_tests=True,
  200. expected_checksum='image_checksum',
  201. drt_output=[url + '\n',
  202. '#MD5:image_checksum-checksum\n',
  203. 'image_checksum-txt',
  204. '\n',
  205. '#EOF\n'],
  206. host=host)
  207. self.assertEqual(host.filesystem.written_files,
  208. {'/tmp/png_result0.png': 'image_checksum\x8a-pngtEXtchecksum\x00image_checksum-checksum'})
  209. def test_test_shell_parse_options(self):
  210. options, args = mock_drt.parse_options(['--platform', 'chromium-mac', '--test-shell',
  211. '--pixel-tests=/tmp/png_result0.png'])
  212. self.assertTrue(options.test_shell)
  213. self.assertTrue(options.pixel_tests)
  214. self.assertEqual(options.pixel_path, '/tmp/png_result0.png')