Create more unit tests for USB Devices and Printer Driver #21

Merged
moritz merged 6 commits from up-coverage-1 into master 2025-10-21 18:55:33 +00:00
3 changed files with 44 additions and 25 deletions
Showing only changes of commit 1f57a802f3 - Show all commits

View File

@@ -138,6 +138,10 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
} }
bool P700Printer::printMonochromeData(const graphics::MonochromeData& data) { bool P700Printer::printMonochromeData(const graphics::MonochromeData& data) {
if (!mUsbHndl) {
spdlog::error("USB Handle is invalid!");
return false;
}
// Send initialization sequence // Send initialization sequence
// The INITIALIZE command needs to be sent as a 128-byte packet with ESC @ at the end // The INITIALIZE command needs to be sent as a 128-byte packet with ESC @ at the end
std::vector<uint8_t> initCmd(128, 0x00); std::vector<uint8_t> initCmd(128, 0x00);

View File

@@ -33,6 +33,7 @@ namespace libusbwrap {
*/ */
class MockUsbDeviceFactory : public IUsbDeviceFactory { class MockUsbDeviceFactory : public IUsbDeviceFactory {
public: public:
MOCK_METHOD(bool, init, (), (override));
MOCK_METHOD(std::vector<std::unique_ptr<IUsbDevice>>, findAllDevices, (), (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)); MOCK_METHOD(std::vector<std::unique_ptr<IUsbDevice>>, findDevices, (uint16_t vid, uint16_t pid), (override));
}; };

View File

@@ -25,32 +25,48 @@
#include "core/PrinterDriverFactory.hpp" #include "core/PrinterDriverFactory.hpp"
#include "core/PrinterService.hpp" #include "core/PrinterService.hpp"
#include "mocks/MockUsbDeviceFactory.hpp"
using ::testing::_; using ::testing::_;
using ::testing::Invoke;
using ::testing::NiceMock; using ::testing::NiceMock;
using ::testing::Return; using ::testing::Return;
namespace ptprnt::core { namespace ptprnt::core {
// Helper function to return empty vector of unique_ptrs
std::vector<std::unique_ptr<libusbwrap::IUsbDevice>> returnEmptyDeviceList() {
return {};
}
// Test fixture for PrinterService tests // Test fixture for PrinterService tests
class PrinterServiceTest : public ::testing::Test { class PrinterServiceTest : public ::testing::Test {
protected: protected:
void SetUp() override { void SetUp() override {
// Service under test // Create mock USB device factory
service = std::make_unique<PrinterService>(); auto mockFactory = std::make_unique<NiceMock<libusbwrap::MockUsbDeviceFactory>>();
mockFactoryPtr = mockFactory.get();
// Default behavior: init succeeds
ON_CALL(*mockFactoryPtr, init()).WillByDefault(Return(true));
// Default behavior: no devices found
ON_CALL(*mockFactoryPtr, findAllDevices()).WillByDefault(Invoke(returnEmptyDeviceList));
// Inject mock factory into service
service = std::make_unique<PrinterService>(std::move(mockFactory));
} }
void TearDown() override { service.reset(); } void TearDown() override { service.reset(); }
std::unique_ptr<PrinterService> service; std::unique_ptr<PrinterService> service;
libusbwrap::MockUsbDeviceFactory* mockFactoryPtr; // Non-owning pointer for expectations
}; };
// Test: PrinterService initialization // Test: PrinterService initialization
TEST_F(PrinterServiceTest, InitializeSuccess) { TEST_F(PrinterServiceTest, InitializeSuccess) {
// PrinterService::initialize() calls UsbDeviceFactory::init() EXPECT_CALL(*mockFactoryPtr, init()).WillOnce(Return(true));
// This will attempt to initialize libusb - we can't easily mock this
// without dependency injection, but we can test the call succeeds
// when libusb is available
EXPECT_TRUE(service->initialize()); EXPECT_TRUE(service->initialize());
} }
@@ -58,37 +74,35 @@ TEST_F(PrinterServiceTest, InitializeSuccess) {
TEST_F(PrinterServiceTest, DetectPrintersNoneFound) { TEST_F(PrinterServiceTest, DetectPrintersNoneFound) {
service->initialize(); service->initialize();
// Mock returns empty device list - no real USB enumeration happens
EXPECT_CALL(*mockFactoryPtr, findAllDevices()).WillOnce(Invoke(returnEmptyDeviceList));
auto printers = service->detectPrinters(); auto printers = service->detectPrinters();
// With no compatible USB devices, we should get an empty list // Should get empty list since mock returns no devices
// (This depends on actual USB devices present, so it might find real hardware) EXPECT_EQ(printers.size(), 0);
// In a real test environment without hardware, this should be empty
EXPECT_GE(printers.size(), 0); // Non-negative count
} }
// Test: Select printer with auto-detect // Test: Select printer with auto-detect when none found
TEST_F(PrinterServiceTest, SelectPrinterAuto) { TEST_F(PrinterServiceTest, SelectPrinterAutoNoneFound) {
service->initialize(); service->initialize();
service->detectPrinters();
// Mock returns empty device list
EXPECT_CALL(*mockFactoryPtr, findAllDevices()).WillOnce(Invoke(returnEmptyDeviceList));
auto printer = service->selectPrinter("auto"); auto printer = service->selectPrinter("auto");
// This will be nullptr if no printers detected // Should be nullptr since no printers detected
// In test environment without hardware, expect nullptr
// (Test passes either way - just exercises the code path)
if (printer != nullptr) {
EXPECT_NE(printer, nullptr);
EXPECT_EQ(service->getCurrentPrinter(), printer);
} else {
EXPECT_EQ(printer, nullptr); EXPECT_EQ(printer, nullptr);
} EXPECT_EQ(service->getCurrentPrinter(), nullptr);
} }
// Test: Select non-existent printer // Test: Select non-existent printer by name
TEST_F(PrinterServiceTest, SelectPrinterNotFound) { TEST_F(PrinterServiceTest, SelectPrinterByNameNotFound) {
service->initialize(); service->initialize();
service->detectPrinters();
// This test doesn't need USB mocking since selectPrinter("name")
// uses PrinterDriverFactory directly, not USB enumeration
auto printer = service->selectPrinter("NonExistentPrinter"); auto printer = service->selectPrinter("NonExistentPrinter");
EXPECT_EQ(printer, nullptr); EXPECT_EQ(printer, nullptr);