55 lines
1.8 KiB
C++
55 lines
1.8 KiB
C++
#include <gtest/gtest.h>
|
|
#include <pdfengine/display_list.hpp>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace pdfengine;
|
|
|
|
// A simple visitor for testing that just records the sequence of visited commands.
|
|
class MockVisitor : public CommandVisitor {
|
|
public:
|
|
std::vector<std::string> calls;
|
|
|
|
void visit(const SaveStateCommand&) override { calls.push_back("SaveState"); }
|
|
void visit(const RestoreStateCommand&) override { calls.push_back("RestoreState"); }
|
|
void visit(const SetTransformCommand& cmd) override { calls.push_back("SetTransform(" + std::to_string(cmd.matrix.a) + ")"); }
|
|
void visit(const FillRectCommand& cmd) override { calls.push_back("FillRect(" + std::to_string(cmd.width) + ")"); }
|
|
void visit(const DrawTextCommand& cmd) override { calls.push_back("DrawText(" + cmd.text + ")"); }
|
|
};
|
|
|
|
TEST(DisplayListTest, RecordAndReplay) {
|
|
DisplayList list;
|
|
|
|
EXPECT_EQ(list.size(), 0);
|
|
|
|
// Record some commands
|
|
list.saveState();
|
|
list.setTransform(Matrix(2.0f, 0.0f, 0.0f, 2.0f, 0.0f, 0.0f));
|
|
list.fillRect(10.0f, 10.0f, 100.0f, 50.0f);
|
|
list.drawText("Hello World", 20.0f, 30.0f);
|
|
list.restoreState();
|
|
|
|
EXPECT_EQ(list.size(), 5);
|
|
|
|
// Replay to the mock visitor
|
|
MockVisitor visitor;
|
|
list.replay(visitor);
|
|
|
|
ASSERT_EQ(visitor.calls.size(), 5);
|
|
EXPECT_EQ(visitor.calls[0], "SaveState");
|
|
EXPECT_EQ(visitor.calls[1], "SetTransform(2.000000)");
|
|
EXPECT_EQ(visitor.calls[2], "FillRect(100.000000)");
|
|
EXPECT_EQ(visitor.calls[3], "DrawText(Hello World)");
|
|
EXPECT_EQ(visitor.calls[4], "RestoreState");
|
|
}
|
|
|
|
TEST(DisplayListTest, Clear) {
|
|
DisplayList list;
|
|
list.saveState();
|
|
list.fillRect(0, 0, 10, 10);
|
|
EXPECT_EQ(list.size(), 2);
|
|
|
|
list.clear();
|
|
EXPECT_EQ(list.size(), 0);
|
|
}
|