Fully rework the conformance test suite

The current conformance test suite is suboptimal in many ways.

All tests are built into the same binary, which makes adding new tests,
builting tests, and running groups of tests much more awkward than it
needs to be. The first issue, especially, raises the bar of contribution
in a significant way, while the other two take their toll on the
maintainer. All of these changes were introduced back when we had both
Clutter and Cogl tests in tree, and because we were building the test
suite for every single change; since then, Cogl moved out of tree with
all its tests, and we build the conformance test suite only when running
the `check` make target.

This admittedly large-ish commit changes the way the conformance test
suite works, taking advantage of the changes in the GTest API and test
harness.

First of all, all tests are now built separately, using their own test
suite as defined by each separate file. All tests run under the TAP
harness provided by GTest and Automake, to gather a proper report using
the Test Anything Protocol without using the `gtester` harness and the
`gtester-report` script. We also use the Makefile rules provided by GLib
to vastly simplify the build environment for the conformance test suite.

On top of the changes for the build and harness, we also provide new API
for creating and running test suites for Clutter. The API is public,
because the test suite has to use it, but it's minimal and mostly
provides convenience wrappers around GTest that make writing test units
for Clutter easier.

This commit disables all tests in the conformance test suite, as well as
moving the data files outside of the tests/data directory; the next few
commits will re-establish the conformance test suite separately so we
can check that everything works in a reliable way.
This commit is contained in:
Emmanuele Bassi
2013-12-12 14:36:16 +00:00
parent cee38c1672
commit 2a660fa298
49 changed files with 1473 additions and 855 deletions

View File

@ -116,6 +116,7 @@ source_h = \
$(srcdir)/clutter-stage.h \
$(srcdir)/clutter-stage-manager.h \
$(srcdir)/clutter-tap-action.h \
$(srcdir)/clutter-test-utils.h \
$(srcdir)/clutter-texture.h \
$(srcdir)/clutter-text.h \
$(srcdir)/clutter-text-buffer.h \
@ -199,6 +200,7 @@ source_c = \
$(srcdir)/clutter-stage-manager.c \
$(srcdir)/clutter-stage-window.c \
$(srcdir)/clutter-tap-action.c \
$(srcdir)/clutter-test-utils.c \
$(srcdir)/clutter-text.c \
$(srcdir)/clutter-text-buffer.c \
$(srcdir)/clutter-transition-group.c \

View File

@ -0,0 +1,422 @@
#include "config.h"
#include "clutter-test-utils.h"
#include <stdlib.h>
#include <glib-object.h>
#include "clutter-actor.h"
#include "clutter-color.h"
#include "clutter-event.h"
#include "clutter-keysyms.h"
#include "clutter-main.h"
#include "clutter-stage.h"
typedef struct {
ClutterActor *stage;
} ClutterTestEnvironment;
static ClutterTestEnvironment *environ = NULL;
/**
* clutter_test_init:
* @argc:
* @argv:
*
* Initializes the Clutter test environment.
*
* Since: 1.18
*/
void
clutter_test_init (int *argc,
char ***argv)
{
if (G_UNLIKELY (environ != NULL))
g_error ("Attempting to initialize the test suite more than once, "
"aborting...\n");
#ifdef CLUTTER_WINDOWING_X11
/* on X11 backends we need the DISPLAY environment set.
*
* check_windowing_backend() will pre-initialize the Clutter
* backend object.
*/
if (clutter_check_windowing_backend (CLUTTER_WINDOWING_X11))
{
const char *display = g_getenv ("DISPLAY");
if (display == NULL || *display == '\0')
{
g_print ("No DISPLAY environment variable found, but we require a "
"DISPLAY set in order to run the conformance test suite.");
exit (0);
}
}
#endif
/* by explicitly setting CLUTTER_VBLANK to "none" we disable the
* synchronisation, and run the master clock using a 60 fps timer
* instead.
*/
g_setenv ("CLUTTER_VBLANK", "none", FALSE);
g_test_init (argc, argv, NULL);
g_test_bug_base ("https://bugzilla.gnome.org/show_bug.cgi?id=%s");
/* perform the actual initialization */
g_assert (clutter_init (NULL, NULL) == CLUTTER_INIT_SUCCESS);
/* our global state, accessible from each test unit */
environ = g_new0 (ClutterTestEnvironment, 1);
}
/**
* clutter_test_get_stage:
*
* Retrieves the #ClutterStage used for testing.
*
* Return value: (transfer none): the stage used for testing
*
* Since: 1.18
*/
ClutterActor *
clutter_test_get_stage (void)
{
g_assert (environ != NULL);
if (environ->stage == NULL)
{
/* create a stage, and ensure that it goes away at the end */
environ->stage = clutter_stage_new ();
clutter_actor_set_name (environ->stage, "Test Stage");
g_object_add_weak_pointer (G_OBJECT (environ->stage),
(gpointer *) &environ->stage);
}
return environ->stage;
}
typedef struct {
gpointer test_func;
gpointer test_data;
GDestroyNotify test_notify;
} ClutterTestData;
static void
clutter_test_func_wrapper (gconstpointer data_)
{
const ClutterTestData *data = data_;
/* ensure that the previous test state has been cleaned up */
g_assert_null (environ->stage);
if (data->test_data != NULL)
{
GTestDataFunc test_func = data->test_func;
test_func (data->test_data);
}
else
{
GTestFunc test_func = data->test_func;
test_func ();
}
if (data->test_notify != NULL)
data->test_notify (data->test_data);
if (environ->stage != NULL)
{
clutter_actor_destroy (environ->stage);
g_assert_null (environ->stage);
}
}
/**
* clutter_test_add: (skip)
* @test_path:
* @test_func:
*
* Adds a test unit to the Clutter test environment.
*
* See also: g_test_add()
*
* Since: 1.18
*/
void
clutter_test_add (const char *test_path,
GTestFunc test_func)
{
clutter_test_add_data_full (test_path, (GTestDataFunc) test_func, NULL, NULL);
}
/**
* clutter_test_add_data: (skip)
* @test_path:
* @test_func:
* @test_data:
*
* Adds a test unit to the Clutter test environment.
*
* See also: g_test_add_data_func()
*
* Since: 1.18
*/
void
clutter_test_add_data (const char *test_path,
GTestDataFunc test_func,
gpointer test_data)
{
clutter_test_add_data_full (test_path, test_func, test_data, NULL);
}
/**
* clutter_test_add_data_full:
* @test_path:
* @test_func: (scope notified)
* @test_data:
* @test_notify:
*
* Adds a test unit to the Clutter test environment.
*
* See also: g_test_add_data_func_full()
*
* Since: 1.18
*/
void
clutter_test_add_data_full (const char *test_path,
GTestDataFunc test_func,
gpointer test_data,
GDestroyNotify test_notify)
{
ClutterTestData *data;
g_return_if_fail (test_path != NULL);
g_return_if_fail (test_func != NULL);
g_assert (environ != NULL);
data = g_new (ClutterTestData, 1);
data->test_func = test_func;
data->test_data = test_data;
data->test_notify = test_notify;
g_test_add_data_func_full (test_path, data,
clutter_test_func_wrapper,
g_free);
}
/**
* clutter_test_run:
*
* Runs the test suite using the units added by calling
* clutter_test_add().
*
* The typical test suite is composed of a list of functions
* called by clutter_test_run(), for instance:
*
* |[
* static void unit_foo (void) { ... }
*
* static void unit_bar (void) { ... }
*
* static void unit_baz (void) { ... }
*
* int
* main (int argc, char *argv[])
* {
* clutter_test_init (&amp;argc, &amp;argv);
*
* clutter_test_add ("/unit/foo", unit_foo);
* clutter_test_add ("/unit/bar", unit_bar);
* clutter_test_add ("/unit/baz", unit_baz);
*
* return clutter_test_run ();
* }
* ]|
*
* Return value: the exit code for the test suite
*
* Since: 1.18
*/
int
clutter_test_run (void)
{
int res;
g_assert (environ != NULL);
res = g_test_run ();
g_free (environ);
return res;
}
typedef struct {
ClutterActor *stage;
ClutterPoint point;
gpointer result;
guint check_actor : 1;
guint check_color : 1;
guint was_painted : 1;
} ValidateData;
static gboolean
validate_stage (gpointer data_)
{
ValidateData *data = data_;
if (data->check_actor)
{
data->result =
clutter_stage_get_actor_at_pos (CLUTTER_STAGE (data->stage),
CLUTTER_PICK_ALL,
data->point.x,
data->point.y);
}
if (data->check_color)
{
data->result =
clutter_stage_read_pixels (CLUTTER_STAGE (data->stage),
data->point.x,
data->point.y,
1, 1);
}
if (!g_test_verbose ())
{
clutter_actor_hide (data->stage);
data->was_painted = TRUE;
}
return G_SOURCE_REMOVE;
}
static gboolean
on_key_press_event (ClutterActor *stage,
ClutterEvent *event,
gpointer data_)
{
ValidateData *data = data_;
if (data->stage == stage &&
clutter_event_get_key_symbol (event) == CLUTTER_KEY_Escape)
{
clutter_actor_hide (stage);
data->was_painted = TRUE;
}
return CLUTTER_EVENT_PROPAGATE;
}
gboolean
clutter_test_check_actor_at_point (ClutterActor *stage,
const ClutterPoint *point,
ClutterActor *actor,
ClutterActor **result)
{
ValidateData *data;
guint press_id = 0;
g_return_val_if_fail (CLUTTER_IS_STAGE (stage), FALSE);
g_return_val_if_fail (point != NULL, FALSE);
g_return_val_if_fail (CLUTTER_IS_ACTOR (stage), FALSE);
g_return_val_if_fail (result != NULL, FALSE);
data = g_new0 (ValidateData, 1);
data->stage = stage;
data->point = *point;
data->check_actor = TRUE;
if (g_test_verbose ())
{
g_printerr ("Press ESC to close the stage and resume the test\n");
press_id = g_signal_connect (stage, "key-press-event",
G_CALLBACK (on_key_press_event),
data);
}
clutter_actor_show (stage);
clutter_threads_add_repaint_func_full (CLUTTER_REPAINT_FLAGS_POST_PAINT,
validate_stage,
data,
NULL);
while (!data->was_painted)
g_main_context_iteration (NULL, TRUE);
*result = data->result;
if (press_id != 0)
g_signal_handler_disconnect (stage, press_id);
g_free (data);
return *result == actor;
}
gboolean
clutter_test_check_color_at_point (ClutterActor *stage,
const ClutterPoint *point,
const ClutterColor *color,
ClutterColor *result)
{
ValidateData *data;
gboolean retval;
guint8 *buffer;
guint press_id = 0;
g_return_val_if_fail (CLUTTER_IS_STAGE (stage), FALSE);
g_return_val_if_fail (point != NULL, FALSE);
g_return_val_if_fail (color != NULL, FALSE);
g_return_val_if_fail (result != NULL, FALSE);
data = g_new0 (ValidateData, 1);
data->stage = stage;
data->point = *point;
data->check_color = TRUE;
if (g_test_verbose ())
{
g_printerr ("Press ESC to close the stage and resume the test\n");
press_id = g_signal_connect (stage, "key-press-event",
G_CALLBACK (on_key_press_event),
data);
}
clutter_actor_show (stage);
clutter_threads_add_repaint_func_full (CLUTTER_REPAINT_FLAGS_POST_PAINT,
validate_stage,
data,
NULL);
while (!data->was_painted)
g_main_context_iteration (NULL, TRUE);
if (press_id != 0)
g_signal_handler_disconnect (stage, press_id);
buffer = data->result;
clutter_color_init (result, buffer[0], buffer[1], buffer[2], 255);
/* we only check the color channels, so we can't use clutter_color_equal() */
retval = buffer[0] == color->red &&
buffer[1] == color->green &&
buffer[2] == color->blue;
g_free (data->result);
g_free (data);
return retval;
}

View File

@ -0,0 +1,164 @@
/*
* Clutter.
*
* An OpenGL based 'interactive canvas' library.
*
* Copyright (C) 2013 Emmanuele Bassi <ebassi@gnome.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __CLUTTER_TEST_UTILS_H__
#define __CLUTTER_TEST_UTILS_H__
#if !defined(__CLUTTER_H_INSIDE__) && !defined(CLUTTER_COMPILATION)
#error "Only <clutter/clutter.h> can be included directly."
#endif
#include <clutter/clutter-types.h>
#include <clutter/clutter-actor.h>
#include <clutter/clutter-color.h>
G_BEGIN_DECLS
/**
* CLUTTER_TEST_UNIT:
* @path: the GTest path for the test function
* @func: the GTestFunc function
*
* Adds @func at the given @path in the test suite.
*
* Since: 1.18
*/
#define CLUTTER_TEST_UNIT(path,func) \
clutter_test_add (path, func);
/**
* CLUTTER_TEST_SUITE:
* @units: a list of %CLUTTER_TEST_UNIT definitions
*
* Defines the entry point and initializes a Clutter test unit, e.g.:
*
* |[
* CLUTTER_TEST_SUITE (
* CLUTTER_TEST_UNIT ("/foobarize", foobarize)
* CLUTTER_TEST_UNIT ("/bar-enabled", bar_enabled)
* )
* ]|
*
* Expands to:
*
* |[
* int
* main (int argc,
* char *argv[])
* {
* clutter_test_init (&amp;argc, &amp;argv);
*
* clutter_test_add ("/foobarize", foobarize);
* clutter_test_add ("/bar-enabled", bar_enabled);
*
* return clutter_test_run ();
* }
* ]|
*
* Since: 1.18
*/
#define CLUTTER_TEST_SUITE(units) \
int \
main (int argc, char *argv[]) \
{ \
clutter_test_init (&argc, &argv); \
\
{ \
units \
} \
\
return clutter_test_run (); \
}
CLUTTER_AVAILABLE_IN_1_18
void clutter_test_init (int *argc,
char ***argv);
CLUTTER_AVAILABLE_IN_1_18
int clutter_test_run (void);
CLUTTER_AVAILABLE_IN_1_18
void clutter_test_add (const char *test_path,
GTestFunc test_func);
CLUTTER_AVAILABLE_IN_1_18
void clutter_test_add_data (const char *test_path,
GTestDataFunc test_func,
gpointer test_data);
CLUTTER_AVAILABLE_IN_1_18
void clutter_test_add_data_full (const char *test_path,
GTestDataFunc test_func,
gpointer test_data,
GDestroyNotify test_notify);
CLUTTER_AVAILABLE_IN_1_18
ClutterActor * clutter_test_get_stage (void);
#define clutter_test_assert_actor_at_point(stage,point,actor) \
G_STMT_START { \
const ClutterPoint *__p = (point); \
ClutterActor *__actor = (actor); \
ClutterActor *__stage = (stage); \
ClutterActor *__res; \
if (clutter_test_check_actor_at_point (__stage, __p, actor, &__res)) ; else { \
const char *__str1 = clutter_actor_get_name (__actor) != NULL \
? clutter_actor_get_name (__actor) \
: G_OBJECT_TYPE_NAME (__actor); \
const char *__str2 = clutter_actor_get_name (__res) != NULL \
? clutter_actor_get_name (__res) \
: G_OBJECT_TYPE_NAME (__res); \
char *__msg = g_strdup_printf ("assertion failed (actor %s at %.2f,%.2f): found actor %s", \
__str1, __p->x, __p->y, __str2); \
g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \
g_free (__msg); \
} \
} G_STMT_END
#define clutter_test_assert_color_at_point(stage,point,color) \
G_STMT_START { \
const ClutterPoint *__p = (point); \
const ClutterColor *__c = (color); \
ClutterActor *__stage = (stage); \
ClutterColor __res; \
if (clutter_test_check_color_at_point (__stage, __p, __c, &__res)) ; else { \
char *__str1 = clutter_color_to_string (__c); \
char *__str2 = clutter_color_to_string (&__res); \
char *__msg = g_strdup_printf ("assertion failed (color %s at %.2f,%.2f): found color %s", \
__str1, __p->x, __p->y, __str2); \
g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \
g_free (__msg); \
g_free (__str1); \
g_free (__str2); \
} \
} G_STMT_END
CLUTTER_AVAILABLE_IN_1_18
gboolean clutter_test_check_actor_at_point (ClutterActor *stage,
const ClutterPoint *point,
ClutterActor *actor,
ClutterActor **result);
CLUTTER_AVAILABLE_IN_1_18
gboolean clutter_test_check_color_at_point (ClutterActor *stage,
const ClutterPoint *point,
const ClutterColor *color,
ClutterColor *result);
G_END_DECLS
#endif /* __CLUTTER_TEST_UTILS_H__ */

View File

@ -99,6 +99,7 @@
#include "clutter-stage.h"
#include "clutter-stage-manager.h"
#include "clutter-tap-action.h"
#include "clutter-test-utils.h"
#include "clutter-texture.h"
#include "clutter-text.h"
#include "clutter-timeline.h"

View File

@ -1364,6 +1364,14 @@ clutter_table_layout_set_span
clutter_table_layout_set_use_animations
clutter_tap_action_get_type
clutter_tap_action_new
clutter_test_add
clutter_test_add_data
clutter_test_add_data_full
clutter_test_check_actor_at_point
clutter_test_check_color_at_point
clutter_test_get_stage
clutter_test_init
clutter_test_run
clutter_texture_get_base_size
clutter_texture_get_cogl_texture
clutter_texture_get_cogl_material