Commit 69da6781 authored by Darren Shen's avatar Darren Shen Committed by Commit Bot

vk: Delay notifying observers when VK is enabled / disabled.

When observers are notified about the VK being enabled / disabled, the
state of the KeyboardController at the time is wrong. For example,
when notified about disabling the VK, KeyboardController::IsEnabled()
still returns true (!).

We move the observer notification code to the correct place and add
tests to verify the state of the KeyboardController at the time of
notification.

Bug: 943446
Change-Id: I663042f743826afdb9880c538e6110ca13b4a6d2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1572983
Commit-Queue: Darren Shen <shend@chromium.org>
Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Cr-Commit-Position: refs/heads/master@{#654787}
parent 74d1c1d9
...@@ -288,15 +288,16 @@ void KeyboardController::EnableKeyboard() { ...@@ -288,15 +288,16 @@ void KeyboardController::EnableKeyboard() {
time_of_last_blur_ = base::Time::UnixEpoch(); time_of_last_blur_ = base::Time::UnixEpoch();
UpdateInputMethodObserver(); UpdateInputMethodObserver();
for (KeyboardControllerObserver& observer : observer_list_)
observer.OnKeyboardEnabledChanged(true);
ActivateKeyboardInContainer( ActivateKeyboardInContainer(
layout_delegate_->GetContainerForDefaultDisplay()); layout_delegate_->GetContainerForDefaultDisplay());
// Start preloading the virtual keyboard UI in the background, so that it // Start preloading the virtual keyboard UI in the background, so that it
// shows up faster when needed. // shows up faster when needed.
LoadKeyboardWindowInBackground(); LoadKeyboardWindowInBackground();
// Notify observers after the keyboard window has a root window.
for (KeyboardControllerObserver& observer : observer_list_)
observer.OnKeyboardEnabledChanged(true);
} }
void KeyboardController::DisableKeyboard() { void KeyboardController::DisableKeyboard() {
...@@ -323,10 +324,12 @@ void KeyboardController::DisableKeyboard() { ...@@ -323,10 +324,12 @@ void KeyboardController::DisableKeyboard() {
animation_observer_.reset(); animation_observer_.reset();
ime_observer_.RemoveAll(); ime_observer_.RemoveAll();
for (KeyboardControllerObserver& observer : observer_list_)
observer.OnKeyboardEnabledChanged(false);
ui_->SetController(nullptr); ui_->SetController(nullptr);
ui_.reset(); ui_.reset();
// Notify observers after |ui_| is reset so that IsEnabled() is false.
for (KeyboardControllerObserver& observer : observer_list_)
observer.OnKeyboardEnabledChanged(false);
} }
void KeyboardController::ActivateKeyboardInContainer(aura::Window* parent) { void KeyboardController::ActivateKeyboardInContainer(aura::Window* parent) {
......
...@@ -772,4 +772,55 @@ TEST_F(KeyboardControllerTest, DontClearObserverList) { ...@@ -772,4 +772,55 @@ TEST_F(KeyboardControllerTest, DontClearObserverList) {
EXPECT_TRUE(IsKeyboardDisabled()); EXPECT_TRUE(IsKeyboardDisabled());
} }
class MockKeyboardControllerObserver : public KeyboardControllerObserver {
public:
MockKeyboardControllerObserver() = default;
~MockKeyboardControllerObserver() override = default;
// KeyboardControllerObserver:
MOCK_METHOD1(OnKeyboardEnabledChanged, void(bool is_enabled));
private:
DISALLOW_COPY_AND_ASSIGN(MockKeyboardControllerObserver);
};
TEST_F(KeyboardControllerTest, OnKeyboardEnabledChangedToEnabled) {
// Start with the keyboard disabled.
keyboard::SetTouchKeyboardEnabled(false);
MockKeyboardControllerObserver mock_observer;
controller().AddObserver(&mock_observer);
EXPECT_CALL(mock_observer, OnKeyboardEnabledChanged(true))
.WillOnce(testing::InvokeWithoutArgs([]() {
auto* controller = keyboard::KeyboardController::Get();
ASSERT_TRUE(controller);
EXPECT_TRUE(controller->IsEnabled());
EXPECT_TRUE(controller->GetKeyboardWindow());
EXPECT_TRUE(controller->GetRootWindow());
}));
keyboard::SetTouchKeyboardEnabled(true);
controller().RemoveObserver(&mock_observer);
}
TEST_F(KeyboardControllerTest, OnKeyboardEnabledChangedToDisabled) {
MockKeyboardControllerObserver mock_observer;
controller().AddObserver(&mock_observer);
EXPECT_CALL(mock_observer, OnKeyboardEnabledChanged(false))
.WillOnce(testing::InvokeWithoutArgs([]() {
auto* controller = keyboard::KeyboardController::Get();
ASSERT_TRUE(controller);
EXPECT_FALSE(controller->IsEnabled());
EXPECT_FALSE(controller->GetKeyboardWindow());
EXPECT_FALSE(controller->GetRootWindow());
}));
keyboard::SetTouchKeyboardEnabled(false);
controller().RemoveObserver(&mock_observer);
}
} // namespace keyboard } // namespace keyboard
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