diff options
-rw-r--r-- | tests/CMakeLists.txt | 9 | ||||
-rw-r--r-- | tests/accounts-service-user.cc | 107 |
2 files changed, 104 insertions, 12 deletions
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<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) { @@ -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); |