ocarina/tests/core/audio.cpp

269 lines
6.1 KiB
C++
Raw Normal View History

/*
* Copyright 2013 (c) Anna Schumaker.
*/
#include <core/audio.h>
extern "C" {
#include <core/collection.h>
#include <core/history.h>
#include <core/idle.h>
}
#include <core/core.h>
#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;
do {
gst_element_get_state(audio_get_player(), &state, NULL,
GST_CLOCK_TIME_NONE);
} while (state != GST_STATE_PAUSED && state != GST_STATE_PLAYING);
return ret;
}
class TestDriver : public AudioDriver
{
public:
bool playing;
int64_t cur_duration;
std::string cur_file;
TestDriver() : playing(false), cur_duration(0) {}
~TestDriver() {};
bool is_playing() { return playing; }
int64_t duration() { return cur_duration; }
} 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 :: current_track(), NULL);
test_equal(audio_get_player(), NULL);
core :: init(NULL, NULL, &test_init_data);
test_equal(audio_load(NULL), false);
test_equal(audio_play(), false);
test_equal(audio_pause(), false);
test_equal(audio_seek(7), false);
test_equal(audio_position(), 0);
test_equal(audio :: current_track(), NULL);
test_equal(load_count, 0);
collection_add("tests/Music/Hyrule Symphony");
while (idle_run_task()) {};
test_equal(audio :: current_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 :: current_track(), track_get(0));
test_equal(queue_size(history_get_queue()), 1);
test_equal(audio_load(NULL), false);
test_equal(load_count, 1);
test_equal(audio :: current_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_pause(), true);
test_equal(audio_pause(), false);
test_equal(test_audio_seek(5 * GST_SECOND), true);
test_equal(audio_position(), 5 * GST_SECOND);
test_equal(test_audio_seek(0), true);
test_equal(audio_position(), 0);
}
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(4242);
test_equal(audio_position(), (long)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, &test_audio_ops);
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();
driver->playing = audio_play();
test_equal(driver->playing, true);
driver->playing = audio_pause();
test_equal(driver->playing, false);
audio_seek(4242);
test_equal(audio_position(), (long)4242);
audio_play();
audio :: stop();
test_equal(driver->playing, false);
audio_seek(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);
driver->playing = 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);
test_not_equal(audio :: current_track(), track);
track = track_get(0);
audio_seek(4242);
audio_load(track);
test_equal(driver->is_playing(), true);
test_equal(audio_position(), (long)0);
audio_seek(4242);
audio_load(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 Playback", test_playback),
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),
);