Fix real Usb Device references in Printer
All checks were successful
Build ptprnt / build (push) Successful in 3m5s
All checks were successful
Build ptprnt / build (push) Successful in 3m5s
This commit is contained in:
@@ -138,6 +138,10 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& 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<uint8_t> initCmd(128, 0x00);
|
||||
|
||||
@@ -33,6 +33,7 @@ namespace libusbwrap {
|
||||
*/
|
||||
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));
|
||||
};
|
||||
|
||||
@@ -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<std::unique_ptr<libusbwrap::IUsbDevice>> returnEmptyDeviceList() {
|
||||
return {};
|
||||
}
|
||||
|
||||
// Test fixture for PrinterService tests
|
||||
class PrinterServiceTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
// Service under test
|
||||
service = std::make_unique<PrinterService>();
|
||||
// Create mock USB device factory
|
||||
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(); }
|
||||
|
||||
std::unique_ptr<PrinterService> 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);
|
||||
|
||||
Reference in New Issue
Block a user