From e4b9f042a74b471f0b5992b99701231c3052e3ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Thu, 10 Apr 2014 17:11:06 +0200 Subject: MockUnitySession: add new mock for unity session, and use it in tests --- tests/backend-dbus/CMakeLists.txt | 2 + tests/backend-dbus/gtest-mock-dbus-fixture.h | 4 ++ tests/backend-dbus/mock-unity-session.cc | 71 ++++++++++++++++++++++++++++ tests/backend-dbus/mock-unity-session.h | 53 +++++++++++++++++++++ tests/backend-dbus/test-actions.cc | 8 +++- 5 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 tests/backend-dbus/mock-unity-session.cc create mode 100644 tests/backend-dbus/mock-unity-session.h (limited to 'tests') diff --git a/tests/backend-dbus/CMakeLists.txt b/tests/backend-dbus/CMakeLists.txt index e28d8e6..6ef68bd 100644 --- a/tests/backend-dbus/CMakeLists.txt +++ b/tests/backend-dbus/CMakeLists.txt @@ -23,6 +23,8 @@ add_library (desktopmock STATIC mock-object.h mock-screen-saver.cc mock-screen-saver.h + mock-unity-session.cc + mock-unity-session.h mock-session-manager.cc mock-session-manager.h mock-user.cc diff --git a/tests/backend-dbus/gtest-mock-dbus-fixture.h b/tests/backend-dbus/gtest-mock-dbus-fixture.h index bab82bf..d4f598c 100644 --- a/tests/backend-dbus/gtest-mock-dbus-fixture.h +++ b/tests/backend-dbus/gtest-mock-dbus-fixture.h @@ -25,6 +25,7 @@ #include "mock-display-manager-seat.h" #include "mock-end-session-dialog.h" #include "mock-screen-saver.h" +#include "mock-unity-session.h" #include "mock-session-manager.h" #include "mock-user.h" #include "mock-webcredentials.h" @@ -42,6 +43,7 @@ class GTestMockDBusFixture: public GTestDBusFixture protected: MockScreenSaver * screen_saver; + MockUnitySession * unity_session; MockSessionManager * session_manager; MockDisplayManagerSeat * dm_seat; MockAccounts * accounts; @@ -60,6 +62,7 @@ class GTestMockDBusFixture: public GTestDBusFixture end_session_dialog = new MockEndSessionDialog (loop, conn); session_manager = new MockSessionManager (loop, conn); screen_saver = new MockScreenSaver (loop, conn); + unity_session = new MockUnitySession (loop, conn); dm_seat = new MockDisplayManagerSeat (loop, conn); g_setenv ("XDG_SEAT_PATH", dm_seat->path(), TRUE); dm_seat->set_guest_allowed (false); @@ -83,6 +86,7 @@ class GTestMockDBusFixture: public GTestDBusFixture delete login1_manager; delete dm_seat; delete screen_saver; + delete unity_session; delete session_manager; delete end_session_dialog; delete webcredentials; diff --git a/tests/backend-dbus/mock-unity-session.cc b/tests/backend-dbus/mock-unity-session.cc new file mode 100644 index 0000000..c996310 --- /dev/null +++ b/tests/backend-dbus/mock-unity-session.cc @@ -0,0 +1,71 @@ +/* + * Copyright 2014 Canonical Ltd. + * + * Authors: + * Marco Trevisan + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "mock-unity-session.h" + + +gboolean +MockUnitySession :: handle_lock (UnitySession * us, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->my_last_action = Lock; + unity_session_complete_lock (us, inv); + return true; +} + +gboolean +MockUnitySession :: handle_prompt_lock (UnitySession * us, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->my_last_action = PromptLock; + unity_session_complete_prompt_lock (us, inv); + return true; +} + +/*** +**** +***/ + +namespace +{ + const char * const UNITY_SESSION_NAME = "com.canonical.Unity"; + const char * const UNITY_SESSION_PATH = "/com/canonical/Unity/Session"; + +} + +MockUnitySession :: MockUnitySession (GMainLoop * loop, + GDBusConnection * bus_connection): + MockObject (loop, bus_connection, UNITY_SESSION_NAME, UNITY_SESSION_PATH), + my_skeleton (unity_session_skeleton_new ()), + my_last_action (None) +{ + g_signal_connect (my_skeleton, "handle-lock", + G_CALLBACK(handle_lock), this); + g_signal_connect (my_skeleton, "handle-prompt-lock", + G_CALLBACK(handle_prompt_lock), this); + + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); +} + +MockUnitySession :: ~MockUnitySession () +{ + g_clear_object (&my_skeleton); +} diff --git a/tests/backend-dbus/mock-unity-session.h b/tests/backend-dbus/mock-unity-session.h new file mode 100644 index 0000000..ded246a --- /dev/null +++ b/tests/backend-dbus/mock-unity-session.h @@ -0,0 +1,53 @@ +/* + * Copyright 2014 Canonical Ltd. + * + * Authors: + * Marco Trevisan + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MOCK_UNITY_SESSION_H +#define MOCK_UNITY_SESSION_H + +#include "mock-object.h" // parent class +#include "backend-dbus/unity-session.h" // Unity Session + +class MockUnitySession: public MockObject +{ + public: + + MockUnitySession (GMainLoop * loop, + GDBusConnection * bus_connection); + virtual ~MockUnitySession (); + + public: + + enum Action { None, Lock, PromptLock, RequestLogout, RequestShutdown, RequestReboot }; + Action last_action () { return my_last_action; } + + private: + + UnitySession * my_skeleton; + Action my_last_action; + + static gboolean handle_lock (UnitySession *, + GDBusMethodInvocation *, + gpointer); + static gboolean handle_prompt_lock (UnitySession *, + GDBusMethodInvocation *, + gpointer); + +}; + +#endif diff --git a/tests/backend-dbus/test-actions.cc b/tests/backend-dbus/test-actions.cc index c0f8517..717509d 100644 --- a/tests/backend-dbus/test-actions.cc +++ b/tests/backend-dbus/test-actions.cc @@ -316,17 +316,19 @@ TEST_F (Actions, Hibernate) TEST_F (Actions, SwitchToScreensaver) { - ASSERT_EQ (MockScreenSaver::None, screen_saver->last_action()); + ASSERT_EQ (MockUnitySession::None, unity_session->last_action()); indicator_session_actions_switch_to_screensaver (actions); wait_msec (50); - ASSERT_EQ (MockScreenSaver::Lock, screen_saver->last_action()); + ASSERT_EQ (MockUnitySession::Lock, unity_session->last_action()); } TEST_F (Actions, SwitchToGreeter) { ASSERT_NE (MockDisplayManagerSeat::GREETER, dm_seat->last_action()); + ASSERT_EQ (MockUnitySession::None, unity_session->last_action()); indicator_session_actions_switch_to_greeter (actions); wait_msec (50); + ASSERT_EQ (MockUnitySession::PromptLock, unity_session->last_action()); ASSERT_EQ (MockDisplayManagerSeat::GREETER, dm_seat->last_action()); } @@ -346,6 +348,7 @@ TEST_F (Actions, SwitchToGuest) wait_for_signal (login1_seat->skeleton(), "notify::active-session"); ASSERT_EQ (guest_session_tag, login1_seat->active_session()); wait_msec (50); + ASSERT_EQ (MockUnitySession::PromptLock, unity_session->last_action()); } TEST_F (Actions, SwitchToUsername) @@ -367,6 +370,7 @@ TEST_F (Actions, SwitchToUsername) wait_for_signal (login1_seat->skeleton(), "notify::active-session"); ASSERT_EQ (dr1_session, login1_seat->active_session()); wait_msec (50); + ASSERT_EQ (MockUnitySession::PromptLock, unity_session->last_action()); indicator_session_actions_switch_to_username (actions, dr2_username); wait_for_signal (login1_seat->skeleton(), "notify::active-session"); -- cgit v1.2.3