@@ -17,19 +17,147 @@
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
23import subprocess
21import subprocess
25import ubuntuuitoolkit
26import ubuntuuitoolkit
26from autopilot import introspection
27from autopilot import introspection
28+from autopilot.matchers import Eventually
29+from testtools.matchers import Equals
30+from ubuntuuitoolkit import fixture_setup
28from unity8 import (
32from unity8 import (
29 get_binary_path,
33 get_binary_path,
30 process_helpers
35 process_helpers
38+from unity8.shell import emulators
40+from unity8.shell.emulators import (
41+ main_window as main_window_emulator
45+ get_mocks_library_path,
46+ get_default_extra_mock_libraries,
50+logger = logging.getLogger(__name__)
53+class LaunchUnityWithFakeSensors(fixtures.Fixture):
55+ """Fixture to launch Unity8 with an injectable sensors backend.
57+ :ivar unity_proxy: The Autopilot proxy object for the Unity shell.
65+ """Restart Unity8 with testability and create sensors."""
68+ fixture_setup.InitctlEnvironmentVariable(
69+ UBUNTU_PLATFORM_API_TEST_OVERRIDE='sensors'))
71+ self.addCleanup(process_helpers.stop_job, 'unity8')
72+ restart_thread = threading.Thread(
73+ target=self._restart_unity_with_testability)
74+ restart_thread.start()
76+ self._create_sensors()
78+ restart_thread.join()
79+ self.fake_sensors = sensors.FakePlatformSensors()
81+ def _get_lightdm_mock_path(self):
82+ lib_path = get_mocks_library_path()
83+ lightdm_mock_path = os.path.abspath(
84+ os.path.join(lib_path, "LightDM", "liblightdm")
87+ if not os.path.exists(lightdm_mock_path):
89+ "LightDM mock does not exist at path {}.".
90+ format(lightdm_mock_path)
92+ return lightdm_mock_path
94+ def _get_qml_import_path_with_mock(self):
95+ """Return the QML2_IMPORT_PATH value with the mock path prepended."""
96+ qml_import_path = [get_mocks_library_path()]
97+ if os.getenv('QML2_IMPORT_PATH') is not None:
98+ qml_import_path.append(os.getenv('QML2_IMPORT_PATH'))
100+ qml_import_path = ':'.join(qml_import_path)
101+ return qml_import_path
103+ def _restart_unity_with_testability(self):
106+ data_dirs = get_data_dirs(True)
107+ if data_dirs is not None:
108+ _environment['XDG_DATA_DIRS'] = data_dirs
110+ _environment['QML2_IMPORT_PATH'] = (
111+ self._get_qml_import_path_with_mock()
114+ new_ld_library_path = [
115+ get_default_extra_mock_libraries(),
116+ self._get_lightdm_mock_path()
118+ if os.getenv('LD_LIBRARY_PATH') is not None:
119+ new_ld_library_path.append(os.getenv('LD_LIBRARY_PATH'))
120+ new_ld_library_path = ':'.join(new_ld_library_path)
121+ _environment['LD_LIBRARY_PATH'] = new_ld_library_path
123+ # FIXME: we shouldn't be doing this
124+ # $MIR_SOCKET, fallback to $XDG_RUNTIME_DIR/mir_socket and
125+ # /tmp/mir_socket as last resort
128+ os.getenv('MIR_SOCKET',
129+ os.path.join(os.getenv('XDG_RUNTIME_DIR', "/tmp"),
134+ os.unlink("/tmp/mir_socket")
138+ binary_arg = "BINARY=%s" % get_binary_path()
139+ env_args = ["%s=%s" % (k, v) for k, v in _environment.items()]
140+ args = [binary_arg] + env_args
141+ self.unity_proxy = process_helpers.restart_unity_with_testability(
143+ self.main_win = self.unity_proxy.select_single(
144+ main_window_emulator.QQuickView)
146+ def _create_sensors(self):
147+ # Wait for unity to start running.
148+ Eventually(Equals(True)).match(
149+ lambda: process_helpers.is_job_running('unity8'))
151+ # Wait for the sensors fifo file to be created.
152+ fifo_path = '/tmp/sensor-fifo-{0}'.format(
153+ process_helpers.get_unity_pid())
154+ Eventually(Equals(True)).match(
155+ lambda: os.path.exists(fifo_path))
157+ with open(fifo_path, 'w') as fifo:
158+ fifo.write('create accel 0 1000 0.1\n')
159+ fifo.write('create light 0 10 1\n')
160+ fifo.write('create proximity\n')
34logger = logging.getLogger(__name__)
162logger = logging.getLogger(__name__)
@@ -188,7 +316,7 @@ class LaunchMockIndicatorService(fixtures.Fixture):
188 self.ensure_not_running = ensure_not_running
316 self.ensure_not_running = ensure_not_running
190 def setUp(self):
318 def setUp(self):
191− super(LaunchMockIndicatorService, self).setUp()
319+ super().setUp()
192 if self.ensure_not_running:
320 if self.ensure_not_running:
193 self.ensure_service_not_running()
321 self.ensure_service_not_running()
194 self.addCleanup(self.stop_service)
322 self.addCleanup(self.stop_service)