Files
ptprnt/tests/p700_printer_test/p700_printer_test.cpp
Moritz Martinius 97edea85af
All checks were successful
Build ptprnt / build (push) Successful in 2m59s
Create more unit tests for USB Devices and Printer Driver (#21)
Reviewed-on: #21
2025-10-21 18:55:32 +00:00

170 lines
4.8 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 "graphics/Bitmap.hpp"
#include "graphics/Monochrome.hpp"
#include "mocks/MockUsbDevice.hpp"
#include "printers/P700Printer.hpp"
using ::testing::_;
using ::testing::DoAll;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::SetArgPointee;
namespace ptprnt::printer {
// Test fixture for P700Printer tests
class P700PrinterTest : public ::testing::Test {
protected:
void SetUp() override {
printer = std::make_unique<P700Printer>();
mockUsbDev = std::make_shared<NiceMock<libusbwrap::MockUsbDevice>>();
// Default mock behaviors
ON_CALL(*mockUsbDev, open()).WillByDefault(Return(true));
ON_CALL(*mockUsbDev, close()).WillByDefault(Return());
ON_CALL(*mockUsbDev, detachKernelDriver(_)).WillByDefault(Return(true));
ON_CALL(*mockUsbDev, claimInterface(_)).WillByDefault(Return(true));
ON_CALL(*mockUsbDev, releaseInterface(_)).WillByDefault(Return(true));
ON_CALL(*mockUsbDev, bulkTransfer(_, _, _, _)).WillByDefault(Return(true));
ON_CALL(*mockUsbDev, getUsbId()).WillByDefault(Return(libusbwrap::usbId{0x04f9, 0x2061}));
}
void TearDown() override { printer.reset(); }
std::unique_ptr<P700Printer> printer;
std::shared_ptr<libusbwrap::MockUsbDevice> mockUsbDev;
};
// Test: Get printer driver name
TEST_F(P700PrinterTest, GetDriverName) {
EXPECT_EQ(printer->getDriverName(), "P700");
}
// Test: Get printer name
TEST_F(P700PrinterTest, GetName) {
auto name = printer->getName();
EXPECT_FALSE(name.empty());
}
// Test: Get USB ID
TEST_F(P700PrinterTest, GetUsbId) {
auto usbId = printer->getUsbId();
EXPECT_EQ(usbId.first, 0x04f9); // Brother VID
EXPECT_EQ(usbId.second, 0x2061); // P700 PID
}
// Test: Get printer version
TEST_F(P700PrinterTest, GetVersion) {
auto version = printer->getVersion();
EXPECT_FALSE(version.empty());
}
// Test: Get printer info
TEST_F(P700PrinterTest, GetPrinterInfo) {
auto info = printer->getPrinterInfo();
EXPECT_EQ(info.pixelLines, 128);
EXPECT_FALSE(info.name.empty());
}
// Test: Attach USB device
TEST_F(P700PrinterTest, AttachUsbDevice) {
bool result = printer->attachUsbDevice(mockUsbDev);
EXPECT_TRUE(result);
}
// Test: Attach USB device with null pointer
TEST_F(P700PrinterTest, AttachNullUsbDevice) {
bool result = printer->attachUsbDevice(nullptr);
EXPECT_FALSE(result);
}
// Test: Detach USB device
TEST_F(P700PrinterTest, DetachUsbDevice) {
printer->attachUsbDevice(mockUsbDev);
bool result = printer->detachUsbDevice();
EXPECT_TRUE(result);
}
// Test: Detach when no device attached
TEST_F(P700PrinterTest, DetachNoDevice) {
bool result = printer->detachUsbDevice();
// Production code returns true when no device is attached (just logs warning)
EXPECT_TRUE(result);
}
// Test: Get printer status without device
TEST_F(P700PrinterTest, GetStatusNoDevice) {
auto status = printer->getPrinterStatus();
EXPECT_EQ(status.tapeWidthPixel, 0); // Should be 0 when no device
}
// Test: Print without attached device
TEST_F(P700PrinterTest, PrintWithoutDevice) {
bool result = printer->print();
// Should fail when no device is attached
EXPECT_FALSE(result);
}
// Test: Print bitmap without device
TEST_F(P700PrinterTest, PrintBitmapWithoutDevice) {
graphics::Bitmap<graphics::ALPHA8> bitmap(10, 10);
bool result = printer->printBitmap(bitmap);
EXPECT_FALSE(result);
}
// Test: Print monochrome data without device
TEST_F(P700PrinterTest, PrintMonochromeDataWithoutDevice) {
graphics::MonochromeData data;
data.width = 10;
data.height = 10;
data.bytes = std::vector<uint8_t>(20, 0xFF);
data.stride = 2;
bool result = printer->printMonochromeData(data);
EXPECT_FALSE(result);
}
// Test: Print label with null pointer
TEST_F(P700PrinterTest, PrintNullLabel) {
std::unique_ptr<graphics::ILabel> label = nullptr;
bool result = printer->printLabel(std::move(label));
EXPECT_FALSE(result);
}
} // namespace ptprnt::printer