Fix coverity issue
[platform/core/api/thumbnail-util.git] / test / thumbnail_util_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 <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <stdbool.h>
22 #include <glib.h>
23 #include <thumbnail_util.h>
24 #include <thumbnail_util_private.h>
25 #include <tzplatform_config.h>
26
27 #define MAX_SIZE 4096
28 GMainLoop *g_loop = NULL;
29 char *g_id = NULL;
30
31 void thumbnail_completed_cb(thumbnail_util_error_e error, const char *request_id, int raw_width, int raw_height, unsigned char *raw_data, int raw_size, void *user_data)
32 {
33 #if 0
34         FILE *fp;
35         char file_name[MAX_SIZE + 1] = {0, };
36
37         memset(file_name, 0, MAX_SIZE);
38         snprintf(file_name, MAX_SIZE, "test_%s.raw", request_id);
39         fp = fopen(file_name, "w");
40         fwrite(raw_data, 1, raw_size, fp);
41         fclose(fp);
42 #endif
43         thumbnail_util_debug("=================[RESULT]");
44         thumbnail_util_debug("error_code [%d]", error);
45         thumbnail_util_debug("request id [%s]", request_id);
46         thumbnail_util_debug("width [%d], height [%d]", raw_width, raw_height);
47         thumbnail_util_debug("raw_data [0x%x], size [%d]", *raw_data, raw_size);
48 #if 0
49         if (strncmp(request_id, "50", 2) == 0)
50                 g_main_loop_quit(g_loop);
51 #else
52         g_main_loop_quit(g_loop);
53 #endif
54 }
55
56 void __convert_i_to_a(unsigned int request_id, char **req_str)
57 {
58         char *buf = NULL;
59         buf = malloc(MAX_SIZE * sizeof(char));
60
61         if (buf != NULL) {
62                 snprintf(buf, MAX_SIZE, "%d", request_id);
63                 *req_str = strndup(buf, strlen(buf));
64                 SAFE_FREE(buf);
65         }
66 }
67
68 gboolean extract_thumbnail_start(gpointer data)
69 {
70         int ret = THUMBNAIL_UTIL_ERROR_NONE;
71 #if 0
72         int i;
73         thumbnail_h test_info;
74         thumbnail_util_create(&test_info);
75         thumbnail_util_set_size(test_info, 512, 288);
76
77         for (i = 0; i < 50; i++) {
78                 char filepath[255] = {0,};
79                 snprintf(filepath, sizeof(filepath), "%s%d.jpg", "/home/owner/content/Images/test_image", i + 1);
80                 thumbnail_util_debug("File : %s", filepath);
81                 thumbnail_util_set_path(test_info, filepath);
82                 ret = thumbnail_util_extract(test_info, thumbnail_completed_cb, test_info, &g_id);
83                 thumbnail_util_debug("GENERATED ID [%s]", g_id);
84         }
85 #else
86         thumbnail_h test_info;
87         thumbnail_util_create(&test_info);
88         thumbnail_util_set_path(test_info, tzplatform_mkpath(TZ_USER_IMAGES, "test_image1.jpg"));
89         thumbnail_util_set_size(test_info, 511, 288);
90         ret = thumbnail_util_extract(test_info, thumbnail_completed_cb, test_info, &g_id);
91         if (ret != THUMBNAIL_UTIL_ERROR_NONE)
92                 thumbnail_util_error("thumbnail_util_extract failed[%d]", ret);
93 #endif
94         thumbnail_util_destroy(test_info);
95
96         if (ret == THUMBNAIL_UTIL_ERROR_NONE)
97                 thumbnail_util_debug("extract raw data is success");
98         else
99                 thumbnail_util_error("extract raw data is failed");
100
101         return false;
102 }
103
104
105 gboolean cancel_all(gpointer data)
106 {
107         int ret = THUMBNAIL_UTIL_ERROR_NONE;
108         thumbnail_h _media_thumb;
109         thumbnail_util_create(&_media_thumb);
110
111         int i, num;
112         num = atoi(g_id);
113         char *req_str = NULL;
114         for (i = num - 20; i < num; i++) {
115                 __convert_i_to_a(i, &req_str);
116                 ret = thumbnail_util_cancel(_media_thumb, req_str);
117                 if (ret == THUMBNAIL_UTIL_ERROR_NONE)
118                         thumbnail_util_debug("thumbnail_util_cancel[%d] is success", i);
119                 else
120                         thumbnail_util_error("thumbnail_util_cancel[%d] is failed", i);
121                 if (req_str) {
122                         free(req_str);
123                         req_str = NULL;
124                 }
125         }
126         thumbnail_util_destroy(_media_thumb);
127
128         return false;
129 }
130
131 int test_extract_thumbnail(bool cancel)
132 {
133         int ret = THUMBNAIL_UTIL_ERROR_NONE;
134         GSource *source = NULL;
135         GMainContext *context = NULL;
136
137         g_loop = g_main_loop_new(NULL, false);
138         context = g_main_loop_get_context(g_loop);
139         source = g_idle_source_new();
140         g_source_set_callback(source, extract_thumbnail_start, NULL, NULL);
141         g_source_attach(source, context);
142
143         /* Logic to cancel */
144         if (cancel) {
145                 GSource *cancel_src = NULL;
146                 cancel_src = g_idle_source_new();
147                 g_source_set_callback(cancel_src, cancel_all, NULL, NULL);
148                 g_source_attach(cancel_src, context);
149         }
150
151         g_main_loop_run(g_loop);
152         g_main_loop_unref(g_loop);
153
154         return ret;
155 }
156
157 int main(int argc, char *argv[])
158 {
159         int ret = THUMBNAIL_UTIL_ERROR_NONE;
160         unsigned char *data = NULL;
161         unsigned int width = 0;
162         unsigned int height = 0;
163         size_t size = 0;
164         FILE *fp;
165         char file_name[MAX_SIZE + 1] = {0, };
166
167         thumbnail_util_debug("--- Thumbnail util test start ---");
168 #if 0
169         ret = test_extract_thumbnail(false);
170         if (ret != THUMBNAIL_UTIL_ERROR_NONE)
171                 thumbnail_util_debug("test_extract_thumbnail failed[%d]", ret);
172 #endif
173         thumbnail_util_debug("--- thumbnail_util_extract_to_file start :: IMAGE ---");
174         ret = thumbnail_util_extract_to_file(tzplatform_mkpath(TZ_USER_IMAGES, "test_image1.jpg"), 320, 240, tzplatform_mkpath(TZ_USER_IMAGES, "save_image1.jpg"));
175         if (ret != THUMBNAIL_UTIL_ERROR_NONE)
176                 thumbnail_util_debug("thumbnail_util_extract_to_file failed[%d]", ret);
177
178         thumbnail_util_debug("--- thumbnail_util_extract_to_buffer start :: IMAGE ---");
179         ret = thumbnail_util_extract_to_buffer(tzplatform_mkpath(TZ_USER_IMAGES, "test_image1.jpg"), 320, 240, &data, &size, &width, &height);
180         if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
181                 thumbnail_util_debug("thumbnail_util_extract_to_buffer failed[%d]", ret);
182         } else {
183                 memset(file_name, 0, sizeof(file_name));
184             snprintf(file_name, MAX_SIZE, "%s/test_image1.raw", tzplatform_getenv(TZ_USER_IMAGES));
185                 fp = fopen(file_name, "w");
186                 fwrite(data, 1, size, fp);
187                 fclose(fp);
188
189                 SAFE_FREE(data);
190         }
191
192         thumbnail_util_debug("--- thumbnail_util_extract_to_file start :: VIDEO ---");
193         ret = thumbnail_util_extract_to_file(tzplatform_mkpath(TZ_USER_IMAGES, "test_video1.mp4"), 320, 240, tzplatform_mkpath(TZ_USER_IMAGES, "save_video1.jpg"));
194         if (ret != THUMBNAIL_UTIL_ERROR_NONE)
195                 thumbnail_util_debug("thumbnail_util_extract_to_file failed[%d]", ret);
196
197         thumbnail_util_debug("--- thumbnail_util_extract_to_buffer start :: VIDEO ---");
198         ret = thumbnail_util_extract_to_buffer(tzplatform_mkpath(TZ_USER_IMAGES, "test_video1.mp4"), 320, 240, &data, &size, &width, &height);
199         if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
200                 thumbnail_util_debug("thumbnail_util_extract_to_buffer failed[%d]", ret);
201         } else {
202                 memset(file_name, 0, sizeof(file_name));
203             snprintf(file_name, MAX_SIZE, "%s/test_video1.raw", tzplatform_getenv(TZ_USER_IMAGES));
204                 fp = fopen(file_name, "w");
205                 fwrite(data, 1, size, fp);
206                 fclose(fp);
207
208                 SAFE_FREE(data);
209         }
210
211         thumbnail_util_debug("--- Thumbnail util test end ---");
212
213         return 0;
214 }