0ffd32c07e81e4776f0aa8a17183fc6c9c88f72c
[platform/core/api/base-utils.git] / unittest / tct-base-utils-core.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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 #include <stdio.h>
17 #include <string.h>
18 #include <malloc.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/wait.h>
23 #include <stdbool.h>
24 #include "tct_common.h"
25
26 #if 0
27 #include <glib.h>
28 #include <app.h>
29 #include <dlog.h>
30 #endif
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 #ifdef MOBILE
37 #include "tct-base-utils-core_mobile.h"
38 #endif
39
40 #ifdef WEARABLE
41 #include "tct-base-utils-core_wearable.h"
42 #endif
43 #ifdef TIZENIOT
44 #include "tct-base-utils-core_tizeniot.h"
45 #endif
46
47 #ifdef TV
48 #include "tct-base-utils-core_tv.h"
49 #endif
50
51 #ifdef __cplusplus
52 }
53 #endif
54
55
56 #include <gtest/gtest.h>
57
58
59 #if 0 /* KDH */
60 static bool app_create(void *data)
61 {
62         return true;
63 }
64
65 static void app_control(app_control_h app_control, void *data)
66 {
67         char* pszGetTCName = NULL;
68         int i=0, result=0, nRet=0;
69         nRet = app_control_get_extra_data(app_control, "testcase_name", &pszGetTCName);
70         if(nRet != APP_CONTROL_ERROR_NONE)
71         {
72                 dlog_print(DLOG_ERROR, "NativeTCT", "[%s:%d] app_control_get_extra_data returns error = %d", __FUNCTION__, __LINE__, nRet);
73                 PRINT_UTC_LOG("\\n[%s][Line : %d]Unable to fetch test case name: app_control_get_extra_data API call fails\\n", __FILE__, __LINE__);
74                 PRINT_TC_RESULT("%d",1);
75                 FREE_MEMORY_TC(pszGetTCName);
76                 return;
77         }
78
79         dlog_print(DLOG_INFO, "NativeTCT", "[%s:%d] Executing TC Name = %s", __FUNCTION__, __LINE__, pszGetTCName);
80         for ( i = 0; tc_array[i].name; i++ )
81         {
82                 if ( 0 == strncmp(pszGetTCName, tc_array[i].name, strlen(pszGetTCName)) )
83                 {
84                         DUMP_UTC_ERRLOG();
85                         dlog_print(DLOG_INFO, "NativeTCT", "%s : Startup begin", pszGetTCName);
86                         if ( tc_array[i].startup )
87                         {
88                                 tc_array[i].startup();
89                         }
90                         dlog_print(DLOG_INFO, "NativeTCT", "%s : Startup end", pszGetTCName);
91
92                         dlog_print(DLOG_INFO, "NativeTCT", "%s : Body begin", pszGetTCName);
93                         result = tc_array[i].function();
94                         dlog_print(DLOG_INFO, "NativeTCT", "%s returns value = %d", pszGetTCName, result);
95                         dlog_print(DLOG_INFO, "NativeTCT", "%s : Body end", pszGetTCName);
96
97                         dlog_print(DLOG_INFO, "NativeTCT", "%s : Cleanup begin", pszGetTCName);
98                         if ( tc_array[i].cleanup )
99                         {
100                                 tc_array[i].cleanup();
101                         }
102                         dlog_print(DLOG_INFO, "NativeTCT", "%s : Cleanup end", pszGetTCName);
103
104                         CLOSE_UTC_ERRLOG();
105                         PRINT_TC_RESULT("%d",result);
106                         FREE_MEMORY_TC(pszGetTCName);
107                         return;
108                 }
109         }
110
111         dlog_print(DLOG_ERROR, "NativeTCT", "[%s:%d] Unable to execute %s : Unknown Test Case Name", __FUNCTION__, __LINE__, pszGetTCName);
112         PRINT_UTC_LOG("\\n[%s][Line : %d]Unable to execute %s : Unknown Test Case Name\\n", __FILE__, __LINE__, pszGetTCName);
113         PRINT_TC_RESULT("%d",1);
114         FREE_MEMORY_TC(pszGetTCName);
115         return;
116 }
117
118 static void app_terminate(void *data)
119 {
120         dlog_print(DLOG_INFO, "NativeTCT", "[%s:%d] Application Package is now Terminating", __FUNCTION__, __LINE__);
121 }
122
123 int main(int argc, char *argv[])
124 {
125         int ret = 0;
126
127
128         ui_app_lifecycle_callback_s event_callback = {0,};
129         event_callback.create = app_create;
130         event_callback.terminate = app_terminate;
131         event_callback.app_control = app_control;
132
133         //setting gcda file location for coverage
134         setenv("GCOV_PREFIX","/tmp",1);
135         dlog_print(DLOG_INFO, "NativeTCT", "[%s:%d] Coverage *.gcda File location set to /tmp/home/abuild/rpmbuild/BUILD/ ", __FUNCTION__, __LINE__);
136
137         dlog_print(DLOG_INFO, "NativeTCT", "[%s:%d] Application Main Function is Invoked", __FUNCTION__, __LINE__);
138         ret = ui_app_main(argc, argv, &event_callback, NULL);
139         if (ret != APP_ERROR_NONE)
140         {
141                 dlog_print(DLOG_ERROR, "NativeTCT", "Application ui_app_main call gets failed. err = %d", ret);
142                 PRINT_UTC_LOG("\\n[%s][Line : %d]Application ui_app_main call gets failed. err = %d\\n", __FILE__, __LINE__, ret);
143                 PRINT_TC_RESULT("%d",1);
144                 return ret;
145         }
146
147         dlog_print(DLOG_INFO, "NativeTCT", "[%s:%d] Application Package is Terminated", __FUNCTION__, __LINE__);
148         return ret;
149 }
150 #else
151
152 TEST(base_utils, base_utils){
153         char *pszGetTCName = NULL;
154     pszGetTCName = (char*)malloc( 256 );
155     memset( pszGetTCName, 0x00, 256);
156     strcpy( pszGetTCName, "utc_capi_base");
157         int i=0, result=0, nRet=0;
158
159         dlog_print(DLOG_INFO, "NativeTCT", "[%s:%d] Executing TC Name = %s", __FUNCTION__, __LINE__, pszGetTCName);
160
161     int successCnt = 0;
162     int errorCnt = 0;
163         for ( i = 0; tc_array[i].name; i++ )
164         {
165                 if ( 0 == strncmp(pszGetTCName, tc_array[i].name, strlen(pszGetTCName)) )
166                 {
167                         if ( tc_array[i].startup )
168                         {
169                                 tc_array[i].startup();
170                         }
171
172                         result = tc_array[i].function();
173
174             if( result == 0 ){
175                 successCnt++;
176             }
177             else {
178                             dlog_print(DLOG_INFO, "NativeTCT", "[Error][%d] %s returns value = %d", i, tc_array[i].name, result);
179                 errorCnt++;
180             }
181
182             EXPECT_EQ(result, 0);
183             // ASSERT_EQ(result, 0);
184
185                         if ( tc_array[i].cleanup )
186                         {
187                                 tc_array[i].cleanup();
188                         }
189                 }
190         else {
191                         dlog_print(DLOG_INFO, "NativeTCT", "Name check error! [%s][%s]", pszGetTCName, tc_array[i].name);
192             ASSERT_EQ(0, 1);
193         }
194         }
195
196         dlog_print(DLOG_INFO, "NativeTCT", "==========================");
197         dlog_print(DLOG_INFO, "NativeTCT", "Success [%4d / %4d]", successCnt, successCnt + errorCnt);
198         dlog_print(DLOG_INFO, "NativeTCT", "Error   [%4d / %4d]", errorCnt, successCnt + errorCnt);
199         dlog_print(DLOG_INFO, "NativeTCT", "==========================");
200
201         FREE_MEMORY_TC(pszGetTCName);
202 }
203
204 int main(int argc, char** argv) {
205     ::testing::InitGoogleTest(&argc, argv);
206     return RUN_ALL_TESTS();
207 }
208
209 #endif