ocarina/tests/core/audio.cpp

289 lines
7.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/tempq.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 = 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_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_deinit()
{
core :: deinit();
test_equal(audio_cur_track(), NULL);
test_equal(audio_get_player(), NULL);
}
void test_pre_init()
{
TestDriver *driver = (TestDriver *)audio :: get_driver();
test_equal(audio_cur_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);
test_equal(audio_duration(), (long)0);
audio_next();
test_equal(audio_cur_track(), TRACK_NULL);
audio :: prev();
test_equal(audio_cur_track(), TRACK_NULL);
}
void test_init2()
{
struct track *track;
test_cp_data_dir();
audio_init(NULL, NULL, &test_audio_ops);
track = audio_cur_track();
test_equal(track, TRACK_NULL);
core :: init(NULL, NULL, &test_init_data);
track = audio_cur_track();
test_not_equal(track, TRACK_NULL);
}
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_cur_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_cur_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_cur_track();
driver->eos();
test_not_equal(audio_cur_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 Next", test_next),
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 Automatic Pausing", test_autopause),
);