File: test-notebook.c

package info (click to toggle)
tepl 6.4.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,692 kB
  • sloc: ansic: 16,879; xml: 542; sh: 20; makefile: 9
file content (282 lines) | stat: -rw-r--r-- 8,475 bytes parent folder | download | duplicates (4)
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
/* SPDX-FileCopyrightText: 2017 - Sébastien Wilmet <swilmet@gnome.org>
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include <tepl/tepl.h>

static void
check_list_equal (GList *expected_list,
		  GList *received_list)
{
	GList *l1;
	GList *l2;

	g_assert_cmpint (g_list_length (expected_list), ==, g_list_length (received_list));

	for (l1 = expected_list, l2 = received_list;
	     l1 != NULL && l2 != NULL;
	     l1 = l1->next, l2 = l2->next)
	{
		gpointer expected_data = l1->data;
		gpointer received_data = l2->data;

		g_assert_true (expected_data == received_data);
	}

	g_assert_true (l1 == NULL);
	g_assert_true (l2 == NULL);
}

static void
test_tab_group_basic (void)
{
	GtkNotebook *notebook;
	TeplTabGroup *tab_group;
	GtkWidget *other_widget;
	TeplTab *tab1;
	TeplTab *tab2;
	GList *expected_list;
	GList *received_list;

	notebook = GTK_NOTEBOOK (tepl_notebook_new ());
	tab_group = TEPL_TAB_GROUP (notebook);
	g_object_ref_sink (notebook);

	gtk_widget_show (GTK_WIDGET (notebook));

	/* Empty */
	g_assert_true (tepl_tab_group_get_tabs (tab_group) == NULL);
	g_assert_true (tepl_tab_group_get_active_tab (tab_group) == NULL);

	/* One child, but not a TeplTab */
	other_widget = gtk_grid_new ();
	gtk_widget_show (other_widget);
	gtk_notebook_append_page (notebook, other_widget, NULL);

	g_assert_cmpint (gtk_notebook_get_n_pages (notebook), ==, 1);
	g_assert_true (tepl_tab_group_get_tabs (tab_group) == NULL);
	g_assert_true (tepl_tab_group_get_active_tab (tab_group) == NULL);

	/* Append one TeplTab */
	tab1 = tepl_tab_new ();
	gtk_widget_show (GTK_WIDGET (tab1));
	tepl_tab_group_append_tab (tab_group, tab1, TRUE);
	expected_list = g_list_append (NULL, tab1);

	g_assert_cmpint (gtk_notebook_get_n_pages (notebook), ==, 2);
	g_assert_true (tepl_tab_group_get_active_tab (tab_group) == tab1);

	received_list = tepl_tab_group_get_tabs (tab_group);
	check_list_equal (expected_list, received_list);
	g_list_free (received_list);
	received_list = NULL;

	gtk_notebook_set_current_page (notebook, 0);
	g_assert_true (tepl_tab_group_get_active_tab (tab_group) == NULL);

	received_list = tepl_tab_group_get_tabs (tab_group);
	check_list_equal (expected_list, received_list);
	g_list_free (received_list);
	received_list = NULL;

	/* Append a second TeplTab */
	tab2 = tepl_tab_new ();
	gtk_widget_show (GTK_WIDGET (tab2));
	tepl_tab_group_append_tab (tab_group, tab2, FALSE);
	expected_list = g_list_append (expected_list, tab2);

	gtk_notebook_set_current_page (notebook, gtk_notebook_page_num (notebook, other_widget));
	g_assert_true (tepl_tab_group_get_active_tab (tab_group) == NULL);

	tepl_tab_group_set_active_tab (tab_group, tab1);
	g_assert_true (tepl_tab_group_get_active_tab (tab_group) == tab1);

	tepl_tab_group_set_active_tab (tab_group, tab2);
	g_assert_true (tepl_tab_group_get_active_tab (tab_group) == tab2);

	received_list = tepl_tab_group_get_tabs (tab_group);
	check_list_equal (expected_list, received_list);
	g_list_free (received_list);
	received_list = NULL;

	/* Move tab2 before tab1 */
	gtk_notebook_reorder_child (notebook, GTK_WIDGET (tab2), 0);
	expected_list = g_list_reverse (expected_list);

	received_list = tepl_tab_group_get_tabs (tab_group);
	check_list_equal (expected_list, received_list);
	g_list_free (received_list);
	received_list = NULL;

	g_list_free (expected_list);
	g_object_unref (notebook);
}

typedef struct
{
	gint active_tab_notify_delta_count;

	/* Must be always equal to active_tab_notify_delta_count. */
	gint active_view_notify_delta_count;

	gint active_buffer_notify_delta_count;
} NotifyDeltaCounters;

static void
check_notify_delta_counters (NotifyDeltaCounters *delta_counters,
			     gint                 expected_tab_delta,
			     gint                 expected_buffer_delta)
{
	g_assert_cmpint (delta_counters->active_tab_notify_delta_count, ==, expected_tab_delta);
	g_assert_cmpint (delta_counters->active_view_notify_delta_count, ==, expected_tab_delta);
	g_assert_cmpint (delta_counters->active_buffer_notify_delta_count, ==, expected_buffer_delta);

	delta_counters->active_tab_notify_delta_count = 0;
	delta_counters->active_view_notify_delta_count = 0;
	delta_counters->active_buffer_notify_delta_count = 0;
}

static void
notify_cb (TeplTabGroup *tab_group,
	   GParamSpec   *pspec,
	   gint         *counter)
{
	(*counter)++;
}

static void
change_buffer (TeplTab *tab)
{
	TeplView *view;
	TeplBuffer *new_buffer;

	view = tepl_tab_get_view (tab);
	new_buffer = tepl_buffer_new ();
	gtk_text_view_set_buffer (GTK_TEXT_VIEW (view), GTK_TEXT_BUFFER (new_buffer));
	g_object_unref (new_buffer);
}

static void
test_tab_group_notify_signals (void)
{
	GtkNotebook *notebook;
	TeplTabGroup *tab_group;
	TeplTab *tab1;
	TeplTab *tab2;
	TeplTab *tab3;
	NotifyDeltaCounters delta_counters = { 0, 0, 0 };

	notebook = GTK_NOTEBOOK (tepl_notebook_new ());
	tab_group = TEPL_TAB_GROUP (notebook);
	g_object_ref_sink (notebook);

	gtk_widget_show (GTK_WIDGET (notebook));

	g_signal_connect (tab_group,
			  "notify::active-tab",
			  G_CALLBACK (notify_cb),
			  &(delta_counters.active_tab_notify_delta_count));

	g_signal_connect (tab_group,
			  "notify::active-view",
			  G_CALLBACK (notify_cb),
			  &(delta_counters.active_view_notify_delta_count));

	g_signal_connect (tab_group,
			  "notify::active-buffer",
			  G_CALLBACK (notify_cb),
			  &(delta_counters.active_buffer_notify_delta_count));

	/* Create first tab. */
	tab1 = tepl_tab_new ();
	gtk_widget_show (GTK_WIDGET (tab1));
	tepl_tab_group_append_tab (tab_group, tab1, FALSE);
	check_notify_delta_counters (&delta_counters, 1, 1);

	/* For the first tab, GtkNotebook has already set it as the active tab. */
	tepl_tab_group_set_active_tab (tab_group, tab1);
	check_notify_delta_counters (&delta_counters, 0, 0);

	/* Change buffer */
	change_buffer (tab1);
	check_notify_delta_counters (&delta_counters, 0, 1);

	/* Remove tab -> active-tab is NULL. */
	gtk_widget_destroy (GTK_WIDGET (tab1));
	check_notify_delta_counters (&delta_counters, 1, 1);
	g_assert_true (tepl_tab_group_get_tabs (tab_group) == NULL);
	g_assert_true (tepl_tab_group_get_active_tab (tab_group) == NULL);

	/* Re-create first tab. */
	tab1 = tepl_tab_new ();
	gtk_widget_show (GTK_WIDGET (tab1));
	// With jump_to = TRUE this time.
	tepl_tab_group_append_tab (tab_group, tab1, TRUE);
	check_notify_delta_counters (&delta_counters, 1, 1);

	/* Append a second tab. */
	tab2 = tepl_tab_new ();
	gtk_widget_show (GTK_WIDGET (tab2));
	tepl_tab_group_append_tab (tab_group, tab2, FALSE);
	check_notify_delta_counters (&delta_counters, 0, 0);

	tepl_tab_group_set_active_tab (tab_group, tab2);
	check_notify_delta_counters (&delta_counters, 1, 1);

	/* Change buffer of tab1. */
	change_buffer (tab1);
	check_notify_delta_counters (&delta_counters, 0, 0);

	/* Change buffer of tab2. */
	change_buffer (tab2);
	check_notify_delta_counters (&delta_counters, 0, 1);

	/* Switch tabs */
	tepl_tab_group_set_active_tab (tab_group, tab1);
	check_notify_delta_counters (&delta_counters, 1, 1);

	tepl_tab_group_set_active_tab (tab_group, tab2);
	check_notify_delta_counters (&delta_counters, 1, 1);

	/* Reorder non-active tab */
	gtk_notebook_reorder_child (notebook, GTK_WIDGET (tab1), 1);
	check_notify_delta_counters (&delta_counters, 0, 0);
	g_assert_true (tepl_tab_group_get_active_tab (tab_group) == tab2);

	/* Reorder active tab.
	 * The order is reset to tab1 -> tab2.
	 */
	gtk_notebook_reorder_child (notebook, GTK_WIDGET (tab2), 1);
	check_notify_delta_counters (&delta_counters, 0, 0);
	g_assert_true (tepl_tab_group_get_active_tab (tab_group) == tab2);

	/* Append a third tab. */
	tab3 = tepl_tab_new ();
	gtk_widget_show (GTK_WIDGET (tab3));
	tepl_tab_group_append_tab (tab_group, tab3, FALSE);
	check_notify_delta_counters (&delta_counters, 0, 0);
	g_assert_true (tepl_tab_group_get_active_tab (tab_group) == tab2);

	/* Remove a non-active tab. */
	gtk_widget_destroy (GTK_WIDGET (tab1));
	check_notify_delta_counters (&delta_counters, 0, 0);

	/* Remove active tab. */
	gtk_widget_destroy (GTK_WIDGET (tab2));
	check_notify_delta_counters (&delta_counters, 1, 1);
	g_assert_true (tepl_tab_group_get_active_tab (tab_group) == tab3);

	g_object_unref (notebook);
}

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

	g_test_add_func ("/notebook/tab-group-basic", test_tab_group_basic);
	g_test_add_func ("/notebook/tab-group-notify-signals", test_tab_group_notify_signals);

	return g_test_run ();
}