PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/WebKit/Tools/Scripts/webkitpy/layout_tests/views/printing_unittest.py

https://gitlab.com/jonnialva90/iridium-browser
Python | 251 lines | 188 code | 33 blank | 30 comment | 4 complexity | e79121413df07b993172d667f1084027 MD5 | raw file
  1. # Copyright (C) 2010, 2012 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 printing.py."""
  29. import StringIO
  30. import optparse
  31. import sys
  32. import time
  33. import unittest
  34. from webkitpy.common.host_mock import MockHost
  35. from webkitpy.common.system import logtesting
  36. from webkitpy.layout_tests import port
  37. from webkitpy.layout_tests.controllers import manager
  38. from webkitpy.layout_tests.models import test_expectations
  39. from webkitpy.layout_tests.models import test_failures
  40. from webkitpy.layout_tests.models import test_results
  41. from webkitpy.layout_tests.views import printing
  42. def get_options(args):
  43. print_options = printing.print_options()
  44. option_parser = optparse.OptionParser(option_list=print_options)
  45. return option_parser.parse_args(args)
  46. class TestUtilityFunctions(unittest.TestCase):
  47. def test_print_options(self):
  48. options, args = get_options([])
  49. self.assertIsNotNone(options)
  50. class FakeRunResults(object):
  51. def __init__(self, total=1, expected=1, unexpected=0, fake_results=None):
  52. fake_results = fake_results or []
  53. self.total = total
  54. self.expected = expected
  55. self.expected_failures = 0
  56. self.unexpected = unexpected
  57. self.expected_skips = 0
  58. self.results_by_name = {}
  59. total_run_time = 0
  60. for result in fake_results:
  61. self.results_by_name[result.shard_name] = result
  62. total_run_time += result.total_run_time
  63. self.run_time = total_run_time + 1
  64. class FakeShard(object):
  65. def __init__(self, shard_name, total_run_time):
  66. self.shard_name = shard_name
  67. self.total_run_time = total_run_time
  68. class Testprinter(unittest.TestCase):
  69. def assertEmpty(self, stream):
  70. self.assertFalse(stream.getvalue())
  71. def assertNotEmpty(self, stream):
  72. self.assertTrue(stream.getvalue())
  73. def assertWritten(self, stream, contents):
  74. self.assertEqual(stream.buflist, contents)
  75. def reset(self, stream):
  76. stream.buflist = []
  77. stream.buf = ''
  78. def get_printer(self, args=None):
  79. args = args or []
  80. printing_options = printing.print_options()
  81. option_parser = optparse.OptionParser(option_list=printing_options)
  82. options, args = option_parser.parse_args(args)
  83. host = MockHost()
  84. self._port = host.port_factory.get('test', options)
  85. nproc = 2
  86. regular_output = StringIO.StringIO()
  87. printer = printing.Printer(self._port, options, regular_output)
  88. return printer, regular_output
  89. def get_result(self, test_name, result_type=test_expectations.PASS, run_time=0):
  90. failures = []
  91. if result_type == test_expectations.TIMEOUT:
  92. failures = [test_failures.FailureTimeout()]
  93. elif result_type == test_expectations.CRASH:
  94. failures = [test_failures.FailureCrash()]
  95. return test_results.TestResult(test_name, failures=failures, test_run_time=run_time)
  96. def test_configure_and_cleanup(self):
  97. # This test verifies that calling cleanup repeatedly and deleting
  98. # the object is safe.
  99. printer, err = self.get_printer()
  100. printer.cleanup()
  101. printer.cleanup()
  102. printer = None
  103. def test_print_config(self):
  104. printer, err = self.get_printer()
  105. # FIXME: it's lame that i have to set these options directly.
  106. printer._options.pixel_tests = True
  107. printer._options.new_baseline = True
  108. printer._options.time_out_ms = 6000
  109. printer._options.slow_time_out_ms = 12000
  110. printer.print_config('/tmp')
  111. self.assertIn("Using port 'test-mac-leopard'", err.getvalue())
  112. self.assertIn('Test configuration: <leopard, x86, release>', err.getvalue())
  113. self.assertIn('View the test results at file:///tmp', err.getvalue())
  114. self.assertIn('View the archived results dashboard at file:///tmp', err.getvalue())
  115. self.assertIn('Baseline search path: test-mac-leopard -> test-mac-snowleopard -> generic', err.getvalue())
  116. self.assertIn('Using Release build', err.getvalue())
  117. self.assertIn('Pixel tests enabled', err.getvalue())
  118. self.assertIn('Command line:', err.getvalue())
  119. self.assertIn('Regular timeout: ', err.getvalue())
  120. self.reset(err)
  121. printer._options.quiet = True
  122. printer.print_config('/tmp')
  123. self.assertNotIn('Baseline search path: test-mac-leopard -> test-mac-snowleopard -> generic', err.getvalue())
  124. def test_print_directory_timings(self):
  125. printer, err = self.get_printer()
  126. printer._options.debug_rwt_logging = True
  127. run_results = FakeRunResults()
  128. run_results.results_by_name = {
  129. "slowShard": FakeShard("slowShard", 16),
  130. "borderlineShard": FakeShard("borderlineShard", 15),
  131. "fastShard": FakeShard("fastShard", 1),
  132. }
  133. printer._print_directory_timings(run_results)
  134. self.assertWritten(err, ['Time to process slowest subdirectories:\n', ' slowShard took 16.0 seconds to run 1 tests.\n', '\n'])
  135. printer, err = self.get_printer()
  136. printer._options.debug_rwt_logging = True
  137. run_results.results_by_name = {
  138. "borderlineShard": FakeShard("borderlineShard", 15),
  139. "fastShard": FakeShard("fastShard", 1),
  140. }
  141. printer._print_directory_timings(run_results)
  142. self.assertWritten(err, [])
  143. def test_print_one_line_summary(self):
  144. def run_test(total, exp, unexp, shards, result):
  145. printer, err = self.get_printer(['--timing'] if shards else None)
  146. fake_results = FakeRunResults(total, exp, unexp, shards)
  147. total_time = fake_results.run_time + 1
  148. printer._print_one_line_summary(total_time, fake_results)
  149. self.assertWritten(err, result)
  150. # Without times:
  151. run_test(1, 1, 0, [], ["The test ran as expected.\n", "\n"])
  152. run_test(2, 1, 1, [], ["\n", "1 test ran as expected, 1 didn't:\n", "\n"])
  153. run_test(3, 2, 1, [], ["\n", "2 tests ran as expected, 1 didn't:\n", "\n"])
  154. run_test(3, 2, 0, [], ["\n", "2 tests ran as expected (1 didn't run).\n", "\n"])
  155. # With times:
  156. fake_shards = [FakeShard("foo", 1), FakeShard("bar", 2)]
  157. run_test(1, 1, 0, fake_shards, ["The test ran as expected in 5.00s (2.00s in rwt, 1x).\n", "\n"])
  158. run_test(2, 1, 1, fake_shards, ["\n", "1 test ran as expected, 1 didn't in 5.00s (2.00s in rwt, 1x):\n", "\n"])
  159. run_test(3, 2, 1, fake_shards, ["\n", "2 tests ran as expected, 1 didn't in 5.00s (2.00s in rwt, 1x):\n", "\n"])
  160. run_test(3, 2, 0, fake_shards, ["\n", "2 tests ran as expected (1 didn't run) in 5.00s (2.00s in rwt, 1x).\n", "\n"])
  161. def test_test_status_line(self):
  162. printer, _ = self.get_printer()
  163. printer._meter.number_of_columns = lambda: 80
  164. actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
  165. self.assertEqual(80, len(actual))
  166. self.assertEqual(actual, '[0/0] fast/dom/HTMLFormElement/associa...after-index-assertion-fail1.html passed')
  167. printer._meter.number_of_columns = lambda: 89
  168. actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
  169. self.assertEqual(89, len(actual))
  170. self.assertEqual(actual, '[0/0] fast/dom/HTMLFormElement/associated-...ents-after-index-assertion-fail1.html passed')
  171. printer._meter.number_of_columns = lambda: sys.maxint
  172. actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
  173. self.assertEqual(90, len(actual))
  174. self.assertEqual(actual, '[0/0] fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html passed')
  175. printer._meter.number_of_columns = lambda: 18
  176. actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
  177. self.assertEqual(18, len(actual))
  178. self.assertEqual(actual, '[0/0] f...l passed')
  179. printer._meter.number_of_columns = lambda: 10
  180. actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
  181. self.assertEqual(actual, '[0/0] associated-elements-after-index-assertion-fail1.html passed')
  182. def test_details(self):
  183. printer, err = self.get_printer(['--details'])
  184. result = self.get_result('passes/image.html')
  185. printer.print_started_test('passes/image.html')
  186. printer.print_finished_test(result, expected=False, exp_str='', got_str='')
  187. self.assertNotEmpty(err)
  188. def test_print_found(self):
  189. printer, err = self.get_printer()
  190. printer.print_found(100, 10, 1, 1)
  191. self.assertWritten(err, ["Found 100 tests; running 10, skipping 90.\n"])
  192. self.reset(err)
  193. printer.print_found(100, 10, 2, 3)
  194. self.assertWritten(err, ["Found 100 tests; running 10 (6 times each: --repeat-each=2 --iterations=3), skipping 90.\n"])
  195. def test_debug_rwt_logging_is_throttled(self):
  196. printer, err = self.get_printer(['--debug-rwt-logging'])
  197. result = self.get_result('passes/image.html')
  198. printer.print_started_test('passes/image.html')
  199. printer.print_finished_test(result, expected=True, exp_str='', got_str='')
  200. printer.print_started_test('passes/text.html')
  201. result = self.get_result('passes/text.html')
  202. printer.print_finished_test(result, expected=True, exp_str='', got_str='')
  203. # Only the first test's start should be printed.
  204. lines = err.buflist
  205. self.assertEqual(len(lines), 1)
  206. self.assertTrue(lines[0].endswith('passes/image.html\n'))