Allow a port to run tests with a custom device setup
[WebKit-https.git] / Tools / Scripts / webkitpy / port / gtk.py
1 # Copyright (C) 2010 Google Inc. All rights reserved.
2 # Copyright (C) 2013 Samsung Electronics.  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 Google name 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 import os
31 import subprocess
32 import uuid
33 import logging
34
35 from webkitpy.common.system import path
36 from webkitpy.common.memoized import memoized
37 from webkitpy.layout_tests.models.test_configuration import TestConfiguration
38 from webkitpy.port.base import Port
39 from webkitpy.port.pulseaudio_sanitizer import PulseAudioSanitizer
40 from webkitpy.port.xvfbdriver import XvfbDriver
41 from webkitpy.port.westondriver import WestonDriver
42 from webkitpy.port.xorgdriver import XorgDriver
43 from webkitpy.port.linux_get_crash_log import GDBCrashLogGenerator
44 from webkitpy.port.leakdetector_valgrind import LeakDetectorValgrind
45
46 _log = logging.getLogger(__name__)
47
48
49 class GtkPort(Port):
50     port_name = "gtk"
51
52     def __init__(self, *args, **kwargs):
53         super(GtkPort, self).__init__(*args, **kwargs)
54         self._pulseaudio_sanitizer = PulseAudioSanitizer()
55         self._wayland = self.get_option("wayland")
56
57         if self.get_option("leaks"):
58             self._leakdetector = LeakDetectorValgrind(self._executive, self._filesystem, self.results_directory())
59             if not self.get_option("wrapper"):
60                 raise ValueError('use --wrapper=\"valgrind\" for memory leak detection on GTK')
61
62         if self._should_use_jhbuild():
63             self._jhbuild_wrapper = [self.path_from_webkit_base('Tools', 'jhbuild', 'jhbuild-wrapper'), '--gtk', 'run']
64             if self.get_option('wrapper'):
65                 self.set_option('wrapper', ' '.join(self._jhbuild_wrapper) + ' ' + self.get_option('wrapper'))
66             else:
67                 self.set_option_default('wrapper', ' '.join(self._jhbuild_wrapper))
68
69     def _built_executables_path(self, *path):
70         return self._build_path(*(('bin',) + path))
71
72     def _built_libraries_path(self, *path):
73         return self._build_path(*(('lib',) + path))
74
75     def _port_flag_for_scripts(self):
76         return "--gtk"
77
78     @memoized
79     def _driver_class(self):
80         if self._wayland:
81             return WestonDriver
82         if os.environ.get("USE_NATIVE_XDISPLAY"):
83             return XorgDriver
84         return XvfbDriver
85
86     def supports_per_test_timeout(self):
87         return True
88
89     def default_timeout_ms(self):
90         # Starting an application under Valgrind takes a lot longer than normal
91         # so increase the timeout (empirically 10x is enough to avoid timeouts).
92         multiplier = 10 if self.get_option("leaks") else 1
93         if self.get_option('configuration') == 'Debug':
94             return multiplier * 12 * 1000
95         return multiplier * 6 * 1000
96
97     def driver_stop_timeout(self):
98         if self.get_option("leaks"):
99             # Wait the default timeout time before killing the process in driver.stop().
100             return self.default_timeout_ms()
101         return super(GtkPort, self).driver_stop_timeout()
102
103     def setup_test_run(self, device_class=None):
104         super(GtkPort, self).setup_test_run(device_class)
105         self._pulseaudio_sanitizer.unload_pulseaudio_module()
106
107         if self.get_option("leaks"):
108             self._leakdetector.clean_leaks_files_from_results_directory()
109
110     def clean_up_test_run(self):
111         super(GtkPort, self).clean_up_test_run()
112         self._pulseaudio_sanitizer.restore_pulseaudio_module()
113
114     def setup_environ_for_server(self, server_name=None):
115         environment = super(GtkPort, self).setup_environ_for_server(server_name)
116         environment['GSETTINGS_BACKEND'] = 'memory'
117         environment['LIBOVERLAY_SCROLLBAR'] = '0'
118         environment['TEST_RUNNER_INJECTED_BUNDLE_FILENAME'] = self._build_path('lib', 'libTestRunnerInjectedBundle.so')
119         environment['TEST_RUNNER_TEST_PLUGIN_PATH'] = self._build_path('lib', 'plugins')
120         environment['OWR_USE_TEST_SOURCES'] = '1'
121         self._copy_value_from_environ_if_set(environment, 'WEBKIT_OUTPUTDIR')
122         # Configure the software libgl renderer if jhbuild ready and we test inside a virtualized window system
123         if self._driver_class() in [XvfbDriver, WestonDriver] and self._should_use_jhbuild():
124             llvmpipe_libgl_path = self.host.executive.run_command(self._jhbuild_wrapper + ['printenv', 'LLVMPIPE_LIBGL_PATH'],
125                                                                   error_handler=self.host.executive.ignore_error).strip()
126             dri_libgl_path = os.path.join(llvmpipe_libgl_path, "dri")
127             if os.path.exists(os.path.join(llvmpipe_libgl_path, "libGL.so")) and os.path.exists(os.path.join(dri_libgl_path, "swrast_dri.so")):
128                     # Force the Gallium llvmpipe software rasterizer
129                     environment['LIBGL_ALWAYS_SOFTWARE'] = "1"
130                     environment['LIBGL_DRIVERS_PATH'] = dri_libgl_path
131                     environment['LD_LIBRARY_PATH'] = llvmpipe_libgl_path
132                     if os.environ.get('LD_LIBRARY_PATH'):
133                             environment['LD_LIBRARY_PATH'] += ':%s' % os.environ.get('LD_LIBRARY_PATH')
134             else:
135                     _log.warning("Can't find Gallium llvmpipe driver. Try to run update-webkitgtk-libs")
136         if self.get_option("leaks"):
137             #  Turn off GLib memory optimisations https://wiki.gnome.org/Valgrind.
138             environment['G_SLICE'] = 'always-malloc'
139             environment['G_DEBUG'] = 'gc-friendly'
140             xmlfilename = "".join(("drt-%p-", uuid.uuid1().hex, "-leaks.xml"))
141             xmlfile = os.path.join(self.results_directory(), xmlfilename)
142             suppressionsfile = self.path_from_webkit_base('Tools', 'Scripts', 'valgrind', 'suppressions.txt')
143             environment['VALGRIND_OPTS'] = \
144                 "--tool=memcheck " \
145                 "--num-callers=40 " \
146                 "--demangle=no " \
147                 "--trace-children=no " \
148                 "--smc-check=all-non-file " \
149                 "--leak-check=yes " \
150                 "--leak-resolution=high " \
151                 "--show-possibly-lost=no " \
152                 "--show-reachable=no " \
153                 "--leak-check=full " \
154                 "--undef-value-errors=no " \
155                 "--gen-suppressions=all " \
156                 "--xml=yes " \
157                 "--xml-file=%s " \
158                 "--suppressions=%s" % (xmlfile, suppressionsfile)
159         return environment
160
161     def _generate_all_test_configurations(self):
162         configurations = []
163         for build_type in self.ALL_BUILD_TYPES:
164             configurations.append(TestConfiguration(version=self._version, architecture='x86', build_type=build_type))
165         return configurations
166
167     def _path_to_driver(self):
168         return self._built_executables_path(self.driver_name())
169
170     def _path_to_image_diff(self):
171         return self._built_executables_path('ImageDiff')
172
173     def _path_to_webcore_library(self):
174         gtk_library_names = [
175             "libwebkitgtk-1.0.so",
176             "libwebkitgtk-3.0.so",
177             "libwebkit2gtk-1.0.so",
178         ]
179
180         for library in gtk_library_names:
181             full_library = self._built_libraries_path(library)
182             if self._filesystem.isfile(full_library):
183                 return full_library
184         return None
185
186     def _search_paths(self):
187         search_paths = []
188         if self._wayland:
189             search_paths.append(self.port_name + "-wayland")
190         search_paths.append(self.port_name)
191         search_paths.append('wk2')
192         search_paths.extend(self.get_option("additional_platform_directory", []))
193         return search_paths
194
195     def default_baseline_search_path(self):
196         return map(self._webkit_baseline_path, self._search_paths())
197
198     def _port_specific_expectations_files(self):
199         return [self._filesystem.join(self._webkit_baseline_path(p), 'TestExpectations') for p in reversed(self._search_paths())]
200
201     def print_leaks_summary(self):
202         if not self.get_option('leaks'):
203             return
204         # FIXME: This is a hack, but we don't have a better way to get this information from the workers yet
205         # because we're in the manager process.
206         leaks_files = self._leakdetector.leaks_files_in_results_directory()
207         if not leaks_files:
208             return
209         self._leakdetector.parse_and_print_leaks_detail(leaks_files)
210
211     def show_results_html_file(self, results_filename):
212         self._run_script("run-minibrowser", [path.abspath_to_uri(self.host.platform, results_filename)])
213
214     def check_sys_deps(self, needs_http):
215         return super(GtkPort, self).check_sys_deps(needs_http) and self._driver_class().check_driver(self)
216
217     def _get_crash_log(self, name, pid, stdout, stderr, newer_than):
218         name = "WebKitWebProcess" if name == "WebProcess" else name
219         return GDBCrashLogGenerator(name, pid, newer_than, self._filesystem, self.path_to_script("process-linux-coredump"), self._path_to_driver).generate_crash_log(stdout, stderr)
220
221     def test_expectations_file_position(self):
222         # GTK port baseline search path is gtk -> wk2 -> generic (as gtk-wk2 and gtk baselines are merged), so port test expectations file is at third to last position.
223         return 2
224
225     def build_webkit_command(self, build_style=None):
226         command = super(GtkPort, self).build_webkit_command(build_style)
227         command.extend(["--gtk", "--update-gtk"])
228         command.append(super(GtkPort, self).make_args())
229         return command
230
231     def run_webkit_tests_command(self):
232         command = super(GtkPort, self).run_webkit_tests_command()
233         command.append("--gtk")
234         return command