[GTK] run-gtk-tests should use webkitpy.port.gtk and webkitpy.port.xvfbdriver
[WebKit-https.git] / Tools / Scripts / webkitpy / port / westondriver.py
1 # Copyright (C) 2013 Zan Dobersek <zandobersek@gmail.com>
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 Google name 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 import logging
30 import os
31 import random
32 import time
33
34 from webkitpy.port.server_process import ServerProcess
35 from webkitpy.port.driver import Driver
36 from webkitpy.port.xvfbdriver import XvfbDriver
37
38 _log = logging.getLogger(__name__)
39
40
41 class WestonDriver(Driver):
42     @staticmethod
43     def check_driver(port):
44         weston_findcmd = ['which', 'weston']
45         if port._should_use_jhbuild():
46             weston_findcmd = port._jhbuild_wrapper + weston_findcmd
47         weston_found = port.host.executive.run_command(weston_findcmd, return_exit_code=True) is 0
48         if not weston_found:
49             _log.error("No weston found. Cannot run layout tests.")
50         return weston_found
51
52     def __init__(self, *args, **kwargs):
53         Driver.__init__(self, *args, **kwargs)
54         self._startup_delay_secs = 1.0
55         self._xvfbdriver = XvfbDriver(*args, **kwargs)
56
57     def _setup_environ_for_test(self):
58         self._driver_directory = self._port.host.filesystem.mkdtemp(prefix='%s-' % self._server_name)
59         driver_environment = self._port.setup_environ_for_server(self._server_name)
60         driver_environment['DISPLAY'] = ":%d" % self._xvfbdriver._xvfb_run(driver_environment)
61         weston_socket = 'WKTesting-weston-%032x' % random.getrandbits(128)
62         weston_command = ['weston', '--socket=%s' % weston_socket, '--width=1024', '--height=768', '--use-pixman']
63         if self._port._should_use_jhbuild():
64             weston_command = self._port._jhbuild_wrapper + weston_command
65         with open(os.devnull, 'w') as devnull:
66             self._weston_process = self._port.host.executive.popen(weston_command, stderr=devnull, env=driver_environment)
67
68         # Give Weston a bit of time to set itself up.
69         time.sleep(self._startup_delay_secs)
70
71         driver_environment['LOCAL_RESOURCE_ROOT'] = self._port.layout_tests_dir()
72         # Currently on WebKit2, there is no API for setting the application cache directory.
73         # Each worker should have its own and it should be cleaned afterwards, when the worker stops.
74         driver_environment['XDG_CACHE_HOME'] = self._ensure_driver_tmpdir_subdirectory('appcache')
75         driver_environment['DUMPRENDERTREE_TEMP'] = self._ensure_driver_tmpdir_subdirectory('drt-temp')
76         driver_environment['WAYLAND_DISPLAY'] = weston_socket
77         driver_environment['GDK_BACKEND'] = 'wayland'
78         if driver_environment.get('DISPLAY'):
79             del driver_environment['DISPLAY']
80         return driver_environment
81
82     def _start(self, pixel_tests, per_test_args):
83         self.stop()
84         self._crashed_process_name = None
85         self._crashed_pid = None
86         self._server_process = self._port._server_process_constructor(self._port, self._server_name, self.cmd_line(pixel_tests, per_test_args), self._setup_environ_for_test())
87         self._server_process.start()
88
89     def stop(self):
90         super(WestonDriver, self).stop()
91         if getattr(self, '_weston_process', None):
92             # The Weston process is terminated instead of killed, giving the Weston a chance to clean up after itself.
93             self._weston_process.terminate()
94             self._weston_process = None
95         if getattr(self, '_driver_directory', None):
96             self._port.host.filesystem.rmtree(str(self._driver_directory))
97
98     def _ensure_driver_tmpdir_subdirectory(self, subdirectory):
99         assert getattr(self, '_driver_directory', None)
100         directory_path = self._port.host.filesystem.join(str(self._driver_directory), subdirectory)
101         self._port.host.filesystem.maybe_make_directory(directory_path)
102         return directory_path