From 5c47f6dbe75e413d2acbcef3e9750e937fb18431 Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Mon, 25 Feb 2013 19:34:56 -0500 Subject: Test libmessaging-menu Use dbusmock to test whether libmessaging-menu calls the right functions on registration and deregistration. --- configure.ac | 18 +++++++++----- debian/control | 1 + test/Makefile.am | 10 ++++++++ test/test-client.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 6 deletions(-) create mode 100755 test/test-client.py diff --git a/configure.ac b/configure.ac index bcd7b20..08ac680 100644 --- a/configure.ac +++ b/configure.ac @@ -75,7 +75,7 @@ AC_SUBST(COVERAGE_CXXFLAGS) AC_SUBST(COVERAGE_LDFLAGS) ########################### -# Google Test framework +# Tests ########################### AC_ARG_ENABLE([tests], @@ -85,10 +85,16 @@ AC_ARG_ENABLE([tests], if test "x$enable_tests" != "xno"; then m4_include([m4/gtest.m4]) CHECK_GTEST - if test "x$enable_tests" = "xauto"; then - enable_tests=${have_gtest} - elif test "x$enable_tests" = "xyes" && test "x$have_gtest" != "xyes"; then - AC_MSG_ERROR([tests were requested but gtest is not installed.]) + AM_PATH_PYTHON(3.0,, [:]) + AC_PYTHON_MODULE(dbusmock) + if test "x$have_gtest" = "xyes" -a "x$HAVE_PYMOD_DBUSMOCK" = "xyes"; then + enable_tests="yes" + else + if test "x$enable_tests" = "xyes"; then + AC_MSG_ERROR([tests were requested but gtest or dbusmock are not installed.]) + else + enable_tests="no" + fi fi fi AM_CONDITIONAL([BUILD_TESTS],[test "x$enable_tests" = "xyes"]) @@ -193,7 +199,7 @@ Messaging Indicator Configuration: Prefix: $prefix Indicator Dir: $INDICATORDIR - gtest: $enable_tests + tests: $enable_tests gcov: $use_gcov introspecion: $enable_introspection documentation: $enable_gtk_doc diff --git a/debian/control b/debian/control index 31292c5..efc21cd 100644 --- a/debian/control +++ b/debian/control @@ -19,6 +19,7 @@ Build-Depends: debhelper (>= 9), libgirepository1.0-dev (>= 0.9.12), gtk-doc-tools, libgtest-dev, + python3-dbusmock, Standards-Version: 3.9.3 Homepage: https://launchpad.net/indicator-messages # If you aren't a member of ~indicator-applet-developers but need to upload diff --git a/test/Makefile.am b/test/Makefile.am index 4671446..87ae2c3 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -65,3 +65,13 @@ 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; + +TESTS += test-client.py diff --git a/test/test-client.py b/test/test-client.py new file mode 100755 index 0000000..e012d6e --- /dev/null +++ b/test/test-client.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 + +import unittest +import dbus +import dbusmock +import subprocess +from gi.repository import GLib, Gio, MessagingMenu + +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 spin_loop(self, ms=10): + GLib.timeout_add(ms, lambda: self.loop.quit()) + self.loop.run() + + def assertMethodCalled(self, name, *expected_args): + calls = self.mock.GetMethodCalls(name) + self.assertEqual(len(calls), 1, 'method %s was not called' % name) + args = calls[0][1] + 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 test_registration(self): + mmapp = MessagingMenu.App.new('empathy.desktop') + mmapp.register() + self.spin_loop() + self.assertMethodCalled('RegisterApplication', 'empathy.desktop', None) + + mmapp.unregister() + self.spin_loop() + self.assertMethodCalled('UnregisterApplication', 'empathy.desktop') + + # ApplicationStoppedRunning is called when the last ref on mmapp is dropped + del mmapp + self.spin_loop() + self.assertMethodCalled('ApplicationStoppedRunning', 'empathy.desktop') + + def test_status(self): + mmapp = MessagingMenu.App.new('empathy.desktop') + mmapp.register() + mmapp.set_status(MessagingMenu.Status.AWAY) + self.spin_loop() + self.assertMethodCalled('SetStatus', 'empathy.desktop', 'away') + +unittest.main(testRunner=unittest.TextTestRunner()) -- cgit v1.2.3