#include "ui/views/touchui/touch_selection_controller_impl.h"
#include <stddef.h>
#include <memory>
#include <string>
#include <utility>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/time/time.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/test/test_cursor_client.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_switches.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/event_constants.h"
#include "ui/events/event_utils.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/render_text.h"
#include "ui/touch_selection/touch_editing_controller.h"
#include "ui/touch_selection/touch_selection_menu_runner.h"
#include "ui/touch_selection/touch_selection_metrics.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/textfield/textfield_test_api.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
using base::ASCIIToUTF16;
using base::UTF16ToUTF8;
namespace {
const int kBarMinHeight = 5;
const int kBarBottomAllowance = 3;
int CompareTextSelectionBounds(const gfx::SelectionBound& b1,
const gfx::SelectionBound& b2) {
if (b1.edge_start().y() < b2.edge_start().y() ||
b1.edge_start().x() < b2.edge_start().x()) {
return -1;
}
if (b1 == b2) {
return 0;
}
return 1;
}
}
namespace views {
class TouchSelectionControllerImplTest : public ViewsTestBase {
public:
TouchSelectionControllerImplTest() = default;
TouchSelectionControllerImplTest(const TouchSelectionControllerImplTest&) =
delete;
TouchSelectionControllerImplTest& operator=(
const TouchSelectionControllerImplTest&) = delete;
~TouchSelectionControllerImplTest() override = default;
void SetUp() override {
ViewsTestBase::SetUp();
test_cursor_client_ =
std::make_unique<aura::test::TestCursorClient>(GetContext());
}
void TearDown() override {
textfield_ = nullptr;
test_cursor_client_.reset();
auto close_widget = [](std::unique_ptr<Widget>& widget) {
if (widget && !widget->IsClosed()) {
widget->Close();
}
};
close_widget(textfield_widget_);
close_widget(widget_);
ViewsTestBase::TearDown();
}
void CreateTextfield() {
textfield_widget_ = std::make_unique<Widget>();
Widget::InitParams params =
CreateParams(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.bounds = gfx::Rect(0, 0, 200, 200);
textfield_widget_->Init(std::move(params));
textfield_ = textfield_widget_->SetContentsView(std::make_unique<View>())
->AddChildView(Builder<Textfield>()
.SetPlaceholderText(u"Foo")
.SetID(1)
.SetBoundsRect(gfx::Rect(0, 0, 200, 21))
.Build());
textfield_widget_->Show();
textfield_->RequestFocus();
}
void CreateWidget() {
widget_ = std::make_unique<Widget>();
Widget::InitParams params = CreateParams(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_POPUP);
params.bounds = gfx::Rect(0, 0, 200, 200);
widget_->Init(std::move(params));
widget_->Show();
}
protected:
static bool IsCursorHandleVisibleFor(TouchSelectionController* controller) {
TouchSelectionControllerImpl* impl =
static_cast<TouchSelectionControllerImpl*>(controller);
return impl->IsCursorHandleVisible();
}
gfx::Rect GetCursorRect(const gfx::SelectionModel& sel) {
return TextfieldTestApi(textfield_)
.GetRenderText()
->GetCursorBounds(sel, true);
}
gfx::Point GetCursorPosition(const gfx::SelectionModel& sel) {
return GetCursorRect(sel).origin();
}
TouchSelectionControllerImpl* GetSelectionController() {
return static_cast<TouchSelectionControllerImpl*>(
TextfieldTestApi(textfield_).touch_selection_controller());
}
void StartTouchEditing() {
TextfieldTestApi(textfield_).CreateTouchSelectionControllerAndNotifyIt();
}
void EndTouchEditing() {
TextfieldTestApi(textfield_).ResetTouchSelectionController();
}
void SimulateSelectionHandleDrag(gfx::Vector2d v, int selection_handle) {
TouchSelectionControllerImpl* controller = GetSelectionController();
views::View* handle = nullptr;
if (selection_handle == 1) {
handle = controller->GetHandle1View();
} else {
handle = controller->GetHandle2View();
}
gfx::Point grip_location =
gfx::Point(handle->size().width() / 2, handle->size().height() / 2);
base::TimeTicks time_stamp = base::TimeTicks();
{
ui::GestureEventDetails details(ui::EventType::kGestureScrollBegin);
ui::GestureEvent scroll_begin(grip_location.x(), grip_location.y(), 0,
time_stamp, details);
handle->OnGestureEvent(&scroll_begin);
}
test_cursor_client_->DisableMouseEvents();
{
ui::GestureEventDetails details(ui::EventType::kGestureScrollUpdate);
gfx::Point update_location = grip_location + v;
ui::GestureEvent scroll_update(update_location.x(), update_location.y(),
0, time_stamp, details);
handle->OnGestureEvent(&scroll_update);
}
{
ui::GestureEventDetails details(ui::EventType::kGestureScrollEnd);
ui::GestureEvent scroll_end(grip_location.x(), grip_location.y(), 0,
time_stamp, details);
handle->OnGestureEvent(&scroll_end);
}
test_cursor_client_->EnableMouseEvents();
}
gfx::NativeView GetCursorHandleNativeView() {
return GetSelectionController()->GetCursorHandleNativeView();
}
gfx::SelectionBound::Type GetSelectionHandle1Type() {
return GetSelectionController()->GetSelectionHandle1Type();
}
gfx::Rect GetSelectionHandle1Bounds() {
return GetSelectionController()->GetSelectionHandle1Bounds();
}
gfx::Rect GetSelectionHandle2Bounds() {
return GetSelectionController()->GetSelectionHandle2Bounds();
}
gfx::Rect GetCursorHandleBounds() {
return GetSelectionController()->GetCursorHandleBounds();
}
gfx::Rect GetExpectedHandleBounds(const gfx::SelectionBound& bound) {
return GetSelectionController()->GetExpectedHandleBounds(bound);
}
bool IsSelectionHandle1Visible() {
return GetSelectionController()->IsSelectionHandle1Visible();
}
bool IsSelectionHandle2Visible() {
return GetSelectionController()->IsSelectionHandle2Visible();
}
bool IsCursorHandleVisible() {
return GetSelectionController()->IsCursorHandleVisible();
}
bool IsQuickMenuVisible() {
TouchSelectionControllerImpl* controller = GetSelectionController();
if (controller && controller->quick_menu_requested_) {
controller->ShowQuickMenuImmediatelyForTesting();
}
return ui::TouchSelectionMenuRunner::GetInstance() &&
ui::TouchSelectionMenuRunner::GetInstance()->IsRunning();
}
bool IsMagnifierVisible() {
return GetSelectionController()->touch_selection_magnifier_ != nullptr;
}
gfx::RenderText* GetRenderText() {
return TextfieldTestApi(textfield_).GetRenderText();
}
gfx::Point GetCursorHandleDragPoint() {
gfx::Rect rect = GetCursorHandleBounds();
const gfx::SelectionModel& sel = textfield_->GetSelectionModel();
int cursor_height = GetCursorRect(sel).height();
gfx::Point point = rect.CenterPoint();
point.Offset(0, cursor_height);
return point;
}
void VerifyHandlePositions(bool cursor_at_selection_handle_1,
bool check_direction,
const base::Location& from_here) {
gfx::SelectionBound anchor, focus;
textfield_->GetSelectionEndPoints(&anchor, &focus);
std::string from_str = from_here.ToString();
if (textfield_->HasSelection()) {
EXPECT_TRUE(IsSelectionHandle1Visible()) << from_str;
EXPECT_TRUE(IsSelectionHandle2Visible()) << from_str;
EXPECT_FALSE(IsCursorHandleVisible());
gfx::Rect sh1_bounds = GetSelectionHandle1Bounds();
gfx::Rect sh2_bounds = GetSelectionHandle2Bounds();
if (cursor_at_selection_handle_1) {
EXPECT_EQ(sh1_bounds, GetExpectedHandleBounds(focus)) << from_str;
EXPECT_EQ(sh2_bounds, GetExpectedHandleBounds(anchor)) << from_str;
} else {
EXPECT_EQ(sh1_bounds, GetExpectedHandleBounds(anchor)) << from_str;
EXPECT_EQ(sh2_bounds, GetExpectedHandleBounds(focus)) << from_str;
}
} else {
EXPECT_FALSE(IsSelectionHandle1Visible()) << from_str;
EXPECT_FALSE(IsSelectionHandle2Visible()) << from_str;
EXPECT_TRUE(IsCursorHandleVisible());
gfx::Rect cursor_bounds = GetCursorHandleBounds();
DCHECK(anchor == focus);
EXPECT_EQ(cursor_bounds, GetExpectedHandleBounds(anchor)) << from_str;
}
if (check_direction) {
if (CompareTextSelectionBounds(anchor, focus) < 0) {
EXPECT_EQ(gfx::SelectionBound::LEFT, anchor.type()) << from_str;
EXPECT_EQ(gfx::SelectionBound::RIGHT, focus.type()) << from_str;
} else if (CompareTextSelectionBounds(anchor, focus) > 0) {
EXPECT_EQ(gfx::SelectionBound::LEFT, focus.type()) << from_str;
EXPECT_EQ(gfx::SelectionBound::RIGHT, anchor.type()) << from_str;
} else {
EXPECT_EQ(gfx::SelectionBound::CENTER, focus.type()) << from_str;
EXPECT_EQ(gfx::SelectionBound::CENTER, anchor.type()) << from_str;
}
}
}
void SetupSelectionInvisibleHandle(uint32_t selection_start) {
CreateTextfield();
std::string some_text("some text");
std::string textfield_text;
for (int i = 0; i < 10; ++i) {
textfield_text += some_text;
}
textfield_->SetText(ASCIIToUTF16(textfield_text));
ui::GestureEventDetails details(ui::EventType::kGestureTap);
details.set_tap_count(1);
ui::GestureEvent tap(0, 0, 0, base::TimeTicks(), details);
textfield_->OnGestureEvent(&tap);
textfield_->SetSelectedRange(gfx::Range(
selection_start, static_cast<uint32_t>(textfield_text.length())));
EXPECT_FALSE(IsSelectionHandle1Visible());
EXPECT_TRUE(IsSelectionHandle2Visible());
EXPECT_EQ(gfx::Range(selection_start,
static_cast<uint32_t>(textfield_text.length())),
textfield_->GetSelectedRange());
}
void CloseHandle1Widget() {
GetSelectionController()->GetHandle1View()->GetWidget()->CloseWithReason(
views::Widget::ClosedReason::kUnspecified);
}
std::unique_ptr<Widget> textfield_widget_;
std::unique_ptr<Widget> widget_;
raw_ptr<Textfield> textfield_ = nullptr;
std::unique_ptr<aura::test::TestCursorClient> test_cursor_client_;
};
TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) {
CreateTextfield();
textfield_->SetText(u"some text");
ui::GestureEventDetails details(ui::EventType::kGestureTap);
details.set_tap_count(1);
ui::GestureEvent tap(0, 0, 0, base::TimeTicks(), details);
textfield_->OnGestureEvent(&tap);
textfield_->SetSelectedRange(gfx::Range(3, 7));
VerifyHandlePositions(false, true, FROM_HERE);
textfield_->SelectAll(false);
VerifyHandlePositions(false, true, FROM_HERE);
textfield_->ClearSelection();
VerifyHandlePositions(false, true, FROM_HERE);
textfield_widget_->GetFocusManager()->ClearFocus();
EXPECT_FALSE(GetSelectionController());
textfield_widget_->GetFocusManager()->SetFocusedView(textfield_);
EXPECT_FALSE(GetSelectionController());
textfield_->OnGestureEvent(&tap);
VerifyHandlePositions(false, true, FROM_HERE);
}
TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) {
CreateTextfield();
textfield_->SetText(u"abc\x05d0\x05d1\x05d2");
ui::GestureEventDetails details(ui::EventType::kGestureTap);
details.set_tap_count(1);
ui::GestureEvent tap(0, 0, 0, base::TimeTicks(), details);
textfield_->OnGestureEvent(&tap);
textfield_->SelectSelectionModel(
gfx::SelectionModel(3, gfx::CURSOR_BACKWARD));
VerifyHandlePositions(false, true, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(2, 3));
VerifyHandlePositions(false, true, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(3, 2));
VerifyHandlePositions(false, true, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(3, 4));
VerifyHandlePositions(false, false, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(4, 3));
VerifyHandlePositions(false, false, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(3, 6));
VerifyHandlePositions(false, false, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(6, 3));
VerifyHandlePositions(false, false, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(0, 6));
VerifyHandlePositions(false, true, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(6, 0));
VerifyHandlePositions(false, true, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(1, 4));
VerifyHandlePositions(false, true, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(4, 1));
VerifyHandlePositions(false, true, FROM_HERE);
}
TEST_F(TouchSelectionControllerImplTest, SelectionUpdateCallbackTest) {
CreateTextfield();
textfield_->SetText(u"textfield with selected text");
ui::GestureEventDetails details(ui::EventType::kGestureTap);
details.set_tap_count(1);
ui::GestureEvent tap(0, 0, 0, base::TimeTicks(), details);
textfield_->OnGestureEvent(&tap);
textfield_->SetSelectedRange(gfx::Range(3, 7));
gfx::Point textfield_origin;
textfield_->ConvertPointToScreen(&textfield_origin);
EXPECT_EQ("tfie", UTF16ToUTF8(textfield_->GetSelectedText()));
VerifyHandlePositions(false, true, FROM_HERE);
const gfx::FontList& font_list = textfield_->GetFontList();
int x = gfx::Canvas::GetStringWidth(u"ld ", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
EXPECT_EQ("tfield ", UTF16ToUTF8(textfield_->GetSelectedText()));
VerifyHandlePositions(false, true, FROM_HERE);
SimulateSelectionHandleDrag(gfx::Vector2d(-50, 0), 1);
EXPECT_EQ("textfield ", UTF16ToUTF8(textfield_->GetSelectedText()));
VerifyHandlePositions(true, true, FROM_HERE);
x = gfx::Canvas::GetStringWidth(u"textfield with ", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
EXPECT_EQ("with ", UTF16ToUTF8(textfield_->GetSelectedText()));
VerifyHandlePositions(true, true, FROM_HERE);
x = gfx::Canvas::GetStringWidth(u"with selected ", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
EXPECT_EQ("selected ", UTF16ToUTF8(textfield_->GetSelectedText()));
VerifyHandlePositions(false, true, FROM_HERE);
}
TEST_F(TouchSelectionControllerImplTest, SelectionUpdateInBidiCallbackTest) {
CreateTextfield();
textfield_->SetText(
u"abc\x05e1\x05e2\x05e3"
u"def");
ui::GestureEventDetails details(ui::EventType::kGestureTap);
details.set_tap_count(1);
ui::GestureEvent tap(0, 0, 0, base::TimeTicks(), details);
textfield_->OnGestureEvent(&tap);
textfield_->SetSelectedRange(gfx::Range(2, 3));
EXPECT_EQ(u"c", textfield_->GetSelectedText());
VerifyHandlePositions(false, true, FROM_HERE);
const gfx::FontList& font_list = textfield_->GetFontList();
int x = gfx::Canvas::GetStringWidth(u"\x05e3", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
EXPECT_EQ(u"c\x05e1\x05e2", textfield_->GetSelectedText());
VerifyHandlePositions(false, true, FROM_HERE);
x = gfx::Canvas::GetStringWidth(u"b", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
EXPECT_EQ(u"bc\x05e1\x05e2", textfield_->GetSelectedText());
VerifyHandlePositions(true, true, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(3, 2));
EXPECT_EQ(u"c", textfield_->GetSelectedText());
VerifyHandlePositions(false, true, FROM_HERE);
x = gfx::Canvas::GetStringWidth(u"\x05e3", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
EXPECT_EQ(u"c\x05e1\x05e2", textfield_->GetSelectedText());
VerifyHandlePositions(true, true, FROM_HERE);
x = gfx::Canvas::GetStringWidth(u"b", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
EXPECT_EQ(u"bc\x05e1\x05e2", textfield_->GetSelectedText());
VerifyHandlePositions(false, true, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(3, 4));
EXPECT_EQ(u"\x05e1", textfield_->GetSelectedText());
VerifyHandlePositions(false, false, FROM_HERE);
right of 'D' and select [D] then move the left selection handle to left
by one character, it should select [ED], instead it selects [F].
Reason: click right of 'D' and left of 'h' return the same x-axis position,
pass this position to FindCursorPosition() returns index of 'h'. which
means the selection start changed from 3 to 6.
Need further investigation on whether this is a bug in Pango and how to
work around it.
// Drag selection handle 2 to left by 1 char.
x = gfx::Canvas::GetStringWidth(u"\x05e2", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
EXPECT_EQ(u"\x05e1\x05e2", textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(false);
*/
x = gfx::Canvas::GetStringWidth(u"d", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
EXPECT_EQ(
u"\x05e2\x05e3"
u"d",
textfield_->GetSelectedText());
VerifyHandlePositions(true, true, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(4, 3));
EXPECT_EQ(u"\x05e1", textfield_->GetSelectedText());
VerifyHandlePositions(false, false, FROM_HERE);
// Drag selection handle 1 to left by 1 char.
x = gfx::Canvas::GetStringWidth(u"\x05e2", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
EXPECT_EQ(u"\x05e1\x05e2", textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(true);
*/
x = gfx::Canvas::GetStringWidth(u"d", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
EXPECT_EQ(
u"\x05e2\x05e3"
u"d",
textfield_->GetSelectedText());
VerifyHandlePositions(false, true, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(5, 6));
EXPECT_EQ(u"\x05e3", textfield_->GetSelectedText());
VerifyHandlePositions(false, false, FROM_HERE);
x = gfx::Canvas::GetStringWidth(u"c", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
EXPECT_EQ(u"c\x05e1\x05e2", textfield_->GetSelectedText());
VerifyHandlePositions(false, true, FROM_HERE);
x = gfx::Canvas::GetStringWidth(u"\x05e2", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
EXPECT_EQ(u"c\x05e1", textfield_->GetSelectedText());
VerifyHandlePositions(true, true, FROM_HERE);
textfield_->SetSelectedRange(gfx::Range(6, 5));
EXPECT_EQ(u"\x05e3", textfield_->GetSelectedText());
VerifyHandlePositions(false, false, FROM_HERE);
x = gfx::Canvas::GetStringWidth(u"c", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
EXPECT_EQ(u"c\x05e1\x05e2", textfield_->GetSelectedText());
VerifyHandlePositions(true, true, FROM_HERE);
x = gfx::Canvas::GetStringWidth(u"\x05e2", font_list);
SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
EXPECT_EQ(u"c\x05e1", textfield_->GetSelectedText());
VerifyHandlePositions(false, false, FROM_HERE);
}
TEST_F(TouchSelectionControllerImplTest,
HiddenSelectionHandleRetainsCursorPosition) {
static const uint32_t selection_start = 10u;
SetupSelectionInvisibleHandle(selection_start);
size_t visible_handle_position = textfield_->GetSelectedRange().end();
for (int i = 0; i < 10; ++i) {
static const int drag_diff = -10;
SimulateSelectionHandleDrag(gfx::Vector2d(drag_diff, 0), 2);
EXPECT_NE(visible_handle_position, textfield_->GetSelectedRange().end());
visible_handle_position = textfield_->GetSelectedRange().end();
EXPECT_EQ(10u, textfield_->GetSelectedRange().start());
}
}
TEST_F(TouchSelectionControllerImplTest, HiddenSelectionHandleExposed) {
static const uint32_t selection_start = 0u;
SetupSelectionInvisibleHandle(selection_start);
while (!IsSelectionHandle1Visible()) {
static const int drag_diff = -10;
SimulateSelectionHandleDrag(gfx::Vector2d(drag_diff, 0), 2);
}
EXPECT_EQ(gfx::SelectionBound::Type::LEFT, GetSelectionHandle1Type());
}
TEST_F(TouchSelectionControllerImplTest,
DoubleTapInTextfieldWithCursorHandleShouldSelectText) {
CreateTextfield();
textfield_->SetText(u"some text");
ui::test::EventGenerator generator(
textfield_->GetWidget()->GetNativeView()->GetRootWindow());
generator.GestureTapAt(gfx::Point(10, 10));
EXPECT_FALSE(textfield_->HasSelection());
VerifyHandlePositions(false, true, FROM_HERE);
gfx::Point cursor_pos = GetCursorHandleBounds().origin();
cursor_pos.Offset(GetCursorHandleBounds().width() / 2, 0);
generator.GestureTapAt(cursor_pos);
generator.GestureTapAt(cursor_pos);
EXPECT_TRUE(textfield_->HasSelection());
}
#if BUILDFLAG(ENABLE_DESKTOP_AURA) || BUILDFLAG(IS_CHROMEOS)
TEST_F(TouchSelectionControllerImplTest,
MenuAppearsAfterDraggingSelectionHandles) {
CreateTextfield();
textfield_->SetText(u"some text in a textfield");
textfield_->SetSelectedRange(gfx::Range(2, 15));
ui::test::EventGenerator generator(
textfield_->GetWidget()->GetNativeView()->GetRootWindow());
EXPECT_FALSE(IsQuickMenuVisible());
generator.GestureTapAt(gfx::Point(30, 15));
EXPECT_TRUE(IsQuickMenuVisible());
SimulateSelectionHandleDrag(gfx::Vector2d(3, 0), 1);
EXPECT_TRUE(IsQuickMenuVisible());
SimulateSelectionHandleDrag(gfx::Vector2d(-5, 0), 2);
EXPECT_TRUE(IsQuickMenuVisible());
textfield_widget_->GetFocusManager()->ClearFocus();
EXPECT_FALSE(IsQuickMenuVisible());
}
#endif
#if BUILDFLAG(IS_CHROMEOS)
TEST_F(TouchSelectionControllerImplTest,
MagnifierShownWhenDraggingCursorHandle) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
{::features::kTouchTextEditingRedesign},
{});
CreateTextfield();
textfield_->SetText(u"some text in a textfield");
ui::test::EventGenerator generator(
textfield_->GetWidget()->GetNativeView()->GetRootWindow());
generator.GestureTapAt(gfx::Point(10, 10));
EXPECT_TRUE(IsCursorHandleVisible());
EXPECT_FALSE(IsMagnifierVisible());
const gfx::Point drag_start = GetCursorHandleBounds().CenterPoint();
const gfx::Point drag_end = drag_start + gfx::Vector2d(50, 0);
generator.GestureScrollSequenceWithCallback(
drag_start, drag_end, base::Milliseconds(50),
5,
base::BindLambdaForTesting(
[&](ui::EventType event_type, const gfx::Vector2dF& offset) {
if (event_type == ui::EventType::kGestureScrollUpdate) {
EXPECT_TRUE(IsMagnifierVisible());
}
}));
EXPECT_FALSE(IsMagnifierVisible());
}
TEST_F(TouchSelectionControllerImplTest,
MagnifierShownWhenDraggingSelectionHandles) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
{::features::kTouchTextEditingRedesign},
{});
CreateTextfield();
textfield_->SetText(u"some text in a textfield");
textfield_->SetSelectedRange(gfx::Range(2, 15));
ui::test::EventGenerator generator(
textfield_->GetWidget()->GetNativeView()->GetRootWindow());
generator.GestureTapAt(gfx::Point(30, 15));
EXPECT_TRUE(IsSelectionHandle1Visible());
EXPECT_FALSE(IsMagnifierVisible());
gfx::Point drag_start = GetSelectionHandle1Bounds().CenterPoint();
gfx::Point drag_end = drag_start + gfx::Vector2d(50, 0);
generator.GestureScrollSequenceWithCallback(
drag_start, drag_end, base::Milliseconds(50),
5,
base::BindLambdaForTesting(
[&](ui::EventType event_type, const gfx::Vector2dF& offset) {
if (event_type == ui::EventType::kGestureScrollUpdate) {
EXPECT_TRUE(IsMagnifierVisible());
}
}));
EXPECT_FALSE(IsMagnifierVisible());
drag_start = GetSelectionHandle2Bounds().CenterPoint();
drag_end = drag_start + gfx::Vector2d(-60, 0);
generator.GestureScrollSequenceWithCallback(
drag_start, drag_end, base::Milliseconds(50),
5,
base::BindLambdaForTesting(
[&](ui::EventType event_type, const gfx::Vector2dF& offset) {
if (event_type == ui::EventType::kGestureScrollUpdate) {
EXPECT_TRUE(IsMagnifierVisible());
}
}));
EXPECT_FALSE(IsMagnifierVisible());
}
TEST_F(TouchSelectionControllerImplTest, MagnifierShownWhenDraggingCursor) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
{::features::kTouchTextEditingRedesign},
{});
CreateTextfield();
textfield_->SetText(u"some text in a textfield");
ui::test::EventGenerator generator(
textfield_->GetWidget()->GetNativeView()->GetRootWindow());
const gfx::Point drag_start =
GetCursorPosition(gfx::SelectionModel(6, gfx::CURSOR_FORWARD));
const gfx::Point drag_end = drag_start + gfx::Vector2d(80, 0);
generator.GestureScrollSequenceWithCallback(
drag_start, drag_end, base::Milliseconds(50),
5,
base::BindLambdaForTesting(
[&](ui::EventType event_type, const gfx::Vector2dF& offset) {
if (event_type == ui::EventType::kGestureScrollUpdate) {
EXPECT_TRUE(IsMagnifierVisible());
}
}));
EXPECT_FALSE(IsMagnifierVisible());
}
TEST_F(TouchSelectionControllerImplTest, DraggingCursorShowsHandle) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
{::features::kTouchTextEditingRedesign},
{});
CreateTextfield();
textfield_->SetText(u"some text in a textfield");
ui::test::EventGenerator generator(
textfield_->GetWidget()->GetNativeView()->GetRootWindow());
const gfx::Point drag_start =
GetCursorPosition(gfx::SelectionModel(6, gfx::CURSOR_FORWARD));
const gfx::Point drag_end = drag_start + gfx::Vector2d(80, 0);
generator.GestureScrollSequenceWithCallback(
drag_start, drag_end, base::Milliseconds(50),
5,
base::BindLambdaForTesting(
[&](ui::EventType event_type, const gfx::Vector2dF& offset) {
if (event_type == ui::EventType::kGestureScrollUpdate) {
EXPECT_FALSE(IsCursorHandleVisible());
EXPECT_FALSE(IsSelectionHandle1Visible());
EXPECT_FALSE(IsSelectionHandle2Visible());
}
}));
EXPECT_TRUE(IsCursorHandleVisible());
EXPECT_FALSE(IsSelectionHandle1Visible());
EXPECT_FALSE(IsSelectionHandle2Visible());
}
TEST_F(TouchSelectionControllerImplTest, TapOnHandleTogglesMenu) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
{::features::kTouchTextEditingRedesign},
{});
CreateTextfield();
textfield_->SetText(u"some text in a textfield");
ui::test::EventGenerator generator(
textfield_->GetWidget()->GetNativeView()->GetRootWindow());
generator.GestureTapAt(gfx::Point(10, 10));
EXPECT_TRUE(IsCursorHandleVisible());
EXPECT_FALSE(IsQuickMenuVisible());
gfx::Point handle_pos = GetCursorHandleBounds().CenterPoint();
generator.GestureTapAt(handle_pos);
EXPECT_TRUE(IsCursorHandleVisible());
EXPECT_TRUE(IsQuickMenuVisible());
generator.GestureTapAt(handle_pos);
EXPECT_TRUE(IsCursorHandleVisible());
EXPECT_FALSE(IsQuickMenuVisible());
generator.GestureTapAt(handle_pos);
EXPECT_TRUE(IsCursorHandleVisible());
EXPECT_TRUE(IsQuickMenuVisible());
generator.GestureTapAt(gfx::Point(60, 10));
EXPECT_TRUE(IsCursorHandleVisible());
EXPECT_FALSE(IsQuickMenuVisible());
}
TEST_F(TouchSelectionControllerImplTest, TapOnCursorTogglesMenu) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
{::features::kTouchTextEditingRedesign},
{});
CreateTextfield();
textfield_->SetText(u"some text in a textfield");
textfield_->SetSelectedRange(gfx::Range(7, 7));
const gfx::Point cursor_position =
GetCursorRect(textfield_->GetSelectionModel()).CenterPoint();
ui::test::EventGenerator generator(
textfield_->GetWidget()->GetNativeView()->GetRootWindow());
generator.GestureTapAt(cursor_position);
EXPECT_TRUE(IsCursorHandleVisible());
EXPECT_TRUE(IsQuickMenuVisible());
generator.AdvanceClock(base::Milliseconds(1000));
generator.GestureTapAt(cursor_position);
EXPECT_TRUE(IsCursorHandleVisible());
EXPECT_FALSE(IsQuickMenuVisible());
generator.AdvanceClock(base::Milliseconds(1000));
generator.GestureTapAt(cursor_position);
EXPECT_TRUE(IsCursorHandleVisible());
EXPECT_TRUE(IsQuickMenuVisible());
generator.AdvanceClock(base::Milliseconds(1000));
generator.GestureTapAt(gfx::Point(100, 10));
EXPECT_TRUE(IsCursorHandleVisible());
EXPECT_FALSE(IsQuickMenuVisible());
}
TEST_F(TouchSelectionControllerImplTest, MenuHiddenWhenDraggingCursor) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
{::features::kTouchTextEditingRedesign},
{});
CreateTextfield();
textfield_->SetText(u"some text in a textfield");
ui::test::EventGenerator generator(
textfield_->GetWidget()->GetNativeView()->GetRootWindow());
generator.GestureTapAt(gfx::Point(10, 10));
generator.GestureTapAt(GetCursorHandleBounds().CenterPoint());
EXPECT_TRUE(IsQuickMenuVisible());
const gfx::Point drag_start =
GetCursorPosition(gfx::SelectionModel(6, gfx::CURSOR_FORWARD));
const gfx::Point drag_end = drag_start + gfx::Vector2d(80, 0);
generator.GestureScrollSequenceWithCallback(
drag_start, drag_end, base::Milliseconds(50),
5,
base::BindLambdaForTesting(
[&](ui::EventType event_type, const gfx::Vector2dF& offset) {
if (event_type == ui::EventType::kGestureScrollUpdate) {
EXPECT_FALSE(IsQuickMenuVisible());
}
}));
}
TEST_F(TouchSelectionControllerImplTest, SelectCommands) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
{::features::kTouchTextEditingRedesign},
{});
CreateTextfield();
textfield_->SetText(u"some text");
ui::test::EventGenerator generator(
textfield_->GetWidget()->GetNativeView()->GetRootWindow());
generator.GestureTapAt(gfx::Point(10, 10));
ui::TouchSelectionMenuClient* menu_client = GetSelectionController();
ASSERT_TRUE(menu_client);
EXPECT_TRUE(menu_client->IsCommandIdEnabled(ui::TouchEditable::kSelectAll));
EXPECT_TRUE(menu_client->IsCommandIdEnabled(ui::TouchEditable::kSelectWord));
menu_client->ExecuteCommand(ui::TouchEditable::kSelectWord,
ui::EF_FROM_TOUCH);
EXPECT_EQ("some", UTF16ToUTF8(textfield_->GetSelectedText()));
menu_client = GetSelectionController();
ASSERT_TRUE(menu_client);
EXPECT_TRUE(menu_client->IsCommandIdEnabled(ui::TouchEditable::kSelectAll));
EXPECT_FALSE(menu_client->IsCommandIdEnabled(ui::TouchEditable::kSelectWord));
menu_client->ExecuteCommand(ui::TouchEditable::kSelectAll, ui::EF_FROM_TOUCH);
EXPECT_EQ("some text", UTF16ToUTF8(textfield_->GetSelectedText()));
menu_client = GetSelectionController();
ASSERT_TRUE(menu_client);
EXPECT_FALSE(menu_client->IsCommandIdEnabled(ui::TouchEditable::kSelectAll));
EXPECT_FALSE(menu_client->IsCommandIdEnabled(ui::TouchEditable::kSelectWord));
}
TEST_F(TouchSelectionControllerImplTest, CursorHandleDraggingMetrics) {
base::HistogramTester histogram_tester;
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
{::features::kTouchTextEditingRedesign},
{});
CreateTextfield();
textfield_->SetText(u"some text in a textfield");
ui::test::EventGenerator generator(
textfield_->GetWidget()->GetNativeView()->GetRootWindow());
generator.GestureTapAt(gfx::Point(10, 10));
EXPECT_TRUE(IsCursorHandleVisible());
const gfx::Point drag_start = GetCursorHandleBounds().CenterPoint();
const gfx::Point drag_end = drag_start + gfx::Vector2d(50, 0);
generator.GestureScrollSequence(drag_start, drag_end,
base::Milliseconds(50),
5);
histogram_tester.ExpectBucketCount(
ui::kTouchSelectionDragTypeHistogramName,
ui::TouchSelectionDragType::kCursorHandleDrag, 1);
}
TEST_F(TouchSelectionControllerImplTest, SelectionHandleDraggingMetrics) {
base::HistogramTester histogram_tester;
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
{::features::kTouchTextEditingRedesign},
{});
CreateTextfield();
textfield_->SetText(u"some text in a textfield");
textfield_->SetSelectedRange(gfx::Range(2, 15));
ui::test::EventGenerator generator(
textfield_->GetWidget()->GetNativeView()->GetRootWindow());
generator.GestureTapAt(gfx::Point(30, 15));
EXPECT_TRUE(IsSelectionHandle1Visible());
gfx::Point drag_start = GetSelectionHandle1Bounds().CenterPoint();
gfx::Point drag_end = drag_start + gfx::Vector2d(50, 0);
generator.GestureScrollSequence(drag_start, drag_end,
base::Milliseconds(50),
5);
histogram_tester.ExpectBucketCount(
ui::kTouchSelectionDragTypeHistogramName,
ui::TouchSelectionDragType::kSelectionHandleDrag, 1);
drag_start = GetSelectionHandle2Bounds().CenterPoint();
drag_end = drag_start + gfx::Vector2d(-60, 0);
generator.GestureScrollSequence(drag_start, drag_end,
base::Milliseconds(50),
5);
histogram_tester.ExpectBucketCount(
ui::kTouchSelectionDragTypeHistogramName,
ui::TouchSelectionDragType::kSelectionHandleDrag, 2);
}
#endif
class TestTouchEditable : public ui::TouchEditable {
public:
explicit TestTouchEditable(aura::Window* window) : window_(window) {
DCHECK(window);
}
void set_bounds(const gfx::Rect& bounds) { bounds_ = bounds; }
void set_cursor_rect(const gfx::RectF& cursor_rect) {
cursor_bound_.SetEdge(cursor_rect.origin(), cursor_rect.bottom_left());
cursor_bound_.set_type(gfx::SelectionBound::Type::CENTER);
}
TestTouchEditable(const TestTouchEditable&) = delete;
TestTouchEditable& operator=(const TestTouchEditable&) = delete;
~TestTouchEditable() override = default;
private:
void MoveCaret(const gfx::Point& position) override { NOTREACHED(); }
void MoveRangeSelectionExtent(const gfx::Point& extent) override {
NOTREACHED();
}
void SelectBetweenCoordinates(const gfx::Point& base,
const gfx::Point& extent) override {
NOTREACHED();
}
void GetSelectionEndPoints(gfx::SelectionBound* anchor,
gfx::SelectionBound* focus) override {
*anchor = *focus = cursor_bound_;
}
gfx::Rect GetBounds() override { return gfx::Rect(bounds_.size()); }
gfx::NativeView GetNativeView() const override { return window_; }
bool IsSelectionDragging() const override { return false; }
void ConvertPointToScreen(gfx::Point* point) override {
aura::client::ScreenPositionClient* screen_position_client =
aura::client::GetScreenPositionClient(window_->GetRootWindow());
if (screen_position_client) {
screen_position_client->ConvertPointToScreen(window_, point);
}
}
void ConvertPointFromScreen(gfx::Point* point) override {
aura::client::ScreenPositionClient* screen_position_client =
aura::client::GetScreenPositionClient(window_->GetRootWindow());
if (screen_position_client) {
screen_position_client->ConvertPointFromScreen(window_, point);
}
}
void OpenContextMenu(const gfx::Point& anchor) override { NOTREACHED(); }
void DestroyTouchSelection() override { NOTREACHED(); }
bool IsCommandIdChecked(int command_id) const override { NOTREACHED(); }
bool IsCommandIdEnabled(int command_id) const override { NOTREACHED(); }
void ExecuteCommand(int command_id, int event_flags) override {
NOTREACHED();
}
raw_ptr<aura::Window> window_;
gfx::Rect bounds_;
gfx::SelectionBound cursor_bound_;
};
TEST_F(TouchSelectionControllerImplTest,
VisibilityOfHandleRegardingClientBounds) {
CreateWidget();
TestTouchEditable touch_editable(widget_->GetNativeView());
std::unique_ptr<TouchSelectionController> touch_selection_controller =
std::make_unique<TouchSelectionControllerImpl>(&touch_editable);
touch_editable.set_bounds(gfx::Rect(0, 0, 100, 20));
touch_editable.set_cursor_rect(gfx::RectF(2.f, 0.f, 1.f, 20.f));
touch_selection_controller->SelectionChanged();
EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
touch_editable.set_cursor_rect(
gfx::RectF(2.f, kBarMinHeight - 20.f, 1.f, 20.f));
touch_selection_controller->SelectionChanged();
EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
touch_editable.set_cursor_rect(
gfx::RectF(2.f, kBarMinHeight - 20.f - 1.f, 1.f, 20.f));
touch_selection_controller->SelectionChanged();
EXPECT_FALSE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
touch_editable.set_cursor_rect(
gfx::RectF(2.f, kBarBottomAllowance, 1.f, 20.f));
touch_selection_controller->SelectionChanged();
EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
touch_editable.set_cursor_rect(
gfx::RectF(2.f, kBarBottomAllowance + 1.f, 1.f, 20.f));
touch_selection_controller->SelectionChanged();
EXPECT_FALSE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
touch_selection_controller.reset();
}
TEST_F(TouchSelectionControllerImplTest, HandlesStackAboveParent) {
aura::Window* root = GetContext();
ui::EventTargeter* targeter =
root->GetHost()->dispatcher()->GetDefaultEventTargeter();
CreateTextfield();
aura::Window* window1 = textfield_widget_->GetNativeView();
StartTouchEditing();
gfx::Point test_point = GetCursorHandleDragPoint();
ui::MouseEvent test_event1(ui::EventType::kMouseMoved, test_point, test_point,
ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
EXPECT_EQ(GetCursorHandleNativeView(),
targeter->FindTargetForEvent(root, &test_event1));
EndTouchEditing();
CreateWidget();
aura::Window* window2 = widget_->GetNativeView();
StartTouchEditing();
ui::MouseEvent test_event2(ui::EventType::kMouseMoved, test_point, test_point,
ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
EXPECT_EQ(window2, targeter->FindTargetForEvent(root, &test_event2));
window1->GetRootWindow()->StackChildAtTop(window1);
ui::MouseEvent test_event3(ui::EventType::kMouseMoved, test_point, test_point,
ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
EXPECT_EQ(GetCursorHandleNativeView(),
targeter->FindTargetForEvent(root, &test_event3));
}
TEST_F(TouchSelectionControllerImplTest, MouseEventDeactivatesTouchSelection) {
CreateTextfield();
EXPECT_FALSE(GetSelectionController());
ui::test::EventGenerator generator(
textfield_widget_->GetNativeView()->GetRootWindow());
generator.set_current_screen_location(gfx::Point(5, 5));
RunPendingMessages();
StartTouchEditing();
EXPECT_TRUE(GetSelectionController());
generator.MoveMouseTo(gfx::Point(5, 10));
RunPendingMessages();
EXPECT_FALSE(GetSelectionController());
generator.MoveMouseTo(gfx::Point(5, 50));
RunPendingMessages();
StartTouchEditing();
EXPECT_TRUE(GetSelectionController());
generator.MoveMouseTo(gfx::Point(5, 55));
RunPendingMessages();
EXPECT_FALSE(GetSelectionController());
generator.MoveMouseTo(gfx::Point(5, 500));
RunPendingMessages();
StartTouchEditing();
EXPECT_TRUE(GetSelectionController());
generator.MoveMouseTo(5, 505);
RunPendingMessages();
EXPECT_FALSE(GetSelectionController());
}
TEST_F(TouchSelectionControllerImplTest, MouseCaptureChangedEventIgnored) {
CreateTextfield();
EXPECT_FALSE(GetSelectionController());
ui::test::EventGenerator generator(
textfield_widget_->GetNativeView()->GetRootWindow());
RunPendingMessages();
StartTouchEditing();
EXPECT_TRUE(GetSelectionController());
ui::MouseEvent capture_changed(ui::EventType::kMouseCaptureChanged,
gfx::Point(5, 5), gfx::Point(5, 5),
base::TimeTicks(), 0, 0);
generator.Dispatch(&capture_changed);
RunPendingMessages();
EXPECT_TRUE(GetSelectionController());
}
TEST_F(TouchSelectionControllerImplTest, KeyEventDeactivatesTouchSelection) {
CreateTextfield();
EXPECT_FALSE(GetSelectionController());
ui::test::EventGenerator generator(
textfield_widget_->GetNativeView()->GetRootWindow());
RunPendingMessages();
StartTouchEditing();
EXPECT_TRUE(GetSelectionController());
generator.PressKey(ui::VKEY_A, 0);
RunPendingMessages();
EXPECT_FALSE(GetSelectionController());
}
TEST_F(TouchSelectionControllerImplTest,
DestroyingEditingHandleWidgetDoesNotCrash) {
CreateTextfield();
textfield_->SetText(u"some text in a textfield");
textfield_->SetSelectedRange(gfx::Range(7, 7));
const gfx::Point cursor_position =
GetCursorRect(textfield_->GetSelectionModel()).CenterPoint();
ui::test::EventGenerator generator(
textfield_->GetWidget()->GetNativeView()->GetRootWindow());
generator.GestureTapAt(cursor_position);
EXPECT_TRUE(GetSelectionController());
CloseHandle1Widget();
RunPendingMessages();
generator.GestureTapAt(cursor_position);
generator.GestureTapAt(cursor_position);
textfield_->DestroyTouchSelection();
RunPendingMessages();
EXPECT_FALSE(GetSelectionController());
}
}