summaryrefslogtreecommitdiff
path: root/libpurple/tests/test_request_field.c
blob: a15d3e12b6edf455793b96f28f9dcc9a7ee81908 (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
/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

#include <glib.h>

#include <purple.h>

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_request_field_notify_filled_cb(G_GNUC_UNUSED GObject *obj,
                                    G_GNUC_UNUSED GParamSpec *pspec,
                                    gpointer data)
{
	gboolean *called = data;

	*called = TRUE;
}

static void
test_request_field_filled_string(void) {
	PurpleRequestField *field = NULL;
	gboolean called = FALSE;

	field = purple_request_field_string_new("test-string", "Test string", NULL,
	                                        FALSE);
	g_signal_connect(field, "notify::filled",
	                 G_CALLBACK(test_request_field_notify_filled_cb), &called);
	g_assert_false(purple_request_field_is_filled(field));

	/* Passing same value should not trigger. */
	called = FALSE;
	purple_request_field_string_set_value(PURPLE_REQUEST_FIELD_STRING(field),
	                                      NULL);
	g_assert_false(called);
	g_assert_false(purple_request_field_is_filled(field));

	/* Passing an empty string should not trigger, as NULL and "" are
	 * considered the same. */
	called = FALSE;
	purple_request_field_string_set_value(PURPLE_REQUEST_FIELD_STRING(field),
	                                      "");
	g_assert_false(called);
	g_assert_false(purple_request_field_is_filled(field));

	/* Now that there's a change from empty to filled, notify should occur. */
	called = FALSE;
	purple_request_field_string_set_value(PURPLE_REQUEST_FIELD_STRING(field),
	                                      "text");
	g_assert_true(called);
	g_assert_true(purple_request_field_is_filled(field));

	/* Passing same value should not trigger. */
	called = FALSE;
	purple_request_field_string_set_value(PURPLE_REQUEST_FIELD_STRING(field),
	                                      "text");
	g_assert_false(called);
	g_assert_true(purple_request_field_is_filled(field));

	/* And then going back to empty should notify. */
	called = FALSE;
	purple_request_field_string_set_value(PURPLE_REQUEST_FIELD_STRING(field),
	                                      "");
	g_assert_true(called);
	g_assert_false(purple_request_field_is_filled(field));

	g_object_unref(field);
}

static void
test_request_field_filled_nonstring(void) {
	/* Anything that's not a string should always be considered filled and
	 * never notify. */
	PurpleRequestField *field = NULL;
	gboolean called = FALSE;

	field = purple_request_field_int_new("test-int", "Test int", 50, 0, 100);
	g_signal_connect(field, "notify::filled",
	                 G_CALLBACK(test_request_field_notify_filled_cb), &called);
	g_assert_true(purple_request_field_is_filled(field));

	called = FALSE;
	purple_request_field_int_set_value(PURPLE_REQUEST_FIELD_INT(field), 50);
	g_assert_false(called);
	g_assert_true(purple_request_field_is_filled(field));

	called = FALSE;
	purple_request_field_int_set_value(PURPLE_REQUEST_FIELD_INT(field), 0);
	g_assert_false(called);
	g_assert_true(purple_request_field_is_filled(field));

	called = FALSE;
	purple_request_field_int_set_value(PURPLE_REQUEST_FIELD_INT(field), 100);
	g_assert_false(called);
	g_assert_true(purple_request_field_is_filled(field));

	g_object_unref(field);
}

/******************************************************************************
 * Main
 *****************************************************************************/
gint
main(gint argc, gchar *argv[]) {
	g_test_init(&argc, &argv, NULL);

	g_test_add_func("/request-field/filled-string",
	                test_request_field_filled_string);
	g_test_add_func("/request-field/filled-nonstring",
	                test_request_field_filled_nonstring);

	return g_test_run();
}