summaryrefslogtreecommitdiff
path: root/unittest/gtest_hal_usb_client.cpp
blob: 3859030df2442fd27d88db3d12803a92bde8bd03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

#include <iostream>
#include <gtest/gtest.h>
#include <system_info.h>
#include "hw/usb_client.h"
#include "unittest.h"

using namespace std;

/*
 * main class
 */
struct hw_info *info;
struct usb_client *client_dev;
struct usb_gadget *gadget_dev;
struct usb_gadget_id gadget_id;
struct usb_gadget_translator *gadget_translator;
static bool need_modelcheck = true;

#define MODEL_NAME      "http://tizen.org/system/model_name"

class USBCLIENTHalTest : public testing::Test
{
	public:
		virtual void SetUp()
		{
			int ret;
			char *model_name = NULL;

			if (need_modelcheck) {
				ret = system_info_get_platform_string(MODEL_NAME, &model_name);
				EXPECT_EQ(SYSTEM_INFO_ERROR_NONE, ret) << "system_info_get_platform_bool failed";

				if (!strncmp(model_name, "artik", 5)) {
					supported = true;
				} else {
					supported = false;
				}
				cout << "supported " << supported << endl;
				need_modelcheck = false;
			}
		}

		virtual void TearDown()
		{

		}
};

/*
 * testcase
 */
TEST_F(USBCLIENTHalTest, InitP)
{
	int ret;

	if (!supported)
		return;

	ret = hw_get_info(USB_CLIENT_HARDWARE_DEVICE_ID,
			(const struct hw_info **)&info);
	if (ret < 0) {
		cout << "There is no device for usb client" << ret << endl;
		return;
	} else {
		EXPECT_EQ(ret, 0) << "Fail to get hal for usb client (" << ret << ")";
	}
	if (!info || !info->open) {
		cout << "There is no function for info open" << endl;
		return;
	}
	ret = info->open(info, NULL, (struct hw_common**)&client_dev);
	EXPECT_EQ(ret, 0) << "Fail to open usb client device (" << ret << ")";
	EXPECT_NE(client_dev, nullptr) << "Fail to get usb client device structure";

	ret = hw_get_info(USB_GADGET_DEVICE_ID,
			(const struct hw_info **)&info);
	if (ret < 0) {
		cout << "There is no device for usb gadget" << ret << endl;
		return;
	} else {
		EXPECT_EQ(ret, 0) << "Fail to get hal for usb gadget (" << ret << ")";
	}

	if (!info || !info->open) {
		cout << "There is no function for info open" << endl;
		return;
	}
	ret = info->open(info, NULL, (struct hw_common**)&gadget_translator);
	EXPECT_EQ(ret, 0) << "Fail to open usb gadget device (" << ret << ")";
}

TEST_F(USBCLIENTHalTest, EnableP)
{
	int ret;
	char str[256];

	if (!supported)
		return;

	//dummy code to prevent error for not used function
	snprintf(str, 255, "%s,", _available_funcs[2]->name);

	if (!client_dev || !client_dev->enable) {
		cout << "There is no function for enable" << endl;
		return;
	}
	ret = client_dev->enable(client_dev);
	EXPECT_EQ(ret, 0) << "Fail to enable (" << ret << ")";
}

TEST_F(USBCLIENTHalTest, ReConfigureGadgetP)
{
	int ret;

	if (!supported)
		return;

	if (!gadget_translator || !gadget_translator->id_to_gadget) {
		cout << "There is no function for id_to_gadget" << endl;
		return;
	}
	gadget_id.function_mask = 7;
	ret = gadget_translator->id_to_gadget(&gadget_id, &gadget_dev);
	EXPECT_EQ(ret, 0) << "Fail to id_to_gadget (" << ret << ")";

	if (!client_dev || !client_dev->disable) {
		cout << "There is no function for disable" << endl;
		return;
	}
	ret = client_dev->disable(client_dev);
	EXPECT_EQ(ret, 0) << "Fail to disable (" << ret << ")";

	if (!client_dev || !client_dev->reconfigure_gadget) {
		cout << "There is no function for reconfigure_gadget" << endl;
		return;
	}
	ret = client_dev->reconfigure_gadget(client_dev, gadget_dev);
	EXPECT_EQ(ret, 0) << "Fail to reconfigure_gadget (" << ret << ")";
}

TEST_F(USBCLIENTHalTest, IsGadgetSupportedP)
{
	bool flag;

	if (!supported)
		return;

	if (!client_dev || !client_dev->is_gadget_supported) {
		cout << "There is no function for is_gadget_supported" << endl;
		return;
	}
	flag = client_dev->is_gadget_supported(client_dev, gadget_dev);
	EXPECT_EQ(flag, true) << "Fail to is_gadget_supported (" << flag << ")";
}

TEST_F(USBCLIENTHalTest, IsFunctionSupportedP)
{
	bool flag;

	if (!supported)
		return;

	if (!client_dev || !client_dev->is_function_supported) {
		cout << "There is no function for is_function_supported" << endl;
		return;
	}
	flag = client_dev->is_function_supported(client_dev, *gadget_dev->funcs);
	EXPECT_EQ(flag, true) << "Fail to is_function_supported (" << flag << ")";
}

TEST_F(USBCLIENTHalTest, GetCurrentGadgetP)
{
	int ret;

	if (!supported)
		return;

	if (!client_dev || !client_dev->get_current_gadget) {
		cout << "There is no function for get_current_gadget" << endl;
		return;
	}
	ret = client_dev->get_current_gadget(client_dev, &gadget_dev);
	EXPECT_EQ(ret, 0) << "Fail to get_current_gadget (" << ret << ")";
}

TEST_F(USBCLIENTHalTest, DisableP)
{
	int ret;

	if (!supported)
		return;

	if (!client_dev || !client_dev->disable) {
		cout << "There is no function for disable" << endl;
		return;
	}
	ret = client_dev->disable(client_dev);
	EXPECT_EQ(ret, 0) << "Fail to disable (" << ret << ")";
}

TEST_F(USBCLIENTHalTest, FreeGadgetP)
{
	if (!supported)
		return;

	if (!client_dev || !client_dev->free_gadget) {
		cout << "There is no function for free_gadget" << endl;
		return;
	}
	client_dev->free_gadget(gadget_dev);
}

TEST_F(USBCLIENTHalTest, DeinitP)
{
	int ret;

	if (!supported)
		return;

	if (!info || !info->close) {
		cout << "There is no function for info close" << endl;
		return;
	}
	ret = info->close((struct hw_common *)client_dev);
	EXPECT_EQ(ret, 0) << "Fail to close usb client device (" << ret << ")";
}

int main(int argc, char **argv)
{
	testing::InitGoogleTest(&argc, argv);

	return RUN_ALL_TESTS();
}