[retro-gtk/wip/aplazas/test: 3/3] tests and fixes



commit c181db2cbd2c3af9d7d665ce2482562e54e04803
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Sun Nov 5 08:26:08 2017 +0100

    tests and fixes

 retro-gtk/retro-core-descriptor.c        |  122 ++++++++++++++++++------
 retro-gtk/retro-core-descriptor.h        |    6 +-
 tests/meson.build                        |   10 ++-
 tests/nestopia.libretro                  |   21 ++++
 tests/test-core-descriptor.c             |  153 ++++++++++++++++++++++++++++++
 tests/test-core-descriptor.gresource.xml |    6 +
 6 files changed, 285 insertions(+), 33 deletions(-)
---
diff --git a/retro-gtk/retro-core-descriptor.c b/retro-gtk/retro-core-descriptor.c
index 1759e6c..3c97990 100644
--- a/retro-gtk/retro-core-descriptor.c
+++ b/retro-gtk/retro-core-descriptor.c
@@ -5,7 +5,7 @@
 struct _RetroCoreDescriptor
 {
   GObject parent_instance;
-  gchar *filename;
+  gchar *uri;
   GKeyFile *key_file;
 };
 
@@ -49,7 +49,7 @@ retro_core_descriptor_finalize (GObject *object)
 {
   RetroCoreDescriptor *self = RETRO_CORE_DESCRIPTOR (object);
 
-  g_free (self->filename);
+  g_free (self->uri);
   g_key_file_unref (self->key_file);
 
   G_OBJECT_CLASS (retro_core_descriptor_parent_class)->finalize (object);
@@ -219,7 +219,7 @@ retro_core_descriptor_check_has_required_key (RetroCoreDescriptor  *self,
                  RETRO_CORE_DESCRIPTOR_ERROR_REQUIRED_KEY_NOT_FOUND,
                  "%s isn't a valid Libretro core descriptor: "
                  "required key %s not found in group [%s].",
-                 self->filename,
+                 self->uri,
                  key,
                  group);
 }
@@ -326,7 +326,7 @@ retro_core_descriptor_check_platform_group (RetroCoreDescriptor  *self,
                    RETRO_CORE_DESCRIPTOR_ERROR_FIRMWARE_NOT_FOUND,
                    "%s isn't a valid Libretro core descriptor:"
                    "[%s] mentioned in [%s] not found.",
-                   self->filename,
+                   self->uri,
                    firmware_group,
                    group);
 
@@ -414,16 +414,9 @@ retro_core_descriptor_has_icon (RetroCoreDescriptor  *self,
 gchar *
 retro_core_descriptor_get_uri (RetroCoreDescriptor *self)
 {
-  gchar *result;
-  GFile *file;
-
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
 
-  file = g_file_new_for_path (self->filename);
-  result = g_file_get_uri (file);
-  g_object_unref (file);
-
-  return result;
+  return g_strdup (self->uri);
 }
 
 /**
@@ -439,7 +432,8 @@ retro_core_descriptor_get_id (RetroCoreDescriptor *self)
 {
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
 
-  return g_path_get_basename (self->filename);
+  // FIXME???
+  return g_path_get_basename (self->uri);
 }
 
 /**
@@ -525,6 +519,8 @@ gchar *
 retro_core_descriptor_get_name (RetroCoreDescriptor  *self,
                                 GError              **error)
 {
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
+
   return g_key_file_get_string (self->key_file,
                                 RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
                                 RETRO_CORE_DESCRIPTOR_NAME_KEY,
@@ -575,7 +571,7 @@ retro_core_descriptor_get_icon (RetroCoreDescriptor  *self,
  *
  * Returns: (nullable) (transfer full): a string or %NULL, free it with g_free()
  */
-char *
+gchar *
 retro_core_descriptor_get_module (RetroCoreDescriptor  *self,
                                   GError              **error)
 {
@@ -608,7 +604,7 @@ retro_core_descriptor_get_module_file (RetroCoreDescriptor  *self,
 
   g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
 
-  file = g_file_new_for_path (self->filename);
+  file = g_file_new_for_uri (self->uri);
   dir = g_file_get_parent (file);
   g_object_unref (file);
   if (dir == NULL)
@@ -651,8 +647,8 @@ retro_core_descriptor_has_platform (RetroCoreDescriptor *self,
                                     const gchar         *platform)
 {
   return has_group_prefixed (self,
-                           RETRO_CORE_DESCRIPTOR_PLATFORM_GROUP_PREFIX,
-                           platform);
+                             RETRO_CORE_DESCRIPTOR_PLATFORM_GROUP_PREFIX,
+                             platform);
 }
 
 /**
@@ -932,8 +928,8 @@ retro_core_descriptor_get_platform_supports_mime_types (RetroCoreDescriptor  *se
 }
 
 /**
- * retro_core_descriptor_new:
- * @filename: the file name of the core descriptor
+ * retro_core_descriptor_new_for_uri:
+ * @uri: the URI of the core descriptor
  * @error: return location for a #GError, or %NULL
  *
  * Creates a new #RetroCoreDescriptor.
@@ -941,31 +937,58 @@ retro_core_descriptor_get_platform_supports_mime_types (RetroCoreDescriptor  *se
  * Returns: (transfer full): a new #RetroCoreDescriptor
  */
 RetroCoreDescriptor *
-retro_core_descriptor_new (const gchar  *filename,
-                           GError      **error)
+retro_core_descriptor_new_for_uri (const gchar  *uri,
+                                   GError      **error)
 {
   RetroCoreDescriptor *self;
+  GFile *file;
+  GFileInputStream *input_stream;
+  GBytes *content;
+  GKeyFile *key_file;
   gchar ** groups;
   gsize groups_length;
   gsize i;
   GError *tmp_error = NULL;
 
-  g_return_val_if_fail (filename != NULL, NULL);
+  g_return_val_if_fail (uri != NULL, NULL);
 
-  self =  g_object_new (RETRO_TYPE_CORE_DESCRIPTOR, NULL);
-  self->filename = g_strdup (filename);
-  self->key_file = g_key_file_new ();
-  g_key_file_load_from_file (self->key_file,
-                             filename,
-                             G_KEY_FILE_NONE,
-                             &tmp_error);
+  file = g_file_new_for_uri (uri);
+  input_stream = g_file_read (file, NULL, &tmp_error);
   if (G_UNLIKELY (tmp_error != NULL)) {
-    g_object_unref (self);
+    g_object_unref (file);
+    g_propagate_error (error, tmp_error);
+
+    return NULL;
+  }
+
+  g_object_unref (file);
+
+  content = g_input_stream_read_bytes (G_INPUT_STREAM (input_stream), 4096, NULL, &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_object_unref (input_stream);
     g_propagate_error (error, tmp_error);
 
     return NULL;
   }
 
+  g_object_unref (input_stream);
+
+  key_file = g_key_file_new ();
+  g_key_file_load_from_bytes (key_file, content, G_KEY_FILE_NONE, &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_bytes_unref (content);
+    g_key_file_unref (key_file);
+    g_propagate_error (error, tmp_error);
+
+    return NULL;
+  }
+
+  g_bytes_unref (content);
+
+  self =  g_object_new (RETRO_TYPE_CORE_DESCRIPTOR, NULL);
+  self->uri = g_strdup (uri);
+  self->key_file = key_file;
+
   retro_core_descriptor_check_libretro_group (self, &tmp_error);
   if (G_UNLIKELY (tmp_error != NULL)) {
     g_object_unref (self);
@@ -1004,3 +1027,42 @@ retro_core_descriptor_new (const gchar  *filename,
 
   return self;
 }
+
+/**
+ * retro_core_descriptor_new:
+ * @filename: the file name of the core descriptor
+ * @error: return location for a #GError, or %NULL
+ *
+ * Creates a new #RetroCoreDescriptor.
+ *
+ * Returns: (transfer full): a new #RetroCoreDescriptor
+ */
+RetroCoreDescriptor *
+retro_core_descriptor_new (const gchar  *filename,
+                           GError      **error)
+{
+  RetroCoreDescriptor *self;
+  gchar *uri;
+  GError *tmp_error = NULL;
+
+  g_return_val_if_fail (filename != NULL, NULL);
+
+  uri = g_filename_to_uri (filename, NULL, &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return NULL;
+  }
+
+  self = retro_core_descriptor_new_for_uri (uri, &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+    g_free (uri);
+
+    return NULL;
+  }
+
+  g_free (uri);
+
+  return self;
+}
diff --git a/retro-gtk/retro-core-descriptor.h b/retro-gtk/retro-core-descriptor.h
index ae1663d..500601a 100644
--- a/retro-gtk/retro-core-descriptor.h
+++ b/retro-gtk/retro-core-descriptor.h
@@ -18,6 +18,8 @@ G_DECLARE_FINAL_TYPE (RetroCoreDescriptor, retro_core_descriptor, RETRO, CORE_DE
 
 RetroCoreDescriptor *retro_core_descriptor_new (const gchar  *filename,
                                                 GError      **error);
+RetroCoreDescriptor *retro_core_descriptor_new_for_uri (const gchar  *uri,
+                                                        GError      **error);
 gboolean retro_core_descriptor_has_icon (RetroCoreDescriptor  *self,
                                          GError              **error);
 gchar *retro_core_descriptor_get_uri (RetroCoreDescriptor *self);
@@ -26,11 +28,11 @@ gboolean retro_core_descriptor_get_is_game (RetroCoreDescriptor  *self,
                                             GError              **error);
 gboolean retro_core_descriptor_get_is_emulator (RetroCoreDescriptor  *self,
                                                 GError              **error);
-char *retro_core_descriptor_get_name (RetroCoreDescriptor  *self,
+gchar *retro_core_descriptor_get_name (RetroCoreDescriptor  *self,
                                       GError              **error);
 GIcon *retro_core_descriptor_get_icon (RetroCoreDescriptor  *self,
                                        GError              **error);
-char *retro_core_descriptor_get_module (RetroCoreDescriptor  *self,
+gchar *retro_core_descriptor_get_module (RetroCoreDescriptor  *self,
                                         GError              **error);
 GFile *retro_core_descriptor_get_module_file (RetroCoreDescriptor  *self,
                                               GError              **error);
diff --git a/tests/meson.build b/tests/meson.build
index b1ba758..80ffe23 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,10 +1,18 @@
+test_core_descriptor_resources = gnome.compile_resources(
+  'test_core_descriptor_resources',
+  'test-core-descriptor.gresource.xml',
+  c_name: 'test',
+  source_dir: '.',
+)
+
 tests = [
+  ['RetroCoreDescriptor', 'test-core-descriptor', [test_core_descriptor_resources[0]]],
 ]
 
 foreach t : tests
   test_display_name = t.get(0)
   test_name = t.get(1)
-  test_srcs = ['@0@.c'.format(test_name)]
+  test_srcs = ['@0@.c'.format(test_name)] + t.get(2, [])
 
   test_exe = executable(test_display_name, test_srcs,
     c_args: retro_gtk_c_args,
diff --git a/tests/nestopia.libretro b/tests/nestopia.libretro
new file mode 100644
index 0000000..d160c05
--- /dev/null
+++ b/tests/nestopia.libretro
@@ -0,0 +1,21 @@
+[Libretro]
+Type=Emulator
+Version=1.0
+Name=Nestopia UE
+Module=nestopia_libretro.so
+LibretroVersion=1
+Authors=Martin Freij;R. Belmont;R. Danbrook;
+License=GPL-2.0+;
+
+[Platform:NintendoEntertainmentSystem]
+MimeType=application/x-nes-rom;
+
+[Platform:FamicomDiskSystem]
+MimeType=application/x-fds-disk;
+Firmwares=FamicomDiskSystem;
+
+[Firmware:FamicomDiskSystem]
+Path=disksys.rom
+MD5=ca30b50f880eb660a320674ed365ef7a
+SHA-512=d7692af63b107b7e79ccf0d90e5ff7e70325e3196a41b6d4d83aaf8ffa5ca976351f8f31bcb1d497c02f7c699e94e77c27a0813f72aa96b576685acaae5bdad5
+Mandatory=true
diff --git a/tests/test-core-descriptor.c b/tests/test-core-descriptor.c
new file mode 100644
index 0000000..4ed8db7
--- /dev/null
+++ b/tests/test-core-descriptor.c
@@ -0,0 +1,153 @@
+/* test-core-descriptor.c
+ *
+ * Copyright (C) 2017 Adrien Plazas <kekun plazas laposte net>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "../retro-gtk/retro-core-descriptor.h"
+
+#define NESTOPIA_DESCRIPTOR_URI "resource:///org/gnome/Retro/TestCoreDescriptor/nestopia.libretro"
+#define NES_PLATFORM "NintendoEntertainmentSystem"
+#define FDS_PLATFORM "FamicomDiskSystem"
+#define NES_MIME_TYPE "application/x-nes-rom"
+#define FDS_MIME_TYPE "application/x-fds-disk"
+
+static const gchar * const NES_MIME_TYPES[] = { NES_MIME_TYPE, NULL };
+static const gchar * const FDS_MIME_TYPES[] = { FDS_MIME_TYPE, NULL };
+
+static void
+test_empty (void)
+{
+  RetroCoreDescriptor *descriptor;
+  GError *error = NULL;
+
+  descriptor = retro_core_descriptor_new_for_uri ("", &error);
+  g_assert_nonnull (error);
+  g_error_free (error);
+
+  g_assert_null (descriptor);
+}
+
+static void
+test_has_platform (void)
+{
+  RetroCoreDescriptor *descriptor;
+  GError *error = NULL;
+
+  descriptor = retro_core_descriptor_new_for_uri (NESTOPIA_DESCRIPTOR_URI,
+                                                  &error);
+  g_assert_no_error (error);
+  g_assert_nonnull (descriptor);
+  g_assert_true (RETRO_IS_CORE_DESCRIPTOR (descriptor));
+
+  g_assert_true (retro_core_descriptor_has_platform (descriptor, NES_PLATFORM));
+  g_assert_true (retro_core_descriptor_has_platform (descriptor, FDS_PLATFORM));
+  g_assert_false (retro_core_descriptor_has_platform (descriptor, "dummy"));
+
+  g_object_unref (descriptor);
+}
+
+static void
+test_get_name (void)
+{
+  RetroCoreDescriptor *descriptor;
+  gchar *name;
+  GError *error = NULL;
+
+  descriptor = retro_core_descriptor_new_for_uri (NESTOPIA_DESCRIPTOR_URI,
+                                                  &error);
+  g_assert_no_error (error);
+  g_assert_nonnull (descriptor);
+  g_assert_true (RETRO_IS_CORE_DESCRIPTOR (descriptor));
+
+  name = retro_core_descriptor_get_name (descriptor, &error);
+  g_assert_no_error (error);
+  g_assert_cmpstr (name, ==, "Nestopia UE");
+  g_free (name);
+
+  g_object_unref (descriptor);
+}
+
+static void
+test_get_mime_type (void)
+{
+  RetroCoreDescriptor *descriptor;
+  gchar **mime_type;
+  gsize length = 0;
+  GError *error = NULL;
+
+  descriptor = retro_core_descriptor_new_for_uri (NESTOPIA_DESCRIPTOR_URI,
+                                                  &error);
+  g_assert_no_error (error);
+  g_assert_nonnull (descriptor);
+  g_assert_true (RETRO_IS_CORE_DESCRIPTOR (descriptor));
+
+  mime_type = retro_core_descriptor_get_mime_type (descriptor, NES_PLATFORM, &length, &error);
+  g_assert_no_error (error);
+  g_assert_cmpuint (length, ==, 1);
+  g_assert_nonnull (mime_type);
+  g_assert_cmpstr (mime_type[0], ==, NES_MIME_TYPE);
+  g_assert_null (mime_type[1]);
+  g_strfreev (mime_type);
+
+  mime_type = retro_core_descriptor_get_mime_type (descriptor, FDS_PLATFORM, &length, &error);
+  g_assert_no_error (error);
+  g_assert_cmpuint (length, ==, 1);
+  g_assert_nonnull (mime_type);
+  g_assert_cmpstr (mime_type[0], ==, FDS_MIME_TYPE);
+  g_assert_null (mime_type[1]);
+  g_strfreev (mime_type);
+
+  g_object_unref (descriptor);
+}
+
+static void
+test_get_platform_supports_mime_types (void)
+{
+  RetroCoreDescriptor *descriptor;
+  gboolean supports;
+  GError *error = NULL;
+
+  descriptor = retro_core_descriptor_new_for_uri (NESTOPIA_DESCRIPTOR_URI,
+                                                  &error);
+  g_assert_no_error (error);
+  g_assert_nonnull (descriptor);
+  g_assert_true (RETRO_IS_CORE_DESCRIPTOR (descriptor));
+
+  supports = retro_core_descriptor_get_platform_supports_mime_types (descriptor, NES_PLATFORM, 
NES_MIME_TYPES, &error);
+  g_assert_no_error (error);
+  g_assert_true (supports);
+
+  supports = retro_core_descriptor_get_platform_supports_mime_types (descriptor, FDS_PLATFORM, 
FDS_MIME_TYPES, &error);
+  g_assert_no_error (error);
+  g_assert_true (supports);
+
+  g_object_unref (descriptor);
+}
+
+int
+main (int   argc,
+      char *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/RetroCoreDescriptor/test_empty", test_empty);
+  g_test_add_func ("/RetroCoreDescriptor/test_has_platform", test_has_platform);
+  g_test_add_func ("/RetroCoreDescriptor/test_get_name", test_get_name);
+  g_test_add_func ("/RetroCoreDescriptor/test_get_mime_type", test_get_mime_type);
+  g_test_add_func ("/RetroCoreDescriptor/test_get_platform_supports_mime_types", 
test_get_platform_supports_mime_types);
+
+  return g_test_run();
+}
diff --git a/tests/test-core-descriptor.gresource.xml b/tests/test-core-descriptor.gresource.xml
new file mode 100644
index 0000000..fb9957a
--- /dev/null
+++ b/tests/test-core-descriptor.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/gnome/Retro/TestCoreDescriptor">
+    <file>nestopia.libretro</file>
+  </gresource>
+</gresources>


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