File: tepl-test-utils.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 (110 lines) | stat: -rw-r--r-- 2,259 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
/* SPDX-FileCopyrightText: 2020 - Sébastien Wilmet <swilmet@gnome.org>
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include "tepl-test-utils.h"
#include <gtk/gtk.h>

/* Common utility functions for the unit tests. */

void
_tepl_test_utils_set_file_content (GFile       *file,
				   const gchar *content)
{
	GError *error = NULL;

	g_file_replace_contents (file,
				 content,
				 strlen (content),
				 NULL,
				 FALSE,
				 G_FILE_CREATE_REPLACE_DESTINATION,
				 NULL,
				 NULL,
				 &error);
	g_assert_no_error (error);
}

gchar *
_tepl_test_utils_get_file_content (GFile *file)
{
	GError *error = NULL;
	gchar *file_content = NULL;

	g_file_load_contents (file, NULL, &file_content, NULL, NULL, &error);
	g_assert_no_error (error);
	g_assert_true (file_content != NULL);

	return file_content;
}

void
_tepl_test_utils_check_file_content (GFile       *file,
				     const gchar *expected_file_content)
{
	gchar *received_file_content;

	received_file_content = _tepl_test_utils_get_file_content (file);
	g_assert_true (g_str_equal (received_file_content, expected_file_content));
	g_free (received_file_content);
}

void
_tepl_test_utils_check_equal_files_content (GFile *file1,
					    GFile *file2)
{
	gchar *file1_content = _tepl_test_utils_get_file_content (file1);
	gchar *file2_content = _tepl_test_utils_get_file_content (file2);

	g_assert_true (g_str_equal (file1_content, file2_content));

	g_free (file1_content);
	g_free (file2_content);
}

struct _TeplWaitSignalData
{
	guint signal_received : 1;
	guint nested_main_loop : 1;
};

static void
wait_signal_cb (TeplWaitSignalData *data)
{
	data->signal_received = TRUE;

	if (data->nested_main_loop)
	{
		gtk_main_quit ();
	}
}

TeplWaitSignalData *
_tepl_test_utils_wait_signal_setup (GObject     *object,
				    const gchar *detailed_signal_name)
{
	TeplWaitSignalData *data;

	data = g_new0 (TeplWaitSignalData, 1);
	data->signal_received = FALSE;
	data->nested_main_loop = FALSE;

	g_signal_connect_swapped (object,
				  detailed_signal_name,
				  G_CALLBACK (wait_signal_cb),
				  data);

	return data;
}

void
_tepl_test_utils_wait_signal (TeplWaitSignalData *data)
{
	if (!data->signal_received)
	{
		data->nested_main_loop = TRUE;
		gtk_main ();
	}

	g_free (data);
}