From e16e9b8bfcb2c47e4a440e2c4c1e9f0223dcf59e Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Sat, 14 Feb 2009 11:45:27 +0000 Subject: [PATCH] [tests] Add conformance tests for ClutterModel ClutterModel has an interactive test but lacks a conformance unit for automatic testing. This is the beginning of that unit, which covers the population and iteration over a ListModel. --- tests/conform/Makefile.am | 1 + tests/conform/test-conform-main.c | 3 + tests/conform/test-model.c | 211 ++++++++++++++++++++++++++++++ 3 files changed, 215 insertions(+) create mode 100644 tests/conform/test-model.c diff --git a/tests/conform/Makefile.am b/tests/conform/Makefile.am index 340f0d852..cc54053d2 100644 --- a/tests/conform/Makefile.am +++ b/tests/conform/Makefile.am @@ -27,6 +27,7 @@ test_conformance_SOURCES = \ test-text-cache.c \ test-anchors.c \ test-npot-texture.c \ + test-model.c \ $(NULL) # For convenience, this provides a way to easily run individual unit tests: diff --git a/tests/conform/test-conform-main.c b/tests/conform/test-conform-main.c index ff3c839f6..718b2f8ca 100644 --- a/tests/conform/test-conform-main.c +++ b/tests/conform/test-conform-main.c @@ -129,5 +129,8 @@ main (int argc, char **argv) TEST_CONFORM_SIMPLE ("/actor", test_anchors); + TEST_CONFORM_SIMPLE ("/model", test_list_model_populate); + TEST_CONFORM_SIMPLE ("/model", test_list_model_iterate); + return g_test_run (); } diff --git a/tests/conform/test-model.c b/tests/conform/test-model.c new file mode 100644 index 000000000..ef7a29b0c --- /dev/null +++ b/tests/conform/test-model.c @@ -0,0 +1,211 @@ +#include +#include +#include + +#include "test-conform-common.h" + +typedef struct _ModelData +{ + ClutterModel *model; + + guint n_row; +} ModelData; + +enum +{ + COLUMN_FOO, /* G_TYPE_STRING */ + COLUMN_BAR, /* G_TYPE_INT */ + + N_COLUMNS +}; + +static const struct { + const gchar *expected_foo; + gint expected_bar; +} base_model[] = { + { "String 1", 1 }, + { "String 2", 2 }, + { "String 3", 3 }, + { "String 4", 4 }, + { "String 5", 5 }, + { "String 6", 6 }, + { "String 7", 7 }, + { "String 8", 8 }, + { "String 9", 9 }, +}; + +static const struct { + const gchar *expected_foo; + gint expected_bar; +} forward_base[] = { + { "String 1", 1 }, + { "String 2", 2 }, + { "String 3", 3 }, + { "String 4", 4 }, + { "String 5", 5 }, + { "String 6", 6 }, + { "String 7", 7 }, + { "String 8", 8 }, + { "String 9", 9 }, +}; + +static const struct { + const gchar *expected_foo; + gint expected_bar; +} backward_base[] = { + { "String 9", 9 }, + { "String 8", 8 }, + { "String 7", 7 }, + { "String 6", 6 }, + { "String 5", 5 }, + { "String 4", 4 }, + { "String 3", 3 }, + { "String 2", 2 }, + { "String 1", 1 }, +}; + +static inline void +compare_iter (ClutterModelIter *iter, + const gchar *expected_foo, + const gint expected_bar) +{ + gchar *foo = NULL; + gint bar = 0; + + clutter_model_iter_get (iter, + COLUMN_FOO, &foo, + COLUMN_BAR, &bar, + -1); + + if (g_test_verbose ()) + g_print ("Row %d: Got [ '%s', '%d' ], expected [ '%s', '%d' ]\n", + clutter_model_iter_get_row (iter), + foo, bar, + expected_foo, expected_bar); + + g_assert_cmpstr (foo, ==, expected_foo); + g_assert_cmpint (bar, ==, expected_bar); + + g_free (foo); +} + +static void +on_row_added (ClutterModel *model, + ClutterModelIter *iter, + gpointer data) +{ + ModelData *model_data = data; + + compare_iter (iter, + base_model[model_data->n_row].expected_foo, + base_model[model_data->n_row].expected_bar); + + model_data->n_row += 1; +} + +void +test_list_model_iterate (TestConformSimpleFixture *fixture, + gconstpointer data) +{ + ModelData test_data = { NULL, 0 }; + ClutterModelIter *iter; + gint i; + + test_data.model = clutter_list_model_new (N_COLUMNS, + G_TYPE_STRING, "Foo", + G_TYPE_INT, "Bar"); + test_data.n_row = 0; + + g_signal_connect (test_data.model, "row-added", + G_CALLBACK (on_row_added), + &test_data); + + for (i = 1; i < 10; i++) + { + gchar *foo = g_strdup_printf ("String %d", i); + + clutter_model_append (test_data.model, + COLUMN_FOO, foo, + COLUMN_BAR, i, + -1); + + g_free (foo); + } + + if (g_test_verbose ()) + g_print ("Forward iteration...\n"); + + iter = clutter_model_get_first_iter (test_data.model); + g_assert (iter != NULL); + + i = 0; + while (!clutter_model_iter_is_last (iter)) + { + compare_iter (iter, + forward_base[i].expected_foo, + forward_base[i].expected_bar); + + iter = clutter_model_iter_next (iter); + i += 1; + } + + g_object_unref (iter); + + if (g_test_verbose ()) + g_print ("Backward iteration...\n"); + + iter = clutter_model_get_last_iter (test_data.model); + g_assert (iter != NULL); + + i = 0; + + do + { + compare_iter (iter, + backward_base[i].expected_foo, + backward_base[i].expected_bar); + + iter = clutter_model_iter_prev (iter); + i += 1; + } + while (!clutter_model_iter_is_first (iter)); + + compare_iter (iter, + backward_base[i].expected_foo, + backward_base[i].expected_bar); + + g_object_unref (iter); + + g_object_unref (test_data.model); +} + +void +test_list_model_populate (TestConformSimpleFixture *fixture, + gconstpointer data) +{ + ModelData test_data = { NULL, 0 }; + gint i; + + test_data.model = clutter_list_model_new (N_COLUMNS, + G_TYPE_STRING, "Foo", + G_TYPE_INT, "Bar"); + test_data.n_row = 0; + + g_signal_connect (test_data.model, "row-added", + G_CALLBACK (on_row_added), + &test_data); + + for (i = 1; i < 10; i++) + { + gchar *foo = g_strdup_printf ("String %d", i); + + clutter_model_append (test_data.model, + COLUMN_FOO, foo, + COLUMN_BAR, i, + -1); + + g_free (foo); + } + + g_object_unref (test_data.model); +}