Create more unit tests for USB Devices and Printer Driver (#21)
All checks were successful
Build ptprnt / build (push) Successful in 2m59s

Reviewed-on: #21
This commit was merged in pull request #21.
This commit is contained in:
2025-10-21 18:55:32 +00:00
parent d8467b8984
commit 97edea85af
30 changed files with 2441 additions and 92 deletions

View File

@@ -0,0 +1,76 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
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 3 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <gmock/gmock.h>
#include "libusbwrap/LibUsbWrapper.hpp"
namespace libusbwrap {
/**
* @brief GMock implementation of ILibUsbWrapper for unit testing
*
* This mock allows tests to verify that UsbDevice and UsbDeviceFactory
* correctly interact with the libusb API without requiring actual USB hardware.
*/
class MockLibUsbWrapper : public ILibUsbWrapper {
public:
// Context management
MOCK_METHOD(int, init, (libusb_context * *ctx), (override));
MOCK_METHOD(void, exit, (libusb_context * ctx), (override));
// Device enumeration
MOCK_METHOD(ssize_t, getDeviceList, (libusb_context * ctx, libusb_device*** list), (override));
MOCK_METHOD(void, freeDeviceList, (libusb_device * *list, int unrefDevices), (override));
MOCK_METHOD(void, refDevice, (libusb_device * dev), (override));
MOCK_METHOD(void, unrefDevice, (libusb_device * dev), (override));
// Device descriptor
MOCK_METHOD(int, getDeviceDescriptor, (libusb_device * dev, libusb_device_descriptor* desc), (override));
// Device opening/closing
MOCK_METHOD(int, open, (libusb_device * dev, libusb_device_handle** handle), (override));
MOCK_METHOD(void, close, (libusb_device_handle * handle), (override));
// Device information
MOCK_METHOD(int, getSpeed, (libusb_device * dev), (override));
MOCK_METHOD(uint8_t, getBusNumber, (libusb_device * dev), (override));
MOCK_METHOD(uint8_t, getPortNumber, (libusb_device * dev), (override));
// Kernel driver management
MOCK_METHOD(int, kernelDriverActive, (libusb_device_handle * handle, int interfaceNo), (override));
MOCK_METHOD(int, detachKernelDriver, (libusb_device_handle * handle, int interfaceNo), (override));
// Interface management
MOCK_METHOD(int, claimInterface, (libusb_device_handle * handle, int interfaceNo), (override));
MOCK_METHOD(int, releaseInterface, (libusb_device_handle * handle, int interfaceNo), (override));
// Data transfer
MOCK_METHOD(int, bulkTransfer,
(libusb_device_handle * handle, uint8_t endpoint, unsigned char* data, int length, int* transferred,
unsigned int timeout),
(override));
// Error handling
MOCK_METHOD(const char*, errorName, (int errorCode), (override));
};
} // namespace libusbwrap

View File

@@ -0,0 +1,50 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
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 3 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <gmock/gmock.h>
#include "printers/interface/IPrinterDriver.hpp"
namespace ptprnt {
/**
* @brief GMock implementation of IPrinterDriver for unit testing
*
* This mock allows tests to verify printer interactions without
* requiring actual printer hardware.
*/
class MockPrinterDriver : public IPrinterDriver {
public:
MOCK_METHOD(std::string_view, getDriverName, (), (override));
MOCK_METHOD(std::string_view, getName, (), (override));
MOCK_METHOD(std::string_view, getVersion, (), (override));
MOCK_METHOD(libusbwrap::usbId, getUsbId, (), (override));
MOCK_METHOD(PrinterInfo, getPrinterInfo, (), (override));
MOCK_METHOD(PrinterStatus, getPrinterStatus, (), (override));
MOCK_METHOD(bool, attachUsbDevice, (std::shared_ptr<libusbwrap::IUsbDevice> usbHndl), (override));
MOCK_METHOD(bool, detachUsbDevice, (), (override));
MOCK_METHOD(bool, printBitmap, (const graphics::Bitmap<graphics::ALPHA8>& bitmap), (override));
MOCK_METHOD(bool, printMonochromeData, (const graphics::MonochromeData& data), (override));
MOCK_METHOD(bool, printLabel, (const std::unique_ptr<graphics::ILabel> label), (override));
MOCK_METHOD(bool, print, (), (override));
};
} // namespace ptprnt

View File

@@ -0,0 +1,57 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
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 3 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <gmock/gmock.h>
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap {
/**
* @brief GMock implementation of IUsbDevice for unit testing
*
* This mock allows tests to verify USB device interactions without
* requiring actual hardware or libusb context.
*/
class MockUsbDevice : public IUsbDevice {
public:
MOCK_METHOD(bool, open, (), (override));
MOCK_METHOD(void, close, (), (override));
// libusb wrappers
MOCK_METHOD(bool, detachKernelDriver, (int interfaceNo), (override));
MOCK_METHOD(bool, claimInterface, (int interfaceNo), (override));
MOCK_METHOD(bool, releaseInterface, (int interfaceNo), (override));
MOCK_METHOD(bool, bulkTransfer,
(uint8_t endpoint, const std::vector<uint8_t>& data, int* tx, unsigned int timeout), (override));
// getters
MOCK_METHOD(const usbId, getUsbId, (), (override));
MOCK_METHOD(const device::Speed, getSpeed, (), (override));
MOCK_METHOD(const uint8_t, getBusNumber, (), (override));
MOCK_METHOD(const uint8_t, getPortNumber, (), (override));
// errors
MOCK_METHOD(const Error, getLastError, (), (override));
MOCK_METHOD(const std::string, getLastErrorString, (), (override));
};
} // namespace libusbwrap

View File

@@ -0,0 +1,41 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
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 3 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <gmock/gmock.h>
#include "libusbwrap/interface/IUsbDeviceFactory.hpp"
namespace libusbwrap {
/**
* @brief GMock implementation of IUsbDeviceFactory for unit testing
*
* This mock allows tests to control USB device discovery without
* requiring actual libusb context or hardware.
*/
class MockUsbDeviceFactory : public IUsbDeviceFactory {
public:
MOCK_METHOD(bool, init, (), (override));
MOCK_METHOD(std::vector<std::unique_ptr<IUsbDevice>>, findAllDevices, (), (override));
MOCK_METHOD(std::vector<std::unique_ptr<IUsbDevice>>, findDevices, (uint16_t vid, uint16_t pid), (override));
};
} // namespace libusbwrap