1888692766020d1e77760c668f7d6dc2ccdb4f8a
[WebKit-https.git] / Tools / Scripts / webkitpy / layout_tests / views / printing_unittest.py
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
29 """Unit tests for printing.py."""
30
31 import StringIO
32 import optparse
33 import sys
34 import time
35 import unittest
36
37 from webkitpy.common.host_mock import MockHost
38
39 from webkitpy.common.system import logtesting
40 from webkitpy import port
41 from webkitpy.layout_tests.controllers import manager
42 from webkitpy.layout_tests.models import test_expectations
43 from webkitpy.layout_tests.models import test_failures
44 from webkitpy.layout_tests.models import test_results
45 from webkitpy.layout_tests.views import printing
46
47
48 def get_options(args):
49     print_options = printing.print_options()
50     option_parser = optparse.OptionParser(option_list=print_options)
51     return option_parser.parse_args(args)
52
53
54 class TestUtilityFunctions(unittest.TestCase):
55     def test_print_options(self):
56         options, args = get_options([])
57         self.assertIsNotNone(options)
58
59
60 class  Testprinter(unittest.TestCase):
61     def assertEmpty(self, stream):
62         self.assertFalse(stream.getvalue())
63
64     def assertNotEmpty(self, stream):
65         self.assertTrue(stream.getvalue())
66
67     def assertWritten(self, stream, contents):
68         self.assertEqual(stream.buflist, contents)
69
70     def reset(self, stream):
71         stream.buflist = []
72         stream.buf = ''
73
74     def get_printer(self, args=None):
75         args = args or []
76         printing_options = printing.print_options()
77         option_parser = optparse.OptionParser(option_list=printing_options)
78         options, args = option_parser.parse_args(args)
79         host = MockHost()
80         self._port = host.port_factory.get('test', options)
81         nproc = 2
82
83         regular_output = StringIO.StringIO()
84         printer = printing.Printer(self._port, options, regular_output)
85         return printer, regular_output
86
87     def get_result(self, test_name, result_type=test_expectations.PASS, run_time=0):
88         failures = []
89         if result_type == test_expectations.TIMEOUT:
90             failures = [test_failures.FailureTimeout()]
91         elif result_type == test_expectations.CRASH:
92             failures = [test_failures.FailureCrash()]
93         return test_results.TestResult(test_name, failures=failures, test_run_time=run_time)
94
95     def test_configure_and_cleanup(self):
96         # This test verifies that calling cleanup repeatedly and deleting
97         # the object is safe.
98         printer, err = self.get_printer()
99         printer.cleanup()
100         printer.cleanup()
101         printer = None
102
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('Placing test results in /tmp', err.getvalue())
114         self.assertIn('Baseline search path: platform/test-mac-leopard -> platform/test-mac-snowleopard -> generic', err.getvalue())
115         self.assertIn('Using Release build', err.getvalue())
116         self.assertIn('Pixel tests enabled', err.getvalue())
117         self.assertIn('Command line:', err.getvalue())
118         self.assertIn('Regular timeout: ', err.getvalue())
119
120         self.reset(err)
121         printer._options.quiet = True
122         printer.print_config('/tmp')
123         self.assertNotIn('Baseline search path: platform/test-mac-leopard -> platform/test-mac-snowleopard -> generic', err.getvalue())
124
125     def test_print_one_line_summary(self):
126         printer, err = self.get_printer()
127         printer._print_one_line_summary(1, 1, 0)
128         self.assertWritten(err, ["The test ran as expected.\n", "\n"])
129
130         printer, err = self.get_printer()
131         printer._print_one_line_summary(1, 1, 0)
132         self.assertWritten(err, ["The test ran as expected.\n", "\n"])
133
134         printer, err = self.get_printer()
135         printer._print_one_line_summary(2, 1, 1)
136         self.assertWritten(err, ["\n", "1 test ran as expected, 1 didn't:\n", "\n"])
137
138         printer, err = self.get_printer()
139         printer._print_one_line_summary(3, 2, 1)
140         self.assertWritten(err, ["\n", "2 tests ran as expected, 1 didn't:\n", "\n"])
141
142         printer, err = self.get_printer()
143         printer._print_one_line_summary(3, 2, 0)
144         self.assertWritten(err, ['\n', "2 tests ran as expected (1 didn't run).\n", '\n'])
145
146     def test_test_status_line(self):
147         printer, _ = self.get_printer()
148         printer._meter.number_of_columns = lambda: 80
149         actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
150         self.assertEqual(80, len(actual))
151         self.assertEqual(actual, '[0/0] fast/dom/HTMLFormElement/associa...after-index-assertion-fail1.html passed')
152
153         printer._meter.number_of_columns = lambda: 80
154         actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed', truncate=False)
155         self.assertEqual(90, len(actual))
156         self.assertEqual(actual, '[0/0] fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html passed')
157
158         printer._meter.number_of_columns = lambda: 89
159         actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
160         self.assertEqual(89, len(actual))
161         self.assertEqual(actual, '[0/0] fast/dom/HTMLFormElement/associated-...ents-after-index-assertion-fail1.html passed')
162
163         printer._meter.number_of_columns = lambda: sys.maxint
164         actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
165         self.assertEqual(90, len(actual))
166         self.assertEqual(actual, '[0/0] fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html passed')
167
168         printer._meter.number_of_columns = lambda: 18
169         actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
170         self.assertEqual(18, len(actual))
171         self.assertEqual(actual, '[0/0] f...l passed')
172
173         printer._meter.number_of_columns = lambda: 10
174         actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
175         self.assertEqual(actual, '[0/0] associated-elements-after-index-assertion-fail1.html passed')
176
177     def test_details(self):
178         printer, err = self.get_printer(['--details'])
179         result = self.get_result('passes/image.html')
180         printer.print_started_test('passes/image.html')
181         printer.print_finished_test(result, expected=False, exp_str='', got_str='')
182         self.assertNotEmpty(err)
183
184     def test_print_found(self):
185         printer, err = self.get_printer()
186
187         printer.print_found(100, 10, 1, 1)
188         self.assertWritten(err, ["Found 100 tests; running 10, skipping 90.\n"])
189
190         self.reset(err)
191         printer.print_found(100, 10, 2, 3)
192         self.assertWritten(err, ["Found 100 tests; running 10 (6 times each: --repeat-each=2 --iterations=3), skipping 90.\n"])