/* * Copyright 2013 (c) Anna Schumaker. */ #include #include #include static unsigned int load_count = 0; static unsigned int state_count = 0; static int pause_count = 0; static unsigned int test_wait_state(void) { GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(test_audio_pipeline())); g_usleep(G_USEC_PER_SEC / 15); while (gst_bus_have_pending(bus)) test_main_loop(); gst_object_unref(bus); return state_count; } static void test_send_error() { GError *error = g_error_new(1, G_FILE_ERROR_BADF, "Simulated Error"); test_audio_error(error, "Fake error for testing"); g_error_free(error); } static void test_audio_load(struct track *track) { load_count++; } static void test_change_state(GstState state) { state_count++; } static void test_config_pause(int n) { pause_count = n; } static struct audio_callbacks test_audio_cb = { .audio_cb_load = test_audio_load, .audio_cb_state_change = test_change_state, .audio_cb_config_pause = test_config_pause, }; static void test_init() { g_assert_null(test_audio_pipeline()); g_assert_null(audio_cur_track()); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_NULL); g_assert_null(audio_next()); core_init(NULL, NULL, NULL, &test_audio_cb, IDLE_SYNC); test_loop_init(); g_assert_false(audio_load(NULL)); g_assert_null(audio_next()); g_assert_null(audio_prev()); test_send_error(); g_assert_false(audio_play()); g_assert_false(audio_pause()); g_assert_false(audio_seek(7)); g_assert_cmpuint(audio_position(), ==, 0); g_assert_cmpuint(audio_duration(), ==, 0); g_assert_cmpuint(audio_get_volume(), ==, 100); g_assert_null(audio_cur_track()); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_NULL); g_assert_cmpuint(load_count, ==, 0); g_assert_cmpuint(state_count, ==, 0); playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony"); while (idle_run_task()) {}; g_assert_null(audio_cur_track()); g_assert_nonnull(test_audio_pipeline()); } static void test_playback() { struct track *tracks[3] = { track_get(0), NULL, track_get(0) }; unsigned int i; for (i = 0; i < 3; i++) { if (i == 0) g_assert_true(audio_load(tracks[i])); else g_assert_false(audio_load(tracks[i])); g_assert_cmpuint(playlist_size(playlist_lookup(PL_SYSTEM, "History")), ==, 1); g_assert_cmpuint(load_count, ==, 1); g_assert_cmpuint(test_wait_state(), ==, 1); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING); g_assert(audio_cur_track() == tracks[0]); g_assert_cmpuint(audio_duration(), ==, tracks[0]->tr_length * GST_SECOND); } audio_set_volume(0); g_assert_cmpfloat(audio_get_volume(), ==, 0); audio_set_volume(50); g_assert_cmpfloat(audio_get_volume(), ==, 50); audio_set_volume(150); g_assert_cmpfloat(audio_get_volume(), ==, 100); g_assert_true(audio_pause()); g_assert_false(audio_pause()); g_assert_cmpuint(test_wait_state(), ==, 2); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED); g_assert_true(audio_seek(5 * GST_SECOND)); g_assert_cmpuint(test_wait_state(), ==, 3); g_assert_cmpuint(audio_position(), ==, 5 * GST_SECOND); g_assert_true(audio_seek(42 * GST_SECOND)); g_assert_cmpuint(test_wait_state(), ==, 4); g_assert_cmpuint(audio_position(), ==, 42 * GST_SECOND); g_assert_true(audio_play()); g_assert_false(audio_play()); g_assert_cmpuint(test_wait_state(), ==, 5); g_assert_true(audio_pause()); g_assert_cmpuint(test_wait_state(), ==, 6); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED); /* Check duration again now that track is fully loaded. */ g_assert_cmpuint(audio_duration(), ==, track_get(0)->tr_length * GST_SECOND); } static void test_next() { struct playlist *history = playlist_lookup(PL_SYSTEM, "History"); int i; state_count = 0; /* First, let's test getting tracks from a temporary queue. */ playlist_add(playlist_lookup(PL_SYSTEM, "Queued Tracks"), track_get(2)); playlist_add(playlist_lookup(PL_SYSTEM, "Queued Tracks"), track_get(1)); playlist_add(playlist_lookup(PL_SYSTEM, "Queued Tracks"), track_get(0)); for (i = 2; i >= 0; i--) { g_assert_cmpuint(playlist_size(playlist_lookup(PL_SYSTEM, "Queued Tracks")), ==, i + 1); if (i > 0) g_assert_cmpuint(audio_next()->tr_track, ==, track_get(i)->tr_track); else /* Simulate an error. */ test_send_error(); g_assert(playlist_at(history, 0) == track_get(i)); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING); g_assert(audio_cur_track() == track_get(i)); } g_assert_cmpuint(test_wait_state(), ==, 3); g_assert_cmpuint(playlist_size(playlist_lookup(PL_SYSTEM, "Queued Tracks")), ==, 0); /* Tracks should now be picked from the collection. */ playlist_select(playlist_lookup(PL_SYSTEM, "Collection")); for (i = 1; i <= 3; i++) { if (i < 3) g_assert_cmpuint(audio_next()->tr_track, ==, i); else /* Simulate an error. */ test_send_error(); g_assert_cmpuint(playlist_at(history, 0)->tr_track, ==, i); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING); g_assert_cmpuint(audio_cur_track()->tr_track, ==, i); } g_assert_cmpuint(test_wait_state(), ==, 6); } static void test_prev() { struct playlist *history = playlist_lookup(PL_SYSTEM, "History"); struct track *track = playlist_at(history, 0); state_count = 0; g_assert_cmpuint(audio_prev()->tr_track, ==, 2); g_assert(playlist_at(history, 0) == track); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING); g_assert_cmpuint(audio_cur_track()->tr_track, ==, 2); g_assert_cmpuint(test_wait_state(), ==, 1); g_assert_cmpuint(audio_prev()->tr_track, ==, 1); g_assert(playlist_at(history, 0) == track); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING); g_assert_cmpuint(audio_cur_track()->tr_track, ==, 1); g_assert_cmpuint(test_wait_state(), ==, 2); g_assert_true(audio_pause()); g_assert_cmpuint(audio_prev()->tr_track, ==, track_get(0)->tr_track); g_assert(playlist_at(history, 0) == track); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING); g_assert_cmpuint(audio_cur_track()->tr_track, ==, track_get(0)->tr_track); g_assert_cmpuint(test_wait_state(), ==, 4); g_assert_cmpuint(audio_prev()->tr_track, ==, track_get(1)->tr_track); g_assert(playlist_at(history, 0) == track); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING); g_assert_cmpuint(audio_cur_track()->tr_track, ==, track_get(1)->tr_track); g_assert_cmpuint(test_wait_state(), ==, 5); g_assert_cmpuint(audio_prev()->tr_track, ==, track_get(2)->tr_track); g_assert(playlist_at(history, 0) == track); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING); g_assert_cmpuint(audio_cur_track()->tr_track, ==, track_get(2)->tr_track); g_assert_cmpuint(test_wait_state(), ==, 6); } void test_autopause() { struct playlist *history = playlist_lookup(PL_SYSTEM, "History"); int i; audio_pause_after(3); g_assert_cmpuint(pause_count, ==, 3); pause_count = 0; audio_pause_after(3); g_assert_cmpuint(pause_count, ==, 0); audio_pause_after(5); g_assert_cmpuint(pause_count, ==, 5); state_count = 0; for (i = 4; i > -1; i--) { test_audio_eos(); g_assert_cmpuint(pause_count, ==, i); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING); g_assert(playlist_at(history, 0) == audio_cur_track()); } g_assert_cmpuint(test_wait_state(), ==, 5); test_audio_eos(); while (idle_run_task()) {} g_assert_cmpint(pause_count, ==, -1); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED); g_assert_cmpuint(test_wait_state(), ==, 6); test_send_error(); g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED); } void test_filepath() { struct playlist *history = playlist_lookup(PL_SYSTEM, "History"); const gchar *path = "tests/Music/Hyrule Symphony/01 - Title Theme.ogg"; const gchar *path2 = "tests/Music/Ocarina of Time/01 - Title Theme.ogg"; struct track *track = track_lookup(path); load_count = 0; playlist_generic_clear(history); g_assert_false(audio_load_filepath(NULL)); g_assert_false(audio_load_filepath("tests/Music/00 - No Track.ogg")); g_assert_cmpuint(playlist_size(history), ==, 0); g_assert_cmpuint(load_count, ==, 0); g_assert_true(audio_load_filepath(path)); g_assert_cmpuint(load_count, ==, 1); g_assert(audio_cur_track() == track); g_assert_cmpuint(playlist_size(history), ==, 1); g_assert_true(playlist_has(history, audio_cur_track())); g_assert_true(audio_load_filepath(path2)); g_assert_cmpuint(load_count, ==, 2); g_assert(audio_cur_track() != track); g_assert_null(audio_cur_track()->tr_library); g_assert_cmpuint(playlist_size(history), ==, 1); g_assert_false(playlist_has(history, audio_cur_track())); g_assert_true(audio_load_filepath(path)); g_assert_cmpuint(load_count, ==, 3); g_assert(audio_cur_track() == track); g_assert_cmpuint(playlist_size(history), ==, 2); g_assert_true(playlist_has(history, audio_cur_track())); } static void test_deinit() { core_deinit(); test_loop_deinit(); g_assert_null(audio_cur_track()); g_assert_null(test_audio_pipeline()); } int main(int argc, char **argv) { g_test_init(&argc, &argv, NULL); g_test_add_func("/Core/Audio/Initialization", test_init); g_test_add_func("/Core/Audio/Playback", test_playback); g_test_add_func("/Core/Audio/Next", test_next); g_test_add_func("/Core/Audio/Previous", test_prev); g_test_add_func("/Core/Audio/Automatic Pausing", test_autopause); g_test_add_func("/Core/Audio/Filepath", test_filepath); g_test_add_func("/Core/Audio/Deinitialization", test_deinit); return g_test_run(); }