testing.c 9.12 KB
Newer Older
1
/* Gtk+ testing utilities
2 3
 * Copyright (C) 2007 Imendio AB
 * Authors: Tim Janik
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *
 * 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <string.h>
#include <math.h>

/* --- test functions --- */
static void
test_button_clicks (void)
{
  int a = 0, b = 0, c = 0;
  GtkWidget *window = gtk_test_display_button_window ("Test Window",
                                                      "Test: gtk_test_widget_click",
                                                      "IgnoreMe1", &a,
                                                      "ClickMe", &b,
                                                      "IgnoreMe2", &c,
                                                      NULL);
  GtkWidget *button = gtk_test_find_widget (window, "*Click*", GTK_TYPE_BUTTON);
  gboolean simsuccess;
  g_assert (button != NULL);
  simsuccess = gtk_test_widget_click (button, 1, 0);
  g_assert (simsuccess == TRUE);
Matthias Clasen's avatar
2.17.3  
Matthias Clasen committed
41 42
  while (gtk_events_pending ()) {
    g_print ("iterate main loop\n");
43
    gtk_main_iteration ();
Matthias Clasen's avatar
2.17.3  
Matthias Clasen committed
44 45 46 47
  }
  g_assert (a == 0);
  g_assert (b > 0);
  g_assert (c == 0);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
}

static void
test_button_keys (void)
{
  int a = 0, b = 0, c = 0;
  GtkWidget *window = gtk_test_display_button_window ("Test Window",
                                                      "Test: gtk_test_widget_send_key",
                                                      "IgnoreMe1", &a,
                                                      "ClickMe", &b,
                                                      "IgnoreMe2", &c,
                                                      NULL);
  GtkWidget *button = gtk_test_find_widget (window, "*Click*", GTK_TYPE_BUTTON);
  gboolean simsuccess;
  g_assert (button != NULL);
  gtk_widget_grab_focus (button);
64
  g_assert (gtk_widget_has_focus (button));
65
  simsuccess = gtk_test_widget_send_key (button, GDK_KEY_Return, 0);
66 67 68
  g_assert (simsuccess == TRUE);
  while (gtk_events_pending ())
    gtk_main_iteration ();
Matthias Clasen's avatar
2.17.3  
Matthias Clasen committed
69 70 71
  g_assert (a == 0);
  g_assert (b > 0);
  g_assert (c == 0);
72 73 74 75 76
}

static void
test_slider_ranges (void)
{
Javier Jardon's avatar
Javier Jardon committed
77
  GtkWidget *child;
78
  GtkWidget *window = gtk_test_create_simple_window ("Test Window", "Test: gtk_test_warp_slider");
79 80
  GtkWidget *hscale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
                                                -50, +50, 5);
Javier Jardon's avatar
Javier Jardon committed
81 82 83

  child = gtk_bin_get_child (GTK_BIN (window));
  gtk_container_add (GTK_CONTAINER (child), hscale);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  gtk_widget_show (hscale);
  gtk_widget_show_now (window);
  while (gtk_events_pending ())
    gtk_main_iteration ();
  gtk_test_slider_set_perc (hscale, 0.0);
  while (gtk_events_pending ())
    gtk_main_iteration ();
  g_assert (gtk_test_slider_get_value (hscale) == -50);
  gtk_test_slider_set_perc (hscale, 50.0);
  while (gtk_events_pending ())
    gtk_main_iteration ();
  g_assert (fabs (gtk_test_slider_get_value (hscale)) < 0.0001);
  gtk_test_slider_set_perc (hscale, 100.0);
  while (gtk_events_pending ())
    gtk_main_iteration ();
  g_assert (gtk_test_slider_get_value (hscale) == +50.0);
}

static void
test_text_access (void)
{
105
  const int N_WIDGETS = 3;
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  GtkWidget *widgets[N_WIDGETS];
  int i = 0;
  widgets[i++] = gtk_test_create_widget (GTK_TYPE_LABEL, NULL);
  widgets[i++] = gtk_test_create_widget (GTK_TYPE_ENTRY, NULL);
  widgets[i++] = gtk_test_create_widget (GTK_TYPE_TEXT_VIEW, NULL);
  g_assert (i == N_WIDGETS);
  for (i = 0; i < N_WIDGETS; i++)
    gtk_test_text_set (widgets[i], "foobar");
  for (i = 0; i < N_WIDGETS; i++)
    {
      gchar *text  = gtk_test_text_get (widgets[i]);
      g_assert (strcmp (text, "foobar") == 0);
      g_free (text);
    }
  for (i = 0; i < N_WIDGETS; i++)
    gtk_test_text_set (widgets[i], "");
  for (i = 0; i < N_WIDGETS; i++)
    {
      gchar *text  = gtk_test_text_get (widgets[i]);
      g_assert (strcmp (text, "") == 0);
      g_free (text);
    }
}

static void
test_xserver_sync (void)
{
  GtkWidget *window = gtk_test_create_simple_window ("Test Window", "Test: test_xserver_sync");
  GtkWidget *darea = gtk_drawing_area_new ();
Javier Jardon's avatar
Javier Jardon committed
135
  GtkWidget *child;
136
  GdkWindow *gdk_window;
137 138
  GTimer *gtimer = g_timer_new();
  gint sync_is_slower = 0, repeat = 5;
Javier Jardon's avatar
Javier Jardon committed
139 140

  child = gtk_bin_get_child (GTK_BIN (window));
141 142
  //gdk_window = gtk_widget_get_window (darea);

143
  gtk_widget_set_size_request (darea, 320, 200);
Javier Jardon's avatar
Javier Jardon committed
144
  gtk_container_add (GTK_CONTAINER (child), darea);
145 146
  gtk_widget_show (darea);
  gtk_widget_show_now (window);
147

148 149
  while (repeat--)
    {
150
      gint i, many = 200;
151
      double nosync_time, sync_time;
152 153
      cairo_t *cr;

154 155
      while (gtk_events_pending ())
        gtk_main_iteration ();
156 157
      gdk_window = gtk_widget_get_window (darea);
      cr = gdk_cairo_create (gdk_window);
158
      cairo_set_source_rgba (cr, 0, 1, 0, 0.1);
159 160 161 162
      /* run a number of consecutive drawing requests, just using drawing queue */
      g_timer_start (gtimer);
      for (i = 0; i < many; i++)
        {
163
          cairo_paint (cr);
164 165 166 167 168 169 170 171 172 173
        }
      g_timer_stop (gtimer);
      nosync_time = g_timer_elapsed (gtimer, NULL);
      gdk_flush();
      while (gtk_events_pending ())
        gtk_main_iteration ();
      g_timer_start (gtimer);
      /* run a number of consecutive drawing requests with intermediate drawing syncs */
      for (i = 0; i < many; i++)
        {
174
          cairo_paint (cr);
175
          gdk_test_render_sync (gdk_window);
176 177 178 179 180 181 182 183 184 185 186 187
        }
      g_timer_stop (gtimer);
      sync_time = g_timer_elapsed (gtimer, NULL);
      sync_is_slower += sync_time > nosync_time * 1.5;
    }
  g_timer_destroy (gtimer);
  g_assert (sync_is_slower > 0);
}

static void
test_spin_button_arrows (void)
{
Javier Jardon's avatar
Javier Jardon committed
188
  GtkWidget *child;
189 190 191 192
  GtkWidget *window = gtk_test_create_simple_window ("Test Window", "Test: test_spin_button_arrows");
  GtkWidget *spinner = gtk_spin_button_new_with_range (0, 100, 5);
  gboolean simsuccess;
  double oldval, newval;
Javier Jardon's avatar
Javier Jardon committed
193

194 195
  gtk_window_set_has_resize_grip (GTK_WINDOW (window), FALSE);

Javier Jardon's avatar
Javier Jardon committed
196 197
  child = gtk_bin_get_child (GTK_BIN (window));
  gtk_container_add (GTK_CONTAINER (child), spinner);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  gtk_widget_show (spinner);
  gtk_widget_show_now (window);
  gtk_test_slider_set_perc (spinner, 0);
  /* check initial spinner value */
  oldval = gtk_test_slider_get_value (spinner);
  g_assert (oldval == 0);
  /* check simple increment */
  simsuccess = gtk_test_spin_button_click (GTK_SPIN_BUTTON (spinner), 1, TRUE);
  g_assert (simsuccess == TRUE);
  while (gtk_events_pending ()) /* let spin button timeout/idle handlers update */
    gtk_main_iteration ();
  newval = gtk_test_slider_get_value (spinner);
  g_assert (newval > oldval);
  /* check maximum warp */
  simsuccess = gtk_test_spin_button_click (GTK_SPIN_BUTTON (spinner), 3, TRUE);
  g_assert (simsuccess == TRUE);
  while (gtk_events_pending ()) /* let spin button timeout/idle handlers update */
    gtk_main_iteration ();
  oldval = gtk_test_slider_get_value (spinner);
  g_assert (oldval == 100);
  /* check simple decrement */
  oldval = gtk_test_slider_get_value (spinner);
  simsuccess = gtk_test_spin_button_click (GTK_SPIN_BUTTON (spinner), 1, FALSE);
  g_assert (simsuccess == TRUE);
  while (gtk_events_pending ()) /* let spin button timeout/idle handlers update */
    gtk_main_iteration ();
  newval = gtk_test_slider_get_value (spinner);
  g_assert (newval < oldval);
  /* check minimum warp */
  simsuccess = gtk_test_spin_button_click (GTK_SPIN_BUTTON (spinner), 3, FALSE);
  g_assert (simsuccess == TRUE);
  while (gtk_events_pending ()) /* let spin button timeout/idle handlers update */
    gtk_main_iteration ();
  oldval = gtk_test_slider_get_value (spinner);
  g_assert (oldval == 0);
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
static void
test_statusbar_remove_all (void)
{
  GtkWidget *statusbar;

  g_test_bug ("640487");

  statusbar = gtk_statusbar_new ();
  g_object_ref_sink (statusbar);

  gtk_statusbar_push (GTK_STATUSBAR (statusbar), 1, "bla");
  gtk_statusbar_push (GTK_STATUSBAR (statusbar), 1, "bla");
  gtk_statusbar_remove_all (GTK_STATUSBAR (statusbar), 1);

  g_object_unref (statusbar);
}

252 253 254 255 256
int
main (int   argc,
      char *argv[])
{
  gtk_test_init (&argc, &argv);
257
  g_test_bug_base ("http://bugzilla.gnome.org/");
258
  gtk_test_register_all_types();
259 260

  g_test_add_func ("/tests/statusbar-remove-all", test_statusbar_remove_all);
261 262 263 264 265 266
  g_test_add_func ("/ui-tests/text-access", test_text_access);
  g_test_add_func ("/ui-tests/button-clicks", test_button_clicks);
  g_test_add_func ("/ui-tests/keys-events", test_button_keys);
  g_test_add_func ("/ui-tests/slider-ranges", test_slider_ranges);
  g_test_add_func ("/ui-tests/xserver-sync", test_xserver_sync);
  g_test_add_func ("/ui-tests/spin-button-arrows", test_spin_button_arrows);
267

268
  return g_test_run();
269
}