From e24692d332254308966b4682d64f9035d7648198 Mon Sep 17 00:00:00 2001
From: Jeremy Whiting <jeremy.whiting@collabora.com>
Date: Mon, 11 Nov 2019 10:48:28 -0700
Subject: [PATCH] Use -check-gl in graphics test when testing gl.

Also added mock-mixed gl test to test scenario of wflinfo working
but check-gl failing.
---
 steam-runtime-tools/graphics.c | 73 ++++++++++++++++++++++++++++++++++
 tests/graphics.c               | 46 +++++++++++++++++++++
 tests/meson.build              | 35 ++++++++++++++++
 3 files changed, 154 insertions(+)

diff --git a/steam-runtime-tools/graphics.c b/steam-runtime-tools/graphics.c
index 196dbacd3..85f7f1031 100644
--- a/steam-runtime-tools/graphics.c
+++ b/steam-runtime-tools/graphics.c
@@ -561,6 +561,28 @@ _argv_for_check_vulkan (const char *helpers_path,
   return argv;
 }
 
+static GPtrArray *
+_argv_for_check_gl (const char *helpers_path,
+                    SrtTestFlags test_flags,
+                    const char *multiarch_tuple,
+                    GError **error)
+{
+  GPtrArray *argv;
+  SrtHelperFlags flags = SRT_HELPER_FLAGS_TIME_OUT;
+
+  if (test_flags & SRT_TEST_FLAGS_TIME_OUT_SOONER)
+    flags |= SRT_HELPER_FLAGS_TIME_OUT_SOONER;
+
+  argv = _srt_get_helper (helpers_path, multiarch_tuple, "check-gl",
+                          flags, error);
+
+  if (argv == NULL)
+    return NULL;
+
+  g_ptr_array_add (argv, NULL);
+  return argv;
+}
+
 /**
  * _srt_check_library_vendor:
  * @multiarch_tuple: A multiarch tuple to check e.g. i386-linux-gnu
@@ -763,6 +785,57 @@ _srt_check_graphics (const char *helpers_path,
   if (parse_wflinfo)
     {
       issues |= _srt_process_wflinfo (parser, &version_string, &renderer_string);
+
+      if (rendering_interface == SRT_RENDERING_INTERFACE_GL &&
+         window_system == SRT_WINDOW_SYSTEM_GLX)
+        {
+          /* Now perform *-check-gl drawing test */
+          g_ptr_array_unref (argv);
+          g_clear_pointer (&output, g_free);
+
+          argv = _argv_for_check_gl (helpers_path,
+                                     test_flags,
+                                     multiarch_tuple,
+                                     &error);
+
+          if (argv == NULL)
+            {
+              issues |= SRT_GRAPHICS_ISSUES_CANNOT_DRAW;
+              /* Put the error message in the 'messages' */
+              child_stderr2 = g_strdup (error->message);
+              goto out;
+            }
+
+          /* Now run and report exit code/messages if failure */
+          if (!g_spawn_sync (NULL,    /* working directory */
+                             (gchar **) argv->pdata,
+                             my_environ,    /* envp */
+                             G_SPAWN_SEARCH_PATH,       /* flags */
+                             NULL,    /* child setup */
+                             NULL,    /* user data */
+                             &output, /* stdout */
+                             &child_stderr2,
+                             &exit_status,
+                             &error))
+            {
+              g_debug ("An error occurred calling the helper: %s", error->message);
+              issues |= SRT_GRAPHICS_ISSUES_CANNOT_DRAW;
+              goto out;
+            }
+
+          if (exit_status != 0)
+            {
+              g_debug ("... wait status %d", exit_status);
+              issues |= SRT_GRAPHICS_ISSUES_CANNOT_DRAW;
+
+              // TERM signal gives us 124 from timeout man page
+              if (WIFEXITED (exit_status) && WEXITSTATUS (exit_status) == 124) // timeout command killed the helper
+                {
+                  g_debug ("helper killed by timeout command");
+                  issues |= SRT_GRAPHICS_ISSUES_TIMEOUT;
+                }
+            }
+        }
     }
   else
     {
diff --git a/tests/graphics.c b/tests/graphics.c
index 552b5bafb..2fac6cc03 100644
--- a/tests/graphics.c
+++ b/tests/graphics.c
@@ -697,6 +697,50 @@ test_mixed_vulkan (Fixture *f,
   g_object_unref (info);
 }
 
+/*
+ * Test a mock system with gl driver but check-gl failure
+ */
+static void
+test_mixed_gl (Fixture *f,
+               gconstpointer context)
+{
+  SrtGraphics *graphics = NULL;
+  SrtGraphicsIssues issues;
+  gchar *tuple;
+  gchar *renderer;
+  gchar *version;
+
+  SrtSystemInfo *info = srt_system_info_new (NULL);
+  srt_system_info_set_helpers_path (info, f->builddir);
+
+  issues = srt_system_info_check_graphics (info,
+                                           "mock-mixed",
+                                           SRT_WINDOW_SYSTEM_GLX,
+                                           SRT_RENDERING_INTERFACE_GL,
+                                           &graphics);
+  g_assert_cmpint (issues, ==, SRT_GRAPHICS_ISSUES_CANNOT_DRAW);
+  g_assert_cmpstr (srt_graphics_get_renderer_string (graphics), ==,
+                   SRT_TEST_GOOD_GRAPHICS_RENDERER);
+  g_assert_cmpstr (srt_graphics_get_version_string (graphics), ==,
+                   SRT_TEST_GOOD_GRAPHICS_VERSION);
+  g_object_get (graphics,
+                "multiarch-tuple", &tuple,
+                "issues", &issues,
+                "renderer-string", &renderer,
+                "version-string", &version,
+                NULL);
+  g_assert_cmpint (issues, ==, SRT_GRAPHICS_ISSUES_CANNOT_DRAW);
+  g_assert_cmpstr (tuple, ==, "mock-mixed");
+  g_assert_cmpstr (renderer, ==, SRT_TEST_GOOD_GRAPHICS_RENDERER);
+  g_assert_cmpstr (version, ==, SRT_TEST_GOOD_GRAPHICS_VERSION);
+  g_free (tuple);
+  g_free (renderer);
+  g_free (version);
+
+  g_object_unref (graphics);
+  g_object_unref (info);
+}
+
 /*
  * Assert that @icd is internally consistent.
  */
@@ -1535,6 +1579,8 @@ main (int argc,
               setup, test_software_rendering, teardown);
   g_test_add ("/graphics/normalize_window_system", Fixture, NULL,
               setup, test_normalize_window_system, teardown);
+  g_test_add ("/graphics/gl-mixed", Fixture, NULL,
+              setup, test_mixed_gl, teardown);
 
   g_test_add ("/graphics/vulkan", Fixture, NULL,
               setup, test_good_vulkan, teardown);
diff --git a/tests/meson.build b/tests/meson.build
index bfd2354aa..d13afefce 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -177,6 +177,13 @@ executable(
   install_dir: tests_dir
 )
 
+executable(
+  'mock-software-check-gl',
+  join_paths('..', 'helpers', 'true.c'),
+  install: true,
+  install_dir: tests_dir
+)
+  
 executable(
   'mock-good-vulkaninfo',
   'mock-good-vulkaninfo.c',
@@ -225,4 +232,32 @@ executable(
   install_dir: tests_dir
 )
 
+executable(
+  'mock-good-check-gl',
+  join_paths('..', 'helpers', 'true.c'),
+  install: true,
+  install_dir: tests_dir
+)
+
+executable(
+  'mock-bad-check-gl',
+  'mock-bad-check-vulkan.c',
+  install: true,
+  install_dir: tests_dir
+)
+
+executable(
+  'mock-mixed-wflinfo',
+  'mock-good-wflinfo.c',
+  install: true,
+  install_dir: tests_dir
+)
+
+executable(
+  'mock-mixed-check-gl',
+  'mock-bad-check-vulkan.c',
+  install: true,
+  install_dir: tests_dir
+)
+
 # vim:set sw=2 sts=2 et:
-- 
GitLab