1
# Copyright 2016 Canonical Ltd.
3
# This program is free software: you can redistribute it and/or modify it
4
# under the terms of the GNU General Public License version 3, as published
5
# by the Free Software Foundation.
7
# This program is distributed in the hope that it will be useful, but
8
# WITHOUT ANY WARRANTY; without even the implied warranties of
9
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
10
# PURPOSE. See the GNU General Public License for more details.
12
# You should have received a copy of the GNU General Public License along
13
# with this program. If not, see <http://www.gnu.org/licenses/>.
20
from libertine import launcher
22
from testtools import TestCase
23
from testtools.matchers import Equals, Contains
24
from unittest.mock import patch
28
class MockDBusServer(object):
30
A mock object that proxies a mock D-Bus server, for testing things that do
31
D-Bus operations in the background.
34
def __init__(self, maliit_server_address):
35
dbusmock.DBusTestCase.start_session_bus()
36
self.maliit_server_address = maliit_server_address
37
self._dbus_connection = dbusmock.DBusTestCase.get_dbus(system_bus=False)
40
self._dbus_mock_server = dbusmock.DBusTestCase.spawn_server(
42
'/org/maliit/server/address',
43
'org.maliit.Server.Address',
45
stdout=subprocess.PIPE)
46
self._mock_interface = self._get_mock_interface()
47
self._mock_interface.AddProperty('', 'address', self.maliit_server_address)
50
self._dbus_mock_server.terminate()
51
self._dbus_mock_server.wait()
56
def _get_mock_interface(self):
57
return dbus.Interface(
58
self._dbus_connection.get_object(
60
'/org/maliit/server/address',
61
'org.maliit.Server.Address'),
65
class TestLauncherConfigUsingDBus(TestCase):
67
Verifies the defined behaviour of the Libertine Launcher Config class with
68
reference to a D-Bus server.
72
Need to save and restore the os.environ for each test in case they
73
change things. That's a global that propagates between tests.
76
fake_maliit_host_address = 'unix:abstract=/tmp/maliit-host-socket'
77
self._dbus_server = self.useFixture(MockDBusServer(maliit_server_address=fake_maliit_host_address))
78
self._cli = [ '-i', self.getUniqueString(), self.getUniqueString() ]
80
def test_maliit_socket_bridge_from_dbus(self):
82
Make sure the Maliit socket bridge gets configured.
84
The Maliit service advertizes it's socket on the D-Bus, although the
85
environment variable, if present, overrides that so to test the D-Bus
86
advert the environment variable must be clear.
89
with patch.dict('os.environ'):
90
if 'MALIIT_SERVER_ADDRESS' in os.environ:
91
del os.environ['MALIIT_SERVER_ADDRESS']
93
config = launcher.Config(self._cli[:])
95
for bridge in config.socket_bridges:
96
if bridge.env_var == 'MALIIT_SERVER_ADDRESS':
97
maliit_bridge = bridge
99
self.assertIsNotNone(maliit_bridge)
100
self.assertThat(maliit_bridge.host_address, Equals(self._dbus_server.maliit_server_address))
101
self.assertThat(maliit_bridge.session_address, Contains('maliit'))
102
self.assertThat(maliit_bridge.session_address, Contains(config.id))