[mutter] context/test: Add helper to run a test session



commit 82d9dda08abf1e11cb193fadf3ae0e0587d40e03
Author: Jonas Ã…dahl <jadahl gmail com>
Date:   Tue Mar 2 15:30:14 2021 +0100

    context/test: Add helper to run a test session
    
    Takes care of setup, starting, running, and terminating, as well as
    running the GLib test suite.
    
    Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1861>

 src/tests/meta-context-test.c | 54 +++++++++++++++++++++++++++++++++++++++++++
 src/tests/meta-context-test.h |  2 ++
 2 files changed, 56 insertions(+)
---
diff --git a/src/tests/meta-context-test.c b/src/tests/meta-context-test.c
index 216c09192c..420aa0ccdd 100644
--- a/src/tests/meta-context-test.c
+++ b/src/tests/meta-context-test.c
@@ -141,6 +141,60 @@ meta_context_test_notify_ready (MetaContext *context)
 {
 }
 
+static gboolean
+run_tests_idle (gpointer user_data)
+{
+  MetaContext *context = user_data;
+
+  if (g_test_run () != 0)
+    {
+      GError *error;
+
+      error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
+                           "One or more tests failed");
+      meta_context_terminate_with_error (context, error);
+    }
+  else
+    {
+      meta_context_terminate (context);
+    }
+
+  return G_SOURCE_REMOVE;
+}
+
+int
+meta_context_test_run_tests (MetaContextTest *context_test)
+{
+  MetaContext *context = META_CONTEXT (context_test);
+  g_autoptr (GError) error = NULL;
+
+  if (!meta_context_setup (context, &error))
+    {
+      g_printerr ("Test case failed to start: %s\n", error->message);
+      return EXIT_FAILURE;
+    }
+
+  if (!meta_context_start (context, &error))
+    {
+      g_printerr ("Test case failed to start: %s\n", error->message);
+      return EXIT_FAILURE;
+    }
+
+  g_idle_add (run_tests_idle, context_test);
+
+  meta_context_notify_ready (context);
+
+  if (!meta_context_run_main_loop (context, &error))
+    {
+      g_printerr ("Test case failed: %s\n", error->message);
+      return EXIT_FAILURE;
+    }
+  else
+    {
+      return EXIT_SUCCESS;
+    }
+}
+
 MetaContext *
 meta_create_test_context (MetaContextTestType type)
 {
diff --git a/src/tests/meta-context-test.h b/src/tests/meta-context-test.h
index f2c7b49280..b7931b075b 100644
--- a/src/tests/meta-context-test.h
+++ b/src/tests/meta-context-test.h
@@ -38,4 +38,6 @@ G_DECLARE_FINAL_TYPE (MetaContextTest, meta_context_test,
 
 MetaContext * meta_create_test_context (MetaContextTestType type);
 
+int meta_context_test_run_tests (MetaContextTest *context_test);
+
 #endif /* META_CONTEXT_TEST_H */


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