test(automation): MockUiBackend recording test double

This commit is contained in:
SoftFever
2026-06-03 01:45:46 +08:00
parent e449a0b618
commit 94c356845e
3 changed files with 73 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ add_executable(${_TEST_NAME}_tests
automation_tests.cpp
test_serializer.cpp
test_locator.cpp
MockUiBackend.cpp
${CMAKE_SOURCE_DIR}/src/slic3r/GUI/Automation/WidgetSerializer.cpp
${CMAKE_SOURCE_DIR}/src/slic3r/GUI/Automation/Locator.cpp
)

View File

@@ -0,0 +1,13 @@
#include "MockUiBackend.hpp"
#include <catch2/catch_all.hpp>
using namespace Slic3r::GUI::Automation;
TEST_CASE("MockUiBackend records calls", "[automation][mock]") {
MockUiBackend mock;
UiNode n; n.id = "btn_slice";
mock.click(n, MouseButton::Left, false, {});
REQUIRE(mock.clicked_ids.size() == 1);
CHECK(mock.clicked_ids[0] == "btn_slice");
CHECK(mock.click_buttons[0] == MouseButton::Left);
}

View File

@@ -0,0 +1,59 @@
#pragma once
#include "slic3r/GUI/Automation/IUiBackend.hpp"
#include <functional>
#include <vector>
namespace Slic3r { namespace GUI { namespace Automation {
// Deterministic fake backend for dispatcher tests. Records every primitive call
// and returns canned data. `tree_provider` lets a test return different trees on
// successive dump_tree() calls (used for sync.wait_for tests).
class MockUiBackend : public IUiBackend {
public:
// Recorded calls (inspected by tests).
int refresh_count = 0;
int dump_count = 0;
std::vector<std::string> clicked_ids; // node.id of each click()
std::vector<MouseButton> click_buttons;
std::vector<std::string> typed_text;
std::vector<std::vector<KeyChord>> sent_keys;
int screenshot_window_count = 0;
int screenshot_viewport_count = 0;
// Canned outputs (set by tests).
UiNode tree; // default tree for dump_tree
AppState state;
PngImage canned_png{ {0x89,0x50,0x4E,0x47}, 4, 4 }; // fake "PNG" bytes
bool click_result = true;
// Optional: per-call tree provider (overrides `tree` when set).
std::function<UiNode(int /*call_index*/)> tree_provider;
void refresh_ui() override { ++refresh_count; }
UiNode dump_tree(const DumpOptions&) override {
const int idx = dump_count++;
return tree_provider ? tree_provider(idx) : tree;
}
AppState app_state() override { return state; }
bool click(const UiNode& node, MouseButton button, bool /*dbl*/,
const std::vector<KeyModifier>&) override {
clicked_ids.push_back(node.id);
click_buttons.push_back(button);
return click_result;
}
bool type_text(const std::string& text) override {
typed_text.push_back(text); return true;
}
bool send_keys(const std::vector<KeyChord>& chords) override {
sent_keys.push_back(chords); return true;
}
PngImage screenshot_window(const UiNode*) override {
++screenshot_window_count; return canned_png;
}
PngImage screenshot_viewport3d(std::optional<int>, std::optional<int>,
std::optional<int>) override {
++screenshot_viewport_count; return canned_png;
}
};
}}} // namespace