Commit 3fe20a7b authored by Keith Lee's avatar Keith Lee Committed by Commit Bot

Remove downcasting to TextInputClient in textfield_unittests for composition and autocorrect

Applying comments in https://chromium-review.googlesource.com/c/chromium/src/+/2327773.

Bug: 1108170
Change-Id: I3a528db4f4a7df288af0a5b02d758f5d9616f011
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2355657Reviewed-by: default avatarMitsuru Oshima <oshima@chromium.org>
Commit-Queue: Keith Lee <keithlee@chromium.org>
Cr-Commit-Position: refs/heads/master@{#798537}
parent d8fada70
...@@ -2984,10 +2984,9 @@ TEST_F(TextfieldTest, CommitComposingTextTest) { ...@@ -2984,10 +2984,9 @@ TEST_F(TextfieldTest, CommitComposingTextTest) {
InitTextfield(); InitTextfield();
ui::CompositionText composition; ui::CompositionText composition;
composition.text = UTF8ToUTF16("abc123"); composition.text = UTF8ToUTF16("abc123");
ui::TextInputClient* client = textfield_; textfield_->SetCompositionText(composition);
client->SetCompositionText(composition);
uint32_t composed_text_length = uint32_t composed_text_length =
client->ConfirmCompositionText(/* keep_selection */ false); textfield_->ConfirmCompositionText(/* keep_selection */ false);
EXPECT_EQ(composed_text_length, static_cast<uint32_t>(6)); EXPECT_EQ(composed_text_length, static_cast<uint32_t>(6));
} }
...@@ -2996,10 +2995,9 @@ TEST_F(TextfieldTest, CommitEmptyComposingTextTest) { ...@@ -2996,10 +2995,9 @@ TEST_F(TextfieldTest, CommitEmptyComposingTextTest) {
InitTextfield(); InitTextfield();
ui::CompositionText composition; ui::CompositionText composition;
composition.text = UTF8ToUTF16(""); composition.text = UTF8ToUTF16("");
ui::TextInputClient* client = textfield_; textfield_->SetCompositionText(composition);
client->SetCompositionText(composition);
uint32_t composed_text_length = uint32_t composed_text_length =
client->ConfirmCompositionText(/* keep_selection */ false); textfield_->ConfirmCompositionText(/* keep_selection */ false);
EXPECT_EQ(composed_text_length, static_cast<uint32_t>(0)); EXPECT_EQ(composed_text_length, static_cast<uint32_t>(0));
} }
...@@ -3009,31 +3007,32 @@ TEST_F(TextfieldTest, GetCompositionCharacterBoundsTest) { ...@@ -3009,31 +3007,32 @@ TEST_F(TextfieldTest, GetCompositionCharacterBoundsTest) {
ui::CompositionText composition; ui::CompositionText composition;
composition.text = UTF8ToUTF16("abc123"); composition.text = UTF8ToUTF16("abc123");
const uint32_t char_count = static_cast<uint32_t>(composition.text.length()); const uint32_t char_count = static_cast<uint32_t>(composition.text.length());
ui::TextInputClient* client = textfield_;
// Compare the composition character bounds with surrounding cursor bounds. // Compare the composition character bounds with surrounding cursor bounds.
for (uint32_t i = 0; i < char_count; ++i) { for (uint32_t i = 0; i < char_count; ++i) {
composition.selection = gfx::Range(i); composition.selection = gfx::Range(i);
client->SetCompositionText(composition); textfield_->SetCompositionText(composition);
gfx::Point cursor_origin = GetCursorBounds().origin(); gfx::Point cursor_origin = GetCursorBounds().origin();
views::View::ConvertPointToScreen(textfield_, &cursor_origin); views::View::ConvertPointToScreen(textfield_, &cursor_origin);
composition.selection = gfx::Range(i + 1); composition.selection = gfx::Range(i + 1);
client->SetCompositionText(composition); textfield_->SetCompositionText(composition);
gfx::Point next_cursor_bottom_left = GetCursorBounds().bottom_left(); gfx::Point next_cursor_bottom_left = GetCursorBounds().bottom_left();
views::View::ConvertPointToScreen(textfield_, &next_cursor_bottom_left); views::View::ConvertPointToScreen(textfield_, &next_cursor_bottom_left);
gfx::Rect character; gfx::Rect character;
EXPECT_TRUE(client->GetCompositionCharacterBounds(i, &character)); EXPECT_TRUE(textfield_->GetCompositionCharacterBounds(i, &character));
EXPECT_EQ(character.origin(), cursor_origin) << " i=" << i; EXPECT_EQ(character.origin(), cursor_origin) << " i=" << i;
EXPECT_EQ(character.bottom_right(), next_cursor_bottom_left) << " i=" << i; EXPECT_EQ(character.bottom_right(), next_cursor_bottom_left) << " i=" << i;
} }
// Return false if the index is out of range. // Return false if the index is out of range.
gfx::Rect rect; gfx::Rect rect;
EXPECT_FALSE(client->GetCompositionCharacterBounds(char_count, &rect)); EXPECT_FALSE(textfield_->GetCompositionCharacterBounds(char_count, &rect));
EXPECT_FALSE(client->GetCompositionCharacterBounds(char_count + 1, &rect)); EXPECT_FALSE(
EXPECT_FALSE(client->GetCompositionCharacterBounds(char_count + 100, &rect)); textfield_->GetCompositionCharacterBounds(char_count + 1, &rect));
EXPECT_FALSE(
textfield_->GetCompositionCharacterBounds(char_count + 100, &rect));
} }
TEST_F(TextfieldTest, GetCompositionCharacterBounds_ComplexText) { TEST_F(TextfieldTest, GetCompositionCharacterBounds_ComplexText) {
...@@ -3059,13 +3058,12 @@ TEST_F(TextfieldTest, GetCompositionCharacterBounds_ComplexText) { ...@@ -3059,13 +3058,12 @@ TEST_F(TextfieldTest, GetCompositionCharacterBounds_ComplexText) {
ui::CompositionText composition; ui::CompositionText composition;
composition.text.assign(kUtf16Chars, kUtf16Chars + kUtf16CharsCount); composition.text.assign(kUtf16Chars, kUtf16Chars + kUtf16CharsCount);
ui::TextInputClient* client = textfield_; textfield_->SetCompositionText(composition);
client->SetCompositionText(composition);
// Make sure GetCompositionCharacterBounds never fails for index. // Make sure GetCompositionCharacterBounds never fails for index.
gfx::Rect rects[kUtf16CharsCount]; gfx::Rect rects[kUtf16CharsCount];
for (uint32_t i = 0; i < kUtf16CharsCount; ++i) for (uint32_t i = 0; i < kUtf16CharsCount; ++i)
EXPECT_TRUE(client->GetCompositionCharacterBounds(i, &rects[i])); EXPECT_TRUE(textfield_->GetCompositionCharacterBounds(i, &rects[i]));
// Here we might expect the following results but it actually depends on how // Here we might expect the following results but it actually depends on how
// Uniscribe or HarfBuzz treats them with given font. // Uniscribe or HarfBuzz treats them with given font.
...@@ -3077,90 +3075,85 @@ TEST_F(TextfieldTest, GetCompositionCharacterBounds_ComplexText) { ...@@ -3077,90 +3075,85 @@ TEST_F(TextfieldTest, GetCompositionCharacterBounds_ComplexText) {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
TEST_F(TextfieldTest, SetAutocorrectRangeText) { TEST_F(TextfieldTest, SetAutocorrectRangeText) {
InitTextfield(); InitTextfield();
ui::TextInputClient* client = textfield_;
ui::CompositionText composition; ui::CompositionText composition;
composition.text = UTF8ToUTF16("Initial txt"); composition.text = UTF8ToUTF16("Initial txt");
client->SetCompositionText(composition); textfield_->SetCompositionText(composition);
client->SetAutocorrectRange(ASCIIToUTF16("text replacement"), textfield_->SetAutocorrectRange(ASCIIToUTF16("text replacement"),
gfx::Range(8, 11)); gfx::Range(8, 11));
gfx::Range autocorrect_range = client->GetAutocorrectRange(); gfx::Range autocorrect_range = textfield_->GetAutocorrectRange();
EXPECT_EQ(autocorrect_range, gfx::Range(8, 24)); EXPECT_EQ(autocorrect_range, gfx::Range(8, 24));
base::string16 text; base::string16 text;
client->GetTextFromRange(gfx::Range(0, 24), &text); textfield_->GetTextFromRange(gfx::Range(0, 24), &text);
EXPECT_EQ(text, UTF8ToUTF16("Initial text replacement")); EXPECT_EQ(text, UTF8ToUTF16("Initial text replacement"));
} }
TEST_F(TextfieldTest, SetAutocorrectRangeExplicitlySet) { TEST_F(TextfieldTest, SetAutocorrectRangeExplicitlySet) {
InitTextfield(); InitTextfield();
ui::TextInputClient* client = textfield_; textfield_->InsertText(UTF8ToUTF16("Initial txt"));
client->InsertText(UTF8ToUTF16("Initial txt")); textfield_->SetAutocorrectRange(ASCIIToUTF16("text replacement"),
client->SetAutocorrectRange(ASCIIToUTF16("text replacement"),
gfx::Range(8, 11)); gfx::Range(8, 11));
gfx::Range autocorrectRange = client->GetAutocorrectRange(); gfx::Range autocorrectRange = textfield_->GetAutocorrectRange();
EXPECT_EQ(autocorrectRange, gfx::Range(8, 24)); EXPECT_EQ(autocorrectRange, gfx::Range(8, 24));
base::string16 text; base::string16 text;
client->GetTextFromRange(gfx::Range(0, 24), &text); textfield_->GetTextFromRange(gfx::Range(0, 24), &text);
EXPECT_EQ(text, UTF8ToUTF16("Initial text replacement")); EXPECT_EQ(text, UTF8ToUTF16("Initial text replacement"));
} }
TEST_F(TextfieldTest, DoesNotSetAutocorrectRangeWhenRangeGivenIsInvalid) { TEST_F(TextfieldTest, DoesNotSetAutocorrectRangeWhenRangeGivenIsInvalid) {
InitTextfield(); InitTextfield();
ui::TextInputClient* client = textfield_;
ui::CompositionText composition; ui::CompositionText composition;
composition.text = UTF8ToUTF16("Initial"); composition.text = UTF8ToUTF16("Initial");
client->SetCompositionText(composition); textfield_->SetCompositionText(composition);
EXPECT_FALSE(client->SetAutocorrectRange(ASCIIToUTF16("text replacement"), EXPECT_FALSE(textfield_->SetAutocorrectRange(ASCIIToUTF16("text replacement"),
gfx::Range(8, 11))); gfx::Range(8, 11)));
EXPECT_EQ(gfx::Range(0, 0), client->GetAutocorrectRange()); EXPECT_EQ(gfx::Range(0, 0), textfield_->GetAutocorrectRange());
gfx::Range range; gfx::Range range;
client->GetTextRange(&range); textfield_->GetTextRange(&range);
base::string16 text; base::string16 text;
client->GetTextFromRange(range, &text); textfield_->GetTextFromRange(range, &text);
EXPECT_EQ(composition.text, text); EXPECT_EQ(composition.text, text);
} }
TEST_F(TextfieldTest, TEST_F(TextfieldTest,
ClearsAutocorrectRangeWhenSetAutocorrectRangeWithEmptyText) { ClearsAutocorrectRangeWhenSetAutocorrectRangeWithEmptyText) {
InitTextfield(); InitTextfield();
ui::TextInputClient* client = textfield_;
ui::CompositionText composition; ui::CompositionText composition;
composition.text = UTF8ToUTF16("Initial"); composition.text = UTF8ToUTF16("Initial");
client->SetCompositionText(composition); textfield_->SetCompositionText(composition);
EXPECT_TRUE( EXPECT_TRUE(
client->SetAutocorrectRange(base::EmptyString16(), gfx::Range(0, 2))); textfield_->SetAutocorrectRange(base::EmptyString16(), gfx::Range(0, 2)));
EXPECT_EQ(gfx::Range(0, 0), client->GetAutocorrectRange()); EXPECT_EQ(gfx::Range(0, 0), textfield_->GetAutocorrectRange());
gfx::Range range; gfx::Range range;
client->GetTextRange(&range); textfield_->GetTextRange(&range);
base::string16 text; base::string16 text;
client->GetTextFromRange(range, &text); textfield_->GetTextFromRange(range, &text);
EXPECT_EQ(composition.text, text); EXPECT_EQ(composition.text, text);
} }
TEST_F(TextfieldTest, TEST_F(TextfieldTest,
ClearsAutocorrectRangeWhenSetAutocorrectRangeWithEmptyRange) { ClearsAutocorrectRangeWhenSetAutocorrectRangeWithEmptyRange) {
InitTextfield(); InitTextfield();
ui::TextInputClient* client = textfield_;
ui::CompositionText composition; ui::CompositionText composition;
composition.text = UTF8ToUTF16("Initial"); composition.text = UTF8ToUTF16("Initial");
client->SetCompositionText(composition); textfield_->SetCompositionText(composition);
EXPECT_TRUE( EXPECT_TRUE(
client->SetAutocorrectRange(UTF8ToUTF16("Test"), gfx::Range(0, 0))); textfield_->SetAutocorrectRange(UTF8ToUTF16("Test"), gfx::Range(0, 0)));
EXPECT_EQ(gfx::Range(0, 0), client->GetAutocorrectRange()); EXPECT_EQ(gfx::Range(0, 0), textfield_->GetAutocorrectRange());
gfx::Range range; gfx::Range range;
client->GetTextRange(&range); textfield_->GetTextRange(&range);
base::string16 text; base::string16 text;
client->GetTextFromRange(range, &text); textfield_->GetTextFromRange(range, &text);
EXPECT_EQ(composition.text, text); EXPECT_EQ(composition.text, text);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment