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);
|
|
|
|
}
|
2024-12-24 00:53:28 +08:00
|
|
|
|
|
|
|
Event MouseMove(int x, int y) {
|
|
|
|
Mouse mouse;
|
|
|
|
mouse.button = Mouse::Left;
|
|
|
|
mouse.motion = Mouse::Moved;
|
|
|
|
mouse.shift = false;
|
|
|
|
mouse.meta = false;
|
|
|
|
mouse.control = false;
|
|
|
|
mouse.x = x;
|
|
|
|
mouse.y = y;
|
|
|
|
return Event::Mouse("jjj", mouse);
|
|
|
|
}
|
|
|
|
|
2024-12-03 15:12:35 +08:00
|
|
|
} // 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-24 00:53:28 +08:00
|
|
|
auto screen = ScreenInteractive::FixedSize(20, 1);
|
|
|
|
EXPECT_EQ(screen.SelectionAsString(), "");
|
|
|
|
Loop loop(&screen, component);
|
|
|
|
screen.PostEvent(MousePressed(3, 1));
|
|
|
|
screen.PostEvent(MouseReleased(10, 1));
|
|
|
|
loop.RunOnce();
|
2024-12-03 15:39:27 +08:00
|
|
|
|
2024-12-24 00:53:28 +08:00
|
|
|
EXPECT_EQ(screen.SelectionAsString(), "rem ipsu");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SelectionTest, SelectionOnChange) {
|
|
|
|
int selectionChangeCounter = 0;
|
|
|
|
auto component = Renderer([&] { return text("Lorem ipsum dolor"); });
|
2024-12-03 15:39:27 +08:00
|
|
|
auto screen = ScreenInteractive::FixedSize(20, 1);
|
2024-12-24 00:53:28 +08:00
|
|
|
screen.SelectionOnChange([&] { selectionChangeCounter++; });
|
2024-12-03 15:39:27 +08:00
|
|
|
|
|
|
|
Loop loop(&screen, component);
|
|
|
|
loop.RunOnce();
|
2024-12-24 00:53:28 +08:00
|
|
|
EXPECT_EQ(selectionChangeCounter, 0);
|
|
|
|
|
2024-12-03 15:39:27 +08:00
|
|
|
screen.PostEvent(MousePressed(3, 1));
|
|
|
|
loop.RunOnce();
|
2024-12-24 00:53:28 +08:00
|
|
|
EXPECT_EQ(selectionChangeCounter, 0);
|
|
|
|
|
|
|
|
screen.PostEvent(MouseMove(5, 1));
|
|
|
|
loop.RunOnce();
|
|
|
|
EXPECT_EQ(selectionChangeCounter, 1);
|
|
|
|
|
|
|
|
screen.PostEvent(MouseMove(7, 1));
|
|
|
|
loop.RunOnce();
|
|
|
|
EXPECT_EQ(selectionChangeCounter, 2);
|
|
|
|
|
2024-12-03 15:39:27 +08:00
|
|
|
screen.PostEvent(MouseReleased(10, 1));
|
|
|
|
loop.RunOnce();
|
2024-12-24 00:53:28 +08:00
|
|
|
EXPECT_EQ(selectionChangeCounter, 3);
|
2024-12-03 15:39:27 +08:00
|
|
|
|
2024-12-24 00:53:28 +08:00
|
|
|
screen.PostEvent(MouseMove(10, 1));
|
|
|
|
loop.RunOnce();
|
|
|
|
EXPECT_EQ(selectionChangeCounter, 3);
|
|
|
|
|
|
|
|
EXPECT_EQ(screen.SelectionAsString(), "rem ipsu");
|
2024-12-03 15:39:27 +08:00
|
|
|
}
|
|
|
|
|
2024-12-24 00:53:28 +08:00
|
|
|
// Check that submitting multiple mouse events quickly doesn't trigger multiple
|
|
|
|
// selection change events.
|
|
|
|
TEST(SelectionTest, SelectionOnChangeSquashedEvents) {
|
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:12:35 +08:00
|
|
|
auto screen = ScreenInteractive::FixedSize(20, 1);
|
2024-12-24 00:53:28 +08:00
|
|
|
screen.SelectionOnChange([&] { selectionChangeCounter++; });
|
2024-12-03 15:12:35 +08:00
|
|
|
|
|
|
|
Loop loop(&screen, component);
|
|
|
|
loop.RunOnce();
|
2024-12-24 00:53:28 +08:00
|
|
|
EXPECT_EQ(selectionChangeCounter, 0);
|
|
|
|
|
2024-12-03 15:12:35 +08:00
|
|
|
screen.PostEvent(MousePressed(3, 1));
|
2024-12-24 00:53:28 +08:00
|
|
|
screen.PostEvent(MouseMove(5, 1));
|
|
|
|
screen.PostEvent(MouseMove(7, 1));
|
2024-12-03 15:12:35 +08:00
|
|
|
loop.RunOnce();
|
2024-12-24 00:53:28 +08:00
|
|
|
EXPECT_EQ(selectionChangeCounter, 1);
|
|
|
|
|
2024-12-03 15:12:35 +08:00
|
|
|
screen.PostEvent(MouseReleased(10, 1));
|
2024-12-24 00:53:28 +08:00
|
|
|
screen.PostEvent(MouseMove(10, 1));
|
2024-12-03 15:12:35 +08:00
|
|
|
loop.RunOnce();
|
|
|
|
EXPECT_EQ(selectionChangeCounter, 2);
|
|
|
|
|
2024-12-24 00:53:28 +08:00
|
|
|
EXPECT_EQ(screen.SelectionAsString(), "rem ipsu");
|
2024-12-03 15:39:27 +08:00
|
|
|
}
|
2024-12-03 15:12:35 +08:00
|
|
|
|
2024-12-03 15:39:27 +08:00
|
|
|
TEST(SelectionTest, StyleSelection) {
|
|
|
|
int selectionChangeCounter = 0;
|
|
|
|
|
2024-12-24 00:53:28 +08:00
|
|
|
auto element = hbox({
|
|
|
|
text("Lorem "),
|
|
|
|
text("ipsum") | selectionColor(Color::Red),
|
|
|
|
text(" 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
|
|
|
|
2024-12-24 00:53:28 +08:00
|
|
|
Render(screen, element.get(), selection);
|
|
|
|
for (int i = 0; i < 20; i++) {
|
|
|
|
if (i >= 2 && i <= 9) {
|
|
|
|
EXPECT_EQ(screen.PixelAt(i, 0).inverted, true);
|
|
|
|
} else {
|
|
|
|
EXPECT_EQ(screen.PixelAt(i, 0).inverted, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= 6 && i <= 9) {
|
|
|
|
EXPECT_EQ(screen.PixelAt(i, 0).foreground_color, Color::Red);
|
|
|
|
} else {
|
|
|
|
EXPECT_EQ(screen.PixelAt(i, 0).foreground_color, Color::Default);
|
|
|
|
}
|
|
|
|
}
|
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
|