This source file includes following definitions.
- RunAnimationForLayer
- OnEvent
- input_method_
- HasKeyboardWindow
- GetKeyboardWindow
- GetBrowserContext
- GetInputMethod
- RequestAudioInput
- LoadSystemKeyboard
- ReloadKeyboardIfNeeded
- GetEventCount
- OnEvent
- OnWindowVisibilityChanged
- SetUp
- TearDown
- root_window
- proxy
- controller
- ShowKeyboard
- SetFocus
- WillHideKeyboard
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- SetUp
- TearDown
- OnKeyboardBoundsChanging
- notified_bounds
- keyboard_container
- keyboard_window
- TEST_F
- TEST_F
- SetUp
- TEST_F
#include "base/bind.h"
#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/focus_client.h"
#include "ui/aura/layout_manager.h"
#include "ui/aura/test/aura_test_helper.h"
#include "ui/aura/test/event_generator.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/base/ime/dummy_text_input_client.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/input_method_factory.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/compositor/layer_type.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/compositor/test/context_factories_for_test.h"
#include "ui/compositor/test/layer_animator_test_controller.h"
#include "ui/gfx/rect.h"
#include "ui/keyboard/keyboard_controller.h"
#include "ui/keyboard/keyboard_controller_observer.h"
#include "ui/keyboard/keyboard_controller_proxy.h"
#include "ui/keyboard/keyboard_switches.h"
namespace keyboard {
namespace {
void RunAnimationForLayer(ui::Layer* layer) {
ASSERT_NE(ui::ScopedAnimationDurationScaleMode::duration_scale_mode(),
ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
ui::LayerAnimatorTestController controller(layer->GetAnimator());
gfx::AnimationContainerElement* element = layer->GetAnimator();
while (controller.animator()->is_animating()) {
controller.StartThreadedAnimationsIfNeeded();
base::TimeTicks step_time = controller.animator()->last_step_time();
element->Step(step_time + base::TimeDelta::FromMilliseconds(1000));
}
}
class TestFocusController : public ui::EventHandler {
public:
explicit TestFocusController(aura::Window* root)
: root_(root) {
root_->AddPreTargetHandler(this);
}
virtual ~TestFocusController() {
root_->RemovePreTargetHandler(this);
}
private:
virtual void OnEvent(ui::Event* event) OVERRIDE {
aura::Window* target = static_cast<aura::Window*>(event->target());
if (event->type() == ui::ET_MOUSE_PRESSED ||
event->type() == ui::ET_TOUCH_PRESSED) {
aura::client::GetFocusClient(target)->FocusWindow(target);
}
}
aura::Window* root_;
DISALLOW_COPY_AND_ASSIGN(TestFocusController);
};
class TestKeyboardControllerProxy : public KeyboardControllerProxy {
public:
TestKeyboardControllerProxy()
: window_(new aura::Window(&delegate_)),
input_method_(ui::CreateInputMethod(NULL,
gfx::kNullAcceleratedWidget)) {
window_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
window_->set_owned_by_parent(false);
}
virtual ~TestKeyboardControllerProxy() {
window_.reset();
}
virtual bool HasKeyboardWindow() const OVERRIDE { return true; }
virtual aura::Window* GetKeyboardWindow() OVERRIDE { return window_.get(); }
virtual content::BrowserContext* GetBrowserContext() OVERRIDE { return NULL; }
virtual ui::InputMethod* GetInputMethod() OVERRIDE {
return input_method_.get();
}
virtual void RequestAudioInput(content::WebContents* web_contents,
const content::MediaStreamRequest& request,
const content::MediaResponseCallback& callback) OVERRIDE { return; }
virtual void LoadSystemKeyboard() OVERRIDE {};
virtual void ReloadKeyboardIfNeeded() OVERRIDE {};
private:
scoped_ptr<aura::Window> window_;
aura::test::TestWindowDelegate delegate_;
scoped_ptr<ui::InputMethod> input_method_;
DISALLOW_COPY_AND_ASSIGN(TestKeyboardControllerProxy);
};
class EventObserver : public ui::EventHandler {
public:
EventObserver() {}
virtual ~EventObserver() {}
int GetEventCount(ui::EventType type) {
return event_counts_[type];
}
private:
virtual void OnEvent(ui::Event* event) OVERRIDE {
ui::EventHandler::OnEvent(event);
event_counts_[event->type()]++;
}
std::map<ui::EventType, int> event_counts_;
DISALLOW_COPY_AND_ASSIGN(EventObserver);
};
class KeyboardContainerObserver : public aura::WindowObserver {
public:
explicit KeyboardContainerObserver(aura::Window* window) : window_(window) {
window_->AddObserver(this);
}
virtual ~KeyboardContainerObserver() {
window_->RemoveObserver(this);
}
private:
virtual void OnWindowVisibilityChanged(aura::Window* window,
bool visible) OVERRIDE {
if (!visible)
base::MessageLoop::current()->Quit();
}
aura::Window* window_;
DISALLOW_COPY_AND_ASSIGN(KeyboardContainerObserver);
};
}
class KeyboardControllerTest : public testing::Test {
public:
KeyboardControllerTest() {}
virtual ~KeyboardControllerTest() {}
virtual void SetUp() OVERRIDE {
bool enable_pixel_output = false;
ui::InitializeContextFactoryForTests(enable_pixel_output);
aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_));
aura_test_helper_->SetUp();
ui::SetUpInputMethodFactoryForTesting();
focus_controller_.reset(new TestFocusController(root_window()));
proxy_ = new TestKeyboardControllerProxy();
controller_.reset(new KeyboardController(proxy_));
}
virtual void TearDown() OVERRIDE {
controller_.reset();
focus_controller_.reset();
aura_test_helper_->TearDown();
ui::TerminateContextFactoryForTests();
}
aura::Window* root_window() { return aura_test_helper_->root_window(); }
KeyboardControllerProxy* proxy() { return proxy_; }
KeyboardController* controller() { return controller_.get(); }
void ShowKeyboard() {
ui::DummyTextInputClient test_text_input_client(ui::TEXT_INPUT_TYPE_TEXT);
SetFocus(&test_text_input_client);
}
protected:
void SetFocus(ui::TextInputClient* client) {
ui::InputMethod* input_method = proxy()->GetInputMethod();
input_method->SetFocusedTextInputClient(client);
if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE)
input_method->ShowImeIfNeeded();
}
bool WillHideKeyboard() {
return controller_->WillHideKeyboard();
}
base::MessageLoopForUI message_loop_;
scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
scoped_ptr<TestFocusController> focus_controller_;
private:
KeyboardControllerProxy* proxy_;
scoped_ptr<KeyboardController> controller_;
DISALLOW_COPY_AND_ASSIGN(KeyboardControllerTest);
};
TEST_F(KeyboardControllerTest, KeyboardSize) {
aura::Window* container(controller()->GetContainerWindow());
gfx::Rect bounds(0, 0, 100, 100);
container->SetBounds(bounds);
const gfx::Rect& before_bounds = proxy()->GetKeyboardWindow()->bounds();
gfx::Rect new_bounds(
before_bounds.x(), before_bounds.y(),
before_bounds.width() / 2, before_bounds.height() / 2);
proxy()->GetKeyboardWindow()->SetBounds(new_bounds);
ASSERT_EQ(before_bounds, proxy()->GetKeyboardWindow()->bounds());
}
TEST_F(KeyboardControllerTest, ClickDoesNotFocusKeyboard) {
const gfx::Rect& root_bounds = root_window()->bounds();
aura::test::EventCountDelegate delegate;
scoped_ptr<aura::Window> window(new aura::Window(&delegate));
window->Init(aura::WINDOW_LAYER_NOT_DRAWN);
window->SetBounds(root_bounds);
root_window()->AddChild(window.get());
window->Show();
window->Focus();
aura::Window* keyboard_container(controller()->GetContainerWindow());
keyboard_container->SetBounds(root_bounds);
root_window()->AddChild(keyboard_container);
keyboard_container->Show();
ShowKeyboard();
EXPECT_TRUE(window->IsVisible());
EXPECT_TRUE(keyboard_container->IsVisible());
EXPECT_TRUE(window->HasFocus());
EXPECT_FALSE(keyboard_container->HasFocus());
EventObserver observer;
keyboard_container->AddPreTargetHandler(&observer);
aura::test::EventGenerator generator(root_window());
generator.MoveMouseTo(proxy()->GetKeyboardWindow()->bounds().CenterPoint());
generator.ClickLeftButton();
EXPECT_TRUE(window->HasFocus());
EXPECT_FALSE(keyboard_container->HasFocus());
EXPECT_EQ("0 0", delegate.GetMouseButtonCountsAndReset());
EXPECT_EQ(1, observer.GetEventCount(ui::ET_MOUSE_PRESSED));
EXPECT_EQ(1, observer.GetEventCount(ui::ET_MOUSE_RELEASED));
generator.MoveMouseTo(gfx::Point());
generator.ClickLeftButton();
EXPECT_EQ("1 1", delegate.GetMouseButtonCountsAndReset());
keyboard_container->RemovePreTargetHandler(&observer);
}
TEST_F(KeyboardControllerTest, EventHitTestingInContainer) {
const gfx::Rect& root_bounds = root_window()->bounds();
aura::test::EventCountDelegate delegate;
scoped_ptr<aura::Window> window(new aura::Window(&delegate));
window->Init(aura::WINDOW_LAYER_NOT_DRAWN);
window->SetBounds(root_bounds);
root_window()->AddChild(window.get());
window->Show();
window->Focus();
aura::Window* keyboard_container(controller()->GetContainerWindow());
keyboard_container->SetBounds(root_bounds);
root_window()->AddChild(keyboard_container);
keyboard_container->Show();
ShowKeyboard();
EXPECT_TRUE(window->IsVisible());
EXPECT_TRUE(keyboard_container->IsVisible());
EXPECT_TRUE(window->HasFocus());
EXPECT_FALSE(keyboard_container->HasFocus());
aura::Window* keyboard_window = proxy()->GetKeyboardWindow();
ui::EventTarget* root = root_window();
ui::EventTargeter* targeter = root->GetEventTargeter();
gfx::Point location = keyboard_window->bounds().CenterPoint();
ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, location, location, ui::EF_NONE,
ui::EF_NONE);
EXPECT_EQ(keyboard_window, targeter->FindTargetForEvent(root, &mouse1));
location.set_y(keyboard_window->bounds().y() - 5);
ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, location, location, ui::EF_NONE,
ui::EF_NONE);
EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root, &mouse2));
}
TEST_F(KeyboardControllerTest, VisibilityChangeWithTextInputTypeChange) {
const gfx::Rect& root_bounds = root_window()->bounds();
ui::DummyTextInputClient input_client_0(ui::TEXT_INPUT_TYPE_TEXT);
ui::DummyTextInputClient input_client_1(ui::TEXT_INPUT_TYPE_TEXT);
ui::DummyTextInputClient input_client_2(ui::TEXT_INPUT_TYPE_TEXT);
ui::DummyTextInputClient no_input_client_0(ui::TEXT_INPUT_TYPE_NONE);
ui::DummyTextInputClient no_input_client_1(ui::TEXT_INPUT_TYPE_NONE);
aura::Window* keyboard_container(controller()->GetContainerWindow());
scoped_ptr<KeyboardContainerObserver> keyboard_container_observer(
new KeyboardContainerObserver(keyboard_container));
keyboard_container->SetBounds(root_bounds);
root_window()->AddChild(keyboard_container);
SetFocus(&input_client_0);
EXPECT_TRUE(keyboard_container->IsVisible());
SetFocus(&no_input_client_0);
EXPECT_TRUE(keyboard_container->IsVisible());
EXPECT_TRUE(WillHideKeyboard());
base::MessageLoop::current()->Run();
EXPECT_FALSE(keyboard_container->IsVisible());
SetFocus(&input_client_1);
EXPECT_TRUE(keyboard_container->IsVisible());
SetFocus(&no_input_client_1);
EXPECT_TRUE(WillHideKeyboard());
SetFocus(&input_client_2);
EXPECT_FALSE(WillHideKeyboard());
EXPECT_TRUE(keyboard_container->IsVisible());
}
TEST_F(KeyboardControllerTest, AlwaysVisibleWhenLocked) {
const gfx::Rect& root_bounds = root_window()->bounds();
ui::DummyTextInputClient input_client_0(ui::TEXT_INPUT_TYPE_TEXT);
ui::DummyTextInputClient input_client_1(ui::TEXT_INPUT_TYPE_TEXT);
ui::DummyTextInputClient no_input_client_0(ui::TEXT_INPUT_TYPE_NONE);
ui::DummyTextInputClient no_input_client_1(ui::TEXT_INPUT_TYPE_NONE);
aura::Window* keyboard_container(controller()->GetContainerWindow());
scoped_ptr<KeyboardContainerObserver> keyboard_container_observer(
new KeyboardContainerObserver(keyboard_container));
keyboard_container->SetBounds(root_bounds);
root_window()->AddChild(keyboard_container);
SetFocus(&input_client_0);
EXPECT_TRUE(keyboard_container->IsVisible());
controller()->set_lock_keyboard(true);
SetFocus(&no_input_client_0);
EXPECT_TRUE(keyboard_container->IsVisible());
EXPECT_FALSE(WillHideKeyboard());
SetFocus(&input_client_1);
EXPECT_TRUE(keyboard_container->IsVisible());
controller()->set_lock_keyboard(false);
SetFocus(&no_input_client_1);
EXPECT_TRUE(WillHideKeyboard());
base::MessageLoop::current()->Run();
EXPECT_FALSE(keyboard_container->IsVisible());
}
TEST_F(KeyboardControllerTest, KeyboardResizingFromContents) {
aura::Window* keyboard_container = controller()->GetContainerWindow();
aura::Window* keyboard_window = proxy()->GetKeyboardWindow();
keyboard_container->SetBounds(gfx::Rect(800, 600));
keyboard_container->AddChild(keyboard_window);
int original_height = keyboard_window->bounds().height();
keyboard_window->SetBounds(gfx::Rect(100, 80));
EXPECT_EQ(original_height, keyboard_window->bounds().height());
proxy()->set_resizing_from_contents(true);
keyboard_window->SetBounds(gfx::Rect(100, 80));
EXPECT_EQ(80, keyboard_window->bounds().height());
keyboard_container->SetBounds(gfx::Rect(400, 300));
EXPECT_EQ(80, keyboard_window->bounds().height());
proxy()->set_resizing_from_contents(false);
keyboard_container->SetBounds(gfx::Rect(800, 600));
EXPECT_EQ(original_height, keyboard_window->bounds().height());
}
class KeyboardControllerAnimationTest : public KeyboardControllerTest,
public KeyboardControllerObserver {
public:
KeyboardControllerAnimationTest() {}
virtual ~KeyboardControllerAnimationTest() {}
virtual void SetUp() OVERRIDE {
ui::ScopedAnimationDurationScaleMode normal_duration_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
KeyboardControllerTest::SetUp();
const gfx::Rect& root_bounds = root_window()->bounds();
keyboard_container()->SetBounds(root_bounds);
root_window()->AddChild(keyboard_container());
controller()->AddObserver(this);
}
virtual void TearDown() OVERRIDE {
controller()->RemoveObserver(this);
KeyboardControllerTest::TearDown();
}
protected:
virtual void OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) OVERRIDE {
notified_bounds_ = new_bounds;
}
const gfx::Rect& notified_bounds() { return notified_bounds_; }
aura::Window* keyboard_container() {
return controller()->GetContainerWindow();
}
aura::Window* keyboard_window() {
return proxy()->GetKeyboardWindow();
}
private:
gfx::Rect notified_bounds_;
DISALLOW_COPY_AND_ASSIGN(KeyboardControllerAnimationTest);
};
TEST_F(KeyboardControllerAnimationTest, ContainerAnimation) {
ui::Layer* layer = keyboard_container()->layer();
ShowKeyboard();
EXPECT_TRUE(keyboard_container()->IsVisible());
EXPECT_TRUE(keyboard_window()->IsVisible());
float show_start_opacity = layer->opacity();
gfx::Transform transform;
transform.Translate(0, keyboard_window()->bounds().height());
EXPECT_EQ(transform, layer->transform());
EXPECT_EQ(gfx::Rect(), notified_bounds());
RunAnimationForLayer(layer);
EXPECT_TRUE(keyboard_container()->IsVisible());
EXPECT_TRUE(keyboard_window()->IsVisible());
float show_end_opacity = layer->opacity();
EXPECT_LT(show_start_opacity, show_end_opacity);
EXPECT_EQ(gfx::Transform(), layer->transform());
EXPECT_EQ(keyboard_window()->bounds(), notified_bounds());
controller()->HideKeyboard(KeyboardController::HIDE_REASON_AUTOMATIC);
EXPECT_TRUE(keyboard_container()->IsVisible());
EXPECT_TRUE(keyboard_container()->layer()->visible());
EXPECT_TRUE(keyboard_window()->IsVisible());
float hide_start_opacity = layer->opacity();
EXPECT_EQ(gfx::Rect(), notified_bounds());
RunAnimationForLayer(layer);
EXPECT_FALSE(keyboard_container()->IsVisible());
EXPECT_FALSE(keyboard_container()->layer()->visible());
EXPECT_FALSE(keyboard_window()->IsVisible());
float hide_end_opacity = layer->opacity();
EXPECT_GT(hide_start_opacity, hide_end_opacity);
EXPECT_EQ(transform, layer->transform());
EXPECT_EQ(gfx::Rect(), notified_bounds());
}
TEST_F(KeyboardControllerAnimationTest, ContainerShowWhileHide) {
ui::Layer* layer = keyboard_container()->layer();
ShowKeyboard();
RunAnimationForLayer(layer);
controller()->HideKeyboard(KeyboardController::HIDE_REASON_AUTOMATIC);
ShowKeyboard();
RunAnimationForLayer(layer);
EXPECT_TRUE(keyboard_container()->IsVisible());
EXPECT_TRUE(keyboard_window()->IsVisible());
EXPECT_EQ(1.0, layer->opacity());
EXPECT_EQ(gfx::Transform(), layer->transform());
}
class KeyboardControllerUsabilityTest : public KeyboardControllerTest {
public:
KeyboardControllerUsabilityTest() {}
virtual ~KeyboardControllerUsabilityTest() {}
virtual void SetUp() OVERRIDE {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kKeyboardUsabilityExperiment);
KeyboardControllerTest::SetUp();
}
private:
DISALLOW_COPY_AND_ASSIGN(KeyboardControllerUsabilityTest);
};
TEST_F(KeyboardControllerUsabilityTest, KeyboardAlwaysVisibleInUsabilityTest) {
const gfx::Rect& root_bounds = root_window()->bounds();
ui::DummyTextInputClient input_client(ui::TEXT_INPUT_TYPE_TEXT);
ui::DummyTextInputClient no_input_client(ui::TEXT_INPUT_TYPE_NONE);
aura::Window* keyboard_container(controller()->GetContainerWindow());
keyboard_container->SetBounds(root_bounds);
root_window()->AddChild(keyboard_container);
SetFocus(&input_client);
EXPECT_TRUE(keyboard_container->IsVisible());
SetFocus(&no_input_client);
EXPECT_TRUE(keyboard_container->IsVisible());
EXPECT_FALSE(WillHideKeyboard());
}
}