This source file includes following definitions.
- GetHandleImage
- GetHandleImageSize
- views_tsc_factory_
- TearDown
- CreateTextfield
- GetCursorPosition
- GetSelectionController
- SimulateSelectionHandleDrag
- GetSelectionHandle1Position
- GetSelectionHandle2Position
- GetCursorHandlePosition
- IsSelectionHandle1Visible
- IsSelectionHandle2Visible
- IsCursorHandleVisible
- GetRenderText
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/command_line.h"
#include "base/strings/utf_string_conversions.h"
#include "grit/ui_resources.h"
#include "ui/aura/test/event_generator.h"
#include "ui/aura/window.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/touch/touch_editing_controller.h"
#include "ui/base/ui_base_switches.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/render_text.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/touchui/touch_selection_controller_impl.h"
#include "ui/views/widget/widget.h"
using base::ASCIIToUTF16;
using base::UTF16ToUTF8;
using base::WideToUTF16;
namespace {
const int kPadding = 10;
gfx::Image* GetHandleImage() {
static gfx::Image* handle_image = NULL;
if (!handle_image) {
handle_image = &ui::ResourceBundle::GetSharedInstance().GetImageNamed(
IDR_TEXT_SELECTION_HANDLE);
}
return handle_image;
}
gfx::Size GetHandleImageSize() {
return GetHandleImage()->Size();
}
}
namespace views {
class TouchSelectionControllerImplTest : public ViewsTestBase {
public:
TouchSelectionControllerImplTest()
: widget_(NULL),
textfield_(NULL),
views_tsc_factory_(new ViewsTouchSelectionControllerFactory) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kEnableTouchEditing);
ui::TouchSelectionControllerFactory::SetInstance(views_tsc_factory_.get());
}
virtual ~TouchSelectionControllerImplTest() {
ui::TouchSelectionControllerFactory::SetInstance(NULL);
}
virtual void TearDown() {
if (widget_)
widget_->Close();
ViewsTestBase::TearDown();
}
void CreateTextfield() {
textfield_ = new Textfield();
widget_ = new Widget;
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.bounds = gfx::Rect(0, 0, 200, 200);
widget_->Init(params);
View* container = new View();
widget_->SetContentsView(container);
container->AddChildView(textfield_);
textfield_->SetBoundsRect(params.bounds);
textfield_->set_id(1);
widget_->Show();
textfield_->RequestFocus();
}
protected:
gfx::Point GetCursorPosition(const gfx::SelectionModel& sel) {
gfx::RenderText* render_text = textfield_->GetRenderText();
gfx::Rect cursor_bounds = render_text->GetCursorBounds(sel, true);
return gfx::Point(cursor_bounds.x(), cursor_bounds.y());
}
TouchSelectionControllerImpl* GetSelectionController() {
return static_cast<TouchSelectionControllerImpl*>(
textfield_->touch_selection_controller_.get());
}
void SimulateSelectionHandleDrag(gfx::Point p, int selection_handle) {
TouchSelectionControllerImpl* controller = GetSelectionController();
if (selection_handle == 1)
controller->SetDraggingHandle(controller->selection_handle_1_.get());
else
controller->SetDraggingHandle(controller->selection_handle_2_.get());
p.Offset(GetHandleImageSize().width() / 2 + kPadding, 0);
controller->SelectionHandleDragged(p);
controller->dragging_handle_ = NULL;
}
gfx::Point GetSelectionHandle1Position() {
return GetSelectionController()->GetSelectionHandle1Position();
}
gfx::Point GetSelectionHandle2Position() {
return GetSelectionController()->GetSelectionHandle2Position();
}
gfx::Point GetCursorHandlePosition() {
return GetSelectionController()->GetCursorHandlePosition();
}
bool IsSelectionHandle1Visible() {
return GetSelectionController()->IsSelectionHandle1Visible();
}
bool IsSelectionHandle2Visible() {
return GetSelectionController()->IsSelectionHandle2Visible();
}
bool IsCursorHandleVisible() {
return GetSelectionController()->IsCursorHandleVisible();
}
gfx::RenderText* GetRenderText() {
return textfield_->GetRenderText();
}
Widget* widget_;
Textfield* textfield_;
scoped_ptr<ViewsTouchSelectionControllerFactory> views_tsc_factory_;
private:
DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImplTest);
};
#define VERIFY_HANDLE_POSITIONS(cursor_at_selection_handle_1) \
{ \
gfx::SelectionModel sel = textfield_->GetSelectionModel(); \
if (textfield_->HasSelection()) { \
EXPECT_TRUE(IsSelectionHandle1Visible()); \
EXPECT_TRUE(IsSelectionHandle2Visible()); \
EXPECT_FALSE(IsCursorHandleVisible()); \
gfx::SelectionModel sel_start = GetRenderText()-> \
GetSelectionModelForSelectionStart(); \
gfx::Point selection_start = GetCursorPosition(sel_start); \
gfx::Point selection_end = GetCursorPosition(sel); \
gfx::Point sh1 = GetSelectionHandle1Position(); \
gfx::Point sh2 = GetSelectionHandle2Position(); \
sh1.Offset(GetHandleImageSize().width() / 2 + kPadding, 0); \
sh2.Offset(GetHandleImageSize().width() / 2 + kPadding, 0); \
if (cursor_at_selection_handle_1) { \
EXPECT_EQ(sh1, selection_end); \
EXPECT_EQ(sh2, selection_start); \
} else { \
EXPECT_EQ(sh1, selection_start); \
EXPECT_EQ(sh2, selection_end); \
} \
} else { \
EXPECT_FALSE(IsSelectionHandle1Visible()); \
EXPECT_FALSE(IsSelectionHandle2Visible()); \
EXPECT_TRUE(IsCursorHandleVisible()); \
gfx::Point cursor_pos = GetCursorPosition(sel); \
gfx::Point ch_pos = GetCursorHandlePosition(); \
ch_pos.Offset(GetHandleImageSize().width() / 2 + kPadding, 0); \
EXPECT_EQ(ch_pos, cursor_pos); \
} \
}
TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) {
CreateTextfield();
textfield_->SetText(ASCIIToUTF16("some text"));
ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(),
ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0);
textfield_->OnGestureEvent(&tap);
textfield_->SelectRange(gfx::Range(3, 7));
VERIFY_HANDLE_POSITIONS(false);
textfield_->SelectAll(false);
VERIFY_HANDLE_POSITIONS(false);
textfield_->ClearSelection();
VERIFY_HANDLE_POSITIONS(false);
widget_->GetFocusManager()->ClearFocus();
EXPECT_FALSE(GetSelectionController());
widget_->GetFocusManager()->SetFocusedView(textfield_);
EXPECT_FALSE(GetSelectionController());
textfield_->OnGestureEvent(&tap);
VERIFY_HANDLE_POSITIONS(false);
}
TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) {
CreateTextfield();
textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(),
ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0);
textfield_->OnGestureEvent(&tap);
textfield_->SelectSelectionModel(
gfx::SelectionModel(3, gfx::CURSOR_BACKWARD));
VERIFY_HANDLE_POSITIONS(false);
textfield_->SelectRange(gfx::Range(2, 3));
VERIFY_HANDLE_POSITIONS(false);
textfield_->SelectRange(gfx::Range(3, 2));
VERIFY_HANDLE_POSITIONS(false);
textfield_->SelectRange(gfx::Range(3, 4));
VERIFY_HANDLE_POSITIONS(false);
textfield_->SelectRange(gfx::Range(4, 3));
VERIFY_HANDLE_POSITIONS(false);
textfield_->SelectRange(gfx::Range(3, 6));
VERIFY_HANDLE_POSITIONS(false);
textfield_->SelectRange(gfx::Range(6, 3));
VERIFY_HANDLE_POSITIONS(false);
textfield_->SelectRange(gfx::Range(0, 6));
VERIFY_HANDLE_POSITIONS(false);
textfield_->SelectRange(gfx::Range(6, 0));
VERIFY_HANDLE_POSITIONS(false);
textfield_->SelectRange(gfx::Range(1, 4));
VERIFY_HANDLE_POSITIONS(false);
textfield_->SelectRange(gfx::Range(4, 1));
VERIFY_HANDLE_POSITIONS(false);
}
TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) {
CreateTextfield();
textfield_->SetText(ASCIIToUTF16("textfield with selected text"));
ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(),
ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0);
textfield_->OnGestureEvent(&tap);
textfield_->SelectRange(gfx::Range(3, 7));
EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfie");
VERIFY_HANDLE_POSITIONS(false);
const gfx::FontList& font_list = textfield_->GetFontList();
int x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("ld "), font_list);
SimulateSelectionHandleDrag(gfx::Point(x, 0), 2);
EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfield ");
VERIFY_HANDLE_POSITIONS(false);
SimulateSelectionHandleDrag(gfx::Point(-50, 0), 1);
EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "textfield ");
VERIFY_HANDLE_POSITIONS(true);
x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("textfield with "), font_list);
SimulateSelectionHandleDrag(gfx::Point(x, 0), 1);
EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "with ");
VERIFY_HANDLE_POSITIONS(true);
x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("with selected "), font_list);
SimulateSelectionHandleDrag(gfx::Point(x, 0), 2);
EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "selected ");
VERIFY_HANDLE_POSITIONS(false);
}
TEST_F(TouchSelectionControllerImplTest, SelectRectInBidiCallbackTest) {
CreateTextfield();
textfield_->SetText(WideToUTF16(L"abc\x05e1\x05e2\x05e3" L"def"));
ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(),
ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0);
textfield_->OnGestureEvent(&tap);
textfield_->SelectRange(gfx::Range(2, 3));
EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(false);
const gfx::FontList& font_list = textfield_->GetFontList();
int x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list);
SimulateSelectionHandleDrag(gfx::Point(x, 0), 2);
EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(false);
x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list);
SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1);
EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(true);
textfield_->SelectRange(gfx::Range(3, 2));
EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(false);
x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list);
SimulateSelectionHandleDrag(gfx::Point(x, 0), 1);
EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(true);
x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list);
SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2);
EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(false);
textfield_->SelectRange(gfx::Range(3, 4));
EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(false);
x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list);
SimulateSelectionHandleDrag(gfx::Point(x, 0), 1);
EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(true);
textfield_->SelectRange(gfx::Range(4, 3));
EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(false);
x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list);
SimulateSelectionHandleDrag(gfx::Point(x, 0), 2);
EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(false);
textfield_->SelectRange(gfx::Range(5, 6));
EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(false);
x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list);
SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2);
EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(false);
x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
SimulateSelectionHandleDrag(gfx::Point(x, 0), 1);
EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(true);
textfield_->SelectRange(gfx::Range(6, 5));
EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(false);
x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list);
SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1);
EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(true);
x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
SimulateSelectionHandleDrag(gfx::Point(x, 0), 2);
EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText());
VERIFY_HANDLE_POSITIONS(false);
}
TEST_F(TouchSelectionControllerImplTest,
HiddenSelectionHandleRetainsCursorPosition) {
CreateTextfield();
std::string textfield_text("some text");
for (int i = 0; i < 10; ++i)
textfield_text += textfield_text;
textfield_->SetText(ASCIIToUTF16(textfield_text));
ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(),
ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0);
textfield_->OnGestureEvent(&tap);
textfield_->SelectRange(gfx::Range(10, textfield_text.length()));
EXPECT_FALSE(IsSelectionHandle1Visible());
EXPECT_TRUE(IsSelectionHandle2Visible());
EXPECT_EQ(gfx::Range(10, textfield_text.length()),
textfield_->GetSelectedRange());
size_t visible_handle_position = textfield_->GetSelectedRange().end();
for (int i = 0; i < 10; ++i) {
SimulateSelectionHandleDrag(gfx::Point(-10, 0), 2);
EXPECT_NE(visible_handle_position, textfield_->GetSelectedRange().end());
visible_handle_position = textfield_->GetSelectedRange().end();
EXPECT_EQ((size_t) 10, textfield_->GetSelectedRange().start());
}
}
TEST_F(TouchSelectionControllerImplTest,
DoubleTapInTextfieldWithCursorHandleShouldSelectWord) {
CreateTextfield();
textfield_->SetText(ASCIIToUTF16("some text"));
aura::test::EventGenerator generator(
textfield_->GetWidget()->GetNativeView()->GetRootWindow());
generator.GestureTapAt(gfx::Point(10, 10));
EXPECT_FALSE(textfield_->HasSelection());
VERIFY_HANDLE_POSITIONS(false);
gfx::Point cursor_pos = GetCursorHandlePosition();
cursor_pos.Offset(GetHandleImageSize().width() / 2 + kPadding, 0);
generator.GestureTapAt(cursor_pos);
generator.GestureTapAt(cursor_pos);
EXPECT_TRUE(textfield_->HasSelection());
VERIFY_HANDLE_POSITIONS(false);
}
}