tizen 2.3 release
[framework/api/url-download.git] / TC / testcase / utc_download_start_positive.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 <tet_api.h>
18 #include <download.h>
19 #include <glib.h>
20 #include <glib-object.h>
21
22 static void startup(void);
23 static void cleanup(void);
24
25 void (*tet_startup)(void) = startup;
26 void (*tet_cleanup)(void) = cleanup;
27
28 static void utc_download_start_positive1(void);
29
30 struct tet_testlist tet_testlist[] = {
31         {utc_download_start_positive1, 1},
32         {NULL, 0},
33 };
34
35 //static url_download_h handle = NULL;
36 static GMainLoop* gloop = NULL;
37 static int is_download_success = true;
38
39 #define TEST_URL "http://static.campaign.naver.com/0/hangeul/2011/img/img_family.gif"
40 //#define TEST_URL "http://cdn.naver.com/naver/NanumFont/setup/NanumFontSetup_TTF_GOTHICLIGHT_hangeulcamp.exe"
41
42 static void state_cb (int download_id, download_state_e state, void *user_data)
43 {
44         const char *TC_NAME = __FUNCTION__;
45         int retcode = 0;
46         download_error_e err = DOWNLOAD_ERROR_NONE;
47         if (state == DOWNLOAD_STATE_COMPLETED || state == DOWNLOAD_STATE_DOWNLOADING) {
48                 is_download_success = true;
49                 g_main_loop_quit(gloop);
50         } else
51                 is_download_success = false;
52         
53         retcode = download_get_error(download_id, &err);
54         if (retcode != DOWNLOAD_ERROR_NONE) {
55                 dts_fail(TC_NAME, "Fail to get error");
56                 g_main_loop_quit(gloop);
57                 return;
58         }
59         dts_message(TC_NAME, "err[%d]", err);
60         if (err == DOWNLOAD_ERROR_INVALID_URL)
61                 dts_message(TC_NAME, "invaild url err");
62         else
63                 dts_message(TC_NAME, "another error[%d]", err);
64 }
65
66 static void startup(void)
67 {
68         g_type_init();
69         gloop = g_main_loop_new (NULL, 0);
70         is_download_success = true;
71 }
72
73 static void cleanup(void)
74 {
75         is_download_success = false;
76         g_main_loop_unref(gloop);
77         gloop = NULL;
78 }
79
80 static void utc_download_start_positive1(void)
81 {
82         const char *TC_NAME = __FUNCTION__;
83         int retcode = 0;
84         int id = 0;
85
86         retcode = download_create(&id);
87         if (retcode != DOWNLOAD_ERROR_NONE)     {
88                 dts_fail(TC_NAME,"Fail to create download handle");
89                 return;
90         }
91         retcode = download_set_url(id, TEST_URL);
92         if ( retcode != DOWNLOAD_ERROR_NONE ) {
93                 dts_fail(TC_NAME,"Fail to set url");
94                 return;
95         }
96         retcode = download_set_state_changed_cb(id, state_cb, NULL);
97         if (retcode != DOWNLOAD_ERROR_NONE)
98                 dts_fail(TC_NAME, "Fail to set callback");
99
100         retcode = download_start(id);
101
102         if (retcode == DOWNLOAD_ERROR_NONE)
103                 g_main_loop_run(gloop);
104         else
105                 is_download_success = false;
106
107         retcode = download_unset_state_changed_cb(id);
108         if ( retcode != DOWNLOAD_ERROR_NONE )
109                 dts_message(TC_NAME,"Fail to unset callback");
110         download_destroy(id);
111
112         if (is_download_success)
113                 dts_pass(TC_NAME, "download content is success");
114         else
115                 dts_fail(TC_NAME, "download content is not success");
116 }
117