-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathLibUsbGlobalTest.java
More file actions
167 lines (153 loc) · 4.26 KB
/
LibUsbGlobalTest.java
File metadata and controls
167 lines (153 loc) · 4.26 KB
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
/*
* Copyright (C) 2013 Klaus Reimer <[email protected]>
* See LICENSE.md for licensing information.
*/
package org.usb4java;
import static org.usb4java.test.UsbAssume.assumeUsbTestsEnabled;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.usb4java.Context;
import org.usb4java.Device;
import org.usb4java.DeviceList;
import org.usb4java.LibUsb;
/**
* Tests the global-scope methods of the {@link LibUsb} class which need a
* open USB context.
*
* @author Klaus Reimer ([email protected])
*/
public class LibUsbGlobalTest
{
/** The libusb context. */
private Context context;
/**
* Set up the test.
*/
@Before
public void setUp()
{
this.context = new Context();
try
{
LibUsb.init(this.context);
}
catch (final Throwable e)
{
this.context = null;
}
}
/**
* Tear down the test.
*/
@After
public void tearDown()
{
if (this.context != null)
{
LibUsb.exit(this.context);
}
}
/**
* Tests the {@link LibUsb#setDebug(Context, int)} method.
*/
@Test
@Deprecated
public void testSetDebug()
{
assumeUsbTestsEnabled();
LibUsb.setDebug(this.context, LibUsb.LOG_LEVEL_DEBUG);
LibUsb.setDebug(this.context, LibUsb.LOG_LEVEL_INFO);
LibUsb.setDebug(this.context, LibUsb.LOG_LEVEL_WARNING);
LibUsb.setDebug(this.context, LibUsb.LOG_LEVEL_ERROR);
LibUsb.setDebug(this.context, LibUsb.LOG_LEVEL_NONE);
}
/**
* Test getting and freeing the device list.
*/
@Test
public void testDeviceList()
{
assumeUsbTestsEnabled();
final DeviceList list = new DeviceList();
final int result = LibUsb.getDeviceList(this.context, list);
assertTrue(
"At least one USB device must be present for the simple unit tests",
result > 0);
assertEquals(result, list.getSize());
int i = 0;
for (final Device device : list)
{
assertNotNull(device);
assertEquals(device, list.get(i));
assertEquals(Device.class, device.getClass());
i++;
}
LibUsb.freeDeviceList(list, true);
}
/**
* Test {@link LibUsb#getDeviceList(Context, DeviceList)} without specifying
* a list container.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetDeviceListWithoutList()
{
assumeUsbTestsEnabled();
LibUsb.getDeviceList(this.context, null);
}
/**
* Checks the {@link LibUsb#le16ToCpu(short)} and
* {@link LibUsb#cpuToLe16(short)} methods.
*/
@Test
public void testEndianConversion()
{
assumeUsbTestsEnabled();
assertEquals(0x1234, LibUsb.le16ToCpu(LibUsb.cpuToLe16((short) 0x1234)));
}
/**
* Tests the {@link LibUsb#hasCapability(int)} method.
*/
@Test
public void testHasCapability()
{
assumeUsbTestsEnabled();
assertTrue(LibUsb.hasCapability(LibUsb.CAP_HAS_CAPABILITY));
assertFalse(LibUsb.hasCapability(0x12345678));
}
/**
* Tests the {@link LibUsb#errorName(int)} method.
*/
@Test
public void testErrorName()
{
assumeUsbTestsEnabled();
assertEquals("LIBUSB_ERROR_IO", LibUsb.errorName(LibUsb.ERROR_IO));
assertEquals("**UNKNOWN**", LibUsb.errorName(0x1234));
}
/**
* Tests the {@link LibUsb#strError(int)} method.
*/
@Test
public void testStrError()
{
assumeUsbTestsEnabled();
assertEquals("Input/Output Error", LibUsb.strError(LibUsb.ERROR_IO));
assertEquals("Other error", LibUsb.strError(0x1234));
}
/**
* Tests the {@link LibUsb#setLocale(String)} method.
*/
@Test
public void testSetLocale()
{
assumeUsbTestsEnabled();
assertEquals(LibUsb.SUCCESS, LibUsb.setLocale("en"));
assertEquals(LibUsb.ERROR_NOT_FOUND, LibUsb.setLocale("zz"));
assertEquals(LibUsb.ERROR_INVALID_PARAM, LibUsb.setLocale("zzz"));
}
}