All checks were successful
Build ptprnt / build (push) Successful in 3m5s
129 lines
4.1 KiB
C++
129 lines
4.1 KiB
C++
/*
|
|
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/>.
|
|
|
|
*/
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <memory>
|
|
#include <vector>
|
|
|
|
#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 {
|
|
// 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) {
|
|
EXPECT_CALL(*mockFactoryPtr, init()).WillOnce(Return(true));
|
|
|
|
EXPECT_TRUE(service->initialize());
|
|
}
|
|
|
|
// Test: Detect printers when none are connected
|
|
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();
|
|
|
|
// Should get empty list since mock returns no devices
|
|
EXPECT_EQ(printers.size(), 0);
|
|
}
|
|
|
|
// Test: Select printer with auto-detect when none found
|
|
TEST_F(PrinterServiceTest, SelectPrinterAutoNoneFound) {
|
|
service->initialize();
|
|
|
|
// Mock returns empty device list
|
|
EXPECT_CALL(*mockFactoryPtr, findAllDevices()).WillOnce(Invoke(returnEmptyDeviceList));
|
|
|
|
auto printer = service->selectPrinter("auto");
|
|
|
|
// Should be nullptr since no printers detected
|
|
EXPECT_EQ(printer, nullptr);
|
|
EXPECT_EQ(service->getCurrentPrinter(), nullptr);
|
|
}
|
|
|
|
// Test: Select non-existent printer by name
|
|
TEST_F(PrinterServiceTest, SelectPrinterByNameNotFound) {
|
|
service->initialize();
|
|
|
|
// 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);
|
|
EXPECT_EQ(service->getCurrentPrinter(), nullptr);
|
|
}
|
|
|
|
// Test: Get current printer when none selected
|
|
TEST_F(PrinterServiceTest, GetCurrentPrinterNoneSelected) {
|
|
EXPECT_EQ(service->getCurrentPrinter(), nullptr);
|
|
}
|
|
|
|
// Test: Print label without selecting printer
|
|
TEST_F(PrinterServiceTest, PrintLabelNoPrinterSelected) {
|
|
// Create a simple label (we need a valid ILabel pointer)
|
|
std::unique_ptr<graphics::ILabel> label = nullptr; // nullptr label for this test
|
|
|
|
bool result = service->printLabel(std::move(label));
|
|
|
|
// Should fail because no printer is selected
|
|
EXPECT_FALSE(result);
|
|
}
|
|
|
|
} // namespace ptprnt::core
|