Commit 683a25ac authored by Darren Shen's avatar Darren Shen Committed by Commit Bot

[VK] Automatically set default size for virtual keyboard on first load.

The virtual keyboard extension is responsible for choosing the size of
the keyboard window. In test code, we mock out the extension but we
don't set a size. This means that test code has to manually set the
bounds of the keyboard window, which is a bit weird. Failing to set
the size result in very confusing test failures too.

Therefore, we set a default virtual keyboard size in the test KeyboardUI
implementations. If test code need a specific size, they can always
resize the window themselves.

Bug: 849995
Change-Id: I3794f11719bcfe722317174d43492141e6f75b80
Reviewed-on: https://chromium-review.googlesource.com/c/1278462
Commit-Queue: Darren Shen <shend@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Cr-Commit-Position: refs/heads/master@{#606642}
parent 9495994e
......@@ -2237,6 +2237,7 @@ static_library("test_support_common") {
"//ui/gl",
"//ui/gl:test_support",
"//ui/keyboard",
"//ui/keyboard:test_support",
"//ui/message_center",
"//ui/message_center/public/cpp",
"//ui/views",
......
......@@ -1664,9 +1664,6 @@ TEST_P(AppListPresenterDelegateVirtualKeyboardTest,
// Manually show the virtual keyboard.
auto* const keyboard_controller = keyboard::KeyboardController::Get();
keyboard_controller->ShowKeyboard(true);
keyboard_controller->GetKeyboardWindow()->SetBounds(
keyboard::KeyboardBoundsFromRootBounds(
Shell::GetPrimaryRootWindow()->bounds(), 100));
ASSERT_TRUE(keyboard::WaitUntilShown());
// Tap or click outside the searchbox, the virtual keyboard should hide.
......
......@@ -11,6 +11,7 @@
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/ime/mock_input_method.h"
#include "ui/keyboard/test/keyboard_test_util.h"
namespace ash {
......@@ -23,6 +24,11 @@ aura::Window* TestKeyboardUI::LoadKeyboardWindow(LoadCallback callback) {
keyboard_window_ = window_factory::NewWindow(&delegate_);
keyboard_window_->Init(ui::LAYER_NOT_DRAWN);
// Set a default size for the keyboard.
display::Screen* screen = display::Screen::GetScreen();
keyboard_window_->SetBounds(keyboard::KeyboardBoundsFromRootBounds(
screen->GetPrimaryDisplay().bounds()));
// Simulate an asynchronous load.
base::SequencedTaskRunnerHandle::Get()->PostTask(FROM_HERE,
std::move(callback));
......
......@@ -591,7 +591,6 @@ TEST_F(VirtualKeyboardControllerAlwaysEnabledTest,
keyboard_controller_->LoadKeyboardWindowInBackground();
// Wait for the keyboard window to load.
base::RunLoop().RunUntilIdle();
keyboard_controller_->GetKeyboardWindow()->SetBounds(gfx::Rect(0, 0, 10, 10));
// Show in secondary display.
keyboard_controller_->ShowKeyboardInDisplay(GetSecondaryDisplay());
......
......@@ -46,8 +46,6 @@ TEST_F(VirtualKeyboardTest, EventsAreHandledBasedOnHitTestBounds) {
auto* keyboard_controller = keyboard::KeyboardController::Get();
keyboard_controller->ShowKeyboard(false);
aura::Window* keyboard_window = keyboard_controller->GetKeyboardWindow();
keyboard_window->SetBounds(gfx::Rect(100, 100, 100, 100));
ASSERT_TRUE(keyboard::WaitUntilShown());
// Add two hit test bounds (coordinates relative to keyboard window).
......@@ -60,7 +58,8 @@ TEST_F(VirtualKeyboardTest, EventsAreHandledBasedOnHitTestBounds) {
// Click at various places within the keyboard window and check whether the
// event passes through the keyboard window to the background window.
ui::test::EventGenerator generator(root_window);
const gfx::Point origin = keyboard_window->bounds().origin();
const gfx::Point origin =
keyboard_controller->visual_bounds_in_screen().origin();
// (0, 0) is inside the first hit rect, so the event is handled by the window
// and is not received by the background window.
......@@ -97,14 +96,13 @@ TEST_F(VirtualKeyboardTest, HitTestBoundsAreResetWhenContainerTypeChanges) {
auto* keyboard_controller = keyboard::KeyboardController::Get();
keyboard_controller->ShowKeyboard(false);
aura::Window* keyboard_window = keyboard_controller->GetKeyboardWindow();
keyboard_window->SetBounds(gfx::Rect(100, 100, 100, 100));
ASSERT_TRUE(keyboard::WaitUntilShown());
// Set empty hit test bounds, so all events pass through to the background.
keyboard_controller->SetHitTestBounds(std::vector<gfx::Rect>());
ui::test::EventGenerator generator(root_window);
aura::Window* keyboard_window = keyboard_controller->GetKeyboardWindow();
// (0, 0) passes through and is received by background window.
generator.MoveMouseTo(keyboard_window->bounds().origin());
......
......@@ -49,12 +49,10 @@ void LoginKeyboardTestBase::ShowKeyboard() {
keyboard_controller->ShowKeyboard(false);
// Set keyboard height to half of the root window - this should overlap with
// lock/login layout.
if (keyboard_controller->GetKeyboardWindow()->bounds().height() == 0) {
int height = Shell::GetPrimaryRootWindow()->bounds().height() / 2;
keyboard_controller->GetKeyboardWindow()->SetBounds(
keyboard::KeyboardBoundsFromRootBounds(
Shell::GetPrimaryRootWindow()->bounds(), height));
}
int height = Shell::GetPrimaryRootWindow()->bounds().height() / 2;
keyboard_controller->GetKeyboardWindow()->SetBounds(
keyboard::KeyboardBoundsFromRootBounds(
Shell::GetPrimaryRootWindow()->bounds(), height));
ASSERT_TRUE(keyboard::WaitUntilShown());
}
......
......@@ -982,9 +982,6 @@ TEST_F(MagnificationControllerTest, TextfieldFocusedWithKeyboard) {
auto* keyboard_controller = keyboard::KeyboardController::Get();
keyboard_controller->ShowKeyboard(true);
gfx::Rect keyboard_bounds = gfx::Rect(0, 300, 800, 300);
keyboard_controller->GetKeyboardWindow()->SetBounds(keyboard_bounds);
// Focus on the text input field.
text_input_helper_.FocusOnTextInputView();
base::RunLoop().RunUntilIdle();
......@@ -993,7 +990,8 @@ TEST_F(MagnificationControllerTest, TextfieldFocusedWithKeyboard) {
gfx::Rect viewport_outside_keyboard_bounds = GetViewport();
viewport_outside_keyboard_bounds.set_height(
viewport_outside_keyboard_bounds.height() -
keyboard_bounds.height() / GetMagnificationController()->GetScale());
keyboard_controller->visual_bounds_in_screen().height() /
GetMagnificationController()->GetScale());
gfx::Rect caret_bounds = text_input_helper_.GetCaretBounds();
......
......@@ -827,17 +827,14 @@ TEST_F(VirtualKeyboardRootWindowControllerTest, ClickWithActiveModalDialog) {
aura::Window* root_window = Shell::GetPrimaryRootWindow();
ASSERT_EQ(root_window, controller->GetRootWindow());
aura::Window* contents_window = controller->GetKeyboardWindow();
contents_window->SetName("KeyboardWindow");
contents_window->SetBounds(
keyboard::KeyboardBoundsFromRootBounds(root_window->bounds(), 100));
contents_window->Show();
controller->ShowKeyboard(false /* locked */);
ASSERT_TRUE(keyboard::WaitUntilShown());
ui::test::TestEventHandler handler;
root_window->AddPreTargetHandler(&handler);
ui::test::EventGenerator root_window_event_generator(root_window);
ui::test::EventGenerator keyboard_event_generator(root_window,
contents_window);
ui::test::EventGenerator keyboard_event_generator(
root_window, controller->GetKeyboardWindow());
views::Widget* modal_widget = CreateModalWidget(gfx::Rect(300, 10, 100, 100));
......@@ -1064,9 +1061,8 @@ TEST_F(VirtualKeyboardRootWindowControllerTest, ClickDoesNotFocusKeyboard) {
auto* keyboard_controller = keyboard::KeyboardController::Get();
keyboard_controller->ShowKeyboard(false);
aura::Window* keyboard_window = keyboard_controller->GetKeyboardWindow();
keyboard_window->SetBounds(gfx::Rect(100, 100, 100, 100));
ASSERT_TRUE(keyboard::WaitUntilShown());
aura::Window* keyboard_window = keyboard_controller->GetKeyboardWindow();
EXPECT_FALSE(keyboard_window->HasFocus());
// Click on the keyboard. Make sure the keyboard receives the event, but does
......
......@@ -74,10 +74,6 @@ TEST_F(VirtualKeyboardAlwaysOnTopControllerTest, NotifyKeyboardBoundsChanging) {
// Show the keyboard.
auto* keyboard_controller = keyboard::KeyboardController::Get();
keyboard_controller->ShowKeyboard(false /* locked */);
const int kKeyboardHeight = 200;
keyboard_controller->GetKeyboardWindow()->SetBounds(
keyboard::KeyboardBoundsFromRootBounds(root_window->bounds(),
kKeyboardHeight));
ASSERT_TRUE(keyboard::WaitUntilShown());
// Verify that test manager was notified of bounds change.
......
......@@ -43,8 +43,6 @@ namespace ash {
namespace {
constexpr int kVirtualKeyboardHeight = 100;
aura::Window* GetContainer(ShellWindowId container_id) {
return Shell::GetPrimaryRootWindowController()->GetContainer(container_id);
}
......@@ -130,9 +128,6 @@ class LockActionHandlerLayoutManagerTest : public AshTestBase {
if (show) {
keyboard->ShowKeyboard(true);
keyboard->GetKeyboardWindow()->SetBounds(
keyboard::KeyboardBoundsFromRootBounds(
Shell::GetPrimaryRootWindow()->bounds(), kVirtualKeyboardHeight));
ASSERT_TRUE(keyboard::WaitUntilShown());
} else {
keyboard->HideKeyboardByUser();
......
......@@ -27,8 +27,6 @@ namespace ash {
namespace {
const int kVirtualKeyboardHeight = 100;
// A login implementation of WidgetDelegate.
class LoginTestWidgetDelegate : public views::WidgetDelegate {
public:
......@@ -88,13 +86,7 @@ class LockLayoutManagerTest : public AshTestBase {
if (show) {
keyboard->ShowKeyboard(false);
if (keyboard->GetKeyboardWindow()->bounds().height() == 0) {
keyboard->GetKeyboardWindow()->SetBounds(
keyboard::KeyboardBoundsFromRootBounds(
Shell::GetPrimaryRootWindow()->bounds(),
kVirtualKeyboardHeight));
ASSERT_TRUE(keyboard::WaitUntilShown());
}
ASSERT_TRUE(keyboard::WaitUntilShown());
} else {
keyboard->HideKeyboardByUser();
}
......
......@@ -57,6 +57,7 @@ class OverviewVirtualKeyboardTest : public WindowSelectorControllerTest {
WindowSelectorControllerTest::SetUp();
TabletModeControllerTestApi().EnterTabletMode();
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(keyboard::IsKeyboardEnabled());
keyboard_controller()->LoadKeyboardWindowInBackground();
......@@ -74,7 +75,7 @@ class OverviewVirtualKeyboardTest : public WindowSelectorControllerTest {
TEST_F(OverviewVirtualKeyboardTest, ToggleOverviewModeHidesVirtualKeyboard) {
keyboard_controller()->ShowKeyboard(false /* locked */);
keyboard::WaitUntilShown();
ASSERT_TRUE(keyboard::WaitUntilShown());
Shell::Get()->window_selector_controller()->ToggleOverview();
......@@ -85,7 +86,7 @@ TEST_F(OverviewVirtualKeyboardTest, ToggleOverviewModeHidesVirtualKeyboard) {
TEST_F(OverviewVirtualKeyboardTest,
ToggleOverviewModeDoesNotHideLockedVirtualKeyboard) {
keyboard_controller()->ShowKeyboard(true /* locked */);
keyboard::WaitUntilShown();
ASSERT_TRUE(keyboard::WaitUntilShown());
Shell::Get()->window_selector_controller()->ToggleOverview();
EXPECT_FALSE(keyboard::IsKeyboardHiding());
......
......@@ -183,13 +183,8 @@ class SystemModalContainerLayoutManagerTest : public AshTestBase {
return;
if (show) {
keyboard->ShowKeyboard(true);
if (keyboard->GetKeyboardWindow()->bounds().height() == 0) {
keyboard->GetKeyboardWindow()->SetBounds(
keyboard::KeyboardBoundsFromRootBounds(
Shell::GetPrimaryRootWindow()->bounds(), 100));
ASSERT_TRUE(keyboard::WaitUntilShown());
}
keyboard->ShowKeyboard(true /* lock */);
ASSERT_TRUE(keyboard::WaitUntilShown());
} else {
keyboard->HideKeyboardByUser();
}
......
......@@ -1731,9 +1731,6 @@ TEST_F(WorkspaceLayoutManagerKeyboardTest,
// Open keyboard in non-sticky mode.
kb_controller->ShowKeyboard(false);
kb_controller->GetKeyboardWindow()->SetBounds(
keyboard::KeyboardBoundsFromRootBounds(
Shell::GetPrimaryRootWindow()->bounds(), 100));
// Window should not be shifted up.
EXPECT_EQ(orig_window_bounds, window->bounds());
......@@ -1923,8 +1920,6 @@ TEST_F(WorkspaceLayoutManagerSystemUiAreaTest,
EXPECT_EQ(0, test_state()->num_system_ui_area_changes());
keyboard_controller->ShowKeyboard(/*lock=*/true);
aura::Window* keyboard_window = keyboard_controller->GetKeyboardWindow();
keyboard_window->SetBounds(gfx::Rect(0, 0, 1, 1));
ASSERT_TRUE(keyboard::WaitUntilShown());
EXPECT_GE(test_state()->num_system_ui_area_changes(), 1);
test_state()->reset_num_system_ui_area_changes();
......@@ -1941,8 +1936,7 @@ TEST_F(WorkspaceLayoutManagerSystemUiAreaTest,
EXPECT_EQ(0, test_state()->num_system_ui_area_changes());
keyboard_controller->ShowKeyboard(/*lock=*/true);
aura::Window* keyboard_window = keyboard_controller->GetKeyboardWindow();
keyboard_window->SetBounds(gfx::Rect(0, 0, 1, 1));
keyboard_controller->SetKeyboardWindowBounds(gfx::Rect(0, 0, 100, 50));
ASSERT_TRUE(keyboard::WaitUntilShown());
EXPECT_GE(test_state()->num_system_ui_area_changes(), 1);
test_state()->reset_num_system_ui_area_changes();
......@@ -1959,8 +1953,6 @@ TEST_F(WorkspaceLayoutManagerSystemUiAreaTest,
EXPECT_EQ(0, test_state()->num_system_ui_area_changes());
keyboard_controller->ShowKeyboard(/*lock=*/true);
aura::Window* keyboard_window = keyboard_controller->GetKeyboardWindow();
keyboard_window->SetBounds(gfx::Rect(0, 0, 1, 1));
ASSERT_TRUE(keyboard::WaitUntilShown());
EXPECT_GE(test_state()->num_system_ui_area_changes(), 1);
test_state()->reset_num_system_ui_area_changes();
......
......@@ -94,6 +94,7 @@ static_library("test_support") {
"//ui/aura",
"//ui/aura:test_support",
"//ui/base:test_support",
"//ui/display",
]
}
......
......@@ -824,7 +824,7 @@ void KeyboardController::PopulateKeyboardContent(
switch (state_) {
case KeyboardControllerState::INITIAL:
DCHECK_EQ(keyboard_window->bounds().height(), 0);
DCHECK(!IsKeyboardVisible());
show_on_keyboard_window_load_ = show_keyboard;
ChangeState(KeyboardControllerState::LOADING_EXTENSION);
return;
......
......@@ -44,8 +44,6 @@
namespace keyboard {
namespace {
const int kDefaultVirtualKeyboardHeight = 100;
// Steps a layer animation until it is completed. Animations must be enabled.
void RunAnimationForLayer(ui::Layer* layer) {
// Animations must be enabled for stepping to work.
......@@ -249,13 +247,7 @@ class KeyboardControllerTest : public aura::test::AuraTestBase,
if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE &&
client->GetTextInputMode() != ui::TEXT_INPUT_MODE_NONE) {
input_method->ShowVirtualKeyboardIfEnabled();
if (controller().GetKeyboardWindow()->bounds().height() == 0) {
// Set initial bounds for test keyboard window.
controller().GetKeyboardWindow()->SetBounds(
KeyboardBoundsFromRootBounds(root_window()->bounds(),
kDefaultVirtualKeyboardHeight));
ASSERT_TRUE(keyboard::WaitUntilShown());
}
ASSERT_TRUE(keyboard::WaitUntilShown());
}
}
......@@ -302,14 +294,11 @@ TEST_F(KeyboardControllerTest, KeyboardSize) {
controller().LoadKeyboardWindowInBackground();
// The keyboard window should not be visible.
aura::Window* keyboard_window = controller().GetKeyboardWindow();
EXPECT_FALSE(keyboard_window->IsVisible());
// The container should be positioned at the bottom of screen and has 0
// height.
const gfx::Rect screen_bounds = root_window()->bounds();
const gfx::Rect initial_keyboard_bounds = keyboard_window->bounds();
EXPECT_EQ(0, initial_keyboard_bounds.height());
EXPECT_EQ(screen_bounds.height(), initial_keyboard_bounds.y());
// Attempt to change window width or move window up from the bottom are
// ignored. Changing window height is supported.
......
......@@ -8,6 +8,7 @@
#include "base/threading/sequenced_task_runner_handle.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
#include "ui/display/screen.h"
#include "ui/keyboard/keyboard_controller.h"
#include "ui/keyboard/keyboard_controller_observer.h"
......@@ -15,6 +16,8 @@ namespace keyboard {
namespace {
constexpr int kDefaultKeyboardHeight = 100;
class KeyboardVisibilityChangeWaiter : public KeyboardControllerObserver {
public:
explicit KeyboardVisibilityChangeWaiter(bool wait_until)
......@@ -111,6 +114,10 @@ bool IsKeyboardHiding() {
KeyboardControllerState::HIDDEN;
}
gfx::Rect KeyboardBoundsFromRootBounds(const gfx::Rect& root_bounds) {
return KeyboardBoundsFromRootBounds(root_bounds, kDefaultKeyboardHeight);
}
gfx::Rect KeyboardBoundsFromRootBounds(const gfx::Rect& root_bounds,
int keyboard_height) {
return gfx::Rect(root_bounds.x(), root_bounds.bottom() - keyboard_height,
......@@ -131,6 +138,11 @@ aura::Window* TestKeyboardUI::LoadKeyboardWindow(LoadCallback callback) {
window_->Init(ui::LAYER_NOT_DRAWN);
window_->set_owned_by_parent(false);
// Set a default size for the keyboard.
display::Screen* screen = display::Screen::GetScreen();
window_->SetBounds(
KeyboardBoundsFromRootBounds(screen->GetPrimaryDisplay().bounds()));
// Simulate an asynchronous load.
base::SequencedTaskRunnerHandle::Get()->PostTask(FROM_HERE,
std::move(callback));
......
......@@ -32,6 +32,10 @@ bool IsKeyboardShowing();
// Returns true if the keyboard is about to hide or already hidden.
bool IsKeyboardHiding();
// Gets the calculated keyboard bounds from |root_bounds|. A default keyboard
// height is used.
gfx::Rect KeyboardBoundsFromRootBounds(const gfx::Rect& root_bounds);
// Gets the calculated keyboard bounds from |root_bounds|. The keyboard height
// is specified by |keyboard_height|.
gfx::Rect KeyboardBoundsFromRootBounds(const gfx::Rect& root_bounds,
......
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