Debian Codegraph @

tests/url_dispatcher_testability/fake_dispatcher.py

raw symbols SCIP
1 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2 #
3 # Copyright (C) 2014 Canonical Ltd.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation; version 3.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import dbus
18 import dbusmock
19 import subprocess
20
21
22 class FakeDispatcherException(Exception):
23 pass
24
25
26 class FakeURLDispatcherService:
27
28 """Fake URL Dispatcher service using a dbusmock interface."""
29
30 def __init__(self):
31 super(FakeURLDispatcherService, self).__init__()
32 self.dbus_connection = dbusmock.DBusTestCase.get_dbus(system_bus=False)
33
34 def start(self):
35 """Start the fake URL Dispatcher service."""
36 self.dbus_mock_server = dbusmock.DBusTestCase.spawn_server(
37 'com.lomiri.URLDispatcher',
38 '/com/lomiri/URLDispatcher',
39 'com.lomiri.URLDispatcher',
40 system_bus=False,
41 stdout=subprocess.PIPE)
42 self.mock = self._get_mock_interface()
43 self.mock.AddMethod(
44 'com.lomiri.URLDispatcher', 'DispatchURL', 'ss', '', '')
45
46 def _get_mock_interface(self):
47 return dbus.Interface(
48 self.dbus_connection.get_object(
49 'com.lomiri.URLDispatcher',
50 '/com/lomiri/URLDispatcher'),
51 dbusmock.MOCK_IFACE)
52
53 def stop(self):
54 """Stop the fake URL Dispatcher service."""
55 self.dbus_mock_server.terminate()
56 self.dbus_mock_server.wait()
57 self.dbus_mock_server.stdout.close()
58
59 def get_last_dispatch_url_call_parameter(self):
60 """Return the parameter used in the last call to dispatch URL."""
61 calls = self.mock.GetCalls()
62 if len(calls) == 0:
63 raise FakeDispatcherException(
64 'URL dispatcher has not been called.')
65 last_call = self.mock.GetCalls()[-1]
66 return last_call[2][0]