Merge branch 'matthiasc/for-master' into 'master'

More work on css transition tests

See merge request GNOME/gtk!3154
This commit is contained in:
Matthias Clasen 2021-02-04 13:28:59 +00:00
commit 26e84a7b8c
8 changed files with 260 additions and 176 deletions

View File

@ -129,7 +129,7 @@ done with
|:-----------|:----------|:------|
|color | [CSS Color Level 3](https://www.w3.org/TR/css3-color/#foreground) | |
|opacity | [CSS Color Level 3](https://www.w3.org/TR/css3-color/#opacity) | |
|filter | [CSS Filter Effect Level 1](https://drafts.fxtf.org/filters/#FilterProperty) | |
|filter | [CSS Filter Effect Level 1](https://drafts.fxtf.org/filters/#FilterProperty) | CSS allows drop-shadow |
|font-family | [CSS Fonts Level 3](https://www.w3.org/TR/css3-fonts/#font-family-prop) | defaults to gtk-font-name setting |
|font-size | [CSS Fonts Level 3](https://www.w3.org/TR/css3-fonts/#font-size-prop) | defaults to gtk-font-name setting |
|font-style | [CSS Fonts Level 3](https://www.w3.org/TR/css3-fonts/#font-style-prop) | |

View File

@ -151,11 +151,11 @@ gtk_css_value_shadow_equal (const GtkCssValue *value1,
const ShadowValue *shadow2 = &value2->shadows[i];
if (shadow1->inset != shadow2->inset ||
_gtk_css_value_equal (shadow1->hoffset, shadow2->hoffset) ||
_gtk_css_value_equal (shadow1->voffset, shadow2->voffset) ||
_gtk_css_value_equal (shadow1->radius, shadow2->radius) ||
_gtk_css_value_equal (shadow1->spread, shadow2->spread) ||
_gtk_css_value_equal (shadow1->color, shadow2->color))
!_gtk_css_value_equal (shadow1->hoffset, shadow2->hoffset) ||
!_gtk_css_value_equal (shadow1->voffset, shadow2->voffset) ||
!_gtk_css_value_equal (shadow1->radius, shadow2->radius) ||
!_gtk_css_value_equal (shadow1->spread, shadow2->spread) ||
!_gtk_css_value_equal (shadow1->color, shadow2->color))
return FALSE;
}

View File

@ -260,8 +260,8 @@ _gtk_css_value_transition (GtkCssValue *start,
guint property_id,
double progress)
{
gtk_internal_return_val_if_fail (start != NULL, FALSE);
gtk_internal_return_val_if_fail (end != NULL, FALSE);
gtk_internal_return_val_if_fail (start != NULL, NULL);
gtk_internal_return_val_if_fail (end != NULL, NULL);
if (start->class != end->class)
return NULL;

View File

@ -0,0 +1,4 @@
[Test]
Exec=/bin/sh -c "env G_ENABLE_DIAGNOSTIC=0 GTK_A11Y=test @libexecdir@/installed-tests/gtk-4.0/css/data --tap -k"
Type=session
Output=TAP

View File

@ -6,6 +6,7 @@ csstest_env.set('G_TEST_BUILDDIR', meson.current_build_dir())
csstest_env.set('GIO_USE_VFS', 'local')
csstest_env.set('GSETTINGS_BACKEND', 'memory')
csstest_env.set('G_ENABLE_DIAGNOSTIC', '0')
csstest_env.set('GDK_DEBUG', 'default-settings')
subdir('parser')
subdir('nodes')
@ -44,14 +45,14 @@ test('data', test_data,
suite: 'css',
)
test_parser = executable('test-css-value', 'test-css-value.c',
transition = executable('transition', 'transition.c',
c_args: common_cflags,
dependencies: libgtk_static_dep,
install: get_option('install-tests'),
install_dir: testexecdir,
)
test('css value', test_parser,
test('transition', transition,
args: [ '--tap', '-k' ],
protocol: 'tap',
env: csstest_env,
@ -66,6 +67,14 @@ if get_option('install-tests')
configuration: conf,
install_dir: testdatadir,
)
conf = configuration_data()
conf.set('libexecdir', gtk_libexecdir)
configure_file(input: 'data.test.in',
output: 'data.test',
configuration: conf,
install_dir: testdatadir,
)
endif
if false and get_option ('profiler')

View File

@ -1,166 +0,0 @@
/*
* Copyright (C) 2021 Red Hat Inc.
*
* Author:
* Matthias Clasen <mclasen@redhat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gtk/gtk.h>
#include "gtk/gtkcssvalueprivate.h"
#include "gtk/gtkcsscolorvalueprivate.h"
#include "gtk/gtkcssstylepropertyprivate.h"
static gboolean
color_is_near (const GdkRGBA *color1,
const GdkRGBA *color2)
{
if (fabs (color1->red - color2->red) > FLT_EPSILON)
return FALSE;
if (fabs (color1->green - color2->green) > FLT_EPSILON)
return FALSE;
if (fabs (color1->blue- color2->blue) > FLT_EPSILON)
return FALSE;
if (fabs (color1->alpha- color2->alpha) > FLT_EPSILON)
return FALSE;
return TRUE;
}
static gboolean
value_is_near (int prop,
GtkCssValue *value1,
GtkCssValue *value2)
{
if (_gtk_css_value_equal (value1, value2))
return TRUE;
switch (prop)
{
case GTK_CSS_PROPERTY_COLOR:
{
GtkCssValue *v1, *v2;
const GdkRGBA *c1, *c2;
gboolean res;
v1 = _gtk_css_value_compute (value1, prop, NULL, NULL, NULL);
v2 = _gtk_css_value_compute (value2, prop, NULL, NULL, NULL);
c1 = gtk_css_color_value_get_rgba (v1);
c2 = gtk_css_color_value_get_rgba (v2);
res = color_is_near (c1, c2);
gtk_css_value_unref (v1);
gtk_css_value_unref (v2);
return res;
}
break;
default:
break;
}
return FALSE;
}
typedef struct {
int prop;
const char *value1;
const char *value2;
double progress;
const char *expected;
} ValueTransitionTest;
static ValueTransitionTest tests[] = {
{ GTK_CSS_PROPERTY_COLOR, "transparent", "rgb(255,0,0)", 0.25, "rgba(255,0,0,0.25)" },
{ GTK_CSS_PROPERTY_BOX_SHADOW, "none", "2px 2px 10px 4px rgb(200,200,200)", 0.5, "1px 1px 5px 2px rgb(100,100,100)" },
{ GTK_CSS_PROPERTY_BOX_SHADOW, "2px 2px 10px 4px rgb(200,200,200)", "none", 0.5, "1px 1px 5px 2px rgb(100,100,100)" },
};
static void
test_transition (gconstpointer data)
{
ValueTransitionTest *test = &tests[GPOINTER_TO_INT (data)];
GtkStyleProperty *prop;
GtkCssValue *value1;
GtkCssValue *value2;
GtkCssValue *expected;
GtkCssValue *result;
GtkCssParser *parser;
GBytes *bytes;
prop = (GtkStyleProperty *)_gtk_css_style_property_lookup_by_id (test->prop);
bytes = g_bytes_new_static (test->value1, strlen (test->value1));
parser = gtk_css_parser_new_for_bytes (bytes, NULL, NULL, NULL, NULL, NULL);
value1 = _gtk_style_property_parse_value (prop, parser);
gtk_css_parser_unref (parser);
g_bytes_unref (bytes);
bytes = g_bytes_new_static (test->value1, strlen (test->value2));
parser = gtk_css_parser_new_for_bytes (bytes, NULL, NULL, NULL, NULL, NULL);
value2 = _gtk_style_property_parse_value (prop, parser);
gtk_css_parser_unref (parser);
g_bytes_unref (bytes);
bytes = g_bytes_new_static (test->value1, strlen (test->expected));
parser = gtk_css_parser_new_for_bytes (bytes, NULL, NULL, NULL, NULL, NULL);
expected = _gtk_style_property_parse_value (prop, parser);
gtk_css_parser_unref (parser);
g_bytes_unref (bytes);
result = _gtk_css_value_transition (value1, value2, test->prop, test->progress);
g_assert_true (value_is_near (test->prop, result, expected));
gtk_css_value_unref (value1);
gtk_css_value_unref (value2);
gtk_css_value_unref (expected);
gtk_css_value_unref (result);
}
int
main (int argc, char **argv)
{
GtkStyleProperty *previous;
int j;
gtk_test_init (&argc, &argv);
previous = NULL;
j = 0;
for (int i = 0; i < G_N_ELEMENTS (tests); i++)
{
ValueTransitionTest *test = &tests[i];
GtkStyleProperty *prop;
char *path;
prop = (GtkStyleProperty *)_gtk_css_style_property_lookup_by_id (test->prop);
if (prop != previous)
{
previous = prop;
j = 0;
}
else
j++;
path = g_strdup_printf ("/css/value/transition/%s/%d", _gtk_style_property_get_name ((GtkStyleProperty *)prop), j);
g_test_add_data_func (path, GINT_TO_POINTER (i), test_transition);
g_free (path);
}
return g_test_run ();
}

236
testsuite/css/transition.c Normal file
View File

@ -0,0 +1,236 @@
/*
* Copyright (C) 2021 Red Hat Inc.
*
* Author:
* Matthias Clasen <mclasen@redhat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gtk/gtk.h>
#include "gtk/gtkcssvalueprivate.h"
#include "gtk/gtkcsscolorvalueprivate.h"
#include "gtk/gtkcssnumbervalueprivate.h"
#include "gtk/gtkcssstylepropertyprivate.h"
#include "gtk/gtkcssstaticstyleprivate.h"
static gboolean
color_is_near (const GdkRGBA *color1,
const GdkRGBA *color2)
{
if (fabs (color1->red - color2->red) > FLT_EPSILON)
return FALSE;
if (fabs (color1->green - color2->green) > FLT_EPSILON)
return FALSE;
if (fabs (color1->blue- color2->blue) > FLT_EPSILON)
return FALSE;
if (fabs (color1->alpha- color2->alpha) > FLT_EPSILON)
return FALSE;
return TRUE;
}
static gboolean
value_is_near (int prop,
GtkCssValue *value1,
GtkCssValue *value2)
{
if (_gtk_css_value_equal (value1, value2))
return TRUE;
switch (prop)
{
case GTK_CSS_PROPERTY_COLOR:
{
const GdkRGBA *c1, *c2;
gboolean res;
c1 = gtk_css_color_value_get_rgba (value1);
c2 = gtk_css_color_value_get_rgba (value2);
res = color_is_near (c1, c2);
return res;
}
break;
case GTK_CSS_PROPERTY_FONT_SIZE:
{
double n1, n2;
n1 = _gtk_css_number_value_get (value1, 100);
n2 = _gtk_css_number_value_get (value2, 100);
return fabs (n1 - n2) < FLT_EPSILON;
}
break;
default:
break;
}
return FALSE;
}
static void
assert_css_value (int prop,
GtkCssValue *result,
GtkCssValue *expected)
{
if (result == expected)
return;
if (((result == NULL) != (expected == NULL)) ||
!value_is_near (prop, result, expected))
{
char *r = result ? _gtk_css_value_to_string (result) : g_strdup ("(nil)");
char *e = expected ? _gtk_css_value_to_string (expected) : g_strdup ("(nil)");
g_print ("Expected %s got %s\n", e, r);
g_free (r);
g_free (e);
g_assert_not_reached ();
}
}
/* Tests for css transitions */
typedef struct {
int prop;
const char *value1;
const char *value2;
double progress;
const char *value3;
} ValueTransitionTest;
static ValueTransitionTest tests[] = {
{ GTK_CSS_PROPERTY_COLOR, "transparent", "rgb(255,0,0)", 0.25, "rgba(255,0,0,0.25)" },
{ GTK_CSS_PROPERTY_BOX_SHADOW, "none", "2px 2px 10px 4px rgb(200,200,200)", 0.5, "1px 1px 5px 2px rgba(200,200,200,0.5)" },
{ GTK_CSS_PROPERTY_BOX_SHADOW, "2px 2px 10px 4px rgb(200,200,200)", "none", 0.5, "1px 1px 5px 2px rgba(200,200,200,0.5)" },
{ GTK_CSS_PROPERTY_BOX_SHADOW, "2px 2px 10px 4px rgb(200,200,200), 0px 10px 8px 6px rgb(200,100,0)", "none", 0.5, "1px 1px 5px 2px rgba(200,200,200,0.5), 0px 5px 4px 3px rgba(200,100,0,0.5)" },
{ GTK_CSS_PROPERTY_FONT_SIZE, "12px", "16px", 0.25, "13px" },
{ GTK_CSS_PROPERTY_FONT_SIZE, "10px", "10pt", 0.5, "11.66666667px" },
{ GTK_CSS_PROPERTY_FONT_FAMILY, "cantarell", "sans", 0, "cantarell"},
{ GTK_CSS_PROPERTY_FONT_FAMILY, "cantarell", "sans", 1, "sans" },
{ GTK_CSS_PROPERTY_FONT_FAMILY, "cantarell", "sans", 0.5, NULL },
{ GTK_CSS_PROPERTY_BACKGROUND_POSITION, "20px 10px", "40px", 0.5, "30px calc(5px + 25%)" },
//TODO We don't currently transition border-image-width
//{ GTK_CSS_PROPERTY_BORDER_IMAGE_WIDTH, "10px 20px", "0px", 0.5, "5px 10px 0.5px 0.5px" },
{ GTK_CSS_PROPERTY_FILTER, "none", "blur(6px)", 0.5, "blur(3px)" },
{ GTK_CSS_PROPERTY_FILTER, "none", "blur(6px),contrast(0.6)", 0.5, "blur(3px),contrast(0.3)" },
{ GTK_CSS_PROPERTY_FILTER, "contrast(0.6)", "blur(6px)", 0.5, NULL},
};
static GtkCssValue *
value_from_string (GtkStyleProperty *prop,
const char *str)
{
GBytes *bytes;
GtkCssParser *parser;
GtkCssValue *value;
bytes = g_bytes_new_static (str, strlen (str));
parser = gtk_css_parser_new_for_bytes (bytes, NULL, NULL, NULL, NULL, NULL);
value = _gtk_style_property_parse_value (prop, parser);
gtk_css_parser_unref (parser);
g_bytes_unref (bytes);
return value;
}
static void
test_transition (gconstpointer data)
{
ValueTransitionTest *test = &tests[GPOINTER_TO_INT (data)];
GtkStyleProperty *prop;
GtkCssValue *value1;
GtkCssValue *value2;
GtkCssValue *value3;
GtkCssValue *computed1;
GtkCssValue *computed2;
GtkCssValue *computed3;
GtkCssValue *result;
GtkStyleProvider *provider;
GtkCssStyle *style;
provider = GTK_STYLE_PROVIDER (gtk_settings_get_default ());
style = gtk_css_static_style_get_default ();
prop = (GtkStyleProperty *)_gtk_css_style_property_lookup_by_id (test->prop);
value1 = value_from_string (prop, test->value1);
g_assert_nonnull (value1);
computed1 = _gtk_css_value_compute (value1, test->prop, provider, style, NULL);
value2 = value_from_string (prop, test->value2);
g_assert_nonnull (value1);
computed2 = _gtk_css_value_compute (value2, test->prop, provider, style, NULL);
if (test->value3)
{
value3 = value_from_string (prop, test->value3);
computed3 = _gtk_css_value_compute (value3, test->prop, provider, style, NULL);
}
else
{
value3 = computed3 = NULL;
}
result = _gtk_css_value_transition (computed1, computed2, test->prop, test->progress);
assert_css_value (test->prop, result, computed3);
gtk_css_value_unref (value1);
gtk_css_value_unref (value2);
if (value3)
gtk_css_value_unref (value3);
gtk_css_value_unref (computed1);
gtk_css_value_unref (computed2);
if (computed3)
gtk_css_value_unref (computed3);
gtk_css_value_unref (result);
}
int
main (int argc, char **argv)
{
GtkStyleProperty *previous;
int j;
gtk_test_init (&argc, &argv);
previous = NULL;
j = 0;
for (int i = 0; i < G_N_ELEMENTS (tests); i++)
{
ValueTransitionTest *test = &tests[i];
GtkStyleProperty *prop;
char *path;
prop = (GtkStyleProperty *)_gtk_css_style_property_lookup_by_id (test->prop);
if (prop != previous)
{
previous = prop;
j = 0;
}
else
j++;
path = g_strdup_printf ("/css/value/transition/%s/%d", _gtk_style_property_get_name ((GtkStyleProperty *)prop), j);
g_test_add_data_func (path, GINT_TO_POINTER (i), test_transition);
g_free (path);
}
return g_test_run ();
}

View File

@ -30,6 +30,7 @@ tick_callback_for_1_frame (GtkWidget *widget,
gpointer unused)
{
reftest_uninhibit_snapshot ();
gtk_widget_queue_draw (widget);
return G_SOURCE_REMOVE;
}