#include "ui/wm/core/compound_event_filter.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "ui/aura/client/cursor_client.h"
#include "ui/aura/env.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/test/test_cursor_client.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/test/event_generator.h"
#include "ui/wm/public/activation_client.h"
namespace {
#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_WIN)
base::TimeTicks GetTime() {
return ui::EventTimeForNow();
}
#endif
}
namespace wm {
namespace {
class ConsumeGestureEventFilter : public ui::EventHandler { … };
}
CompoundEventFilterTest;
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(CompoundEventFilterTest, CursorVisibilityChange) {
std::unique_ptr<CompoundEventFilter> compound_filter(new CompoundEventFilter);
aura::Env::GetInstance()->AddPreTargetHandler(compound_filter.get());
aura::test::TestWindowDelegate delegate;
std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
&delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
window->Show();
window->SetCapture();
aura::test::TestCursorClient cursor_client(root_window());
ui::KeyEvent key = ui::KeyEvent::FromCharacter(
'a', ui::VKEY_A, ui::DomCode::NONE, ui::EF_NONE);
DispatchEventUsingWindowDispatcher(&key);
EXPECT_FALSE(cursor_client.IsCursorVisible());
ui::MouseEvent enter(ui::EventType::kMouseEntered, gfx::Point(10, 10),
gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
enter.SetFlags(enter.flags() | ui::EF_IS_SYNTHESIZED);
DispatchEventUsingWindowDispatcher(&enter);
EXPECT_FALSE(cursor_client.IsCursorVisible());
ui::MouseEvent move(ui::EventType::kMouseMoved, gfx::Point(10, 10),
gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
move.SetFlags(enter.flags() | ui::EF_IS_SYNTHESIZED);
DispatchEventUsingWindowDispatcher(&move);
EXPECT_FALSE(cursor_client.IsCursorVisible());
ui::MouseEvent real_move(ui::EventType::kMouseMoved, gfx::Point(10, 10),
gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
DispatchEventUsingWindowDispatcher(&real_move);
EXPECT_TRUE(cursor_client.IsCursorVisible());
cursor_client.set_should_hide_cursor_on_key_event(false);
key = ui::KeyEvent::FromCharacter('a', ui::VKEY_A, ui::DomCode::NONE,
ui::EF_NONE);
DispatchEventUsingWindowDispatcher(&key);
EXPECT_TRUE(cursor_client.IsCursorVisible());
cursor_client.set_should_hide_cursor_on_key_event(true);
key = ui::KeyEvent::FromCharacter('a', ui::VKEY_A, ui::DomCode::NONE,
ui::EF_NONE);
DispatchEventUsingWindowDispatcher(&key);
EXPECT_FALSE(cursor_client.IsCursorVisible());
ui::MouseEvent exit(ui::EventType::kMouseExited, gfx::Point(10, 10),
gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
exit.SetFlags(enter.flags() | ui::EF_IS_SYNTHESIZED);
DispatchEventUsingWindowDispatcher(&exit);
EXPECT_FALSE(cursor_client.IsCursorVisible());
aura::Env::GetInstance()->RemovePreTargetHandler(compound_filter.get());
}
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_WIN)
TEST_F(CompoundEventFilterTest, TouchHidesCursor) {
std::unique_ptr<CompoundEventFilter> compound_filter(new CompoundEventFilter);
aura::Env::GetInstance()->AddPreTargetHandler(compound_filter.get());
aura::test::TestWindowDelegate delegate;
std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
&delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
window->Show();
window->SetCapture();
aura::test::TestCursorClient cursor_client(root_window());
ui::MouseEvent mouse0(ui::EventType::kMouseMoved, gfx::Point(10, 10),
gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
DispatchEventUsingWindowDispatcher(&mouse0);
EXPECT_TRUE(cursor_client.IsMouseEventsEnabled());
EXPECT_TRUE(cursor_client.IsCursorVisible());
ui::TouchEvent press0(ui::EventType::kTouchPressed, gfx::Point(90, 90),
GetTime(),
ui::PointerDetails(ui::EventPointerType::kTouch, 1));
DispatchEventUsingWindowDispatcher(&press0);
EXPECT_FALSE(cursor_client.IsMouseEventsEnabled());
EXPECT_FALSE(cursor_client.IsCursorVisible());
ui::TouchEvent move(ui::EventType::kTouchMoved, gfx::Point(10, 10), GetTime(),
ui::PointerDetails(ui::EventPointerType::kTouch, 1));
DispatchEventUsingWindowDispatcher(&move);
EXPECT_FALSE(cursor_client.IsMouseEventsEnabled());
EXPECT_FALSE(cursor_client.IsCursorVisible());
ui::TouchEvent release(ui::EventType::kTouchReleased, gfx::Point(10, 10),
GetTime(),
ui::PointerDetails(ui::EventPointerType::kTouch, 1));
DispatchEventUsingWindowDispatcher(&release);
EXPECT_FALSE(cursor_client.IsMouseEventsEnabled());
EXPECT_FALSE(cursor_client.IsCursorVisible());
ui::MouseEvent mouse1(ui::EventType::kMouseMoved, gfx::Point(10, 10),
gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
DispatchEventUsingWindowDispatcher(&mouse1);
EXPECT_TRUE(cursor_client.IsMouseEventsEnabled());
EXPECT_TRUE(cursor_client.IsCursorVisible());
ui::TouchEvent press1(ui::EventType::kTouchPressed, gfx::Point(90, 90),
GetTime(),
ui::PointerDetails(ui::EventPointerType::kTouch, 1));
GetActivationClient(root_window())->ActivateWindow(window.get());
DispatchEventUsingWindowDispatcher(&press1);
EXPECT_FALSE(cursor_client.IsMouseEventsEnabled());
EXPECT_FALSE(cursor_client.IsCursorVisible());
aura::Env::GetInstance()->RemovePreTargetHandler(compound_filter.get());
}
#endif
TEST_F(CompoundEventFilterTest, FilterConsumedGesture) { … }
TEST_F(CompoundEventFilterTest, DontHideWhenMouseDown) { … }
#if BUILDFLAG(IS_WIN)
TEST_F(CompoundEventFilterTest, DontShowCursorOnMouseMovesFromTouch) {
std::unique_ptr<CompoundEventFilter> compound_filter(new CompoundEventFilter);
aura::Env::GetInstance()->AddPreTargetHandler(compound_filter.get());
aura::test::TestWindowDelegate delegate;
std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
&delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
window->Show();
window->SetCapture();
aura::test::TestCursorClient cursor_client(root_window());
cursor_client.DisableMouseEvents();
EXPECT_FALSE(cursor_client.IsMouseEventsEnabled());
ui::MouseEvent mouse0(ui::EventType::kMouseMoved, gfx::Point(10, 10),
gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
mouse0.SetFlags(mouse0.flags() | ui::EF_FROM_TOUCH);
DispatchEventUsingWindowDispatcher(&mouse0);
EXPECT_FALSE(cursor_client.IsMouseEventsEnabled());
mouse0.SetFlags(mouse0.flags() & ~ui::EF_FROM_TOUCH);
DispatchEventUsingWindowDispatcher(&mouse0);
EXPECT_TRUE(cursor_client.IsMouseEventsEnabled());
aura::Env::GetInstance()->RemovePreTargetHandler(compound_filter.get());
}
#endif
}