dd86720c10f94786e8d35115e5d71a6ee300c20e
[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         }
122         thumbnail_util_destroy(_media_thumb);
123
124         return false;
125 }
126
127 int test_extract_thumbnail(bool cancel)
128 {
129         int ret = THUMBNAIL_UTIL_ERROR_NONE;
130         GSource *source = NULL;
131         GMainContext *context = NULL;
132
133         g_loop = g_main_loop_new(NULL, false);
134         context = g_main_loop_get_context(g_loop);
135         source = g_idle_source_new();
136         g_source_set_callback(source, extract_thumbnail_start, NULL, NULL);
137         g_source_attach(source, context);
138
139         /* Logic to cancel */
140         if (cancel) {
141                 GSource *cancel_src = NULL;
142                 cancel_src = g_idle_source_new();
143                 g_source_set_callback(cancel_src, cancel_all, NULL, NULL);
144                 g_source_attach(cancel_src, context);
145         }
146
147         g_main_loop_run(g_loop);
148         g_main_loop_unref(g_loop);
149
150         return ret;
151 }
152
153 int main(int argc, char *argv[])
154 {
155         int ret = THUMBNAIL_UTIL_ERROR_NONE;
156         unsigned char *data = NULL;
157         unsigned int width = 0;
158         unsigned int height = 0;
159         size_t size = 0;
160         FILE *fp;
161         char file_name[MAX_SIZE + 1] = {0, };
162
163         thumbnail_util_debug("--- Thumbnail util test start ---");
164 #if 0
165         ret = test_extract_thumbnail(false);
166         if (ret != THUMBNAIL_UTIL_ERROR_NONE)
167                 thumbnail_util_debug("test_extract_thumbnail failed[%d]", ret);
168 #endif
169         thumbnail_util_debug("--- thumbnail_util_extract_to_file start :: IMAGE ---");
170         ret = thumbnail_util_extract_to_file(tzplatform_mkpath(TZ_USER_IMAGES, "test_image1.jpg"), 320, 240, tzplatform_mkpath(TZ_USER_IMAGES, "save_image1.jpg"));
171         if (ret != THUMBNAIL_UTIL_ERROR_NONE)
172                 thumbnail_util_debug("thumbnail_util_extract_to_file failed[%d]", ret);
173
174         thumbnail_util_debug("--- thumbnail_util_extract_to_buffer start :: IMAGE ---");
175         ret = thumbnail_util_extract_to_buffer(tzplatform_mkpath(TZ_USER_IMAGES, "test_image1.jpg"), 320, 240, &data, &size, &width, &height);
176         if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
177                 thumbnail_util_debug("thumbnail_util_extract_to_buffer failed[%d]", ret);
178         } else {
179                 memset(file_name, 0, sizeof(file_name));
180             snprintf(file_name, MAX_SIZE, "%s/test_image1.raw", tzplatform_getenv(TZ_USER_IMAGES));
181                 fp = fopen(file_name, "w");
182                 fwrite(data, 1, size, fp);
183                 fclose(fp);
184
185                 SAFE_FREE(data);
186         }
187
188         thumbnail_util_debug("--- thumbnail_util_extract_to_file start :: VIDEO ---");
189         ret = thumbnail_util_extract_to_file(tzplatform_mkpath(TZ_USER_IMAGES, "test_video1.mp4"), 320, 240, tzplatform_mkpath(TZ_USER_IMAGES, "save_video1.jpg"));
190         if (ret != THUMBNAIL_UTIL_ERROR_NONE)
191                 thumbnail_util_debug("thumbnail_util_extract_to_file failed[%d]", ret);
192
193         thumbnail_util_debug("--- thumbnail_util_extract_to_buffer start :: VIDEO ---");
194         ret = thumbnail_util_extract_to_buffer(tzplatform_mkpath(TZ_USER_IMAGES, "test_video1.mp4"), 320, 240, &data, &size, &width, &height);
195         if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
196                 thumbnail_util_debug("thumbnail_util_extract_to_buffer failed[%d]", ret);
197         } else {
198                 memset(file_name, 0, sizeof(file_name));
199             snprintf(file_name, MAX_SIZE, "%s/test_video1.raw", tzplatform_getenv(TZ_USER_IMAGES));
200                 fp = fopen(file_name, "w");
201                 fwrite(data, 1, size, fp);
202                 fclose(fp);
203
204                 SAFE_FREE(data);
205         }
206
207         thumbnail_util_debug("--- Thumbnail util test end ---");
208
209         return 0;
210 }