2 # Copyright (C) 2011 Google Inc. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 """Unit tests for MockDRT."""
35 from webkitpy.common import newstringio
37 from webkitpy.layout_tests.port import mock_drt
38 from webkitpy.layout_tests.port import factory
39 from webkitpy.layout_tests.port import port_testcase
40 from webkitpy.layout_tests.port import test
42 from webkitpy.tool import mocktool
43 mock_options = mocktool.MockOptions(configuration='Release')
46 class MockDRTPortTest(port_testcase.PortTestCase):
47 def make_port(self, options=mock_options):
48 if sys.platform == 'win32':
49 # We use this because the 'win' port doesn't work yet.
50 return mock_drt.MockDRTPort(port_name='mock-chromium-win', options=options)
51 return mock_drt.MockDRTPort(options=options)
53 def test_default_worker_model(self):
54 # only overridding the default test; we don't care about this one.
57 def test_port_name_in_constructor(self):
58 self.assertTrue(mock_drt.MockDRTPort(port_name='mock-test'))
60 def test_check_sys_deps(self):
63 def test_uses_apache(self):
66 def integration_test_http_lock(self):
69 def integration_test_start_helper(self):
72 def integration_test_http_server__normal(self):
75 def integration_test_http_server__fails(self):
78 def integration_test_websocket_server__normal(self):
81 def integration_test_websocket_server__fails(self):
84 def integration_test_helper(self):
88 class MockDRTTest(unittest.TestCase):
89 def input_line(self, port, test_name, checksum=None):
90 url = port.test_to_uri(test_name)
91 # FIXME: we shouldn't have to work around platform-specific issues
93 if url.startswith('file:////'):
94 url = url[len('file:////') - 1:]
95 if url.startswith('file:///'):
96 url = url[len('file:///') - 1:]
99 return url + "'" + checksum + '\n'
102 def extra_args(self, pixel_tests):
104 return ['--pixel-tests', '-']
107 def make_drt(self, options, args, filesystem, stdin, stdout, stderr):
108 return mock_drt.MockDRT(options, args, filesystem, stdin, stdout, stderr)
110 def make_input_output(self, port, test_name, pixel_tests,
111 expected_checksum, drt_output, drt_input=None):
113 if not expected_checksum:
114 expected_checksum = port.expected_checksum(test_name)
116 drt_input = self.input_line(port, test_name, expected_checksum)
117 text_output = port.expected_text(test_name)
120 drt_output = self.expected_output(port, test_name, pixel_tests,
121 text_output, expected_checksum)
122 return (drt_input, drt_output)
124 def expected_output(self, port, test_name, pixel_tests, text_output, expected_checksum):
125 if pixel_tests and expected_checksum:
126 return ['Content-Type: text/plain\n',
130 'ActualHash: %s\n' % expected_checksum,
131 'ExpectedHash: %s\n' % expected_checksum,
134 return ['Content-Type: text/plain\n',
139 def assertTest(self, test_name, pixel_tests, expected_checksum=None,
140 drt_output=None, filesystem=None):
142 filesystem = filesystem or test.unit_test_filesystem()
143 port = factory.get(platform, filesystem=filesystem)
144 drt_input, drt_output = self.make_input_output(port, test_name,
145 pixel_tests, expected_checksum, drt_output)
147 args = ['--platform', 'test'] + self.extra_args(pixel_tests)
148 stdin = newstringio.StringIO(drt_input)
149 stdout = newstringio.StringIO()
150 stderr = newstringio.StringIO()
151 options, args = mock_drt.parse_options(args)
153 drt = self.make_drt(options, args, filesystem, stdin, stdout, stderr)
156 self.assertEqual(res, 0)
158 # We use the StringIO.buflist here instead of getvalue() because
159 # the StringIO might be a mix of unicode/ascii and 8-bit strings.
160 self.assertEqual(stdout.buflist, drt_output)
161 self.assertEqual(stderr.getvalue(), '')
164 filesystem = test.unit_test_filesystem()
165 stdin = newstringio.StringIO()
166 stdout = newstringio.StringIO()
167 stderr = newstringio.StringIO()
168 res = mock_drt.main(['--platform', 'test'] + self.extra_args(False),
169 filesystem, stdin, stdout, stderr)
170 self.assertEqual(res, 0)
171 self.assertEqual(stdout.getvalue(), '')
172 self.assertEqual(stderr.getvalue(), '')
173 self.assertEqual(filesystem.written_files, {})
175 def test_pixeltest_passes(self):
176 # This also tests that we handle HTTP: test URLs properly.
177 self.assertTest('http/tests/passes/text.html', True)
179 def test_pixeltest__fails(self):
180 self.assertTest('failures/expected/checksum.html', pixel_tests=True,
181 expected_checksum='wrong-checksum',
182 drt_output=['Content-Type: text/plain\n',
186 'ActualHash: checksum-checksum\n',
187 'ExpectedHash: wrong-checksum\n',
188 'Content-Type: image/png\n',
189 'Content-Length: 43\n',
190 'checksum\x8a-pngtEXtchecksum\x00checksum-checksum',
193 def test_textonly(self):
194 self.assertTest('passes/image.html', False)
196 def test_checksum_in_png(self):
197 self.assertTest('passes/checksum_in_image.html', True)
200 class MockChromiumDRTTest(MockDRTTest):
201 def extra_args(self, pixel_tests):
203 return ['--pixel-tests=/tmp/png_result0.png']
206 def make_drt(self, options, args, filesystem, stdin, stdout, stderr):
207 options.chromium = True
209 # We have to set these by hand because --platform test won't trigger
210 # the Chromium code paths.
211 options.pixel_path = '/tmp/png_result0.png'
212 options.pixel_tests = True
214 return mock_drt.MockChromiumDRT(options, args, filesystem, stdin, stdout, stderr)
216 def input_line(self, port, test_name, checksum=None):
217 url = port.test_to_uri(test_name)
219 return url + ' 6000 ' + checksum + '\n'
220 return url + ' 6000\n'
222 def expected_output(self, port, test_name, pixel_tests, text_output, expected_checksum):
223 url = port.test_to_uri(test_name)
224 if pixel_tests and expected_checksum:
225 return ['#URL:%s\n' % url,
226 '#MD5:%s\n' % expected_checksum,
231 return ['#URL:%s\n' % url,
236 def test_pixeltest__fails(self):
237 filesystem = test.unit_test_filesystem()
238 self.assertTest('failures/expected/checksum.html', pixel_tests=True,
239 expected_checksum='wrong-checksum',
240 drt_output=['#URL:file:///test.checkout/LayoutTests/failures/expected/checksum.html\n',
241 '#MD5:checksum-checksum\n',
245 filesystem=filesystem)
246 self.assertEquals(filesystem.written_files,
247 {'/tmp/png_result0.png': 'checksum\x8a-pngtEXtchecksum\x00checksum-checksum'})
249 def test_chromium_parse_options(self):
250 options, args = mock_drt.parse_options(['--platform', 'chromium-cg-mac',
251 '--pixel-tests=/tmp/png_result0.png'])
252 self.assertTrue(options.chromium)
253 self.assertTrue(options.pixel_tests)
254 self.assertEquals(options.pixel_path, '/tmp/png_result0.png')
257 if __name__ == '__main__':