Commit 73b410cf authored by My Nguyen's avatar My Nguyen Committed by Commit Bot

Add handling of Learn More button for Emoji Suggester

- Press "Enter", open settings when Learn More button is chosen.
- Press "Up" from first candidate/ no candidates chosen, highlight
Learn More button.
- Press "Down" from last candidate, highlight Learn More button.

Bug: 1101195
Change-Id: I7900cef2ea361f777b6d6586d396541f10234946
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2280751Reviewed-by: default avatarKeith Lee <keithlee@chromium.org>
Reviewed-by: default avatarJing Wang <jiwan@chromium.org>
Commit-Queue: My Nguyen <myy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#785291}
parent 398466e9
......@@ -16,6 +16,8 @@
#include "base/task/thread_pool.h"
#include "chrome/browser/ui/ash/keyboard/chrome_keyboard_controller_client.h"
#include "chromeos/services/ime/constants.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
namespace chromeos {
......@@ -76,6 +78,9 @@ EmojiSuggester::EmojiSuggester(SuggestionHandlerInterface* engine)
current_candidate_.id = ui::ime::ButtonId::kSuggestion;
current_candidate_.window_type =
ui::ime::AssistiveWindowType::kEmojiSuggestion;
learn_more_button_.id = ui::ime::ButtonId::kLearnMore;
learn_more_button_.window_type =
ui::ime::AssistiveWindowType::kEmojiSuggestion;
}
EmojiSuggester::~EmojiSuggester() = default;
......@@ -132,29 +137,45 @@ SuggestionStatus EmojiSuggester::HandleKeyEvent(
SuggestionStatus status = SuggestionStatus::kNotHandled;
std::string error;
if (event.key == "Enter") {
if (AcceptSuggestion(current_candidate_.index))
if (is_learn_more_button_chosen_) {
engine_->ClickButton(learn_more_button_);
status = SuggestionStatus::kOpenSettings;
} else if (AcceptSuggestion(current_candidate_.index)) {
status = SuggestionStatus::kAccept;
}
} else if (event.key == "Down") {
if (!properties_.show_indices) {
ShowSuggestionWindowWithIndices(true);
}
current_candidate_.index < candidates_.size() - 1
? current_candidate_.index++
: current_candidate_.index = 0;
BuildCandidateAnnounceString();
engine_->SetButtonHighlighted(context_id_, current_candidate_, true,
&error);
// If current_candidate.index is the last one, goes to learn_more_button.
if (current_candidate_.index == candidates_.size() - 1) {
SetLearnMoreButtonHighlighted(true);
} else {
current_candidate_.index < candidates_.size() - 1
? current_candidate_.index++
: current_candidate_.index = 0;
if (is_learn_more_button_chosen_) {
SetLearnMoreButtonHighlighted(false);
}
BuildCandidateAnnounceString();
engine_->SetButtonHighlighted(context_id_, current_candidate_, true,
&error);
}
status = SuggestionStatus::kBrowsing;
} else if (event.key == "Up") {
if (!properties_.show_indices) {
ShowSuggestionWindowWithIndices(true);
}
current_candidate_.index > 0 && current_candidate_.index != INT_MAX
? current_candidate_.index--
: current_candidate_.index = candidates_.size() - 1;
BuildCandidateAnnounceString();
engine_->SetButtonHighlighted(context_id_, current_candidate_, true,
&error);
// If current_candidate.index is the first one, goes to learn_more_button.
if (current_candidate_.index == 0 || (current_candidate_.index == INT_MAX &&
!is_learn_more_button_chosen_)) {
SetLearnMoreButtonHighlighted(true);
} else {
current_candidate_.index > 0 && current_candidate_.index != INT_MAX
? current_candidate_.index--
: current_candidate_.index = candidates_.size() - 1;
SetCandidateButtonHighlighted(true);
}
status = SuggestionStatus::kBrowsing;
} else if (event.key == "Esc") {
DismissSuggestion();
......@@ -241,6 +262,7 @@ void EmojiSuggester::ResetState() {
candidates_.clear();
current_candidate_.index = INT_MAX;
last_event_key_ = base::EmptyString();
is_learn_more_button_chosen_ = false;
}
void EmojiSuggester::BuildCandidateAnnounceString() {
......@@ -250,6 +272,40 @@ void EmojiSuggester::BuildCandidateAnnounceString() {
current_candidate_.index + 1, candidates_.size());
}
void EmojiSuggester::SetCandidateButtonHighlighted(bool highlighted) {
if (highlighted) {
if (is_learn_more_button_chosen_) {
SetLearnMoreButtonHighlighted(false);
}
BuildCandidateAnnounceString();
}
std::string error;
engine_->SetButtonHighlighted(context_id_, current_candidate_, highlighted,
&error);
if (!error.empty()) {
LOG(ERROR) << "Failed to set candidate button highlighted " << error;
}
}
void EmojiSuggester::SetLearnMoreButtonHighlighted(bool highlighted) {
if (highlighted && current_candidate_.index != INT_MAX) {
SetCandidateButtonHighlighted(false);
}
std::string error;
learn_more_button_.announce_string =
highlighted ? l10n_util::GetStringUTF8(IDS_LEARN_MORE)
: base::EmptyString();
engine_->SetButtonHighlighted(context_id_, learn_more_button_, highlighted,
&error);
if (!error.empty()) {
LOG(ERROR) << "Failed to set learn more button highlighted " << error;
} else {
is_learn_more_button_chosen_ = highlighted;
if (highlighted)
current_candidate_.index = INT_MAX;
}
}
AssistiveType EmojiSuggester::GetProposeActionType() {
return AssistiveType::kEmoji;
}
......@@ -258,4 +314,8 @@ bool EmojiSuggester::GetSuggestionShownForTesting() const {
return suggestion_shown_;
}
size_t EmojiSuggester::GetCandidatesSizeForTesting() const {
return candidates_.size();
}
} // namespace chromeos
......@@ -34,6 +34,7 @@ class EmojiSuggester : public Suggester {
void LoadEmojiMapForTesting(const std::string& emoji_data);
bool GetSuggestionShownForTesting() const;
size_t GetCandidatesSizeForTesting() const;
private:
void ShowSuggestion(const std::string& text);
......@@ -44,6 +45,9 @@ class EmojiSuggester : public Suggester {
void ResetState();
void BuildCandidateAnnounceString();
void SetCandidateButtonHighlighted(bool highlighted);
void SetLearnMoreButtonHighlighted(bool highlighted);
SuggestionHandlerInterface* const engine_;
// ID of the focused text field, 0 if none is focused.
......@@ -59,6 +63,8 @@ class EmojiSuggester : public Suggester {
AssistiveWindowProperties properties_;
ui::ime::AssistiveWindowButton current_candidate_;
ui::ime::AssistiveWindowButton learn_more_button_;
bool is_learn_more_button_chosen_ = false;
// The map holding one-word-mapping to emojis.
std::map<std::string, std::vector<base::string16>> emoji_map_;
......
......@@ -20,13 +20,31 @@ class TestSuggestionHandler : public SuggestionHandlerInterface {
const ui::ime::AssistiveWindowButton& button,
bool highlighted,
std::string* error) override {
return false;
switch (button.id) {
case ui::ime::ButtonId::kLearnMore:
learn_more_button_highlighted_ = highlighted;
return true;
case ui::ime::ButtonId::kSuggestion:
// If highlighted, needs to unhighlight previously highlighted button.
if (currently_highlighted_index_ != INT_MAX && highlighted) {
candidate_highlighted_[currently_highlighted_index_] = 0;
}
currently_highlighted_index_ = highlighted ? button.index : INT_MAX;
candidate_highlighted_[button.index] = highlighted ? 1 : 0;
return true;
default:
return false;
}
}
bool SetAssistiveWindowProperties(
int context_id,
const AssistiveWindowProperties& assistive_window,
std::string* error) override {
candidate_highlighted_.clear();
for (size_t i = 0; i < assistive_window.candidates.size(); i++) {
candidate_highlighted_.push_back(0);
}
show_indices_ = assistive_window.show_indices;
return true;
}
......@@ -35,6 +53,15 @@ class TestSuggestionHandler : public SuggestionHandlerInterface {
EXPECT_EQ(show_indices_, show_indices);
}
void VerifyLearnMoreButtonHighlighted(const bool highlighted) {
EXPECT_EQ(learn_more_button_highlighted_, highlighted);
}
void VerifyCandidateHighlighted(const int index, const bool highlighted) {
int expect = highlighted ? 1 : 0;
EXPECT_EQ(candidate_highlighted_[index], expect);
}
bool DismissSuggestion(int context_id, std::string* error) override {
return false;
}
......@@ -68,6 +95,9 @@ class TestSuggestionHandler : public SuggestionHandlerInterface {
private:
bool show_indices_ = false;
bool learn_more_button_highlighted_ = false;
std::vector<int> candidate_highlighted_;
size_t currently_highlighted_index_ = INT_MAX;
};
class EmojiSuggesterTest : public testing::Test {
......@@ -216,14 +246,74 @@ TEST_F(EmojiSuggesterTest,
TEST_F(EmojiSuggesterTest,
ReturnkAcceptWhenPressingEnterAndACandidateHasBeenChosenByPressingUp) {
EXPECT_TRUE(emoji_suggester_->Suggest(base::UTF8ToUTF16("happy ")));
// Press "Down" to choose a candidate.
InputMethodEngineBase::KeyboardEvent event1;
event1.key = "Up";
emoji_suggester_->HandleKeyEvent(event1);
InputMethodEngineBase::KeyboardEvent event2;
event2.key = "Enter";
EXPECT_EQ(SuggestionStatus::kAccept,
emoji_suggester_->HandleKeyEvent(event2));
// Press "Up" twice to choose last candidate.
Press("Up");
Press("Up");
EXPECT_EQ(SuggestionStatus::kAccept, Press("Enter"));
}
TEST_F(EmojiSuggesterTest, HighlightFirstCandidateWhenPressingDown) {
EXPECT_TRUE(emoji_suggester_->Suggest(base::UTF8ToUTF16("happy ")));
Press("Down");
engine_->VerifyCandidateHighlighted(0, true);
}
TEST_F(EmojiSuggesterTest, HighlightButtonCorrectlyWhenPressingUp) {
EXPECT_TRUE(emoji_suggester_->Suggest(base::UTF8ToUTF16("happy ")));
// Press "Up" to choose learn more button.
Press("Up");
engine_->VerifyLearnMoreButtonHighlighted(true);
// Press "Up" to go through candidates;
for (size_t i = emoji_suggester_->GetCandidatesSizeForTesting(); i > 0; i--) {
Press("Up");
engine_->VerifyCandidateHighlighted(i - 1, true);
engine_->VerifyLearnMoreButtonHighlighted(false);
if (i != emoji_suggester_->GetCandidatesSizeForTesting()) {
engine_->VerifyCandidateHighlighted(i, false);
}
}
// Press "Up" to go to learn more button from first candidate.
Press("Up");
engine_->VerifyLearnMoreButtonHighlighted(true);
}
TEST_F(EmojiSuggesterTest, HighlightButtonCorrectlyWhenPressingDown) {
EXPECT_TRUE(emoji_suggester_->Suggest(base::UTF8ToUTF16("happy ")));
// Press "Down" to go through candidates.
for (size_t i = 0; i < emoji_suggester_->GetCandidatesSizeForTesting(); i++) {
Press("Down");
engine_->VerifyCandidateHighlighted(i, true);
engine_->VerifyLearnMoreButtonHighlighted(false);
if (i != 0) {
engine_->VerifyCandidateHighlighted(i - 1, false);
}
}
// Go to LearnMore Button
Press("Down");
engine_->VerifyLearnMoreButtonHighlighted(true);
engine_->VerifyCandidateHighlighted(
emoji_suggester_->GetCandidatesSizeForTesting() - 1, false);
// Go to first candidate
Press("Down");
engine_->VerifyLearnMoreButtonHighlighted(false);
engine_->VerifyCandidateHighlighted(0, true);
}
TEST_F(EmojiSuggesterTest,
OpenSettingWhenPressingEnterAndLearnMoreButtonIsChosen) {
EXPECT_TRUE(emoji_suggester_->Suggest(base::UTF8ToUTF16("happy ")));
// Choose Learn More Button.
Press("Up");
engine_->VerifyLearnMoreButtonHighlighted(true);
EXPECT_EQ(Press("Enter"), SuggestionStatus::kOpenSettings);
}
TEST_F(EmojiSuggesterTest, DoesNotShowIndicesWhenFirstSuggesting) {
......
......@@ -27,6 +27,7 @@ enum class SuggestionStatus {
kAccept = 1,
kDismiss = 2,
kBrowsing = 3,
kOpenSettings = 4,
};
} // namespace chromeos
......
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