/* * Copyright 2013 (c) Anna Schumaker. */ #include extern "C" { #include #include #include #include } #include #include "test.h" struct track *TRACK_NULL = NULL; static unsigned int load_count = 0; static bool test_audio_seek(gint64 pos) { bool ret = audio_seek(pos); GstState state = audio_cur_state(); while (state != GST_STATE_PAUSED && state != GST_STATE_PLAYING) state = audio_cur_state(); return ret; } class TestDriver : public AudioDriver { public: bool playing; std::string cur_file; TestDriver() : playing(false) {} ~TestDriver() {}; bool is_playing() { return playing; } } driver; static void test_audio_load(struct track *track, GstState state) { load_count++; } static struct audio_ops test_audio_ops = { test_audio_load, }; static struct core_init_data test_init_data = { NULL, NULL, NULL, NULL, &test_audio_ops, }; static void test_init() { test_equal(audio_get_player(), NULL); test_equal(audio_cur_track(), NULL); test_equal(audio_cur_state(), GST_STATE_NULL); test_equal(audio_next(), NULL); core :: init(NULL, NULL, &test_init_data); test_equal(audio_load(NULL), false); test_equal(audio_next(), NULL); test_equal(audio_prev(), NULL); test_equal(audio_play(), false); test_equal(audio_pause(), false); test_equal(audio_stop(), false); test_equal(audio_seek(7), false); test_equal(audio_position(), 0); test_equal(audio_duration(), 0); test_equal(audio_cur_track(), NULL); test_equal(audio_cur_state(), GST_STATE_NULL); test_equal(load_count, 0); collection_add("tests/Music/Hyrule Symphony"); while (idle_run_task()) {}; test_equal(audio_cur_track(), NULL); test_not_equal(audio_get_player(), NULL); } static void test_playback() { test_equal(audio_load(track_get(0)), true); test_equal(load_count, 1); test_equal(audio_cur_track(), track_get(0)); test_equal(audio_cur_state(), GST_STATE_NULL); test_equal(queue_size(history_get_queue()), 1); test_equal(audio_duration(), track_get(0)->tr_length * GST_SECOND); test_equal(audio_load(NULL), false); test_equal(load_count, 1); test_equal(audio_cur_track(), track_get(0)); test_equal(queue_size(history_get_queue()), 1); test_equal(audio_load(track_get(0)), false); test_equal(load_count, 1); test_equal(queue_size(history_get_queue()), 1); test_equal(audio_play(), true); test_equal(audio_play(), false); test_equal(audio_cur_state(), GST_STATE_PLAYING); test_equal(audio_pause(), true); test_equal(audio_pause(), false); test_equal(audio_cur_state(), GST_STATE_PAUSED); test_equal(test_audio_seek(5 * GST_SECOND), true); test_equal(audio_position(), 5 * GST_SECOND); test_equal(audio_stop(), true); test_equal(audio_position(), 0); test_equal(audio_cur_state(), GST_STATE_PAUSED); test_equal(test_audio_seek(42 * GST_SECOND), true); test_equal(audio_position(), 42 * GST_SECOND); test_equal(audio_play(), true); test_equal(audio_stop(), true); test_equal(audio_cur_state(), GST_STATE_PAUSED); test_equal(audio_position(), 0); /* Check duration again now that track is fully loaded. */ test_equal(audio_duration(), track_get(0)->tr_length * GST_SECOND); } static void test_next() { struct queue *history_q = history_get_queue(); struct queue *temp_q = tempq_alloc(NULL, 0); int i; /* First, let's test getting tracks from a temporary queue. */ queue_add(temp_q, track_get(2)); queue_add(temp_q, track_get(1)); queue_add(temp_q, track_get(0)); for (i = 2; i >= 0; i--) { test_loop_equal(queue_size(temp_q), i + 1, i); test_loop_equal(audio_next()->tr_dbe.dbe_index, i, i); test_loop_equal(queue_at(history_q, 0), track_get(i), i); test_loop_equal(audio_cur_state(), GST_STATE_PAUSED, i); test_loop_equal(audio_cur_track(), track_get(i), i); } test_loop_passed(); test_equal(tempq_get(0), NULL); test_equal(audio_play(), true); /* Tracks should now be picked from the collection. */ for (i = 1; i <= 3; i++) { test_loop_equal(audio_next()->tr_track, i, i); test_loop_equal(queue_at(history_q, 0)->tr_track, i, i); test_loop_equal(audio_cur_state(), GST_STATE_PLAYING, i); test_loop_equal(audio_cur_track()->tr_track, i, i); } test_loop_passed(); } static void test_prev() { struct queue *history_q = history_get_queue(); struct track *track = queue_at(history_q, 0); test_equal(audio_prev()->tr_track, 2); test_equal(queue_at(history_q, 0), track); test_equal(audio_cur_state(), GST_STATE_PLAYING); test_equal(audio_cur_track()->tr_track, 2); test_equal(audio_prev()->tr_track, 1); test_equal(queue_at(history_q, 0), track); test_equal(audio_cur_state(), GST_STATE_PLAYING); test_equal(audio_cur_track()->tr_track, 1); test_equal(audio_pause(), true); test_equal(audio_prev()->tr_dbe.dbe_index, 0); test_equal(queue_at(history_q, 0), track); test_equal(audio_cur_state(), GST_STATE_PAUSED); test_equal(audio_cur_track()->tr_dbe.dbe_index, 0); test_equal(audio_prev()->tr_dbe.dbe_index, 1); test_equal(queue_at(history_q, 0), track); test_equal(audio_cur_state(), GST_STATE_PAUSED); test_equal(audio_cur_track()->tr_dbe.dbe_index, 1); test_equal(audio_prev()->tr_dbe.dbe_index, 2); test_equal(queue_at(history_q, 0), track); test_equal(audio_cur_state(), GST_STATE_PAUSED); test_equal(audio_cur_track()->tr_dbe.dbe_index, 2); } static void test_deinit() { core :: deinit(); test_equal(audio_cur_track(), NULL); test_equal(audio_get_player(), NULL); } void test_autopause() { TestDriver *driver = (TestDriver *)audio :: get_driver(); core :: init(NULL, NULL, &test_init_data); audio_play(); test_equal(audio :: pause_enabled(), false); test_equal(audio :: pause_count(), (unsigned)0); audio :: pause_after(true, 3); test_equal(audio :: pause_enabled(), true); test_equal(audio :: pause_count(), (unsigned)3); audio :: pause_after(false, 3); test_equal(audio :: pause_enabled(), false); test_equal(audio :: pause_count(), (unsigned)3); audio :: pause_after(false, 5); test_equal(audio :: pause_enabled(), true); test_equal(audio :: pause_count(), (unsigned)5); for (int i = 4; i >= 0; i--) { driver->eos(); test_equal(audio :: pause_enabled(), true); test_equal(audio :: pause_count(), (unsigned)i); test_equal(driver->is_playing(), true); } driver->eos(); test_equal(audio :: pause_enabled(), false); test_equal(audio :: pause_count(), (unsigned)0); test_equal(driver->is_playing(), false); } DECLARE_UNIT_TESTS( UNIT_TEST("Audio Initialization", test_init), UNIT_TEST("Audio Playback", test_playback), UNIT_TEST("Audio Next", test_next), UNIT_TEST("Audio Previous", test_prev), UNIT_TEST("Audio Deinitialization", test_deinit), UNIT_TEST("Test Audio Automatic Pausing", test_autopause), );