summaryrefslogtreecommitdiffstats
path: root/ui/base
diff options
context:
space:
mode:
authornona@chromium.org <nona@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-07 12:01:27 +0000
committernona@chromium.org <nona@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-07 12:01:27 +0000
commit7fd94ade101b26e2c6853d87eac5f3cca503d4a6 (patch)
tree6dd73bfa60da8a7958eae3b8712ffcdd325c0a05 /ui/base
parent3cf81cb978164c0d417b61170b0565effd1e3305 (diff)
downloadchromium_src-7fd94ade101b26e2c6853d87eac5f3cca503d4a6.zip
chromium_src-7fd94ade101b26e2c6853d87eac5f3cca503d4a6.tar.gz
chromium_src-7fd94ade101b26e2c6853d87eac5f3cca503d4a6.tar.bz2
Support surrounding text feature.
Some Thai input requires surrounding text feature. This patch implement full-spec surrounding text feature for ibus. BUG=158111 TEST=Ran ui_unittests and manually done on lumpy. Review URL: https://chromiumcodereview.appspot.com/11275148 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@166383 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base')
-rw-r--r--ui/base/ime/input_method_ibus.cc29
-rw-r--r--ui/base/ime/input_method_ibus.h3
-rw-r--r--ui/base/ime/input_method_ibus_unittest.cc105
3 files changed, 117 insertions, 20 deletions
diff --git a/ui/base/ime/input_method_ibus.cc b/ui/base/ime/input_method_ibus.cc
index c7e8325..7f71eba 100644
--- a/ui/base/ime/input_method_ibus.cc
+++ b/ui/base/ime/input_method_ibus.cc
@@ -228,32 +228,31 @@ void InputMethodIBus::OnCaretBoundsChanged(const TextInputClient* client) {
// This function runs asynchronously.
ibus_client_->SetCursorLocation(rect, composition_head);
+ ui::Range text_range;
ui::Range selection_range;
- if (!GetTextInputClient()->GetSelectionRange(&selection_range)) {
- previous_selected_text_.clear();
+ string16 surrounding_text;
+ if (!GetTextInputClient()->GetTextRange(&text_range) ||
+ !GetTextInputClient()->GetTextFromRange(text_range, &surrounding_text) ||
+ !GetTextInputClient()->GetSelectionRange(&selection_range)) {
+ previous_surrounding_text_.clear();
+ previous_selection_range_ = ui::Range::InvalidRange();
return;
}
- string16 selection_text;
- if (!GetTextInputClient()->GetTextFromRange(selection_range,
- &selection_text)) {
- previous_selected_text_.clear();
- return;
- }
-
- if (previous_selected_text_ == selection_text)
+ if (previous_selection_range_ == selection_range &&
+ previous_surrounding_text_ == surrounding_text)
return;
- previous_selected_text_ = selection_text;
+ previous_selection_range_ = selection_range;
+ previous_surrounding_text_ = surrounding_text;
// In the original meaning of SetSurroundingText is not just selection text,
// but currently there are no way to retrieve surrounding text in
// TextInputClient.
- // TODO(nona): Implement fully surrounding text retrieval.
GetInputContextClient()->SetSurroundingText(
- UTF16ToUTF8(selection_text),
- 0UL, /* cursor position. */
- selection_range.length()); /* selection anchor position. */
+ UTF16ToUTF8(surrounding_text),
+ selection_range.start(), /* cursor position. */
+ selection_range.end()); /* selection anchor position. */
}
void InputMethodIBus::CancelComposition(const TextInputClient* client) {
diff --git a/ui/base/ime/input_method_ibus.h b/ui/base/ime/input_method_ibus.h
index c708995..0e4286e 100644
--- a/ui/base/ime/input_method_ibus.h
+++ b/ui/base/ime/input_method_ibus.h
@@ -200,7 +200,8 @@ class UI_EXPORT InputMethodIBus : public InputMethodBase {
// processing result of the pending key event.
string16 result_text_;
- string16 previous_selected_text_;
+ string16 previous_surrounding_text_;
+ ui::Range previous_selection_range_;
// Indicates if input context is focused or not.
bool context_focused_;
diff --git a/ui/base/ime/input_method_ibus_unittest.cc b/ui/base/ime/input_method_ibus_unittest.cc
index c356b30..1cacf8b3 100644
--- a/ui/base/ime/input_method_ibus_unittest.cc
+++ b/ui/base/ime/input_method_ibus_unittest.cc
@@ -291,6 +291,31 @@ class AsynchronousKeyEventHandler {
DISALLOW_COPY_AND_ASSIGN(AsynchronousKeyEventHandler);
};
+class SetSurroundingTextVerifier {
+ public:
+ SetSurroundingTextVerifier(const std::string& expected_surrounding_text,
+ uint32 expected_cursor_position,
+ uint32 expected_anchor_position)
+ : expected_surrounding_text_(expected_surrounding_text),
+ expected_cursor_position_(expected_cursor_position),
+ expected_anchor_position_(expected_anchor_position) {}
+
+ void Verify(const std::string& text,
+ uint32 cursor_pos,
+ uint32 anchor_pos) {
+ EXPECT_EQ(expected_surrounding_text_, text);
+ EXPECT_EQ(expected_cursor_position_, cursor_pos);
+ EXPECT_EQ(expected_anchor_position_, anchor_pos);
+ }
+
+ private:
+ const std::string expected_surrounding_text_;
+ const uint32 expected_cursor_position_;
+ const uint32 expected_anchor_position_;
+
+ DISALLOW_COPY_AND_ASSIGN(SetSurroundingTextVerifier);
+};
+
class InputMethodIBusTest : public internal::InputMethodDelegate,
public testing::Test,
public TextInputClient {
@@ -377,13 +402,22 @@ class InputMethodIBusTest : public internal::InputMethodDelegate,
CompositionText empty;
return composition_text_ != empty;
}
- virtual bool GetTextRange(Range* range) OVERRIDE { return false; }
+ virtual bool GetTextRange(Range* range) OVERRIDE {
+ *range = text_range_;
+ return true;
+ }
virtual bool GetCompositionTextRange(Range* range) OVERRIDE { return false; }
- virtual bool GetSelectionRange(Range* range) OVERRIDE { return false; }
+ virtual bool GetSelectionRange(Range* range) OVERRIDE {
+ *range = selection_range_;
+ return true;
+ }
+
virtual bool SetSelectionRange(const Range& range) OVERRIDE { return false; }
virtual bool DeleteRange(const Range& range) OVERRIDE { return false; }
- virtual bool GetTextFromRange(const Range& range,
- string16* text) OVERRIDE { return false; }
+ virtual bool GetTextFromRange(const Range& range, string16* text) OVERRIDE {
+ *text = surrounding_text_.substr(range.GetMin(), range.length());
+ return true;
+ }
virtual void OnInputMethodChanged() OVERRIDE {
++on_input_method_changed_call_count_;
}
@@ -449,6 +483,9 @@ class InputMethodIBusTest : public internal::InputMethodDelegate,
TextInputType input_type_;
bool can_compose_inline_;
gfx::Rect caret_bounds_;
+ ui::Range text_range_;
+ ui::Range selection_range_;
+ string16 surrounding_text_;
// Variables for mock dbus connections.
chromeos::MockDBusThreadManagerWithoutGMock* mock_dbus_thread_manager_;
@@ -1068,6 +1105,66 @@ TEST_F(InputMethodIBusTest, ExtractCompositionTextTest_SelectionEndWithCursor) {
EXPECT_TRUE(composition_text.underlines[0].thick);
}
+TEST_F(InputMethodIBusTest, SurroundingText_NoSelectionTest) {
+ SetCreateContextSuccessHandler();
+ ime_->Init(true);
+ // Click a text input form.
+ input_type_ = TEXT_INPUT_TYPE_TEXT;
+ ime_->OnTextInputTypeChanged(this);
+ // Start the daemon.
+ chromeos::DBusThreadManager::Get()->InitIBusBus("dummy address");
+ ime_->OnConnected();
+
+ // Set the TextInputClient behaviors.
+ surrounding_text_ = UTF8ToUTF16("abcdef");
+ text_range_ = ui::Range(0, 6);
+ selection_range_ = ui::Range(3, 3);
+
+ // Set the verifier for SetSurroundingText mock call.
+ SetSurroundingTextVerifier verifier(UTF16ToUTF8(surrounding_text_),
+ selection_range_.start(),
+ selection_range_.end());
+
+ mock_ibus_input_context_client_->set_set_surrounding_text_handler(
+ base::Bind(&SetSurroundingTextVerifier::Verify,
+ base::Unretained(&verifier)));
+ ime_->OnCaretBoundsChanged(this);
+
+ // Check the call count.
+ EXPECT_EQ(1,
+ mock_ibus_input_context_client_->set_surrounding_text_call_count());
+}
+
+TEST_F(InputMethodIBusTest, SurroundingText_SelectionTest) {
+ SetCreateContextSuccessHandler();
+ ime_->Init(true);
+ // Click a text input form.
+ input_type_ = TEXT_INPUT_TYPE_TEXT;
+ ime_->OnTextInputTypeChanged(this);
+ // Start the daemon.
+ chromeos::DBusThreadManager::Get()->InitIBusBus("dummy address");
+ ime_->OnConnected();
+
+ // Set the TextInputClient behaviors.
+ surrounding_text_ = UTF8ToUTF16("abcdef");
+ text_range_ = ui::Range(0, 6);
+ selection_range_ = ui::Range(2, 5);
+
+ // Set the verifier for SetSurroundingText mock call.
+ SetSurroundingTextVerifier verifier(UTF16ToUTF8(surrounding_text_),
+ selection_range_.start(),
+ selection_range_.end());
+
+ mock_ibus_input_context_client_->set_set_surrounding_text_handler(
+ base::Bind(&SetSurroundingTextVerifier::Verify,
+ base::Unretained(&verifier)));
+ ime_->OnCaretBoundsChanged(this);
+
+ // Check the call count.
+ EXPECT_EQ(1,
+ mock_ibus_input_context_client_->set_surrounding_text_call_count());
+}
+
class InputMethodIBusKeyEventTest : public InputMethodIBusTest {
public:
InputMethodIBusKeyEventTest() {}