2014-06-01 17:52:49 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
2015-12-15 08:16:06 -05:00
|
|
|
#include <core/core.h>
|
2015-12-17 10:32:01 -05:00
|
|
|
#include <tests/test.h>
|
2017-04-05 16:52:23 -04:00
|
|
|
#include <tests/loop.h>
|
2014-06-01 17:52:49 -04:00
|
|
|
|
2015-12-16 12:50:14 -05:00
|
|
|
static unsigned int load_count = 0;
|
2015-12-18 08:09:34 -05:00
|
|
|
static unsigned int state_count = 0;
|
2015-12-16 12:50:14 -05:00
|
|
|
static int pause_count = 0;
|
2014-12-19 14:37:47 -05:00
|
|
|
|
|
|
|
|
2017-04-05 16:52:23 -04:00
|
|
|
static unsigned int test_wait_state(void)
|
2015-12-15 15:50:02 -05:00
|
|
|
{
|
2017-04-05 16:52:23 -04:00
|
|
|
GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(test_audio_pipeline()));
|
2015-12-15 15:50:02 -05:00
|
|
|
|
2017-04-05 16:52:23 -04:00
|
|
|
g_usleep(G_USEC_PER_SEC / 15);
|
|
|
|
while (gst_bus_have_pending(bus))
|
|
|
|
test_main_loop();
|
|
|
|
|
|
|
|
gst_object_unref(bus);
|
|
|
|
return state_count;
|
2015-12-15 15:50:02 -05:00
|
|
|
}
|
|
|
|
|
2015-12-17 09:17:25 -05:00
|
|
|
static void test_send_error()
|
|
|
|
{
|
2017-04-06 08:13:30 -04:00
|
|
|
GError *error = g_error_new(1, G_FILE_ERROR_BADF, "Simulated Error");
|
|
|
|
test_audio_error(error, "Fake error for testing");
|
2015-12-17 09:17:25 -05:00
|
|
|
g_error_free(error);
|
|
|
|
}
|
|
|
|
|
2015-12-18 08:09:34 -05:00
|
|
|
static void test_audio_load(struct track *track) { load_count++; }
|
|
|
|
static void test_change_state(GstState state) { state_count++; }
|
2015-12-16 12:50:14 -05:00
|
|
|
static void test_config_pause(int n) { pause_count = n; }
|
|
|
|
|
2016-09-24 10:29:22 -04:00
|
|
|
static struct audio_callbacks test_audio_cb = {
|
|
|
|
.audio_cb_load = test_audio_load,
|
|
|
|
.audio_cb_state_change = test_change_state,
|
|
|
|
.audio_cb_config_pause = test_config_pause,
|
2015-12-15 10:23:15 -05:00
|
|
|
};
|
2014-12-19 14:37:47 -05:00
|
|
|
|
2015-12-15 08:16:06 -05:00
|
|
|
|
|
|
|
static void test_init()
|
|
|
|
{
|
2017-04-05 09:13:37 -04:00
|
|
|
g_assert_null(test_audio_pipeline());
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_null(audio_cur_track());
|
|
|
|
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_NULL);
|
|
|
|
g_assert_null(audio_next());
|
2015-12-15 08:16:06 -05:00
|
|
|
|
2016-09-24 10:53:25 -04:00
|
|
|
core_init(NULL, NULL, NULL, &test_audio_cb, IDLE_SYNC);
|
2017-04-05 16:52:23 -04:00
|
|
|
test_loop_init();
|
2015-12-15 08:16:06 -05:00
|
|
|
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_false(audio_load(NULL));
|
|
|
|
g_assert_null(audio_next());
|
|
|
|
g_assert_null(audio_prev());
|
2015-12-17 09:17:25 -05:00
|
|
|
test_send_error();
|
2016-03-16 10:34:49 -04:00
|
|
|
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);
|
2016-08-18 09:18:13 -04:00
|
|
|
g_assert_cmpuint(audio_get_volume(), ==, 100);
|
2016-03-16 10:34:49 -04:00
|
|
|
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);
|
2015-12-15 10:23:15 -05:00
|
|
|
|
2016-05-11 11:13:42 -04:00
|
|
|
playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony");
|
2015-12-15 10:23:15 -05:00
|
|
|
while (idle_run_task()) {};
|
|
|
|
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_null(audio_cur_track());
|
2017-04-05 09:13:37 -04:00
|
|
|
g_assert_nonnull(test_audio_pipeline());
|
2015-12-15 10:46:48 -05:00
|
|
|
}
|
2015-12-15 08:16:06 -05:00
|
|
|
|
2015-12-15 10:23:15 -05:00
|
|
|
static void test_playback()
|
|
|
|
{
|
2015-12-18 08:34:42 -05:00
|
|
|
struct track *tracks[3] = { track_get(0), NULL, track_get(0) };
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
2016-03-16 10:34:49 -04:00
|
|
|
if (i == 0)
|
|
|
|
g_assert_true(audio_load(tracks[i]));
|
|
|
|
else
|
|
|
|
g_assert_false(audio_load(tracks[i]));
|
2016-09-12 13:49:27 -04:00
|
|
|
g_assert_cmpuint(playlist_size(playlist_lookup(PL_SYSTEM, "History")), ==, 1);
|
2017-04-05 16:52:23 -04:00
|
|
|
g_assert_cmpuint(load_count, ==, 1);
|
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 1);
|
2016-03-16 10:34:49 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-08-18 09:18:13 -04:00
|
|
|
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);
|
|
|
|
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_true(audio_pause());
|
|
|
|
g_assert_false(audio_pause());
|
2017-04-05 16:52:23 -04:00
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 2);
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED);
|
|
|
|
|
2017-04-05 16:52:23 -04:00
|
|
|
g_assert_true(audio_seek(5 * GST_SECOND));
|
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 3);
|
|
|
|
g_assert_cmpuint(audio_position(), ==, 5 * GST_SECOND);
|
|
|
|
g_assert_true(audio_seek(42 * GST_SECOND));
|
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 4);
|
|
|
|
g_assert_cmpuint(audio_position(), ==, 42 * GST_SECOND);
|
2016-03-16 10:34:49 -04:00
|
|
|
|
|
|
|
g_assert_true(audio_play());
|
|
|
|
g_assert_false(audio_play());
|
2017-04-05 16:52:23 -04:00
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 5);
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_true(audio_pause());
|
2017-04-05 16:52:23 -04:00
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 6);
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED);
|
2015-12-16 08:00:19 -05:00
|
|
|
|
|
|
|
/* Check duration again now that track is fully loaded. */
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(audio_duration(), ==, track_get(0)->tr_length * GST_SECOND);
|
2015-12-15 10:23:15 -05:00
|
|
|
}
|
|
|
|
|
2015-12-16 11:23:18 -05:00
|
|
|
static void test_next()
|
|
|
|
{
|
2016-09-21 16:04:52 -04:00
|
|
|
struct playlist *history = playlist_lookup(PL_SYSTEM, "History");
|
2015-12-16 11:23:18 -05:00
|
|
|
int i;
|
|
|
|
|
2015-12-18 08:09:34 -05:00
|
|
|
state_count = 0;
|
2015-12-16 11:23:18 -05:00
|
|
|
/* First, let's test getting tracks from a temporary queue. */
|
2016-09-12 13:49:27 -04:00
|
|
|
playlist_add(playlist_lookup(PL_SYSTEM, "Queued Tracks"), track_get(2));
|
|
|
|
playlist_add(playlist_lookup(PL_SYSTEM, "Queued Tracks"), track_get(1));
|
|
|
|
playlist_add(playlist_lookup(PL_SYSTEM, "Queued Tracks"), track_get(0));
|
2015-12-16 11:23:18 -05:00
|
|
|
|
|
|
|
for (i = 2; i >= 0; i--) {
|
2016-09-12 13:49:27 -04:00
|
|
|
g_assert_cmpuint(playlist_size(playlist_lookup(PL_SYSTEM, "Queued Tracks")), ==, i + 1);
|
2016-03-16 10:34:49 -04:00
|
|
|
if (i > 0)
|
|
|
|
g_assert_cmpuint(audio_next()->tr_track, ==, track_get(i)->tr_track);
|
|
|
|
else /* Simulate an error. */
|
2015-12-17 09:17:25 -05:00
|
|
|
test_send_error();
|
2016-09-21 16:04:52 -04:00
|
|
|
g_assert(playlist_at(history, 0) == track_get(i));
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
|
|
|
|
g_assert(audio_cur_track() == track_get(i));
|
|
|
|
}
|
2017-04-05 16:52:23 -04:00
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 3);
|
2016-09-12 13:49:27 -04:00
|
|
|
g_assert_cmpuint(playlist_size(playlist_lookup(PL_SYSTEM, "Queued Tracks")), ==, 0);
|
2015-12-16 11:23:18 -05:00
|
|
|
|
|
|
|
/* Tracks should now be picked from the collection. */
|
2016-09-12 13:08:24 -04:00
|
|
|
playlist_select(playlist_lookup(PL_SYSTEM, "Collection"));
|
2015-12-16 11:23:18 -05:00
|
|
|
for (i = 1; i <= 3; i++) {
|
2016-03-16 10:34:49 -04:00
|
|
|
if (i < 3)
|
|
|
|
g_assert_cmpuint(audio_next()->tr_track, ==, i);
|
|
|
|
else /* Simulate an error. */
|
2015-12-17 09:17:25 -05:00
|
|
|
test_send_error();
|
2016-09-21 16:04:52 -04:00
|
|
|
g_assert_cmpuint(playlist_at(history, 0)->tr_track, ==, i);
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
|
|
|
|
g_assert_cmpuint(audio_cur_track()->tr_track, ==, i);
|
|
|
|
}
|
2017-04-05 16:52:23 -04:00
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 6);
|
2015-12-16 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
2015-12-16 12:21:59 -05:00
|
|
|
static void test_prev()
|
2015-12-15 10:46:48 -05:00
|
|
|
{
|
2016-09-21 16:04:52 -04:00
|
|
|
struct playlist *history = playlist_lookup(PL_SYSTEM, "History");
|
|
|
|
struct track *track = playlist_at(history, 0);
|
2014-06-01 17:52:49 -04:00
|
|
|
|
2015-12-18 08:09:34 -05:00
|
|
|
state_count = 0;
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(audio_prev()->tr_track, ==, 2);
|
2016-09-21 16:04:52 -04:00
|
|
|
g_assert(playlist_at(history, 0) == track);
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
|
|
|
|
g_assert_cmpuint(audio_cur_track()->tr_track, ==, 2);
|
2017-04-05 16:52:23 -04:00
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 1);
|
2016-03-16 10:34:49 -04:00
|
|
|
|
|
|
|
g_assert_cmpuint(audio_prev()->tr_track, ==, 1);
|
2016-09-21 16:04:52 -04:00
|
|
|
g_assert(playlist_at(history, 0) == track);
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
|
|
|
|
g_assert_cmpuint(audio_cur_track()->tr_track, ==, 1);
|
2017-04-05 16:52:23 -04:00
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 2);
|
2016-03-16 10:34:49 -04:00
|
|
|
|
|
|
|
g_assert_true(audio_pause());
|
|
|
|
g_assert_cmpuint(audio_prev()->tr_track, ==, track_get(0)->tr_track);
|
2016-09-21 16:04:52 -04:00
|
|
|
g_assert(playlist_at(history, 0) == track);
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
|
|
|
|
g_assert_cmpuint(audio_cur_track()->tr_track, ==, track_get(0)->tr_track);
|
2017-04-05 16:52:23 -04:00
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 4);
|
2016-03-16 10:34:49 -04:00
|
|
|
|
|
|
|
g_assert_cmpuint(audio_prev()->tr_track, ==, track_get(1)->tr_track);
|
2016-09-21 16:04:52 -04:00
|
|
|
g_assert(playlist_at(history, 0) == track);
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
|
|
|
|
g_assert_cmpuint(audio_cur_track()->tr_track, ==, track_get(1)->tr_track);
|
2017-04-05 16:52:23 -04:00
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 5);
|
2016-03-16 10:34:49 -04:00
|
|
|
|
|
|
|
g_assert_cmpuint(audio_prev()->tr_track, ==, track_get(2)->tr_track);
|
2016-09-21 16:04:52 -04:00
|
|
|
g_assert(playlist_at(history, 0) == track);
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
|
|
|
|
g_assert_cmpuint(audio_cur_track()->tr_track, ==, track_get(2)->tr_track);
|
2017-04-05 16:52:23 -04:00
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 6);
|
2014-06-01 17:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_autopause()
|
|
|
|
{
|
2016-09-21 16:04:52 -04:00
|
|
|
struct playlist *history = playlist_lookup(PL_SYSTEM, "History");
|
2015-12-16 12:50:14 -05:00
|
|
|
int i;
|
2014-06-01 17:52:49 -04:00
|
|
|
|
2015-12-16 12:50:14 -05:00
|
|
|
audio_pause_after(3);
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(pause_count, ==, 3);
|
2014-06-01 17:52:49 -04:00
|
|
|
|
2015-12-16 12:50:14 -05:00
|
|
|
pause_count = 0;
|
|
|
|
audio_pause_after(3);
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(pause_count, ==, 0);
|
2014-06-01 17:52:49 -04:00
|
|
|
|
2015-12-16 12:50:14 -05:00
|
|
|
audio_pause_after(5);
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(pause_count, ==, 5);
|
2014-06-01 17:52:49 -04:00
|
|
|
|
2015-12-18 08:09:34 -05:00
|
|
|
state_count = 0;
|
2015-12-16 12:50:14 -05:00
|
|
|
for (i = 4; i > -1; i--) {
|
2017-04-06 08:13:30 -04:00
|
|
|
test_audio_eos();
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(pause_count, ==, i);
|
|
|
|
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
|
2016-09-21 16:04:52 -04:00
|
|
|
g_assert(playlist_at(history, 0) == audio_cur_track());
|
2016-03-16 10:34:49 -04:00
|
|
|
}
|
2017-04-05 16:52:23 -04:00
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 5);
|
2014-06-01 17:52:49 -04:00
|
|
|
|
2017-04-06 08:13:30 -04:00
|
|
|
test_audio_eos();
|
2016-09-29 12:43:27 -04:00
|
|
|
while (idle_run_task()) {}
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpint(pause_count, ==, -1);
|
|
|
|
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED);
|
2017-04-05 16:52:23 -04:00
|
|
|
g_assert_cmpuint(test_wait_state(), ==, 6);
|
2015-12-18 08:34:42 -05:00
|
|
|
|
|
|
|
test_send_error();
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED);
|
2015-12-16 12:50:14 -05:00
|
|
|
}
|
|
|
|
|
2017-04-21 11:08:06 -04:00
|
|
|
void test_filepath()
|
|
|
|
{
|
|
|
|
struct playlist *history = playlist_lookup(PL_SYSTEM, "History");
|
|
|
|
const gchar *path = "tests/Music/Hyrule Symphony/01 - Title Theme.ogg";
|
|
|
|
const gchar *path2 = "tests/Music/Ocarina of Time/01 - Title Theme.ogg";
|
|
|
|
struct track *track = track_lookup(path);
|
|
|
|
|
|
|
|
load_count = 0;
|
|
|
|
playlist_generic_clear(history);
|
|
|
|
|
|
|
|
g_assert_false(audio_load_filepath(NULL));
|
|
|
|
g_assert_false(audio_load_filepath("tests/Music/00 - No Track.ogg"));
|
|
|
|
g_assert_cmpuint(playlist_size(history), ==, 0);
|
|
|
|
g_assert_cmpuint(load_count, ==, 0);
|
|
|
|
|
|
|
|
g_assert_true(audio_load_filepath(path));
|
|
|
|
g_assert_cmpuint(load_count, ==, 1);
|
|
|
|
g_assert(audio_cur_track() == track);
|
|
|
|
g_assert_cmpuint(playlist_size(history), ==, 1);
|
|
|
|
g_assert_true(playlist_has(history, audio_cur_track()));
|
|
|
|
|
|
|
|
g_assert_true(audio_load_filepath(path2));
|
|
|
|
g_assert_cmpuint(load_count, ==, 2);
|
|
|
|
g_assert(audio_cur_track() != track);
|
|
|
|
g_assert_null(audio_cur_track()->tr_library);
|
|
|
|
g_assert_cmpuint(playlist_size(history), ==, 1);
|
|
|
|
g_assert_false(playlist_has(history, audio_cur_track()));
|
|
|
|
|
|
|
|
g_assert_true(audio_load_filepath(path));
|
|
|
|
g_assert_cmpuint(load_count, ==, 3);
|
|
|
|
g_assert(audio_cur_track() == track);
|
|
|
|
g_assert_cmpuint(playlist_size(history), ==, 2);
|
|
|
|
g_assert_true(playlist_has(history, audio_cur_track()));
|
|
|
|
}
|
|
|
|
|
2015-12-16 12:50:14 -05:00
|
|
|
static void test_deinit()
|
|
|
|
{
|
2015-12-17 10:06:40 -05:00
|
|
|
core_deinit();
|
2017-04-05 16:52:23 -04:00
|
|
|
test_loop_deinit();
|
2016-03-16 10:34:49 -04:00
|
|
|
g_assert_null(audio_cur_track());
|
2017-04-05 09:13:37 -04:00
|
|
|
g_assert_null(test_audio_pipeline());
|
2014-06-01 17:52:49 -04:00
|
|
|
}
|
|
|
|
|
2015-12-16 12:50:14 -05:00
|
|
|
|
2016-03-16 10:34:49 -04:00
|
|
|
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);
|
2017-04-21 11:08:06 -04:00
|
|
|
g_test_add_func("/Core/Audio/Filepath", test_filepath);
|
2016-03-16 10:34:49 -04:00
|
|
|
g_test_add_func("/Core/Audio/Deinitialization", test_deinit);
|
|
|
|
return g_test_run();
|
|
|
|
}
|