[ACR-1838] Deprecate all bookmark and playlist funtions
[platform/core/api/media-content.git] / test / media-content_test.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <sys/time.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <media_content.h>
22 #include <media_info_private.h>
23 #include <glib.h>
24 #include <tzplatform_config.h>
25 #include <media_content_internal.h>
26
27
28 static filter_h g_filter = NULL;
29
30 static GMainLoop *g_loop = NULL;
31
32 #define test_audio_id "3304285f-1070-41af-8b4e-f0086cc768f3"
33 #define test_video_id "53c43e7e-53d2-4194-80a6-55d5dcde0def"
34 #define test_image_id "db1c184c-6f31-43b4-b924-8c00ac5b6197"
35
36 static void get_audio_meta(media_info_h media)
37 {
38         audio_meta_h audio = NULL;
39         char *c_value = NULL;
40
41         content_debug("=== audio meta ===");
42         if (media_info_get_audio(media, &audio) != MEDIA_CONTENT_ERROR_NONE)
43                 return;
44
45         if (audio_meta_get_media_id(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
46                 content_debug("audio_id : [%s]", c_value);
47                 SAFE_FREE(c_value);
48         }
49
50         if (audio_meta_get_album(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
51                 content_debug("album : [%s]", c_value);
52                 SAFE_FREE(c_value);
53         }
54
55         if (audio_meta_get_artist(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
56                 content_debug("artist : [%s]", c_value);
57                 SAFE_FREE(c_value);
58         }
59
60         if (audio_meta_get_album_artist(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
61                 content_debug("album artist : [%s]", c_value);
62                 SAFE_FREE(c_value);
63         }
64
65         if (audio_meta_get_genre(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
66                 content_debug("genre : [%s]", c_value);
67                 SAFE_FREE(c_value);
68         }
69
70         if (audio_meta_get_year(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
71                 content_debug("year : [%s]", c_value);
72                 SAFE_FREE(c_value);
73         }
74
75         if (audio_meta_get_track_num(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
76                 content_debug("track number : [%s]", c_value);
77                 SAFE_FREE(c_value);
78         }
79
80         audio_meta_destroy(audio);
81 }
82
83 static void get_image_meta(media_info_h media)
84 {
85         image_meta_h image = NULL;
86         char *c_value = NULL;
87         int i_value = 0;
88         media_content_orientation_e orientation;
89
90         content_debug("=== image meta ===");
91         if (media_info_get_image(media, &image) != MEDIA_CONTENT_ERROR_NONE)
92                 return;
93
94         if (image_meta_get_width(image, &i_value) == MEDIA_CONTENT_ERROR_NONE)
95                 content_debug("width : [%d]", i_value);
96
97         if (image_meta_get_height(image, &i_value) == MEDIA_CONTENT_ERROR_NONE)
98                 content_debug("height : [%d]", i_value);
99
100         if (image_meta_get_orientation(image, &orientation) == MEDIA_CONTENT_ERROR_NONE)
101                 content_debug("orientation : [%d]", orientation);
102
103         if (image_meta_get_date_taken(image, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
104                 content_debug("datetaken : [%s]", c_value);
105                 SAFE_FREE(c_value);
106         }
107
108         image_meta_destroy(image);
109 }
110
111 static void get_book_meta(media_info_h media)
112 {
113         book_meta_h book = NULL;
114         char *c_value = NULL;
115
116         content_debug("=== book meta ===");
117         if (media_info_get_book(media, &book) != MEDIA_CONTENT_ERROR_NONE)
118                 return;
119
120         if (book_meta_get_media_id(book, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
121                 content_debug("media_id : [%s]", c_value);
122                 SAFE_FREE(c_value);
123         }
124
125         if (book_meta_get_author(book, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
126                 content_debug("author : [%s]", c_value);
127                 SAFE_FREE(c_value);
128         }
129
130         if (book_meta_get_subject(book, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
131                 content_debug("subject : [%s]", c_value);
132                 SAFE_FREE(c_value);
133         }
134
135         book_meta_destroy(book);
136 }
137
138 static void get_album_meta(media_album_h album)
139 {
140         char *c_value = NULL;
141         int i_value = 0;
142
143         content_debug("=== album meta ===");
144         if (media_album_get_album_id(album, &i_value) == MEDIA_CONTENT_ERROR_NONE)
145                 content_debug("album id : [%d]", i_value);
146
147         if (media_album_get_name(album, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
148                 content_debug("album name : [%s]", c_value);
149                 SAFE_FREE(c_value);
150         }
151
152         if (media_album_get_artist(album, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
153                 content_debug("album artist : [%s]", c_value);
154                 SAFE_FREE(c_value);
155         }
156 }
157
158 static void get_folder_meta(media_folder_h folder, char **folder_id)
159 {
160         char *c_value = NULL;
161
162         content_debug("=== folder meta ===");
163         if (media_folder_get_folder_id(folder, &c_value) != MEDIA_CONTENT_ERROR_NONE) {
164                 content_debug("folder_id : %s", c_value);
165                 *folder_id = c_value;
166         }
167
168         if (media_folder_get_path(folder, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
169                 content_debug("folder_path : %s", c_value);
170                 SAFE_FREE(c_value);
171         }
172
173         if (media_folder_get_name(folder, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
174                 content_debug("folder_name : %s", c_value);
175                 SAFE_FREE(c_value);
176         }
177 }
178
179 static void get_media_meta(media_info_h media)
180 {
181         char *c_value = NULL;
182         time_t t_value = 0;
183         unsigned long long size = 0;
184         media_content_type_e media_type = 0;
185 #ifdef _USE_TVPD_MODE
186         int i_value = 0;
187 #endif
188
189         content_debug("=== media meta ===");
190         if (media_info_get_media_type(media, &media_type) == MEDIA_CONTENT_ERROR_NONE)
191                 content_debug("media_type : [%d]", media_type);
192
193         if (media_info_get_media_id(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
194                 content_debug("media_id : [%s]", c_value);
195                 SAFE_FREE(c_value);
196         }
197
198         if (media_info_get_file_path(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
199                 content_debug("file_path : [%s]", c_value);
200                 SAFE_FREE(c_value);
201         }
202
203         if (media_info_get_display_name(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
204                 content_debug("display_name : [%s]", c_value);
205                 SAFE_FREE(c_value);
206         }
207
208         if (media_info_get_mime_type(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
209                 content_debug("mime_type : [%s]", c_value);
210                 SAFE_FREE(c_value);
211         }
212
213         if (media_info_get_thumbnail_path(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
214                 content_debug("thumbnail_path : [%s]", c_value);
215                 SAFE_FREE(c_value);
216         }
217
218         if (media_info_get_size(media, &size) == MEDIA_CONTENT_ERROR_NONE)
219                 content_debug("size : [%lld]", size);
220
221         if (media_info_get_added_time(media, &t_value) == MEDIA_CONTENT_ERROR_NONE)
222                 content_debug("added_time : [%ld]", t_value);
223
224         if (media_info_get_modified_time(media, &t_value) == MEDIA_CONTENT_ERROR_NONE)
225                 content_debug("modified_time : [%ld]", t_value);
226
227 #ifdef _USE_TVPD_MODE
228         if (media_info_get_stitched_state(media, &i_value) == MEDIA_CONTENT_ERROR_NONE)
229                 content_debug("360 stitched : [%d]", i_value);
230 #endif
231
232         if (media_type == MEDIA_CONTENT_TYPE_MUSIC)
233                 get_audio_meta(media);
234
235         if (media_type == MEDIA_CONTENT_TYPE_IMAGE)
236                 get_image_meta(media);
237
238         if (media_type == MEDIA_CONTENT_TYPE_BOOK)
239                 get_book_meta(media);
240
241         if (media_type == MEDIA_CONTENT_TYPE_OTHERS)
242                 content_debug("Other type");
243 }
244
245 bool media_item_cb(media_info_h media, void *user_data)
246 {
247         if (!media) {
248                 content_debug("NO Item");
249                 return true;
250         }
251
252         get_media_meta(media);
253
254         return true;
255 }
256
257 bool gallery_folder_list_cb(media_folder_h folder, void *user_data)
258 {
259         media_folder_h new_folder = NULL;
260         media_folder_clone(&new_folder, folder);
261
262         GList **list = (GList**)user_data;
263         *list = g_list_append(*list, new_folder);
264
265         return true;
266 }
267
268 bool gallery_media_item_cb(media_info_h media, void *user_data)
269 {
270         media_info_h new_media = NULL;
271         int ret = MEDIA_CONTENT_ERROR_NONE;
272
273         ret = media_info_clone(&new_media, media);
274
275         if (ret != MEDIA_CONTENT_ERROR_NONE) {
276                 GList **list = (GList**)user_data;
277                 *list = g_list_append(*list, new_media);
278         }
279
280         return true;
281 }
282
283 bool folder_list_cb(media_folder_h folder, void *user_data)
284 {
285         int item_count = 0;
286         char *folder_id = NULL;
287         media_folder_h *_folder = (media_folder_h*)user_data;
288
289         content_debug("===========================");
290         if (folder != NULL) {
291                 if (_folder != NULL)
292                         media_folder_clone(_folder, folder);
293
294                 get_folder_meta(folder, &folder_id);
295
296                 if (media_folder_get_media_count_from_db(folder_id, g_filter, &item_count) != MEDIA_CONTENT_ERROR_NONE) {
297                         SAFE_FREE(folder_id);
298                         content_error("[ERROR] media_folder_get_media_count_from_db is failed");
299                         return false;
300                 }
301
302                 if (media_folder_foreach_media_from_db(folder_id, g_filter, media_item_cb, NULL) != MEDIA_CONTENT_ERROR_NONE) {
303                         SAFE_FREE(folder_id);
304                         content_error("[ERROR] media_folder_foreach_media_from_db is failed");
305                         return false;
306                 }
307
308                 SAFE_FREE(folder_id);
309                 return true;
310         } else {
311                 return false;
312         }
313
314 }
315
316 bool album_list_cb(media_album_h album, void *user_data)
317 {
318         int album_id = 0;
319         int media_count = 0;
320         int ret = MEDIA_CONTENT_ERROR_NONE;
321
322         if (album != NULL) {
323                 get_album_meta(album);
324                 media_album_get_album_id(album, &album_id);
325
326                 ret = media_album_get_media_count_from_db(album_id, NULL, &media_count);
327                 if (ret != MEDIA_CONTENT_ERROR_NONE)
328                         content_error("error media_album_get_media_count_from_db : [%d]", ret);
329
330                 content_debug("media_count : [%d]", media_count);
331
332                 ret = media_album_foreach_media_from_db(album_id, NULL, media_item_cb, NULL);
333                 if (ret != MEDIA_CONTENT_ERROR_NONE) {
334                         content_error("error media_album_foreach_media_from_db : [%d]", ret);
335                         return false;
336                 }
337
338         } else {
339                 content_error("album item not Found!!");
340         }
341
342         return true;
343 }
344
345 bool group_list_cb(const char *group_name, void *user_data)
346 {
347         int media_count = 0;
348         int *idx = user_data;
349
350         content_debug("group item : [%s] [%d]", group_name, *idx);
351
352         if (media_group_get_media_count_from_db(group_name, *idx, g_filter, &media_count) != MEDIA_CONTENT_ERROR_NONE)
353                 return false;
354
355         content_debug("media_count : [%d]", media_count);
356
357         if (media_group_foreach_media_from_db(group_name, *idx, g_filter, media_item_cb, NULL) != MEDIA_CONTENT_ERROR_NONE)
358                 return false;
359
360         return true;
361 }
362
363 int test_filter_create(void)
364 {
365         content_debug("\n============Filter Create============\n\n");
366
367         int ret = MEDIA_CONTENT_ERROR_NONE;
368
369         /* Filter for media */
370         const char *condition = "MEDIA_TYPE=3"; /*MEDIA_TYPE 0-image, 1-video, 2-sound, 3-music, 4-other*/
371
372         ret = media_filter_create(&g_filter);
373
374         /* Set condition and collate
375          * Condition string : You can make where statement of sql.
376          * Colation : You can use collation when comparing.
377          * Ex) In case of FILE_NAME='Samsung' as condition string,
378          *      if you want to compare with NOCASE collation,
379          *      call media_filter_set_condition(g_filter, condition, MEDIA_CONTENT_COLLATE_NOCASE);
380          *      if you want to compare in case-sensitive,
381          *      call media_filter_set_condition(g_filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
382          */
383         ret = media_filter_set_condition(g_filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
384
385         return ret;
386 }
387
388 int test_filter_destroy(void)
389 {
390         content_debug("\n============Filter Create============\n\n");
391
392         int ret = MEDIA_CONTENT_ERROR_NONE;
393
394         ret = media_filter_destroy(g_filter);
395
396         return ret;
397 }
398
399 int test_connect_database(void)
400 {
401         int ret = MEDIA_CONTENT_ERROR_NONE;
402
403         content_debug("\n============DB Connection Test============\n\n");
404
405         ret = media_content_connect();
406
407         if (ret == MEDIA_CONTENT_ERROR_NONE)
408                 content_debug("connection is success\n\n");
409         else
410                 content_error("connection is failed\n\n");
411
412         return ret;
413 }
414
415 int test_gallery_scenario(void)
416 {
417         int ret = MEDIA_CONTENT_ERROR_NONE;
418         unsigned int i = 0;
419         filter_h filter = NULL;
420
421         int count;
422         GList *folder_list = NULL;
423         media_folder_h folder_handle = NULL;
424
425         /* First, Get folder list */
426         ret = media_folder_foreach_folder_from_db(filter, gallery_folder_list_cb, &folder_list);
427         if (ret != MEDIA_CONTENT_ERROR_NONE) {
428                 content_error("media_folder_foreach_folder_from_db failed: %d", ret);
429                 return -1;
430         } else {
431                 content_debug("media_folder_foreach_folder_from_db success!!");
432                 char *folder_id = NULL;
433
434                 for (i = 0; i < g_list_length(folder_list); i++) {
435                         folder_handle = (media_folder_h)g_list_nth_data(folder_list, i);
436
437                         get_folder_meta(folder_handle, &folder_id);
438
439                         ret = media_folder_get_media_count_from_db(folder_id, filter, &count);
440                         SAFE_FREE(folder_id);
441                         if (ret != MEDIA_CONTENT_ERROR_NONE) {
442                                 content_error("media_folder_get_media_count_from_db failed: %d", ret);
443                                 return -1;
444                         } else {
445                                 content_debug("media count [%d] : %d", i, count);
446                         }
447                 }
448         }
449
450         /* To check performance */
451         struct timeval start, end;
452         gettimeofday(&start, NULL);
453
454         /* Second, Get all item list */
455         media_info_h media_handle = NULL;
456         GList *all_item_list = NULL;
457
458         media_content_collation_e collate_type = MEDIA_CONTENT_COLLATE_NOCASE;
459         media_content_order_e order_type = MEDIA_CONTENT_ORDER_DESC;
460         ret = media_filter_create(&filter);
461         if (ret != MEDIA_CONTENT_ERROR_NONE) {
462                 content_error("Fail to create filter");
463                 return ret;
464         }
465         ret = media_filter_set_condition(filter, "MEDIA_TYPE = 0", collate_type);
466         if (ret != MEDIA_CONTENT_ERROR_NONE) {
467                 media_filter_destroy(filter);
468                 content_error("Fail to set condition");
469                 return ret;
470         }
471         ret = media_filter_set_order(filter, order_type, MEDIA_DISPLAY_NAME, collate_type);
472         if (ret != MEDIA_CONTENT_ERROR_NONE) {
473                 media_filter_destroy(filter);
474                 content_error("Fail to set order");
475                 return ret;
476         }
477
478         ret = media_info_foreach_media_from_db(filter, gallery_media_item_cb, &all_item_list);
479         if (ret != MEDIA_CONTENT_ERROR_NONE) {
480                 content_error("media_info_foreach_media_from_db failed: %d", ret);
481                 media_filter_destroy(filter);
482                 return -1;
483         } else {
484                 content_debug("media_info_foreach_media_from_db success");
485
486                 for (i = 0; i < g_list_length(all_item_list); i++) {
487                         media_handle = (media_info_h)g_list_nth_data(all_item_list, i);
488                         get_media_meta(media_handle);
489                 }
490         }
491
492         media_filter_destroy(filter);
493         filter = NULL;
494
495         /* To check performance */
496         gettimeofday(&end, NULL);
497         long time = (end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec);
498         content_debug("Time : %ld\n", time);
499
500         /* Third, Get item list of a folder */
501         GList *item_list = NULL;
502
503         for (i = 0; i < g_list_length(folder_list); i++) {
504                 unsigned int j = 0;
505                 char *folder_id = NULL;
506                 folder_handle = (media_folder_h)g_list_nth_data(folder_list, i);
507
508                 get_folder_meta(folder_handle, &folder_id);
509
510                 ret = media_folder_foreach_media_from_db(folder_id, filter, gallery_media_item_cb, &item_list);
511                 SAFE_FREE(folder_id);
512
513                 if (ret != MEDIA_CONTENT_ERROR_NONE) {
514                         content_error("media_folder_foreach_media_from_db failed: %d", ret);
515                         return -1;
516                 } else {
517                         content_error("media_folder_foreach_media_from_db success!");
518
519                         for (j = 0; j < g_list_length(item_list); j++) {
520                                 media_handle = (media_info_h)g_list_nth_data(item_list, j);
521                                 get_media_meta(media_handle);
522                         }
523                 }
524         }
525
526         /* Remove folder list */
527         if (folder_list) {
528                 for (i = 0; i < g_list_length(folder_list); i++) {
529                         folder_handle = (media_folder_h)g_list_nth_data(folder_list, i);
530                         media_folder_destroy(folder_handle);
531                 }
532
533                 g_list_free(folder_list);
534         }
535
536         /* Remove all items list */
537         if (all_item_list) {
538                 for (i = 0; i < g_list_length(all_item_list); i++) {
539                         media_handle = (media_info_h)g_list_nth_data(all_item_list, i);
540                         ret = media_info_destroy(media_handle);
541                         if (ret != MEDIA_CONTENT_ERROR_NONE)
542                                 content_error("media_info_destroy failed: %d", ret);
543                 }
544
545                 g_list_free(all_item_list);
546         }
547
548         /* Remove items list */
549         if (item_list) {
550                 for (i = 0; i < g_list_length(item_list); i++) {
551                         media_handle = (media_info_h)g_list_nth_data(item_list, i);
552                         ret = media_info_destroy(media_handle);
553                         if (ret != MEDIA_CONTENT_ERROR_NONE)
554                                 content_error("media_info_destroy failed: %d", ret);
555                 }
556
557                 g_list_free(item_list);
558         }
559
560         return MEDIA_CONTENT_ERROR_NONE;
561 }
562
563 /*Get All Music file. sort by Title and not case sensitive*/
564 int test_get_all_music_files(void)
565 {
566         int ret = MEDIA_CONTENT_ERROR_NONE;
567         int media_count = 0;
568         filter_h filter;
569
570         /*Set Filter*/
571         const char *condition = "MEDIA_TYPE=3"; /*0-image, 1-video, 2-sound, 3-music, 4-other*/
572
573         ret = media_filter_create(&filter);
574         if (ret != MEDIA_CONTENT_ERROR_NONE) {
575                 content_error("Fail to create filter");
576                 return ret;
577         }
578         ret = media_filter_set_condition(filter, condition, MEDIA_CONTENT_COLLATE_LOCALIZED);
579         if (ret != MEDIA_CONTENT_ERROR_NONE) {
580                 media_filter_destroy(filter);
581                 content_error("Fail to set condition");
582                 return ret;
583         }
584         ret = media_filter_set_order(filter, MEDIA_CONTENT_ORDER_ASC, MEDIA_TITLE, MEDIA_CONTENT_COLLATE_LOCALIZED);
585         if (ret != MEDIA_CONTENT_ERROR_NONE) {
586                 media_filter_destroy(filter);
587                 content_error("Fail to set order");
588                 return ret;
589         }
590
591         /*Get Media Count*/
592         ret = media_info_get_media_count_from_db(filter, &media_count);
593         if (ret != MEDIA_CONTENT_ERROR_NONE) {
594                 media_filter_destroy(filter);
595                 content_error("Fail to get media count");
596                 return ret;
597         }
598
599         content_debug("media_count : [%d]", media_count);
600
601         ret = media_info_foreach_media_from_db(filter, media_item_cb, NULL);
602         if (ret != MEDIA_CONTENT_ERROR_NONE) {
603                 media_filter_destroy(filter);
604                 content_error("Fail to get media");
605                 return ret;
606         }
607
608         ret = media_filter_destroy(filter);
609
610         return ret;
611 }
612
613 int test_media_info_operation(void)
614 {
615         int ret = MEDIA_CONTENT_ERROR_NONE;
616         int media_count = 0;
617
618         content_debug("\n============Media info Test============\n\n");
619
620         test_filter_create();
621
622         ret = media_info_get_media_count_from_db(g_filter, &media_count);
623         if (ret != MEDIA_CONTENT_ERROR_NONE)
624                 content_error("media_info_get_media_count_from_db failed: %d", ret);
625         else
626                 content_debug("media_count : [%d]", media_count);
627
628         ret = media_info_foreach_media_from_db(g_filter, media_item_cb, NULL);
629         if (ret == MEDIA_CONTENT_ERROR_NONE)
630                 content_debug("media_info_foreach_media_from_db is success");
631         else
632                 content_error("media_info_foreach_media_from_db is failed");
633
634         test_filter_destroy();
635
636         return ret;
637 }
638
639 int test_folder_operation(void)
640 {
641         int ret = MEDIA_CONTENT_ERROR_NONE;
642         filter_h filter = NULL;
643         media_folder_h folder = NULL;
644         char *folder_id = NULL;
645         int count = 0;
646
647         content_debug("\n============Folder Test============\n\n");
648
649         test_filter_create();
650
651         ret = media_filter_create(&filter);
652         if (ret != MEDIA_CONTENT_ERROR_NONE) {
653                 content_error("[ERROR] media_folder_filter_create is failed");
654                 return ret;
655         }
656
657         media_filter_set_condition(filter, "MEDIA_TYPE = 0 or MEDIA_TYPE = 1", MEDIA_CONTENT_COLLATE_DEFAULT);  /*MEDIA_TYPE 0-image, 1-video, 2-sound, 3-music, 4-other*/
658         media_filter_set_offset(filter, 0, 5);
659         media_filter_set_order(filter, MEDIA_CONTENT_ORDER_DESC, MEDIA_PATH, MEDIA_CONTENT_COLLATE_NOCASE);
660
661         ret = media_folder_get_folder_count_from_db(filter, &count);
662         content_debug("Folder count : [%d]", count);
663
664         ret = media_folder_foreach_folder_from_db(filter, folder_list_cb, &folder);
665
666         filter_h m_filter = NULL;
667
668         ret = media_filter_create(&m_filter);
669         if (ret != MEDIA_CONTENT_ERROR_NONE) {
670                 test_filter_destroy();
671                 media_filter_destroy(filter);
672                 content_error("[ERROR] media_info_filter_create is failed");
673                 return ret;
674         }
675
676         media_filter_set_condition(m_filter, "MEDIA_TYPE=1", MEDIA_CONTENT_COLLATE_DEFAULT);    /*MEDIA_TYPE 0-image, 1-video, 2-sound, 3-music, 4-other*/
677         media_filter_set_offset(m_filter, 0, 5);
678         media_filter_set_order(m_filter, MEDIA_CONTENT_ORDER_DESC, MEDIA_PATH, MEDIA_CONTENT_COLLATE_NOCASE);
679
680         ret = media_folder_foreach_media_from_db(folder_id, m_filter, media_item_cb, NULL);
681         if (ret != MEDIA_CONTENT_ERROR_NONE)
682                 content_error("[ERROR] media_folder_foreach_media_from_db is failed, error code : %d", ret);
683
684         media_filter_destroy(filter);
685         media_filter_destroy(m_filter);
686
687         test_filter_destroy();
688
689         /* fix prevent: Resource Leak */
690         SAFE_FREE(folder_id);
691
692         return ret;
693 }
694
695 int test_album_list(void)
696 {
697         content_debug("\n============Album Test============\n\n");
698
699         int ret = MEDIA_CONTENT_ERROR_NONE;
700         int album_count = 0;
701         filter_h filter;
702
703         /*Set Filter*/
704         const char *condition = "MEDIA_TYPE=3"; /*0-image, 1-video, 2-sound, 3-music, 4-other*/
705
706         ret = media_filter_create(&filter);
707         if (ret != MEDIA_CONTENT_ERROR_NONE) {
708                 content_error("Fail to create filter");
709                 return ret;
710         }
711         ret = media_filter_set_condition(filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
712         if (ret != MEDIA_CONTENT_ERROR_NONE) {
713                 media_filter_destroy(filter);
714                 content_error("Fail to set condition");
715                 return ret;
716         }
717         ret = media_filter_set_order(filter, MEDIA_CONTENT_ORDER_ASC, MEDIA_ALBUM, MEDIA_CONTENT_COLLATE_NOCASE);
718         if (ret != MEDIA_CONTENT_ERROR_NONE) {
719                 media_filter_destroy(filter);
720                 content_error("Fail to set order");
721                 return ret;
722         }
723
724         ret = media_album_get_album_count_from_db(filter, &album_count);
725         if (ret != MEDIA_CONTENT_ERROR_NONE) {
726                 media_filter_destroy(filter);
727                 return ret;
728         } else {
729                 content_debug("album_count [%d]", album_count);
730         }
731
732         ret = media_album_foreach_album_from_db(filter, album_list_cb, NULL);
733         if (ret != MEDIA_CONTENT_ERROR_NONE)
734                 content_error("error media_album_foreach_album_from_db : [%d]", ret);
735
736         ret = media_filter_destroy(filter);
737         if (ret != MEDIA_CONTENT_ERROR_NONE)
738                 content_error("error media_filter_destroy : [%d]", ret);
739
740         return ret;
741 }
742
743 int test_group_operation(void)
744 {
745         content_debug("\n============Group Test============\n\n");
746
747         int ret = MEDIA_CONTENT_ERROR_NONE;
748         int group_count = 0;
749         int idx = 0;
750
751         ret = test_filter_create();
752         if (ret != MEDIA_CONTENT_ERROR_NONE) {
753                 content_error("[error(0x%08x)", ret);
754                 return ret;
755         }
756
757         for (idx = 0; idx < MEDIA_CONTENT_GROUP_MAX; idx++) {
758                 ret = media_group_get_group_count_from_db(g_filter, idx, &group_count);
759                 if (ret != MEDIA_CONTENT_ERROR_NONE) {
760                         test_filter_destroy();
761                         content_error("media_group_get_group_count_from_db fail. ret=[%d] idx=[%d]", ret, idx);
762                         return ret;
763                 } else {
764                         content_debug("[%2d]group_count [%d]", idx, group_count);
765                 }
766
767                 ret = media_group_foreach_group_from_db(g_filter, idx, group_list_cb, &idx);
768                 if (ret != MEDIA_CONTENT_ERROR_NONE)
769                         content_error("media_group_foreach_group_from_db failed: %d", ret);
770         }
771         ret = test_filter_destroy();
772
773         return ret;
774 }
775
776 int test_update_operation()
777 {
778         int ret = MEDIA_CONTENT_ERROR_NONE;
779         unsigned int i = 0;
780         media_info_h media_handle = NULL;
781         GList *all_item_list = NULL;
782
783         /* Get all item list */
784         ret = media_info_foreach_media_from_db(NULL, gallery_media_item_cb, &all_item_list);
785         if (ret != MEDIA_CONTENT_ERROR_NONE) {
786                 content_error("media_info_foreach_media_from_db failed: %d", ret);
787                 return -1;
788         } else {
789                 content_debug("media_info_foreach_media_from_db success");
790
791                 for (i = 0; i < g_list_length(all_item_list); i++) {
792                         media_handle = (media_info_h)g_list_nth_data(all_item_list, i);
793                         get_media_meta(media_handle);
794                 }
795         }
796
797         return MEDIA_CONTENT_ERROR_NONE;
798 }
799
800 int test_insert(void)
801 {
802         int ret = MEDIA_CONTENT_ERROR_NONE;
803         const char *path = tzplatform_mkpath(TZ_USER_IMAGES, "Default.jpg");
804 #if 0
805         const char *path = tzplatform_mkpath(TZ_USER_DOCUMENTS, "other.txt"));
806         char *path = NULL;
807 #endif
808         media_info_h media_item = NULL;
809         content_debug("\n============DB Insert Test============\n\n");
810
811         ret = media_info_insert_to_db(path, &media_item);
812
813         if ((ret == MEDIA_CONTENT_ERROR_NONE) && (media_item != NULL)) {
814                 content_debug("Insertion is success");
815         } else {
816                 content_error("Insertion is failed");
817                 ret = media_info_destroy(media_item);
818                 return ret;
819         }
820
821         char *media_id = NULL;
822
823         ret = media_info_get_media_id(media_item, &media_id);
824         if (ret != MEDIA_CONTENT_ERROR_NONE)
825                 content_error("media_info_get_media_id failed: %d", ret);
826         else
827                 content_debug("Media ID: %s", media_id);
828
829         SAFE_FREE(media_id);
830
831         ret = media_info_destroy(media_item);
832         if (ret != MEDIA_CONTENT_ERROR_NONE)
833                 content_error("media_info_destroy failed: %d", ret);
834
835         return ret;
836 }
837
838 int test_move(void)
839 {
840         int ret = MEDIA_CONTENT_ERROR_NONE;
841         const char *move_media_id = "60aea677-4742-408e-b5f7-f2628062d06d";
842         const char *dst_path = tzplatform_mkpath(TZ_USER_IMAGES, "XX/Default1.jpg");
843         media_info_h move_media = NULL;
844
845         ret = media_info_get_media_from_db(move_media_id, &move_media);
846         if (ret == MEDIA_CONTENT_ERROR_NONE)
847                 content_debug("media_info_get_media_from_db success");
848         else
849                 content_error("media_info_get_media_from_db failed: %d", ret);
850
851         content_debug("\n============DB Move Test============\n\n");
852
853         if (move_media) {
854                 ret = media_info_move_to_db(move_media, dst_path);
855
856                 if (ret == MEDIA_CONTENT_ERROR_NONE)
857                         content_debug("Move is success");
858                 else
859                         content_error("Move is failed");
860
861                 ret = media_info_destroy(move_media);
862         } else {
863                 content_debug("There is no item : %s", move_media_id);
864         }
865
866         return ret;
867 }
868
869 int test_create_thumbnail(const char *path)
870 {
871         int ret = MEDIA_CONTENT_ERROR_NONE;
872         media_info_h media;
873         char *thumb_path = NULL;
874
875         ret = media_info_insert_to_db(path, &media);
876         content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, ret, "media_info_insert_to_db failed [%d]", ret);
877
878         ret = media_info_generate_thumbnail(media);
879         if (ret != MEDIA_CONTENT_ERROR_NONE) {
880                 content_error("media_info_generate_thumbnail failed [%d]", ret);
881                 goto FINALIZE;
882         }
883
884         ret = media_info_get_thumbnail_path(media, &thumb_path);
885         if (ret != MEDIA_CONTENT_ERROR_NONE) {
886                 content_error("media_info_get_thumbnail_path failed [%d]", ret);
887                 goto FINALIZE;
888         }
889
890         content_debug("Thumbnail Path [%s]", thumb_path);
891         if (thumb_path)
892                 free(thumb_path);
893
894 FINALIZE:
895         media_info_destroy(media);
896         return ret;
897 }
898
899 int test_ebook_text_finder(const char *keyword)
900 {
901         int ret = MEDIA_CONTENT_ERROR_NONE;
902         char **book_path_list = NULL;
903         unsigned int book_path_len = 0;
904         unsigned int i = 0;
905         media_info_h media = NULL;
906         book_meta_h book = NULL;
907         char *s_value = NULL;
908         long long ms_time = 0;
909         struct timeval start_time;
910         struct timeval end_time;
911
912         gettimeofday(&start_time, NULL);
913
914         ret = book_meta_get_path_with_keyword(keyword, &book_path_list, &book_path_len);
915         if (ret == MEDIA_CONTENT_ERROR_NONE)
916                 content_debug("book_meta_get_path_with_keyword is success");
917         else
918                 content_error("book_meta_get_path_with_keyword is failed");
919
920         gettimeofday(&end_time, NULL);
921
922         for (i = 0; i < book_path_len; i++) {
923                 ret = media_info_get_media_from_db_by_path(book_path_list[i], &media);
924                 if (ret != MEDIA_CONTENT_ERROR_NONE)
925                         continue;
926
927                 content_debug("===============================");
928                 content_debug("[%d] %s", i, book_path_list[i]);
929                 content_debug("===============================");
930                 media_info_get_title(media, &s_value);
931                 content_debug("Title  : %s", s_value);
932                 g_free(s_value);
933                 s_value = NULL;
934
935                 ret = media_info_get_book(media, &book);
936                 if (ret != MEDIA_CONTENT_ERROR_NONE) {
937                         media_info_destroy(media);
938                         continue;
939                 }
940
941                 ret = book_meta_get_author(book, &s_value);
942                 if (ret == MEDIA_CONTENT_ERROR_NONE && s_value) {
943                         content_debug("Author : %s", s_value);
944                         g_free(s_value);
945                         s_value = NULL;
946                 }
947
948                 ret = book_meta_get_date(book, &s_value);
949                 if (ret == MEDIA_CONTENT_ERROR_NONE && s_value) {
950                         content_debug("Date   : %s", s_value);
951                         g_free(s_value);
952                         s_value = NULL;
953                 }
954
955                 content_debug("===============================");
956                 ret = book_meta_destroy(book);
957                 if (ret != MEDIA_CONTENT_ERROR_NONE)
958                         content_error("book_meta_destroy failed");
959
960                 ret = media_info_destroy(media);
961                 if (ret != MEDIA_CONTENT_ERROR_NONE)
962                         content_error("media_info_destroy failed");
963
964                 g_free(book_path_list[i]);
965         }
966
967         ms_time = (end_time.tv_sec * 1000LL + end_time.tv_usec / 1000) - (start_time.tv_sec * 1000LL + start_time.tv_usec/ 1000);
968         content_debug("Search Time [%lld]", ms_time);
969
970         g_free(book_path_list);
971
972         return ret;
973 }
974 int test_disconnect_database(void)
975 {
976         int ret = MEDIA_CONTENT_ERROR_NONE;
977         content_debug("\n============DB Disconnection Test============\n\n");
978
979         ret = media_content_disconnect();
980
981         if (ret == MEDIA_CONTENT_ERROR_NONE)
982                 content_debug("disconnection is success");
983         else
984                 content_error("disconnection is failed");
985
986         return ret;
987 }
988
989 static int g_total_photo_size = 0;
990 static int g_total_video_size = 0;
991 static int g_total_mp3_size = 0;
992 static int g_total_voice_memo_size = 0;
993
994 bool dft_cb(media_info_h media, void *user_data)
995 {
996         unsigned long long file_size = 0;
997         media_content_type_e media_type = -1;
998         char *mime_type = NULL;
999         int ret = MEDIA_CONTENT_ERROR_NONE;
1000
1001         if (media == NULL)
1002                 return true;
1003
1004         ret = media_info_get_media_type(media, &media_type);
1005         if (ret != MEDIA_CONTENT_ERROR_NONE)
1006                 content_error("media_info_get_media_type failed: %d", ret);
1007         ret = media_info_get_size(media, &file_size);
1008         if (ret != MEDIA_CONTENT_ERROR_NONE)
1009                 content_error("media_info_get_size failed: %d", ret);
1010         ret = media_info_get_mime_type(media, &mime_type);
1011         if (ret != MEDIA_CONTENT_ERROR_NONE)
1012                 content_error("media_info_get_mime_type failed: %d", ret);
1013
1014         if (media_type == MEDIA_CONTENT_TYPE_IMAGE)
1015                 g_total_photo_size += file_size;
1016         else if (media_type == MEDIA_CONTENT_TYPE_VIDEO)
1017                 g_total_video_size += file_size;
1018         else if (media_type == MEDIA_CONTENT_TYPE_SOUND)
1019                 g_total_voice_memo_size += file_size;
1020         else if (media_type == MEDIA_CONTENT_TYPE_MUSIC) {
1021                 if ((mime_type != NULL) && (!strcmp("audio/mpeg", mime_type)))
1022                         g_total_mp3_size += file_size;
1023                 else
1024                         g_total_voice_memo_size += file_size;
1025         } else
1026                 content_debug("invalid media_type");
1027
1028         SAFE_FREE(mime_type);
1029
1030         return true;
1031
1032 }
1033
1034 int DFT_test(void)
1035 {
1036         int ret = MEDIA_CONTENT_ERROR_NONE;
1037         content_debug("\n============DFT_test============\n\n");
1038
1039         filter_h filter = NULL;
1040         int media_cnt = 0;
1041
1042         /*MEDIA_TYPE 0-image, 1-video, 2-sound, 3-music, 4-other*/
1043
1044         ret = media_filter_create(&filter);
1045
1046 /*Internal Memory*/
1047         content_debug("[Internal Memory]\n");
1048         /*1. Photo ============================================================*/
1049         ret = media_filter_set_condition(filter, "MEDIA_TYPE=0", MEDIA_CONTENT_COLLATE_DEFAULT);
1050
1051         /*Get Photo Count*/
1052         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1053         content_debug("Photo count = [%d]\n", media_cnt);
1054
1055         /*Get Photo Size*/
1056         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1057         content_debug("Photo size = [%d]\n", g_total_photo_size);
1058
1059         /*2. Video ============================================================*/
1060         ret = media_filter_set_condition(filter, "MEDIA_TYPE=1", MEDIA_CONTENT_COLLATE_DEFAULT);
1061
1062         /*Get Video Count*/
1063         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1064         content_debug("Video count = [%d]\n", media_cnt);
1065
1066         /*Get Video Size*/
1067         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1068         content_debug("Video size = [%d]\n", g_total_video_size);
1069
1070         /*3. MP3 ============================================================*/
1071         ret = media_filter_set_condition(filter, "MEDIA_TYPE=3 AND MEDIA_MIME_TYPE=\"audio/mpeg\"", MEDIA_CONTENT_COLLATE_DEFAULT);
1072
1073         /*Get MP3 Count*/
1074         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1075         content_debug("MP3 count = [%d]\n", media_cnt);
1076
1077         /*Get MP3 Size*/
1078         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1079         content_debug("MP3 size = [%d]\n", g_total_mp3_size);
1080
1081         /*4. Voice Memo ============================================================*/
1082         ret = media_filter_set_condition(filter, "(MEDIA_MIME_TYPE=\"audio/AMR\" OR MEDIA_MIME_TYPE=\"audio/mp4\")", MEDIA_CONTENT_COLLATE_DEFAULT);
1083
1084         /*Get Voice Memo Count*/
1085         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1086         content_debug("Voice Memo count = [%d]\n", media_cnt);
1087
1088         /*Get Voice Memo Size*/
1089         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1090         content_debug("Voice Memo size = [%d]\n", g_total_voice_memo_size);
1091
1092         g_total_photo_size = 0;
1093         g_total_video_size = 0;
1094         g_total_mp3_size = 0;
1095         g_total_voice_memo_size = 0;
1096
1097 /*External Memory*/
1098         content_debug("\n[External Memory]\n");
1099         /*1. Photo ============================================================*/
1100         ret = media_filter_set_condition(filter, "MEDIA_TYPE=0", MEDIA_CONTENT_COLLATE_DEFAULT);
1101
1102         /*Get Photo Count*/
1103         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1104         content_debug("Photo count = [%d]\n", media_cnt);
1105
1106         /*Get Photo Size*/
1107         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1108         content_debug("Photo size = [%d]\n", g_total_photo_size);
1109
1110         /*2. Video ============================================================*/
1111         ret = media_filter_set_condition(filter, "MEDIA_TYPE=1", MEDIA_CONTENT_COLLATE_DEFAULT);
1112
1113         /*Get Video Count*/
1114         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1115         content_debug("Video count = [%d]\n", media_cnt);
1116
1117         /*Get Video Size*/
1118         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1119         content_debug("Video size = [%d]\n", g_total_video_size);
1120
1121         /*3. MP3 ============================================================*/
1122         ret = media_filter_set_condition(filter, "MEDIA_TYPE=3 AND MEDIA_MIME_TYPE=\"audio/mpeg\"", MEDIA_CONTENT_COLLATE_DEFAULT);
1123
1124         /*Get MP3 Count*/
1125         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1126         content_debug("MP3 count = [%d]\n", media_cnt);
1127
1128         /*Get MP3 Size*/
1129         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1130         content_debug("MP3 size = [%d]\n", g_total_mp3_size);
1131
1132         /*4. Voice Memo ============================================================*/
1133         ret = media_filter_set_condition(filter, "(MEDIA_MIME_TYPE=\"audio/AMR\" OR MEDIA_MIME_TYPE=\"audio/mp4\")", MEDIA_CONTENT_COLLATE_DEFAULT);
1134
1135         /*Get Voice Memo Count*/
1136         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1137         content_debug("Voice Memo count = [%d]\n", media_cnt);
1138
1139         /*Get Voice Memo Size*/
1140         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1141         content_debug("Voice Memo size = [%d]\n", g_total_voice_memo_size);
1142         ret = media_filter_destroy(filter);
1143
1144         return ret;
1145 }
1146
1147 void insert_batch_cb(media_content_error_e error, void * user_data)
1148 {
1149         content_debug("media_info_insert_batch_to_db completed!\n");
1150 }
1151
1152 int test_batch_operations()
1153 {
1154         int ret = -1;
1155         int i = 0;
1156         char *file_list[10];
1157
1158         for (i = 0; i < 10; i++) {
1159                 file_list[i] = g_strdup_printf("%s%d.jpg", tzplatform_mkpath(TZ_USER_CONTENT, "test/image"), i+1);
1160                 content_debug("File : %s\n", file_list[i]);
1161         }
1162
1163         ret = media_info_insert_batch_to_db((const char **)file_list, 10, insert_batch_cb, NULL);
1164         if (ret != MEDIA_CONTENT_ERROR_NONE)
1165                 content_error("media_info_insert_batch_to_db failed : %d\n", ret);
1166
1167         for (i = 0; i < 10; i++)
1168                 g_free(file_list[i]);
1169
1170         return ret;
1171 }
1172
1173 void _scan_cb(media_content_error_e err, void *user_data)
1174 {
1175         content_debug("scan callback is called : %d\n", err);
1176         g_main_loop_quit(g_loop);
1177
1178         return;
1179 }
1180
1181 int test_scan_file()
1182 {
1183         int ret = -1;
1184
1185         const char *file_path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image1.jpg");
1186         ret = media_content_scan_file(file_path);
1187         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1188                 content_error("Fail to media_content_scan_file : %d", ret);
1189                 return ret;
1190         }
1191
1192         return 0;
1193 }
1194
1195 gboolean test_scan_dir_start(gpointer data)
1196 {
1197         int ret = -1;
1198
1199         const char *dir_path = tzplatform_getenv(TZ_USER_CONTENT);
1200
1201         ret = media_content_scan_folder(dir_path, TRUE, _scan_cb, NULL);
1202
1203         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1204                 content_error("Fail to test_scan_dir_start : %d", ret);
1205                 return ret;
1206         }
1207
1208         return 0;
1209 }
1210
1211 gboolean cancel_scan_dir_start(gpointer data)
1212 {
1213         int ret = MEDIA_CONTENT_ERROR_NONE;
1214
1215         const char *dir_path = tzplatform_getenv(TZ_USER_IMAGES);
1216
1217         ret = media_content_cancel_scan_folder(dir_path);
1218
1219         if (ret == MEDIA_CONTENT_ERROR_NONE)
1220                 content_debug("media_content_cancel_scan_folder is success");
1221         else
1222                 content_error("media_content_cancel_scan_folder is failed");
1223
1224         return false;
1225 }
1226
1227 int test_scan_dir(int cancel)
1228 {
1229         GSource *source = NULL;
1230         GMainContext *context = NULL;
1231
1232         g_loop = g_main_loop_new(NULL, FALSE);
1233         context = g_main_loop_get_context(g_loop);
1234         source = g_idle_source_new();
1235         g_source_set_callback(source, test_scan_dir_start, NULL, NULL);
1236         g_source_attach(source, context);
1237
1238         if (cancel) {
1239                 GSource *cancel_src = NULL;
1240                 cancel_src = g_idle_source_new();
1241                 g_source_set_callback(cancel_src, cancel_scan_dir_start, NULL, NULL);
1242                 g_source_attach(cancel_src, context);
1243         }
1244
1245         g_main_loop_run(g_loop);
1246         g_main_loop_unref(g_loop);
1247
1248         return 0;
1249 }
1250
1251 void _noti_cb(media_content_error_e error,
1252                                 int pid,
1253                                 media_content_db_update_item_type_e update_item,
1254                                 media_content_db_update_type_e update_type,
1255                                 media_content_type_e media_type,
1256                                 char *uuid,
1257                                 char *path,
1258                                 char *mime_type,
1259                                 void *user_data)
1260 {
1261         if (error == 0)
1262                 content_debug("noti success! : %d\n", error);
1263         else
1264                 content_debug("error occurred! : %d\n", error);
1265
1266         content_debug("Noti from PID(%d)\n", pid);
1267
1268         if (update_item == MEDIA_ITEM_FILE)
1269                 content_debug("Noti item : MEDIA_ITEM_FILE\n");
1270         else if (update_item == MEDIA_ITEM_DIRECTORY)
1271                 content_debug("Noti item : MEDIA_ITEM_DIRECTORY\n");
1272
1273         if (update_type == MEDIA_CONTENT_INSERT)
1274                 content_debug("Noti type : MEDIA_CONTENT_INSERT\n");
1275         else if (update_type == MEDIA_CONTENT_DELETE)
1276                 content_debug("Noti type : MEDIA_CONTENT_DELETE\n");
1277         else if (update_type == MEDIA_CONTENT_UPDATE)
1278                 content_debug("Noti type : MEDIA_CONTENT_UPDATE\n");
1279
1280         content_debug("content type : %d\n", media_type);
1281
1282         if (path)
1283                 content_debug("path : %s\n", path);
1284         else
1285                 content_debug("path not\n");
1286
1287         if (uuid)
1288                 content_debug("uuid : %s\n", uuid);
1289         else
1290                 content_debug("uuid not\n");
1291
1292         if (mime_type)
1293                 content_debug("mime_type : %s\n", mime_type);
1294         else
1295                 content_debug("mime not\n");
1296
1297         if (user_data) content_debug("String : %s\n", (char *)user_data);
1298
1299         return;
1300 }
1301
1302 void _noti_cb_2(media_content_error_e error,
1303                                 int pid,
1304                                 media_content_db_update_item_type_e update_item,
1305                                 media_content_db_update_type_e update_type,
1306                                 media_content_type_e media_type,
1307                                 char *uuid,
1308                                 char *path,
1309                                 char *mime_type,
1310                                 void *user_data)
1311 {
1312         if (error == 0)
1313                 content_debug("noti_2 success! : %d\n", error);
1314         else
1315                 content_debug("error occurred! : %d\n", error);
1316
1317         content_debug("Noti_2 from PID(%d)\n", pid);
1318
1319         g_main_loop_quit(g_loop);
1320         return;
1321 }
1322
1323 static media_content_noti_h noti_h;
1324 static media_content_noti_h noti_h_2;
1325
1326 gboolean _send_noti_operations(gpointer data)
1327 {
1328         int ret = MEDIA_CONTENT_ERROR_NONE;
1329
1330         /* First of all, noti subscription */
1331         char *user_str = strdup("hi");
1332         media_content_add_db_updated_cb(_noti_cb, (void*)user_str, &noti_h);
1333         media_content_add_db_updated_cb(_noti_cb_2, (void*)user_str, &noti_h_2);
1334
1335         /* media_info_insert_to_db */
1336         media_info_h media_item = NULL;
1337         const char *path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image1.jpg");
1338
1339         ret = media_info_insert_to_db(path, &media_item);
1340         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1341                 content_error("media_info_insert_to_db failed : %d", ret);
1342                 media_info_destroy(media_item);
1343                 return FALSE;
1344         }
1345
1346         content_debug("media_info_insert_to_db success");
1347
1348         media_content_remove_db_updated_cb(noti_h);
1349
1350         media_info_destroy(media_item);
1351
1352         return FALSE;
1353 }
1354
1355 int test_noti()
1356 {
1357         int ret = MEDIA_CONTENT_ERROR_NONE;
1358         GSource *source = NULL;
1359         GMainContext *context = NULL;
1360
1361         g_loop = g_main_loop_new(NULL, FALSE);
1362         context = g_main_loop_get_context(g_loop);
1363         source = g_idle_source_new();
1364         g_source_set_callback(source, _send_noti_operations, NULL, NULL);
1365         g_source_attach(source, context);
1366
1367         g_main_loop_run(g_loop);
1368         g_main_loop_unref(g_loop);
1369
1370         test_filter_destroy();
1371         media_content_remove_db_updated_cb(noti_h_2);
1372
1373         return ret;
1374 }
1375 #ifdef _USE_TVPD_MODE
1376 filter_h g_tv_filter = NULL;
1377
1378 int test_tv_filter_create(void)
1379 {
1380         content_debug("\n============Filter Create============\n\n");
1381
1382         int ret = MEDIA_CONTENT_ERROR_NONE;
1383
1384         ret = media_filter_create(&g_tv_filter);
1385
1386         ret = media_filter_set_storage(g_tv_filter, "0a22a163-e634-4a2e-ba14-0469a969eea0");
1387
1388         ret = media_filter_set_order(g_tv_filter, MEDIA_CONTENT_ORDER_ASC, MEDIA_TITLE, MEDIA_CONTENT_COLLATE_DEFAULT);
1389
1390         return ret;
1391 }
1392
1393 int test_tv_filter_destroy(void)
1394 {
1395         content_debug("\n============Filter Destroy============\n\n");
1396
1397         int ret = MEDIA_CONTENT_ERROR_NONE;
1398
1399         ret = media_filter_destroy(g_tv_filter);
1400
1401         return ret;
1402 }
1403
1404 bool pvr_item_cb(media_pvr_h pvr, void *user_data)
1405 {
1406         int ret = MEDIA_CONTENT_ERROR_NONE;
1407         char *c_value = NULL;
1408         int i_value = 0;
1409         bool b_value = false;
1410         unsigned long long l_value = 0;
1411
1412         if (pvr == NULL) {
1413                 content_debug("NO Item");
1414                 return true;
1415         }
1416
1417         ret = media_pvr_get_media_id(pvr, &c_value);
1418         if (ret != MEDIA_CONTENT_ERROR_NONE)
1419                 content_error("Fail to media_pvr_get_media_id");
1420         content_debug("media_id [%s]", c_value);
1421         SAFE_FREE(c_value);
1422
1423         ret = media_pvr_get_channel_name(pvr, &c_value);
1424         if (ret != MEDIA_CONTENT_ERROR_NONE)
1425                 content_error("Fail to media_pvr_get_channel_name");
1426         content_debug("channel_name [%s]", c_value);
1427         SAFE_FREE(c_value);
1428
1429         ret = media_pvr_get_program_title(pvr, &c_value);
1430         if (ret != MEDIA_CONTENT_ERROR_NONE)
1431                 content_error("Fail to media_pvr_get_program_title");
1432         content_debug("program_title [%s]", c_value);
1433         SAFE_FREE(c_value);
1434
1435         ret = media_pvr_get_program_crid(pvr, &c_value);
1436         if (ret != MEDIA_CONTENT_ERROR_NONE)
1437                 content_error("Fail to media_pvr_get_program_crid");
1438         content_debug("program_crid [%s]", c_value);
1439         SAFE_FREE(c_value);
1440
1441         ret = media_pvr_get_guidance(pvr, &c_value);
1442         if (ret != MEDIA_CONTENT_ERROR_NONE)
1443                 content_error("Fail to media_pvr_get_guidance");
1444         content_debug("guidance [%s]", c_value);
1445         SAFE_FREE(c_value);
1446
1447         ret = media_pvr_get_synopsis(pvr, &c_value);
1448         if (ret != MEDIA_CONTENT_ERROR_NONE)
1449                 content_error("Fail to media_pvr_get_synopsis");
1450         content_debug("synopsis [%s]", c_value);
1451         SAFE_FREE(c_value);
1452
1453         ret = media_pvr_get_genre(pvr, &c_value);
1454         if (ret != MEDIA_CONTENT_ERROR_NONE)
1455                 content_error("Fail to media_pvr_get_synopsis");
1456         content_debug("genre [%s]", c_value);
1457         SAFE_FREE(c_value);
1458
1459         ret = media_pvr_get_language(pvr, &c_value);
1460         if (ret != MEDIA_CONTENT_ERROR_NONE)
1461                 content_error("Fail to media_pvr_get_language");
1462         content_debug("language [%s]", c_value);
1463         SAFE_FREE(c_value);
1464
1465         ret = media_pvr_get_path(pvr, &c_value);
1466         if (ret != MEDIA_CONTENT_ERROR_NONE)
1467                 content_error("Fail to media_pvr_get_path");
1468         content_debug("path [%s]", c_value);
1469         SAFE_FREE(c_value);
1470
1471         ret = media_pvr_get_storage_id(pvr, &c_value);
1472         if (ret != MEDIA_CONTENT_ERROR_NONE)
1473                 content_error("Fail to media_pvr_get_storage_id");
1474         content_debug("storage_id [%s]", c_value);
1475         SAFE_FREE(c_value);
1476
1477         ret = media_pvr_get_size(pvr, &l_value);
1478         if (ret != MEDIA_CONTENT_ERROR_NONE)
1479                 content_error("Fail to media_pvr_get_size");
1480         content_debug("size [%lld]", l_value);
1481
1482         ret = media_pvr_get_timezone(pvr, &i_value);
1483         if (ret != MEDIA_CONTENT_ERROR_NONE)
1484                 content_error("Fail to media_pvr_get_timezone");
1485         content_debug("timezone [%d]", i_value);
1486
1487         ret = media_pvr_get_ptc(pvr, &i_value);
1488         if (ret != MEDIA_CONTENT_ERROR_NONE)
1489                 content_error("Fail to media_pvr_get_ptc");
1490         content_debug("ptc [%d]", i_value);
1491
1492         ret = media_pvr_get_major(pvr, &i_value);
1493         if (ret != MEDIA_CONTENT_ERROR_NONE)
1494                 content_error("Fail to media_pvr_get_major");
1495         content_debug("[%d]", i_value);
1496
1497         ret = media_pvr_get_minor(pvr, &i_value);
1498         if (ret != MEDIA_CONTENT_ERROR_NONE)
1499                 content_error("Fail to media_pvr_get_minor");
1500         content_debug("minor [%d]", i_value);
1501
1502         ret = media_pvr_get_channel_type(pvr, &i_value);
1503         if (ret != MEDIA_CONTENT_ERROR_NONE)
1504                 content_error("Fail to media_pvr_get_channel_type");
1505         content_debug("channel_type [%d]", i_value);
1506
1507         ret = media_pvr_get_program_num(pvr, &i_value);
1508         if (ret != MEDIA_CONTENT_ERROR_NONE)
1509                 content_error("Fail to media_pvr_get_program_num");
1510         content_debug("program_num [%d]", i_value);
1511
1512         unsigned int ui_value = 0;
1513         ret = media_pvr_get_service_profile(pvr, &ui_value);
1514         if (ret != MEDIA_CONTENT_ERROR_NONE)
1515                 content_error("Fail to media_pvr_get_service_profile");
1516         content_debug("service_profile [%u]", ui_value);
1517
1518         ret = media_pvr_get_duration(pvr, &i_value);
1519         if (ret != MEDIA_CONTENT_ERROR_NONE)
1520                 content_error("Fail to media_pvr_get_duration");
1521         content_debug("duration [%d]", i_value);
1522
1523         ret = media_pvr_get_embargo_time(pvr, &i_value);
1524         if (ret != MEDIA_CONTENT_ERROR_NONE)
1525                 content_error("Fail to media_pvr_get_embargo_time");
1526         content_debug("embargo_time [%d]", i_value);
1527
1528         ret = media_pvr_get_expiry_time(pvr, &i_value);
1529         if (ret != MEDIA_CONTENT_ERROR_NONE)
1530                 content_error("Fail to media_pvr_get_expiry_time");
1531         content_debug("expiry_time [%d]", i_value);
1532
1533         ret = media_pvr_get_parental_rating(pvr, &i_value);
1534         if (ret != MEDIA_CONTENT_ERROR_NONE)
1535                 content_error("Fail to media_pvr_get_parental_rating");
1536         content_debug("parental_rating [%d]", i_value);
1537
1538         ret = media_pvr_get_start_time(pvr, &i_value);
1539         if (ret != MEDIA_CONTENT_ERROR_NONE)
1540                 content_error("Fail to media_pvr_get_start_time");
1541         content_debug("start_time [%d]", i_value);
1542
1543         ret = media_pvr_get_program_start_time(pvr, &i_value);
1544         if (ret != MEDIA_CONTENT_ERROR_NONE)
1545                 content_error("Fail to media_pvr_get_program_start_time");
1546         content_debug("program_start_time [%d]", i_value);
1547
1548         ret = media_pvr_get_program_end_time(pvr, &i_value);
1549         if (ret != MEDIA_CONTENT_ERROR_NONE)
1550                 content_error("Fail to media_pvr_get_program_end_time");
1551         content_debug("program_end_time [%d]", i_value);
1552
1553         ret = media_pvr_get_program_date(pvr, &i_value);
1554         if (ret != MEDIA_CONTENT_ERROR_NONE)
1555                 content_error("Fail to media_pvr_get_program_date");
1556         content_debug("program_date [%d]", i_value);
1557
1558         ret = media_pvr_get_timer_record(pvr, &b_value);
1559         if (ret != MEDIA_CONTENT_ERROR_NONE)
1560                 content_error("Fail to media_pvr_get_timer_record");
1561         content_debug("timer_record [%d]", b_value);
1562
1563         ret = media_pvr_get_series_record(pvr, &b_value);
1564         if (ret != MEDIA_CONTENT_ERROR_NONE)
1565                 content_error("Fail to media_pvr_get_series_record");
1566         content_debug("series_record [%d]", b_value);
1567
1568         ret = media_pvr_get_hd(pvr, &i_value);
1569         if (ret != MEDIA_CONTENT_ERROR_NONE)
1570                 content_error("Fail to media_pvr_get_hd");
1571         content_debug("hd [%d]", i_value);
1572
1573         ret = media_pvr_get_subtitle(pvr, &b_value);
1574         if (ret != MEDIA_CONTENT_ERROR_NONE)
1575                 content_error("Fail to media_pvr_get_subtitle");
1576         content_debug("subtitle [%d]", b_value);
1577
1578         ret = media_pvr_get_ttx(pvr, &b_value);
1579         if (ret != MEDIA_CONTENT_ERROR_NONE)
1580                 content_error("Fail to media_pvr_get_ttx");
1581         content_debug("ttx [%d]", b_value);
1582
1583         ret = media_pvr_get_ad(pvr, &b_value);
1584         if (ret != MEDIA_CONTENT_ERROR_NONE)
1585                 content_error("Fail to media_pvr_get_ad");
1586         content_debug("ad [%d]", b_value);
1587
1588         ret = media_pvr_get_hard_of_hearing_radio(pvr, &b_value);
1589         if (ret != MEDIA_CONTENT_ERROR_NONE)
1590                 content_error("Fail to media_pvr_get_hard_of_hearing_radio");
1591         content_debug("hard_of_hearing_radio [%d]", b_value);
1592
1593         ret = media_pvr_get_data_service(pvr, &b_value);
1594         if (ret != MEDIA_CONTENT_ERROR_NONE)
1595                 content_error("Fail to media_pvr_get_data_service");
1596         content_debug("data_service [%d]", b_value);
1597
1598         ret = media_pvr_get_content_lock(pvr, &b_value);
1599         if (ret != MEDIA_CONTENT_ERROR_NONE)
1600                 content_error("Fail to media_pvr_get_content_lock");
1601         content_debug("content_lock [%d]", b_value);
1602
1603         ret = media_pvr_get_content_watch(pvr, &b_value);
1604         if (ret != MEDIA_CONTENT_ERROR_NONE)
1605                 content_error("Fail to media_pvr_get_content_watch");
1606         content_debug("content_watch [%d]", b_value);
1607
1608         ret = media_pvr_get_has_audio_only(pvr, &b_value);
1609         if (ret != MEDIA_CONTENT_ERROR_NONE)
1610                 content_error("Fail to media_pvr_get_has_audio_only");
1611         content_debug("has_audio_only [%d]", b_value);
1612
1613         ret = media_pvr_get_is_local_record(pvr, &b_value);
1614         if (ret != MEDIA_CONTENT_ERROR_NONE)
1615                 content_error("Fail to media_pvr_get_is_local_record");
1616         content_debug("is_local_record [%d]", b_value);
1617
1618         ret = media_pvr_get_resolution(pvr, (media_pvr_resolution_e*)&i_value);
1619         if (ret != MEDIA_CONTENT_ERROR_NONE)
1620                 content_error("Fail to media_pvr_get_resolution");
1621         content_debug("resolution [%d]", i_value);
1622
1623         ret = media_pvr_get_aspectratio(pvr, (media_pvr_aspectratio_e*)&i_value);
1624         if (ret != MEDIA_CONTENT_ERROR_NONE)
1625                 content_error("Fail to media_pvr_get_aspectratio");
1626         content_debug("aspectratio [%d]", i_value);
1627
1628         ret = media_pvr_get_highlight(pvr, &b_value);
1629         if (ret != MEDIA_CONTENT_ERROR_NONE)
1630                 content_error("Fail to media_pvr_get_highlight");
1631         content_debug("highlight [%d]", b_value);
1632
1633
1634         return TRUE;
1635 }
1636
1637 int test_pvr()
1638 {
1639         int ret = MEDIA_CONTENT_ERROR_NONE;
1640         int media_count = 0;
1641
1642         content_debug("\n============PVR Test============\n\n");
1643
1644         test_tv_filter_create();
1645
1646         ret = media_pvr_get_media_count_from_db(g_tv_filter, &media_count);
1647         if (ret != MEDIA_CONTENT_ERROR_NONE)
1648                 content_error("media_pvr_get_media_count_from_db failed: %d", ret);
1649         else
1650                 content_debug("media_count : [%d]", media_count);
1651
1652         ret = media_pvr_foreach_media_from_db(g_tv_filter, pvr_item_cb, NULL);
1653         if (ret != MEDIA_CONTENT_ERROR_NONE)
1654                 content_error("media_pvr_foreach_media_from_db is failed");
1655
1656         test_tv_filter_destroy();
1657
1658         return ret;
1659 }
1660
1661 int test_pvr_update_db(void)
1662 {
1663         int ret = MEDIA_CONTENT_ERROR_NONE;
1664         media_pvr_h pvr = NULL;
1665
1666         ret = media_pvr_get_pvr_from_db("ff9b5a9a-a7b4-47f4-8255-84e007e25f13", &pvr);
1667         if (ret != MEDIA_CONTENT_ERROR_NONE)
1668                 content_error("media_pvr_get_pvr_from_db failed: %d", ret);
1669
1670         ret = media_pvr_set_content_lock(pvr, TRUE);
1671         if (ret != MEDIA_CONTENT_ERROR_NONE)
1672                 content_error("Fail to media_pvr_set_content_lock");
1673
1674         ret = media_pvr_set_content_watch(pvr, TRUE);
1675         if (ret != MEDIA_CONTENT_ERROR_NONE)
1676                 content_error("Fail to media_pvr_set_content_watch");
1677
1678         ret = media_pvr_set_program_title(pvr, "TEST TITLE");
1679         if (ret != MEDIA_CONTENT_ERROR_NONE)
1680                 content_error("Fail to media_pvr_set_program_title");
1681
1682         ret = media_pvr_set_highlight(pvr, TRUE);
1683         if (ret != MEDIA_CONTENT_ERROR_NONE)
1684                 content_error("Fail to media_pvr_set_highlight");
1685
1686         ret = media_pvr_update_to_db(pvr);
1687         if (ret != MEDIA_CONTENT_ERROR_NONE)
1688                 content_error("Fail to media_pvr_update_to_db");
1689
1690         if (pvr != NULL)
1691                 media_pvr_destroy(pvr);
1692
1693         return ret;
1694 }
1695 #endif
1696
1697 int main(int argc, char *argv[])
1698 {
1699         int ret = MEDIA_CONTENT_ERROR_NONE;
1700
1701         content_debug("--- content manager test start ---\n\n");
1702
1703         ret = test_connect_database();
1704         if (ret != MEDIA_CONTENT_ERROR_NONE)
1705                 return MEDIA_CONTENT_ERROR_NONE;
1706 #ifdef _USE_TVPD_MODE
1707         test_pvr();
1708
1709         test_pvr_update_db();
1710
1711         test_pvr();
1712 #endif
1713
1714 #if 0
1715         if (argc == 2) {
1716                 ret = test_ebook_text_finder(argv[1]);
1717                 if (ret != MEDIA_CONTENT_ERROR_NONE)
1718                         return ret;
1719         }
1720
1721         ret = test_move();
1722         if (ret != MEDIA_CONTENT_ERROR_NONE)
1723                 return ret;
1724
1725         ret = test_gallery_scenario();
1726         if (ret != MEDIA_CONTENT_ERROR_NONE)
1727                 return ret;
1728
1729         ret = test_get_all_music_files();
1730         if (ret != MEDIA_CONTENT_ERROR_NONE)
1731                 return ret;
1732
1733         ret = test_media_info_operation();
1734         if (ret != MEDIA_CONTENT_ERROR_NONE)
1735                 return ret;
1736
1737         ret = test_folder_operation();
1738         if (ret != MEDIA_CONTENT_ERROR_NONE)
1739                 return ret;
1740
1741         ret = test_album_list();
1742         if (ret != MEDIA_CONTENT_ERROR_NONE)
1743                 return ret;
1744
1745         ret = test_group_operation();
1746         if (ret != MEDIA_CONTENT_ERROR_NONE)
1747                 return ret;
1748
1749         ret = test_update_operation();
1750         if (ret != MEDIA_CONTENT_ERROR_NONE)
1751                 return ret;
1752
1753         ret = test_insert();
1754         if (ret != MEDIA_CONTENT_ERROR_NONE)
1755                 return ret;
1756
1757         ret = test_move();
1758         if (ret != MEDIA_CONTENT_ERROR_NONE)
1759                 return ret;
1760
1761         if (argc == 2) {
1762                 ret = test_create_thumbnail(argv[1]);
1763                 if (ret != MEDIA_CONTENT_ERROR_NONE)
1764                         return ret;
1765         }
1766
1767         ret = DFT_test();
1768         if (ret != MEDIA_CONTENT_ERROR_NONE)
1769                 return ret;
1770
1771         ret = test_batch_operations();
1772         if (ret != MEDIA_CONTENT_ERROR_NONE)
1773                 return MEDIA_CONTENT_ERROR_NONE;
1774
1775         ret = test_scan_file();
1776         if (ret != MEDIA_CONTENT_ERROR_NONE)
1777                 return MEDIA_CONTENT_ERROR_NONE;
1778
1779         ret = test_scan_dir(true);
1780         if (ret != MEDIA_CONTENT_ERROR_NONE)
1781                 return MEDIA_CONTENT_ERROR_NONE;
1782
1783         ret = test_noti();
1784         if (ret != MEDIA_CONTENT_ERROR_NONE)
1785                 return MEDIA_CONTENT_ERROR_NONE;
1786 #endif
1787
1788         ret = test_disconnect_database();
1789         if (ret != MEDIA_CONTENT_ERROR_NONE)
1790                 return ret;
1791
1792         content_debug("--- content manager test end ---\n");
1793
1794         return ret;
1795 }