All checks were successful
Build ptprnt / build (push) Successful in 2m28s
248 lines
7.0 KiB
C++
248 lines
7.0 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 <gtest/gtest.h>
|
|
|
|
#include "graphics/Bitmap.hpp"
|
|
#include "graphics/Label.hpp"
|
|
#include "graphics/Monochrome.hpp"
|
|
#include "printers/FakePrinter.hpp"
|
|
|
|
namespace ptprnt::printer {
|
|
|
|
class FakePrinterTest : public ::testing::Test {
|
|
protected:
|
|
void SetUp() override {
|
|
printer = std::make_unique<FakePrinter>();
|
|
}
|
|
|
|
void TearDown() override { printer.reset(); }
|
|
|
|
std::unique_ptr<FakePrinter> printer;
|
|
};
|
|
|
|
// Test: Get driver name
|
|
TEST_F(FakePrinterTest, GetDriverName) {
|
|
auto name = printer->getDriverName();
|
|
EXPECT_FALSE(name.empty());
|
|
EXPECT_EQ(name, "FakePrinter");
|
|
}
|
|
|
|
// Test: Get printer name
|
|
TEST_F(FakePrinterTest, GetName) {
|
|
auto name = printer->getName();
|
|
EXPECT_FALSE(name.empty());
|
|
}
|
|
|
|
// Test: Get USB ID
|
|
TEST_F(FakePrinterTest, GetUsbId) {
|
|
auto usbId = printer->getUsbId();
|
|
EXPECT_EQ(usbId.first, 0x0000); // Virtual printer - no USB ID
|
|
EXPECT_EQ(usbId.second, 0x0000); // Virtual printer - no USB ID
|
|
}
|
|
|
|
// Test: Get printer version
|
|
TEST_F(FakePrinterTest, GetVersion) {
|
|
auto version = printer->getVersion();
|
|
EXPECT_FALSE(version.empty());
|
|
}
|
|
|
|
// Test: Get printer info
|
|
TEST_F(FakePrinterTest, GetPrinterInfo) {
|
|
auto info = printer->getPrinterInfo();
|
|
|
|
EXPECT_FALSE(info.driverName.empty());
|
|
EXPECT_FALSE(info.name.empty());
|
|
EXPECT_GT(info.pixelLines, 0);
|
|
}
|
|
|
|
// Test: Get printer status without device
|
|
TEST_F(FakePrinterTest, GetPrinterStatusWithoutDevice) {
|
|
auto status = printer->getPrinterStatus();
|
|
|
|
// FakePrinter should return empty status when no device attached
|
|
EXPECT_EQ(status.tapeWidthMm, 0);
|
|
}
|
|
|
|
// Test: Get printer status with device
|
|
TEST_F(FakePrinterTest, GetPrinterStatusWithDevice) {
|
|
printer->attachUsbDevice(nullptr);
|
|
auto status = printer->getPrinterStatus();
|
|
|
|
// FakePrinter should return default status when device attached
|
|
EXPECT_EQ(status.tapeWidthMm, 12); // Default tape width
|
|
}
|
|
|
|
// Test: Attach USB device (should always succeed for fake printer)
|
|
TEST_F(FakePrinterTest, AttachUsbDevice) {
|
|
bool result = printer->attachUsbDevice(nullptr);
|
|
|
|
// FakePrinter doesn't need a real device
|
|
EXPECT_TRUE(result);
|
|
}
|
|
|
|
// Test: Detach USB device
|
|
TEST_F(FakePrinterTest, DetachUsbDevice) {
|
|
printer->attachUsbDevice(nullptr);
|
|
bool result = printer->detachUsbDevice();
|
|
|
|
EXPECT_TRUE(result);
|
|
}
|
|
|
|
// Test: Print without attaching device first
|
|
TEST_F(FakePrinterTest, PrintWithoutDevice) {
|
|
bool result = printer->print();
|
|
|
|
// FakePrinter should fail if no device attached
|
|
EXPECT_FALSE(result);
|
|
}
|
|
|
|
// Test: Print after attaching device
|
|
TEST_F(FakePrinterTest, PrintWithDevice) {
|
|
printer->attachUsbDevice(nullptr);
|
|
bool result = printer->print();
|
|
|
|
EXPECT_TRUE(result);
|
|
}
|
|
|
|
// Test: Print bitmap
|
|
TEST_F(FakePrinterTest, PrintBitmap) {
|
|
graphics::Bitmap<graphics::ALPHA8> bitmap(100, 128);
|
|
|
|
// Fill with some pattern
|
|
std::vector<graphics::ALPHA8> pixels(bitmap.getWidth() * bitmap.getHeight());
|
|
for (size_t i = 0; i < pixels.size(); ++i) {
|
|
pixels[i] = (i % 2) ? 0xFF : 0x00;
|
|
}
|
|
bitmap.setPixels(pixels);
|
|
|
|
printer->attachUsbDevice(nullptr);
|
|
bool result = printer->printBitmap(bitmap);
|
|
|
|
EXPECT_TRUE(result);
|
|
|
|
// Check that last print was saved
|
|
const auto& lastPrint = printer->getLastPrint();
|
|
EXPECT_GT(lastPrint.getWidth(), 0);
|
|
EXPECT_GT(lastPrint.getHeight(), 0);
|
|
}
|
|
|
|
// Test: Print monochrome data
|
|
TEST_F(FakePrinterTest, PrintMonochromeData) {
|
|
graphics::Bitmap<graphics::ALPHA8> bitmap(50, 128);
|
|
auto pixels = bitmap.getPixelsCpy();
|
|
auto mono = graphics::Monochrome(pixels, bitmap.getWidth(), bitmap.getHeight());
|
|
auto data = mono.get();
|
|
|
|
printer->attachUsbDevice(nullptr);
|
|
bool result = printer->printMonochromeData(data);
|
|
|
|
EXPECT_TRUE(result);
|
|
|
|
// Verify last print
|
|
const auto& lastPrint = printer->getLastPrint();
|
|
EXPECT_GT(lastPrint.getWidth(), 0);
|
|
}
|
|
|
|
// Test: Print label
|
|
TEST_F(FakePrinterTest, PrintLabel) {
|
|
auto label = std::make_unique<graphics::Label>(128);
|
|
label->create("Test Label");
|
|
|
|
printer->attachUsbDevice(nullptr);
|
|
bool result = printer->printLabel(std::move(label));
|
|
|
|
EXPECT_TRUE(result);
|
|
}
|
|
|
|
// Test: Print null label
|
|
TEST_F(FakePrinterTest, PrintNullLabel) {
|
|
printer->attachUsbDevice(nullptr);
|
|
bool result = printer->printLabel(nullptr);
|
|
|
|
EXPECT_FALSE(result);
|
|
}
|
|
|
|
// Test: Print empty bitmap
|
|
TEST_F(FakePrinterTest, PrintEmptyBitmap) {
|
|
graphics::Bitmap<graphics::ALPHA8> bitmap(0, 0);
|
|
|
|
printer->attachUsbDevice(nullptr);
|
|
bool result = printer->printBitmap(bitmap);
|
|
|
|
// Should handle empty bitmap gracefully
|
|
EXPECT_TRUE(result);
|
|
}
|
|
|
|
// Test: Get last print before printing
|
|
TEST_F(FakePrinterTest, GetLastPrintBeforePrinting) {
|
|
// Should throw when getting last print before any print operation
|
|
EXPECT_THROW({
|
|
const auto& lastPrint = printer->getLastPrint();
|
|
(void)lastPrint; // Suppress unused variable warning
|
|
}, std::runtime_error);
|
|
}
|
|
|
|
// Test: Save last print to PNG (may fail without valid data)
|
|
TEST_F(FakePrinterTest, SaveLastPrintToPng) {
|
|
graphics::Bitmap<graphics::ALPHA8> bitmap(100, 128);
|
|
|
|
printer->attachUsbDevice(nullptr);
|
|
printer->printBitmap(bitmap);
|
|
|
|
// Try to save to /tmp
|
|
bool result = printer->saveLastPrintToPng("/tmp/test_fake_printer_output.png");
|
|
|
|
// Should succeed if we have valid print data
|
|
EXPECT_TRUE(result);
|
|
}
|
|
|
|
// Test: Multiple prints
|
|
TEST_F(FakePrinterTest, MultiplePrints) {
|
|
graphics::Bitmap<graphics::ALPHA8> bitmap1(50, 128);
|
|
graphics::Bitmap<graphics::ALPHA8> bitmap2(100, 128);
|
|
|
|
printer->attachUsbDevice(nullptr);
|
|
|
|
bool result1 = printer->printBitmap(bitmap1);
|
|
EXPECT_TRUE(result1);
|
|
|
|
bool result2 = printer->printBitmap(bitmap2);
|
|
EXPECT_TRUE(result2);
|
|
|
|
// Last print should be from second bitmap
|
|
const auto& lastPrint = printer->getLastPrint();
|
|
EXPECT_GT(lastPrint.getWidth(), 0);
|
|
}
|
|
|
|
// Test: Detach and reattach
|
|
TEST_F(FakePrinterTest, DetachAndReattach) {
|
|
printer->attachUsbDevice(nullptr);
|
|
EXPECT_TRUE(printer->detachUsbDevice());
|
|
|
|
// Should be able to reattach
|
|
EXPECT_TRUE(printer->attachUsbDevice(nullptr));
|
|
|
|
// Should be able to print after reattach
|
|
graphics::Bitmap<graphics::ALPHA8> bitmap(50, 128);
|
|
EXPECT_TRUE(printer->printBitmap(bitmap));
|
|
}
|
|
|
|
} // namespace ptprnt::printer
|