|
|
|
@@ -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
|
|
|
|
EXPECT_EQ(printer, nullptr);
|
|
|
|
// (Test passes either way - just exercises the code path)
|
|
|
|
EXPECT_EQ(service->getCurrentPrinter(), nullptr);
|
|
|
|
if (printer != nullptr) {
|
|
|
|
|
|
|
|
EXPECT_NE(printer, nullptr);
|
|
|
|
|
|
|
|
EXPECT_EQ(service->getCurrentPrinter(), printer);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
EXPECT_EQ(printer, 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);
|
|
|
|
|