1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/base/ime/mock_ime_input_context_handler.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/base/ime/composition_text.h"
9 #include "ui/base/ime/input_method.h"
10 #include "ui/gfx/range/range.h"
11
12 namespace ui {
13
MockIMEInputContextHandler()14 MockIMEInputContextHandler::MockIMEInputContextHandler()
15 : commit_text_call_count_(0),
16 set_selection_range_call_count_(0),
17 update_preedit_text_call_count_(0),
18 delete_surrounding_text_call_count_(0),
19 last_sent_key_event_(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0) {}
20
~MockIMEInputContextHandler()21 MockIMEInputContextHandler::~MockIMEInputContextHandler() {}
22
CommitText(const std::string & text)23 void MockIMEInputContextHandler::CommitText(const std::string& text) {
24 ++commit_text_call_count_;
25 last_commit_text_ = text;
26 }
27
UpdateCompositionText(const CompositionText & text,uint32_t cursor_pos,bool visible)28 void MockIMEInputContextHandler::UpdateCompositionText(
29 const CompositionText& text,
30 uint32_t cursor_pos,
31 bool visible) {
32 ++update_preedit_text_call_count_;
33 last_update_composition_arg_.composition_text = text;
34 last_update_composition_arg_.selection = gfx::Range(cursor_pos);
35 last_update_composition_arg_.is_visible = visible;
36 }
37
38 #if defined(OS_CHROMEOS)
SetCompositionRange(uint32_t before,uint32_t after,const std::vector<ui::ImeTextSpan> & text_spans)39 bool MockIMEInputContextHandler::SetCompositionRange(
40 uint32_t before,
41 uint32_t after,
42 const std::vector<ui::ImeTextSpan>& text_spans) {
43 // TODO(shend): Make this work with before, after and different text contents.
44 last_update_composition_arg_.composition_text.text =
45 base::UTF8ToUTF16(last_commit_text_);
46 return true;
47 }
48
SetSelectionRange(uint32_t start,uint32_t end)49 bool MockIMEInputContextHandler::SetSelectionRange(uint32_t start,
50 uint32_t end) {
51 ++set_selection_range_call_count_;
52 last_update_composition_arg_.selection = gfx::Range(start, end);
53 return true;
54 }
55 #endif
56
DeleteSurroundingText(int32_t offset,uint32_t length)57 void MockIMEInputContextHandler::DeleteSurroundingText(int32_t offset,
58 uint32_t length) {
59 ++delete_surrounding_text_call_count_;
60 last_delete_surrounding_text_arg_.offset = offset;
61 last_delete_surrounding_text_arg_.length = length;
62 }
63
GetSurroundingTextInfo()64 SurroundingTextInfo MockIMEInputContextHandler::GetSurroundingTextInfo() {
65 return SurroundingTextInfo();
66 }
67
Reset()68 void MockIMEInputContextHandler::Reset() {
69 commit_text_call_count_ = 0;
70 set_selection_range_call_count_ = 0;
71 update_preedit_text_call_count_ = 0;
72 delete_surrounding_text_call_count_ = 0;
73 last_commit_text_.clear();
74 last_sent_key_event_ = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, 0);
75 }
76
SendKeyEvent(KeyEvent * event)77 void MockIMEInputContextHandler::SendKeyEvent(KeyEvent* event) {
78 last_sent_key_event_ = *event;
79 }
80
GetInputMethod()81 InputMethod* MockIMEInputContextHandler::GetInputMethod() {
82 return nullptr;
83 }
84
ConfirmCompositionText(bool reset_engine,bool keep_selection)85 void MockIMEInputContextHandler::ConfirmCompositionText(bool reset_engine,
86 bool keep_selection) {
87 // TODO(b/134473433) Modify this function so that when keep_selection is
88 // true, the selection is not changed when text committed
89 if (keep_selection) {
90 NOTIMPLEMENTED_LOG_ONCE();
91 }
92 if (!HasCompositionText())
93 return;
94
95 CommitText(
96 base::UTF16ToUTF8(last_update_composition_arg_.composition_text.text));
97 last_update_composition_arg_.composition_text.text = base::string16();
98 }
99
HasCompositionText()100 bool MockIMEInputContextHandler::HasCompositionText() {
101 return !last_update_composition_arg_.composition_text.text.empty();
102 }
103
104 } // namespace ui
105