mirror of
https://github.com/brl/mutter.git
synced 2024-11-26 18:11:05 -05:00
[tests] Exercise the Model filtering
Add a test unit that exercises the ClutterModel iteration API when there is a filter in place.
This commit is contained in:
parent
01d172293c
commit
f3e33bd25a
@ -139,6 +139,7 @@ main (int argc, char **argv)
|
||||
|
||||
TEST_CONFORM_SIMPLE ("/model", test_list_model_populate);
|
||||
TEST_CONFORM_SIMPLE ("/model", test_list_model_iterate);
|
||||
TEST_CONFORM_SIMPLE ("/model", test_list_model_filter);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
@ -64,25 +64,50 @@ static const struct {
|
||||
{ "String 1", 1 },
|
||||
};
|
||||
|
||||
static const struct {
|
||||
const gchar *expected_foo;
|
||||
gint expected_bar;
|
||||
} filter_odd[] = {
|
||||
{ "String 1", 1 },
|
||||
{ "String 3", 3 },
|
||||
{ "String 5", 5 },
|
||||
{ "String 7", 7 },
|
||||
{ "String 9", 9 },
|
||||
};
|
||||
|
||||
static const struct {
|
||||
const gchar *expected_foo;
|
||||
gint expected_bar;
|
||||
} filter_even[] = {
|
||||
{ "String 8", 8 },
|
||||
{ "String 6", 6 },
|
||||
{ "String 4", 4 },
|
||||
{ "String 2", 2 },
|
||||
};
|
||||
|
||||
static inline void
|
||||
compare_iter (ClutterModelIter *iter,
|
||||
const gint expected_row,
|
||||
const gchar *expected_foo,
|
||||
const gint expected_bar)
|
||||
{
|
||||
gchar *foo = NULL;
|
||||
gint bar = 0;
|
||||
gint row = 0;
|
||||
|
||||
row = clutter_model_iter_get_row (iter);
|
||||
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),
|
||||
g_print ("Row %d => %d: Got [ '%s', '%d' ], expected [ '%s', '%d' ]\n",
|
||||
row, expected_row,
|
||||
foo, bar,
|
||||
expected_foo, expected_bar);
|
||||
|
||||
g_assert_cmpint (row, ==, expected_row);
|
||||
g_assert_cmpstr (foo, ==, expected_foo);
|
||||
g_assert_cmpint (bar, ==, expected_bar);
|
||||
|
||||
@ -97,12 +122,114 @@ on_row_added (ClutterModel *model,
|
||||
ModelData *model_data = data;
|
||||
|
||||
compare_iter (iter,
|
||||
model_data->n_row,
|
||||
base_model[model_data->n_row].expected_foo,
|
||||
base_model[model_data->n_row].expected_bar);
|
||||
|
||||
model_data->n_row += 1;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
filter_even_rows (ClutterModel *model,
|
||||
ClutterModelIter *iter,
|
||||
gpointer dummy G_GNUC_UNUSED)
|
||||
{
|
||||
gint bar_value;
|
||||
|
||||
clutter_model_iter_get (iter, COLUMN_BAR, &bar_value, -1);
|
||||
|
||||
if (bar_value % 2 == 0)
|
||||
return TRUE;
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
filter_odd_rows (ClutterModel *model,
|
||||
ClutterModelIter *iter,
|
||||
gpointer dummy G_GNUC_UNUSED)
|
||||
{
|
||||
gint bar_value;
|
||||
|
||||
clutter_model_iter_get (iter, COLUMN_BAR, &bar_value, -1);
|
||||
|
||||
if (bar_value % 2 != 0)
|
||||
return TRUE;
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void
|
||||
test_list_model_filter (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;
|
||||
|
||||
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 (filter odd)...\n");
|
||||
|
||||
clutter_model_set_filter (test_data.model, filter_odd_rows, NULL, NULL);
|
||||
|
||||
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, i,
|
||||
filter_odd[i].expected_foo,
|
||||
filter_odd[i].expected_bar);
|
||||
|
||||
iter = clutter_model_iter_next (iter);
|
||||
i += 1;
|
||||
}
|
||||
|
||||
g_object_unref (iter);
|
||||
|
||||
if (g_test_verbose ())
|
||||
g_print ("Backward iteration (filter even)...\n");
|
||||
|
||||
clutter_model_set_filter (test_data.model, filter_even_rows, NULL, NULL);
|
||||
|
||||
iter = clutter_model_get_last_iter (test_data.model);
|
||||
g_assert (iter != NULL);
|
||||
|
||||
i = 0;
|
||||
do
|
||||
{
|
||||
compare_iter (iter, G_N_ELEMENTS (filter_even) - i - 1,
|
||||
filter_even[i].expected_foo,
|
||||
filter_even[i].expected_bar);
|
||||
|
||||
iter = clutter_model_iter_prev (iter);
|
||||
i += 1;
|
||||
}
|
||||
while (!clutter_model_iter_is_first (iter));
|
||||
|
||||
g_object_unref (iter);
|
||||
|
||||
g_object_unref (test_data.model);
|
||||
}
|
||||
|
||||
void
|
||||
test_list_model_iterate (TestConformSimpleFixture *fixture,
|
||||
gconstpointer data)
|
||||
@ -141,7 +268,7 @@ test_list_model_iterate (TestConformSimpleFixture *fixture,
|
||||
i = 0;
|
||||
while (!clutter_model_iter_is_last (iter))
|
||||
{
|
||||
compare_iter (iter,
|
||||
compare_iter (iter, i,
|
||||
forward_base[i].expected_foo,
|
||||
forward_base[i].expected_bar);
|
||||
|
||||
@ -158,10 +285,9 @@ test_list_model_iterate (TestConformSimpleFixture *fixture,
|
||||
g_assert (iter != NULL);
|
||||
|
||||
i = 0;
|
||||
|
||||
do
|
||||
{
|
||||
compare_iter (iter,
|
||||
compare_iter (iter, G_N_ELEMENTS (backward_base) - i - 1,
|
||||
backward_base[i].expected_foo,
|
||||
backward_base[i].expected_bar);
|
||||
|
||||
@ -170,7 +296,7 @@ test_list_model_iterate (TestConformSimpleFixture *fixture,
|
||||
}
|
||||
while (!clutter_model_iter_is_first (iter));
|
||||
|
||||
compare_iter (iter,
|
||||
compare_iter (iter, G_N_ELEMENTS (backward_base) - i - 1,
|
||||
backward_base[i].expected_foo,
|
||||
backward_base[i].expected_bar);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user