1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/ime/ash/mock_ime_input_context_handler.h"
#include <string_view>
#include "base/logging.h"
#include "base/notreached.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/base/ime/composition_text.h"
#include "ui/base/ime/input_method.h"
#include "ui/gfx/range/range.h"
namespace ash {
MockIMEInputContextHandler::MockIMEInputContextHandler()
: commit_text_call_count_(0),
update_preedit_text_call_count_(0),
delete_surrounding_text_call_count_(0) {}
MockIMEInputContextHandler::~MockIMEInputContextHandler() = default;
void MockIMEInputContextHandler::CommitText(
const std::u16string& text,
ui::TextInputClient::InsertTextCursorBehavior cursor_behavior) {
++commit_text_call_count_;
last_commit_text_ = text;
observers_.Notify(&Observer::OnCommitText, text);
}
void MockIMEInputContextHandler::UpdateCompositionText(
const ui::CompositionText& text,
uint32_t cursor_pos,
bool visible) {
++update_preedit_text_call_count_;
last_update_composition_arg_.composition_text = text;
last_update_composition_arg_.selection = gfx::Range(cursor_pos);
last_update_composition_arg_.is_visible = visible;
}
bool MockIMEInputContextHandler::SetCompositionRange(
uint32_t before,
uint32_t after,
const std::vector<ui::ImeTextSpan>& text_spans) {
// TODO(shend): Make this work with before, after and different text contents.
last_update_composition_arg_.composition_text.text = last_commit_text_;
return true;
}
bool MockIMEInputContextHandler::SetComposingRange(
uint32_t start,
uint32_t end,
const std::vector<ui::ImeTextSpan>& text_spans) {
// TODO(shend): Make this work with start, end and different text contents.
last_update_composition_arg_.composition_text.text = last_commit_text_;
return true;
}
gfx::Range MockIMEInputContextHandler::GetAutocorrectRange() {
return autocorrect_range_;
}
void MockIMEInputContextHandler::SetAutocorrectRange(
const gfx::Range& range,
SetAutocorrectRangeDoneCallback callback) {
if (autocorrect_enabled_) {
autocorrect_range_ = range;
}
std::move(callback).Run(autocorrect_enabled_);
}
std::optional<ui::GrammarFragment>
MockIMEInputContextHandler::GetGrammarFragmentAtCursor() {
for (const auto& fragment : grammar_fragments_) {
if (fragment.range.Contains(cursor_range_)) {
return fragment;
}
}
return std::nullopt;
}
bool MockIMEInputContextHandler::ClearGrammarFragments(
const gfx::Range& range) {
std::vector<ui::GrammarFragment> updated_fragments;
for (const ui::GrammarFragment& fragment : grammar_fragments_) {
if (!range.Contains(fragment.range)) {
updated_fragments.push_back(fragment);
}
}
grammar_fragments_ = updated_fragments;
return true;
}
bool MockIMEInputContextHandler::AddGrammarFragments(
const std::vector<ui::GrammarFragment>& fragments) {
grammar_fragments_.insert(grammar_fragments_.end(), fragments.begin(),
fragments.end());
return true;
}
void MockIMEInputContextHandler::DeleteSurroundingText(
uint32_t num_char16s_before_cursor,
uint32_t num_char16s_after_cursor) {
++delete_surrounding_text_call_count_;
last_delete_surrounding_text_arg_.num_char16s_before_cursor =
num_char16s_before_cursor;
last_delete_surrounding_text_arg_.num_char16s_after_cursor =
num_char16s_after_cursor;
}
void MockIMEInputContextHandler::ReplaceSurroundingText(
uint32_t length_before_selection,
uint32_t length_after_selection,
const std::u16string_view replacement_text) {
last_replace_surrounding_text_arg_.length_before_selection =
length_before_selection;
last_replace_surrounding_text_arg_.length_after_selection =
length_after_selection;
last_replace_surrounding_text_arg_.replacement_text =
std::u16string(replacement_text);
}
SurroundingTextInfo MockIMEInputContextHandler::GetSurroundingTextInfo() {
SurroundingTextInfo info;
info.selection_range = cursor_range_;
info.offset = 0;
return info;
}
void MockIMEInputContextHandler::Reset() {
commit_text_call_count_ = 0;
update_preedit_text_call_count_ = 0;
delete_surrounding_text_call_count_ = 0;
autocorrect_enabled_ = true;
last_commit_text_.clear();
sent_key_events_.clear();
}
void MockIMEInputContextHandler::SendKeyEvent(ui::KeyEvent* event) {
sent_key_events_.emplace_back(*event);
}
ui::InputMethod* MockIMEInputContextHandler::GetInputMethod() {
return nullptr;
}
void MockIMEInputContextHandler::ConfirmComposition(bool reset_engine) {
// TODO(b/134473433) Modify this function so that the selection is unchanged.
NOTIMPLEMENTED_LOG_ONCE();
if (!HasCompositionText()) {
return;
}
CommitText(
last_update_composition_arg_.composition_text.text,
ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
last_update_composition_arg_.composition_text.text = std::u16string();
}
bool MockIMEInputContextHandler::HasCompositionText() {
return !last_update_composition_arg_.composition_text.text.empty();
}
ukm::SourceId MockIMEInputContextHandler::GetClientSourceForMetrics() {
return ukm::kInvalidSourceId;
}
void MockIMEInputContextHandler::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void MockIMEInputContextHandler::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
} // namespace ash
|