File: test-book-client-e164-param.c

package info (click to toggle)
evolution-data-server 3.56.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 46,656 kB
  • sloc: ansic: 365,352; xml: 578; cpp: 482; perl: 297; sh: 62; makefile: 60; python: 35; javascript: 29
file content (190 lines) | stat: -rw-r--r-- 5,215 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2013 Intel Corporation
 *
 * This program 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.
 *
 * This program 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 program. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Tristan Van Berkom <tristanvb@openismus.com>
 */

#include "evolution-data-server-config.h"

#include <stdlib.h>
#include <libebook/libebook.h>

#include "client-test-utils.h"
#include "e-test-server-utils.h"

#ifdef ENABLE_PHONENUMBER

typedef struct {
	ETestServerClosure parent;

	gchar *vcard_name;
	gchar *formatted_number;
	gchar *country_calling_code;
	gchar *national_number;
} TestData;

static void
test_data_free (gpointer user_data)
{
	TestData *const data = user_data;

	g_free (data->vcard_name);
	g_free (data->formatted_number);
	g_free (data->country_calling_code);
	g_free (data->national_number);
	g_free (data);
}

static TestData *
test_data_new (const gchar *vcard_name,
               const gchar *formatted_number,
               const gchar *country_calling_code,
               const gchar *national_number,
               gboolean direct)
{
	TestData *const data = g_new0 (TestData, 1);

	data->parent.type = direct ? E_TEST_SERVER_DIRECT_ADDRESS_BOOK : E_TEST_SERVER_ADDRESS_BOOK;
	data->parent.destroy_closure_func = test_data_free;
	data->vcard_name = g_strdup (vcard_name);
	data->formatted_number = g_strdup (formatted_number);
	data->country_calling_code = g_strdup (country_calling_code);
	data->national_number = g_strdup (national_number);

	g_print ("%d %p\n", data->parent.calendar_source_type, data->parent.destroy_closure_func);

	return data;
}

static void
test_add_e164_param (ETestServerFixture *fixture,
                     gconstpointer user_data)
{
	const TestData *const data = user_data;
	EBookClient          *book_client;
	EContact             *contact;
	gchar                *vcard;
	gchar                *uid;
	EVCardAttribute      *tel;
	GList                *values;
	GError               *error = NULL;

	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
	g_print ("%p\n", book_client);

	vcard = new_vcard_from_test_case (data->vcard_name);
	contact = e_contact_new_from_vcard (vcard);
	g_free (vcard);

	tel = e_vcard_get_attribute (E_VCARD (contact), EVC_TEL);
	values = tel ? e_vcard_attribute_get_values (tel) : NULL;

	g_assert_true (values != NULL);
	g_assert_cmpstr (values->data, ==, data->formatted_number);

	values = e_vcard_attribute_get_param (tel, EVC_X_E164);
	g_assert_true (values == NULL);

	if (!e_book_client_add_contact_sync (book_client, contact, E_BOOK_OPERATION_FLAG_NONE, &uid, NULL, &error))
		g_error ("Failed to add contact: %s", error->message);

	g_object_unref (contact);

	if (!e_book_client_get_contact_sync (book_client, uid, &contact, NULL, &error))
		g_error ("Failed to restore contact: %s", error->message);

	g_free (uid);

	tel = e_vcard_get_attribute (E_VCARD (contact), EVC_TEL);
	values = tel ? e_vcard_attribute_get_values (tel) : NULL;

	g_assert_true (values != NULL);
	g_assert_cmpstr (values->data, ==, data->formatted_number);

	values = e_vcard_attribute_get_param (tel, EVC_X_E164);

	g_assert_true (values != NULL);
	g_assert_cmpstr (values->data, ==, data->national_number);

	if (data->country_calling_code) {
		g_assert_true (values->next != NULL);
		g_assert_cmpstr (values->next->data, ==, data->country_calling_code);
	} else {
		g_assert_true (values->next == NULL);
	}
}

#endif /* ENABLE_PHONENUMBER */

gint
main (gint argc,
      gchar **argv)
{
	g_test_init (&argc, &argv, NULL);
	g_test_bug_base ("https://gitlab.gnome.org/GNOME/evolution-data-server/");

	client_test_utils_read_args (argc, argv);

#ifdef ENABLE_PHONENUMBER

	g_test_add (
		"/EBookClient/AddContact/AddE164Param/1",
		ETestServerFixture,
		test_data_new (
			"custom-1",
			"+1-221-5423789",
			"+1", "2215423789",
			FALSE),
		e_test_server_utils_setup,
		test_add_e164_param,
		e_test_server_utils_teardown);
	g_test_add (
		"/EBookClient/AddContact/AddE164Param/2",
		ETestServerFixture,
		test_data_new (
			"custom-2",
			"7654321",
			NULL, "7654321",
			FALSE),
		e_test_server_utils_setup,
		test_add_e164_param,
		e_test_server_utils_teardown);
	g_test_add (
		"/EBookClient/DirectAccess/AddContact/AddE164Param/1",
		ETestServerFixture,
		test_data_new (
			"custom-1",
			"+1-221-5423789",
			"+1", "2215423789",
			TRUE),
		e_test_server_utils_setup,
		test_add_e164_param,
		e_test_server_utils_teardown);
	g_test_add (
		"/EBookClient/DirectAccess/AddContact/AddE164Param/2",
		ETestServerFixture,
		test_data_new (
			"custom-2",
			"7654321",
			NULL, "7654321",
			TRUE),
		e_test_server_utils_setup,
		test_add_e164_param,
		e_test_server_utils_teardown);

#endif /* ENABLE_PHONENUMBER */

	return e_test_server_utils_run (argc, argv);
}