diff options
-rw-r--r-- | src/pulse-manager.c | 23 | ||||
-rw-r--r-- | src/pulse-manager.h | 3 | ||||
-rw-r--r-- | tests/mockpulse.c | 6 | ||||
-rw-r--r-- | tests/test-pulse-manager.c | 118 |
4 files changed, 101 insertions, 49 deletions
diff --git a/src/pulse-manager.c b/src/pulse-manager.c index 2f86a90..a95c887 100644 --- a/src/pulse-manager.c +++ b/src/pulse-manager.c @@ -72,6 +72,11 @@ void establish_pulse_activities(SoundServiceDbus *service) pa_context_connect(pulse_context, NULL, PA_CONTEXT_NOFAIL, NULL); } +pa_context* get_context() +{ + return pulse_context; +} + void close_pulse_activites() { if (pulse_context != NULL){ @@ -98,7 +103,11 @@ static void reconnect_to_pulse() pa_context_unref(pulse_context); pulse_context = NULL; } - g_hash_table_destroy(sink_hash); + + if(sink_hash != NULL){ + g_hash_table_destroy(sink_hash); + sink_hash = NULL; + } // reconnect pulse_context = pa_context_new(pa_glib_mainloop_get_api(pa_main_loop), "ayatana.indicator.sound"); @@ -115,7 +124,6 @@ static void destroy_sink_info(void *value) sink_info *sink = (sink_info*)value; g_free(sink->name); g_free(sink->description); - g_free(sink->icon_name); g_free(sink); } @@ -289,11 +297,9 @@ static void pulse_sink_info_callback(pa_context *c, const pa_sink_info *sink, in g_debug("About to add an item to our hash"); sink_info *value; value = g_new0(sink_info, 1); - value->index = value->device_index = sink->index; + value->index = sink->index; value->name = g_strdup(sink->name); value->description = g_strdup(sink->description); - value->icon_name = g_strdup(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_ICON_NAME)); - value->active_port = (sink->active_port != NULL); value->mute = !!sink->mute; value->volume = sink->volume; value->base_volume = sink->base_volume; @@ -363,8 +369,6 @@ static void update_sink_info(pa_context *c, const pa_sink_info *info, int eol, v sink_info *s = g_hash_table_lookup(sink_hash, GINT_TO_POINTER(info->index)); s->name = g_strdup(info->name); s->description = g_strdup(info->description); - s->icon_name = g_strdup(pa_proplist_gets(info->proplist, PA_PROP_DEVICE_ICON_NAME)); - s->active_port = (info->active_port != NULL); gboolean mute_changed = s->mute != !!info->mute; s->mute = !!info->mute; gboolean volume_changed = (pa_cvolume_equal(&info->volume, &s->volume) == 0); @@ -398,14 +402,11 @@ static void update_sink_info(pa_context *c, const pa_sink_info *info, int eol, v } else { - sink_info *value; value = g_new0(sink_info, 1); - value->index = value->device_index = info->index; + value->index = info->index; value->name = g_strdup(info->name); value->description = g_strdup(info->description); - value->icon_name = g_strdup(pa_proplist_gets(info->proplist, PA_PROP_DEVICE_ICON_NAME)); - value->active_port = (info->active_port != NULL); value->mute = !!info->mute; value->volume = info->volume; value->base_volume = info->base_volume; diff --git a/src/pulse-manager.h b/src/pulse-manager.h index e1777fb..60a7ad3 100644 --- a/src/pulse-manager.h +++ b/src/pulse-manager.h @@ -31,13 +31,10 @@ with this program. If not, see <http://www.gnu.org/licenses/>. typedef struct { gchar* name; gchar* description; - gchar* icon_name; gint index; - gint device_index; pa_cvolume volume; pa_channel_map channel_map; gboolean mute; - gboolean active_port; pa_volume_t base_volume; } sink_info; diff --git a/tests/mockpulse.c b/tests/mockpulse.c index a41de30..b868e07 100644 --- a/tests/mockpulse.c +++ b/tests/mockpulse.c @@ -73,6 +73,12 @@ struct pa_operation { int refcount; }; + +/*void pa_context_connect(pa_context* c, const char *server, pa_context_flags_t flags, const pa_spawn_api *api)*/ +/*{*/ +/* set_pa_context_get_state_result(c, PA_CONTEXT_CONNECTING);*/ +/*}*/ + /* Can be made into a list if we need multiple callbacks */ static pa_sink_info *next_sink_info; diff --git a/tests/test-pulse-manager.c b/tests/test-pulse-manager.c index 169acbd..5fff7b9 100644 --- a/tests/test-pulse-manager.c +++ b/tests/test-pulse-manager.c @@ -18,53 +18,101 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -/* we test static functions */ +#include <string.h> + +/* we intend to test static functions which are not to be exported */ #include "../src/pulse-manager.c" #include "mockpulse.h" +static pa_sink_info* mock_sink_info(); -static void test_pa_context_exit() -{ - pa_context* context = pa_context_new(NULL, "foo"); - pa_context_set_state_callback(context, context_state_callback, NULL); - // => call context_state_callback(context, NULL); - // pa_context_get_state is mocked to return the right FAIL flag. - set_pa_context_get_state_result(context, PA_CONTEXT_FAILED); - context_state_callback(context, NULL); - // 1. test to make sure relevant variables are tidied up - // XXX: Conor to do. - // 2. then using the same pa_context_get_state we could ensure the manager - // is attempting to reconnect to PA. - pa_context_unref(context); -} -static void test_sink_insert() + +/*static void test_pa_context_exit()*/ +/*{*/ +/* pa_context* context = pa_context_new(NULL, "foo");*/ + +/* pa_context_set_state_callback(context, context_state_callback, NULL);*/ +/* // => call context_state_callback(context, NULL);*/ +/* // pa_context_get_state is mocked to return the right FAIL flag.*/ +/* set_pa_context_get_state_result(context, PA_CONTEXT_FAILED);*/ +/* context_state_callback(context, NULL);*/ +/* // Test to ensure context is tidied after failure.*/ +/* g_assert(context == NULL);*/ +/* // 2. then using the same pa_context_get_state we could ensure the manager*/ +/* // is attempting to reconnect to PA. */ +/* //g_assert(PA_CONTEXT_CONNECTING == pa_context_get_state(get_context()));*/ +/* //pa_context_unref(context);*/ +/*}*/ + +static void test_sink_update { - sink_info *sink_details; - pa_context* context = pa_context_new(NULL, "foo"); - sink_details = g_new0(sink_info, 1); - sink_details->index = 8; - sink_details->name = "mock_sink"; - sink_details->description = "mock description"; - sink_details->mute = FALSE; - pa_cvolume volume; // nearly full volume: - pa_cvolume_set(&volume, 1, 30000); - sink_details->volume = volume; - pa_sink_info *expected = g_new0(pa_sink_info, 1); - expected->name = g_strdup("foo"); - expected->index = 8; - expected->description = g_strdup("more details"); +/* pa_sink_info *expected = g_new0(pa_sink_info, 1);*/ +/* expected->name = g_strdup("foo");*/ +/* expected->index = 8;*/ +/* expected->description = g_strdup("more details");*/ // fill it out here more. // hook into our pa_context_get_sink_info_by_index to pass exppected to // update_sink_info - set_pa_context_get_sink_info(expected); + // set_pa_context_get_sink_info(expected); // update_sink_info is a static method in pulse-manager.c ? - pa_context_get_sink_info_by_index(context, sink_details->index, update_sink_info, NULL); +/* pa_context_get_sink_info_by_index(context, sink_details->index, update_sink_info, NULL);*/ // the mockinkg lib should then return this mocked up sink_info to the // method update_sink_info which tests could be wrote against to make sure // everthing is populated correctly. +/* pa_context_unref(context);*/ +/* g_free(expected);*/ + + +} + +static void test_sink_cache_population() +{ + pa_context* context = pa_context_new(NULL, "foo"); + + pa_sink_info* mock_sink = mock_sink_info(); + + set_pa_context_get_sink_info(mock_sink); + + sink_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, destroy_sink_info); + + pa_context_get_sink_info_by_index(context, mock_sink->index, pulse_sink_info_callback, NULL); + + GList *keys = g_hash_table_get_keys(sink_hash); + gint position = g_list_index(keys, GINT_TO_POINTER(mock_sink->index)); + + g_assert(position >= 0); + + sink_info* stored_sink_info = g_hash_table_lookup(sink_hash, GINT_TO_POINTER(mock_sink->index)); + + g_assert(g_ascii_strncasecmp(mock_sink->name, stored_sink_info->name, strlen(mock_sink->name)) == 0); + g_assert(g_ascii_strncasecmp(mock_sink->description, stored_sink_info->description, strlen(mock_sink->description)) == 0); + g_assert(!!mock_sink->mute == stored_sink_info->mute); + g_assert(mock_sink->index == stored_sink_info->index); + g_assert(pa_cvolume_equal(&mock_sink->volume, &stored_sink_info->volume)); + + g_free(mock_sink); + g_hash_table_destroy(sink_hash); pa_context_unref(context); - g_free(expected); +} + +/** +Helper Methods +**/ + +static pa_sink_info* +mock_sink_info() +{ + pa_sink_info* mock_sink; + mock_sink = g_new0(pa_sink_info, 1); + mock_sink->index = 8; + mock_sink->name = g_strdup("mock_sink"); + mock_sink->description = g_strdup("mock description"); + mock_sink->mute = 0; + pa_cvolume volume; // nearly full volume: + pa_cvolume_set(&volume, 1, 30000); + mock_sink->volume = volume; + return mock_sink; } @@ -73,8 +121,8 @@ gint main (gint argc, gchar * argv[]) g_type_init(); g_test_init(&argc, &argv, NULL); - g_test_add_func("/indicator-sound/pulse-manager/sink-insert", test_sink_insert); - g_test_add_func("/indicator-sound/pulse-manager/pa-context-exit", test_pa_context_exit); + g_test_add_func("/indicator-sound/pulse-manager/sink-cache-population", test_sink_cache_population); + //g_test_add_func("/indicator-sound/pulse-manager/pa-context-exit", test_pa_context_exit); return g_test_run (); } |