From 7da8553896b1967d44292e211e9573e2d2d18e74 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 11 Feb 2014 20:27:19 -0600 Subject: Adding in an accounts service base test --- tests/CMakeLists.txt | 9 +++++ tests/accounts-service-user.cc | 85 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 tests/accounts-service-user.cc (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8e79fd0..e6a5f34 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -20,3 +20,12 @@ 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 gtest ${SOUNDSERVICE_LIBRARIES} ${TEST_LIBRARIES}) +add_test(accounts-service-user-test accounts-service-user-test) + diff --git a/tests/accounts-service-user.cc b/tests/accounts-service-user.cc new file mode 100644 index 0000000..db79d14 --- /dev/null +++ b/tests/accounts-service-user.cc @@ -0,0 +1,85 @@ +/* + * 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 . + * + * Authors: + * Ted Gould + */ + +#include +#include +#include + +extern "C" { +#include "indicator-sound-service.h" +} + +class AccountsServiceUserTest : public ::testing::Test +{ + + protected: + DbusTestService * service = NULL; + + GDBusConnection * session = NULL; + GDBusConnection * system = NULL; + + virtual void SetUp() { + service = dbus_test_service_new(NULL); + 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); + 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); + g_dbus_connection_set_exit_on_close(system, FALSE); + g_object_add_weak_pointer(G_OBJECT(system), (gpointer *)&system); + } + + virtual void TearDown() { + g_clear_object(&service); + + g_object_unref(session); + g_object_unref(system); + + unsigned int cleartry = 0; + while (session != NULL && system != NULL && cleartry < 100) { + loop(100); + cleartry++; + } + + ASSERT_EQ(nullptr, session); + ASSERT_EQ(nullptr, system); + } + + static gboolean timeout_cb (gpointer user_data) { + GMainLoop * loop = static_cast(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); + } +}; + +TEST_F(AccountsServiceUserTest, BasicObject) { + + +} -- cgit v1.2.3 From 3dceea54230f478ca188b4987ef352e13ea09f69 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 11 Feb 2014 21:29:30 -0600 Subject: Allocating an object and working around libaccountsservice0 --- tests/CMakeLists.txt | 8 +++++++- tests/accounts-service-user.cc | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e6a5f34..b25b20a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -26,6 +26,12 @@ add_test(name-watch-test name-watch-test) include_directories(${CMAKE_SOURCE_DIR}/src) add_executable (accounts-service-user-test accounts-service-user.cc) -target_link_libraries (accounts-service-user-test gtest ${SOUNDSERVICE_LIBRARIES} ${TEST_LIBRARIES}) +target_link_libraries ( + accounts-service-user-test + indicator-sound-service-lib + gtest + ${SOUNDSERVICE_LIBRARIES} + ${TEST_LIBRARIES} +) add_test(accounts-service-user-test accounts-service-user-test) diff --git a/tests/accounts-service-user.cc b/tests/accounts-service-user.cc index db79d14..ff1debc 100644 --- a/tests/accounts-service-user.cc +++ b/tests/accounts-service-user.cc @@ -20,6 +20,7 @@ #include #include #include +#include extern "C" { #include "indicator-sound-service.h" @@ -50,13 +51,20 @@ class AccountsServiceUserTest : public ::testing::Test } virtual void TearDown() { + /* These are the things that libaccountservice0 doesn't clean up :-( */ + g_object_unref(act_user_manager_get_default()); + g_object_unref(system); + g_object_unref(system); + g_object_unref(system); + /* End shitty untested library cleanup */ + g_clear_object(&service); g_object_unref(session); g_object_unref(system); unsigned int cleartry = 0; - while (session != NULL && system != NULL && cleartry < 100) { + while ((session != NULL || system != NULL) && cleartry < 100) { loop(100); cleartry++; } @@ -80,6 +88,7 @@ class AccountsServiceUserTest : public ::testing::Test }; TEST_F(AccountsServiceUserTest, BasicObject) { - - + AccountsServiceUser * srv = accounts_service_user_new(); + loop(50); + g_object_unref(srv); } -- cgit v1.2.3 From 073c7cefb2ae5aaae12e2251523732034e3b2f4b Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 11 Feb 2014 22:19:03 -0600 Subject: Drop a dbus mock on this problem --- tests/accounts-service-user.cc | 61 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/accounts-service-user.cc b/tests/accounts-service-user.cc index ff1debc..2a83ee5 100644 --- a/tests/accounts-service-user.cc +++ b/tests/accounts-service-user.cc @@ -31,12 +31,66 @@ class AccountsServiceUserTest : public ::testing::Test protected: DbusTestService * service = NULL; + DbusTestDbusMock * mock = NULL; GDBusConnection * session = NULL; GDBusConnection * system = NULL; virtual void SetUp() { service = dbus_test_service_new(NULL); + + 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, + "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); + + dbus_test_service_add_task(service, DBUS_TEST_TASK(mock)); dbus_test_service_start_tasks(service); g_setenv("DBUS_SYSTEM_BUS_ADDRESS", g_getenv("DBUS_SESSION_BUS_ADDRESS"), TRUE); @@ -53,11 +107,12 @@ class AccountsServiceUserTest : public ::testing::Test virtual void TearDown() { /* These are the things that libaccountservice0 doesn't clean up :-( */ g_object_unref(act_user_manager_get_default()); - g_object_unref(system); - g_object_unref(system); - g_object_unref(system); + for (int i = 0; i < 11; i++) { + g_object_unref(system); + } /* End shitty untested library cleanup */ + g_clear_object(&mock); g_clear_object(&service); g_object_unref(session); -- cgit v1.2.3 From 7035341609179f1d646b3c48df79c15d4fa5a72f Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 12 Feb 2014 11:22:32 -0600 Subject: Setting up the build so we can have Vala mocks --- src/CMakeLists.txt | 3 ++ tests/CMakeLists.txt | 71 ++++++++++++++++++++++++++++++++++++++++++++ tests/media-player-mock.vala | 51 +++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 tests/media-player-mock.vala (limited to 'tests') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d548c66..7d58afb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,7 @@ set(HEADER_PATH "${CMAKE_CURRENT_BINARY_DIR}/indicator-sound-service.h") set(SYMBOLS_PATH "${CMAKE_CURRENT_BINARY_DIR}/indicator-sound-service.def") +set(VAPI_PATH "${CMAKE_CURRENT_BINARY_DIR}/indicator-sound-service.vapi") vapi_gen(accounts-service LIBRARY @@ -93,6 +94,8 @@ vala_finish(indicator-sound-service ${HEADER_PATH} GENERATE_SYMBOLS ${SYMBOLS_PATH} + GENERATE_VAPI + ${VAPI_PATH} ) set_source_files_properties( diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b25b20a..09073b9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,6 +10,76 @@ 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 +) + ########################### # Name Watch Test @@ -29,6 +99,7 @@ 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} diff --git a/tests/media-player-mock.vala b/tests/media-player-mock.vala new file mode 100644 index 0000000..8a4a6cf --- /dev/null +++ b/tests/media-player-mock.vala @@ -0,0 +1,51 @@ +/* + * 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 . + * + * Authors: + * Ted Gould + */ + +public class MediaPlayerMock: MediaPlayer { + + 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"); + } + +} -- cgit v1.2.3 From 691f7056cc888444b55ad1a6a6949926162cffe4 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 12 Feb 2014 13:39:53 -0600 Subject: Ensuring we can allocate the player, add it to an object, and woot! --- tests/accounts-service-user.cc | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/accounts-service-user.cc b/tests/accounts-service-user.cc index 2a83ee5..e36686b 100644 --- a/tests/accounts-service-user.cc +++ b/tests/accounts-service-user.cc @@ -24,6 +24,7 @@ extern "C" { #include "indicator-sound-service.h" +#include "vala-mocks.h" } class AccountsServiceUserTest : public ::testing::Test @@ -107,7 +108,7 @@ class AccountsServiceUserTest : public ::testing::Test virtual void TearDown() { /* These are the things that libaccountservice0 doesn't clean up :-( */ g_object_unref(act_user_manager_get_default()); - for (int i = 0; i < 11; i++) { + for (int i = 0; i < 11 && system != NULL; i++) { g_object_unref(system); } /* End shitty untested library cleanup */ @@ -116,7 +117,8 @@ class AccountsServiceUserTest : public ::testing::Test g_clear_object(&service); g_object_unref(session); - g_object_unref(system); + if (system != NULL) + g_object_unref(system); unsigned int cleartry = 0; while ((session != NULL || system != NULL) && cleartry < 100) { @@ -147,3 +149,15 @@ TEST_F(AccountsServiceUserTest, BasicObject) { loop(50); g_object_unref(srv); } + +TEST_F(AccountsServiceUserTest, SetMediaPlayer) { + AccountsServiceUser * srv = accounts_service_user_new(); + MediaPlayerMock * media = media_player_mock_new(); + + accounts_service_user_set_player(srv, MEDIA_PLAYER(media)); + + loop(50); + + g_object_unref(media); + g_object_unref(srv); +} -- cgit v1.2.3 From f71a5f1208e38b670c2895cdd38b082a26210f75 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 12 Feb 2014 13:54:59 -0600 Subject: Adding mock values to fill out mock media player --- tests/media-player-mock.vala | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tests') diff --git a/tests/media-player-mock.vala b/tests/media-player-mock.vala index 8a4a6cf..14028f5 100644 --- a/tests/media-player-mock.vala +++ b/tests/media-player-mock.vala @@ -19,6 +19,31 @@ 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"); } -- cgit v1.2.3 From c2a9d0e2995ea7d32e0f3d82886ac34517694698 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 12 Feb 2014 15:13:11 -0600 Subject: Get the player test put together --- tests/CMakeLists.txt | 9 +++- tests/accounts-service-user.cc | 107 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 104 insertions(+), 12 deletions(-) (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 09073b9..a703d24 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -104,5 +104,12 @@ target_link_libraries ( ${SOUNDSERVICE_LIBRARIES} ${TEST_LIBRARIES} ) -add_test(accounts-service-user-test accounts-service-user-test) +# 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-user.cc b/tests/accounts-service-user.cc index e36686b..65806e3 100644 --- a/tests/accounts-service-user.cc +++ b/tests/accounts-service-user.cc @@ -36,6 +36,7 @@ class AccountsServiceUserTest : public ::testing::Test GDBusConnection * session = NULL; GDBusConnection * system = NULL; + GDBusProxy * proxy = NULL; virtual void SetUp() { service = dbus_test_service_new(NULL); @@ -97,29 +98,36 @@ class AccountsServiceUserTest : public ::testing::Test 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() { - /* These are the things that libaccountservice0 doesn't clean up :-( */ - g_object_unref(act_user_manager_get_default()); - for (int i = 0; i < 11 && system != NULL; i++) { - g_object_unref(system); - } - /* End shitty untested library cleanup */ - + g_clear_object(&proxy); g_clear_object(&mock); g_clear_object(&service); g_object_unref(session); - if (system != NULL) - g_object_unref(system); + 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); @@ -128,6 +136,7 @@ class AccountsServiceUserTest : public ::testing::Test ASSERT_EQ(nullptr, session); ASSERT_EQ(nullptr, system); + #endif } static gboolean timeout_cb (gpointer user_data) { @@ -142,6 +151,38 @@ class AccountsServiceUserTest : public ::testing::Test g_main_loop_run(loop); g_main_loop_unref(loop); } + + static int unref_idle (gpointer user_data) { + g_variant_unref(static_cast(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) { @@ -151,12 +192,56 @@ TEST_F(AccountsServiceUserTest, BasicObject) { } 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(); - MediaPlayerMock * media = media_player_mock_new(); accounts_service_user_set_player(srv, MEDIA_PLAYER(media)); - loop(50); + 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); -- cgit v1.2.3 From bfb2375fbd1647c5be63ee67e91b099b3faac05d Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 12 Feb 2014 15:34:24 -0600 Subject: Grab the generated header --- tests/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a703d24..1556fc7 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -80,6 +80,7 @@ target_link_libraries( indicator-sound-service-lib ) +include_directories(${CMAKE_CURRENT_BINARY_DIR}) ########################### # Name Watch Test -- cgit v1.2.3 From bc01d6ceef3dbc5119a0ac7b2fba1583f62c3d48 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Fri, 21 Feb 2014 13:00:34 -0600 Subject: Adding the timeout to the mock --- tests/accounts-service-user.cc | 3 +++ 1 file changed, 3 insertions(+) (limited to 'tests') diff --git a/tests/accounts-service-user.cc b/tests/accounts-service-user.cc index 65806e3..a52b3a0 100644 --- a/tests/accounts-service-user.cc +++ b/tests/accounts-service-user.cc @@ -67,6 +67,9 @@ class AccountsServiceUserTest : public ::testing::Test 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, + "Timeout", 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); -- cgit v1.2.3 From 129467f4faccdfd852584bb26da4011d01955239 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Fri, 21 Feb 2014 13:06:24 -0600 Subject: Can't watch hockey and type --- tests/accounts-service-user.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/accounts-service-user.cc b/tests/accounts-service-user.cc index a52b3a0..522bec9 100644 --- a/tests/accounts-service-user.cc +++ b/tests/accounts-service-user.cc @@ -68,7 +68,7 @@ class AccountsServiceUserTest : public ::testing::Test 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, - "Timeout", G_VARIANT_TYPE_UINT64, + "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, -- cgit v1.2.3 From 88dbefac5c013ed65619ed37b991f35535ef85ee Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 3 Mar 2014 14:50:52 -0600 Subject: Put the accounts service mock into a class --- tests/accounts-service-mock.h | 90 ++++++++++++++++++++++++++++++++++++++++++ tests/accounts-service-user.cc | 60 +++------------------------- 2 files changed, 95 insertions(+), 55 deletions(-) create mode 100644 tests/accounts-service-mock.h (limited to 'tests') 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 . + * + * Authors: + * Ted Gould + */ + +#include + +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 index 522bec9..b39b546 100644 --- a/tests/accounts-service-user.cc +++ b/tests/accounts-service-user.cc @@ -22,6 +22,8 @@ #include #include +#include "accounts-service-mock.h" + extern "C" { #include "indicator-sound-service.h" #include "vala-mocks.h" @@ -41,61 +43,9 @@ class AccountsServiceUserTest : public ::testing::Test virtual void SetUp() { service = dbus_test_service_new(NULL); - 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); - - dbus_test_service_add_task(service, DBUS_TEST_TASK(mock)); + 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); -- cgit v1.2.3