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_default_max_locked_shards(self): 63 pass 64 65 def test_diff_image(self): 66 pass 67 68 def test_diff_image_crashed(self): 69 pass 70 71 def test_uses_apache(self): 72 pass 73 74 def test_get_crash_log(self): 75 pass 76 77 def test_check_build(self): 78 pass 79 80 81 class MockDRTTest(unittest.TestCase): 82 def input_line(self, port, test_name, pixel_tests, checksum=None): 83 url = port.create_driver(0).test_to_uri(test_name) 84 if url.startswith('file://'): 85 url = url[len('file://'):] 86 if pixel_tests: 87 url += "'--pixel-test" 88 if checksum: 89 url += "'" + checksum 90 return url + '\n' 91 92 def make_drt(self, options, args, host, stdin, stdout, stderr): 93 return mock_drt.MockDRT(options, args, host, stdin, stdout, stderr) 94 95 def make_input_output(self, port, test_name, pixel_tests, 96 expected_checksum, drt_output, drt_input=None, expected_text=None): 97 if pixel_tests: 98 if not expected_checksum: 99 expected_checksum = port.expected_checksum(test_name) 100 if not drt_input: 101 drt_input = self.input_line(port, test_name, pixel_tests, expected_checksum) 102 text_output = expected_text or port.expected_text(test_name) or '' 103 104 if not drt_output: 105 drt_output = self.expected_output(port, test_name, pixel_tests, 106 text_output, expected_checksum) 107 return (drt_input, drt_output) 108 109 def expected_output(self, port, test_name, pixel_tests, text_output, expected_checksum): 110 output = ['Content-Type: text/plain\n'] 111 if text_output: 112 output.append(text_output) 113 output.append('#EOF\n') 114 if pixel_tests and expected_checksum: 115 output.extend(['\n', 116 'ActualHash: %s\n' % expected_checksum, 117 'ExpectedHash: %s\n' % expected_checksum]) 118 output.append('#EOF\n') 119 return output 120 121 def assertTest(self, test_name, pixel_tests, expected_checksum=None, drt_output=None, host=None, expected_text=None): 122 port_name = 'test' 123 host = host or MockSystemHost() 124 test.add_unit_tests_to_mock_filesystem(host.filesystem) 125 port = PortFactory(host).get(port_name) 126 drt_input, drt_output = self.make_input_output(port, test_name, 127 pixel_tests, expected_checksum, drt_output, drt_input=None, expected_text=expected_text) 128 129 args = ['--dump-render-tree', '--platform', port_name, '-'] 130 stdin = newstringio.StringIO(drt_input) 131 stdout = newstringio.StringIO() 132 stderr = newstringio.StringIO() 133 options, args = mock_drt.parse_options(args) 134 135 drt = self.make_drt(options, args, host, stdin, stdout, stderr) 136 res = drt.run() 137 138 self.assertEqual(res, 0) 139 140 # We use the StringIO.buflist here instead of getvalue() because 141 # the StringIO might be a mix of unicode/ascii and 8-bit strings. 142 self.assertEqual(stdout.buflist, drt_output) 143 self.assertEqual(stderr.getvalue(), '#EOF\n') 144 145 def test_main(self): 146 host = MockSystemHost() 147 test.add_unit_tests_to_mock_filesystem(host.filesystem) 148 stdin = newstringio.StringIO() 149 stdout = newstringio.StringIO() 150 stderr = newstringio.StringIO() 151 res = mock_drt.main(['--dump-render-tree', '--platform', 'test', '-'], 152 host, stdin, stdout, stderr) 153 self.assertEqual(res, 0) 154 self.assertEqual(stdout.getvalue(), '') 155 self.assertEqual(stderr.getvalue(), '') 156 self.assertEqual(host.filesystem.written_files, {}) 157 158 def test_pixeltest_passes(self): 159 # This also tests that we handle HTTP: test URLs properly. 160 self.assertTest('http/tests/passes/text.html', True) 161 162 def test_pixeltest__fails(self): 163 self.assertTest('failures/expected/image_checksum.html', pixel_tests=True, 164 expected_checksum='image_checksum-checksum', 165 drt_output=['Content-Type: text/plain\n', 166 'image_checksum-txt', 167 '#EOF\n', 168 '\n', 169 'ActualHash: image_checksum-checksum\n', 170 'ExpectedHash: image_checksum-checksum\n', 171 '#EOF\n']) 172 173 def test_textonly(self): 174 self.assertTest('passes/image.html', False) 175 176 def test_checksum_in_png(self): 177 self.assertTest('passes/checksum_in_image.html', True) 178 179 def test_missing_image(self): 180 self.assertTest('failures/expected/missing_image.html', True) 181 182 def test_missing_text(self): 183 self.assertTest('failures/expected/missing_text.html', True) 184 185 def test_reftest_match(self): 186 self.assertTest('passes/reftest.html', True, expected_checksum='mock-checksum', expected_text='reference text\n') 187 188 def test_reftest_mismatch(self): 189 self.assertTest('passes/mismatch.html', True, expected_checksum='mock-checksum', expected_text='reference text\n') 190 191 def test_audio(self): 192 self.assertTest('passes/audio.html', pixel_tests=True, 193 drt_output=['Content-Type: audio/wav\n', 194 'Content-Transfer-Encoding: base64\n', 195 'YXVkaW8td2F2', 196 '\n', 197 '#EOF\n', 198 '#EOF\n']) 199 200 def test_virtual(self): 201 self.assertTest('virtual/passes/text.html', True) 202