diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CMakeLists.txt | 94 | ||||
-rw-r--r-- | tests/accounts-service-mock.h | 90 | ||||
-rw-r--r-- | tests/accounts-service-user.cc | 201 | ||||
-rw-r--r-- | tests/media-player-mock.vala | 76 |
4 files changed, 461 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8e79fd0..1556fc7 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,6 +10,77 @@ add_library (gtest STATIC ${GTEST_SOURCE_DIR}/gtest_main.cc) target_link_libraries(gtest ${GTEST_LIBS}) +########################### +# Vala Mocks +########################### + +set(VALA_MOCKS_HEADER_PATH "${CMAKE_CURRENT_BINARY_DIR}/vala-mocks.h") +set(VALA_MOCKS_SYMBOLS_PATH "${CMAKE_CURRENT_BINARY_DIR}/vala-mocks.def") + +vala_init(vala-mocks + DEPENDS + indicator-sound-service-lib + PACKAGES + config + gio-2.0 + gio-unix-2.0 + libxml-2.0 + libpulse + libpulse-mainloop-glib + libnotify + accounts-service + indicator-sound-service + OPTIONS + --ccode + --thread + --vapidir=${CMAKE_BINARY_DIR}/src/ + --vapidir=${CMAKE_SOURCE_DIR}/vapi/ + --vapidir=. +) + +vala_add(vala-mocks + media-player-mock.vala +) + +vala_finish(vala-mocks + SOURCES + vala_mocks_VALA_SOURCES + OUTPUTS + vala_mocks_VALA_C + GENERATE_HEADER + ${VALA_MOCKS_HEADER_PATH} + GENERATE_SYMBOLS + ${VALA_MOCKS_SYMBOLS_PATH} +) + +set_source_files_properties( + ${vala_mocks_VALA_SOURCES} + PROPERTIES + HEADER_FILE_ONLY TRUE +) + +set( + VALA_MOCKS_SOURCES + ${vala_mocks_VALA_SOURCES} + ${vala_mocks_VALA_C} + ${VALA_MOCKS_SYMBOLS_PATH} +) + +add_definitions( + -Wno-unused-but-set-variable +) + +add_library( + vala-mocks-lib STATIC + ${VALA_MOCKS_SOURCES} +) + +target_link_libraries( + vala-mocks-lib + indicator-sound-service-lib +) + +include_directories(${CMAKE_CURRENT_BINARY_DIR}) ########################### # Name Watch Test @@ -20,3 +91,26 @@ add_executable (name-watch-test name-watch-test.cc ${CMAKE_SOURCE_DIR}/src/bus-w target_link_libraries (name-watch-test gtest ${SOUNDSERVICE_LIBRARIES}) add_test(name-watch-test name-watch-test) +########################### +# Accounts Service User +########################### + +include_directories(${CMAKE_SOURCE_DIR}/src) +add_executable (accounts-service-user-test accounts-service-user.cc) +target_link_libraries ( + accounts-service-user-test + indicator-sound-service-lib + vala-mocks-lib + gtest + ${SOUNDSERVICE_LIBRARIES} + ${TEST_LIBRARIES} +) + +# Split tests to work around libaccountservice sucking +add_test(accounts-service-user-test-basic + accounts-service-user-test --gtest_filter=AccountsServiceUserTest.BasicObject +) + +add_test(accounts-service-user-test-player + accounts-service-user-test --gtest_filter=AccountsServiceUserTest.SetMediaPlayer +) diff --git a/tests/accounts-service-mock.h b/tests/accounts-service-mock.h new file mode 100644 index 0000000..225d7b5 --- /dev/null +++ b/tests/accounts-service-mock.h @@ -0,0 +1,90 @@ +/* + * Copyright © 2014 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY 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 <http://www.gnu.org/licenses/>. + * + * Authors: + * Ted Gould <ted@canonical.com> + */ + +#include <libdbustest/dbus-test.h> + +class AccountsServiceMock +{ + DbusTestDbusMock * mock = nullptr; + + public: + AccountsServiceMock () { + mock = dbus_test_dbus_mock_new("org.freedesktop.Accounts"); + + DbusTestDbusMockObject * baseobj = dbus_test_dbus_mock_get_object(mock, "/org/freedesktop/Accounts", "org.freedesktop.Accounts", NULL); + + dbus_test_dbus_mock_object_add_method(mock, baseobj, + "CacheUser", G_VARIANT_TYPE_STRING, G_VARIANT_TYPE_OBJECT_PATH, + "ret = dbus.ObjectPath('/user')\n", NULL); + dbus_test_dbus_mock_object_add_method(mock, baseobj, + "FindUserById", G_VARIANT_TYPE_INT64, G_VARIANT_TYPE_OBJECT_PATH, + "ret = dbus.ObjectPath('/user')\n", NULL); + dbus_test_dbus_mock_object_add_method(mock, baseobj, + "FindUserByName", G_VARIANT_TYPE_STRING, G_VARIANT_TYPE_OBJECT_PATH, + "ret = dbus.ObjectPath('/user')\n", NULL); + dbus_test_dbus_mock_object_add_method(mock, baseobj, + "ListCachedUsers", NULL, G_VARIANT_TYPE_OBJECT_PATH_ARRAY, + "ret = [ dbus.ObjectPath('/user') ]\n", NULL); + dbus_test_dbus_mock_object_add_method(mock, baseobj, + "UncacheUser", G_VARIANT_TYPE_STRING, NULL, + "", NULL); + + DbusTestDbusMockObject * userobj = dbus_test_dbus_mock_get_object(mock, "/user", "org.freedesktop.Accounts.User", NULL); + dbus_test_dbus_mock_object_add_property(mock, userobj, + "UserName", G_VARIANT_TYPE_STRING, + g_variant_new_string(g_get_user_name()), NULL); + + DbusTestDbusMockObject * soundobj = dbus_test_dbus_mock_get_object(mock, "/user", "com.canonical.indicator.sound.AccountsService", NULL); + dbus_test_dbus_mock_object_add_property(mock, soundobj, + "Timestamp", G_VARIANT_TYPE_UINT64, + g_variant_new_uint64(0), NULL); + dbus_test_dbus_mock_object_add_property(mock, soundobj, + "PlayerName", G_VARIANT_TYPE_STRING, + g_variant_new_string(""), NULL); + dbus_test_dbus_mock_object_add_property(mock, soundobj, + "PlayerIcon", G_VARIANT_TYPE_VARIANT, + g_variant_new_variant(g_variant_new_string("")), NULL); + dbus_test_dbus_mock_object_add_property(mock, soundobj, + "Running", G_VARIANT_TYPE_BOOLEAN, + g_variant_new_boolean(FALSE), NULL); + dbus_test_dbus_mock_object_add_property(mock, soundobj, + "State", G_VARIANT_TYPE_STRING, + g_variant_new_string(""), NULL); + dbus_test_dbus_mock_object_add_property(mock, soundobj, + "Title", G_VARIANT_TYPE_STRING, + g_variant_new_string(""), NULL); + dbus_test_dbus_mock_object_add_property(mock, soundobj, + "Artist", G_VARIANT_TYPE_STRING, + g_variant_new_string(""), NULL); + dbus_test_dbus_mock_object_add_property(mock, soundobj, + "Album", G_VARIANT_TYPE_STRING, + g_variant_new_string(""), NULL); + dbus_test_dbus_mock_object_add_property(mock, soundobj, + "ArtUrl", G_VARIANT_TYPE_STRING, + g_variant_new_string(""), NULL); + } + + ~AccountsServiceMock () { + g_clear_object(&mock); + } + + operator DbusTestTask* () { + return DBUS_TEST_TASK(mock); + } +}; diff --git a/tests/accounts-service-user.cc b/tests/accounts-service-user.cc new file mode 100644 index 0000000..b39b546 --- /dev/null +++ b/tests/accounts-service-user.cc @@ -0,0 +1,201 @@ +/* + * Copyright © 2014 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY 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 <http://www.gnu.org/licenses/>. + * + * Authors: + * Ted Gould <ted@canonical.com> + */ + +#include <gtest/gtest.h> +#include <gio/gio.h> +#include <libdbustest/dbus-test.h> +#include <act/act.h> + +#include "accounts-service-mock.h" + +extern "C" { +#include "indicator-sound-service.h" +#include "vala-mocks.h" +} + +class AccountsServiceUserTest : public ::testing::Test +{ + + protected: + DbusTestService * service = NULL; + DbusTestDbusMock * mock = NULL; + + GDBusConnection * session = NULL; + GDBusConnection * system = NULL; + GDBusProxy * proxy = NULL; + + virtual void SetUp() { + service = dbus_test_service_new(NULL); + + AccountsServiceMock service_mock; + + dbus_test_service_add_task(service, (DbusTestTask*)service_mock); + dbus_test_service_start_tasks(service); + + g_setenv("DBUS_SYSTEM_BUS_ADDRESS", g_getenv("DBUS_SESSION_BUS_ADDRESS"), TRUE); + + session = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL); + ASSERT_NE(nullptr, session); + g_dbus_connection_set_exit_on_close(session, FALSE); + g_object_add_weak_pointer(G_OBJECT(session), (gpointer *)&session); + + system = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL); + ASSERT_NE(nullptr, system); + g_dbus_connection_set_exit_on_close(system, FALSE); + g_object_add_weak_pointer(G_OBJECT(system), (gpointer *)&system); + + proxy = g_dbus_proxy_new_sync(session, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + "org.freedesktop.Accounts", + "/user", + "org.freedesktop.DBus.Properties", + NULL, NULL); + ASSERT_NE(nullptr, proxy); + } + + virtual void TearDown() { + g_clear_object(&proxy); + g_clear_object(&mock); + g_clear_object(&service); + + g_object_unref(session); + g_object_unref(system); + + #if 0 + /* Accounts Service keeps a bunch of references around so we + have to split the tests and can't check this :-( */ + unsigned int cleartry = 0; + while ((session != NULL || system != NULL) && cleartry < 100) { + loop(100); + cleartry++; + } + + ASSERT_EQ(nullptr, session); + ASSERT_EQ(nullptr, system); + #endif + } + + static gboolean timeout_cb (gpointer user_data) { + GMainLoop * loop = static_cast<GMainLoop *>(user_data); + g_main_loop_quit(loop); + return G_SOURCE_REMOVE; + } + + void loop (unsigned int ms) { + GMainLoop * loop = g_main_loop_new(NULL, FALSE); + g_timeout_add(ms, timeout_cb, loop); + g_main_loop_run(loop); + g_main_loop_unref(loop); + } + + static int unref_idle (gpointer user_data) { + g_variant_unref(static_cast<GVariant *>(user_data)); + return G_SOURCE_REMOVE; + } + + const gchar * get_property_string (const gchar * name) { + GVariant * propval = g_dbus_proxy_call_sync(proxy, + "Get", + g_variant_new("(ss)", "com.canonical.indicator.sound.AccountsService", name), + G_DBUS_CALL_FLAGS_NONE, + -1, NULL, NULL + ); + + if (propval == nullptr) { + return nullptr; + } + + /* NOTE: This is a bit of a hack, basically if main gets + called the returned string becomes invalid. But it + makes the test much easier to read :-/ */ + g_idle_add(unref_idle, propval); + + const gchar * ret = NULL; + GVariant * child = g_variant_get_child_value(propval, 0); + GVariant * vstr = g_variant_get_variant(child); + ret = g_variant_get_string(vstr, NULL); + g_variant_unref(vstr); + g_variant_unref(child); + + return ret; + } +}; + +TEST_F(AccountsServiceUserTest, BasicObject) { + AccountsServiceUser * srv = accounts_service_user_new(); + loop(50); + g_object_unref(srv); +} + +TEST_F(AccountsServiceUserTest, SetMediaPlayer) { + MediaPlayerTrack * track = media_player_track_new("Artist", "Title", "Album", "http://art.url"); + + MediaPlayerMock * media = MEDIA_PLAYER_MOCK( + g_object_new(TYPE_MEDIA_PLAYER_MOCK, + "mock-id", "player-id", + "mock-name", "Test Player", + "mock-state", "Playing", + "mock-is-running", TRUE, + "mock-can-raise", FALSE, + "mock-current-track", track, + NULL) + ); + g_clear_object(&track); + + AccountsServiceUser * srv = accounts_service_user_new(); + + accounts_service_user_set_player(srv, MEDIA_PLAYER(media)); + + loop(500); + + /* Verify the values are on the other side of the bus */ + EXPECT_STREQ("Test Player", get_property_string("PlayerName")); + EXPECT_STREQ("Playing", get_property_string("State")); + EXPECT_STREQ("Title", get_property_string("Title")); + EXPECT_STREQ("Artist", get_property_string("Artist")); + EXPECT_STREQ("Album", get_property_string("Album")); + EXPECT_STREQ("http://art.url", get_property_string("ArtUrl")); + + /* Check changing the track info */ + track = media_player_track_new("Artist-ish", "Title-like", "Psuedo Album", "http://fake.art.url"); + media_player_mock_set_mock_current_track(media, track); + g_clear_object(&track); + accounts_service_user_set_player(srv, MEDIA_PLAYER(media)); + + loop(500); + + EXPECT_STREQ("Test Player", get_property_string("PlayerName")); + EXPECT_STREQ("Playing", get_property_string("State")); + EXPECT_STREQ("Title-like", get_property_string("Title")); + EXPECT_STREQ("Artist-ish", get_property_string("Artist")); + EXPECT_STREQ("Psuedo Album", get_property_string("Album")); + EXPECT_STREQ("http://fake.art.url", get_property_string("ArtUrl")); + + /* Check to ensure the state can be updated */ + media_player_set_state(MEDIA_PLAYER(media), "Paused"); + accounts_service_user_set_player(srv, MEDIA_PLAYER(media)); + + loop(500); + + EXPECT_STREQ("Paused", get_property_string("State")); + + g_object_unref(media); + g_object_unref(srv); +} diff --git a/tests/media-player-mock.vala b/tests/media-player-mock.vala new file mode 100644 index 0000000..14028f5 --- /dev/null +++ b/tests/media-player-mock.vala @@ -0,0 +1,76 @@ +/* + * Copyright © 2014 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY 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 <http://www.gnu.org/licenses/>. + * + * Authors: + * Ted Gould <ted@canonical.com> + */ + +public class MediaPlayerMock: MediaPlayer { + + /* Superclass variables */ + public override string id { get { return mock_id; } } + public override string name { get { return mock_name; } } + public override string state { get { return mock_state; } set { this.mock_state = value; }} + public override Icon? icon { get { return mock_icon; } } + public override string dbus_name { get { return mock_dbus_name; } } + + public override bool is_running { get { return mock_is_running; } } + public override bool can_raise { get { return mock_can_raise; } } + + public override MediaPlayer.Track? current_track { get { return mock_current_track; } set { this.mock_current_track = value; } } + + /* Mock values */ + public string mock_id { get; set; } + public string mock_name { get; set; } + public string mock_state { get; set; } + public Icon? mock_icon { get; set; } + public string mock_dbus_name { get; set; } + + public bool mock_is_running { get; set; } + public bool mock_can_raise { get; set; } + + public MediaPlayer.Track? mock_current_track { get; set; } + + /* Virtual functions */ + public override void activate () { + debug("Mock activate"); + } + public override void play_pause () { + debug("Mock play_pause"); + } + public override void next () { + debug("Mock next"); + } + public override void previous () { + debug("Mock previous"); + } + + public override uint get_n_playlists() { + debug("Mock get_n_playlists"); + return 0; + } + public override string get_playlist_id (int index) { + debug("Mock get_playlist_id"); + return ""; + } + public override string get_playlist_name (int index) { + debug("Mock get_playlist_name"); + return ""; + } + public override void activate_playlist_by_name (string playlist) { + debug("Mock activate_playlist_by_name"); + } + +} |