mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-10-21 20:08:16 +08:00

Some checks failed
Build / Bazel, cl, windows-latest (push) Has been cancelled
Build / Bazel, clang++, macos-latest (push) Has been cancelled
Build / Bazel, clang++, ubuntu-latest (push) Has been cancelled
Build / Bazel, g++, macos-latest (push) Has been cancelled
Build / Bazel, g++, ubuntu-latest (push) Has been cancelled
Build / CMake, cl, windows-latest (push) Has been cancelled
Build / CMake, gcc, ubuntu-latest (push) Has been cancelled
Build / CMake, llvm, ubuntu-latest (push) Has been cancelled
Build / CMake, llvm, macos-latest (push) Has been cancelled
Build / Test modules (llvm, ubuntu-latest) (push) Has been cancelled
Documentation / documentation (push) Has been cancelled
Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com> Bug:https://github.com/ArthurSonzogni/FTXUI/issues/1131 Bug:https://github.com/ArthurSonzogni/FTXUI/discussions/1130
338 lines
11 KiB
C++
338 lines
11 KiB
C++
// Copyright 2022 Arthur Sonzogni. All rights reserved.
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
// the LICENSE file.
|
|
#include <ftxui/dom/direction.hpp> // for Direction, Direction::Down, Direction::Left, Direction::Right, Direction::Up
|
|
#include <string> // for string
|
|
|
|
#include "ftxui/component/component.hpp" // for ResizableSplit, Renderer, ResizableSplitBottom, ResizableSplitLeft, ResizableSplitRight, ResizableSplitTop
|
|
#include "ftxui/component/component_base.hpp" // for ComponentBase, Component
|
|
#include "ftxui/component/event.hpp" // for Event
|
|
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed, Mouse::Released
|
|
#include "ftxui/dom/elements.hpp" // for Element, separatorDouble, text
|
|
#include "ftxui/dom/node.hpp" // for Render
|
|
#include "ftxui/screen/screen.hpp" // for Screen
|
|
#include "gtest/gtest.h" // for AssertionResult, Message, TestPartResult, Test, EXPECT_EQ, EXPECT_TRUE, TEST
|
|
|
|
// NOLINTBEGIN
|
|
namespace ftxui {
|
|
|
|
namespace {
|
|
Component BasicComponent() {
|
|
return Renderer([](bool focused) { return text(""); });
|
|
}
|
|
|
|
Event MousePressed(int x, int y) {
|
|
Mouse mouse;
|
|
mouse.button = Mouse::Left;
|
|
mouse.motion = Mouse::Pressed;
|
|
mouse.shift = false;
|
|
mouse.meta = false;
|
|
mouse.control = false;
|
|
mouse.x = x;
|
|
mouse.y = y;
|
|
return Event::Mouse("jjj", mouse);
|
|
}
|
|
|
|
Event MouseReleased(int x, int y) {
|
|
Mouse mouse;
|
|
mouse.button = Mouse::Left;
|
|
mouse.motion = Mouse::Released;
|
|
mouse.shift = false;
|
|
mouse.meta = false;
|
|
mouse.control = false;
|
|
mouse.x = x;
|
|
mouse.y = y;
|
|
return Event::Mouse("jjj", mouse);
|
|
}
|
|
} // namespace
|
|
|
|
TEST(ResizableSplit, BasicLeft) {
|
|
int position = 3;
|
|
auto component =
|
|
ResizableSplitLeft(BasicComponent(), BasicComponent(), &position);
|
|
auto screen = Screen(20, 20);
|
|
Render(screen, component->Render());
|
|
EXPECT_EQ(position, 3);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(3, 1)));
|
|
EXPECT_EQ(position, 3);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(10, 1)));
|
|
EXPECT_EQ(position, 10);
|
|
EXPECT_TRUE(component->OnEvent(MouseReleased(10, 1)));
|
|
EXPECT_EQ(position, 10);
|
|
}
|
|
|
|
TEST(ResizableSplit, BasicLeftWithCustomSeparator) {
|
|
int position = 1;
|
|
auto component = ResizableSplit({
|
|
.main = BasicComponent(),
|
|
.back = BasicComponent(),
|
|
.direction = Direction::Left,
|
|
.main_size = &position,
|
|
.separator_func = [] { return separatorDouble(); },
|
|
});
|
|
auto screen = Screen(4, 4);
|
|
Render(screen, component->Render());
|
|
EXPECT_EQ(position, 1);
|
|
EXPECT_EQ(screen.ToString(),
|
|
" ║ \r\n"
|
|
" ║ \r\n"
|
|
" ║ \r\n"
|
|
" ║ ");
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 1)));
|
|
EXPECT_EQ(position, 1);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(2, 1)));
|
|
EXPECT_EQ(position, 2);
|
|
EXPECT_TRUE(component->OnEvent(MouseReleased(2, 1)));
|
|
EXPECT_EQ(position, 2);
|
|
}
|
|
|
|
TEST(ResizableSplit, BasicRight) {
|
|
int position = 3;
|
|
auto component =
|
|
ResizableSplitRight(BasicComponent(), BasicComponent(), &position);
|
|
auto screen = Screen(20, 20);
|
|
Render(screen, component->Render());
|
|
EXPECT_EQ(position, 3);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(16, 1)));
|
|
EXPECT_EQ(position, 3);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(10, 1)));
|
|
EXPECT_EQ(position, 9);
|
|
EXPECT_TRUE(component->OnEvent(MouseReleased(10, 1)));
|
|
EXPECT_EQ(position, 9);
|
|
}
|
|
|
|
TEST(ResizableSplit, BasicRightWithCustomSeparator) {
|
|
int position = 1;
|
|
auto component = ResizableSplit({
|
|
.main = BasicComponent(),
|
|
.back = BasicComponent(),
|
|
.direction = Direction::Right,
|
|
.main_size = &position,
|
|
.separator_func = [] { return separatorDouble(); },
|
|
});
|
|
auto screen = Screen(4, 4);
|
|
Render(screen, component->Render());
|
|
EXPECT_EQ(position, 1);
|
|
EXPECT_EQ(screen.ToString(),
|
|
" ║ \r\n"
|
|
" ║ \r\n"
|
|
" ║ \r\n"
|
|
" ║ ");
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(2, 1)));
|
|
EXPECT_EQ(position, 1);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 1)));
|
|
EXPECT_EQ(position, 2);
|
|
EXPECT_TRUE(component->OnEvent(MouseReleased(1, 1)));
|
|
EXPECT_EQ(position, 2);
|
|
}
|
|
|
|
TEST(ResizableSplit, BasicTop) {
|
|
int position = 3;
|
|
auto component =
|
|
ResizableSplitTop(BasicComponent(), BasicComponent(), &position);
|
|
auto screen = Screen(20, 20);
|
|
Render(screen, component->Render());
|
|
EXPECT_EQ(position, 3);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 3)));
|
|
EXPECT_EQ(position, 3);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 10)));
|
|
EXPECT_EQ(position, 10);
|
|
EXPECT_TRUE(component->OnEvent(MouseReleased(1, 10)));
|
|
EXPECT_EQ(position, 10);
|
|
}
|
|
|
|
TEST(ResizableSplit, BasicTopWithCustomSeparator) {
|
|
int position = 1;
|
|
auto component = ResizableSplit({
|
|
.main = BasicComponent(),
|
|
.back = BasicComponent(),
|
|
.direction = Direction::Up,
|
|
.main_size = &position,
|
|
.separator_func = [] { return separatorDouble(); },
|
|
});
|
|
auto screen = Screen(4, 4);
|
|
Render(screen, component->Render());
|
|
EXPECT_EQ(position, 1);
|
|
EXPECT_EQ(screen.ToString(),
|
|
" \r\n"
|
|
"════\r\n"
|
|
" \r\n"
|
|
" ");
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 1)));
|
|
EXPECT_EQ(position, 1);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 2)));
|
|
EXPECT_EQ(position, 2);
|
|
EXPECT_TRUE(component->OnEvent(MouseReleased(1, 2)));
|
|
EXPECT_EQ(position, 2);
|
|
}
|
|
|
|
TEST(ResizableSplit, BasicBottom) {
|
|
int position = 3;
|
|
auto component =
|
|
ResizableSplitBottom(BasicComponent(), BasicComponent(), &position);
|
|
auto screen = Screen(20, 20);
|
|
Render(screen, component->Render());
|
|
EXPECT_EQ(position, 3);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 16)));
|
|
EXPECT_EQ(position, 3);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 10)));
|
|
EXPECT_EQ(position, 9);
|
|
EXPECT_TRUE(component->OnEvent(MouseReleased(1, 10)));
|
|
EXPECT_EQ(position, 9);
|
|
}
|
|
|
|
TEST(ResizableSplit, BasicBottomWithCustomSeparator) {
|
|
int position = 1;
|
|
auto component = ResizableSplit({
|
|
.main = BasicComponent(),
|
|
.back = BasicComponent(),
|
|
.direction = Direction::Down,
|
|
.main_size = &position,
|
|
.separator_func = [] { return separatorDouble(); },
|
|
});
|
|
auto screen = Screen(4, 4);
|
|
Render(screen, component->Render());
|
|
EXPECT_EQ(position, 1);
|
|
EXPECT_EQ(screen.ToString(),
|
|
" \r\n"
|
|
" \r\n"
|
|
"════\r\n"
|
|
" ");
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 2)));
|
|
EXPECT_EQ(position, 1);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 1)));
|
|
EXPECT_EQ(position, 2);
|
|
EXPECT_TRUE(component->OnEvent(MouseReleased(1, 1)));
|
|
EXPECT_EQ(position, 2);
|
|
}
|
|
|
|
TEST(ResizableSplit, NavigationVertical) {
|
|
int position = 0;
|
|
auto component_top = BasicComponent();
|
|
auto component_bottom = BasicComponent();
|
|
auto component =
|
|
ResizableSplitTop(component_top, component_bottom, &position);
|
|
|
|
EXPECT_TRUE(component_top->Active());
|
|
EXPECT_FALSE(component_bottom->Active());
|
|
|
|
EXPECT_FALSE(component->OnEvent(Event::ArrowRight));
|
|
EXPECT_TRUE(component_top->Active());
|
|
EXPECT_FALSE(component_bottom->Active());
|
|
|
|
EXPECT_TRUE(component->OnEvent(Event::ArrowDown));
|
|
EXPECT_FALSE(component_top->Active());
|
|
EXPECT_TRUE(component_bottom->Active());
|
|
|
|
EXPECT_FALSE(component->OnEvent(Event::ArrowDown));
|
|
EXPECT_FALSE(component_top->Active());
|
|
EXPECT_TRUE(component_bottom->Active());
|
|
|
|
EXPECT_TRUE(component->OnEvent(Event::ArrowUp));
|
|
EXPECT_TRUE(component_top->Active());
|
|
EXPECT_FALSE(component_bottom->Active());
|
|
}
|
|
|
|
TEST(ResizableSplit, MinMaxSizeLeft) {
|
|
int position = 5;
|
|
auto component = ResizableSplit({
|
|
.main = BasicComponent(),
|
|
.back = BasicComponent(),
|
|
.direction = Direction::Left,
|
|
.main_size = &position,
|
|
.separator_func = [] { return separatorDouble(); },
|
|
.min = 3,
|
|
.max = 8,
|
|
});
|
|
auto screen = Screen(20, 20);
|
|
Render(screen, component->Render());
|
|
EXPECT_EQ(position, 5);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(5, 1)));
|
|
EXPECT_EQ(position, 5);
|
|
// Try to resize below min
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(2, 1)));
|
|
EXPECT_EQ(position, 3); // Clamped to min
|
|
// Try to resize above max
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(10, 1)));
|
|
EXPECT_EQ(position, 8); // Clamped to max
|
|
EXPECT_TRUE(component->OnEvent(MouseReleased(10, 1)));
|
|
EXPECT_EQ(position, 8);
|
|
}
|
|
|
|
TEST(ResizableSplit, MinMaxSizeRight) {
|
|
int position = 5;
|
|
auto component = ResizableSplit({
|
|
.main = BasicComponent(),
|
|
.back = BasicComponent(),
|
|
.direction = Direction::Right,
|
|
.main_size = &position,
|
|
.separator_func = [] { return separatorDouble(); },
|
|
.min = 3,
|
|
.max = 8,
|
|
});
|
|
auto screen = Screen(20, 20);
|
|
Render(screen, component->Render());
|
|
EXPECT_EQ(position, 5);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(14, 1)));
|
|
EXPECT_EQ(position, 5);
|
|
// Try to resize below min
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(18, 1)));
|
|
EXPECT_EQ(position, 3); // Clamped to min
|
|
// Try to resize above max
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(10, 1)));
|
|
EXPECT_EQ(position, 8); // Clamped to max
|
|
EXPECT_TRUE(component->OnEvent(MouseReleased(10, 1)));
|
|
EXPECT_EQ(position, 8);
|
|
}
|
|
|
|
TEST(ResizableSplit, MinMaxSizeTop) {
|
|
int position = 5;
|
|
auto component = ResizableSplit({
|
|
.main = BasicComponent(),
|
|
.back = BasicComponent(),
|
|
.direction = Direction::Up,
|
|
.main_size = &position,
|
|
.separator_func = [] { return separatorDouble(); },
|
|
.min = 2,
|
|
.max = 10,
|
|
});
|
|
auto screen = Screen(20, 20);
|
|
Render(screen, component->Render());
|
|
EXPECT_EQ(position, 5);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 5)));
|
|
EXPECT_EQ(position, 5);
|
|
// Try to resize below min
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 1)));
|
|
EXPECT_EQ(position, 2); // Clamped to min
|
|
// Try to resize above max
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 15)));
|
|
EXPECT_EQ(position, 10); // Clamped to max
|
|
}
|
|
|
|
TEST(ResizableSplit, MinMaxSizeBottom) {
|
|
int position = 5;
|
|
auto component = ResizableSplit({
|
|
.main = BasicComponent(),
|
|
.back = BasicComponent(),
|
|
.direction = Direction::Down,
|
|
.main_size = &position,
|
|
.separator_func = [] { return separatorDouble(); },
|
|
.min = 3,
|
|
.max = 12,
|
|
});
|
|
auto screen = Screen(20, 20);
|
|
Render(screen, component->Render());
|
|
EXPECT_EQ(position, 5);
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 14)));
|
|
EXPECT_EQ(position, 5);
|
|
// Try to resize below min
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 18)));
|
|
EXPECT_EQ(position, 3); // Clamped to min
|
|
// Try to resize above max
|
|
EXPECT_TRUE(component->OnEvent(MousePressed(1, 5)));
|
|
EXPECT_EQ(position, 12); // Clamped to max
|
|
}
|
|
|
|
} // namespace ftxui
|
|
// NOLINTEND
|