Commit 2f26886b authored by Anastasia Helfinstein's avatar Anastasia Helfinstein Committed by Commit Bot

Have Dictation stop when focus changes

With this change, Dictation stops listening when a new tab is opened, as
well as in most cases where focus changes between two text fields on a
page.

I will follow up on why it does not stop when using my minimal example
of two <input type='text'> tags (and nothing else).

Bug: 866489
Change-Id: Ib0a60dcb3b5871173bceeab9675491c81e72414d
Reviewed-on: https://chromium-review.googlesource.com/c/1347002Reviewed-by: default avatarDominic Mazzoni <dmazzoni@chromium.org>
Reviewed-by: default avatarKatie Dektar <katie@chromium.org>
Commit-Queue: Anastasia Helfinstein <anastasi@google.com>
Cr-Commit-Position: refs/heads/master@{#610899}
parent b1ba120f
...@@ -61,9 +61,15 @@ ui::IMEInputContextHandlerInterface* GetInputContext() { ...@@ -61,9 +61,15 @@ ui::IMEInputContextHandlerInterface* GetInputContext() {
DictationChromeos::DictationChromeos(Profile* profile) DictationChromeos::DictationChromeos(Profile* profile)
: composition_(std::make_unique<ui::CompositionText>()), : composition_(std::make_unique<ui::CompositionText>()),
profile_(profile), profile_(profile),
weak_ptr_factory_(this) {} weak_ptr_factory_(this) {
if (GetInputContext() && GetInputContext()->GetInputMethod())
GetInputContext()->GetInputMethod()->AddObserver(this);
}
DictationChromeos::~DictationChromeos() = default; DictationChromeos::~DictationChromeos() {
if (GetInputContext() && GetInputContext()->GetInputMethod())
GetInputContext()->GetInputMethod()->RemoveObserver(this);
}
bool DictationChromeos::OnToggleDictation() { bool DictationChromeos::OnToggleDictation() {
if (speech_recognizer_) { if (speech_recognizer_) {
...@@ -113,6 +119,18 @@ void DictationChromeos::OnSpeechRecognitionStateChanged( ...@@ -113,6 +119,18 @@ void DictationChromeos::OnSpeechRecognitionStateChanged(
void DictationChromeos::GetSpeechAuthParameters(std::string* auth_scope, void DictationChromeos::GetSpeechAuthParameters(std::string* auth_scope,
std::string* auth_token) {} std::string* auth_token) {}
void DictationChromeos::OnTextInputStateChanged(
const ui::TextInputClient* client) {
if (!client)
return;
if (client->GetFocusReason() ==
ui::TextInputClient::FocusReason::FOCUS_REASON_NONE)
return;
DictationOff();
}
void DictationChromeos::DictationOff() { void DictationChromeos::DictationOff() {
if (!speech_recognizer_) if (!speech_recognizer_)
return; return;
......
...@@ -11,9 +11,11 @@ ...@@ -11,9 +11,11 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "chrome/browser/speech/speech_recognizer_delegate.h" #include "chrome/browser/speech/speech_recognizer_delegate.h"
#include "content/public/browser/speech_recognition_session_preamble.h" #include "content/public/browser/speech_recognition_session_preamble.h"
#include "ui/base/ime/input_method_observer.h"
namespace ui { namespace ui {
struct CompositionText; struct CompositionText;
class TextInputClient;
} // namespace ui } // namespace ui
class Profile; class Profile;
...@@ -22,7 +24,8 @@ class SpeechRecognizer; ...@@ -22,7 +24,8 @@ class SpeechRecognizer;
namespace chromeos { namespace chromeos {
// Provides global dictation (type what you speak) on Chrome OS. // Provides global dictation (type what you speak) on Chrome OS.
class DictationChromeos : public SpeechRecognizerDelegate { class DictationChromeos : public SpeechRecognizerDelegate,
public ui::InputMethodObserver {
public: public:
explicit DictationChromeos(Profile* profile); explicit DictationChromeos(Profile* profile);
~DictationChromeos() override; ~DictationChromeos() override;
...@@ -41,6 +44,14 @@ class DictationChromeos : public SpeechRecognizerDelegate { ...@@ -41,6 +44,14 @@ class DictationChromeos : public SpeechRecognizerDelegate {
void GetSpeechAuthParameters(std::string* auth_scope, void GetSpeechAuthParameters(std::string* auth_scope,
std::string* auth_token) override; std::string* auth_token) override;
// ui::InputMethodObserver:
void OnTextInputStateChanged(const ui::TextInputClient* client) override;
void OnCaretBoundsChanged(const ui::TextInputClient* client) override {}
void OnInputMethodDestroyed(const ui::InputMethod* input_method) override {}
void OnShowVirtualKeyboardIfEnabled() override {}
void OnFocus() override {}
void OnBlur() override {}
// Saves current dictation result and stops listening. // Saves current dictation result and stops listening.
void DictationOff(); void DictationOff();
......
...@@ -6,7 +6,9 @@ ...@@ -6,7 +6,9 @@
#include "chrome/browser/chromeos/accessibility/accessibility_manager.h" #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
#include "chrome/browser/chromeos/accessibility/dictation_chromeos.h" #include "chrome/browser/chromeos/accessibility/dictation_chromeos.h"
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
#include "ui/base/ime/dummy_text_input_client.h"
#include "ui/base/ime/ime_bridge.h" #include "ui/base/ime/ime_bridge.h"
#include "ui/base/ime/input_method_base.h"
#include "ui/base/ime/mock_ime_input_context_handler.h" #include "ui/base/ime/mock_ime_input_context_handler.h"
namespace chromeos { namespace chromeos {
...@@ -42,6 +44,10 @@ class DictationTest : public InProcessBrowserTest { ...@@ -42,6 +44,10 @@ class DictationTest : public InProcessBrowserTest {
is_final); is_final);
} }
void NotifyTextInputStateChanged(ui::TextInputClient* client) {
GetManager()->dictation_->OnTextInputStateChanged(client);
}
ui::CompositionText GetLastCompositionText() { ui::CompositionText GetLastCompositionText() {
return input_context_handler_->last_update_composition_arg() return input_context_handler_->last_update_composition_arg()
.composition_text; .composition_text;
...@@ -145,4 +151,19 @@ IN_PROC_BROWSER_TEST_F(DictationTest, SwitchInputContext) { ...@@ -145,4 +151,19 @@ IN_PROC_BROWSER_TEST_F(DictationTest, SwitchInputContext) {
ui::IMEBridge::Get()->SetInputContextHandler(nullptr); ui::IMEBridge::Get()->SetInputContextHandler(nullptr);
} }
IN_PROC_BROWSER_TEST_F(DictationTest, ChangeInputField) {
// Turn on dictation and start speaking.
AccessibilityManager::Get()->ToggleDictation();
SendSpeechResult(kFinalSpeechResult, false /* is_final */);
// Change the input state to a new client.
std::unique_ptr<ui::TextInputClient> new_client =
std::make_unique<ui::DummyTextInputClient>();
NotifyTextInputStateChanged(new_client.get());
// Check that dictation has turned off.
EXPECT_EQ(1, input_context_handler_->commit_text_call_count());
EXPECT_EQ(kFinalSpeechResult, input_context_handler_->last_commit_text());
}
} // namespace chromeos } // 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