Blame tests/stresstest-toolbar.c

Packit 98cdb6
/* stresstest-toolbar.c
Packit 98cdb6
 *
Packit 98cdb6
 * Copyright (C) 2003 Soeren Sandmann <sandmann@daimi.au.dk>
Packit 98cdb6
 *
Packit 98cdb6
 * This library is free software; you can redistribute it and/or
Packit 98cdb6
 * modify it under the terms of the GNU Library General Public
Packit 98cdb6
 * License as published by the Free Software Foundation; either
Packit 98cdb6
 * version 2 of the License, or (at your option) any later version.
Packit 98cdb6
 *
Packit 98cdb6
 * This library is distributed in the hope that it will be useful,
Packit 98cdb6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 98cdb6
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 98cdb6
 * Library General Public License for more details.
Packit 98cdb6
 *
Packit 98cdb6
 * You should have received a copy of the GNU Library General Public
Packit 98cdb6
 * License along with this library; if not, write to the
Packit 98cdb6
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 98cdb6
 * Boston, MA 02111-1307, USA.
Packit 98cdb6
 */
Packit 98cdb6
#undef GTK_DISABLE_DEPRECATED
Packit 98cdb6
#include "config.h"
Packit 98cdb6
#include <gtk/gtk.h>
Packit 98cdb6
Packit 98cdb6
typedef struct _Info Info;
Packit 98cdb6
struct _Info
Packit 98cdb6
{
Packit 98cdb6
  GtkWindow  *window;
Packit 98cdb6
  GtkToolbar *toolbar;
Packit 98cdb6
  gint	      counter;
Packit 98cdb6
};
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
add_random (GtkToolbar *toolbar, gint n)
Packit 98cdb6
{
Packit 98cdb6
  gint position;
Packit 98cdb6
  gchar *label = g_strdup_printf ("Button %d", n);
Packit 98cdb6
Packit 98cdb6
  GtkWidget *widget = gtk_button_new_with_label (label);
Packit 98cdb6
Packit 98cdb6
  g_free (label);
Packit 98cdb6
  gtk_widget_show_all (widget);
Packit 98cdb6
Packit 98cdb6
  if (g_list_length (toolbar->children) == 0)
Packit 98cdb6
    position = 0;
Packit 98cdb6
  else
Packit 98cdb6
    position = g_random_int_range (0, g_list_length (toolbar->children));
Packit 98cdb6
Packit 98cdb6
  gtk_toolbar_insert_widget (toolbar, widget, "Bar", "Baz", position);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
remove_random (GtkToolbar *toolbar)
Packit 98cdb6
{
Packit 98cdb6
  GtkToolbarChild *child;
Packit 98cdb6
  gint position;
Packit 98cdb6
Packit 98cdb6
  if (!toolbar->children)
Packit 98cdb6
    return;
Packit 98cdb6
Packit 98cdb6
  position = g_random_int_range (0, g_list_length (toolbar->children));
Packit 98cdb6
Packit 98cdb6
  child = g_list_nth_data (toolbar->children, position);
Packit 98cdb6
  
Packit 98cdb6
  gtk_container_remove (GTK_CONTAINER (toolbar), child->widget);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static gboolean
Packit 98cdb6
stress_test_old_api (gpointer data)
Packit 98cdb6
{
Packit 98cdb6
  typedef enum {
Packit 98cdb6
    ADD_RANDOM,
Packit 98cdb6
    REMOVE_RANDOM,
Packit 98cdb6
    LAST_ACTION
Packit 98cdb6
  } Action;
Packit 98cdb6
      
Packit 98cdb6
  Info *info = data;
Packit 98cdb6
  Action action;
Packit 98cdb6
  
Packit 98cdb6
  if (info->counter++ == 200)
Packit 98cdb6
    {
Packit 98cdb6
      gtk_main_quit ();
Packit 98cdb6
      return FALSE;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  if (!info->toolbar)
Packit 98cdb6
    {
Packit 98cdb6
      info->toolbar = GTK_TOOLBAR (gtk_toolbar_new ());
Packit 98cdb6
      gtk_container_add (GTK_CONTAINER (info->window),
Packit 98cdb6
			 GTK_WIDGET (info->toolbar));
Packit 98cdb6
      gtk_widget_show (GTK_WIDGET (info->toolbar));
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  if (!info->toolbar->children)
Packit 98cdb6
    {
Packit 98cdb6
      add_random (info->toolbar, info->counter);
Packit 98cdb6
      return TRUE;
Packit 98cdb6
    }
Packit 98cdb6
  else if (g_list_length (info->toolbar->children) > 50)
Packit 98cdb6
    {
Packit 98cdb6
      int i;
Packit 98cdb6
      for (i = 0; i < 25; i++)
Packit 98cdb6
	remove_random (info->toolbar);
Packit 98cdb6
      return TRUE;
Packit 98cdb6
    }
Packit 98cdb6
  
Packit 98cdb6
  action = g_random_int_range (0, LAST_ACTION);
Packit 98cdb6
Packit 98cdb6
  switch (action)
Packit 98cdb6
    {
Packit 98cdb6
    case ADD_RANDOM:
Packit 98cdb6
      add_random (info->toolbar, info->counter);
Packit 98cdb6
      break;
Packit 98cdb6
Packit 98cdb6
    case REMOVE_RANDOM:
Packit 98cdb6
      remove_random (info->toolbar);
Packit 98cdb6
      break;
Packit 98cdb6
      
Packit 98cdb6
    default:
Packit 98cdb6
      g_assert_not_reached();
Packit 98cdb6
      break;
Packit 98cdb6
    }
Packit 98cdb6
  
Packit 98cdb6
  return TRUE;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
gint
Packit 98cdb6
main (gint argc, gchar **argv)
Packit 98cdb6
{
Packit 98cdb6
  Info info;
Packit 98cdb6
  
Packit 98cdb6
  gtk_init (&argc, &argv);
Packit 98cdb6
Packit 98cdb6
  info.toolbar = NULL;
Packit 98cdb6
  info.counter = 0;
Packit 98cdb6
  info.window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
Packit 98cdb6
Packit 98cdb6
  gtk_widget_show (GTK_WIDGET (info.window));
Packit 98cdb6
  
Packit 98cdb6
  gdk_threads_add_idle (stress_test_old_api, &info;;
Packit 98cdb6
Packit 98cdb6
  gtk_widget_show_all (GTK_WIDGET (info.window));
Packit 98cdb6
  
Packit 98cdb6
  gtk_main ();
Packit 98cdb6
Packit 98cdb6
  gtk_widget_destroy (GTK_WIDGET (info.window));
Packit 98cdb6
Packit 98cdb6
  info.toolbar = NULL;
Packit 98cdb6
  info.window = NULL;
Packit 98cdb6
  
Packit 98cdb6
  return 0;
Packit 98cdb6
}