Home | History | Annotate | Download | only in port
      1 # Copyright (C) 2011 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 MockDRT."""
     30 
     31 import sys
     32 import webkitpy.thirdparty.unittest2 as unittest
     33 
     34 from webkitpy.common import newstringio
     35 from webkitpy.common.system.systemhost_mock import MockSystemHost
     36 from webkitpy.layout_tests.port import mock_drt
     37 from webkitpy.layout_tests.port import port_testcase
     38 from webkitpy.layout_tests.port import test
     39 from webkitpy.layout_tests.port.factory import PortFactory
     40 from webkitpy.tool import mocktool
     41 
     42 
     43 mock_options = mocktool.MockOptions(configuration='Release')
     44 
     45 
     46 class MockDRTPortTest(port_testcase.PortTestCase):
     47 
     48     def make_port(self, host=None, options=mock_options):
     49         host = host or MockSystemHost()
     50         test.add_unit_tests_to_mock_filesystem(host.filesystem)
     51         return mock_drt.MockDRTPort(host, port_name='mock-mac', options=options)
     52 
     53     def make_wdiff_available(self, port):
     54         port._make_wdiff_available()
     55 
     56     def test_port_name_in_constructor(self):
     57         self.assertTrue(mock_drt.MockDRTPort(MockSystemHost(), port_name='mock-test'))
     58 
     59     def test_check_sys_deps(self):
     60         pass
     61 
     62     def test_diff_image(self):
     63         pass
     64 
     65     def test_diff_image_crashed(self):
     66         pass
     67 
     68     def test_uses_apache(self):
     69         pass
     70 
     71     def test_get_crash_log(self):
     72         pass
     73 
     74     def test_check_build(self):
     75         pass
     76 
     77 
     78 class MockDRTTest(unittest.TestCase):
     79     def input_line(self, port, test_name, checksum=None):
     80         url = port.create_driver(0).test_to_uri(test_name)
     81         if url.startswith('file://'):
     82             url = url[len('file://'):]
     83 
     84         if checksum:
     85             return url + "'" + checksum + '\n'
     86         return url + '\n'
     87 
     88     def extra_args(self, pixel_tests):
     89         if pixel_tests:
     90             return ['--pixel-tests', '-']
     91         return ['-']
     92 
     93     def make_drt(self, options, args, host, stdin, stdout, stderr):
     94         return mock_drt.MockDRT(options, args, host, stdin, stdout, stderr)
     95 
     96     def make_input_output(self, port, test_name, pixel_tests,
     97                           expected_checksum, drt_output, drt_input=None, expected_text=None):
     98         if pixel_tests:
     99             if not expected_checksum:
    100                 expected_checksum = port.expected_checksum(test_name)
    101         if not drt_input:
    102             drt_input = self.input_line(port, test_name, expected_checksum)
    103         text_output = expected_text or port.expected_text(test_name) or ''
    104 
    105         if not drt_output:
    106             drt_output = self.expected_output(port, test_name, pixel_tests,
    107                                               text_output, expected_checksum)
    108         return (drt_input, drt_output)
    109 
    110     def expected_output(self, port, test_name, pixel_tests, text_output, expected_checksum):
    111         output = ['Content-Type: text/plain\n']
    112         if text_output:
    113             output.append(text_output)
    114         output.append('#EOF\n')
    115         if pixel_tests and expected_checksum:
    116             output.extend(['\n',
    117                            'ActualHash: %s\n' % expected_checksum,
    118                            'ExpectedHash: %s\n' % expected_checksum])
    119         output.append('#EOF\n')
    120         return output
    121 
    122     def assertTest(self, test_name, pixel_tests, expected_checksum=None, drt_output=None, host=None, expected_text=None):
    123         port_name = 'test'
    124         host = host or MockSystemHost()
    125         test.add_unit_tests_to_mock_filesystem(host.filesystem)
    126         port = PortFactory(host).get(port_name)
    127         drt_input, drt_output = self.make_input_output(port, test_name,
    128             pixel_tests, expected_checksum, drt_output, drt_input=None, expected_text=expected_text)
    129 
    130         args = ['--platform', port_name] + self.extra_args(pixel_tests)
    131         stdin = newstringio.StringIO(drt_input)
    132         stdout = newstringio.StringIO()
    133         stderr = newstringio.StringIO()
    134         options, args = mock_drt.parse_options(args)
    135 
    136         drt = self.make_drt(options, args, host, stdin, stdout, stderr)
    137         res = drt.run()
    138 
    139         self.assertEqual(res, 0)
    140 
    141         # We use the StringIO.buflist here instead of getvalue() because
    142         # the StringIO might be a mix of unicode/ascii and 8-bit strings.
    143         self.assertEqual(stdout.buflist, drt_output)
    144         self.assertEqual(stderr.getvalue(), '' if options.test_shell else '#EOF\n')
    145 
    146     def test_main(self):
    147         host = MockSystemHost()
    148         test.add_unit_tests_to_mock_filesystem(host.filesystem)
    149         stdin = newstringio.StringIO()
    150         stdout = newstringio.StringIO()
    151         stderr = newstringio.StringIO()
    152         res = mock_drt.main(['--platform', 'test'] + self.extra_args(False),
    153                             host, stdin, stdout, stderr)
    154         self.assertEqual(res, 0)
    155         self.assertEqual(stdout.getvalue(), '')
    156         self.assertEqual(stderr.getvalue(), '')
    157         self.assertEqual(host.filesystem.written_files, {})
    158 
    159     def test_pixeltest_passes(self):
    160         # This also tests that we handle HTTP: test URLs properly.
    161         self.assertTest('http/tests/passes/text.html', True)
    162 
    163     def test_pixeltest__fails(self):
    164         self.assertTest('failures/expected/image_checksum.html', pixel_tests=True,
    165             expected_checksum='image_checksum-checksum',
    166             drt_output=['Content-Type: text/plain\n',
    167                         'image_checksum-txt',
    168                         '#EOF\n',
    169                         '\n',
    170                         'ActualHash: image_checksum-checksum\n',
    171                         'ExpectedHash: image_checksum-checksum\n',
    172                         '#EOF\n'])
    173 
    174     def test_textonly(self):
    175         self.assertTest('passes/image.html', False)
    176 
    177     def test_checksum_in_png(self):
    178         self.assertTest('passes/checksum_in_image.html', True)
    179 
    180     def test_missing_image(self):
    181         self.assertTest('failures/expected/missing_image.html', True)
    182 
    183     def test_missing_text(self):
    184         self.assertTest('failures/expected/missing_text.html', True)
    185 
    186     def test_reftest_match(self):
    187         self.assertTest('passes/reftest.html', False, expected_checksum='mock-checksum', expected_text='reference text\n')
    188         self.assertTest('passes/reftest.html', True, expected_checksum='mock-checksum', expected_text='reference text\n')
    189 
    190     def test_reftest_mismatch(self):
    191         self.assertTest('passes/mismatch.html', False, expected_checksum='mock-checksum', expected_text='reference text\n')
    192         self.assertTest('passes/mismatch.html', True, expected_checksum='mock-checksum', expected_text='reference text\n')
    193 
    194 
    195 class MockTestShellTest(MockDRTTest):
    196     def extra_args(self, pixel_tests):
    197         if pixel_tests:
    198             return ['--pixel-tests=/tmp/png_result0.png']
    199         return []
    200 
    201     def make_drt(self, options, args, host, stdin, stdout, stderr):
    202         options.test_shell = True
    203 
    204         # We have to set these by hand because --platform test won't trigger
    205         # the Chromium code paths.
    206         options.pixel_path = '/tmp/png_result0.png'
    207         options.pixel_tests = True
    208 
    209         return mock_drt.MockTestShell(options, args, host, stdin, stdout, stderr)
    210 
    211     def input_line(self, port, test_name, checksum=None):
    212         url = port.create_driver(0).test_to_uri(test_name)
    213         if checksum:
    214             return url + ' 6000 ' + checksum + '\n'
    215         return url + ' 6000\n'
    216 
    217     def expected_output(self, port, test_name, pixel_tests, text_output, expected_checksum):
    218         url = port.create_driver(0).test_to_uri(test_name)
    219         output = ['#URL:%s\n' % url]
    220         if expected_checksum:
    221             output.append('#MD5:%s\n' % expected_checksum)
    222         if text_output:
    223             output.append(text_output)
    224             if not text_output.endswith('\n'):
    225                 output.append('\n')
    226         output.append('#EOF\n')
    227         return output
    228 
    229     def test_pixeltest__fails(self):
    230         host = MockSystemHost()
    231         url = '#URL:file://'
    232         url = url + '%s/failures/expected/image_checksum.html' % PortFactory(host).get('test').layout_tests_dir()
    233         self.assertTest('failures/expected/image_checksum.html', pixel_tests=True,
    234             expected_checksum='image_checksum',
    235             drt_output=[url + '\n',
    236                         '#MD5:image_checksum-checksum\n',
    237                         'image_checksum-txt',
    238                         '\n',
    239                         '#EOF\n'],
    240             host=host)
    241         self.assertEqual(host.filesystem.written_files,
    242             {'/tmp/png_result0.png': 'image_checksum\x8a-pngtEXtchecksum\x00image_checksum-checksum'})
    243 
    244     def test_test_shell_parse_options(self):
    245         options, args = mock_drt.parse_options(['--platform', 'mac', '--test-shell',
    246             '--pixel-tests=/tmp/png_result0.png'])
    247         self.assertTrue(options.test_shell)
    248         self.assertTrue(options.pixel_tests)
    249         self.assertEqual(options.pixel_path, '/tmp/png_result0.png')
    250