A GTK+ / GStreamer based music player
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

260 lines
7.8 KiB

/*
* Copyright 2013 (c) Anna Schumaker.
*/
#include <core/audio.h>
#include <core/idle.h>
#include <core/playlist.h>
#include <core/core.h>
#include <tests/test.h>
static unsigned int load_count = 0;
static unsigned int state_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(test_audio_player()),
error, "Fake error for testing");
audio_error(message);
gst_message_unref(message);
g_error_free(error);
}
static void test_audio_load(struct track *track) { load_count++; }
static void test_change_state(GstState state) { state_count++; }
static void test_config_pause(int n) { pause_count = n; }
static struct audio_ops test_audio_ops = {
test_audio_load,
test_change_state,
test_config_pause,
};
static struct core_init_data test_init_data = {
.audio_ops = &test_audio_ops,
};
static void test_init()
{
g_assert_null(test_audio_player());
g_assert_null(audio_cur_track());
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_NULL);
g_assert_null(audio_next());
core_init(NULL, NULL, &test_init_data);
g_assert_false(audio_load(NULL));
g_assert_null(audio_next());
g_assert_null(audio_prev());
test_send_error();
g_assert_false(audio_play());
g_assert_false(audio_pause());
g_assert_false(audio_seek(7));
g_assert_cmpuint(audio_position(), ==, 0);
g_assert_cmpuint(audio_duration(), ==, 0);
g_assert_cmpuint(audio_get_volume(), ==, 100);
g_assert_null(audio_cur_track());
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_NULL);
g_assert_cmpuint(load_count, ==, 0);
g_assert_cmpuint(state_count, ==, 0);
playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony");
while (idle_run_task()) {};
g_assert_null(audio_cur_track());
g_assert_nonnull(test_audio_player());
}
static void test_playback()
{
struct track *tracks[3] = { track_get(0), NULL, track_get(0) };
unsigned int i;
for (i = 0; i < 3; i++) {
if (i == 0)
g_assert_true(audio_load(tracks[i]));
else
g_assert_false(audio_load(tracks[i]));
g_assert_cmpuint(playlist_size(PL_SYSTEM, "History"), ==, 1);
g_assert_cmpuint(load_count, ==, 1);
g_assert_cmpuint(state_count, ==, 1);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert(audio_cur_track() == tracks[0]);
g_assert_cmpuint(audio_duration(), ==, tracks[0]->tr_length * GST_SECOND);
}
audio_set_volume(0);
g_assert_cmpfloat(audio_get_volume(), ==, 0);
audio_set_volume(50);
g_assert_cmpfloat(audio_get_volume(), ==, 50);
audio_set_volume(150);
g_assert_cmpfloat(audio_get_volume(), ==, 100);
g_assert_true(audio_pause());
g_assert_false(audio_pause());
g_assert_cmpuint(state_count, ==, 2);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED);
g_assert_true(test_audio_seek(5 * GST_SECOND));
g_assert_cmpuint(audio_position(), ==, 5 * GST_SECOND);
g_assert_true(test_audio_seek(42 * GST_SECOND));
g_assert_cmpuint(audio_position(), ==, 42 * GST_SECOND);
g_assert_true(audio_play());
g_assert_false(audio_play());
g_assert_cmpuint(state_count, ==, 3);
g_assert_true(audio_pause());
g_assert_cmpuint(state_count, ==, 4);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED);
/* Check duration again now that track is fully loaded. */
g_assert_cmpuint(audio_duration(), ==, track_get(0)->tr_length * GST_SECOND);
}
static void test_next()
{
struct queue *history_q = playlist_get_queue(PL_SYSTEM, "History");
int i;
state_count = 0;
/* First, let's test getting tracks from a temporary queue. */
playlist_add(PL_SYSTEM, "Queued Tracks", track_get(2));
playlist_add(PL_SYSTEM, "Queued Tracks", track_get(1));
playlist_add(PL_SYSTEM, "Queued Tracks", track_get(0));
for (i = 2; i >= 0; i--) {
g_assert_cmpuint(playlist_size(PL_SYSTEM, "Queued Tracks"), ==, i + 1);
if (i > 0)
g_assert_cmpuint(audio_next()->tr_track, ==, track_get(i)->tr_track);
else /* Simulate an error. */
test_send_error();
g_assert(queue_at(history_q, 0) == track_get(i));
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert(audio_cur_track() == track_get(i));
}
g_assert_cmpuint(state_count, ==, 3);
g_assert_cmpuint(playlist_size(PL_SYSTEM, "Queued Tracks"), ==, 0);
/* Tracks should now be picked from the collection. */
playlist_select(PL_SYSTEM, "Collection");
for (i = 1; i <= 3; i++) {
if (i < 3)
g_assert_cmpuint(audio_next()->tr_track, ==, i);
else /* Simulate an error. */
test_send_error();
g_assert_cmpuint(queue_at(history_q, 0)->tr_track, ==, i);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert_cmpuint(audio_cur_track()->tr_track, ==, i);
}
g_assert_cmpuint(state_count, ==, 6);
}
static void test_prev()
{
struct queue *history_q = playlist_get_queue(PL_SYSTEM, "History");
struct track *track = queue_at(history_q, 0);
state_count = 0;
g_assert_cmpuint(audio_prev()->tr_track, ==, 2);
g_assert(queue_at(history_q, 0) == track);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert_cmpuint(audio_cur_track()->tr_track, ==, 2);
g_assert_cmpuint(state_count, ==, 1);
g_assert_cmpuint(audio_prev()->tr_track, ==, 1);
g_assert(queue_at(history_q, 0) == track);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert_cmpuint(audio_cur_track()->tr_track, ==, 1);
g_assert_cmpuint(state_count, ==, 2);
g_assert_true(audio_pause());
g_assert_cmpuint(audio_prev()->tr_track, ==, track_get(0)->tr_track);
g_assert(queue_at(history_q, 0) == track);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert_cmpuint(audio_cur_track()->tr_track, ==, track_get(0)->tr_track);
g_assert_cmpuint(state_count, ==, 4);
g_assert_cmpuint(audio_prev()->tr_track, ==, track_get(1)->tr_track);
g_assert(queue_at(history_q, 0) == track);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert_cmpuint(audio_cur_track()->tr_track, ==, track_get(1)->tr_track);
g_assert_cmpuint(state_count, ==, 5);
g_assert_cmpuint(audio_prev()->tr_track, ==, track_get(2)->tr_track);
g_assert(queue_at(history_q, 0) == track);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert_cmpuint(audio_cur_track()->tr_track, ==, track_get(2)->tr_track);
g_assert_cmpuint(state_count, ==, 6);
}
void test_autopause()
{
struct queue *history_q = playlist_get_queue(PL_SYSTEM, "History");
int i;
audio_pause_after(3);
g_assert_cmpuint(pause_count, ==, 3);
pause_count = 0;
audio_pause_after(3);
g_assert_cmpuint(pause_count, ==, 0);
audio_pause_after(5);
g_assert_cmpuint(pause_count, ==, 5);
state_count = 0;
for (i = 4; i > -1; i--) {
audio_eos();
g_assert_cmpuint(pause_count, ==, i);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert(queue_at(history_q, 0) == audio_cur_track());
}
g_assert_cmpuint(state_count, ==, 5);
audio_eos();
while (idle_run_task()) {}
g_assert_cmpint(pause_count, ==, -1);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED);
g_assert_cmpuint(state_count, ==, 6);
test_send_error();
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED);
}
static void test_deinit()
{
core_deinit();
g_assert_null(audio_cur_track());
g_assert_null(test_audio_player());
}
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/Core/Audio/Initialization", test_init);
g_test_add_func("/Core/Audio/Playback", test_playback);
g_test_add_func("/Core/Audio/Next", test_next);
g_test_add_func("/Core/Audio/Previous", test_prev);
g_test_add_func("/Core/Audio/Automatic Pausing", test_autopause);
g_test_add_func("/Core/Audio/Deinitialization", test_deinit);
return g_test_run();
}