Merge branch 'main' into 'main'

Tests: add GtkPicture builder test

See merge request GNOME/gtk!7486
This commit is contained in:
Matthias Clasen 2024-07-27 03:08:15 +00:00
commit a9c3163a64
4 changed files with 114 additions and 10 deletions

View File

@ -161,7 +161,8 @@
* - colors (in a format understood by [method@Gdk.RGBA.parse])
* - `GVariant` (can be specified in the format understood by
* [func@GLib.Variant.parse])
* - pixbufs (can be specified as a filename of an image file to load)
* - pixbufs (can be specified as an object id, a resource path or a filename of an image file to load relative to the Builder file or the CWD if [method@Gtk.Builder.add_from_string] was used)
* - GFile (like pixbufs, can be specified as an object id, a URI or a filename of a file to load relative to the Builder file or the CWD if [method@Gtk.Builder.add_from_string] was used)
*
* Objects can be referred to by their name and by default refer to
* objects declared in the local XML fragment and objects exposed via
@ -2656,20 +2657,49 @@ gtk_builder_value_from_string_type (GtkBuilder *builder,
}
else if (G_VALUE_HOLDS (value, G_TYPE_FILE))
{
GObject *object = g_hash_table_lookup (priv->objects, string);
GFile *file;
if (g_hash_table_contains (priv->objects, string))
if (object)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"Could not create file '%s': "
" '%s' is already used as object id",
string, string);
return FALSE;
if (g_type_is_a (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
{
g_value_set_object (value, object);
return TRUE;
}
else
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"Could not create file '%s': "
" '%s' is already used as object id",
string, string);
return FALSE;
}
}
if (!g_uri_is_valid (string, G_URI_FLAGS_NONE, NULL))
{
gchar *fullpath = _gtk_builder_get_absolute_filename (builder, string);
file = g_file_new_for_path (fullpath);
g_free (fullpath);
}
else if (g_str_has_prefix (string, "file://"))
{
gchar *path = g_uri_unescape_string (string + strlen ("file://"), "/");
gchar *fullpath = _gtk_builder_get_absolute_filename (builder, path);
file = g_file_new_for_path (fullpath);
g_free (fullpath);
g_free (path);
}
else
{
file = g_file_new_for_uri (string);
}
file = g_file_new_for_uri (string);
g_value_set_object (value, file);
g_object_unref (G_OBJECT (file));

View File

@ -2917,6 +2917,70 @@ test_buildable (void)
g_object_unref (my_gtk_buildable);
}
static GFile *
builder_object_get_file_property (GtkBuilder *builder, const gchar *object_id)
{
GObject *obj = gtk_builder_get_object (builder, object_id);
GFile *value = NULL;
g_assert (obj);
g_object_get(obj, "file", &value, NULL);
g_assert (value);
g_assert (g_type_is_a (G_OBJECT_TYPE (value), G_TYPE_FILE));
return value;
}
static void
test_picture (void)
{
GtkBuilder *builder;
GError *error = NULL;
GFile *file_path, *file_uri;
gchar *contents = NULL;
gchar *path, *uri;
/* Load from file so builder knows where to read the files from */
path = g_test_build_filename (G_TEST_DIST, "ui", "picture.ui", NULL);
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, path, &error);
if (error)
{
g_print ("ERROR: %s", error->message);
g_error_free (error);
}
g_assert_null (error);
file_path = builder_object_get_file_property (builder, "relative_path");
path = g_file_get_path (file_path);
g_assert (path);
/* Check path is absolute */
g_assert (g_path_is_absolute (path));
/* Check contents can be loaded */
g_assert (g_file_load_contents (file_path, NULL, &contents, NULL, NULL, NULL));
g_free (contents);
file_uri = builder_object_get_file_property (builder, "relative_uri");
uri = g_file_get_uri (file_uri);
g_assert (uri);
/* Check uri is absolute */
g_assert (g_str_has_prefix (uri, "file://"));
g_assert (g_path_is_absolute (uri + strlen("file://")));
g_assert (g_file_load_contents (file_uri, NULL, &contents, NULL, NULL, NULL));
g_free (contents);
g_object_unref (builder);
g_free (path);
g_free (uri);
}
int
main (int argc, char **argv)
{
@ -2966,6 +3030,7 @@ main (int argc, char **argv)
g_test_add_func ("/Builder/Expressions", test_expressions);
g_test_add_func ("/Builder/Child Dispose Order", test_child_dispose_order);
g_test_add_func ("/Builder/Buildable", test_buildable);
g_test_add_func ("/Builder/Picture", test_picture);
return g_test_run();
}

View File

@ -0,0 +1 @@
SUCCESS

View File

@ -0,0 +1,8 @@
<interface>
<object class="GtkPicture" id="relative_path">
<property name="file">../../gdk/image-data/image.png</property>
</object>
<object class="GtkPicture" id="relative_uri">
<property name="file">file://../../gdk/image-data/image.png</property>
</object>
</interface>