[glib/wip/resources: 5/6] Add tests for GResource



commit d96d13adc39ede056ac34f55e37e8c8f6a458c0b
Author: Alexander Larsson <alexl redhat com>
Date:   Wed Dec 21 21:45:16 2011 +0100

    Add tests for GResource

 gio/tests/Makefile.am         |   23 +++-
 gio/tests/resources.c         |  372 +++++++++++++++++++++++++++++++++++++++++
 gio/tests/test.gresource.xml  |   13 ++
 gio/tests/test1.txt           |    1 +
 gio/tests/test2.gresource.xml |    6 +
 gio/tests/test2.txt           |    1 +
 gio/tests/test3.gresource.xml |    6 +
 gio/tests/test3.txt           |    1 +
 8 files changed, 422 insertions(+), 1 deletions(-)
---
diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am
index 7c7bded..16c4802 100644
--- a/gio/tests/Makefile.am
+++ b/gio/tests/Makefile.am
@@ -59,6 +59,7 @@ TEST_PROGS +=	 		\
 	network-monitor		\
 	fileattributematcher	\
 	gmenumodel		\
+	resources		\
 	$(NULL)
 
 if OS_UNIX
@@ -392,6 +393,9 @@ gdbus_connection_flush_helper_LDADD = $(progs_ldadd)
 appinfo_SOURCES = appinfo.c
 appinfo_LDADD   = $(progs_ldadd)
 
+resources_SOURCES = resources.c test_resources.c test_resources2.c test_resources2.h
+resources_LDADD   = $(progs_ldadd)
+
 appinfo_test_SOURCES = appinfo-test.c
 appinfo_test_LDADD   = $(progs_ldadd)
 
@@ -560,15 +564,30 @@ EXTRA_DIST += \
 	cert-key.pem			\
 	cert-list.pem			\
 	nothing.pem			\
+	test.gresource.xml		\
+	test2.gresource.xml		\
 	$(NULL)
 
-MISC_STUFF = test.mo
+MISC_STUFF = test.mo test.gresource
 
 test.mo: de.po
 	$(MSGFMT) -o test.mo $(srcdir)/de.po; \
 	$(MKDIR_P) de/LC_MESSAGES; \
 	cp -f test.mo de/LC_MESSAGES
 
+BUILT_SOURCES += test_resources.c test_resources2.c test_resources2.h
+test_resources.c: test2.gresource.xml test1.txt
+	$(top_builddir)/gio/glib-compile-resources --target=$@ --generate-source --c-name _g_test1 $(srcdir)/test2.gresource.xml
+
+test_resources2.c: test3.gresource.xml test1.txt
+	$(top_builddir)/gio/glib-compile-resources --target=$@ --generate-source --c-name _g_test2 --manual-register $(srcdir)/test3.gresource.xml
+
+test_resources2.h: test3.gresource.xml
+	$(top_builddir)/gio/glib-compile-resources --target=$@ --generate-header --c-name _g_test2 --manual-register $(srcdir)/test3.gresource.xml
+
+test.gresource: test.gresource.xml test1.txt test2.txt test3.txt
+	$(top_builddir)/gio/glib-compile-resources --target=$@ --sourcedir=$(srcdir) $(srcdir)/test.gresource.xml
+
 CLEANFILES = gdbus-test-codegen-generated.[ch] gdbus-test-codegen-generated-doc-*.xml
 
 DISTCLEANFILES = \
@@ -576,6 +595,8 @@ DISTCLEANFILES = \
 	org.gtk.test.enums.xml		\
 	de/LC_MESSAGES/test.mo		\
 	test.mo				\
+	test.gresource			\
+	test_resources.c		\
 	gsettings.store			\
 	gschemas.compiled 		\
 	schema-source/gschemas.compiled
diff --git a/gio/tests/resources.c b/gio/tests/resources.c
new file mode 100644
index 0000000..a0e1381
--- /dev/null
+++ b/gio/tests/resources.c
@@ -0,0 +1,372 @@
+/* GLib testing framework examples and tests
+ *
+ * Copyright (C) 2011 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <gio/gio.h>
+#include "test_resources2.h"
+
+static void
+test_resource (GResource *resource)
+{
+  GError *error = NULL;
+  gboolean found, success;
+  gsize size;
+  guint32 flags;
+  GBytes *data;
+  char **children;
+  GInputStream *in;
+  char buffer[128];
+
+  found = g_resource_get_info (resource,
+			       "/not/there",
+			       &size, &flags, &error);
+  g_assert (!found);
+  g_assert (error != NULL);
+  g_clear_error (&error);
+
+  found = g_resource_get_info (resource,
+			       "/test1.txt",
+			       &size, &flags, &error);
+  g_assert (found);
+  g_assert (error == NULL);
+  g_assert (size == 6);
+  g_assert (flags == (G_RESOURCE_FLAGS_LOCALIZED | G_RESOURCE_FLAGS_COMPRESSED));
+
+  found = g_resource_get_info (resource,
+			       "/a_prefix/test2.txt",
+			       &size, &flags, &error);
+  g_assert (found);
+  g_assert (error == NULL);
+  g_assert (size == 6);
+  g_assert (flags == 0);
+
+  found = g_resource_get_info (resource,
+			       "/a_prefix/test2-alias.txt",
+			       &size, &flags, &error);
+  g_assert (found);
+  g_assert (error == NULL);
+  g_assert (size == 6);
+  g_assert (flags == 0);
+
+  found = g_resource_get_info (resource,
+			       "/sv/test1.txt",
+			       &size, &flags, &error);
+  g_assert (found);
+  g_assert (error == NULL);
+  g_assert (size == 6);
+  g_assert (flags == 0);
+
+  /* This will fail due to compression */
+  data = g_resource_lookup_data (resource,
+				 "/test1.txt",
+				 &error);
+  g_assert (data == NULL);
+  g_assert (error != NULL);
+  g_clear_error (&error);
+
+  in = g_resource_open_stream (resource,
+			       "/test1.txt",
+			       &error);
+  g_assert (in != NULL);
+  g_assert (error == NULL);
+
+  success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
+				     &size,
+				     NULL, &error);
+  g_assert (success);
+  g_assert (error == NULL);
+  g_assert (size == 6);
+  g_assert_cmpstr (buffer, ==, "test1\n");
+
+  g_input_stream_close (in, NULL, NULL);
+
+  data = g_resource_lookup_data (resource,
+				 "/a_prefix/test2.txt",
+				 &error);
+  g_assert (data != NULL);
+  g_assert (error == NULL);
+  size = g_bytes_get_size (data);
+  g_assert (size == 6);
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
+
+  data = g_resource_lookup_data (resource,
+				 "/a_prefix/test2-alias.txt",
+				 &error);
+  g_assert (data != NULL);
+  g_assert (error == NULL);
+  size = g_bytes_get_size (data);
+  g_assert (size == 6);
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
+
+  data = g_resource_lookup_data (resource,
+				 "/sv/test1.txt",
+				 &error);
+  g_assert (data != NULL);
+  g_assert (error == NULL);
+  g_assert (size == 6);
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test3\n");
+
+  children = g_resource_enumerate_children  (resource,
+					     "/not/here",
+					     &error);
+  g_assert (children == NULL);
+  g_assert (error != NULL);
+  g_clear_error (&error);
+
+  children = g_resource_enumerate_children  (resource,
+					     "/a_prefix",
+					     &error);
+  g_assert (children != NULL);
+  g_assert (error == NULL);
+  g_assert (g_strv_length (children) == 2);
+}
+
+static void
+test_resource_file (void)
+{
+  GResource *resource;
+  GError *error = NULL;
+
+  resource = g_resource_load ("not-there", &error);
+  g_assert (resource == NULL);
+  g_assert (error != NULL);
+  g_clear_error (&error);
+
+  resource = g_resource_load ("test.gresource", &error);
+  g_assert (resource != NULL);
+  g_assert (error == NULL);
+
+  test_resource (resource);
+  g_resource_unref (resource);
+}
+
+static void
+test_resource_data (void)
+{
+  GResource *resource;
+  GError *error = NULL;
+  gboolean loaded_file;
+  char *content;
+  gsize content_size;
+  GBytes *data;
+
+  loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
+				     NULL);
+  g_assert (loaded_file);
+
+  data = g_bytes_new_take (content, content_size);
+  resource = g_resource_new_from_data (data, &error);
+  g_assert (resource != NULL);
+  g_assert (error == NULL);
+
+  test_resource (resource);
+
+  g_resource_unref (resource);
+}
+
+static void
+test_resource_registred (void)
+{
+  GResource *resource;
+  GError *error = NULL;
+  gboolean found, success;
+  gsize size;
+  guint32 flags;
+  GBytes *data;
+  char **children;
+  GInputStream *in;
+  char buffer[128];
+
+  resource = g_resource_load ("test.gresource", &error);
+  g_assert (resource != NULL);
+  g_assert (error == NULL);
+
+  found = g_resources_get_info ("/test1.txt",
+				&size, &flags, &error);
+  g_assert (!found);
+  g_assert (error != NULL);
+  g_clear_error (&error);
+
+  g_resources_register (resource);
+
+  found = g_resources_get_info ("/test1.txt",
+				&size, &flags, &error);
+  g_assert (found);
+  g_assert (error == NULL);
+  g_assert (size == 6);
+  g_assert (flags == (G_RESOURCE_FLAGS_LOCALIZED | G_RESOURCE_FLAGS_COMPRESSED));
+
+  found = g_resources_get_info ("/a_prefix/test2.txt",
+				&size, &flags, &error);
+  g_assert (found);
+  g_assert (error == NULL);
+  g_assert (size == 6);
+  g_assert (flags == 0);
+
+  found = g_resources_get_info ("/a_prefix/test2-alias.txt",
+				&size, &flags, &error);
+  g_assert (found);
+  g_assert (error == NULL);
+  g_assert (size == 6);
+  g_assert (flags == 0);
+
+  found = g_resources_get_info ("/sv/test1.txt",
+				&size, &flags, &error);
+  g_assert (found);
+  g_assert (error == NULL);
+  g_assert (size == 6);
+  g_assert (flags == 0);
+
+  /* This will fail due to compression */
+  data = g_resources_lookup_data ("/test1.txt",
+				  &error);
+  g_assert (data == NULL);
+  g_assert (error != NULL);
+  g_clear_error (&error);
+
+  in = g_resources_open_stream ("/test1.txt",
+				&error);
+  g_assert (in != NULL);
+  g_assert (error == NULL);
+
+  success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
+				     &size,
+				     NULL, &error);
+  g_assert (success);
+  g_assert (error == NULL);
+  g_assert (size == 6);
+  g_assert_cmpstr (buffer, ==, "test1\n");
+
+  g_input_stream_close (in, NULL, NULL);
+
+
+  data = g_resources_lookup_data ("/a_prefix/test2.txt",
+				  &error);
+  g_assert (data != NULL);
+  g_assert (error == NULL);
+  size = g_bytes_get_size (data);
+  g_assert (size == 6);
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
+
+  data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
+				  &error);
+  g_assert (data != NULL);
+  g_assert (error == NULL);
+  size = g_bytes_get_size (data);
+  g_assert (size == 6);
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
+
+  data = g_resources_lookup_data ("/sv/test1.txt",
+				  &error);
+  g_assert (data != NULL);
+  g_assert (error == NULL);
+  size = g_bytes_get_size (data);
+  g_assert (size == 6);
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test3\n");
+
+  children = g_resources_enumerate_children ("/not/here",
+					     &error);
+  g_assert (children == NULL);
+  g_assert (error != NULL);
+  g_clear_error (&error);
+
+  children = g_resources_enumerate_children ("/a_prefix",
+					     &error);
+  g_assert (children != NULL);
+  g_assert (error == NULL);
+  g_assert (g_strv_length (children) == 2);
+
+  g_resources_unregister (resource);
+
+  found = g_resources_get_info ("/test1.txt",
+				&size, &flags, &error);
+  g_assert (!found);
+  g_assert (error != NULL);
+  g_clear_error (&error);
+}
+
+static void
+test_resource_automatic (void)
+{
+  GError *error = NULL;
+  gboolean found;
+  gsize size;
+  guint32 flags;
+  GBytes *data;
+
+  found = g_resources_get_info ("/auto_loaded/test1.txt",
+				&size, &flags, &error);
+  g_assert (found);
+  g_assert (error == NULL);
+  g_assert (size == 6);
+  g_assert (flags == 0);
+
+  data = g_resources_lookup_data ("/auto_loaded/test1.txt",
+				  &error);
+  g_assert (data != NULL);
+  g_assert (error == NULL);
+  size = g_bytes_get_size (data);
+  g_assert (size == 6);
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
+}
+
+static void
+test_resource_manual (void)
+{
+  GError *error = NULL;
+  gboolean found;
+  gsize size;
+  guint32 flags;
+  GBytes *data;
+
+  found = g_resources_get_info ("/manual_loaded/test1.txt",
+				&size, &flags, &error);
+  g_assert (found);
+  g_assert (error == NULL);
+  g_assert (size == 6);
+  g_assert (flags == 0);
+
+  data = g_resources_lookup_data ("/manual_loaded/test1.txt",
+				  &error);
+  g_assert (data != NULL);
+  g_assert (error == NULL);
+  size = g_bytes_get_size (data);
+  g_assert (size == 6);
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
+}
+
+int
+main (int   argc,
+      char *argv[])
+{
+  g_type_init ();
+  g_test_init (&argc, &argv, NULL);
+
+  _g_test2_register_resource ();
+
+  g_test_add_func ("/resource/file", test_resource_file);
+  g_test_add_func ("/resource/data", test_resource_data);
+  g_test_add_func ("/resource/registred", test_resource_registred);
+  g_test_add_func ("/resource/manual", test_resource_manual);
+#ifdef G_HAS_CONSTRUCTORS
+  g_test_add_func ("/resource/automatic", test_resource_automatic);
+#endif
+
+  return g_test_run();
+}
diff --git a/gio/tests/test.gresource.xml b/gio/tests/test.gresource.xml
new file mode 100644
index 0000000..c9885e5
--- /dev/null
+++ b/gio/tests/test.gresource.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource>
+    <file compressed="true">test1.txt</file>
+  </gresource>
+  <gresource prefix="/a_prefix">
+    <file alias="test2-alias.txt">test2.txt</file>
+    <file>test2.txt</file>
+  </gresource>
+  <gresource lang="sv">
+    <file alias="test1.txt">test3.txt</file>
+  </gresource>
+</gresources>
diff --git a/gio/tests/test1.txt b/gio/tests/test1.txt
new file mode 100644
index 0000000..a5bce3f
--- /dev/null
+++ b/gio/tests/test1.txt
@@ -0,0 +1 @@
+test1
diff --git a/gio/tests/test2.gresource.xml b/gio/tests/test2.gresource.xml
new file mode 100644
index 0000000..6876280
--- /dev/null
+++ b/gio/tests/test2.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/auto_loaded">
+    <file>test1.txt</file>
+  </gresource>
+</gresources>
diff --git a/gio/tests/test2.txt b/gio/tests/test2.txt
new file mode 100644
index 0000000..180cf83
--- /dev/null
+++ b/gio/tests/test2.txt
@@ -0,0 +1 @@
+test2
diff --git a/gio/tests/test3.gresource.xml b/gio/tests/test3.gresource.xml
new file mode 100644
index 0000000..fdf26fa
--- /dev/null
+++ b/gio/tests/test3.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/manual_loaded">
+    <file>test1.txt</file>
+  </gresource>
+</gresources>
diff --git a/gio/tests/test3.txt b/gio/tests/test3.txt
new file mode 100644
index 0000000..df6b0d2
--- /dev/null
+++ b/gio/tests/test3.txt
@@ -0,0 +1 @@
+test3



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]