driver_unittest.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. # Copyright (C) 2010 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. import unittest2 as unittest
  29. from webkitpy.common.system.systemhost_mock import MockSystemHost
  30. from webkitpy.port import Port, Driver, DriverOutput
  31. from webkitpy.port.server_process_mock import MockServerProcess
  32. # FIXME: remove the dependency on TestWebKitPort
  33. from webkitpy.port.port_testcase import TestWebKitPort
  34. from webkitpy.tool.mocktool import MockOptions
  35. class DriverOutputTest(unittest.TestCase):
  36. def test_strip_metrics(self):
  37. patterns = [
  38. ('RenderView at (0,0) size 800x600', 'RenderView '),
  39. ('text run at (0,0) width 100: "some text"', '"some text"'),
  40. ('RenderBlock {HTML} at (0,0) size 800x600', 'RenderBlock {HTML} '),
  41. ('RenderBlock {INPUT} at (29,3) size 12x12 [color=#000000]', 'RenderBlock {INPUT}'),
  42. ('RenderBlock (floating) {DT} at (5,5) size 79x310 [border: (5px solid #000000)]',
  43. 'RenderBlock (floating) {DT} [border: px solid #000000)]'),
  44. ('\n "truncate text "\n', '\n "truncate text"\n'),
  45. ('RenderText {#text} at (0,3) size 41x12\n text run at (0,3) width 41: "whimper "\n',
  46. 'RenderText {#text} \n "whimper"\n'),
  47. ("""text run at (0,0) width 109: ".one {color: green;}"
  48. text run at (109,0) width 0: " "
  49. text run at (0,17) width 81: ".1 {color: red;}"
  50. text run at (81,17) width 0: " "
  51. text run at (0,34) width 102: ".a1 {color: green;}"
  52. text run at (102,34) width 0: " "
  53. text run at (0,51) width 120: "P.two {color: purple;}"
  54. text run at (120,51) width 0: " "\n""",
  55. '".one {color: green;} .1 {color: red;} .a1 {color: green;} P.two {color: purple;}"\n'),
  56. ('text-- other text', 'text--other text'),
  57. (' some output "truncate trailing spaces at end of line after text" \n',
  58. ' some output "truncate trailing spaces at end of line after text"\n'),
  59. (r'scrollWidth 120', r'scrollWidth'),
  60. (r'scrollHeight 120', r'scrollHeight'),
  61. ]
  62. for pattern in patterns:
  63. driver_output = DriverOutput(pattern[0], None, None, None)
  64. driver_output.strip_metrics()
  65. self.assertEqual(driver_output.text, pattern[1])
  66. class DriverTest(unittest.TestCase):
  67. def make_port(self):
  68. port = Port(MockSystemHost(), 'test', MockOptions(configuration='Release'))
  69. port._config.build_directory = lambda configuration: '/mock-build'
  70. return port
  71. def _assert_wrapper(self, wrapper_string, expected_wrapper):
  72. wrapper = Driver(self.make_port(), None, pixel_tests=False)._command_wrapper(wrapper_string)
  73. self.assertEqual(wrapper, expected_wrapper)
  74. def test_command_wrapper(self):
  75. self._assert_wrapper(None, [])
  76. self._assert_wrapper("valgrind", ["valgrind"])
  77. # Validate that shlex works as expected.
  78. command_with_spaces = "valgrind --smc-check=\"check with spaces!\" --foo"
  79. expected_parse = ["valgrind", "--smc-check=check with spaces!", "--foo"]
  80. self._assert_wrapper(command_with_spaces, expected_parse)
  81. def test_test_to_uri(self):
  82. port = self.make_port()
  83. driver = Driver(port, None, pixel_tests=False)
  84. self.assertEqual(driver.test_to_uri('foo/bar.html'), 'file://%s/foo/bar.html' % port.layout_tests_dir())
  85. self.assertEqual(driver.test_to_uri('http/tests/foo.html'), 'http://127.0.0.1:8000/foo.html')
  86. self.assertEqual(driver.test_to_uri('http/tests/ssl/bar.html'), 'https://127.0.0.1:8443/ssl/bar.html')
  87. def test_uri_to_test(self):
  88. port = self.make_port()
  89. driver = Driver(port, None, pixel_tests=False)
  90. self.assertEqual(driver.uri_to_test('file://%s/foo/bar.html' % port.layout_tests_dir()), 'foo/bar.html')
  91. self.assertEqual(driver.uri_to_test('http://127.0.0.1:8000/foo.html'), 'http/tests/foo.html')
  92. self.assertEqual(driver.uri_to_test('https://127.0.0.1:8443/ssl/bar.html'), 'http/tests/ssl/bar.html')
  93. def test_read_block(self):
  94. port = TestWebKitPort()
  95. driver = Driver(port, 0, pixel_tests=False)
  96. driver._server_process = MockServerProcess(lines=[
  97. 'ActualHash: foobar',
  98. 'Content-Type: my_type',
  99. 'Content-Transfer-Encoding: none',
  100. "#EOF",
  101. ])
  102. content_block = driver._read_block(0)
  103. self.assertEqual(content_block.content_type, 'my_type')
  104. self.assertEqual(content_block.encoding, 'none')
  105. self.assertEqual(content_block.content_hash, 'foobar')
  106. driver._server_process = None
  107. def test_read_binary_block(self):
  108. port = TestWebKitPort()
  109. driver = Driver(port, 0, pixel_tests=True)
  110. driver._server_process = MockServerProcess(lines=[
  111. 'ActualHash: actual',
  112. 'ExpectedHash: expected',
  113. 'Content-Type: image/png',
  114. 'Content-Length: 9',
  115. "12345678",
  116. "#EOF",
  117. ])
  118. content_block = driver._read_block(0)
  119. self.assertEqual(content_block.content_type, 'image/png')
  120. self.assertEqual(content_block.content_hash, 'actual')
  121. self.assertEqual(content_block.content, '12345678\n')
  122. self.assertEqual(content_block.decoded_content, '12345678\n')
  123. driver._server_process = None
  124. def test_read_base64_block(self):
  125. port = TestWebKitPort()
  126. driver = Driver(port, 0, pixel_tests=True)
  127. driver._server_process = MockServerProcess(lines=[
  128. 'ActualHash: actual',
  129. 'ExpectedHash: expected',
  130. 'Content-Type: image/png',
  131. 'Content-Transfer-Encoding: base64',
  132. 'Content-Length: 12',
  133. 'MTIzNDU2NzgK#EOF',
  134. ])
  135. content_block = driver._read_block(0)
  136. self.assertEqual(content_block.content_type, 'image/png')
  137. self.assertEqual(content_block.content_hash, 'actual')
  138. self.assertEqual(content_block.encoding, 'base64')
  139. self.assertEqual(content_block.content, 'MTIzNDU2NzgK')
  140. self.assertEqual(content_block.decoded_content, '12345678\n')
  141. def test_no_timeout(self):
  142. port = TestWebKitPort()
  143. port._config.build_directory = lambda configuration: '/mock-build'
  144. driver = Driver(port, 0, pixel_tests=True, no_timeout=True)
  145. self.assertEqual(driver.cmd_line(True, []), ['/mock-build/DumpRenderTree', '--no-timeout', '-'])
  146. def test_check_for_driver_crash(self):
  147. port = TestWebKitPort()
  148. driver = Driver(port, 0, pixel_tests=True)
  149. class FakeServerProcess(object):
  150. def __init__(self, crashed):
  151. self.crashed = crashed
  152. def pid(self):
  153. return 1234
  154. def name(self):
  155. return 'FakeServerProcess'
  156. def has_crashed(self):
  157. return self.crashed
  158. def stop(self, timeout):
  159. pass
  160. def assert_crash(driver, error_line, crashed, name, pid, unresponsive=False):
  161. self.assertEqual(driver._check_for_driver_crash(error_line), crashed)
  162. self.assertEqual(driver._crashed_process_name, name)
  163. self.assertEqual(driver._crashed_pid, pid)
  164. self.assertEqual(driver._subprocess_was_unresponsive, unresponsive)
  165. driver.stop()
  166. driver._server_process = FakeServerProcess(False)
  167. assert_crash(driver, '', False, None, None)
  168. driver._crashed_process_name = None
  169. driver._crashed_pid = None
  170. driver._server_process = FakeServerProcess(False)
  171. driver._subprocess_was_unresponsive = False
  172. assert_crash(driver, '#CRASHED\n', True, 'FakeServerProcess', 1234)
  173. driver._crashed_process_name = None
  174. driver._crashed_pid = None
  175. driver._server_process = FakeServerProcess(False)
  176. driver._subprocess_was_unresponsive = False
  177. assert_crash(driver, '#CRASHED - WebProcess\n', True, 'WebProcess', None)
  178. driver._crashed_process_name = None
  179. driver._crashed_pid = None
  180. driver._server_process = FakeServerProcess(False)
  181. driver._subprocess_was_unresponsive = False
  182. assert_crash(driver, '#CRASHED - WebProcess (pid 8675)\n', True, 'WebProcess', 8675)
  183. driver._crashed_process_name = None
  184. driver._crashed_pid = None
  185. driver._server_process = FakeServerProcess(False)
  186. driver._subprocess_was_unresponsive = False
  187. assert_crash(driver, '#PROCESS UNRESPONSIVE - WebProcess (pid 8675)\n', True, 'WebProcess', 8675, True)
  188. driver._crashed_process_name = None
  189. driver._crashed_pid = None
  190. driver._server_process = FakeServerProcess(False)
  191. driver._subprocess_was_unresponsive = False
  192. assert_crash(driver, '#CRASHED - renderer (pid 8675)\n', True, 'renderer', 8675)
  193. driver._crashed_process_name = None
  194. driver._crashed_pid = None
  195. driver._server_process = FakeServerProcess(True)
  196. driver._subprocess_was_unresponsive = False
  197. assert_crash(driver, '', True, 'FakeServerProcess', 1234)
  198. def test_creating_a_port_does_not_write_to_the_filesystem(self):
  199. port = TestWebKitPort()
  200. driver = Driver(port, 0, pixel_tests=True)
  201. self.assertEqual(port._filesystem.written_files, {})
  202. self.assertEqual(port._filesystem.last_tmpdir, None)
  203. def test_stop_cleans_up_properly(self):
  204. port = TestWebKitPort()
  205. port._server_process_constructor = MockServerProcess
  206. driver = Driver(port, 0, pixel_tests=True)
  207. driver.start(True, [])
  208. last_tmpdir = port._filesystem.last_tmpdir
  209. self.assertNotEquals(last_tmpdir, None)
  210. driver.stop()
  211. self.assertFalse(port._filesystem.isdir(last_tmpdir))
  212. def test_two_starts_cleans_up_properly(self):
  213. port = TestWebKitPort()
  214. port._server_process_constructor = MockServerProcess
  215. driver = Driver(port, 0, pixel_tests=True)
  216. driver.start(True, [])
  217. last_tmpdir = port._filesystem.last_tmpdir
  218. driver._start(True, [])
  219. self.assertFalse(port._filesystem.isdir(last_tmpdir))
  220. def test_start_actually_starts(self):
  221. port = TestWebKitPort()
  222. port._server_process_constructor = MockServerProcess
  223. driver = Driver(port, 0, pixel_tests=True)
  224. driver.start(True, [])
  225. self.assertTrue(driver._server_process.started)