From e2e27b5e147d08e150eefd9d64dcb504594f86b8 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 3 Mar 2014 10:43:31 -0600 Subject: Setting up a basic sound menu test --- tests/CMakeLists.txt | 17 +++++++++++++++++ tests/sound-menu-test.cc | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/sound-menu-test.cc (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1556fc7..0247998 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -114,3 +114,20 @@ add_test(accounts-service-user-test-basic add_test(accounts-service-user-test-player accounts-service-user-test --gtest_filter=AccountsServiceUserTest.SetMediaPlayer ) + +########################### +# Accounts Service User +########################### + +include_directories(${CMAKE_SOURCE_DIR}/src) +add_executable (sound-menu-test sound-menu-test.cc) +target_link_libraries ( + sound-menu-test + indicator-sound-service-lib + vala-mocks-lib + gtest + ${SOUNDSERVICE_LIBRARIES} + ${TEST_LIBRARIES} +) + +add_test(sound-menu-test sound-menu-test) diff --git a/tests/sound-menu-test.cc b/tests/sound-menu-test.cc new file mode 100644 index 0000000..199391c --- /dev/null +++ b/tests/sound-menu-test.cc @@ -0,0 +1,49 @@ +/* + * 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 + +extern "C" { +#include "indicator-sound-service.h" +#include "vala-mocks.h" +} + +class SoundMenuTest : public ::testing::Test +{ + protected: + GTestDBus * bus = nullptr; + + virtual void SetUp() { + bus = g_test_dbus_new(G_TEST_DBUS_NONE); + g_test_dbus_up(bus); + } + + virtual void TearDown() { + g_test_dbus_down(bus); + g_clear_object(&bus); + } +}; + +TEST_F(SoundMenuTest, BasicObject) { + SoundMenu * menu = sound_menu_new (nullptr, SOUND_MENU_DISPLAY_FLAGS_NONE); + + g_clear_object(&menu); + return; +} -- cgit v1.2.3 From 248a67022dcfb0cc2540720007ee122fbc3eb6ea Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 3 Mar 2014 11:04:00 -0600 Subject: Skeleton add/remove player --- tests/sound-menu-test.cc | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'tests') diff --git a/tests/sound-menu-test.cc b/tests/sound-menu-test.cc index 199391c..4d28c4a 100644 --- a/tests/sound-menu-test.cc +++ b/tests/sound-menu-test.cc @@ -47,3 +47,33 @@ TEST_F(SoundMenuTest, BasicObject) { g_clear_object(&menu); return; } + +TEST_F(SoundMenuTest, AddRemovePlayer) { + SoundMenu * menu = sound_menu_new (nullptr, SOUND_MENU_DISPLAY_FLAGS_NONE); + + 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); + + sound_menu_add_player(menu, MEDIA_PLAYER(media)); + + /* TODO: Verify */ + + sound_menu_remove_player(menu, MEDIA_PLAYER(media)); + + /* TODO: Verify */ + + g_clear_object(&media); + g_clear_object(&menu); + return; +} -- cgit v1.2.3 From cc8600927594dcdfcba74813788d90c999156468 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 3 Mar 2014 11:11:32 -0600 Subject: Test the number of entries in the menu --- tests/sound-menu-test.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/sound-menu-test.cc b/tests/sound-menu-test.cc index 4d28c4a..5d18d08 100644 --- a/tests/sound-menu-test.cc +++ b/tests/sound-menu-test.cc @@ -44,6 +44,8 @@ class SoundMenuTest : public ::testing::Test TEST_F(SoundMenuTest, BasicObject) { SoundMenu * menu = sound_menu_new (nullptr, SOUND_MENU_DISPLAY_FLAGS_NONE); + ASSERT_NE(nullptr, menu); + g_clear_object(&menu); return; } @@ -67,11 +69,12 @@ TEST_F(SoundMenuTest, AddRemovePlayer) { sound_menu_add_player(menu, MEDIA_PLAYER(media)); - /* TODO: Verify */ + ASSERT_NE(nullptr, menu->menu); + EXPECT_EQ(2, g_menu_model_get_n_items(G_MENU_MODEL(menu->menu))); sound_menu_remove_player(menu, MEDIA_PLAYER(media)); - /* TODO: Verify */ + EXPECT_EQ(1, g_menu_model_get_n_items(G_MENU_MODEL(menu->menu))); g_clear_object(&media); g_clear_object(&menu); -- cgit v1.2.3 From 37dcddab978f1d050287a83353216e54ad74670b Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 3 Mar 2014 11:34:08 -0600 Subject: Test attributes on the menu --- tests/sound-menu-test.cc | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'tests') diff --git a/tests/sound-menu-test.cc b/tests/sound-menu-test.cc index 5d18d08..10c0cb9 100644 --- a/tests/sound-menu-test.cc +++ b/tests/sound-menu-test.cc @@ -39,6 +39,21 @@ class SoundMenuTest : public ::testing::Test g_test_dbus_down(bus); g_clear_object(&bus); } + + void verify_item_attribute (GMenuModel * mm, guint index, const gchar * name, GVariant * value) { + g_variant_ref_sink(value); + + gchar * variantstr = g_variant_print(value, TRUE); + g_debug("Expecting item %d to have a '%s' attribute: %s", index, name, variantstr); + + const GVariantType * type = g_variant_get_type(value); + GVariant * itemval = g_menu_model_get_item_attribute_value(mm, index, name, type); + + ASSERT_NE(nullptr, itemval); + EXPECT_TRUE(g_variant_equal(itemval, value)); + + g_variant_unref(value); + } }; TEST_F(SoundMenuTest, BasicObject) { @@ -72,6 +87,22 @@ TEST_F(SoundMenuTest, AddRemovePlayer) { ASSERT_NE(nullptr, menu->menu); EXPECT_EQ(2, g_menu_model_get_n_items(G_MENU_MODEL(menu->menu))); + GMenuModel * section = g_menu_model_get_item_link(G_MENU_MODEL(menu->menu), 1, G_MENU_LINK_SECTION); + ASSERT_NE(nullptr, section); + EXPECT_EQ(2, g_menu_model_get_n_items(section)); /* No playlists, so two items */ + + /* Player display */ + verify_item_attribute(section, 0, "action", g_variant_new_string("indicator.player-id")); + verify_item_attribute(section, 0, "x-canonical-type", g_variant_new_string("com.canonical.unity.media-player")); + + /* Player control */ + verify_item_attribute(section, 1, "x-canonical-type", g_variant_new_string("com.canonical.unity.playback-item")); + verify_item_attribute(section, 1, "x-canonical-play-action", g_variant_new_string("indicator.play.player-id")); + verify_item_attribute(section, 1, "x-canonical-next-action", g_variant_new_string("indicator.next.player-id")); + verify_item_attribute(section, 1, "x-canonical-previous-action", g_variant_new_string("indicator.previous.player-id")); + + g_clear_object(§ion); + sound_menu_remove_player(menu, MEDIA_PLAYER(media)); EXPECT_EQ(1, g_menu_model_get_n_items(G_MENU_MODEL(menu->menu))); -- 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 From a251c36b195cf1e24a6934c4ea667b753a1ee538 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 3 Mar 2014 15:33:21 -0600 Subject: Setting up a test for the media player user --- tests/CMakeLists.txt | 23 ++++++++++- tests/media-player-user.cc | 100 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 tests/media-player-user.cc (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0247998..d4aa66e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -116,7 +116,7 @@ add_test(accounts-service-user-test-player ) ########################### -# Accounts Service User +# Sound Menu ########################### include_directories(${CMAKE_SOURCE_DIR}/src) @@ -131,3 +131,24 @@ target_link_libraries ( ) add_test(sound-menu-test sound-menu-test) + +########################### +# Accounts Service User +########################### + +include_directories(${CMAKE_SOURCE_DIR}/src) +add_executable (media-player-user-test media-player-user.cc) +target_link_libraries ( + media-player-user-test + indicator-sound-service-lib + vala-mocks-lib + gtest + ${SOUNDSERVICE_LIBRARIES} + ${TEST_LIBRARIES} +) + +# Split tests to work around libaccountservice sucking +add_test(media-player-user-test-basic + media-player-user-test --gtest_filter=MediaPlayerUserTest.BasicObject +) + diff --git a/tests/media-player-user.cc b/tests/media-player-user.cc new file mode 100644 index 0000000..3e241d8 --- /dev/null +++ b/tests/media-player-user.cc @@ -0,0 +1,100 @@ +/* + * 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 + +#include "accounts-service-mock.h" + +extern "C" { +#include "indicator-sound-service.h" +} + +class MediaPlayerUserTest : 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); + + 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); + } + + virtual void TearDown() { + 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(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(MediaPlayerUserTest, BasicObject) { + MediaPlayerUser * player = media_player_user_new("user"); + ASSERT_NE(nullptr, player); + + g_clear_object(&player); +} -- cgit v1.2.3 From cf305cb68f4fd401a7fe1b4552ec2f46d93028af Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 3 Mar 2014 15:38:38 -0600 Subject: Make naming consistent --- tests/CMakeLists.txt | 2 +- tests/sound-menu-test.cc | 113 ----------------------------------------------- tests/sound-menu.cc | 113 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 114 deletions(-) delete mode 100644 tests/sound-menu-test.cc create mode 100644 tests/sound-menu.cc (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d4aa66e..9049045 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -120,7 +120,7 @@ add_test(accounts-service-user-test-player ########################### include_directories(${CMAKE_SOURCE_DIR}/src) -add_executable (sound-menu-test sound-menu-test.cc) +add_executable (sound-menu-test sound-menu.cc) target_link_libraries ( sound-menu-test indicator-sound-service-lib diff --git a/tests/sound-menu-test.cc b/tests/sound-menu-test.cc deleted file mode 100644 index 10c0cb9..0000000 --- a/tests/sound-menu-test.cc +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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 - -extern "C" { -#include "indicator-sound-service.h" -#include "vala-mocks.h" -} - -class SoundMenuTest : public ::testing::Test -{ - protected: - GTestDBus * bus = nullptr; - - virtual void SetUp() { - bus = g_test_dbus_new(G_TEST_DBUS_NONE); - g_test_dbus_up(bus); - } - - virtual void TearDown() { - g_test_dbus_down(bus); - g_clear_object(&bus); - } - - void verify_item_attribute (GMenuModel * mm, guint index, const gchar * name, GVariant * value) { - g_variant_ref_sink(value); - - gchar * variantstr = g_variant_print(value, TRUE); - g_debug("Expecting item %d to have a '%s' attribute: %s", index, name, variantstr); - - const GVariantType * type = g_variant_get_type(value); - GVariant * itemval = g_menu_model_get_item_attribute_value(mm, index, name, type); - - ASSERT_NE(nullptr, itemval); - EXPECT_TRUE(g_variant_equal(itemval, value)); - - g_variant_unref(value); - } -}; - -TEST_F(SoundMenuTest, BasicObject) { - SoundMenu * menu = sound_menu_new (nullptr, SOUND_MENU_DISPLAY_FLAGS_NONE); - - ASSERT_NE(nullptr, menu); - - g_clear_object(&menu); - return; -} - -TEST_F(SoundMenuTest, AddRemovePlayer) { - SoundMenu * menu = sound_menu_new (nullptr, SOUND_MENU_DISPLAY_FLAGS_NONE); - - 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); - - sound_menu_add_player(menu, MEDIA_PLAYER(media)); - - ASSERT_NE(nullptr, menu->menu); - EXPECT_EQ(2, g_menu_model_get_n_items(G_MENU_MODEL(menu->menu))); - - GMenuModel * section = g_menu_model_get_item_link(G_MENU_MODEL(menu->menu), 1, G_MENU_LINK_SECTION); - ASSERT_NE(nullptr, section); - EXPECT_EQ(2, g_menu_model_get_n_items(section)); /* No playlists, so two items */ - - /* Player display */ - verify_item_attribute(section, 0, "action", g_variant_new_string("indicator.player-id")); - verify_item_attribute(section, 0, "x-canonical-type", g_variant_new_string("com.canonical.unity.media-player")); - - /* Player control */ - verify_item_attribute(section, 1, "x-canonical-type", g_variant_new_string("com.canonical.unity.playback-item")); - verify_item_attribute(section, 1, "x-canonical-play-action", g_variant_new_string("indicator.play.player-id")); - verify_item_attribute(section, 1, "x-canonical-next-action", g_variant_new_string("indicator.next.player-id")); - verify_item_attribute(section, 1, "x-canonical-previous-action", g_variant_new_string("indicator.previous.player-id")); - - g_clear_object(§ion); - - sound_menu_remove_player(menu, MEDIA_PLAYER(media)); - - EXPECT_EQ(1, g_menu_model_get_n_items(G_MENU_MODEL(menu->menu))); - - g_clear_object(&media); - g_clear_object(&menu); - return; -} diff --git a/tests/sound-menu.cc b/tests/sound-menu.cc new file mode 100644 index 0000000..10c0cb9 --- /dev/null +++ b/tests/sound-menu.cc @@ -0,0 +1,113 @@ +/* + * 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 + +extern "C" { +#include "indicator-sound-service.h" +#include "vala-mocks.h" +} + +class SoundMenuTest : public ::testing::Test +{ + protected: + GTestDBus * bus = nullptr; + + virtual void SetUp() { + bus = g_test_dbus_new(G_TEST_DBUS_NONE); + g_test_dbus_up(bus); + } + + virtual void TearDown() { + g_test_dbus_down(bus); + g_clear_object(&bus); + } + + void verify_item_attribute (GMenuModel * mm, guint index, const gchar * name, GVariant * value) { + g_variant_ref_sink(value); + + gchar * variantstr = g_variant_print(value, TRUE); + g_debug("Expecting item %d to have a '%s' attribute: %s", index, name, variantstr); + + const GVariantType * type = g_variant_get_type(value); + GVariant * itemval = g_menu_model_get_item_attribute_value(mm, index, name, type); + + ASSERT_NE(nullptr, itemval); + EXPECT_TRUE(g_variant_equal(itemval, value)); + + g_variant_unref(value); + } +}; + +TEST_F(SoundMenuTest, BasicObject) { + SoundMenu * menu = sound_menu_new (nullptr, SOUND_MENU_DISPLAY_FLAGS_NONE); + + ASSERT_NE(nullptr, menu); + + g_clear_object(&menu); + return; +} + +TEST_F(SoundMenuTest, AddRemovePlayer) { + SoundMenu * menu = sound_menu_new (nullptr, SOUND_MENU_DISPLAY_FLAGS_NONE); + + 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); + + sound_menu_add_player(menu, MEDIA_PLAYER(media)); + + ASSERT_NE(nullptr, menu->menu); + EXPECT_EQ(2, g_menu_model_get_n_items(G_MENU_MODEL(menu->menu))); + + GMenuModel * section = g_menu_model_get_item_link(G_MENU_MODEL(menu->menu), 1, G_MENU_LINK_SECTION); + ASSERT_NE(nullptr, section); + EXPECT_EQ(2, g_menu_model_get_n_items(section)); /* No playlists, so two items */ + + /* Player display */ + verify_item_attribute(section, 0, "action", g_variant_new_string("indicator.player-id")); + verify_item_attribute(section, 0, "x-canonical-type", g_variant_new_string("com.canonical.unity.media-player")); + + /* Player control */ + verify_item_attribute(section, 1, "x-canonical-type", g_variant_new_string("com.canonical.unity.playback-item")); + verify_item_attribute(section, 1, "x-canonical-play-action", g_variant_new_string("indicator.play.player-id")); + verify_item_attribute(section, 1, "x-canonical-next-action", g_variant_new_string("indicator.next.player-id")); + verify_item_attribute(section, 1, "x-canonical-previous-action", g_variant_new_string("indicator.previous.player-id")); + + g_clear_object(§ion); + + sound_menu_remove_player(menu, MEDIA_PLAYER(media)); + + EXPECT_EQ(1, g_menu_model_get_n_items(G_MENU_MODEL(menu->menu))); + + g_clear_object(&media); + g_clear_object(&menu); + return; +} -- cgit v1.2.3 From 263afe9d3a2743b559e312f2bb198c8fe5b539f6 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 3 Mar 2014 15:48:51 -0600 Subject: Expect the base sets of data --- tests/media-player-user.cc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'tests') diff --git a/tests/media-player-user.cc b/tests/media-player-user.cc index 3e241d8..f9fee36 100644 --- a/tests/media-player-user.cc +++ b/tests/media-player-user.cc @@ -96,5 +96,26 @@ TEST_F(MediaPlayerUserTest, BasicObject) { MediaPlayerUser * player = media_player_user_new("user"); ASSERT_NE(nullptr, player); + /* Protected, but no useful data */ + EXPECT_FALSE(media_player_get_is_running(MEDIA_PLAYER(player))); + EXPECT_TRUE(media_player_get_can_raise(MEDIA_PLAYER(player))); + EXPECT_STREQ("user", media_player_get_id(MEDIA_PLAYER(player))); + EXPECT_STREQ("", media_player_get_name(MEDIA_PLAYER(player))); + EXPECT_STREQ("", media_player_get_state(MEDIA_PLAYER(player))); + EXPECT_EQ(nullptr, media_player_get_icon(MEDIA_PLAYER(player))); + EXPECT_EQ(nullptr, media_player_get_current_track(MEDIA_PLAYER(player))); + + /* Get the proxy -- but no good data */ + loop(100); + + /* Ensure even with the proxy we don't have anything */ + EXPECT_FALSE(media_player_get_is_running(MEDIA_PLAYER(player))); + EXPECT_TRUE(media_player_get_can_raise(MEDIA_PLAYER(player))); + EXPECT_STREQ("user", media_player_get_id(MEDIA_PLAYER(player))); + EXPECT_STREQ("", media_player_get_name(MEDIA_PLAYER(player))); + EXPECT_STREQ("", media_player_get_state(MEDIA_PLAYER(player))); + EXPECT_EQ(nullptr, media_player_get_icon(MEDIA_PLAYER(player))); + EXPECT_EQ(nullptr, media_player_get_current_track(MEDIA_PLAYER(player))); + g_clear_object(&player); } -- cgit v1.2.3 From e74821bf9bfbf22d7ddf8099fd879bd33121a353 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 3 Mar 2014 16:45:58 -0600 Subject: Add a test with real data --- tests/media-player-user.cc | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'tests') diff --git a/tests/media-player-user.cc b/tests/media-player-user.cc index f9fee36..1f8b48d 100644 --- a/tests/media-player-user.cc +++ b/tests/media-player-user.cc @@ -36,6 +36,7 @@ class MediaPlayerUserTest : public ::testing::Test GDBusConnection * session = NULL; GDBusConnection * system = NULL; + GDBusProxy * proxy = NULL; virtual void SetUp() { service = dbus_test_service_new(NULL); @@ -56,9 +57,19 @@ class MediaPlayerUserTest : public ::testing::Test 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(&service); g_object_unref(session); @@ -90,6 +101,15 @@ class MediaPlayerUserTest : public ::testing::Test g_main_loop_run(loop); g_main_loop_unref(loop); } + + void set_property (const gchar * name, GVariant * value) { + g_dbus_proxy_call_sync(proxy, + "Set", + g_variant_new("(ssv)", "com.canonical.indicator.sound.AccountsService", name, value), + G_DBUS_CALL_FLAGS_NONE, + -1, NULL, NULL + ); + } }; TEST_F(MediaPlayerUserTest, BasicObject) { @@ -119,3 +139,46 @@ TEST_F(MediaPlayerUserTest, BasicObject) { g_clear_object(&player); } + +TEST_F(MediaPlayerUserTest, DataSet) { + /* Put data into Acts */ + set_property("Timestamp", g_variant_new_uint64(g_get_monotonic_time())); + set_property("PlayerName", g_variant_new_string("The Player Formerly Known as Prince")); + GIcon * in_icon = g_themed_icon_new_with_default_fallbacks("foo-bar-fallback"); + set_property("PlayerIcon", g_variant_new_variant(g_icon_serialize(in_icon))); + set_property("State", g_variant_new_string("Chillin'")); + set_property("Title", g_variant_new_string("Dictator")); + set_property("Artist", g_variant_new_string("Bansky")); + set_property("Album", g_variant_new_string("Vinyl is dead")); + set_property("ArtUrl", g_variant_new_string("http://art.url")); + + /* Build our media player */ + MediaPlayerUser * player = media_player_user_new("user"); + ASSERT_NE(nullptr, player); + + /* Get the proxy -- and it's precious precious data -- oh, my, precious! */ + loop(100); + + /* Ensure even with the proxy we don't have anything */ + EXPECT_TRUE(media_player_get_is_running(MEDIA_PLAYER(player))); + EXPECT_TRUE(media_player_get_can_raise(MEDIA_PLAYER(player))); + EXPECT_STREQ("user", media_player_get_id(MEDIA_PLAYER(player))); + EXPECT_STREQ("The Player Formerly Known as Prince", media_player_get_name(MEDIA_PLAYER(player))); + EXPECT_STREQ("Chillin'", media_player_get_state(MEDIA_PLAYER(player))); + + GIcon * out_icon = media_player_get_icon(MEDIA_PLAYER(player)); + EXPECT_NE(nullptr, out_icon); + EXPECT_TRUE(g_icon_equal(in_icon, out_icon)); + g_clear_object(&out_icon); + + MediaPlayerTrack * track = media_player_get_current_track(MEDIA_PLAYER(player)); + EXPECT_NE(nullptr, track); + EXPECT_STREQ("Dictator", media_player_track_get_title(track)); + EXPECT_STREQ("Bansky", media_player_track_get_artist(track)); + EXPECT_STREQ("Vinyl is dead", media_player_track_get_album(track)); + EXPECT_STREQ("http://art.url", media_player_track_get_art_url(track)); + g_clear_object(&track); + + g_clear_object(&in_icon); + g_clear_object(&player); +} -- cgit v1.2.3 From 6a34818664b50a6b2e9157e6321ed663e9359b1d Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 4 Mar 2014 15:24:10 -0600 Subject: Making a test for checking that we can get in and out of running states --- tests/CMakeLists.txt | 6 ++++++ tests/accounts-service-mock.h | 15 +++++++++++++-- tests/media-player-user.cc | 42 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 53 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9049045..9b0586a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -151,4 +151,10 @@ target_link_libraries ( add_test(media-player-user-test-basic media-player-user-test --gtest_filter=MediaPlayerUserTest.BasicObject ) +add_test(media-player-user-test-dataset + media-player-user-test --gtest_filter=MediaPlayerUserTest.DataSet +) +add_test(media-player-user-test-timeout + media-player-user-test --gtest_filter=MediaPlayerUserTest.TimeoutTest +) diff --git a/tests/accounts-service-mock.h b/tests/accounts-service-mock.h index 225d7b5..d4dae7e 100644 --- a/tests/accounts-service-mock.h +++ b/tests/accounts-service-mock.h @@ -22,6 +22,8 @@ class AccountsServiceMock { DbusTestDbusMock * mock = nullptr; + DbusTestDbusMockObject * soundobj = nullptr; + DbusTestDbusMockObject * userobj = nullptr; public: AccountsServiceMock () { @@ -45,12 +47,12 @@ class AccountsServiceMock "UncacheUser", G_VARIANT_TYPE_STRING, NULL, "", NULL); - DbusTestDbusMockObject * userobj = dbus_test_dbus_mock_get_object(mock, "/user", "org.freedesktop.Accounts.User", NULL); + 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); + 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); @@ -81,10 +83,19 @@ class AccountsServiceMock } ~AccountsServiceMock () { + g_debug("Destroying the Accounts Service Mock"); g_clear_object(&mock); } operator DbusTestTask* () { return DBUS_TEST_TASK(mock); } + + operator DbusTestDbusMock* () { + return mock; + } + + DbusTestDbusMockObject * get_sound () { + return soundobj; + } }; diff --git a/tests/media-player-user.cc b/tests/media-player-user.cc index 1f8b48d..2132e14 100644 --- a/tests/media-player-user.cc +++ b/tests/media-player-user.cc @@ -32,7 +32,7 @@ class MediaPlayerUserTest : public ::testing::Test protected: DbusTestService * service = NULL; - DbusTestDbusMock * mock = NULL; + AccountsServiceMock service_mock; GDBusConnection * session = NULL; GDBusConnection * system = NULL; @@ -41,7 +41,6 @@ class MediaPlayerUserTest : public ::testing::Test 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); @@ -103,12 +102,7 @@ class MediaPlayerUserTest : public ::testing::Test } void set_property (const gchar * name, GVariant * value) { - g_dbus_proxy_call_sync(proxy, - "Set", - g_variant_new("(ssv)", "com.canonical.indicator.sound.AccountsService", name, value), - G_DBUS_CALL_FLAGS_NONE, - -1, NULL, NULL - ); + dbus_test_dbus_mock_object_update_property((DbusTestDbusMock *)service_mock, service_mock.get_sound(), name, value, NULL); } }; @@ -182,3 +176,35 @@ TEST_F(MediaPlayerUserTest, DataSet) { g_clear_object(&in_icon); g_clear_object(&player); } + +TEST_F(MediaPlayerUserTest, TimeoutTest) { + /* Put data into Acts -- but 15 minutes ago */ + set_property("Timestamp", g_variant_new_uint64(g_get_monotonic_time() - 15 * 60 * 1000 * 1000)); + set_property("PlayerName", g_variant_new_string("The Player Formerly Known as Prince")); + GIcon * in_icon = g_themed_icon_new_with_default_fallbacks("foo-bar-fallback"); + set_property("PlayerIcon", g_variant_new_variant(g_icon_serialize(in_icon))); + set_property("State", g_variant_new_string("Chillin'")); + set_property("Title", g_variant_new_string("Dictator")); + set_property("Artist", g_variant_new_string("Bansky")); + set_property("Album", g_variant_new_string("Vinyl is dead")); + set_property("ArtUrl", g_variant_new_string("http://art.url")); + + /* Build our media player */ + MediaPlayerUser * player = media_player_user_new("user"); + ASSERT_NE(nullptr, player); + + /* Get the proxy -- and the old data, so old, like forever */ + loop(100); + + /* Ensure that we show up as not running */ + EXPECT_FALSE(media_player_get_is_running(MEDIA_PLAYER(player))); + + /* Update to make running */ + set_property("Timestamp", g_variant_new_uint64(g_get_monotonic_time())); + loop(100); + + EXPECT_TRUE(media_player_get_is_running(MEDIA_PLAYER(player))); + + g_clear_object(&in_icon); + g_clear_object(&player); +} -- cgit v1.2.3