From 1f57a802f35017e98788113df6e8d611813c5cdb Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Tue, 21 Oct 2025 20:51:17 +0200 Subject: [PATCH] Fix real Usb Device references in Printer --- src/printers/P700Printer.cpp | 4 ++ tests/mocks/MockUsbDeviceFactory.hpp | 1 + .../printer_service_test.cpp | 64 +++++++++++-------- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/printers/P700Printer.cpp b/src/printers/P700Printer.cpp index 006cebd..cb17f54 100644 --- a/src/printers/P700Printer.cpp +++ b/src/printers/P700Printer.cpp @@ -138,6 +138,10 @@ bool P700Printer::printBitmap(const graphics::Bitmap& bitmap) } bool P700Printer::printMonochromeData(const graphics::MonochromeData& data) { + if (!mUsbHndl) { + spdlog::error("USB Handle is invalid!"); + return false; + } // Send initialization sequence // The INITIALIZE command needs to be sent as a 128-byte packet with ESC @ at the end std::vector initCmd(128, 0x00); diff --git a/tests/mocks/MockUsbDeviceFactory.hpp b/tests/mocks/MockUsbDeviceFactory.hpp index 4089dbb..3022379 100644 --- a/tests/mocks/MockUsbDeviceFactory.hpp +++ b/tests/mocks/MockUsbDeviceFactory.hpp @@ -33,6 +33,7 @@ namespace libusbwrap { */ class MockUsbDeviceFactory : public IUsbDeviceFactory { public: + MOCK_METHOD(bool, init, (), (override)); MOCK_METHOD(std::vector>, findAllDevices, (), (override)); MOCK_METHOD(std::vector>, findDevices, (uint16_t vid, uint16_t pid), (override)); }; diff --git a/tests/printer_service_test/printer_service_test.cpp b/tests/printer_service_test/printer_service_test.cpp index 922adb6..2d29c55 100644 --- a/tests/printer_service_test/printer_service_test.cpp +++ b/tests/printer_service_test/printer_service_test.cpp @@ -25,32 +25,48 @@ #include "core/PrinterDriverFactory.hpp" #include "core/PrinterService.hpp" +#include "mocks/MockUsbDeviceFactory.hpp" using ::testing::_; +using ::testing::Invoke; using ::testing::NiceMock; using ::testing::Return; namespace ptprnt::core { +// Helper function to return empty vector of unique_ptrs +std::vector> returnEmptyDeviceList() { + return {}; +} + // Test fixture for PrinterService tests class PrinterServiceTest : public ::testing::Test { protected: void SetUp() override { - // Service under test - service = std::make_unique(); + // Create mock USB device factory + auto mockFactory = std::make_unique>(); + 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(std::move(mockFactory)); } void TearDown() override { service.reset(); } std::unique_ptr service; + libusbwrap::MockUsbDeviceFactory* mockFactoryPtr; // Non-owning pointer for expectations }; // Test: PrinterService initialization TEST_F(PrinterServiceTest, InitializeSuccess) { - // PrinterService::initialize() calls UsbDeviceFactory::init() - // 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_CALL(*mockFactoryPtr, init()).WillOnce(Return(true)); + EXPECT_TRUE(service->initialize()); } @@ -58,37 +74,35 @@ TEST_F(PrinterServiceTest, InitializeSuccess) { TEST_F(PrinterServiceTest, DetectPrintersNoneFound) { service->initialize(); + // Mock returns empty device list - no real USB enumeration happens + EXPECT_CALL(*mockFactoryPtr, findAllDevices()).WillOnce(Invoke(returnEmptyDeviceList)); + auto printers = service->detectPrinters(); - // With no compatible USB devices, we should get an empty list - // (This depends on actual USB devices present, so it might find real hardware) - // In a real test environment without hardware, this should be empty - EXPECT_GE(printers.size(), 0); // Non-negative count + // Should get empty list since mock returns no devices + EXPECT_EQ(printers.size(), 0); } -// Test: Select printer with auto-detect -TEST_F(PrinterServiceTest, SelectPrinterAuto) { +// Test: Select printer with auto-detect when none found +TEST_F(PrinterServiceTest, SelectPrinterAutoNoneFound) { service->initialize(); - service->detectPrinters(); + + // Mock returns empty device list + EXPECT_CALL(*mockFactoryPtr, findAllDevices()).WillOnce(Invoke(returnEmptyDeviceList)); auto printer = service->selectPrinter("auto"); - // This will be nullptr if 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); - } + // Should be nullptr since no printers detected + EXPECT_EQ(printer, nullptr); + EXPECT_EQ(service->getCurrentPrinter(), nullptr); } -// Test: Select non-existent printer -TEST_F(PrinterServiceTest, SelectPrinterNotFound) { +// Test: Select non-existent printer by name +TEST_F(PrinterServiceTest, SelectPrinterByNameNotFound) { 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"); EXPECT_EQ(printer, nullptr);