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);
220 contacts_query_set_filter(query, filter);
221 contacts_filter_destroy(filter);
227 static int _check_list(const char *uri, contacts_list_h in_list, bool is_snippet)
229 if (NULL == in_list) {
235 if (0 == strcmp(uri, _contacts_person_contact._uri)) {
236 uri_type = TEST_URI_PERSON_CONTACT;
237 } else if (0 == strcmp(uri, _contacts_person_grouprel._uri)) {
238 uri_type = TEST_URI_PERSON_GROUPREL;
239 } else if (0 == strcmp(uri, _contacts_person_group_assigned._uri)) {
240 uri_type = TEST_URI_PERSON_GROUP_ASSIGNED;
241 } else if (0 == strcmp(uri, _contacts_person_group_not_assigned._uri)) {
242 uri_type = TEST_URI_PERSON_GROUP_NOT_ASSIGNED;
243 } else if (0 == strcmp(uri, _contacts_person_number._uri)) {
244 uri_type = TEST_URI_PERSON_NUMBER;
245 } else if (0 == strcmp(uri, _contacts_person_email._uri)) {
246 uri_type = TEST_URI_PERSON_EMAIL;
247 } else if (0 == strcmp(uri, _contacts_person._uri)) {
248 uri_type = TEST_URI_PERSON;
250 ERR("Invalid uri[%s]", uri);
255 contacts_list_get_count(in_list, &count);
256 DBG("count(%d)", count);
258 contacts_list_first(in_list);
259 contacts_record_h record = NULL;
260 while (CONTACTS_ERROR_NONE == contacts_list_get_current_record_p(in_list, &record)) {
262 char *display_name = NULL;
264 int snippet_type = NULL;
265 char *snippet_string = NULL;
268 case TEST_URI_PERSON_CONTACT:
269 contacts_record_get_int(record, _contacts_person_contact.contact_id, &contact_id);
270 contacts_record_get_str_p(record, _contacts_person_contact.display_name, &display_name);
271 if (false == is_snippet)
274 contacts_record_get_int(record, _contacts_person_contact.snippet_type, &snippet_type);
275 contacts_record_get_str_p(record, _contacts_person_contact.snippet_string, &snippet_string);
277 case TEST_URI_PERSON_GROUPREL:
278 contacts_record_get_int(record, _contacts_person_grouprel.contact_id, &contact_id);
279 contacts_record_get_str_p(record, _contacts_person_grouprel.display_name, &display_name);
280 if (false == is_snippet)
283 contacts_record_get_int(record, _contacts_person_grouprel.snippet_type, &snippet_type);
284 contacts_record_get_str_p(record, _contacts_person_grouprel.snippet_string, &snippet_string);
286 case TEST_URI_PERSON_GROUP_ASSIGNED:
287 contacts_record_get_int(record, _contacts_person_group_assigned.contact_id, &contact_id);
288 contacts_record_get_str_p(record, _contacts_person_group_assigned.display_name, &display_name);
289 if (false == is_snippet)
292 contacts_record_get_int(record, _contacts_person_group_assigned.snippet_type, &snippet_type);
293 contacts_record_get_str_p(record, _contacts_person_group_assigned.snippet_string, &snippet_string);
295 case TEST_URI_PERSON_GROUP_NOT_ASSIGNED:
296 contacts_record_get_int(record, _contacts_person_group_not_assigned.contact_id, &contact_id);
297 contacts_record_get_str_p(record, _contacts_person_group_not_assigned.display_name, &display_name);
298 if (false == is_snippet)
301 contacts_record_get_int(record, _contacts_person_group_not_assigned.snippet_type, &snippet_type);
302 contacts_record_get_str_p(record, _contacts_person_group_not_assigned.snippet_string, &snippet_string);
304 case TEST_URI_PERSON_NUMBER:
305 contacts_record_get_int(record, _contacts_person_number.display_contact_id, &contact_id);
306 contacts_record_get_str_p(record, _contacts_person_number.display_name, &display_name);
307 contacts_record_get_str_p(record, _contacts_person_number.number, &number);
308 if (false == is_snippet)
311 contacts_record_get_int(record, _contacts_person_number.snippet_type, &snippet_type);
312 contacts_record_get_str_p(record, _contacts_person_number.snippet_string, &snippet_string);
314 case TEST_URI_PERSON_EMAIL:
315 contacts_record_get_int(record, _contacts_person_email.display_contact_id, &contact_id);
316 contacts_record_get_str_p(record, _contacts_person_email.display_name, &display_name);
317 if (false == is_snippet)
320 contacts_record_get_int(record, _contacts_person_email.snippet_type, &snippet_type);
321 contacts_record_get_str_p(record, _contacts_person_email.snippet_string, &snippet_string);
323 case TEST_URI_PERSON:
324 contacts_record_get_int(record, _contacts_person.display_contact_id, &contact_id);
325 contacts_record_get_str_p(record, _contacts_person.display_name, &display_name);
326 if (false == is_snippet)
329 contacts_record_get_int(record, _contacts_person.snippet_type, &snippet_type);
330 contacts_record_get_str_p(record, _contacts_person.snippet_string, &snippet_string);
333 DBG("id(%d) name[%s] |%d|%s|", contact_id, display_name, snippet_type, snippet_string);
335 contacts_list_next(in_list);
341 static int test_get_records(int argc, char **argv)
343 DBG("[%s]", __func__);
346 int i_count = sizeof(uris)/sizeof(uris[0]);
348 const char *uri = NULL;
357 int ids[TEST_CONTACT_COUNT] = {0};
358 _insert_contact_01(&ids[0]);
359 _insert_contact_02(&ids[1]);
361 for (i = 0; i < i_count; i++) {
362 uri = 3 < argc ? argv[3] : uris[i];
365 contacts_list_h get_list = NULL;
367 DBG("search_records");
368 contacts_db_get_all_records(uri, offset, limit, &get_list);
369 _check_list(uri, get_list, false);
370 contacts_list_destroy(get_list, true);
373 DBG("search_records_with_query");
374 contacts_query_h query = NULL;
375 _get_query_on_uri(uri, limit, offset, &query);
376 contacts_db_get_records_with_query(query, offset, limit, &get_list);
377 _check_list(uri, get_list, false);
378 contacts_list_destroy(get_list, true);
381 contacts_query_destroy(query);
384 for (i = 0; i < TEST_CONTACT_COUNT; i++)
385 _delete_contact(ids[i]);
387 contacts_disconnect();
391 static int test_search_records(int argc, char **argv)
393 DBG("[%s]", __func__);
396 int i_count = sizeof(uris)/sizeof(uris[0]);
397 int j_count = sizeof(keywords)/sizeof(keywords[0]);
399 const char *uri = NULL;
400 const char *keyword = NULL;
411 int ids[TEST_CONTACT_COUNT] = {0};
412 _insert_contact_01(&ids[0]);
413 _insert_contact_02(&ids[1]);
415 for (i = 0; i < i_count; i++) {
416 uri = 3 < argc ? argv[3] : uris[i];
419 for (j = 0; j < j_count; j++) {
420 keyword = 4 < argc ? argv[4] : keywords[j];
421 DBG("keyword[%s]", keyword);
423 contacts_list_h get_list = NULL;
425 DBG("search_records");
426 contacts_db_search_records(uri, keyword, offset, limit, &get_list);
427 _check_list(uri, get_list, false);
428 contacts_list_destroy(get_list, true);
431 DBG("search_records_with_range:CONTACTS_SEARCH_RANGE_NAME");
432 contacts_db_search_records_with_range(uri, keyword, offset, limit,
433 CONTACTS_SEARCH_RANGE_NAME, &get_list);
434 _check_list(uri, get_list, false);
435 contacts_list_destroy(get_list, true);
438 DBG("search_records_with_range:CONTACTS_SEARCH_RANGE_NUMBER");
439 contacts_db_search_records_with_range(uri, keyword, offset, limit,
440 CONTACTS_SEARCH_RANGE_NUMBER, &get_list);
441 _check_list(uri, get_list, false);
442 contacts_list_destroy(get_list, true);
445 DBG("search_records_with_range:CONTACTS_SEARCH_RANGE_DATA");
446 contacts_db_search_records_with_range(uri, keyword, offset, limit,
447 CONTACTS_SEARCH_RANGE_DATA, &get_list);
448 _check_list(uri, get_list, false);
449 contacts_list_destroy(get_list, true);
452 DBG("search_records_with_query");
453 contacts_query_h query = NULL;
454 _get_query_on_uri(uri, limit, offset, &query);
455 contacts_db_search_records_with_query(query, keyword, offset, limit,
457 _check_list(uri, get_list, false);
458 contacts_list_destroy(get_list, true);
461 contacts_query_destroy(query);
465 for (i = 0; i < TEST_CONTACT_COUNT; i++)
466 _delete_contact(ids[i]);
468 contacts_disconnect();
473 * /usr/bin//contacts-service-test 1 2 {uri} {keyword}
475 static int test_search_records_for_snippet(int argc, char **argv)
477 DBG("[%s]", __func__);
480 int i_count = sizeof(uris)/sizeof(uris[0]);
481 int j_count = sizeof(keywords)/sizeof(keywords[0]);
483 const char *uri = NULL;
484 const char *keyword = NULL;
485 char *start_match = "[";
486 char *end_match = "]";
497 int ids[TEST_CONTACT_COUNT] = {0};
498 _insert_contact_01(&ids[0]);
499 _insert_contact_02(&ids[1]);
501 for (i = 0; i < i_count; i++) {
502 uri = 3 < argc ? argv[3] : uris[i];
503 DBG(">>>>> uri[%s]", uri);
505 for (j = 0; j < j_count; j++) {
506 keyword = 4 < argc ? argv[4] : keywords[j];
507 DBG("keyword [%s]", keyword);
509 contacts_list_h get_list = NULL;
511 DBG("search_records_for_snippet");
512 contacts_db_search_records_for_snippet(uri, keyword, offset, limit,
513 start_match, end_match, 0, &get_list);
514 _check_list(uri, get_list, true);
515 contacts_list_destroy(get_list, true);
518 DBG("search_records_with_range_for_snippet:CONTACTS_SEARCH_RANGE_NAME");
519 contacts_db_search_records_with_range_for_snippet(uri, keyword, offset,
520 limit, CONTACTS_SEARCH_RANGE_NAME, start_match, end_match, 0, &get_list);
521 _check_list(uri, get_list, true);
522 contacts_list_destroy(get_list, true);
525 DBG("search_records_with_range_for_snippet:CONTACTS_SEARCH_RANGE_NUMBER");
526 contacts_db_search_records_with_range_for_snippet(uri, keyword, offset,
527 limit, CONTACTS_SEARCH_RANGE_NUMBER, start_match, end_match, 0, &get_list);
528 _check_list(uri, get_list, true);
529 contacts_list_destroy(get_list, true);
532 DBG("search_records_with_range_for_snippet:CONTACTS_SEARCH_RANGE_DATA");
533 contacts_db_search_records_with_range_for_snippet(uri, keyword, offset,
534 limit, CONTACTS_SEARCH_RANGE_DATA, start_match, end_match, 0, &get_list);
535 _check_list(uri, get_list, true);
536 contacts_list_destroy(get_list, true);
539 DBG("search_records_with_query_for_snippet");
540 contacts_query_h query = NULL;
541 _get_query_on_uri(uri, limit, offset, &query);
542 contacts_db_search_records_with_query_for_snippet(query, keyword, offset,
543 limit, start_match, end_match, 0, &get_list);
544 _check_list(uri, get_list, true);
545 contacts_list_destroy(get_list, true);
547 contacts_query_destroy(query);
551 for (i = 0; i < TEST_CONTACT_COUNT; i++)
552 _delete_contact(ids[i]);
554 contacts_disconnect();
558 static const func _func[] = {
561 test_search_records_for_snippet,
564 int test_query(int argc, char **argv)
568 int count = sizeof(_func) / sizeof(func);
569 if (true == test_main_is_selected(argc, argv, 2, _func, count))
573 for (i = 0; i < count; i++) {
574 test_utils_cleanup();
575 if (_func[i](argc, argv) < 0)