/* * Copyright 2013 (c) Anna Schumaker. */ #include extern "C" { #include } #include #include "test.h" struct track *TRACK_NULL = NULL; class TestDriver : public AudioDriver { public: bool playing; int64_t cur_pos; int64_t cur_duration; std::string cur_file; TestDriver() : playing(false), cur_pos(0), cur_duration(0) {} ~TestDriver() {}; void load(struct track *track) { cur_file = track_path(track); playing = false; cur_pos = 0; } void play() { playing = true; } void pause() { playing = false; } bool is_playing() { return playing; } void seek_to(int64_t offset) { cur_pos = offset; } int64_t position() { return cur_pos; } int64_t duration() { return cur_duration; } } driver; static struct core_init_data test_init_data = { NULL, NULL, NULL, NULL, }; static void test_init() { test_equal(audio :: current_track(), NULL); test_equal(audio_get_player(), NULL); core :: init(NULL, NULL, &test_init_data); test_equal(audio :: current_track(), NULL); test_not_equal(audio_get_player(), NULL); } static void test_deinit() { core :: deinit(); test_equal(audio :: current_track(), NULL); test_equal(audio_get_player(), NULL); } void test_pre_init() { TestDriver *driver = (TestDriver *)audio :: get_driver(); test_equal(audio :: current_track(), TRACK_NULL); audio :: play(); test_equal(driver->playing, false); driver->playing = true; audio :: pause(); test_equal(driver->playing, true); audio :: stop(); test_equal(driver->playing, true); driver->playing = false; audio :: seek_to(4242); test_equal(driver->position(), (long)0); driver->cur_pos = 4242; test_equal(audio :: position(), (long)0); driver->cur_pos = 0; driver->cur_duration = 4242; test_equal(audio :: duration(), (long)0); driver->cur_duration = 0; audio :: next(); test_equal(audio :: current_track(), TRACK_NULL); audio :: prev(); test_equal(audio :: current_track(), TRACK_NULL); } void test_init2() { struct track *track; test_cp_data_dir(); audio_init(NULL, NULL); track = audio :: current_track(); test_equal(track, TRACK_NULL); core :: init(NULL, NULL, &test_init_data); track = audio :: current_track(); test_not_equal(track, TRACK_NULL); } void test_playback_controls() { TestDriver *driver = (TestDriver *)audio :: get_driver(); audio :: play(); test_equal(driver->playing, true); audio :: pause(); test_equal(driver->playing, false); audio :: seek_to(4242); test_equal(driver->cur_pos, (long)4242); test_equal(audio :: position(), (long)4242); audio :: play(); audio :: stop(); test_equal(driver->playing, false); test_equal(driver->cur_pos, (long)0); audio :: seek_to(4242); driver->cur_duration = 424242; test_equal(audio :: position(), (long)4242); test_equal(audio :: duration(), (long)424242); } void test_track_controls() { struct track *track = NULL; TestDriver *driver = (TestDriver *)audio :: get_driver(); queue_unset_flag(collection_get_queue(), Q_RANDOM); audio :: pause(); audio :: next(); test_not_equal(audio :: current_track()->tr_dbe.dbe_index, (unsigned)2); test_equal(driver->is_playing(), false); audio :: play(); audio :: next(); test_equal(driver->is_playing(), true); audio :: load_track(track); test_not_equal(audio :: current_track(), track); track = track_get(0); audio :: seek_to(4242); audio :: load_track(track); test_equal(driver->is_playing(), true); test_equal(audio :: position(), (long)0); audio :: seek_to(4242); audio :: load_track(track); test_equal(driver->is_playing(), true); test_equal(audio :: position(), (long)4242); track = audio :: current_track(); driver->eos(); test_not_equal(audio :: current_track(), track); } void test_autopause() { TestDriver *driver = (TestDriver *)audio :: get_driver(); 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 Deinitialization", test_deinit), UNIT_TEST("Test Audio Pre-Init", test_pre_init), UNIT_TEST("Test Audio Init 2", test_init2), UNIT_TEST("Test Audio Playback Controls", test_playback_controls), UNIT_TEST("Test Audio Automatic Pausing", test_autopause), );