gtktestutils.c 4.9 KB
Newer Older
Tim Janik's avatar
Tim Janik committed
1
/* Gtk+ testing utilities
2
3
 * Copyright (C) 2007 Imendio AB
 * Authors: Tim Janik
Tim Janik's avatar
Tim Janik committed
4
5
6
7
8
9
10
11
12
13
14
15
 *
 * 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
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Tim Janik's avatar
Tim Janik committed
17
 */
Matthias Clasen's avatar
Matthias Clasen committed
18

19

20
#include "config.h"
Matthias Clasen's avatar
Matthias Clasen committed
21

22
#include "gtkspinbutton.h"
23
24
25
26
27
28
#include "gtkmain.h"
#include "gtkbox.h"
#include "gtklabel.h"
#include "gtkbutton.h"
#include "gtktextview.h"
#include "gtkrange.h"
Tim Janik's avatar
Tim Janik committed
29
30
31
32
33

#include <locale.h>
#include <string.h>
#include <math.h>

34
35
36
37
38
39
/* This is a hack.
 * We want to include the same headers as gtktypefuncs.c but we are not
 * allowed to include gtkx.h directly during GTK compilation.
 * So....
 */
#undef GTK_COMPILATION
40
#include <gtk/gtk.h>
41
#define GTK_COMPILATION
Tim Janik's avatar
Tim Janik committed
42

43
#include <gsk/ngl/gsknglrenderer.h>
44
45
46
47
48
49
50
51
52

#ifdef GDK_WINDOWING_BROADWAY
#include <gsk/broadway/gskbroadwayrenderer.h>
#endif

#ifdef GDK_RENDERING_VULKAN
#include <gsk/vulkan/gskvulkanrenderer.h>
#endif

53
54
55
56
#ifdef GDK_WINDOWING_X11
#include <gdk/x11/gdkx.h>
#endif

Tim Janik's avatar
Tim Janik committed
57
58
/**
 * gtk_test_init:
59
 * @argcp: Address of the `argc` parameter of the
Matthias Clasen's avatar
Matthias Clasen committed
60
61
62
63
 *   main() function. Changed if any arguments were handled.
 * @argvp: (inout) (array length=argcp): Address of the `argv`
 *   parameter of main(). Any parameters understood by g_test_init()
 *   or gtk_init() are stripped before return.
Matthias Clasen's avatar
Matthias Clasen committed
64
 * @...: currently unused
Matthias Clasen's avatar
Matthias Clasen committed
65
 *
Matthias Clasen's avatar
Matthias Clasen committed
66
 * This function is used to initialize a GTK test program.
Tim Janik's avatar
Tim Janik committed
67
68
 *
 * It will in turn call g_test_init() and gtk_init() to properly
Matthias Clasen's avatar
Matthias Clasen committed
69
 * initialize the testing framework and graphical toolkit. It’ll
70
71
 * also set the program’s locale to “C”. This is done to make test
 * program environments as deterministic as possible.
Matthias Clasen's avatar
Matthias Clasen committed
72
 *
Tim Janik's avatar
Tim Janik committed
73
74
 * Like gtk_init() and g_test_init(), any known arguments will be
 * processed and stripped from @argc and @argv.
Matthias Clasen's avatar
Matthias Clasen committed
75
 */
Tim Janik's avatar
Tim Janik committed
76
77
78
79
80
void
gtk_test_init (int    *argcp,
               char ***argvp,
               ...)
{
81
82
83
84
  /* g_test_init is defined as a macro that aborts if assertions
   * are disabled. We don't want that, so we call the function.
   */
  (g_test_init) (argcp, argvp, NULL);
Tim Janik's avatar
Tim Janik committed
85
  gtk_disable_setlocale();
Benjamin Otte's avatar
Benjamin Otte committed
86
  setlocale (LC_ALL, "en_US.UTF-8");
87

Matthias Clasen's avatar
Matthias Clasen committed
88
  gtk_init ();
Tim Janik's avatar
Tim Janik committed
89
90
}

91
92
93
94
95
static gboolean
quit_main_loop_callback (GtkWidget     *widget,
                         GdkFrameClock *frame_clock,
                         gpointer       user_data)
{
96
97
98
99
100
  gboolean *done = user_data;

  *done = TRUE;

  g_main_context_wakeup (NULL);
101
102
103
104
105
106
107
108

  return G_SOURCE_REMOVE;
}

/**
 * gtk_test_widget_wait_for_draw:
 * @widget: the widget to wait for
 *
Matthias Clasen's avatar
Matthias Clasen committed
109
110
111
112
 * Enters the main loop and waits for @widget to be “drawn”.
 *
 * In this context that means it waits for the frame clock of
 * @widget to have run a full styling, layout and drawing cycle.
113
114
115
116
 *
 * This function is intended to be used for syncing with actions that
 * depend on @widget relayouting or on interaction with the display
 * server.
Matthias Clasen's avatar
Matthias Clasen committed
117
 */
118
119
120
121
void
gtk_test_widget_wait_for_draw (GtkWidget *widget)
{
  g_return_if_fail (GTK_IS_WIDGET (widget));
122
  gboolean done = FALSE;
123
124
125
126
127
128
129

  /* We can do this here because the whole tick procedure does not
   * reenter the main loop. Otherwise we'd need to manually get the
   * frame clock and connect to the after-paint signal.
   */
  gtk_widget_add_tick_callback (widget,
                                quit_main_loop_callback,
130
                                &done,
131
132
                                NULL);

133
134
  while (!done)
    g_main_context_iteration (NULL, TRUE);
135
136
}

137
138
139
140
static GType *all_registered_types = NULL;
static guint  n_all_registered_types = 0;

/**
141
 * gtk_test_list_all_types:
142
143
144
145
 * @n_types: location to store number of types
 *
 * Return the type ids that have been registered after
 * calling gtk_test_register_all_types().
Christian Dywan's avatar
Christian Dywan committed
146
 *
Matthias Clasen's avatar
Matthias Clasen committed
147
148
149
 * Returns: (array length=n_types zero-terminated=1) (transfer none):
 *    0-terminated array of type ids
 */
150
151
152
153
154
155
156
157
const GType*
gtk_test_list_all_types (guint *n_types)
{
  if (n_types)
    *n_types = n_all_registered_types;
  return all_registered_types;
}

Tim Janik's avatar
Tim Janik committed
158
/**
159
 * gtk_test_register_all_types:
Tim Janik's avatar
Tim Janik committed
160
 *
Matthias Clasen's avatar
Matthias Clasen committed
161
162
 * Force registration of all core GTK object types.
 *
Tim Janik's avatar
Tim Janik committed
163
164
165
166
167
168
 * This allowes to refer to any of those object types via
 * g_type_from_name() after calling this function.
 **/
void
gtk_test_register_all_types (void)
{
169
170
171
172
173
174
  if (!all_registered_types)
    {
      const guint max_gtk_types = 999;
      GType *tp;
      all_registered_types = g_new0 (GType, max_gtk_types);
      tp = all_registered_types;
175
#include <gtktypefuncs.inc>
176
177
178
179
      n_all_registered_types = tp - all_registered_types;
      g_assert (n_all_registered_types + 1 < max_gtk_types);
      *tp = 0;
    }
Tim Janik's avatar
Tim Janik committed
180
}