2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include "test_debug.h"
22 #include "test_main.h"
23 #include "test_utils.h"
25 #define TEST_CONTACT_COUNT 2
28 TEST_URI_PERSON_CONTACT,
29 TEST_URI_PERSON_GROUPREL,
30 TEST_URI_PERSON_GROUP_ASSIGNED,
31 TEST_URI_PERSON_GROUP_NOT_ASSIGNED,
32 TEST_URI_PERSON_NUMBER,
33 TEST_URI_PERSON_EMAIL,
37 static const char *uris[] = {
38 "tizen.contacts_view.person/simple_contact",
39 "tizen.contacts_view.person/simple_contact/number",
40 "tizen.contacts_view.person/simple_contact/email",
41 "tizen.contacts_view.person/simple_contact/group",
42 "tizen.contacts_view.person/simple_contact/group_assigned",
43 "tizen.contacts_view.person/simple_contact/group_not_assigned",
44 "tizen.contacts_view.person",
47 static const char *keywords[] = {
49 "123", "ㅎㄱ", "ㄱㄷ", "music", "Seoul",
50 "SEOUL", "run", "tiz", "plat", "hong",
56 static contacts_record_h _get_name(const char *first, const char *last)
58 contacts_record_h name = NULL;
59 contacts_record_create(_contacts_name._uri, &name);
60 contacts_record_set_str(name, _contacts_name.first, first);
61 contacts_record_set_str(name, _contacts_name.last, last);
65 static contacts_record_h _get_number(const char *phone_number)
67 contacts_record_h number = NULL;
68 contacts_record_create(_contacts_number._uri, &number);
69 contacts_record_set_str(number, _contacts_number.number, phone_number);
73 static contacts_record_h _get_email(int type, const char *email_address)
75 contacts_record_h email = NULL;
76 contacts_record_create(_contacts_email._uri, &email);
77 contacts_record_set_int(email, _contacts_email.type, type);
78 contacts_record_set_str(email, _contacts_email.email, email_address);
82 static contacts_record_h _get_nickname(const char *name)
84 contacts_record_h nickname = NULL;
85 contacts_record_create(_contacts_nickname._uri, &nickname);
86 contacts_record_set_str(nickname, _contacts_nickname.name, name);
90 static contacts_record_h _get_address(const char *street, const char *locality,
91 const char *region, const char *country)
93 contacts_record_h address = NULL;
94 contacts_record_create(_contacts_address._uri, &address);
95 contacts_record_set_str(address, _contacts_address.street, street);
96 contacts_record_set_str(address, _contacts_address.locality, locality);
97 contacts_record_set_str(address, _contacts_address.region, region);
98 contacts_record_set_str(address, _contacts_address.country, country);
102 static contacts_record_h _get_note(const char *contents)
104 contacts_record_h note = NULL;
105 contacts_record_create(_contacts_note._uri, ¬e);
106 contacts_record_set_str(note, _contacts_note.note, contents);
110 static contacts_record_h _get_company(const char *department, const char *name,
113 contacts_record_h company = NULL;
114 contacts_record_create(_contacts_company._uri, &company);
115 contacts_record_set_str(company, _contacts_company.department, department);
116 contacts_record_set_str(company, _contacts_company.name, name);
117 contacts_record_set_str(company, _contacts_company.role, role);
121 int _insert_contact_01(int *id)
124 contacts_record_h contact = NULL;
125 contacts_record_create(_contacts_contact._uri, &contact);
126 contacts_record_add_child_record(contact, _contacts_contact.name,
127 _get_name("홍길동", "korea123"));
128 contacts_record_add_child_record(contact, _contacts_contact.number,
129 _get_number("010-1234-1234"));
130 contacts_record_add_child_record(contact, _contacts_contact.email,
131 _get_email(CONTACTS_EMAIL_TYPE_HOME, "gildong.hong@music.com"));
132 contacts_record_add_child_record(contact, _contacts_contact.nickname,
133 _get_nickname("super"));
134 contacts_record_add_child_record(contact, _contacts_contact.address,
135 _get_address("hongic Univ", "mapo", "Seoul", "Korea"));
136 contacts_record_add_child_record(contact, _contacts_contact.note,
137 _get_note("running"));
138 contacts_record_add_child_record(contact, _contacts_contact.company,
139 _get_company("Platform Lab", "tizen", "PIMS"));
140 contacts_db_insert_record(contact, &contact_id);
141 contacts_record_destroy(contact, true);
142 DBG("contact_id(%d)", contact_id);
150 int _insert_contact_02(int *id)
153 contacts_record_h contact = NULL;
154 contacts_record_create(_contacts_contact._uri, &contact);
155 contacts_record_add_child_record(contact, _contacts_contact.name,
156 _get_name("AlpaGo", "pro9"));
157 contacts_record_add_child_record(contact, _contacts_contact.number,
158 _get_number("+851034123412"));
159 contacts_record_add_child_record(contact, _contacts_contact.email,
160 _get_email(CONTACTS_EMAIL_TYPE_HOME, "alpago@baduk.com"));
161 contacts_record_add_child_record(contact, _contacts_contact.nickname,
162 _get_nickname("prime"));
163 contacts_record_add_child_record(contact, _contacts_contact.address,
164 _get_address("broad", "young123", "Gyoung", "Korea"));
165 contacts_record_add_child_record(contact, _contacts_contact.note,
166 _get_note("day123"));
167 contacts_record_add_child_record(contact, _contacts_contact.company,
168 _get_company("customer", "new123", "role"));
169 contacts_db_insert_record(contact, &contact_id);
170 contacts_record_destroy(contact, true);
171 DBG("contact_id(%d)", contact_id);
180 static void _delete_contact(int contact_id)
182 contacts_db_delete_record(_contacts_contact._uri, contact_id);
185 static void _get_query_on_uri(const char *uri, int limit, int offset, contacts_query_h *out_query)
187 contacts_query_h query = NULL;
188 contacts_query_create(uri, &query);
190 contacts_filter_h filter = NULL;
191 contacts_filter_create(uri, &filter);
194 if (0 == strcmp(uri, _contacts_person_contact._uri)) {
195 contacts_filter_add_int(filter, _contacts_person_contact.person_id,
196 CONTACTS_MATCH_GREATER_THAN, 0);
197 } else if (0 == strcmp(uri, _contacts_person_grouprel._uri)) {
198 contacts_filter_add_int(filter, _contacts_person_grouprel.link_count,
199 CONTACTS_MATCH_GREATER_THAN, 0);
200 } else if (0 == strcmp(uri, _contacts_person_group_assigned._uri)) {
201 contacts_filter_add_int(filter, _contacts_person_group_assigned.person_id,
202 CONTACTS_MATCH_GREATER_THAN, 0);
203 } else if (0 == strcmp(uri, _contacts_person_group_not_assigned._uri)) {
204 contacts_filter_add_int(filter, _contacts_person_group_not_assigned.link_count,
205 CONTACTS_MATCH_GREATER_THAN, 0);
206 } else if (0 == strcmp(uri, _contacts_person_number._uri)) {
207 contacts_filter_add_int(filter, _contacts_person_number.person_id,
208 CONTACTS_MATCH_GREATER_THAN, 0);
209 } else if (0 == strcmp(uri, _contacts_person_email._uri)) {
210 contacts_filter_add_int(filter, _contacts_person_email.person_id,
211 CONTACTS_MATCH_GREATER_THAN, 0);
212 } else if (0 == strcmp(uri, _contacts_person._uri)) {
213 contacts_filter_add_int(filter, _contacts_person.link_count,
214 CONTACTS_MATCH_GREATER_THAN, 0);
216 ERR("Invalid uri[%s]", uri);
217 contacts_filter_destroy(filter);
218 contacts_query_destroy(query);
222 contacts_query_set_filter(query, filter);
223 contacts_filter_destroy(filter);
229 static int _check_list(const char *uri, contacts_list_h in_list, bool is_snippet)
231 if (NULL == in_list) {
237 if (0 == strcmp(uri, _contacts_person_contact._uri)) {
238 uri_type = TEST_URI_PERSON_CONTACT;
239 } else if (0 == strcmp(uri, _contacts_person_grouprel._uri)) {
240 uri_type = TEST_URI_PERSON_GROUPREL;
241 } else if (0 == strcmp(uri, _contacts_person_group_assigned._uri)) {
242 uri_type = TEST_URI_PERSON_GROUP_ASSIGNED;
243 } else if (0 == strcmp(uri, _contacts_person_group_not_assigned._uri)) {
244 uri_type = TEST_URI_PERSON_GROUP_NOT_ASSIGNED;
245 } else if (0 == strcmp(uri, _contacts_person_number._uri)) {
246 uri_type = TEST_URI_PERSON_NUMBER;
247 } else if (0 == strcmp(uri, _contacts_person_email._uri)) {
248 uri_type = TEST_URI_PERSON_EMAIL;
249 } else if (0 == strcmp(uri, _contacts_person._uri)) {
250 uri_type = TEST_URI_PERSON;
252 ERR("Invalid uri[%s]", uri);
257 contacts_list_get_count(in_list, &count);
258 DBG("count(%d)", count);
260 contacts_list_first(in_list);
261 contacts_record_h record = NULL;
262 while (CONTACTS_ERROR_NONE == contacts_list_get_current_record_p(in_list, &record)) {
264 char *display_name = NULL;
266 int snippet_type = 0;
267 char *snippet_string = NULL;
270 case TEST_URI_PERSON_CONTACT:
271 contacts_record_get_int(record, _contacts_person_contact.contact_id, &contact_id);
272 contacts_record_get_str_p(record, _contacts_person_contact.display_name, &display_name);
273 if (false == is_snippet)
276 contacts_record_get_int(record, _contacts_person_contact.snippet_type, &snippet_type);
277 contacts_record_get_str_p(record, _contacts_person_contact.snippet_string, &snippet_string);
279 case TEST_URI_PERSON_GROUPREL:
280 contacts_record_get_int(record, _contacts_person_grouprel.contact_id, &contact_id);
281 contacts_record_get_str_p(record, _contacts_person_grouprel.display_name, &display_name);
282 if (false == is_snippet)
285 contacts_record_get_int(record, _contacts_person_grouprel.snippet_type, &snippet_type);
286 contacts_record_get_str_p(record, _contacts_person_grouprel.snippet_string, &snippet_string);
288 case TEST_URI_PERSON_GROUP_ASSIGNED:
289 contacts_record_get_int(record, _contacts_person_group_assigned.contact_id, &contact_id);
290 contacts_record_get_str_p(record, _contacts_person_group_assigned.display_name, &display_name);
291 if (false == is_snippet)
294 contacts_record_get_int(record, _contacts_person_group_assigned.snippet_type, &snippet_type);
295 contacts_record_get_str_p(record, _contacts_person_group_assigned.snippet_string, &snippet_string);
297 case TEST_URI_PERSON_GROUP_NOT_ASSIGNED:
298 contacts_record_get_int(record, _contacts_person_group_not_assigned.contact_id, &contact_id);
299 contacts_record_get_str_p(record, _contacts_person_group_not_assigned.display_name, &display_name);
300 if (false == is_snippet)
303 contacts_record_get_int(record, _contacts_person_group_not_assigned.snippet_type, &snippet_type);
304 contacts_record_get_str_p(record, _contacts_person_group_not_assigned.snippet_string, &snippet_string);
306 case TEST_URI_PERSON_NUMBER:
307 contacts_record_get_int(record, _contacts_person_number.display_contact_id, &contact_id);
308 contacts_record_get_str_p(record, _contacts_person_number.display_name, &display_name);
309 contacts_record_get_str_p(record, _contacts_person_number.number, &number);
310 if (false == is_snippet)
313 contacts_record_get_int(record, _contacts_person_number.snippet_type, &snippet_type);
314 contacts_record_get_str_p(record, _contacts_person_number.snippet_string, &snippet_string);
316 case TEST_URI_PERSON_EMAIL:
317 contacts_record_get_int(record, _contacts_person_email.display_contact_id, &contact_id);
318 contacts_record_get_str_p(record, _contacts_person_email.display_name, &display_name);
319 if (false == is_snippet)
322 contacts_record_get_int(record, _contacts_person_email.snippet_type, &snippet_type);
323 contacts_record_get_str_p(record, _contacts_person_email.snippet_string, &snippet_string);
325 case TEST_URI_PERSON:
326 contacts_record_get_int(record, _contacts_person.display_contact_id, &contact_id);
327 contacts_record_get_str_p(record, _contacts_person.display_name, &display_name);
328 if (false == is_snippet)
331 contacts_record_get_int(record, _contacts_person.snippet_type, &snippet_type);
332 contacts_record_get_str_p(record, _contacts_person.snippet_string, &snippet_string);
335 DBG("id(%d) name[%s] |%d|%s|", contact_id, display_name, snippet_type, snippet_string);
337 contacts_list_next(in_list);
343 static int test_get_records(int argc, char **argv)
345 DBG("[%s]", __func__);
348 int i_count = sizeof(uris)/sizeof(uris[0]);
350 const char *uri = NULL;
359 int ids[TEST_CONTACT_COUNT] = {0};
360 _insert_contact_01(&ids[0]);
361 _insert_contact_02(&ids[1]);
363 for (i = 0; i < i_count; i++) {
364 uri = 3 < argc ? argv[3] : uris[i];
367 contacts_list_h get_list = NULL;
369 DBG("search_records");
370 contacts_db_get_all_records(uri, offset, limit, &get_list);
371 _check_list(uri, get_list, false);
372 contacts_list_destroy(get_list, true);
375 DBG("search_records_with_query");
376 contacts_query_h query = NULL;
377 _get_query_on_uri(uri, limit, offset, &query);
378 contacts_db_get_records_with_query(query, offset, limit, &get_list);
379 _check_list(uri, get_list, false);
380 contacts_list_destroy(get_list, true);
383 contacts_query_destroy(query);
386 for (i = 0; i < TEST_CONTACT_COUNT; i++)
387 _delete_contact(ids[i]);
389 contacts_disconnect();
393 static int test_search_records(int argc, char **argv)
395 DBG("[%s]", __func__);
398 int i_count = sizeof(uris)/sizeof(uris[0]);
399 int j_count = sizeof(keywords)/sizeof(keywords[0]);
401 const char *uri = NULL;
402 const char *keyword = NULL;
413 int ids[TEST_CONTACT_COUNT] = {0};
414 _insert_contact_01(&ids[0]);
415 _insert_contact_02(&ids[1]);
417 for (i = 0; i < i_count; i++) {
418 uri = 3 < argc ? argv[3] : uris[i];
421 for (j = 0; j < j_count; j++) {
422 keyword = 4 < argc ? argv[4] : keywords[j];
423 DBG("keyword[%s]", keyword);
425 contacts_list_h get_list = NULL;
427 DBG("search_records");
428 contacts_db_search_records(uri, keyword, offset, limit, &get_list);
429 _check_list(uri, get_list, false);
430 contacts_list_destroy(get_list, true);
433 DBG("search_records_with_range:CONTACTS_SEARCH_RANGE_NAME");
434 contacts_db_search_records_with_range(uri, keyword, offset, limit,
435 CONTACTS_SEARCH_RANGE_NAME, &get_list);
436 _check_list(uri, get_list, false);
437 contacts_list_destroy(get_list, true);
440 DBG("search_records_with_range:CONTACTS_SEARCH_RANGE_NUMBER");
441 contacts_db_search_records_with_range(uri, keyword, offset, limit,
442 CONTACTS_SEARCH_RANGE_NUMBER, &get_list);
443 _check_list(uri, get_list, false);
444 contacts_list_destroy(get_list, true);
447 DBG("search_records_with_range:CONTACTS_SEARCH_RANGE_DATA");
448 contacts_db_search_records_with_range(uri, keyword, offset, limit,
449 CONTACTS_SEARCH_RANGE_DATA, &get_list);
450 _check_list(uri, get_list, false);
451 contacts_list_destroy(get_list, true);
454 DBG("search_records_with_query");
455 contacts_query_h query = NULL;
456 _get_query_on_uri(uri, limit, offset, &query);
457 contacts_db_search_records_with_query(query, keyword, offset, limit,
459 _check_list(uri, get_list, false);
460 contacts_list_destroy(get_list, true);
463 contacts_query_destroy(query);
467 for (i = 0; i < TEST_CONTACT_COUNT; i++)
468 _delete_contact(ids[i]);
470 contacts_disconnect();
475 * /usr/bin//contacts-service-test 1 2 {uri} {keyword}
477 static int test_search_records_for_snippet(int argc, char **argv)
479 DBG("[%s]", __func__);
482 int i_count = sizeof(uris)/sizeof(uris[0]);
483 int j_count = sizeof(keywords)/sizeof(keywords[0]);
485 const char *uri = NULL;
486 const char *keyword = NULL;
487 char *start_match = "[";
488 char *end_match = "]";
497 if (contacts_connect() != CONTACTS_ERROR_NONE)
498 DBG("contacts_connect failed \n");
500 int ids[TEST_CONTACT_COUNT] = {0};
501 _insert_contact_01(&ids[0]);
502 _insert_contact_02(&ids[1]);
504 for (i = 0; i < i_count; i++) {
505 uri = 3 < argc ? argv[3] : uris[i];
506 DBG(">>>>> uri[%s]", uri);
508 for (j = 0; j < j_count; j++) {
509 keyword = 4 < argc ? argv[4] : keywords[j];
510 DBG("keyword [%s]", keyword);
512 contacts_list_h get_list = NULL;
514 DBG("search_records_for_snippet");
515 contacts_db_search_records_for_snippet(uri, keyword, offset, limit,
516 start_match, end_match, 0, &get_list);
517 _check_list(uri, get_list, true);
518 contacts_list_destroy(get_list, true);
521 DBG("search_records_with_range_for_snippet:CONTACTS_SEARCH_RANGE_NAME");
522 contacts_db_search_records_with_range_for_snippet(uri, keyword, offset,
523 limit, CONTACTS_SEARCH_RANGE_NAME, start_match, end_match, 0, &get_list);
524 _check_list(uri, get_list, true);
525 contacts_list_destroy(get_list, true);
528 DBG("search_records_with_range_for_snippet:CONTACTS_SEARCH_RANGE_NUMBER");
529 contacts_db_search_records_with_range_for_snippet(uri, keyword, offset,
530 limit, CONTACTS_SEARCH_RANGE_NUMBER, start_match, end_match, 0, &get_list);
531 _check_list(uri, get_list, true);
532 contacts_list_destroy(get_list, true);
535 DBG("search_records_with_range_for_snippet:CONTACTS_SEARCH_RANGE_DATA");
536 contacts_db_search_records_with_range_for_snippet(uri, keyword, offset,
537 limit, CONTACTS_SEARCH_RANGE_DATA, start_match, end_match, 0, &get_list);
538 _check_list(uri, get_list, true);
539 contacts_list_destroy(get_list, true);
542 DBG("search_records_with_query_for_snippet");
543 contacts_query_h query = NULL;
544 _get_query_on_uri(uri, limit, offset, &query);
545 contacts_db_search_records_with_query_for_snippet(query, keyword, offset,
546 limit, start_match, end_match, 0, &get_list);
547 _check_list(uri, get_list, true);
548 contacts_list_destroy(get_list, true);
550 contacts_query_destroy(query);
554 for (i = 0; i < TEST_CONTACT_COUNT; i++)
555 _delete_contact(ids[i]);
557 contacts_disconnect();
561 static const func _func[] = {
564 test_search_records_for_snippet,
567 int test_query(int argc, char **argv)
571 int count = sizeof(_func) / sizeof(func);
572 if (true == test_main_is_selected(argc, argv, 2, _func, count))
576 for (i = 0; i < count; i++) {
577 test_utils_cleanup();
578 if (_func[i](argc, argv) < 0)