diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile.am | 11 | ||||
-rw-r--r-- | test/applications/test.desktop | 2 | ||||
-rwxr-xr-x | test/test-client.py | 75 |
3 files changed, 88 insertions, 0 deletions
diff --git a/test/Makefile.am b/test/Makefile.am index 4671446..4f1a163 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -65,3 +65,14 @@ libindicator_messages_service_la_LIBADD = \ libindicator_messages_service_la_LDFLAGS = \ $(COVERAGE_LDFLAGS) + +###################################### +# Test client with dbusmock +###################################### + +TESTS_ENVIRONMENT = \ + export LD_LIBRARY_PATH=$(top_builddir)/libmessaging-menu/.libs; \ + export GI_TYPELIB_PATH=$(top_builddir)/libmessaging-menu; \ + export XDG_DATA_DIRS=$(abs_srcdir); + +TESTS += test-client.py diff --git a/test/applications/test.desktop b/test/applications/test.desktop new file mode 100644 index 0000000..c2332b9 --- /dev/null +++ b/test/applications/test.desktop @@ -0,0 +1,2 @@ +[Desktop Entry] +Type=Application diff --git a/test/test-client.py b/test/test-client.py new file mode 100755 index 0000000..a1d503f --- /dev/null +++ b/test/test-client.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 + +import unittest +import dbus +from dbus.mainloop.glib import DBusGMainLoop +import dbusmock +import subprocess +from gi.repository import GLib, Gio, MessagingMenu + +DBusGMainLoop(set_as_default=True) + +class MessagingMenuTest(dbusmock.DBusTestCase): + @classmethod + def setUpClass(klass): + klass.start_session_bus() + klass.bus = klass.get_dbus(False) + + def setUp(self): + name = 'com.canonical.indicator.messages' + obj_path = '/com/canonical/indicator/messages/service' + iface = 'com.canonical.indicator.messages.service' + + self.messaging_service = self.spawn_server(name, obj_path, iface, stdout=subprocess.PIPE) + self.mock = dbus.Interface(self.bus.get_object(name, obj_path), dbusmock.MOCK_IFACE) + self.mock.AddMethod('', 'RegisterApplication', 'so', '', '') + self.mock.AddMethod('', 'UnregisterApplication', 's', '', '') + self.mock.AddMethod('', 'ApplicationStoppedRunning', 's', '', '') + self.mock.AddMethod('', 'SetStatus', 'ss', '', '') + + self.loop = GLib.MainLoop() + + def tearDown(self): + self.messaging_service.terminate() + self.messaging_service.wait() + + def assertArgumentsEqual(self, args, *expected_args): + self.assertEqual(len(args), len(expected_args)) + for i in range(len(args)): + if expected_args[i]: + self.assertEqual(args[i], expected_args[i]) + + def assertMethodCalled(self, name, *expected_args): + # set a flag on timeout, assertions don't get bubbled up through c functions + self.timed_out = False + def timeout(): self.timed_out = True + timeout_id = GLib.timeout_add_seconds(10, timeout) + while 1: + calls = self.mock.GetMethodCalls(name) + if len(calls) > 0: + GLib.source_remove(timeout_id) + self.assertArgumentsEqual(calls[0][1], *expected_args) + break + GLib.MainContext.default().iteration(True) + if self.timed_out: + raise self.failureException('method %s was not called after 10 seconds' % name) + + def test_registration(self): + mmapp = MessagingMenu.App.new('test.desktop') + mmapp.register() + self.assertMethodCalled('RegisterApplication', 'test.desktop', None) + + mmapp.unregister() + self.assertMethodCalled('UnregisterApplication', 'test.desktop') + + # ApplicationStoppedRunning is called when the last ref on mmapp is dropped + del mmapp + self.assertMethodCalled('ApplicationStoppedRunning', 'test.desktop') + + def test_status(self): + mmapp = MessagingMenu.App.new('test.desktop') + mmapp.register() + mmapp.set_status(MessagingMenu.Status.AWAY) + self.assertMethodCalled('SetStatus', 'test.desktop', 'away') + +unittest.main(testRunner=unittest.TextTestRunner()) |