1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include <glib.h>
#include "../src/celluloid-playlist-model.h"
#include "../src/celluloid-playlist-item.h"
#include <stdio.h>
#define TEST_DATA \
{ {"Foo", "file:///foo.webm", 123456}, \
{"Bar", "file:///bar.webm", 12345}, \
{"Baz", "file:///baz.webm", 1234}, \
{NULL, NULL, 0} }
struct ItemData
{
const gchar *title;
const gchar *uri;
gdouble duration;
};
static CelluloidPlaylistModel *
add_test_data(CelluloidPlaylistModel *model)
{
const struct ItemData data[] = TEST_DATA;
const guint initial_n_items =
g_list_model_get_n_items(G_LIST_MODEL(model));
for(guint i = 0; data[i].title; i++)
{
gchar *title = g_strdup(data[i].title);
gchar *uri = g_strdup(data[i].uri);
CelluloidPlaylistItem *item =
celluloid_playlist_item_new_take
(title, uri, data[i].duration, FALSE);
celluloid_playlist_model_append(model, item);
const guint n_items =
g_list_model_get_n_items(G_LIST_MODEL(model));
g_assert_cmpuint(n_items, ==, initial_n_items + i + 1);
}
return model;
}
static void
test_append(void)
{
const struct ItemData data[] = TEST_DATA;
CelluloidPlaylistModel *model = celluloid_playlist_model_new();
g_assert_true(g_list_model_get_n_items(G_LIST_MODEL(model)) == 0);
add_test_data(model);
const guint n_items = g_list_model_get_n_items(G_LIST_MODEL(model));
for(guint i = 0; i < n_items; i++)
{
CelluloidPlaylistItem *item =
g_list_model_get_item(G_LIST_MODEL(model), i);
const gchar *title =
celluloid_playlist_item_get_title(item);
const gchar *uri =
celluloid_playlist_item_get_uri(item);
const gdouble duration =
celluloid_playlist_item_get_duration(item);
g_assert_cmpstr(title, ==, data[i].title);
g_assert_cmpstr(uri, ==, data[i].uri);
g_assert_cmpfloat(duration, ==, data[i].duration);
}
g_object_unref(model);
}
static void
test_clear(void)
{
CelluloidPlaylistModel *model = celluloid_playlist_model_new();
guint n_items = 0;
add_test_data(model);
n_items = g_list_model_get_n_items(G_LIST_MODEL(model));
g_assert_cmpuint(n_items, ==, 3);
celluloid_playlist_model_clear(model);
n_items = g_list_model_get_n_items(G_LIST_MODEL(model));
g_assert_cmpuint(n_items, ==, 0);
// Check if the clear function works correctly if the model is already
// empty.
celluloid_playlist_model_clear(model);
n_items = g_list_model_get_n_items(G_LIST_MODEL(model));
g_assert_cmpuint(n_items, ==, 0);
g_object_unref(model);
}
static void
test_set_current(void)
{
CelluloidPlaylistModel *model = celluloid_playlist_model_new();
gint current = -1;
add_test_data(model);
current = celluloid_playlist_model_get_current(model);
g_assert_cmpint(current, <, 0);
celluloid_playlist_model_set_current(model, 1);
current = celluloid_playlist_model_get_current(model);
g_assert_cmpint(current, ==, 1);
celluloid_playlist_model_set_current(model, 10);
current = celluloid_playlist_model_get_current(model);
g_assert_cmpint(current, ==, 2);
celluloid_playlist_model_set_current(model, -10);
current = celluloid_playlist_model_get_current(model);
g_assert_cmpint(current, <, 0);
g_object_unref(model);
}
static void
handle_contents_changed( CelluloidPlaylistModel *model,
guint position,
guint removed,
guint added,
gpointer data )
{
guint *counter = data;
*counter += 1;
}
static void
handle_items_changed( CelluloidPlaylistModel *model,
guint position,
guint removed,
guint added,
gpointer data )
{
guint *counter = data;
*counter += 1;
}
static void
test_signals(void)
{
CelluloidPlaylistModel *model = celluloid_playlist_model_new();
guint contents_changed_counter = 0;
guint items_changed_counter = 0;
g_signal_connect( model,
"contents-changed",
G_CALLBACK(handle_contents_changed),
&contents_changed_counter );
g_signal_connect( model,
"items-changed",
G_CALLBACK(handle_items_changed),
&items_changed_counter );
add_test_data(model);
g_assert_cmpuint(contents_changed_counter, ==, 3);
g_assert_cmpuint(items_changed_counter, ==, 3);
celluloid_playlist_model_set_current(model, 2);
g_assert_cmpuint(contents_changed_counter, ==, 3);
g_assert_cmpuint(items_changed_counter, ==, 4);
celluloid_playlist_model_remove(model, 1);
g_assert_cmpuint(contents_changed_counter, ==, 4);
g_assert_cmpuint(items_changed_counter, ==, 5);
g_object_unref(model);
}
int
main(gint argc, gchar **argv)
{
g_test_init(&argc, &argv, NULL);
g_test_set_nonfatal_assertions();
g_test_add_func("/test-append", test_append);
g_test_add_func("/test-clear", test_clear);
g_test_add_func("/test-set-current", test_set_current);
g_test_add_func("/test-signals", test_signals);
return g_test_run();
}
|