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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* Main wrapper for TreeModel test suite.
 * Copyright (C) 2011  Kristian Rietveld  <kris@gtk.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/>.
 */

#include <ctk/ctk.h>

#include "treemodel.h"

int
main (int    argc,
      char **argv)
{
  ctk_test_init (&argc, &argv, NULL);

  g_test_bug_base ("http://bugzilla.gnome.org/");

  register_list_store_tests ();
  register_tree_store_tests ();
  register_model_ref_count_tests ();
  register_sort_model_tests ();
  register_filter_model_tests ();

  return g_test_run ();
}

/*
 * Signal monitor
 */

static const char *
signal_name_to_string (SignalName signal)
{
  switch (signal)
    {
      case ROW_INSERTED:
          return "row-inserted";

      case ROW_DELETED:
          return "row-deleted";

      case ROW_CHANGED:
          return "row-changed";

      case ROW_HAS_CHILD_TOGGLED:
          return "row-has-child-toggled";

      case ROWS_REORDERED:
          return "rows-reordered";

      default:
          /* Fall through */
          break;
    }

  return "(unknown)";
}

typedef struct
{
  SignalName signal;
  CtkTreePath *path;

  /* For rows-reordered */
  int *new_order;
  int len;
}
Signal;


static Signal *
signal_new (SignalName signal, CtkTreePath *path)
{
  Signal *s;

  s = g_new0 (Signal, 1);
  s->signal = signal;
  s->path = ctk_tree_path_copy (path);
  s->new_order = NULL;

  return s;
}

static Signal *
signal_new_with_order (SignalName signal, CtkTreePath *path,
                       int *new_order, int len)
{
  Signal *s = signal_new (signal, path);

  s->new_order = new_order;
  s->len = len;

  return s;
}

static void
signal_free (Signal *s)
{
  if (s->path)
    ctk_tree_path_free (s->path);

  g_free (s);
}


struct _SignalMonitor
{
  GQueue *queue;
  CtkTreeModel *client;
  gulong signal_ids[LAST_SIGNAL];
};


static void
signal_monitor_generic_handler (SignalMonitor *m,
                                SignalName     signal,
                                CtkTreeModel  *model,
                                CtkTreeIter   *iter,
                                CtkTreePath   *path,
                                int           *new_order)<--- Parameter 'new_order' can be declared as pointer to const
{
  Signal *s;

  if (g_queue_is_empty (m->queue))
    {
      gchar *path_str;

      path_str = ctk_tree_path_to_string (path);
      g_error ("Signal queue empty, got signal %s path %s",
               signal_name_to_string (signal), path_str);
      g_free (path_str);

      g_assert_not_reached ();
    }

  if (m->client != model)
    {
      g_error ("Model mismatch; expected %p, got %p",
               m->client, model);
      g_assert_not_reached ();
    }

  s = g_queue_peek_tail (m->queue);

#if 0
  /* For debugging: output signals that are coming in.  Leaks memory. */
  g_print ("signal=%s path=%s\n", signal_name_to_string (signal),
           ctk_tree_path_to_string (path));
#endif

  if (s->signal != signal ||
      (ctk_tree_path_get_depth (s->path) == 0 &&
       ctk_tree_path_get_depth (path) != 0) ||
      (ctk_tree_path_get_depth (s->path) != 0 &&
       ctk_tree_path_compare (s->path, path) != 0))
    {
      gchar *path_str, *s_path_str;

      s_path_str = ctk_tree_path_to_string (s->path);
      path_str = ctk_tree_path_to_string (path);

      g_error ("Signals don't match; expected signal %s path %s, got signal %s path %s",
               signal_name_to_string (s->signal), s_path_str,
               signal_name_to_string (signal), path_str);

      g_free (s_path_str);
      g_free (path_str);

      g_assert_not_reached ();
    }

  if (signal == ROWS_REORDERED && s->new_order != NULL)
    {
      int i, len;

      g_assert (new_order != NULL);

      len = ctk_tree_model_iter_n_children (model, iter);
      g_assert (s->len == len);

      for (i = 0; i < len; i++)
        g_assert (s->new_order[i] == new_order[i]);
    }

  s = g_queue_pop_tail (m->queue);

  signal_free (s);
}

static void
signal_monitor_row_inserted (CtkTreeModel *model,
                             CtkTreePath  *path,
                             CtkTreeIter  *iter,
                             gpointer      data)
{
  signal_monitor_generic_handler (data, ROW_INSERTED,
                                  model, iter, path, NULL);
}

static void
signal_monitor_row_deleted (CtkTreeModel *model,
                            CtkTreePath  *path,
                            gpointer      data)
{
  signal_monitor_generic_handler (data, ROW_DELETED,
                                  model, NULL, path, NULL);
}

static void
signal_monitor_row_changed (CtkTreeModel *model,
                            CtkTreePath  *path,
                            CtkTreeIter  *iter,
                            gpointer      data)
{
  signal_monitor_generic_handler (data, ROW_CHANGED,
                                  model, iter, path, NULL);
}

static void
signal_monitor_row_has_child_toggled (CtkTreeModel *model,
                                      CtkTreePath  *path,
                                      CtkTreeIter  *iter,
                                      gpointer      data)
{
  signal_monitor_generic_handler (data, ROW_HAS_CHILD_TOGGLED,
                                  model, iter, path, NULL);
}

static void
signal_monitor_rows_reordered (CtkTreeModel *model,
                               CtkTreePath  *path,
                               CtkTreeIter  *iter,
                               gint         *new_order,
                               gpointer      data)
{
  signal_monitor_generic_handler (data, ROWS_REORDERED,
                                  model, iter, path, new_order);
}

SignalMonitor *
signal_monitor_new (CtkTreeModel *client)
{
  SignalMonitor *m;

  m = g_new0 (SignalMonitor, 1);
  m->client = g_object_ref (client);
  m->queue = g_queue_new ();

  m->signal_ids[ROW_INSERTED] = g_signal_connect (client,
                                                  "row-inserted",
                                                  G_CALLBACK (signal_monitor_row_inserted),
                                                  m);
  m->signal_ids[ROW_DELETED] = g_signal_connect (client,
                                                 "row-deleted",
                                                 G_CALLBACK (signal_monitor_row_deleted),
                                                 m);
  m->signal_ids[ROW_CHANGED] = g_signal_connect (client,
                                                 "row-changed",
                                                 G_CALLBACK (signal_monitor_row_changed),
                                                 m);
  m->signal_ids[ROW_HAS_CHILD_TOGGLED] = g_signal_connect (client,
                                                           "row-has-child-toggled",
                                                           G_CALLBACK (signal_monitor_row_has_child_toggled),
                                                           m);
  m->signal_ids[ROWS_REORDERED] = g_signal_connect (client,
                                                    "rows-reordered",
                                                    G_CALLBACK (signal_monitor_rows_reordered),
                                                    m);

  return m;
}

void
signal_monitor_free (SignalMonitor *m)
{
  int i;

  for (i = 0; i < LAST_SIGNAL; i++)
    g_signal_handler_disconnect (m->client, m->signal_ids[i]);

  g_object_unref (m->client);

  if (m->queue)
    g_queue_free (m->queue);

  g_free (m);
}

void
signal_monitor_assert_is_empty (SignalMonitor *m)
{
  g_assert (g_queue_is_empty (m->queue));
}

void
signal_monitor_append_signal_path (SignalMonitor *m,
                                   SignalName     signal,
                                   CtkTreePath   *path)
{
  Signal *s;

  s = signal_new (signal, path);
  g_queue_push_head (m->queue, s);
}

void
signal_monitor_append_signal_reordered (SignalMonitor *m,
                                        SignalName     signal,
                                        CtkTreePath   *path,
                                        int           *new_order,
                                        int            len)
{
  Signal *s;

  s = signal_new_with_order (signal, path, new_order, len);
  g_queue_push_head (m->queue, s);
}

void
signal_monitor_append_signal (SignalMonitor *m,
                              SignalName     signal,
                              const gchar   *path_string)
{
  Signal *s;
  CtkTreePath *path;

  path = ctk_tree_path_new_from_string (path_string);

  s = signal_new (signal, path);
  g_queue_push_head (m->queue, s);

  ctk_tree_path_free (path);
}