Blame SOURCES/0160-tests-Add-some-frame-assembly-unit-tests.patch

73b847
From 7f3216fa9c25a4de000d334411d4ca63aa58dee5 Mon Sep 17 00:00:00 2001
73b847
From: Benjamin Berg <bberg@redhat.com>
73b847
Date: Mon, 9 Dec 2019 11:52:05 +0100
73b847
Subject: [PATCH 160/181] tests: Add some frame assembly unit tests
73b847
73b847
---
73b847
 meson.build                 |   3 +
73b847
 tests/meson.build           |  35 +++++++++-
73b847
 tests/test-fpi-assembling.c | 135 ++++++++++++++++++++++++++++++++++++
73b847
 3 files changed, 171 insertions(+), 2 deletions(-)
73b847
 create mode 100644 tests/test-fpi-assembling.c
73b847
73b847
diff --git a/meson.build b/meson.build
73b847
index c42cf2d..96700f6 100644
73b847
--- a/meson.build
73b847
+++ b/meson.build
73b847
@@ -82,6 +82,9 @@ gio_dep = dependency('gio-unix-2.0', version: '>=' + glib_min_version)
73b847
 gusb_dep = dependency('gusb', version: '>= 0.3.0')
73b847
 mathlib_dep = cc.find_library('m', required: false)
73b847
 
73b847
+# The following dependencies are only used for tests
73b847
+cairo_dep = dependency('cairo', required: false)
73b847
+
73b847
 # Drivers
73b847
 drivers = get_option('drivers').split(',')
73b847
 virtual_drivers = [ 'virtual_image' ]
73b847
diff --git a/tests/meson.build b/tests/meson.build
73b847
index 8ab3791..29ff6af 100644
73b847
--- a/tests/meson.build
73b847
+++ b/tests/meson.build
73b847
@@ -59,6 +59,7 @@ test_utils = static_library('fprint-test-utils',
73b847
 unit_tests = [
73b847
     'fpi-device',
73b847
     'fpi-ssm',
73b847
+    'fpi-assembling',
73b847
 ]
73b847
 
73b847
 if 'virtual_image' in drivers
73b847
@@ -68,11 +69,41 @@ if 'virtual_image' in drivers
73b847
     ]
73b847
 endif
73b847
 
73b847
+unit_tests_deps = { 'fpi-assembling' : [cairo_dep] }
73b847
+
73b847
+test_config = configuration_data()
73b847
+test_config.set_quoted('SOURCE_ROOT', meson.source_root())
73b847
+test_config_h = configure_file(output: 'test-config.h', configuration: test_config)
73b847
+
73b847
 foreach test_name: unit_tests
73b847
+    if unit_tests_deps.has_key(test_name)
73b847
+        missing_deps = false
73b847
+        foreach dep: unit_tests_deps[test_name]
73b847
+            if not dep.found()
73b847
+                missing_deps = true
73b847
+                break
73b847
+            endif
73b847
+        endforeach
73b847
+
73b847
+        if missing_deps
73b847
+            # Create a dummy test that always skips instead
73b847
+            warning('Test @0@ cannot be compiled due to missing dependencies'.format(test_name))
73b847
+            test(test_name,
73b847
+                find_program('sh'),
73b847
+                suite: ['unit-tests'],
73b847
+                args: ['-c', 'exit 77'],
73b847
+            )
73b847
+            continue
73b847
+        endif
73b847
+        extra_deps = unit_tests_deps[test_name]
73b847
+    else
73b847
+        extra_deps = []
73b847
+    endif
73b847
+
73b847
     basename = 'test-' + test_name
73b847
     test_exe = executable(basename,
73b847
-        sources: basename + '.c',
73b847
-        dependencies: libfprint_private_dep,
73b847
+        sources: [basename + '.c', test_config_h],
73b847
+        dependencies: [ libfprint_private_dep ] + extra_deps,
73b847
         c_args: common_cflags,
73b847
         link_with: test_utils,
73b847
     )
73b847
diff --git a/tests/test-fpi-assembling.c b/tests/test-fpi-assembling.c
73b847
new file mode 100644
73b847
index 0000000..c5b1bca
73b847
--- /dev/null
73b847
+++ b/tests/test-fpi-assembling.c
73b847
@@ -0,0 +1,135 @@
73b847
+/*
73b847
+ * Example fingerprint device prints listing and deletion
73b847
+ * Enrolls your right index finger and saves the print to disk
73b847
+ * Copyright (C) 2019 Benjamin Berg <bberg@redhat.com>
73b847
+ *
73b847
+ * This library is free software; you can redistribute it and/or
73b847
+ * modify it under the terms of the GNU Lesser General Public
73b847
+ * License as published by the Free Software Foundation; either
73b847
+ * version 2.1 of the License, or (at your option) any later version.
73b847
+ *
73b847
+ * This library is distributed in the hope that it will be useful,
73b847
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
73b847
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
73b847
+ * Lesser General Public License for more details.
73b847
+ *
73b847
+ * You should have received a copy of the GNU Lesser General Public
73b847
+ * License along with this library; if not, write to the Free Software
73b847
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73b847
+ */
73b847
+
73b847
+#include <glib.h>
73b847
+#include <cairo.h>
73b847
+#include "fpi-assembling.h"
73b847
+#include "fpi-image.h"
73b847
+#include "test-config.h"
73b847
+
73b847
+typedef struct
73b847
+{
73b847
+  struct fpi_frame frame;
73b847
+  cairo_surface_t *surf;
73b847
+  guchar          *data;
73b847
+  guint            stride;
73b847
+  guint            width;
73b847
+  guint            height;
73b847
+  guint            x;
73b847
+  guint            y;
73b847
+} cairo_frame;
73b847
+
73b847
+static unsigned char
73b847
+cairo_get_pixel (struct fpi_frame_asmbl_ctx *ctx,
73b847
+                 struct fpi_frame           *frame,
73b847
+                 unsigned int                x,
73b847
+                 unsigned int                y)
73b847
+{
73b847
+  cairo_frame *c_frame = (void *) frame; /* Indirect cast to avoid alignment warning. */
73b847
+
73b847
+  x = x + c_frame->x;
73b847
+  y = y + c_frame->y;
73b847
+
73b847
+  g_assert (x < c_frame->width);
73b847
+  g_assert (y < c_frame->height);
73b847
+
73b847
+  return c_frame->data[x * 4 + y * c_frame->stride + 1];
73b847
+}
73b847
+
73b847
+static void
73b847
+test_frame_assembling (void)
73b847
+{
73b847
+  g_autofree char *path = NULL;
73b847
+  cairo_surface_t *img = NULL;
73b847
+  int width, height, stride, offset;
73b847
+  int test_height;
73b847
+  guchar *data;
73b847
+  struct fpi_frame_asmbl_ctx ctx = { 0, };
73b847
+
73b847
+  g_autoptr(FpImage) fp_img = NULL;
73b847
+  GSList *frames = NULL;
73b847
+
73b847
+  g_assert_false (SOURCE_ROOT == NULL);
73b847
+  path = g_build_path (G_DIR_SEPARATOR_S, SOURCE_ROOT, "tests", "vfs5011", "capture.png", NULL);
73b847
+
73b847
+  img = cairo_image_surface_create_from_png (path);
73b847
+  data = cairo_image_surface_get_data (img);
73b847
+  width = cairo_image_surface_get_width (img);
73b847
+  height = cairo_image_surface_get_height (img);
73b847
+  stride = cairo_image_surface_get_stride (img);
73b847
+  g_assert_cmpint (cairo_image_surface_get_format (img), ==, CAIRO_FORMAT_RGB24);
73b847
+
73b847
+  ctx.get_pixel = cairo_get_pixel;
73b847
+  ctx.frame_width = width;
73b847
+  ctx.frame_height = 20;
73b847
+  ctx.image_width = width;
73b847
+
73b847
+  offset = 10;
73b847
+  test_height = height - (height - ctx.frame_height) % offset;
73b847
+
73b847
+  /* for now, fixed offset */
73b847
+  for (int y = 0; y + ctx.frame_height < height; y += offset)
73b847
+    {
73b847
+      cairo_frame *frame = g_new0 (cairo_frame, 1);
73b847
+
73b847
+      frame->surf = img;
73b847
+      frame->width = width;
73b847
+      frame->height = height;
73b847
+      frame->stride = stride;
73b847
+      frame->data = data;
73b847
+      frame->x = 0;
73b847
+      frame->y = y;
73b847
+      //frame->y = test_height - ctx.frame_height - y;
73b847
+
73b847
+      frames = g_slist_append (frames, frame);
73b847
+    }
73b847
+  //offset = -offset;
73b847
+
73b847
+  fpi_do_movement_estimation (&ctx, frames);
73b847
+  for (GSList *l = frames->next; l != NULL; l = l->next)
73b847
+    {
73b847
+      cairo_frame * frame = l->data;
73b847
+
73b847
+      g_assert_cmpint (frame->frame.delta_x, ==, 0);
73b847
+      g_assert_cmpint (frame->frame.delta_y, ==, offset);
73b847
+    }
73b847
+
73b847
+  fp_img = fpi_assemble_frames (&ctx, frames);
73b847
+  g_assert_cmpint (fp_img->height, ==, test_height);
73b847
+
73b847
+  /* The FpImage and cairo surface need to be identical in the test area */
73b847
+  for (int y = 0; y < test_height; y++)
73b847
+    for (int x = 0; x < width; x++)
73b847
+      g_assert_cmpint (data[x * 4 + y * stride + 1], ==, fp_img->data[x + y * width]);
73b847
+
73b847
+  g_slist_free_full (frames, g_free);
73b847
+  cairo_surface_destroy (img);
73b847
+  g_assert (1);
73b847
+}
73b847
+
73b847
+int
73b847
+main (int argc, char *argv[])
73b847
+{
73b847
+  g_test_init (&argc, &argv, NULL);
73b847
+
73b847
+  g_test_add_func ("/assembling/frames", test_frame_assembling);
73b847
+
73b847
+  return g_test_run ();
73b847
+}
73b847
-- 
73b847
2.24.1
73b847