File: test-toast.c

package info (click to toggle)
libadwaita-1 1.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 12,252 kB
  • sloc: ansic: 86,011; xml: 189; python: 60; sh: 30; makefile: 23; javascript: 9
file content (341 lines) | stat: -rw-r--r-- 10,384 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2021 Purism SPC
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * Author: Alice Mikhaylenko <alice.mikhaylenko@puri.sm>
 */

#include <adwaita.h>

static void
increment (int *data)
{
  (*data)++;
}

static void
test_adw_toast_title (void)
{
  AdwToast *toast = adw_toast_new ("Title");
  char *title;
  int notified = 0;

  g_assert_nonnull (toast);

  g_signal_connect_swapped (toast, "notify::title", G_CALLBACK (increment), &notified);

  g_object_get (toast, "title", &title, NULL);
  g_assert_cmpstr (title, ==, "Title");

  adw_toast_set_title (toast, "Another title");
  g_assert_cmpstr (adw_toast_get_title (toast), ==, "Another title");
  g_assert_cmpint (notified, ==, 1);

  g_object_set (toast, "title", "Title", NULL);
  g_assert_cmpstr (adw_toast_get_title (toast), ==, "Title");
  g_assert_cmpint (notified, ==, 2);

  g_free (title);
  g_assert_finalize_object (toast);
}

static void
test_adw_toast_title_format (void)
{
  AdwToast *toast;
  const int n_value = 42;
  char *title;

  toast = adw_toast_new_format ("Title %d", n_value);

  g_assert_nonnull (toast);

  g_object_get (toast, "title", &title, NULL);
  g_assert_cmpstr (title, ==, "Title 42");

  g_free (title);
  g_assert_finalize_object (toast);
}

static void
test_adw_toast_button_label (void)
{
  AdwToast *toast = adw_toast_new ("Title");
  char *button_label;
  int notified = 0;

  g_assert_nonnull (toast);

  g_signal_connect_swapped (toast, "notify::button-label", G_CALLBACK (increment), &notified);

  g_object_get (toast, "button-label", &button_label, NULL);
  g_assert_null (button_label);

  adw_toast_set_button_label (toast, "Button");
  g_assert_cmpstr (adw_toast_get_button_label (toast), ==, "Button");
  g_assert_cmpint (notified, ==, 1);

  g_object_set (toast, "button-label", "Button 2", NULL);
  g_assert_cmpstr (adw_toast_get_button_label (toast), ==, "Button 2");
  g_assert_cmpint (notified, ==, 2);

  g_assert_finalize_object (toast);
}

static void
test_adw_toast_action_name (void)
{
  AdwToast *toast = adw_toast_new ("Title");
  char *action_name;
  int notified = 0;

  g_assert_nonnull (toast);

  g_signal_connect_swapped (toast, "notify::action-name", G_CALLBACK (increment), &notified);

  g_object_get (toast, "action-name", &action_name, NULL);
  g_assert_null (action_name);

  adw_toast_set_action_name (toast, "win.something");
  g_assert_cmpstr (adw_toast_get_action_name (toast), ==, "win.something");
  g_assert_cmpint (notified, ==, 1);

  g_object_set (toast, "action-name", "win.something-else", NULL);
  g_assert_cmpstr (adw_toast_get_action_name (toast), ==, "win.something-else");
  g_assert_cmpint (notified, ==, 2);

  g_assert_finalize_object (toast);
}

static void
test_adw_toast_action_target (void)
{
  AdwToast *toast = adw_toast_new ("Title");
  GVariant *action_target, *variant;
  int notified = 0;

  g_assert_nonnull (toast);

  g_signal_connect_swapped (toast, "notify::action-target", G_CALLBACK (increment), &notified);

  g_object_get (toast, "action-target", &action_target, NULL);
  g_assert_null (action_target);

  variant = g_variant_ref_sink (g_variant_new_int32 (1));
  adw_toast_set_action_target_value (toast, g_variant_new_int32 (1));
  g_assert_cmpvariant (adw_toast_get_action_target_value (toast), variant);
  g_assert_cmpint (notified, ==, 1);
  g_variant_unref (variant);

  variant = g_variant_ref_sink (g_variant_new_int32 (2));
  g_object_set (toast, "action-target", g_variant_new_int32 (2), NULL);
  g_assert_cmpvariant (adw_toast_get_action_target_value (toast), variant);
  g_assert_cmpint (notified, ==, 2);
  g_variant_unref (variant);

  variant = g_variant_ref_sink (g_variant_new_int32 (3));
  adw_toast_set_action_target (toast, "i", 3);
  g_assert_cmpvariant (adw_toast_get_action_target_value (toast), variant);
  g_assert_cmpint (notified, ==, 3);
  g_variant_unref (variant);

  g_assert_finalize_object (toast);
}

static void
test_adw_toast_detailed_action_name (void)
{
  AdwToast *toast = adw_toast_new ("Title");
  GVariant *variant = g_variant_ref_sink (g_variant_new_int32 (2));

  g_assert_nonnull (toast);

  g_assert_null (adw_toast_get_action_name (toast));
  g_assert_null (adw_toast_get_action_target_value (toast));

  adw_toast_set_detailed_action_name (toast, "win.something");
  g_assert_cmpstr (adw_toast_get_action_name (toast), ==, "win.something");
  g_assert_null (adw_toast_get_action_target_value (toast));

  adw_toast_set_detailed_action_name (toast, "win.something(2)");
  g_assert_cmpstr (adw_toast_get_action_name (toast), ==, "win.something");
  g_assert_cmpvariant (adw_toast_get_action_target_value (toast), variant);

  g_variant_unref (variant);
  g_assert_finalize_object (toast);
}

static void
test_adw_toast_priority (void)
{
  AdwToast *toast = adw_toast_new ("Title");
  AdwToastPriority priority;
  int notified = 0;

  g_assert_nonnull (toast);

  g_signal_connect_swapped (toast, "notify::priority", G_CALLBACK (increment), &notified);

  g_object_get (toast, "priority", &priority, NULL);
  g_assert_cmpint (priority, ==, ADW_TOAST_PRIORITY_NORMAL);

  adw_toast_set_priority (toast, ADW_TOAST_PRIORITY_HIGH);
  g_assert_cmpint (adw_toast_get_priority (toast), ==, ADW_TOAST_PRIORITY_HIGH);
  g_assert_cmpint (notified, ==, 1);

  g_object_set (toast, "priority", ADW_TOAST_PRIORITY_NORMAL, NULL);
  g_assert_cmpint (adw_toast_get_priority (toast), ==, ADW_TOAST_PRIORITY_NORMAL);
  g_assert_cmpint (notified, ==, 2);

  g_assert_finalize_object (toast);
}

static void
test_adw_toast_timeout (void)
{
  AdwToast *toast = adw_toast_new ("Title");
  guint timeout;
  int notified = 0;

  g_assert_nonnull (toast);

  g_signal_connect_swapped (toast, "notify::timeout", G_CALLBACK (increment), &notified);

  g_object_get (toast, "timeout", &timeout, NULL);
  g_assert_cmpint (timeout, ==, 5);

  adw_toast_set_timeout (toast, 10);
  g_assert_cmpint (adw_toast_get_timeout (toast), ==, 10);
  g_assert_cmpint (notified, ==, 1);

  g_object_set (toast, "timeout", 5, NULL);
  g_assert_cmpint (adw_toast_get_timeout (toast), ==, 5);
  g_assert_cmpint (notified, ==, 2);

  g_assert_finalize_object (toast);
}

static void
test_adw_toast_dismiss (void)
{
  AdwToast *toast = adw_toast_new ("Title");
  AdwToastOverlay *overlay = g_object_ref_sink (ADW_TOAST_OVERLAY (adw_toast_overlay_new ()));

  g_assert_nonnull (overlay);
  g_assert_nonnull (toast);

  adw_toast_overlay_add_toast (overlay, g_object_ref (toast));
  adw_toast_dismiss (toast);

  /* Repeat dismiss() calls should no-op */
  adw_toast_overlay_add_toast (overlay, g_object_ref (toast));
  adw_toast_dismiss (toast);
  adw_toast_dismiss (toast);
  adw_toast_dismiss (toast);

  g_assert_finalize_object (overlay);
  g_assert_finalize_object (toast);
}

static void
test_adw_toast_custom_title (void)
{
  AdwToast *toast = adw_toast_new ("Title");
  GtkWidget *widget = NULL;
  char *title;
  int notified = 0;

  g_assert_nonnull (toast);

  g_signal_connect_swapped (toast, "notify::custom-title", G_CALLBACK (increment), &notified);

  g_object_get (toast, "title", &title, NULL);
  g_assert_cmpstr (title, ==, "Title");
  g_object_get (toast, "custom-title", &widget, NULL);
  g_assert_null (widget);

  adw_toast_set_title (toast, "Another title");
  g_assert_cmpint (notified, ==, 0);

  widget = g_object_ref_sink (gtk_label_new ("Custom title"));
  adw_toast_set_custom_title (toast, widget);
  g_assert_true (adw_toast_get_custom_title (toast) == widget);
  g_assert_null (adw_toast_get_title (toast));
  g_assert_cmpint (notified, ==, 1);

  adw_toast_set_title (toast, "Final title");
  g_assert_null (adw_toast_get_custom_title (toast));
  g_assert_cmpstr (adw_toast_get_title (toast), ==, "Final title");
  g_assert_cmpint (notified, ==, 2);

  g_free (title);
  g_assert_finalize_object (toast);
  g_assert_finalize_object (widget);
}

static void
test_adw_toast_custom_title_overlay (void)
{
  AdwToastOverlay *first_overlay = g_object_ref_sink (ADW_TOAST_OVERLAY (adw_toast_overlay_new ()));
  AdwToastOverlay *second_overlay = g_object_ref_sink (ADW_TOAST_OVERLAY (adw_toast_overlay_new ()));
  AdwToast *toast = adw_toast_new ("");
  GtkWidget *widget = gtk_label_new ("Custom title");

  g_assert_nonnull (first_overlay);
  g_assert_nonnull (second_overlay);
  g_assert_nonnull (toast);

  adw_toast_set_custom_title (toast, g_object_ref (widget));

  adw_toast_overlay_add_toast (first_overlay, g_object_ref (toast));
  adw_toast_dismiss (toast);
  adw_toast_overlay_add_toast (second_overlay, g_object_ref (toast));

  g_assert_finalize_object (first_overlay);
  g_assert_finalize_object (second_overlay);
  g_assert_finalize_object (toast);
  g_assert_finalize_object (widget);
}

static void
test_adw_toast_use_markup (void)
{
  AdwToastOverlay *toast_overlay = g_object_ref_sink (ADW_TOAST_OVERLAY (adw_toast_overlay_new()));
  AdwToast *toast = adw_toast_new ("");

  g_assert_nonnull (toast_overlay);
  g_assert_nonnull (toast);

  adw_toast_overlay_add_toast (toast_overlay, g_object_ref (toast));
  adw_toast_set_use_markup (toast, FALSE);
  adw_toast_set_title (toast, "<span false>bad markup</sp>");

  g_assert_cmpstr (adw_toast_get_title (toast), ==, "<span false>bad markup</sp>");

  g_assert_finalize_object (toast_overlay);
  g_assert_finalize_object (toast);
}

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

  g_test_add_func ("/Adwaita/Toast/title", test_adw_toast_title);
  g_test_add_func ("/Adwaita/Toast/title_format", test_adw_toast_title_format);
  g_test_add_func ("/Adwaita/Toast/button_label", test_adw_toast_button_label);
  g_test_add_func ("/Adwaita/Toast/action_name", test_adw_toast_action_name);
  g_test_add_func ("/Adwaita/Toast/action_target", test_adw_toast_action_target);
  g_test_add_func ("/Adwaita/Toast/detailed_action_name", test_adw_toast_detailed_action_name);
  g_test_add_func ("/Adwaita/Toast/priority", test_adw_toast_priority);
  g_test_add_func ("/Adwaita/Toast/timeout", test_adw_toast_timeout);
  g_test_add_func ("/Adwaita/Toast/dismiss", test_adw_toast_dismiss);
  g_test_add_func ("/Adwaita/Toast/custom_title", test_adw_toast_custom_title);
  g_test_add_func ("/Adwaita/Toast/custom_title_overlay", test_adw_toast_custom_title_overlay);
  g_test_add_func ("/Adwaita/Toast/use_markup", test_adw_toast_use_markup);

  return g_test_run ();
}