Commit 7d8383f1 authored by My Nguyen's avatar My Nguyen Committed by Commit Bot

Add pref to show_tab

Stop showing tab to users after they have accepted the suggestion 10
times. Doing this by adding a pref value.

Bug: 1081940
Change-Id: Ibbc9d8c3a7e5fcc03d3e1d0a6a2b92ae110b0c85
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2196883
Commit-Queue: My Nguyen <myy@chromium.org>
Reviewed-by: default avatarJing Wang <jiwan@chromium.org>
Reviewed-by: default avatarDarren Shen <shend@chromium.org>
Cr-Commit-Position: refs/heads/master@{#769930}
parent bf3752d5
......@@ -10,9 +10,11 @@
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chromeos/constants/chromeos_pref_names.h"
#include "components/autofill/core/browser/data_model/autofill_profile.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
using input_method::InputMethodEngineBase;
......@@ -25,6 +27,7 @@ const char kAssistEmailPrefix[] = "my email is ";
const char kAssistNamePrefix[] = "my name is ";
const char kAssistAddressPrefix[] = "my address is ";
const char kAssistPhoneNumberPrefix[] = "my phone number is ";
const char kAnnounceShowTab[] = "Press tab to insert.";
constexpr base::TimeDelta kTtsShowDelay =
base::TimeDelta::FromMilliseconds(1200);
......@@ -114,6 +117,13 @@ SuggestionStatus PersonalInfoSuggester::HandleKeyEvent(
if (suggestion_shown_) {
if (event.key == "Tab" || event.key == "Right") {
AcceptSuggestion();
int tab_acceptance_count = GetTabAcceptanceCount();
if (tab_acceptance_count < kMaxTabAcceptanceCount) {
DictionaryPrefUpdate update(profile_->GetPrefs(),
prefs::kAssistiveInputFeatureSettings);
update->SetIntKey(kPersonalInfoSuggesterTabAcceptanceCount,
tab_acceptance_count + 1);
}
return SuggestionStatus::kAccept;
} else if (event.key == "Esc") {
DismissSuggestion();
......@@ -195,8 +205,9 @@ base::string16 PersonalInfoSuggester::GetSuggestion(
void PersonalInfoSuggester::ShowSuggestion(const base::string16& text,
const size_t confirmed_length) {
std::string error;
suggestion_handler_->SetSuggestion(context_id_, text, confirmed_length, true,
&error);
bool show_tab = GetTabAcceptanceCount() < kMaxTabAcceptanceCount;
suggestion_handler_->SetSuggestion(context_id_, text, confirmed_length,
show_tab, &error);
if (!error.empty()) {
LOG(ERROR) << "Fail to show suggestion. " << error;
}
......@@ -205,14 +216,27 @@ void PersonalInfoSuggester::ShowSuggestion(const base::string16& text,
tts_handler_->Announce(
// TODO(jiwan): Add translation to other languages when we support more
// than English.
base::StringPrintf("Suggested text %s. Press tab to insert.",
base::UTF16ToUTF8(text).c_str()),
base::StringPrintf(
"Suggested text %s. %s", base::UTF16ToUTF8(text).c_str(),
show_tab ? kAnnounceShowTab : base::EmptyString().c_str()),
kTtsShowDelay);
}
suggestion_shown_ = true;
}
int PersonalInfoSuggester::GetTabAcceptanceCount() {
DictionaryPrefUpdate update(profile_->GetPrefs(),
prefs::kAssistiveInputFeatureSettings);
auto tab_acceptance_count =
update->FindIntKey(kPersonalInfoSuggesterTabAcceptanceCount);
if (!tab_acceptance_count.has_value()) {
update->SetIntKey(kPersonalInfoSuggesterTabAcceptanceCount, 0);
return 0;
}
return *tab_acceptance_count;
}
AssistiveType PersonalInfoSuggester::GetProposeActionType() {
return proposed_action_type_;
}
......
......@@ -21,6 +21,10 @@ class Profile;
namespace chromeos {
const char kPersonalInfoSuggesterTabAcceptanceCount[] =
"personal_info_suggester_tab_acceptance_count";
const int kMaxTabAcceptanceCount = 10;
AssistiveType ProposeAssistiveAction(const base::string16& text);
class TtsHandler : public content::UtteranceEventDelegate {
......@@ -80,6 +84,8 @@ class PersonalInfoSuggester : public Suggester {
void AcceptSuggestion();
int GetTabAcceptanceCount();
SuggestionHandlerInterface* const suggestion_handler_;
// ID of the focused text field, 0 if none is focused.
......
......@@ -8,10 +8,12 @@
#include "base/guid.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/constants/chromeos_pref_names.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/data_model/autofill_profile.h"
#include "components/autofill/core/browser/test_autofill_client.h"
#include "components/autofill/core/browser/test_personal_data_manager.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -34,6 +36,7 @@ class TestSuggestionHandler : public SuggestionHandlerInterface {
std::string* error) override {
suggestion_text_ = text;
confirmed_length_ = confirmed_length;
show_tab_ = show_tab;
return true;
}
......@@ -50,11 +53,14 @@ class TestSuggestionHandler : public SuggestionHandlerInterface {
EXPECT_EQ(confirmed_length_, confirmed_length);
}
void VerifyShowTab(const bool show_tab) { EXPECT_EQ(show_tab_, show_tab); }
bool IsSuggestionAccepted() { return suggestion_accepted_; }
private:
base::string16 suggestion_text_;
size_t confirmed_length_ = 0;
bool show_tab_ = false;
bool suggestion_accepted_ = false;
};
......@@ -228,4 +234,35 @@ TEST_F(PersonalInfoSuggesterTest, AnnounceSpokenFeedbackWhenChromeVoxIsOn) {
"Inserted suggestion %s.", base::UTF16ToUTF8(email_).c_str()));
}
TEST_F(PersonalInfoSuggesterTest, DoNotShowTabAfterMaxTabAcceptanceCount) {
for (int i = 0; i < kMaxTabAcceptanceCount; i++) {
suggester_->Suggest(base::UTF8ToUTF16("my email is "));
::input_method::InputMethodEngineBase::KeyboardEvent event;
event.key = "Tab";
suggester_->HandleKeyEvent(event);
suggestion_handler_->VerifyShowTab(true);
}
suggester_->Suggest(base::UTF8ToUTF16("my email is "));
suggestion_handler_->VerifyShowTab(false);
}
TEST_F(PersonalInfoSuggesterTest, DoNotAnnouncePressTabWhenTabNotShown) {
profile_->set_profile_name(base::UTF16ToUTF8(email_));
profile_->GetPrefs()->SetBoolean(
ash::prefs::kAccessibilitySpokenFeedbackEnabled, true);
DictionaryPrefUpdate update(profile_->GetPrefs(),
prefs::kAssistiveInputFeatureSettings);
update->SetIntKey(kPersonalInfoSuggesterTabAcceptanceCount,
kMaxTabAcceptanceCount);
suggester_->Suggest(base::UTF8ToUTF16("my email is "));
suggestion_handler_->VerifyShowTab(false);
task_environment_.FastForwardBy(base::TimeDelta::FromMilliseconds(500));
tts_handler_->VerifyAnnouncement("");
task_environment_.FastForwardBy(base::TimeDelta::FromMilliseconds(1000));
tts_handler_->VerifyAnnouncement(base::StringPrintf(
"Suggested text %s. ", base::UTF16ToUTF8(email_).c_str()));
}
} // 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