@@ -24,16 +24,14 @@ try:
2424except ImportError:
2525 Gio = None
2626
27+import logging
28+import os
29+
2730from autopilot import introspection
2831from autopilot.platform import model
2932from autopilot.testcase import AutopilotTestCase
3033from autopilot.matchers import Eventually
31−from autopilot.input import Touch
3234from autopilot.display import Display
33−import logging
34−import os.path
35−import subprocess
36−import sys
3735from testtools.matchers import Equals
3836from ubuntuuitoolkit import (
3937 fixture_setup as toolkit_fixtures,
@@ -93,36 +91,25 @@ class UnityTestCase(AutopilotTestCase):
9391 @classmethod
9492 def setUpClass(cls):
9593 try:
96− output = subprocess.check_output(
97− ["/sbin/initctl", "status", "unity8"],
98− stderr=subprocess.STDOUT,
99− universal_newlines=True,
100− )
101− except subprocess.CalledProcessError as e:
102− sys.stderr.write(
103− "Error: `initctl status unity8` failed, most probably the "
104− "unity8 session could not be found:\n\n"
105− "{0}\n"
106− "Please install unity8 or copy data/unity8.conf to "
107− "{1}\n".format(
108− e.output,
109− os.path.join(os.getenv("XDG_CONFIG_HOME",
110− os.path.join(os.getenv("HOME"),
111− ".config")
112− ),
113− "upstart")
114− )
115− )
116− sys.exit(1)
117−
118− if "start/" in output:
119− sys.stderr.write(
120− "Error: Unity is currently running, these tests require it to "
121− "be 'stopped'.\n"
122− "Please run this command before running these tests: \n"
123− "initctl stop unity8\n"
94+ is_unity_running = process_helpers.is_job_running('unity8')
95+ except process_helpers.JobError as e:
96+ xdg_config_home = os.getenv(
97+ 'XDG_CONFIG_HOME', os.path.join(os.getenv('HOME'), '.config'))
98+ upstart_config_path = os.path.join(xdg_config_home, 'upstart')
99+ logger.error(
100+ '`initctl status unity8` failed, most probably the '
101+ 'unity8 session could not be found:\n\n'
102+ '{0}\n'
103+ 'Please install unity8 or copy data/unity8.conf to '
104+ '{1}\n'.format(e.output, upstart_config_path)
124105 )
125− sys.exit(2)
106+ raise e
107+ else:
108+ assert not is_unity_running, (
109+ 'Unity is currently running, these tests require it to be '
110+ 'stopped.\n'
111+ 'Please run this command before running these tests: \n'
112+ 'initctl stop unity8\n')
126113
127114 def setUp(self):
128115 super().setUp()
@@ -134,13 +121,6 @@ class UnityTestCase(AutopilotTestCase):
134121 self._data_dirs_mock_enabled = True
135122 self._environment = {}
136123
137− # FIXME: This is a work around re: lp:1238417
138− if model() != "Desktop":
139− from autopilot.input import _uinput
140− _uinput._touch_device = _uinput.create_touch_device()
141− self.addCleanup(_uinput._touch_device.close)
142−
143− self.touch = Touch.create()
144124 self._setup_display_details()
145125
146126 def _setup_display_details(self):
@@ -196,40 +176,6 @@ class UnityTestCase(AutopilotTestCase):
196176 divisor = divisor * 2
197177 return divisor
198178
199− def _patch_environment(self, key, value):
200− """Wrapper for patching env for upstart environment."""
201− try:
202− current_value = subprocess.check_output(
203− ["/sbin/initctl", "get-env", "--global", key],
204− stderr=subprocess.STDOUT,
205− universal_newlines=True,
206− ).rstrip()
207− except subprocess.CalledProcessError:
208− current_value = None
209−
210− subprocess.call([
211− "/sbin/initctl",
212− "set-env",
213− "--global",
214− "%s=%s" % (key, value)
215− ], stderr=subprocess.STDOUT)
216− self.addCleanup(self._upstart_reset_env, key, current_value)
217−
218− def _upstart_reset_env(self, key, value):
219− logger.info("Resetting upstart env %s to %s", key, value)
220− if value is None:
221− subprocess.call(
222− ["/sbin/initctl", "unset-env", key],
223− stderr=subprocess.STDOUT,
224− )
225− else:
226− subprocess.call([
227− "/sbin/initctl",
228− "set-env",
229− "--global",
230− "%s=%s" % (key, value)
231− ], stderr=subprocess.STDOUT)
232−
233179 def launch_unity(self, mode="full-greeter", *args):
234180 """
235181 Launch the unity shell, return a proxy object for it.
@@ -257,21 +203,6 @@ class UnityTestCase(AutopilotTestCase):
257203 if self._data_dirs_mock_enabled:
258204 self._patch_data_dirs()
259205
260− # FIXME: we shouldn't be doing this
261− # $MIR_SOCKET, fallback to $XDG_RUNTIME_DIR/mir_socket and
262− # /tmp/mir_socket as last resort
263− try:
264− os.unlink(
265− os.getenv('MIR_SOCKET',
266− os.path.join(os.getenv('XDG_RUNTIME_DIR', "/tmp"),
267− "mir_socket")))
268− except OSError:
269− pass
270− try:
271− os.unlink("/tmp/mir_socket")
272− except OSError:
273− pass
274−
275206 unity8_cli_args_list = ["--mode={}".format(mode)]
276207 if len(args) != 0:
277208 unity8_cli_args_list += args
@@ -297,26 +228,15 @@ class UnityTestCase(AutopilotTestCase):
297228
298229 def _launch_unity_with_upstart(self, binary_path, args):
299230 logger.info("Starting unity")
300− self._patch_environment("QT_LOAD_TESTABILITY", 1)
301−
302− binary_arg = "BINARY=%s" % binary_path
303− extra_args = "ARGS=%s" % " ".join(args)
304− env_args = ["%s=%s" % (k, v) for k, v in self._environment.items()]
305− all_args = [binary_arg, extra_args] + env_args
231+ self.useFixture(toolkit_fixtures.InitctlEnvironmentVariable(
232+ global_=True, QT_LOAD_TESTABILITY=1))
306233
307− self.addCleanup(self._cleanup_launching_upstart_unity)
308−
309− return process_helpers.restart_unity_with_testability(*all_args)
310−
311− def _cleanup_launching_upstart_unity(self):
312− logger.info("Stopping unity")
313− try:
314− subprocess.check_output(
315− ["/sbin/initctl", "stop", "unity8"],
316− stderr=subprocess.STDOUT
317− )
318− except subprocess.CalledProcessError:
319− logger.warning("Appears unity was already stopped!")
234+ variables = self._environment
235+ variables['ARGS'] = " ".join(args)
236+ launch_unity_fixture = fixture_setup.RestartUnityWithTestability(
237+ binary_path, variables)
238+ self.useFixture(launch_unity_fixture)
239+ return launch_unity_fixture.unity_proxy
320240
321241 def _patch_data_dirs(self):
322242 data_dirs = get_data_dirs(self._data_dirs_mock_enabled)