From c5b38121ec7ffd2148f420e357dc106bd2bd6cf6 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 18 Nov 2010 23:45:42 -0500 Subject: [PATCH] Add tests for matching These tests try to decipher how specificity works for selectors. --- gtk/tests/stylecontext.c | 204 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 203 insertions(+), 1 deletion(-) diff --git a/gtk/tests/stylecontext.c b/gtk/tests/stylecontext.c index 8a53821576..8888160b0a 100644 --- a/gtk/tests/stylecontext.c +++ b/gtk/tests/stylecontext.c @@ -255,16 +255,218 @@ test_parse_declarations (void) } } +static void +test_path (void) +{ + GtkWidgetPath *path; + GtkWidgetPath *path2; + gint pos; + GtkRegionFlags flags; + + path = gtk_widget_path_new (); + g_assert_cmpint (gtk_widget_path_length (path), ==, 0); + + pos = gtk_widget_path_append_type (path, GTK_TYPE_WINDOW); + g_assert_cmpint (pos, ==, 0); + g_assert_cmpint (gtk_widget_path_length (path), ==, 1); + g_assert (gtk_widget_path_iter_get_widget_type (path, 0) == GTK_TYPE_WINDOW); + g_assert (gtk_widget_path_is_type (path, GTK_TYPE_WIDGET)); + g_assert (gtk_widget_path_iter_get_name (path, 0) == NULL); + + pos = gtk_widget_path_append_type (path, GTK_TYPE_WIDGET); + g_assert_cmpint (pos, ==, 1); + g_assert_cmpint (gtk_widget_path_length (path), ==, 2); + gtk_widget_path_iter_set_widget_type (path, pos, GTK_TYPE_BUTTON); + g_assert (gtk_widget_path_is_type (path, GTK_TYPE_BUTTON)); + g_assert (gtk_widget_path_has_parent (path, GTK_TYPE_WIDGET)); + g_assert (gtk_widget_path_has_parent (path, GTK_TYPE_WINDOW)); + g_assert (!gtk_widget_path_has_parent (path, GTK_TYPE_DIALOG)); + g_assert (gtk_widget_path_iter_get_name (path, 1) == NULL); + + gtk_widget_path_iter_set_name (path, 1, "name"); + g_assert (gtk_widget_path_iter_has_name (path, 1, "name")); + + gtk_widget_path_iter_add_class (path, 1, "class1"); + gtk_widget_path_iter_add_class (path, 1, "class2"); + g_assert (gtk_widget_path_iter_has_class (path, 1, "class1")); + g_assert (gtk_widget_path_iter_has_class (path, 1, "class2")); + g_assert (!gtk_widget_path_iter_has_class (path, 1, "class3")); + + path2 = gtk_widget_path_copy (path); + g_assert (gtk_widget_path_iter_has_class (path2, 1, "class1")); + g_assert (gtk_widget_path_iter_has_class (path2, 1, "class2")); + g_assert (!gtk_widget_path_iter_has_class (path2, 1, "class3")); + gtk_widget_path_free (path2); + + gtk_widget_path_iter_remove_class (path, 1, "class2"); + g_assert (gtk_widget_path_iter_has_class (path, 1, "class1")); + g_assert (!gtk_widget_path_iter_has_class (path, 1, "class2")); + gtk_widget_path_iter_clear_classes (path, 1); + g_assert (!gtk_widget_path_iter_has_class (path, 1, "class1")); + + gtk_widget_path_iter_add_region (path, 1, "tab", 0); + gtk_widget_path_iter_add_region (path, 1, "title", GTK_REGION_EVEN | GTK_REGION_FIRST); + + g_assert (gtk_widget_path_iter_has_region (path, 1, "tab", &flags) && + flags == 0); + g_assert (gtk_widget_path_iter_has_region (path, 1, "title", &flags) && + flags == (GTK_REGION_EVEN | GTK_REGION_FIRST)); + g_assert (!gtk_widget_path_iter_has_region (path, 1, "extension", NULL)); + + path2 = gtk_widget_path_copy (path); + g_assert (gtk_widget_path_iter_has_region (path2, 1, "tab", &flags) && + flags == 0); + g_assert (gtk_widget_path_iter_has_region (path2, 1, "title", &flags) && + flags == (GTK_REGION_EVEN | GTK_REGION_FIRST)); + g_assert (!gtk_widget_path_iter_has_region (path2, 1, "extension", NULL)); + gtk_widget_path_free (path2); + + gtk_widget_path_free (path); +} + +static void +test_match (void) +{ + GtkStyleContext *context; + GtkWidgetPath *path; + GtkCssProvider *provider; + GError *error; + const gchar *data; + GdkRGBA *color; + GdkRGBA expected; + + error = NULL; + provider = gtk_css_provider_new (); + + gdk_rgba_parse (&expected, "#fff"); + + context = gtk_style_context_new (); + + path = gtk_widget_path_new (); + gtk_widget_path_append_type (path, GTK_TYPE_WINDOW); + gtk_widget_path_append_type (path, GTK_TYPE_BOX); + gtk_widget_path_append_type (path, GTK_TYPE_BUTTON); + gtk_widget_path_iter_set_name (path, 0, "mywindow"); + gtk_widget_path_iter_add_class (path, 2, "button"); + gtk_style_context_set_path (context, path); + gtk_widget_path_free (path); + + gtk_style_context_add_provider (context, + GTK_STYLE_PROVIDER (provider), + GTK_STYLE_PROVIDER_PRIORITY_USER); + + data = "* { color: #fff }"; + gtk_css_provider_load_from_data (provider, data, -1, &error); + g_assert_no_error (error); + gtk_style_context_invalidate (context); + gtk_style_context_get (context, 0, "color", &color, NULL); + g_assert (gdk_rgba_equal (color, &expected)); + gdk_rgba_free (color); + + data = "* { color: #f00 }\n" + "GtkButton { color: #fff }"; + gtk_css_provider_load_from_data (provider, data, -1, &error); + g_assert_no_error (error); + gtk_style_context_invalidate (context); + gtk_style_context_get (context, 0, "color", &color, NULL); + g_assert (gdk_rgba_equal (color, &expected)); + gdk_rgba_free (color); + + data = "* { color: #f00 }\n" + "GtkButton { color: #fff }\n" + "GtkWindow > GtkButton { color: #000 }"; + gtk_css_provider_load_from_data (provider, data, -1, &error); + g_assert_no_error (error); + gtk_style_context_invalidate (context); + gtk_style_context_get (context, 0, "color", &color, NULL); + g_assert (gdk_rgba_equal (color, &expected)); + gdk_rgba_free (color); + + data = "* { color: #f00 }\n" + ".button { color: #fff }"; + gtk_css_provider_load_from_data (provider, data, -1, &error); + g_assert_no_error (error); + gtk_style_context_invalidate (context); + gtk_style_context_get (context, 0, "color", &color, NULL); + g_assert (gdk_rgba_equal (color, &expected)); + gdk_rgba_free (color); + + data = "* { color: #f00 }\n" + "GtkButton { color: #000 }\n" + ".button { color: #fff }"; + gtk_css_provider_load_from_data (provider, data, -1, &error); + g_assert_no_error (error); + gtk_style_context_invalidate (context); + gtk_style_context_get (context, 0, "color", &color, NULL); + g_assert (gdk_rgba_equal (color, &expected)); + gdk_rgba_free (color); + + data = "* { color: #f00 }\n" + "GtkButton { color: #000 }\n" + "GtkWindow GtkButton { color: #fff }"; + gtk_css_provider_load_from_data (provider, data, -1, &error); + g_assert_no_error (error); + gtk_style_context_invalidate (context); + gtk_style_context_get (context, 0, "color", &color, NULL); + g_assert (gdk_rgba_equal (color, &expected)); + gdk_rgba_free (color); + + data = "* { color: #f00 }\n" + ".button { color: #000 }\n" + "GtkWindow .button { color: #fff }"; + gtk_css_provider_load_from_data (provider, data, -1, &error); + g_assert_no_error (error); + gtk_style_context_invalidate (context); + gtk_style_context_get (context, 0, "color", &color, NULL); + g_assert (gdk_rgba_equal (color, &expected)); + gdk_rgba_free (color); + + data = "* { color: #f00 }\n" + "* .button { color: #000 }\n" + "#mywindow .button { color: #fff }"; + gtk_css_provider_load_from_data (provider, data, -1, &error); + g_assert_no_error (error); + gtk_style_context_invalidate (context); + gtk_style_context_get (context, 0, "color", &color, NULL); + g_assert (gdk_rgba_equal (color, &expected)); + gdk_rgba_free (color); + + data = "* { color: #f00 }\n" + "GtkWindow .button { color: #000 }\n" + "GtkWindow#mywindow .button { color: #fff }"; + gtk_css_provider_load_from_data (provider, data, -1, &error); + g_assert_no_error (error); + gtk_style_context_invalidate (context); + gtk_style_context_get (context, 0, "color", &color, NULL); + g_assert (gdk_rgba_equal (color, &expected)); + gdk_rgba_free (color); + + data = "* { color: #f00 }\n" + "GtkWindow .button { color: #fff }\n" + "GObject .button { color: #000 }"; + gtk_css_provider_load_from_data (provider, data, -1, &error); + g_assert_no_error (error); + gtk_style_context_invalidate (context); + gtk_style_context_get (context, 0, "color", &color, NULL); + g_assert (gdk_rgba_equal (color, &expected)); + gdk_rgba_free (color); + + g_object_unref (provider); + g_object_unref (context); +} + int main (int argc, char *argv[]) { - g_type_init (); + gtk_init (NULL, NULL); g_test_init (&argc, &argv, NULL); g_test_add_func ("/style/parse/empty", test_parse_empty); g_test_add_func ("/style/parse/at", test_parse_at); g_test_add_func ("/style/parse/selectors", test_parse_selectors); g_test_add_func ("/style/parse/declarations", test_parse_declarations); + g_test_add_func ("/style/path", test_path); + g_test_add_func ("/style/match", test_match); return g_test_run (); }