Skip to content

Commit

Permalink
Merge 198816d into b3a85a3
Browse files Browse the repository at this point in the history
  • Loading branch information
nomis52 committed Nov 25, 2014
2 parents b3a85a3 + 198816d commit e6ef540
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 9 deletions.
123 changes: 123 additions & 0 deletions plugins/usbdmx/LibUsbThreadTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* LibUsbThreadTest.cpp
* Test fixture for the LibUsbThread class
* Copyright (C) 2014 Simon Newton
*/

#include <libusb.h>
#include <cppunit/extensions/HelperMacros.h>
#include <memory>
#include <string>
#include <vector>

#include "ola/Logging.h"
#include "ola/testing/TestUtils.h"
#include "plugins/usbdmx/LibUsbThread.h"

using std::auto_ptr;

namespace {
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000102)
int hotplug_callback(OLA_UNUSED struct libusb_context *ctx,
OLA_UNUSED struct libusb_device *dev,
OLA_UNUSED libusb_hotplug_event event,
OLA_UNUSED void *user_data) {
return 0;
}
#endif
} // namespace

class LibUsbThreadTest: public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(LibUsbThreadTest);
CPPUNIT_TEST(testNonHotplug);
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000102)
CPPUNIT_TEST(testHotplug);
#endif
CPPUNIT_TEST_SUITE_END();

public:
LibUsbThreadTest() : m_context(NULL) {}

void setUp();
void tearDown();

void testNonHotplug();
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000102)
void testHotplug();
#endif

private:
libusb_context *m_context;

void AttemptDeviceOpen(ola::plugin::usbdmx::LibUsbThread *thread);
};

CPPUNIT_TEST_SUITE_REGISTRATION(LibUsbThreadTest);

void LibUsbThreadTest::setUp() {
if (libusb_init(&m_context)) {
OLA_INFO << "Failed to init libusb";
}
}

void LibUsbThreadTest::tearDown() {
if (m_context) {
libusb_exit(m_context);
}
}

void LibUsbThreadTest::testNonHotplug() {
if (!m_context) {
return;
}

ola::plugin::usbdmx::LibUsbSimpleThread thread(m_context);
OLA_ASSERT_TRUE(thread.Init());
AttemptDeviceOpen(&thread);
}

#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000102)
void LibUsbThreadTest::testHotplug() {
ola::plugin::usbdmx::LibUsbHotplugThread thread(m_context, hotplug_callback,
NULL);
OLA_ASSERT_TRUE(thread.Init());
AttemptDeviceOpen(&thread);
}
#endif

/*
* Try to open any USB device so we can test interaction with the thread.
*/
void LibUsbThreadTest::AttemptDeviceOpen(
ola::plugin::usbdmx::LibUsbThread *thread) {
libusb_device_handle *usb_handle = NULL;
libusb_device **device_list;
size_t device_count = libusb_get_device_list(NULL, &device_list);

for (unsigned int i = 0; i < device_count; i++) {
libusb_device *usb_device = device_list[i];

if (libusb_open(usb_device, &usb_handle) == 0) {
thread->OpenHandle();
break;
}
}
if (usb_handle) {
thread->CloseHandle(usb_handle);
}
}

42 changes: 33 additions & 9 deletions plugins/usbdmx/Makefile.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
# LIBRARIES
##################################################
if USE_LIBUSB
noinst_LTLIBRARIES += plugins/usbdmx/libolausbdmxwidget.la
plugins_usbdmx_libolausbdmxwidget_la_SOURCES = \
plugins/usbdmx/LibUsbAdaptor.cpp \
plugins/usbdmx/LibUsbAdaptor.h \
plugins/usbdmx/LibUsbThread.cpp \
plugins/usbdmx/LibUsbThread.h \
plugins/usbdmx/Widget.h \
plugins/usbdmx/WidgetFactory.h
plugins_usbdmx_libolausbdmxwidget_la_CXXFLAGS = \
$(COMMON_CXXFLAGS) \
$(libusb_CFLAGS)
plugins_usbdmx_libolausbdmxwidget_la_LIBADD = \
$(libusb_LIBS) \
common/libolacommon.la

lib_LTLIBRARIES += plugins/usbdmx/libolausbdmx.la
plugins_usbdmx_libolausbdmx_la_SOURCES = \
plugins/usbdmx/AnymaDevice.cpp \
Expand All @@ -11,10 +26,6 @@ plugins_usbdmx_libolausbdmx_la_SOURCES = \
plugins/usbdmx/EuroliteProDevice.h \
plugins/usbdmx/EuroliteProOutputPort.cpp \
plugins/usbdmx/EuroliteProOutputPort.h \
plugins/usbdmx/LibUsbAdaptor.cpp \
plugins/usbdmx/LibUsbAdaptor.h \
plugins/usbdmx/LibUsbThread.cpp \
plugins/usbdmx/LibUsbThread.h \
plugins/usbdmx/LibUsbUtils.cpp \
plugins/usbdmx/LibUsbUtils.h \
plugins/usbdmx/FirmwareLoader.h \
Expand All @@ -31,10 +42,23 @@ plugins_usbdmx_libolausbdmx_la_SOURCES = \
plugins/usbdmx/VellemanDevice.cpp \
plugins/usbdmx/VellemanDevice.h \
plugins/usbdmx/VellemanOutputPort.cpp \
plugins/usbdmx/VellemanOutputPort.h \
plugins/usbdmx/Widget.h \
plugins/usbdmx/WidgetFactory.h
plugins/usbdmx/VellemanOutputPort.h
plugins_usbdmx_libolausbdmx_la_CXXFLAGS = $(COMMON_CXXFLAGS) $(libusb_CFLAGS)
plugins_usbdmx_libolausbdmx_la_LIBADD = $(libusb_LIBS) \
common/libolacommon.la
plugins_usbdmx_libolausbdmx_la_LIBADD = \
plugins/usbdmx/libolausbdmxwidget.la

# TESTS
##################################################
test_programs += \
plugins/usbdmx/LibUsbThreadTester

COMMON_USBDMX_TEST_LDADD = $(COMMON_TESTING_LIBS) \
$(libusb_LIBS) \
plugins/usbdmx/libolausbdmxwidget.la

plugins_usbdmx_LibUsbThreadTester_SOURCES = \
plugins/usbdmx/LibUsbThreadTest.cpp
plugins_usbdmx_LibUsbThreadTester_CXXFLAGS = $(COMMON_TESTING_FLAGS) \
$(libusb_CFLAGS)
plugins_usbdmx_LibUsbThreadTester_LDADD = $(COMMON_USBDMX_TEST_LDADD)
endif

0 comments on commit e6ef540

Please sign in to comment.