initial import
[vuplus_webkit] / Tools / Scripts / webkitpy / layout_tests / port / mock_drt_unittest.py
1 #!/usr/bin/env python
2 # Copyright (C) 2011 Google Inc. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
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
13 # distribution.
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.
17 #
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.
29
30 """Unit tests for MockDRT."""
31
32 import sys
33 import unittest
34
35 from webkitpy.common import newstringio
36
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
41
42 from webkitpy.tool import mocktool
43 mock_options = mocktool.MockOptions(configuration='Release')
44
45
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)
52
53     def test_default_worker_model(self):
54         # only overridding the default test; we don't care about this one.
55         pass
56
57     def test_port_name_in_constructor(self):
58         self.assertTrue(mock_drt.MockDRTPort(port_name='mock-test'))
59
60     def test_check_sys_deps(self):
61         pass
62
63     def test_uses_apache(self):
64         pass
65
66     def integration_test_http_lock(self):
67         pass
68
69     def integration_test_start_helper(self):
70         pass
71
72     def integration_test_http_server__normal(self):
73         pass
74
75     def integration_test_http_server__fails(self):
76         pass
77
78     def integration_test_websocket_server__normal(self):
79         pass
80
81     def integration_test_websocket_server__fails(self):
82         pass
83
84     def integration_test_helper(self):
85         pass
86
87
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
92         # here.
93         if url.startswith('file:////'):
94             url = url[len('file:////') - 1:]
95         if url.startswith('file:///'):
96             url = url[len('file:///') - 1:]
97
98         if checksum:
99             return url + "'" + checksum + '\n'
100         return url + '\n'
101
102     def extra_args(self, pixel_tests):
103         if pixel_tests:
104             return ['--pixel-tests', '-']
105         return ['-']
106
107     def make_drt(self, options, args, filesystem, stdin, stdout, stderr):
108         return mock_drt.MockDRT(options, args, filesystem, stdin, stdout, stderr)
109
110     def make_input_output(self, port, test_name, pixel_tests,
111                           expected_checksum, drt_output, drt_input=None):
112         if pixel_tests:
113             if not expected_checksum:
114                 expected_checksum = port.expected_checksum(test_name)
115         if not drt_input:
116             drt_input = self.input_line(port, test_name, expected_checksum)
117         text_output = port.expected_text(test_name)
118
119         if not drt_output:
120             drt_output = self.expected_output(port, test_name, pixel_tests,
121                                               text_output, expected_checksum)
122         return (drt_input, drt_output)
123
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',
127                     text_output,
128                     '#EOF\n',
129                     '\n',
130                     'ActualHash: %s\n' % expected_checksum,
131                     'ExpectedHash: %s\n' % expected_checksum,
132                     '#EOF\n']
133         else:
134             return ['Content-Type: text/plain\n',
135                     text_output,
136                     '#EOF\n',
137                     '#EOF\n']
138
139     def assertTest(self, test_name, pixel_tests, expected_checksum=None,
140                    drt_output=None, filesystem=None):
141         platform = 'test'
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)
146
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)
152
153         drt = self.make_drt(options, args, filesystem, stdin, stdout, stderr)
154         res = drt.run()
155
156         self.assertEqual(res, 0)
157
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(), '')
162
163     def test_main(self):
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, {})
174
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)
178
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',
183                         'checksum-txt',
184                         '#EOF\n',
185                         '\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',
191                         '#EOF\n'])
192
193     def test_textonly(self):
194         self.assertTest('passes/image.html', False)
195
196     def test_checksum_in_png(self):
197         self.assertTest('passes/checksum_in_image.html', True)
198
199
200 class MockChromiumDRTTest(MockDRTTest):
201     def extra_args(self, pixel_tests):
202         if pixel_tests:
203             return ['--pixel-tests=/tmp/png_result0.png']
204         return []
205
206     def make_drt(self, options, args, filesystem, stdin, stdout, stderr):
207         options.chromium = True
208
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
213
214         return mock_drt.MockChromiumDRT(options, args, filesystem, stdin, stdout, stderr)
215
216     def input_line(self, port, test_name, checksum=None):
217         url = port.test_to_uri(test_name)
218         if checksum:
219             return url + ' 6000 ' + checksum + '\n'
220         return url + ' 6000\n'
221
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,
227                     text_output,
228                     '\n',
229                     '#EOF\n']
230         else:
231             return ['#URL:%s\n' % url,
232                     text_output,
233                     '\n',
234                     '#EOF\n']
235
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',
242                         'checksum-txt',
243                         '\n',
244                         '#EOF\n'],
245             filesystem=filesystem)
246         self.assertEquals(filesystem.written_files,
247             {'/tmp/png_result0.png': 'checksum\x8a-pngtEXtchecksum\x00checksum-checksum'})
248
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')
255
256
257 if __name__ == '__main__':
258     port_testcase.main()