Add cpp files for HAL test
[platform/core/system/libdevice-node.git] / unittest / gtest_hal_usb_client.cpp
1
2 #include <iostream>
3 #include <gtest/gtest.h>
4 #include <system_info.h>
5 #include "hw/usb_client.h"
6 #include "unittest.h"
7
8 using namespace std;
9
10 /*
11  * main class
12  */
13 struct hw_info *info;
14 struct usb_client *client_dev;
15 struct usb_gadget *gadget_dev;
16 struct usb_gadget_id gadget_id;
17 struct usb_gadget_translator *gadget_translator;
18 static bool need_modelcheck = true;
19
20 #define MODEL_NAME      "http://tizen.org/system/model_name"
21
22 class USBCLIENTHalTest : public testing::Test
23 {
24         public:
25                 virtual void SetUp()
26                 {
27                         int ret;
28                         char *model_name = NULL;
29
30                         if (need_modelcheck) {
31                                 ret = system_info_get_platform_string(MODEL_NAME, &model_name);
32                                 EXPECT_EQ(SYSTEM_INFO_ERROR_NONE, ret) << "system_info_get_platform_bool failed";
33
34                                 if (!strncmp(model_name, "artik", 5)) {
35                                         supported = true;
36                                 } else {
37                                         supported = false;
38                                 }
39                                 cout << "supported " << supported << endl;
40                                 need_modelcheck = false;
41                         }
42                 }
43
44                 virtual void TearDown()
45                 {
46
47                 }
48 };
49
50 /*
51  * testcase
52  */
53 TEST_F(USBCLIENTHalTest, InitP)
54 {
55         int ret;
56
57         if (!supported)
58                 return;
59
60         ret = hw_get_info(USB_CLIENT_HARDWARE_DEVICE_ID,
61                         (const struct hw_info **)&info);
62         if (ret < 0) {
63                 cout << "There is no device for usb client" << ret << endl;
64                 return;
65         } else {
66                 EXPECT_EQ(ret, 0) << "Fail to get hal for usb client (" << ret << ")";
67         }
68         if (!info || !info->open) {
69                 cout << "There is no function for info open" << endl;
70                 return;
71         }
72         ret = info->open(info, NULL, (struct hw_common**)&client_dev);
73         EXPECT_EQ(ret, 0) << "Fail to open usb client device (" << ret << ")";
74         EXPECT_NE(client_dev, nullptr) << "Fail to get usb client device structure";
75
76         ret = hw_get_info(USB_GADGET_DEVICE_ID,
77                         (const struct hw_info **)&info);
78         if (ret < 0) {
79                 cout << "There is no device for usb gadget" << ret << endl;
80                 return;
81         } else {
82                 EXPECT_EQ(ret, 0) << "Fail to get hal for usb gadget (" << ret << ")";
83         }
84
85         if (!info || !info->open) {
86                 cout << "There is no function for info open" << endl;
87                 return;
88         }
89         ret = info->open(info, NULL, (struct hw_common**)&gadget_translator);
90         EXPECT_EQ(ret, 0) << "Fail to open usb gadget device (" << ret << ")";
91 }
92
93 TEST_F(USBCLIENTHalTest, EnableP)
94 {
95         int ret;
96         char str[256];
97
98         if (!supported)
99                 return;
100
101         //dummy code to prevent error for not used function
102         snprintf(str, 255, "%s,", _available_funcs[2]->name);
103
104         if (!client_dev || !client_dev->enable) {
105                 cout << "There is no function for enable" << endl;
106                 return;
107         }
108         ret = client_dev->enable(client_dev);
109         EXPECT_EQ(ret, 0) << "Fail to enable (" << ret << ")";
110 }
111
112 TEST_F(USBCLIENTHalTest, ReConfigureGadgetP)
113 {
114         int ret;
115
116         if (!supported)
117                 return;
118
119         if (!gadget_translator || !gadget_translator->id_to_gadget) {
120                 cout << "There is no function for id_to_gadget" << endl;
121                 return;
122         }
123         gadget_id.function_mask = 7;
124         ret = gadget_translator->id_to_gadget(&gadget_id, &gadget_dev);
125         EXPECT_EQ(ret, 0) << "Fail to id_to_gadget (" << ret << ")";
126
127         if (!client_dev || !client_dev->disable) {
128                 cout << "There is no function for disable" << endl;
129                 return;
130         }
131         ret = client_dev->disable(client_dev);
132         EXPECT_EQ(ret, 0) << "Fail to disable (" << ret << ")";
133
134         if (!client_dev || !client_dev->reconfigure_gadget) {
135                 cout << "There is no function for reconfigure_gadget" << endl;
136                 return;
137         }
138         ret = client_dev->reconfigure_gadget(client_dev, gadget_dev);
139         EXPECT_EQ(ret, 0) << "Fail to reconfigure_gadget (" << ret << ")";
140 }
141
142 TEST_F(USBCLIENTHalTest, IsGadgetSupportedP)
143 {
144         bool flag;
145
146         if (!supported)
147                 return;
148
149         if (!client_dev || !client_dev->is_gadget_supported) {
150                 cout << "There is no function for is_gadget_supported" << endl;
151                 return;
152         }
153         flag = client_dev->is_gadget_supported(client_dev, gadget_dev);
154         EXPECT_EQ(flag, true) << "Fail to is_gadget_supported (" << flag << ")";
155 }
156
157 TEST_F(USBCLIENTHalTest, IsFunctionSupportedP)
158 {
159         bool flag;
160
161         if (!supported)
162                 return;
163
164         if (!client_dev || !client_dev->is_function_supported) {
165                 cout << "There is no function for is_function_supported" << endl;
166                 return;
167         }
168         flag = client_dev->is_function_supported(client_dev, *gadget_dev->funcs);
169         EXPECT_EQ(flag, true) << "Fail to is_function_supported (" << flag << ")";
170 }
171
172 TEST_F(USBCLIENTHalTest, GetCurrentGadgetP)
173 {
174         int ret;
175
176         if (!supported)
177                 return;
178
179         if (!client_dev || !client_dev->get_current_gadget) {
180                 cout << "There is no function for get_current_gadget" << endl;
181                 return;
182         }
183         ret = client_dev->get_current_gadget(client_dev, &gadget_dev);
184         EXPECT_EQ(ret, 0) << "Fail to get_current_gadget (" << ret << ")";
185 }
186
187 TEST_F(USBCLIENTHalTest, DisableP)
188 {
189         int ret;
190
191         if (!supported)
192                 return;
193
194         if (!client_dev || !client_dev->disable) {
195                 cout << "There is no function for disable" << endl;
196                 return;
197         }
198         ret = client_dev->disable(client_dev);
199         EXPECT_EQ(ret, 0) << "Fail to disable (" << ret << ")";
200 }
201
202 TEST_F(USBCLIENTHalTest, DeinitP)
203 {
204         int ret;
205
206         if (!supported)
207                 return;
208
209         if (!info || !info->close) {
210                 cout << "There is no function for info close" << endl;
211                 return;
212         }
213         ret = info->close((struct hw_common *)client_dev);
214         EXPECT_EQ(ret, 0) << "Fail to close usb client device (" << ret << ")";
215 }
216
217 int main(int argc, char **argv)
218 {
219         testing::InitGoogleTest(&argc, argv);
220
221         return RUN_ALL_TESTS();
222 }
223