117 lines
3.6 KiB
C++
117 lines
3.6 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 "../../tests/mocks/MockPrinterDriver.hpp"
|
|
#include "../../tests/mocks/MockUsbDevice.hpp"
|
|
#include "core/PrinterDriverFactory.hpp"
|
|
#include "core/PrinterService.hpp"
|
|
|
|
using ::testing::_;
|
|
using ::testing::NiceMock;
|
|
using ::testing::Return;
|
|
|
|
namespace ptprnt::core {
|
|
|
|
// Test fixture for PrinterService tests
|
|
class PrinterServiceTest : public ::testing::Test {
|
|
protected:
|
|
void SetUp() override {
|
|
// Service under test
|
|
service = std::make_unique<PrinterService>();
|
|
}
|
|
|
|
void TearDown() override { service.reset(); }
|
|
|
|
std::unique_ptr<PrinterService> service;
|
|
};
|
|
|
|
// 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_TRUE(service->initialize());
|
|
}
|
|
|
|
// Test: Detect printers when none are connected
|
|
TEST_F(PrinterServiceTest, DetectPrintersNoneFound) {
|
|
service->initialize();
|
|
|
|
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
|
|
}
|
|
|
|
// Test: Select printer with auto-detect
|
|
TEST_F(PrinterServiceTest, SelectPrinterAuto) {
|
|
service->initialize();
|
|
service->detectPrinters();
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
// Test: Select non-existent printer
|
|
TEST_F(PrinterServiceTest, SelectPrinterNotFound) {
|
|
service->initialize();
|
|
service->detectPrinters();
|
|
|
|
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
|