[mutter] tests: Make testboxes an actual test



commit 92f4ffc0dd66faa77a96e130562b77c716e26f3b
Author: Jonas Ã…dahl <jadahl gmail com>
Date:   Mon Jul 9 18:42:19 2018 +0200

    tests: Make testboxes an actual test
    
    testboxes was a binary that did unit testing, but it wasn't integrated
    to the test system, so in effect, it was never run. Instead integrate it
    into the other mutter unit tests. This includes changing a few of
    meta_warning()s into g_warning()s so that the GTest framework can handle
    them.

 src/Makefile-tests.am                         |  9 +--
 src/core/boxes.c                              | 14 ++--
 src/{core/testboxes.c => tests/boxes-tests.c} | 97 +++++++++++----------------
 src/tests/boxes-tests.h                       | 23 +++++++
 src/tests/unit-tests.c                        |  2 +
 5 files changed, 76 insertions(+), 69 deletions(-)
---
diff --git a/src/Makefile-tests.am b/src/Makefile-tests.am
index 278d3b048..45b2d6bfe 100644
--- a/src/Makefile-tests.am
+++ b/src/Makefile-tests.am
@@ -48,6 +48,8 @@ mutter_test_unit_tests_SOURCES = \
        tests/test-utils.c \
        tests/test-utils.h \
        tests/unit-tests.c \
+       tests/boxes-tests.c \
+       tests/boxes-tests.h \
        tests/meta-backend-test.c \
        tests/meta-backend-test.h \
        tests/meta-monitor-manager-test.c \
@@ -89,10 +91,3 @@ run-headless-start-test: mutter-test-headless-start-test
 run-tests: run-test-runner-tests run-unit-tests run-headless-start-test
 
 endif
-
-# Some random test programs for bits of the code
-
-testboxes_SOURCES = core/testboxes.c
-testboxes_LDADD = $(MUTTER_LIBS) libmutter-$(LIBMUTTER_API_VERSION).la
-
-noinst_PROGRAMS += testboxes
diff --git a/src/core/boxes.c b/src/core/boxes.c
index 35e9ac3cd..91c9ba12b 100644
--- a/src/core/boxes.c
+++ b/src/core/boxes.c
@@ -437,8 +437,8 @@ merge_spanning_rects_in_region (GList *region)
 
   if (region == NULL)
     {
-      meta_warning ("Region to merge was empty!  Either you have a some "
-                    "pathological STRUT list or there's a bug somewhere!\n");
+      g_warning ("Region to merge was empty!  Either you have a some "
+                 "pathological STRUT list or there's a bug somewhere!\n");
       return NULL;
     }
 
@@ -961,7 +961,7 @@ meta_rectangle_clamp_to_fit_into_region (const GList         *spanning_rects,
   /* Clamp rect appropriately */
   if (best_rect == NULL)
     {
-      meta_warning ("No rect whose size to clamp to found!\n");
+      g_warning ("No rect whose size to clamp to found!\n");
 
       /* If it doesn't fit, at least make it no bigger than it has to be */
       if (!(fixed_directions & FIXED_DIRECTION_X))
@@ -1024,7 +1024,9 @@ meta_rectangle_clip_to_region (const GList         *spanning_rects,
 
   /* Clip rect appropriately */
   if (best_rect == NULL)
-    meta_warning ("No rect to clip to found!\n");
+    {
+      g_warning ("No rect to clip to found!\n");
+    }
   else
     {
       /* Extra precaution with checking fixed direction shouldn't be needed
@@ -1120,7 +1122,9 @@ meta_rectangle_shove_into_region (const GList         *spanning_rects,
 
   /* Shove rect appropriately */
   if (best_rect == NULL)
-    meta_warning ("No rect to shove into found!\n");
+    {
+      g_warning ("No rect to shove into found!\n");
+    }
   else
     {
       /* Extra precaution with checking fixed direction shouldn't be needed
diff --git a/src/core/testboxes.c b/src/tests/boxes-tests.c
similarity index 96%
rename from src/core/testboxes.c
rename to src/tests/boxes-tests.c
index 94da52b6b..bc5d2d0ce 100644
--- a/src/core/testboxes.c
+++ b/src/tests/boxes-tests.c
@@ -19,20 +19,23 @@
  * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "boxes-private.h"
+#include "tests/boxes-tests.h"
+
 #include <glib.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include <X11/Xutil.h> /* Just for the definition of the various gravities */
-#include <time.h>      /* To initialize random seed */
+#include <X11/Xutil.h>
+#include <time.h>
 #include <math.h>
 
+#include "core/boxes-private.h"
+
 #define NUM_RANDOM_RUNS 10000
 
 static void
 init_random_ness (void)
 {
-  srand(time(NULL));
+  srand (time (NULL));
 }
 
 static void
@@ -111,8 +114,6 @@ test_area (void)
 
   temp = meta_rect (0, 0, 5, 7);
   g_assert (meta_rectangle_area (&temp) == 35);
-
-  printf ("%s passed.\n", G_STRFUNC);
 }
 
 static void
@@ -139,8 +140,6 @@ test_intersect (void)
 
   meta_rectangle_intersect (&b, &d, &b);
   g_assert (meta_rectangle_equal (&b, &b_intersect_d));
-
-  printf ("%s passed.\n", G_STRFUNC);
 }
 
 static void
@@ -158,8 +157,6 @@ test_equal (void)
   g_assert (!meta_rectangle_equal (&a, &d));
   g_assert (!meta_rectangle_equal (&a, &e));
   g_assert (!meta_rectangle_equal (&a, &f));
-
-  printf ("%s passed.\n", G_STRFUNC);
 }
 
 static void
@@ -181,8 +178,6 @@ test_overlap_funcs (void)
   g_assert (!meta_rectangle_overlap (&temp1, &temp2));
   g_assert (!meta_rectangle_horiz_overlap (&temp1, &temp2));
   g_assert ( meta_rectangle_vert_overlap (&temp1, &temp2));
-
-  printf ("%s passed.\n", G_STRFUNC);
 }
 
 static void
@@ -219,8 +214,6 @@ test_basic_fitting (void)
   g_assert (!meta_rectangle_contains_rect (&temp1, &temp3));
   g_assert ( meta_rectangle_could_fit_rect (&temp1, &temp3));
   g_assert (!meta_rectangle_could_fit_rect (&temp3, &temp2));
-
-  printf ("%s passed.\n", G_STRFUNC);
 }
 
 static void
@@ -652,16 +645,16 @@ test_regions_okay (void)
   /*************************************************************/
   /* Make sure test region 5 has the right spanning rectangles */
   /*************************************************************/
-  printf ("The next test intentionally causes a warning, "
-          "but it can be ignored.\n");
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
+                         "Region to merge was empty!*");
   region = get_screen_region (5);
+  g_test_assert_expected_messages ();
+
   verify_lists_are_equal (region, NULL);
 
   /* FIXME: Still to do:
    *   - Create random struts and check the regions somehow
    */
-
-  printf ("%s passed.\n", G_STRFUNC);
 }
 
 static void
@@ -704,8 +697,6 @@ test_region_fitting (void)
   g_assert (!meta_rectangle_contained_in_region (region, &rect));
 
   meta_rectangle_free_list_and_elements (region);
-
-  printf ("%s passed.\n", G_STRFUNC);
 }
 
 static void
@@ -760,14 +751,17 @@ test_clamping_to_region (void)
                                            &min_size);
   g_assert (rect.width == 400 && rect.height == 1180);
 
-  printf ("The next test intentionally causes a warning, "
-          "but it can be ignored.\n");
   rect = meta_rect (50, 50, 10000, 10000);
   min_size.width = 600;  min_size.height = 1170;
+
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
+                         "No rect whose size to clamp to found*");
   meta_rectangle_clamp_to_fit_into_region (region,
                                            fixed_directions,
                                            &rect,
                                            &min_size);
+  g_test_assert_expected_messages ();
+
   g_assert (rect.width == 600 && rect.height == 1170);
 
   rect = meta_rect (350, 50, 100, 1100);
@@ -788,20 +782,21 @@ test_clamping_to_region (void)
                                            &min_size);
   g_assert (rect.width == 400 && rect.height == 1100);
 
-  printf ("The next test intentionally causes a warning, "
-          "but it can be ignored.\n");
   rect = meta_rect (300, 70, 999999, 999999);
   min_size.width = 100;  min_size.height = 200;
   fixed_directions = FIXED_DIRECTION_Y;
+
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
+                         "No rect whose size to clamp to found*");
   meta_rectangle_clamp_to_fit_into_region (region,
                                            fixed_directions,
                                            &rect,
                                            &min_size);
+  g_test_assert_expected_messages ();
+
   g_assert (rect.width == 100 && rect.height == 999999);
 
   meta_rectangle_free_list_and_elements (region);
-
-  printf ("%s passed.\n", G_STRFUNC);
 }
 
 static gboolean
@@ -883,8 +878,6 @@ test_clipping_to_region (void)
   g_assert (meta_rectangle_equal (&rect, &temp));
 
   meta_rectangle_free_list_and_elements (region);
-
-  printf ("%s passed.\n", G_STRFUNC);
 }
 
 static void
@@ -953,8 +946,6 @@ test_shoving_into_region (void)
   g_assert (meta_rectangle_equal (&rect, &temp));
 
   meta_rectangle_free_list_and_elements (region);
-
-  printf ("%s passed.\n", G_STRFUNC);
 }
 
 static void
@@ -1133,8 +1124,6 @@ test_find_onscreen_edges (void)
   verify_edge_lists_are_equal (edges, tmp);
   meta_rectangle_free_list_and_elements (tmp);
   meta_rectangle_free_list_and_elements (edges);
-
-  printf ("%s passed.\n", G_STRFUNC);
 }
 
 static void
@@ -1222,8 +1211,6 @@ test_find_nonintersected_monitor_edges (void)
   verify_edge_lists_are_equal (edges, tmp);
   meta_rectangle_free_list_and_elements (tmp);
   meta_rectangle_free_list_and_elements (edges);
-
-  printf ("%s passed.\n", G_STRFUNC);
 }
 
 static void
@@ -1323,8 +1310,6 @@ test_gravity_resize (void)
                                       430,
                                       211);
   g_assert (meta_rectangle_equal (&rect, &temp));
-
-  printf ("%s passed.\n", G_STRFUNC);
 }
 
 #define EPSILON 0.000000001
@@ -1376,35 +1361,33 @@ test_find_closest_point_to_line (void)
                                                   px,  py,
                                                   &rx, &ry);
   g_assert (fabs (rx - answer_x) < EPSILON && fabs (ry - answer_y) < EPSILON);
-
-  printf ("%s passed.\n", G_STRFUNC);
 }
 
-int
-main(void)
+void
+init_boxes_tests (void)
 {
   init_random_ness ();
-  test_area ();
-  test_intersect ();
-  test_equal ();
-  test_overlap_funcs ();
-  test_basic_fitting ();
 
-  test_regions_okay ();
-  test_region_fitting ();
+  g_test_add_func ("/util/boxes/area", test_area);
+  g_test_add_func ("/util/boxes/intersect", test_intersect);
+  g_test_add_func ("/util/boxes/equal", test_equal);
+  g_test_add_func ("/util/boxes/overlap", test_overlap_funcs);
+  g_test_add_func ("/util/boxes/basic-fitting", test_basic_fitting);
 
-  test_clamping_to_region ();
-  test_clipping_to_region ();
-  test_shoving_into_region ();
+  g_test_add_func ("/util/boxes/regions-ok", test_regions_okay);
+  g_test_add_func ("/util/boxes/regions-fitting", test_region_fitting);
+
+  g_test_add_func ("/util/boxes/clamp-to-region", test_clamping_to_region);
+  g_test_add_func ("/util/boxes/clip-to-region", test_clipping_to_region);
+  g_test_add_func ("/util/boxes/shove-into-region", test_shoving_into_region);
 
   /* And now the functions dealing with edges more than boxes */
-  test_find_onscreen_edges ();
-  test_find_nonintersected_monitor_edges ();
+  g_test_add_func ("/util/boxes/onscreen-edges", test_find_onscreen_edges);
+  g_test_add_func ("/util/boxes/nonintersected-monitor-edges",
+                   test_find_nonintersected_monitor_edges);
 
   /* And now the misfit functions that don't quite fit in anywhere else... */
-  test_gravity_resize ();
-  test_find_closest_point_to_line ();
-
-  printf ("All tests passed.\n");
-  return 0;
+  g_test_add_func ("/util/boxes/gravity-resize", test_gravity_resize);
+  g_test_add_func ("/util/boxes/closest-point-to-line",
+                   test_find_closest_point_to_line);
 }
diff --git a/src/tests/boxes-tests.h b/src/tests/boxes-tests.h
new file mode 100644
index 000000000..9f3b778b7
--- /dev/null
+++ b/src/tests/boxes-tests.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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 2 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/>.
+ */
+
+#ifndef BOXES_TESTS_H
+#define BOXES_TESTS_H
+
+void init_boxes_tests (void);
+
+#endif /* BOXES_TESTS_H */
diff --git a/src/tests/unit-tests.c b/src/tests/unit-tests.c
index 59c70d5db..f933ff9ba 100644
--- a/src/tests/unit-tests.c
+++ b/src/tests/unit-tests.c
@@ -28,6 +28,7 @@
 #include "compositor/meta-plugin-manager.h"
 #include "core/boxes-private.h"
 #include "core/main-private.h"
+#include "tests/boxes-tests.h"
 #include "tests/meta-backend-test.h"
 #include "tests/monitor-config-migration-unit-tests.h"
 #include "tests/monitor-unit-tests.h"
@@ -252,6 +253,7 @@ init_tests (int argc, char **argv)
   init_monitor_store_tests ();
   init_monitor_config_migration_tests ();
   init_monitor_tests ();
+  init_boxes_tests ();
 }
 
 int


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