summaryrefslogtreecommitdiff
path: root/tests/stresstest-toolbar.c
blob: bab169f76bdf3cdfa352680582699c82c159faca (plain)
1
2
3
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* stresstest-toolbar.c
 *
 * Copyright (C) 2003 Soeren Sandmann <sandmann@daimi.au.dk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */
#include "config.h"
#include <gtk/gtk.h>

typedef struct _Info Info;
struct _Info
{
  GtkWindow  *window;
  GtkToolbar *toolbar;
  gint	      counter;
};

static void
add_random (GtkToolbar *toolbar, gint n)
{
  gint n_items;
  gint position;
  gchar *label = g_strdup_printf ("Button %d", n);

  GtkToolItem *toolitem = gtk_tool_button_new (NULL, label);
  gtk_tool_item_set_tooltip_text (toolitem, "Bar");

  g_free (label);
  gtk_widget_show_all (GTK_WIDGET (toolitem));

  n_items = gtk_toolbar_get_n_items (toolbar);
  if (n_items == 0)
    position = 0;
  else
    position = g_random_int_range (0, n_items);

  gtk_toolbar_insert (toolbar, toolitem, position);
}

static void
remove_random (GtkToolbar *toolbar)
{
  GtkToolItem *tool_item;
  gint n_items;
  gint position;

  n_items = gtk_toolbar_get_n_items (toolbar);

  if (n_items == 0)
    return;

  position = g_random_int_range (0, n_items);

  tool_item = gtk_toolbar_get_nth_item (toolbar, position);

  gtk_container_remove (GTK_CONTAINER (toolbar),
                        GTK_WIDGET (tool_item));
}

static gboolean
stress_test_old_api (gpointer data)
{
  typedef enum {
    ADD_RANDOM,
    REMOVE_RANDOM,
    LAST_ACTION
  } Action;
      
  Info *info = data;
  Action action;
  gint n_items;

  if (info->counter++ == 200)
    {
      gtk_main_quit ();
      return FALSE;
    }

  if (!info->toolbar)
    {
      info->toolbar = GTK_TOOLBAR (gtk_toolbar_new ());
      gtk_container_add (GTK_CONTAINER (info->window),
			 GTK_WIDGET (info->toolbar));
      gtk_widget_show (GTK_WIDGET (info->toolbar));
    }

  n_items = gtk_toolbar_get_n_items (info->toolbar);
  if (n_items == 0)
    {
      add_random (info->toolbar, info->counter);
      return TRUE;
    }
  else if (n_items > 50)
    {
      int i;
      for (i = 0; i < 25; i++)
	remove_random (info->toolbar);
      return TRUE;
    }
  
  action = g_random_int_range (0, LAST_ACTION);

  switch (action)
    {
    case ADD_RANDOM:
      add_random (info->toolbar, info->counter);
      break;

    case REMOVE_RANDOM:
      remove_random (info->toolbar);
      break;
      
    default:
      g_assert_not_reached();
      break;
    }
  
  return TRUE;
}


gint
main (gint argc, gchar **argv)
{
  Info info;
  
  gtk_init ();

  info.toolbar = NULL;
  info.counter = 0;
  info.window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));

  gtk_widget_show (GTK_WIDGET (info.window));
  
  gdk_threads_add_idle (stress_test_old_api, &info);

  gtk_widget_show_all (GTK_WIDGET (info.window));
  
  gtk_main ();

  gtk_widget_destroy (GTK_WIDGET (info.window));

  info.toolbar = NULL;
  info.window = NULL;
  
  return 0;
}