All checks were successful
Build ptprnt / build (push) Successful in 2m59s
Reviewed-on: #21
231 lines
6.5 KiB
C++
231 lines
6.5 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/LabelBuilder.hpp"
|
|
#include "graphics/interface/ILabel.hpp"
|
|
|
|
namespace ptprnt::graphics {
|
|
|
|
class LabelBuilderTest : public ::testing::Test {
|
|
protected:
|
|
void SetUp() override {
|
|
builder = std::make_unique<LabelBuilder>(128); // P700 printer height
|
|
}
|
|
|
|
void TearDown() override { builder.reset(); }
|
|
|
|
std::unique_ptr<LabelBuilder> builder;
|
|
};
|
|
|
|
// Test: Constructor
|
|
TEST_F(LabelBuilderTest, Constructor) {
|
|
EXPECT_NO_THROW(LabelBuilder(128));
|
|
EXPECT_NO_THROW(LabelBuilder(64));
|
|
}
|
|
|
|
// Test: Add single text
|
|
TEST_F(LabelBuilderTest, AddSingleText) {
|
|
auto& result = builder->addText("Hello");
|
|
|
|
// Should return reference to builder for chaining
|
|
EXPECT_EQ(&result, builder.get());
|
|
}
|
|
|
|
// Test: Add multiple texts
|
|
TEST_F(LabelBuilderTest, AddMultipleTexts) {
|
|
builder->addText("Line 1")
|
|
.addText("Line 2")
|
|
.addText("Line 3");
|
|
|
|
// Build should succeed
|
|
auto label = builder->build();
|
|
EXPECT_NE(label, nullptr);
|
|
}
|
|
|
|
// Test: Add empty text (should be ignored)
|
|
TEST_F(LabelBuilderTest, AddEmptyText) {
|
|
builder->addText("");
|
|
|
|
auto label = builder->build();
|
|
EXPECT_NE(label, nullptr);
|
|
}
|
|
|
|
// Test: Set font family
|
|
TEST_F(LabelBuilderTest, SetFontFamily) {
|
|
auto& result = builder->setFontFamily("monospace");
|
|
|
|
// Should return reference to builder for chaining
|
|
EXPECT_EQ(&result, builder.get());
|
|
}
|
|
|
|
// Test: Set font size
|
|
TEST_F(LabelBuilderTest, SetFontSize) {
|
|
auto& result = builder->setFontSize(24.0);
|
|
|
|
// Should return reference to builder for chaining
|
|
EXPECT_EQ(&result, builder.get());
|
|
}
|
|
|
|
// Test: Set horizontal alignment
|
|
TEST_F(LabelBuilderTest, SetHAlign) {
|
|
auto& result = builder->setHAlign(HAlignPosition::CENTER);
|
|
|
|
// Should return reference to builder for chaining
|
|
EXPECT_EQ(&result, builder.get());
|
|
}
|
|
|
|
// Test: Set vertical alignment
|
|
TEST_F(LabelBuilderTest, SetVAlign) {
|
|
auto& result = builder->setVAlign(VAlignPosition::TOP);
|
|
|
|
// Should return reference to builder for chaining
|
|
EXPECT_EQ(&result, builder.get());
|
|
}
|
|
|
|
// Test: Build with default settings
|
|
TEST_F(LabelBuilderTest, BuildWithDefaults) {
|
|
builder->addText("Test");
|
|
|
|
auto label = builder->build();
|
|
|
|
EXPECT_NE(label, nullptr);
|
|
EXPECT_GT(label->getWidth(), 0);
|
|
EXPECT_GT(label->getHeight(), 0);
|
|
}
|
|
|
|
// Test: Build with custom settings
|
|
TEST_F(LabelBuilderTest, BuildWithCustomSettings) {
|
|
builder->setFontFamily("serif")
|
|
.setFontSize(48.0)
|
|
.setHAlign(HAlignPosition::CENTER)
|
|
.setVAlign(VAlignPosition::MIDDLE)
|
|
.addText("Custom Text");
|
|
|
|
auto label = builder->build();
|
|
|
|
EXPECT_NE(label, nullptr);
|
|
}
|
|
|
|
// Test: Method chaining
|
|
TEST_F(LabelBuilderTest, MethodChaining) {
|
|
auto label = builder->setFontFamily("monospace")
|
|
.setFontSize(20.0)
|
|
.setHAlign(HAlignPosition::RIGHT)
|
|
.setVAlign(VAlignPosition::BOTTOM)
|
|
.addText("Chained")
|
|
.addText("Methods")
|
|
.build();
|
|
|
|
EXPECT_NE(label, nullptr);
|
|
}
|
|
|
|
// Test: Reset builder
|
|
TEST_F(LabelBuilderTest, Reset) {
|
|
builder->setFontFamily("monospace")
|
|
.setFontSize(48.0)
|
|
.setHAlign(HAlignPosition::RIGHT)
|
|
.setVAlign(VAlignPosition::BOTTOM)
|
|
.addText("Test");
|
|
|
|
auto& result = builder->reset();
|
|
|
|
// Should return reference to builder for chaining
|
|
EXPECT_EQ(&result, builder.get());
|
|
|
|
// After reset, should be able to build with defaults
|
|
builder->addText("After Reset");
|
|
auto label = builder->build();
|
|
EXPECT_NE(label, nullptr);
|
|
}
|
|
|
|
// Test: Build empty label
|
|
TEST_F(LabelBuilderTest, BuildEmptyLabel) {
|
|
// Build without adding any text
|
|
auto label = builder->build();
|
|
|
|
EXPECT_NE(label, nullptr);
|
|
}
|
|
|
|
// Test: Multiple builds from same builder
|
|
TEST_F(LabelBuilderTest, MultipleBuilds) {
|
|
builder->addText("First Build");
|
|
auto label1 = builder->build();
|
|
EXPECT_NE(label1, nullptr);
|
|
|
|
// Build again with same content
|
|
auto label2 = builder->build();
|
|
EXPECT_NE(label2, nullptr);
|
|
|
|
// Both labels should be valid
|
|
EXPECT_GT(label1->getWidth(), 0);
|
|
EXPECT_GT(label2->getWidth(), 0);
|
|
}
|
|
|
|
// Test: All horizontal alignments
|
|
TEST_F(LabelBuilderTest, AllHorizontalAlignments) {
|
|
builder->addText("Left").setHAlign(HAlignPosition::LEFT);
|
|
auto label1 = builder->build();
|
|
EXPECT_NE(label1, nullptr);
|
|
|
|
builder->reset().addText("Center").setHAlign(HAlignPosition::CENTER);
|
|
auto label2 = builder->build();
|
|
EXPECT_NE(label2, nullptr);
|
|
|
|
builder->reset().addText("Right").setHAlign(HAlignPosition::RIGHT);
|
|
auto label3 = builder->build();
|
|
EXPECT_NE(label3, nullptr);
|
|
|
|
builder->reset().addText("Justify").setHAlign(HAlignPosition::JUSTIFY);
|
|
auto label4 = builder->build();
|
|
EXPECT_NE(label4, nullptr);
|
|
}
|
|
|
|
// Test: All vertical alignments
|
|
TEST_F(LabelBuilderTest, AllVerticalAlignments) {
|
|
builder->addText("Top").setVAlign(VAlignPosition::TOP);
|
|
auto label1 = builder->build();
|
|
EXPECT_NE(label1, nullptr);
|
|
|
|
builder->reset().addText("Middle").setVAlign(VAlignPosition::MIDDLE);
|
|
auto label2 = builder->build();
|
|
EXPECT_NE(label2, nullptr);
|
|
|
|
builder->reset().addText("Bottom").setVAlign(VAlignPosition::BOTTOM);
|
|
auto label3 = builder->build();
|
|
EXPECT_NE(label3, nullptr);
|
|
}
|
|
|
|
// Test: Different font sizes
|
|
TEST_F(LabelBuilderTest, DifferentFontSizes) {
|
|
builder->setFontSize(12.0).addText("Small");
|
|
auto label1 = builder->build();
|
|
EXPECT_NE(label1, nullptr);
|
|
|
|
builder->reset().setFontSize(64.0).addText("Large");
|
|
auto label2 = builder->build();
|
|
EXPECT_NE(label2, nullptr);
|
|
|
|
// Larger font should produce wider label (assuming same text length)
|
|
// Note: This is a heuristic test and may not always be true depending on font rendering
|
|
}
|
|
|
|
} // namespace ptprnt::graphics
|