From 8fcd514168059919d47637e2899835f5761f9b11 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Thu, 14 Jan 2021 10:25:37 +0100
Subject: [PATCH] dispatch: Add simple test
---
src/components/renderer.cpp | 8 +++++
tests/CMakeLists.txt | 1 +
tests/unit_tests/tags/dispatch.cpp | 47 ++++++++++++++++++++++++++++++
3 files changed, 56 insertions(+)
create mode 100644 tests/unit_tests/tags/dispatch.cpp
diff --git a/src/components/renderer.cpp b/src/components/renderer.cpp
index 3872c283..2ce64eb3 100644
--- a/src/components/renderer.cpp
+++ b/src/components/renderer.cpp
@@ -251,6 +251,14 @@ void renderer::begin(xcb_rectangle_t rect) {
void renderer::end() {
m_log.trace_x("renderer: end");
+ /*
+ * Finalize the positions of the action blocks.
+ * Up until this point, the positions were relative to the start of the
+ * alignment block the action was located in.
+ * Here we add the start position of the block as well as the start position
+ * of the bar itself (without borders or tray) to create positions relative to
+ * the bar window.
+ */
for (auto& a : m_action_ctxt.get_blocks()) {
a.start_x += block_x(a.align) + m_rect.x;
a.end_x += block_x(a.align) + m_rect.x;
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 4cf86439..d0271077 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -63,6 +63,7 @@ add_unit_test(drawtypes/label)
add_unit_test(drawtypes/ramp)
add_unit_test(drawtypes/iconset)
add_unit_test(tags/parser)
+add_unit_test(tags/dispatch)
# Run make check to build and run all unit tests
add_custom_target(check
diff --git a/tests/unit_tests/tags/dispatch.cpp b/tests/unit_tests/tags/dispatch.cpp
new file mode 100644
index 00000000..895bba57
--- /dev/null
+++ b/tests/unit_tests/tags/dispatch.cpp
@@ -0,0 +1,47 @@
+#include "tags/dispatch.hpp"
+
+#include "common/test.hpp"
+#include "components/logger.hpp"
+#include "gmock/gmock.h"
+
+using namespace polybar;
+using namespace std;
+using namespace tags;
+
+using ::testing::InSequence;
+using ::testing::_;
+
+class MockRenderer : public renderer_interface {
+ public:
+ MockRenderer(action_context& action_ctxt) : renderer_interface(action_ctxt){};
+
+ MOCK_METHOD(void, render_offset, (const context& ctxt, int pixels), (override));
+ MOCK_METHOD(void, render_text, (const context& ctxt, const string&& str), (override));
+ MOCK_METHOD(void, change_alignment, (const context& ctxt), (override));
+ MOCK_METHOD(void, action_open, (const context& ctxt, mousebtn btn, action_t id), (override));
+ MOCK_METHOD(void, action_close, (const context& ctxt, action_t id), (override));
+};
+
+class TestableDispatch : public dispatch {};
+
+class Dispatch : public ::testing::Test {
+ protected:
+ unique_ptr action_ctxt = make_unique();
+
+ unique_ptr parser = make_unique(logger(loglevel::NONE), *action_ctxt);
+};
+
+TEST_F(Dispatch, ignoreFormatting) {
+ MockRenderer r(*action_ctxt);
+
+ {
+ InSequence seq;
+ EXPECT_CALL(r, render_offset(_, 10)).Times(1);
+ EXPECT_CALL(r, render_text(_, string{"abc"})).Times(1);
+ EXPECT_CALL(r, render_text(_, string{"foo"})).Times(1);
+ }
+
+ bar_settings settings;
+
+ parser->parse(settings, r, "%{O10}abc%{F-}foo");
+}