ocarina/tests/core/audio.cpp

253 lines
6.6 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"
static unsigned int load_count = 0;
static int pause_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;
}
static void test_send_error()
{
GstMessage *message;
GError *error;
error = g_error_new(1, G_FILE_ERROR_BADF, "Simulated Error");
message = gst_message_new_error(GST_OBJECT(audio_get_player()),
error, "Error generated for testing");
audio_error(message);
gst_message_unref(message);
g_error_free(error);
}
static void test_audio_load(struct track *track, GstState state)
{
load_count++;
}
static void test_config_pause(int n) { pause_count = n; }
static struct audio_ops test_audio_ops = {
test_audio_load,
test_config_pause,
};
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_send_error();
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);
if (i > 0) {
test_loop_equal(audio_next()->tr_dbe.dbe_index, i, i);
} else /* Simulate an error. */
test_send_error();
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);
/* Tracks should now be picked from the collection. */
test_equal(audio_play(), true);
for (i = 1; i <= 3; i++) {
if (i < 3) {
test_loop_equal(audio_next()->tr_track, i, i);
} else /* Simulate an error. */
test_send_error();
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);
}
void test_autopause()
{
struct queue *history_q = history_get_queue();
int i;
test_equal(audio_play(), true);
test_equal(pause_count, 0);
audio_pause_after(3);
test_equal(pause_count, 3);
pause_count = 0;
audio_pause_after(3);
test_equal(pause_count, 0);
audio_pause_after(5);
test_equal(pause_count, 5);
for (i = 4; i > -1; i--) {
audio_eos();
test_loop_equal(pause_count, i, i);
test_loop_equal(audio_cur_state(), GST_STATE_PLAYING, i);
test_loop_equal(queue_at(history_q, 0), audio_cur_track(), i);
} test_loop_passed();
audio_eos();
test_equal(pause_count, -1);
test_not_equal(audio_cur_state(), GST_STATE_PLAYING);
}
static void test_deinit()
{
core_deinit();
test_equal(audio_cur_track(), NULL);
test_equal(audio_get_player(), NULL);
}
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 Automatic Pausing", test_autopause),
UNIT_TEST("Audio Deinitialization", test_deinit),
);