ocarina/tests/audio/audio.cpp

174 lines
4.0 KiB
C++

/*
* Copyright 2013 (c) Anna Schumaker.
*/
#include <audio.h>
#include <deck.h>
#include <library.h>
#include <print.h>
#include <stdlib.h>
#include <string>
#include <unistd.h>
void gen_library()
{
system("tests/library/gen_library.sh");
print("\n");
}
void check_ret(const std :: string &test, bool ret, bool expected)
{
print("Test %s: ", test.c_str());
if (ret == expected)
print("Success!\n");
else
print("Failed.\n");
}
void check_ret(const std :: string &test, long ret, long expected)
{
print("Test %s: ", test.c_str());
if (ret == expected)
print("Success!\n");
else
print("Failed (Expected %ld but got %ld)\n", expected, ret);
}
/* Call various functions without a track loaded */
void test_0()
{
check_ret("0a", audio :: play(), false);
check_ret("0b", audio :: pause(), false);
check_ret("0c", audio :: seek_to(10), false);
check_ret("0d", audio :: next(), false);
check_ret("0e", audio :: stop(), false);
check_ret("0f", audio :: position(), 0);
check_ret("0g", audio :: duration(), 0);
try {
print("Test 0h: ");
audio :: current_trackid();
print("Failed.\n");
} catch (int error) {
print("Success!\n");
}
print("\n");
}
void test_1()
{
library :: Song song;
library :: lookup(0, &song);
check_ret("1a", audio :: next(), true);
check_ret("1b", audio :: play(), true);
check_ret("1c", audio :: pause(), true);
check_ret("1d", audio :: seek_to(1 * GST_SECOND), true);
check_ret("1e", audio :: stop(), true);
check_ret("1f", audio :: current_trackid() == 0, true);
check_ret("1g", audio :: position(), 0);
print("\n");
}
/* Test pause_after() */
unsigned int test_2_count = 0;
int test_2_cb(gpointer data)
{
long seek_pos, pos, max;
library :: Song song;
GMainLoop *loop = (GMainLoop *)data;
library :: lookup(audio :: current_trackid(), &song);
pos = audio :: position();
switch (test_2_count) {
case 0:
break;
case 1:
check_ret("2g", audio :: duration(), song.track->length * GST_SECOND);
seek_pos = (song.track->length * GST_SECOND) - GST_SECOND;
check_ret("2h", audio :: seek_to(seek_pos), true);
break;
case 2:
max = (song.track->length * GST_SECOND) - GST_SECOND + (501 * GST_MSECOND);
check_ret("2i", pos <= max, true);
check_ret("2j", audio :: stop(), true);
break;
case 3:
check_ret("2k", pos, 0);
check_ret("2l", audio :: play(), true);
check_ret("2m", audio :: seek_to(audio :: duration() - 1), true);
break;
case 4:
check_ret("2n", audio :: pause_count(), (long)2);
check_ret("2o", audio :: seek_to(audio :: duration() - 1), true);
break;
case 5:
check_ret("2p", audio :: pause_count(), (long)1);
check_ret("2q", audio :: seek_to(audio :: duration() - 1), true);
break;
case 6:
check_ret("2r", audio :: pause_count(), (long)0);
check_ret("2s", audio :: seek_to(audio :: duration() - 1), true);
break;
case 7:
check_ret("2t", audio :: pause_enabled(), false);
check_ret("2u", audio :: pause_count(), (long)0);
break;
case 8:
pos = audio :: position();
check_ret("2v", (0 <= pos) && (pos <= GST_MSECOND), true);
break;
case 9:
pos = audio :: position();
check_ret("2w", (0 <= pos) && (pos <= GST_MSECOND), true);
default:
g_main_quit(loop);
}
test_2_count++;
return true;
}
void test_2()
{
GMainLoop *loop;
check_ret("2a", audio :: pause_enabled(), false);
check_ret("2b", audio :: pause_count(), (long)0);
audio :: pause_after(true, 3);
check_ret("2c", audio :: pause_enabled(), true);
check_ret("2d", audio :: pause_count(), (long)3);
audio :: next();
check_ret("2e", audio :: pause_enabled(), true);
check_ret("2f", audio :: pause_count(), (long)3);
audio :: play();
loop = g_main_loop_new(NULL, FALSE);
g_timeout_add(500, test_2_cb, loop);
g_main_loop_run(loop);
}
int main(int argc, char **argv)
{
Playlist *plist;
gen_library();
/* Initialize before testing */
audio :: init(&argc, &argv);
test_0();
/* Read in library, set up a playlist */
library :: add_path("/tmp/library/0");
plist = deck :: create();
for (unsigned int i = 0; i < 150; i++)
plist->add(i);
test_1();
test_2();
//audio :: quit();
return 0;
}