[gtk/matthiasc/for-master] Add tests for content serialization
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/matthiasc/for-master] Add tests for content serialization
- Date: Sun, 2 May 2021 04:03:15 +0000 (UTC)
commit a5cab71c9bced548dfa5192b6199f541de07734e
Author: Matthias Clasen <mclasen redhat com>
Date: Sun May 2 00:01:57 2021 -0400
Add tests for content serialization
Test that roundtripping various types of content
through a stream of a given mimetype works as
expected.
testsuite/gdk/contentserializer.c | 204 ++++++++++++++++++++++++++++++++++++++
testsuite/gdk/meson.build | 1 +
2 files changed, 205 insertions(+)
---
diff --git a/testsuite/gdk/contentserializer.c b/testsuite/gdk/contentserializer.c
new file mode 100644
index 0000000000..c317531088
--- /dev/null
+++ b/testsuite/gdk/contentserializer.c
@@ -0,0 +1,204 @@
+#include <stdlib.h>
+
+#include <gtk/gtk.h>
+
+typedef struct {
+ GOutputStream *ostream;
+ GInputStream *istream;
+ const char *mime_type;
+ GValue value;
+ gboolean done;
+} TestData;
+
+static void
+deserialize_done (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GError *error = NULL;
+ gboolean res;
+ TestData *data = user_data;
+ GValue value = G_VALUE_INIT;
+
+ g_value_init (&value, G_VALUE_TYPE (&data->value));
+
+ res = gdk_content_deserialize_finish (result, &value, &error);
+ g_assert_true (res);
+ g_assert_no_error (error);
+
+ if (G_VALUE_TYPE (&data->value) == G_TYPE_STRING)
+ g_assert_cmpstr (g_value_get_string (&data->value), ==, g_value_get_string (&value));
+ else if (G_VALUE_TYPE (&data->value) == GDK_TYPE_RGBA)
+ {
+ GdkRGBA *c1 = g_value_get_boxed (&data->value);
+ GdkRGBA *c2 = g_value_get_boxed (&value);
+
+ g_assert_true (gdk_rgba_equal (c1, c2));
+ }
+ else if (G_VALUE_TYPE (&data->value) == G_TYPE_FILE)
+ {
+ GFile *f1 = g_value_get_object (&data->value);
+ GFile *f2 = g_value_get_object (&value);
+
+ g_assert_true (g_file_equal (f1, f2));
+ }
+ else if (G_VALUE_TYPE (&data->value) == GDK_TYPE_FILE_LIST)
+ {
+ GSList *s1 = g_value_get_boxed (&data->value);
+ GSList *s2 = g_value_get_boxed (&value);
+ GSList *l1, *l2;
+
+ g_assert_cmpint (g_slist_length (s1), ==, g_slist_length (s2));
+ for (l1 = s1, l2 = s2; l1 != NULL; l1 = l1->next, l2 = l2->next)
+ {
+ GFile *f1 = l1->data;
+ GFile *f2 = l2->data;
+ g_assert_true (g_file_equal (f1, f2));
+ }
+ }
+ else
+ g_assert_not_reached ();
+
+ g_value_unset (&value);
+
+ data->done = TRUE;
+ g_main_context_wakeup (NULL);
+}
+
+static void
+serialize_done (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GError *error = NULL;
+ gboolean res;
+ TestData *data = user_data;
+ gpointer serialized_data;
+ gsize serialized_len;
+
+ res = gdk_content_serialize_finish (result, &error);
+ g_assert_true (res);
+ g_assert_no_error (error);
+
+ serialized_data = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data->ostream));
+ serialized_len = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data->ostream));
+ data->istream = g_memory_input_stream_new_from_data (serialized_data, serialized_len, NULL);
+
+ gdk_content_deserialize_async (data->istream,
+ data->mime_type,
+ G_VALUE_TYPE (&data->value),
+ G_PRIORITY_DEFAULT,
+ NULL,
+ deserialize_done,
+ data);
+}
+
+static void
+test_content_roundtrip (const GValue *value,
+ const char *mime_type)
+{
+ TestData data = { 0, };
+
+ data.ostream = g_memory_output_stream_new_resizable ();
+ data.mime_type = g_strdup (mime_type);
+ g_value_init (&data.value, G_VALUE_TYPE (value));
+ g_value_copy (value, &data.value);
+ data.done = FALSE;
+
+ gdk_content_serialize_async (data.ostream,
+ data.mime_type,
+ &data.value,
+ G_PRIORITY_DEFAULT,
+ NULL,
+ serialize_done,
+ &data);
+
+ while (!data.done)
+ g_main_context_iteration (NULL, TRUE);
+
+ g_object_unref (data.ostream);
+ g_object_unref (data.istream);
+ g_value_unset (&data.value);
+}
+
+static void
+test_content_text_plain_utf8 (void)
+{
+ GValue value = G_VALUE_INIT;
+
+ g_value_init (&value, G_TYPE_STRING);
+ g_value_set_string (&value, "ABCDEF12345");
+ test_content_roundtrip (&value, "text/plain;charset=utf-8");
+ g_value_unset (&value);
+}
+
+static void
+test_content_text_plain (void)
+{
+ GValue value = G_VALUE_INIT;
+
+ g_value_init (&value, G_TYPE_STRING);
+ g_value_set_string (&value, "ABCDEF12345");
+ test_content_roundtrip (&value, "text/plain");
+ g_value_unset (&value);
+}
+
+static void
+test_content_color (void)
+{
+ GdkRGBA color;
+ GValue value = G_VALUE_INIT;
+
+ gdk_rgba_parse (&color, "magenta");
+ g_value_init (&value, GDK_TYPE_RGBA);
+ g_value_set_boxed (&value, &color);
+ test_content_roundtrip (&value, "application/x-color");
+ g_value_unset (&value);
+}
+
+static void
+test_content_file (void)
+{
+ GFile *file;
+ GValue value = G_VALUE_INIT;
+
+ file = g_file_new_for_path ("/etc/passwd");
+ g_value_init (&value, G_TYPE_FILE);
+ g_value_set_object (&value, file);
+ test_content_roundtrip (&value, "text/uri-list");
+ g_value_unset (&value);
+ g_object_unref (file);
+}
+
+static void
+test_content_files (void)
+{
+ GSList *files;
+ GValue value = G_VALUE_INIT;
+
+ files = NULL;
+ files = g_slist_append (files, g_file_new_for_path ("/etc/passwd"));
+ files = g_slist_append (files, g_file_new_for_path ("/boot/ostree"));
+
+ g_value_init (&value, GDK_TYPE_FILE_LIST);
+ g_value_set_boxed (&value, files);
+ test_content_roundtrip (&value, "text/uri-list");
+ g_value_unset (&value);
+ g_slist_free_full (files, g_object_unref);
+}
+
+int
+main (int argc, char *argv[])
+{
+ (g_test_init) (&argc, &argv, NULL);
+
+ gtk_init ();
+
+ g_test_add_func ("/content/text_plain_utf8", test_content_text_plain_utf8);
+ g_test_add_func ("/content/text_plain", test_content_text_plain);
+ g_test_add_func ("/content/color", test_content_color);
+ g_test_add_func ("/content/file", test_content_file);
+ g_test_add_func ("/content/files", test_content_files);
+
+ return g_test_run ();
+}
diff --git a/testsuite/gdk/meson.build b/testsuite/gdk/meson.build
index 005cb64ec7..50bfd0b5d0 100644
--- a/testsuite/gdk/meson.build
+++ b/testsuite/gdk/meson.build
@@ -13,6 +13,7 @@ tests = [
'array',
'cairo',
'clipboard',
+ 'contentserializer',
'cursor',
'display',
'displaymanager',
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]