2024-12-03 15:12:35 +08:00
|
|
|
// 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 <gtest/gtest.h>
|
|
|
|
#include <csignal> // for raise, SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM
|
|
|
|
|
2024-12-23 00:19:50 +08:00
|
|
|
#include "ftxui/component/component.hpp" // for Input, Renderer, Vertical
|
|
|
|
#include "ftxui/component/event.hpp" // for Event
|
|
|
|
#include "ftxui/component/loop.hpp" // for Loop
|
2024-12-03 15:12:35 +08:00
|
|
|
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed, Mouse::Released
|
|
|
|
#include "ftxui/component/screen_interactive.hpp"
|
2024-12-23 00:19:50 +08:00
|
|
|
#include "ftxui/dom/elements.hpp" // for text
|
|
|
|
#include "ftxui/dom/node.hpp" // for Render
|
|
|
|
#include "ftxui/screen/screen.hpp" // for Screen
|
2024-12-03 15:12:35 +08:00
|
|
|
|
|
|
|
// NOLINTBEGIN
|
|
|
|
namespace ftxui {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
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
|
|
|
|
|
2024-12-03 15:39:27 +08:00
|
|
|
TEST(SelectionTest, DefaultSelection) {
|
2024-12-23 00:19:50 +08:00
|
|
|
auto component = Renderer([&] { return text("Lorem ipsum dolor"); });
|
2024-12-03 15:39:27 +08:00
|
|
|
|
|
|
|
auto screen = ScreenInteractive::FixedSize(20, 1);
|
|
|
|
|
|
|
|
Loop loop(&screen, component);
|
|
|
|
|
|
|
|
loop.RunOnce();
|
|
|
|
screen.PostEvent(MousePressed(3, 1));
|
|
|
|
loop.RunOnce();
|
|
|
|
screen.PostEvent(MouseReleased(10, 1));
|
|
|
|
loop.RunOnce();
|
|
|
|
|
|
|
|
EXPECT_STREQ(screen.GetSelectedContent(component).c_str(), "rem ipsu");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SelectionTest, CallbackSelection) {
|
2024-12-03 15:12:35 +08:00
|
|
|
int selectionChangeCounter = 0;
|
|
|
|
|
2024-12-23 00:19:50 +08:00
|
|
|
auto component = Renderer([&] { return text("Lorem ipsum dolor"); });
|
2024-12-03 15:39:27 +08:00
|
|
|
|
2024-12-03 15:12:35 +08:00
|
|
|
auto screen = ScreenInteractive::FixedSize(20, 1);
|
|
|
|
|
|
|
|
Loop loop(&screen, component);
|
|
|
|
|
|
|
|
loop.RunOnce();
|
|
|
|
screen.PostEvent(MousePressed(3, 1));
|
|
|
|
loop.RunOnce();
|
|
|
|
screen.PostEvent(MouseReleased(10, 1));
|
|
|
|
loop.RunOnce();
|
|
|
|
|
|
|
|
EXPECT_EQ(selectionChangeCounter, 2);
|
|
|
|
|
2024-12-03 15:39:27 +08:00
|
|
|
EXPECT_STREQ(screen.GetSelectedContent(component).c_str(), "rem ipsu");
|
|
|
|
}
|
2024-12-03 15:12:35 +08:00
|
|
|
|
2024-12-03 15:39:27 +08:00
|
|
|
TEST(SelectionTest, StyleSelection) {
|
|
|
|
int selectionChangeCounter = 0;
|
|
|
|
|
2024-12-23 00:19:50 +08:00
|
|
|
auto component = Renderer([&] { return text("Lorem ipsum dolor"); });
|
2024-12-03 15:39:27 +08:00
|
|
|
|
|
|
|
auto screen = ScreenInteractive::FixedSize(20, 1);
|
|
|
|
|
2024-12-23 00:19:50 +08:00
|
|
|
Selection selection(2, 0, 9, 0);
|
2024-12-03 15:39:27 +08:00
|
|
|
|
|
|
|
Render(screen, component->Render().get(), selection);
|
|
|
|
|
|
|
|
EXPECT_EQ(screen.ToString(), "Lo\x1B[21mrem ipsu\x1B[24mm dolor ");
|
2024-12-03 15:12:35 +08:00
|
|
|
}
|
|
|
|
|
2024-12-03 15:47:26 +08:00
|
|
|
TEST(SelectionTest, VBoxSelection) {
|
|
|
|
auto component = Renderer([&] {
|
2024-12-23 00:19:50 +08:00
|
|
|
return vbox({text("Lorem ipsum dolor"), text("Ut enim ad minim")});
|
2024-12-03 15:47:26 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
auto screen = ScreenInteractive::FixedSize(20, 2);
|
|
|
|
|
|
|
|
Selection selection(2, 0, 2, 1);
|
|
|
|
|
|
|
|
Render(screen, component->Render().get(), selection);
|
|
|
|
|
2024-12-23 00:19:50 +08:00
|
|
|
EXPECT_EQ(screen.ToString(),
|
|
|
|
"Lo\x1B[7mrem ipsum dolor\x1B[27m \r\n\x1B[7mUt \x1B[27menim ad "
|
|
|
|
"minim ");
|
2024-12-03 15:47:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SelectionTest, VBoxSaturatedSelection) {
|
|
|
|
auto component = Renderer([&] {
|
2024-12-23 00:19:50 +08:00
|
|
|
return vbox({text("Lorem ipsum dolor"), text("Ut enim ad minim"),
|
|
|
|
text("Duis aute irure")});
|
2024-12-03 15:47:26 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
auto screen = ScreenInteractive::FixedSize(20, 3);
|
|
|
|
|
|
|
|
Selection selection(2, 0, 2, 2);
|
|
|
|
|
|
|
|
Render(screen, component->Render().get(), selection);
|
|
|
|
|
2024-12-23 00:19:50 +08:00
|
|
|
EXPECT_EQ(screen.ToString(),
|
|
|
|
"Lo\x1B[7mrem ipsum dolor\x1B[27m \r\n\x1B[7mUt enim ad "
|
|
|
|
"minim\x1B[27m \r\n\x1B[7mDui\x1B[27ms aute irure ");
|
2024-12-03 15:47:26 +08:00
|
|
|
}
|
|
|
|
|
2024-12-03 16:01:10 +08:00
|
|
|
TEST(SelectionTest, HBoxSelection) {
|
|
|
|
auto component = Renderer([&] {
|
2024-12-23 00:19:50 +08:00
|
|
|
return hbox({text("Lorem ipsum dolor"), text("Ut enim ad minim")});
|
2024-12-03 16:01:10 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
auto screen = ScreenInteractive::FixedSize(40, 1);
|
|
|
|
|
|
|
|
Selection selection(2, 0, 20, 0);
|
|
|
|
|
|
|
|
Render(screen, component->Render().get(), selection);
|
|
|
|
|
2024-12-23 00:19:50 +08:00
|
|
|
EXPECT_EQ(screen.ToString(),
|
|
|
|
"Lo\x1B[7mrem ipsum dolorUt e\x1B[27mnim ad minim ");
|
2024-12-03 16:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SelectionTest, HBoxSaturatedSelection) {
|
|
|
|
auto component = Renderer([&] {
|
2024-12-23 00:19:50 +08:00
|
|
|
return hbox({text("Lorem ipsum dolor"), text("Ut enim ad minim"),
|
|
|
|
text("Duis aute irure")});
|
2024-12-03 16:01:10 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
auto screen = ScreenInteractive::FixedSize(60, 1);
|
|
|
|
|
|
|
|
Selection selection(2, 0, 35, 0);
|
|
|
|
|
|
|
|
Render(screen, component->Render().get(), selection);
|
|
|
|
|
2024-12-23 00:19:50 +08:00
|
|
|
EXPECT_EQ(screen.ToString(),
|
|
|
|
"Lo\x1B[7mrem ipsum dolorUt enim ad minimDui\x1B[27ms aute irure "
|
|
|
|
" ");
|
2024-12-03 16:01:10 +08:00
|
|
|
}
|
2024-12-03 15:39:27 +08:00
|
|
|
|
2024-12-03 15:12:35 +08:00
|
|
|
} // namespace ftxui
|
|
|
|
// NOLINTEND
|