def test_returns_proxy_object(self, gpofep, ual): launcher = UpstartApplicationLauncher() with patch.object(launcher, '_launch_app'): with patch.object(launcher, '_get_pid_for_launched_app'): with patch.object(launcher, '_get_glib_loop'): result = launcher.launch('') self.assertEqual(result, gpofep.return_value)
def test_calls_get_glib_loop(self, gpofep, ual): launcher = UpstartApplicationLauncher() with patch.object(launcher, '_launch_app'): with patch.object(launcher, '_get_pid_for_launched_app'): with patch.object(launcher, '_get_glib_loop') as ggl: launcher.launch('') ggl.assert_called_once_with()
def test_on_stopped_only_sets_status_on_correct_app_id(self): state = { 'expected_app_id': 'gedit', } UpstartApplicationLauncher._on_stopped('some_game', state) self.assertThat(state, Not(Contains('status')))
def test_stop_logs_error_on_timeout(self): mock_add_detail = Mock() mock_glib_loop = Mock() patch_get_loop = patch.object( UpstartApplicationLauncher, '_get_glib_loop', new=mock_glib_loop, ) mock_UAL = Mock() # we replace the add_observer function with one that can set the # tiemout state, so we can ibject the timeout condition within the # glib loop. This is ugly, but necessary. def fake_add_observer(fn, state): state['status'] = UpstartApplicationLauncher.Timeout mock_UAL.observer_add_app_stop = fake_add_observer patch_UAL = patch.object(_l, 'UbuntuAppLaunch', new=mock_UAL) launcher = UpstartApplicationLauncher(mock_add_detail) app_id = self.getUniqueString() mock_logger = Mock() patch_logger = patch.object(_l, '_logger', new=mock_logger) with ExitStack() as patches: patches.enter_context(patch_get_loop) patches.enter_context(patch_UAL) patches.enter_context(patch_logger) launcher._stop_application(app_id) mock_logger.error.assert_called_once_with( "Timed out waiting for Application with app_id '%s' to stop.", app_id)
def assertFailedObserverSetsExtraMessage(self, fail_type, expected_msg): """Assert that the on_failed observer must set the expected message for a particular failure_type.""" expected_app_id = self.getUniqueString() state = {'expected_app_id': expected_app_id, 'loop': Mock()} UpstartApplicationLauncher._on_failed(expected_app_id, fail_type, state) self.assertEqual(expected_msg, state['message'])
def test_calls_get_pid(self, gpofep, ual): launcher = UpstartApplicationLauncher() token = self.getUniqueString() with patch.object(launcher, '_launch_app'): with patch.object(launcher, '_get_pid_for_launched_app') as gp: with patch.object(launcher, '_get_glib_loop'): launcher.launch(token) gp.assert_called_once_with(token)
def test_gets_correct_proxy_object(self, gpofep, ual): launcher = UpstartApplicationLauncher() with patch.object(launcher, '_launch_app'): with patch.object(launcher, '_get_pid_for_launched_app') as gp: with patch.object(launcher, '_get_glib_loop'): launcher.launch('') gpofep.assert_called_once_with(pid=gp.return_value, emulator_base=None, dbus_bus='session')
def test_handle_list(self, gpofep, ual): launcher = UpstartApplicationLauncher() token_a = self.getUniqueString() token_b = self.getUniqueString() with patch.object(launcher, '_launch_app') as la: with patch.object(launcher, '_get_pid_for_launched_app'): with patch.object(launcher, '_get_glib_loop'): launcher.launch(token_a, [token_b]) la.assert_called_once_with(token_a, [token_b])
def test_attach_application_log_does_nothing_wth_no_log_specified(self): app_id = self.getUniqueString() case_addDetail = Mock() launcher = UpstartApplicationLauncher(case_addDetail) j = MagicMock(spec=_l.journal.Reader) with patch.object(_l.journal, 'Reader', return_value=j): launcher._attach_application_log(app_id) expected = launcher._get_user_unit_match(app_id) j.add_match.assert_called_once_with(_SYSTEMD_USER_UNIT=expected) self.assertEqual(0, case_addDetail.call_count)
def test_attach_application_log_attaches_log(self): token = self.getUniqueString() case_addDetail = Mock() launcher = UpstartApplicationLauncher(case_addDetail) app_id = self.getUniqueString() j = MagicMock(spec=_l.journal.Reader) j.__iter__ = lambda x: iter([token]) with patch.object(_l.journal, 'Reader', return_value=j): launcher._attach_application_log(app_id) self.assertEqual(1, case_addDetail.call_count) content_name, content_obj = case_addDetail.call_args[0] self.assertEqual("Application Log (%s)" % app_id, content_name) self.assertThat(content_obj.as_text(), Contains(token))
def test_check_error_raises_RuntimeError_on_timeout(self): fn = lambda: UpstartApplicationLauncher._check_status_error( UpstartApplicationLauncher.Timeout) self.assertThat( fn, raises( RuntimeError( "Timed out while waiting for application to launch")))
def test_stop_calls_libUAL_stop_function(self): mock_add_detail = Mock() mock_glib_loop = Mock() patch_get_loop = patch.object( UpstartApplicationLauncher, '_get_glib_loop', new=mock_glib_loop, ) mock_UAL = Mock() patch_UAL = patch.object(_l, 'UbuntuAppLaunch', new=mock_UAL) launcher = UpstartApplicationLauncher(mock_add_detail) app_id = self.getUniqueString() with ExitStack() as patches: patches.enter_context(patch_get_loop) patches.enter_context(patch_UAL) launcher._stop_application(app_id) mock_UAL.stop_application.assert_called_once_with(app_id)
def test_get_pid_calls_upstart_module(self): expected_return = self.getUniqueInteger() with patch.object(_l, 'UbuntuAppLaunch') as mock_ual: mock_ual.get_primary_pid.return_value = expected_return observed = UpstartApplicationLauncher._get_pid_for_launched_app( 'gedit') mock_ual.get_primary_pid.assert_called_once_with('gedit') self.assertThat(expected_return, Equals(observed))
def test_stop_adds_app_stopped_observer(self): mock_add_detail = Mock() mock_glib_loop = Mock() patch_get_loop = patch.object( UpstartApplicationLauncher, '_get_glib_loop', new=mock_glib_loop, ) mock_UAL = Mock() patch_UAL = patch.object(_l, 'UbuntuAppLaunch', new=mock_UAL) launcher = UpstartApplicationLauncher(mock_add_detail) app_id = self.getUniqueString() with ExitStack() as patches: patches.enter_context(patch_get_loop) patches.enter_context(patch_UAL) launcher._stop_application(app_id) call_args = mock_UAL.observer_add_app_stop.call_args[0] self.assertThat(call_args[0], Equals(UpstartApplicationLauncher._on_stopped)) self.assertThat(call_args[1]['expected_app_id'], Equals(app_id))
def test_add_application_cleanups_does_nothing_when_app_failed(self): state = { 'status': UpstartApplicationLauncher.Failed, } launcher = UpstartApplicationLauncher(Mock()) launcher.setUp() launcher._maybe_add_application_cleanups(state) self.assertThat(launcher._cleanups._cleanups, HasLength(0))
def test_add_application_cleanups_adds_both_cleanup_actions(self): token = self.getUniqueString() state = { 'status': UpstartApplicationLauncher.Started, 'expected_app_id': token, } launcher = UpstartApplicationLauncher(Mock()) launcher.setUp() launcher._maybe_add_application_cleanups(state) self.assertThat( launcher._cleanups._cleanups, Contains((launcher._attach_application_log, (token, ), {}))) self.assertThat(launcher._cleanups._cleanups, Contains((launcher._stop_application, (token, ), {})))
def test_launch_app_calls_upstart_module(self): with patch.object(_l, 'UbuntuAppLaunch') as mock_ual: UpstartApplicationLauncher._launch_app('gedit', ['some', 'uris']) mock_ual.start_application_test.assert_called_once_with( 'gedit', ['some', 'uris'])
def test_raises_exception_on_unknown_kwargs(self): self.assertThat( lambda: UpstartApplicationLauncher(self.addDetail, unknown=True), raises( TypeError("__init__() got an unexpected keyword argument " "'unknown'")))
def test_check_error_raises_RuntimeError_on_failure(self): fn = lambda: UpstartApplicationLauncher._check_status_error( UpstartApplicationLauncher.Failed) self.assertThat(fn, raises(RuntimeError("Application Launch Failed")))
def test_get_loop_returns_glib_mainloop_instance(self): loop = UpstartApplicationLauncher._get_glib_loop() self.assertThat(loop, IsInstance(GLib.MainLoop))
def test_check_error_does_nothing_on_None(self): UpstartApplicationLauncher._check_status_error(None)
def test_check_error_raises_RuntimeError_with_extra_message(self): fn = lambda: UpstartApplicationLauncher._check_status_error( UpstartApplicationLauncher.Failed, "extra message") self.assertThat( fn, raises(RuntimeError("Application Launch Failed: extra message")))
def test_can_launch_upstart_app(self): path = self.get_qml_viewer_app_path() fixture = self.useFixture(TempDesktopFile(exec_=path, )) launcher = self.useFixture(UpstartApplicationLauncher()) launcher.launch(fixture.get_desktop_file_id())
def test_can_construct_UpstartApplicationLauncher(self): UpstartApplicationLauncher(self.addDetail)