123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902 |
- // Copyright 2015 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "components/exo/pointer.h"
- #include "ash/constants/app_types.h"
- #include "ash/public/cpp/shell_window_ids.h"
- #include "ash/shell.h"
- #include "ash/wm/desks/desks_util.h"
- #include "ash/wm/window_positioning_utils.h"
- #include "base/bind.h"
- #include "base/run_loop.h"
- #include "base/test/scoped_feature_list.h"
- #include "build/chromeos_buildflags.h"
- #include "chromeos/ui/base/window_properties.h"
- #include "components/exo/buffer.h"
- #include "components/exo/data_source.h"
- #include "components/exo/data_source_delegate.h"
- #include "components/exo/pointer_constraint_delegate.h"
- #include "components/exo/pointer_delegate.h"
- #include "components/exo/pointer_stylus_delegate.h"
- #include "components/exo/relative_pointer_delegate.h"
- #include "components/exo/seat.h"
- #include "components/exo/shell_surface.h"
- #include "components/exo/sub_surface.h"
- #include "components/exo/surface.h"
- #include "components/exo/test/exo_test_base.h"
- #include "components/exo/test/exo_test_data_exchange_delegate.h"
- #include "components/exo/test/exo_test_helper.h"
- #include "components/exo/test/mock_security_delegate.h"
- #include "components/exo/test/shell_surface_builder.h"
- #include "components/viz/common/quads/compositor_frame.h"
- #include "components/viz/service/surfaces/surface.h"
- #include "components/viz/service/surfaces/surface_manager.h"
- #include "testing/gmock/include/gmock/gmock.h"
- #include "ui/aura/client/aura_constants.h"
- #include "ui/aura/client/cursor_client.h"
- #include "ui/aura/client/focus_client.h"
- #include "ui/base/cursor/mojom/cursor_type.mojom-shared.h"
- #include "ui/base/dragdrop/mojom/drag_drop_types.mojom-shared.h"
- #include "ui/events/base_event_utils.h"
- #include "ui/events/event.h"
- #include "ui/events/event_utils.h"
- #include "ui/events/test/event_generator.h"
- #include "ui/events/types/event_type.h"
- #include "ui/gfx/geometry/vector2d_f.h"
- #include "ui/views/widget/widget.h"
- #if BUILDFLAG(IS_CHROMEOS_ASH)
- #include "ash/constants/ash_features.h"
- #include "ash/drag_drop/drag_drop_controller.h"
- #include "base/test/bind.h"
- #include "components/exo/wm_helper.h"
- #include "ui/aura/client/drag_drop_client.h"
- #include "ui/aura/window_tree_host.h"
- #include "ui/events/test/events_test_utils.h"
- #endif
- using ::testing::_;
- using ::testing::AnyNumber;
- namespace exo {
- namespace {
- void DispatchGesture(ui::EventType gesture_type, gfx::Point location) {
- ui::GestureEventDetails event_details(gesture_type);
- ui::GestureEvent gesture_event(location.x(), location.y(), 0,
- ui::EventTimeForNow(), event_details);
- ui::EventSource* event_source =
- ash::Shell::GetPrimaryRootWindow()->GetHost()->GetEventSource();
- ui::EventSourceTestApi event_source_test(event_source);
- ui::EventDispatchDetails details =
- event_source_test.SendEventToSink(&gesture_event);
- CHECK(!details.dispatcher_destroyed);
- }
- class MockPointerDelegate : public PointerDelegate {
- public:
- MockPointerDelegate() {}
- // Overridden from PointerDelegate:
- MOCK_METHOD1(OnPointerDestroying, void(Pointer*));
- MOCK_CONST_METHOD1(CanAcceptPointerEventsForSurface, bool(Surface*));
- MOCK_METHOD3(OnPointerEnter, void(Surface*, const gfx::PointF&, int));
- MOCK_METHOD1(OnPointerLeave, void(Surface*));
- MOCK_METHOD2(OnPointerMotion, void(base::TimeTicks, const gfx::PointF&));
- MOCK_METHOD3(OnPointerButton, void(base::TimeTicks, int, bool));
- MOCK_METHOD3(OnPointerScroll,
- void(base::TimeTicks, const gfx::Vector2dF&, bool));
- MOCK_METHOD1(OnPointerScrollStop, void(base::TimeTicks));
- MOCK_METHOD0(OnPointerFrame, void());
- };
- class MockRelativePointerDelegate : public RelativePointerDelegate {
- public:
- MockRelativePointerDelegate() = default;
- ~MockRelativePointerDelegate() = default;
- // Overridden from RelativePointerDelegate:
- MOCK_METHOD1(OnPointerDestroying, void(Pointer*));
- MOCK_METHOD3(OnPointerRelativeMotion,
- void(base::TimeTicks,
- const gfx::Vector2dF&,
- const gfx::Vector2dF&));
- };
- class MockPointerConstraintDelegate : public PointerConstraintDelegate {
- public:
- MockPointerConstraintDelegate() {
- ON_CALL(*this, OnConstraintActivated).WillByDefault([this]() {
- activated_count++;
- });
- ON_CALL(*this, OnConstraintBroken).WillByDefault([this]() {
- broken_count++;
- });
- }
- ~MockPointerConstraintDelegate() = default;
- // Overridden from PointerConstraintDelegate:
- MOCK_METHOD0(OnConstraintActivated, void());
- MOCK_METHOD0(OnAlreadyConstrained, void());
- MOCK_METHOD0(OnConstraintBroken, void());
- MOCK_METHOD0(IsPersistent, bool());
- MOCK_METHOD0(GetConstrainedSurface, Surface*());
- MOCK_METHOD0(OnDefunct, void());
- int activated_count = 0;
- int broken_count = 0;
- };
- class MockPointerStylusDelegate : public PointerStylusDelegate {
- public:
- MockPointerStylusDelegate() {}
- // Overridden from PointerStylusDelegate:
- MOCK_METHOD(void, OnPointerDestroying, (Pointer*));
- MOCK_METHOD(void, OnPointerToolChange, (ui::EventPointerType));
- MOCK_METHOD(void, OnPointerForce, (base::TimeTicks, float));
- MOCK_METHOD(void, OnPointerTilt, (base::TimeTicks, const gfx::Vector2dF&));
- };
- class TestDataSourceDelegate : public DataSourceDelegate {
- public:
- TestDataSourceDelegate() {}
- TestDataSourceDelegate(const TestDataSourceDelegate&) = delete;
- TestDataSourceDelegate& operator=(const TestDataSourceDelegate&) = delete;
- // Overridden from DataSourceDelegate:
- void OnDataSourceDestroying(DataSource* device) override {}
- void OnTarget(const absl::optional<std::string>& mime_type) override {}
- void OnSend(const std::string& mime_type, base::ScopedFD fd) override {}
- void OnCancelled() override {}
- void OnDndDropPerformed() override {}
- void OnDndFinished() override {}
- void OnAction(DndAction dnd_action) override {}
- bool CanAcceptDataEventsForSurface(Surface* surface) const override {
- return true;
- }
- };
- class PointerTest : public test::ExoTestBase {
- public:
- PointerTest() = default;
- PointerTest(const PointerTest&) = delete;
- PointerTest& operator=(const PointerTest&) = delete;
- void SetUp() override {
- test::ExoTestBase::SetUp();
- // Sometimes underlying infra (i.e. X11 / Xvfb) may emit pointer events
- // which can break MockPointerDelegate's expectations, so they should be
- // consumed before starting. See https://crbug.com/854674.
- base::RunLoop().RunUntilIdle();
- }
- };
- #if BUILDFLAG(IS_CHROMEOS_ASH)
- class PointerConstraintTest : public PointerTest {
- public:
- PointerConstraintTest() = default;
- PointerConstraintTest(const PointerConstraintTest&) = delete;
- PointerConstraintTest& operator=(const PointerConstraintTest&) = delete;
- void SetUp() override {
- PointerTest::SetUp();
- shell_surface_ = BuildShellSurfaceWhichPermitsPointerLock();
- surface_ = shell_surface_->surface_for_testing();
- seat_ = std::make_unique<Seat>();
- pointer_ = std::make_unique<Pointer>(&delegate_, seat_.get());
- focus_client_ =
- aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
- focus_client_->FocusWindow(surface_->window());
- generator_ = std::make_unique<ui::test::EventGenerator>(
- ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate_, CanAcceptPointerEventsForSurface(surface_))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(constraint_delegate_, GetConstrainedSurface())
- .WillRepeatedly(testing::Return(surface_));
- }
- void TearDown() override {
- // Many objects need to be destroyed before teardown for various reasons.
- seat_.reset();
- shell_surface_.reset();
- surface_ = nullptr;
- // Some tests generate mouse events which Pointer::OnMouseEvent() handles
- // during the run loop. That routine accesses WMHelper. So, make sure any
- // such pending tasks finish before TearDown() destroys the WMHelper.
- base::RunLoop().RunUntilIdle();
- PointerTest::TearDown();
- }
- std::unique_ptr<ShellSurface> BuildShellSurfaceWhichPermitsPointerLock() {
- std::unique_ptr<ShellSurface> shell_surface =
- test::ShellSurfaceBuilder({10, 10}).BuildShellSurface();
- shell_surface->GetWidget()->GetNativeWindow()->SetProperty(
- chromeos::kUseOverviewToExitPointerLock, true);
- return shell_surface;
- }
- std::unique_ptr<ui::test::EventGenerator> generator_;
- std::unique_ptr<Pointer> pointer_;
- std::unique_ptr<Seat> seat_;
- testing::NiceMock<MockPointerConstraintDelegate> constraint_delegate_;
- testing::NiceMock<MockPointerDelegate> delegate_;
- std::unique_ptr<ShellSurface> shell_surface_;
- Surface* surface_;
- aura::client::FocusClient* focus_client_;
- };
- #endif
- TEST_F(PointerTest, SetCursor) {
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerFrame()).Times(1);
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin());
- std::unique_ptr<Surface> pointer_surface(new Surface);
- std::unique_ptr<Buffer> pointer_buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- pointer_surface->Attach(pointer_buffer.get());
- pointer_surface->Commit();
- // Set pointer surface.
- pointer->SetCursor(pointer_surface.get(), gfx::Point(5, 5));
- base::RunLoop().RunUntilIdle();
- const viz::CompositorRenderPass* last_render_pass;
- {
- viz::SurfaceId surface_id = pointer->host_window()->GetSurfaceId();
- viz::SurfaceManager* surface_manager = GetSurfaceManager();
- ASSERT_TRUE(surface_manager->GetSurfaceForId(surface_id)->HasActiveFrame());
- const viz::CompositorFrame& frame =
- surface_manager->GetSurfaceForId(surface_id)->GetActiveFrame();
- EXPECT_EQ(gfx::Rect(0, 0, 10, 10),
- frame.render_pass_list.back()->output_rect);
- last_render_pass = frame.render_pass_list.back().get();
- }
- // Adjust hotspot.
- pointer->SetCursor(pointer_surface.get(), gfx::Point());
- base::RunLoop().RunUntilIdle();
- // Verify that adjustment to hotspot resulted in new frame.
- {
- viz::SurfaceId surface_id = pointer->host_window()->GetSurfaceId();
- viz::SurfaceManager* surface_manager = GetSurfaceManager();
- ASSERT_TRUE(surface_manager->GetSurfaceForId(surface_id)->HasActiveFrame());
- const viz::CompositorFrame& frame =
- surface_manager->GetSurfaceForId(surface_id)->GetActiveFrame();
- EXPECT_TRUE(frame.render_pass_list.back().get() != last_render_pass);
- }
- // Unset pointer surface.
- pointer->SetCursor(nullptr, gfx::Point());
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, SetCursorNull) {
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerFrame()).Times(1);
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin());
- pointer->SetCursor(nullptr, gfx::Point());
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(nullptr, pointer->root_surface());
- aura::client::CursorClient* cursor_client = aura::client::GetCursorClient(
- shell_surface->GetWidget()->GetNativeWindow()->GetRootWindow());
- EXPECT_EQ(ui::mojom::CursorType::kNone, cursor_client->GetCursor().type());
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, SetCursorType) {
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerFrame()).Times(1);
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin());
- pointer->SetCursorType(ui::mojom::CursorType::kIBeam);
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(nullptr, pointer->root_surface());
- aura::client::CursorClient* cursor_client = aura::client::GetCursorClient(
- shell_surface->GetWidget()->GetNativeWindow()->GetRootWindow());
- EXPECT_EQ(ui::mojom::CursorType::kIBeam, cursor_client->GetCursor().type());
- // Set the pointer with surface after setting pointer type.
- std::unique_ptr<Surface> pointer_surface(new Surface);
- std::unique_ptr<Buffer> pointer_buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- pointer_surface->Attach(pointer_buffer.get());
- pointer_surface->Commit();
- pointer->SetCursor(pointer_surface.get(), gfx::Point());
- base::RunLoop().RunUntilIdle();
- {
- viz::SurfaceId surface_id = pointer->host_window()->GetSurfaceId();
- viz::SurfaceManager* surface_manager = GetSurfaceManager();
- ASSERT_TRUE(surface_manager->GetSurfaceForId(surface_id)->HasActiveFrame());
- const viz::CompositorFrame& frame =
- surface_manager->GetSurfaceForId(surface_id)->GetActiveFrame();
- EXPECT_EQ(gfx::Rect(0, 0, 10, 10),
- frame.render_pass_list.back()->output_rect);
- }
- // Set the pointer type after the pointer surface is specified.
- pointer->SetCursorType(ui::mojom::CursorType::kCross);
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(nullptr, pointer->root_surface());
- EXPECT_EQ(ui::mojom::CursorType::kCross, cursor_client->GetCursor().type());
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, SetCursorTypeOutsideOfSurface) {
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin() -
- gfx::Vector2d(1, 1));
- pointer->SetCursorType(ui::mojom::CursorType::kIBeam);
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(nullptr, pointer->root_surface());
- aura::client::CursorClient* cursor_client = aura::client::GetCursorClient(
- shell_surface->GetWidget()->GetNativeWindow()->GetRootWindow());
- // The cursor type shouldn't be the specified one, since the pointer is
- // located outside of the surface.
- EXPECT_NE(ui::mojom::CursorType::kIBeam, cursor_client->GetCursor().type());
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, SetCursorAndSetCursorType) {
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerFrame()).Times(1);
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin());
- std::unique_ptr<Surface> pointer_surface(new Surface);
- std::unique_ptr<Buffer> pointer_buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- pointer_surface->Attach(pointer_buffer.get());
- pointer_surface->Commit();
- // Set pointer surface.
- pointer->SetCursor(pointer_surface.get(), gfx::Point());
- EXPECT_EQ(1u, pointer->GetActivePresentationCallbacksForTesting().size());
- base::RunLoop().RunUntilIdle();
- {
- viz::SurfaceId surface_id = pointer->host_window()->GetSurfaceId();
- viz::SurfaceManager* surface_manager = GetSurfaceManager();
- ASSERT_TRUE(surface_manager->GetSurfaceForId(surface_id)->HasActiveFrame());
- const viz::CompositorFrame& frame =
- surface_manager->GetSurfaceForId(surface_id)->GetActiveFrame();
- EXPECT_EQ(gfx::Rect(0, 0, 10, 10),
- frame.render_pass_list.back()->output_rect);
- }
- // Set the cursor type to the kNone through SetCursorType.
- pointer->SetCursorType(ui::mojom::CursorType::kNone);
- EXPECT_TRUE(pointer->GetActivePresentationCallbacksForTesting().empty());
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(nullptr, pointer->root_surface());
- // Set the same pointer surface again.
- pointer->SetCursor(pointer_surface.get(), gfx::Point());
- EXPECT_EQ(1u, pointer->GetActivePresentationCallbacksForTesting().size());
- auto& list =
- pointer->GetActivePresentationCallbacksForTesting().begin()->second;
- base::RunLoop runloop;
- list.push_back(base::BindRepeating(
- [](base::RepeatingClosure callback, const gfx::PresentationFeedback&) {
- callback.Run();
- },
- runloop.QuitClosure()));
- runloop.Run();
- {
- viz::SurfaceId surface_id = pointer->host_window()->GetSurfaceId();
- viz::SurfaceManager* surface_manager = GetSurfaceManager();
- ASSERT_TRUE(surface_manager->GetSurfaceForId(surface_id)->HasActiveFrame());
- const viz::CompositorFrame& frame =
- surface_manager->GetSurfaceForId(surface_id)->GetActiveFrame();
- EXPECT_EQ(gfx::Rect(0, 0, 10, 10),
- frame.render_pass_list.back()->output_rect);
- }
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, SetCursorNullAndSetCursorType) {
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerFrame()).Times(1);
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin());
- // Set nullptr surface.
- pointer->SetCursor(nullptr, gfx::Point());
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(nullptr, pointer->root_surface());
- aura::client::CursorClient* cursor_client = aura::client::GetCursorClient(
- shell_surface->GetWidget()->GetNativeWindow()->GetRootWindow());
- EXPECT_EQ(ui::mojom::CursorType::kNone, cursor_client->GetCursor().type());
- // Set the cursor type.
- pointer->SetCursorType(ui::mojom::CursorType::kIBeam);
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(nullptr, pointer->root_surface());
- EXPECT_EQ(ui::mojom::CursorType::kIBeam, cursor_client->GetCursor().type());
- // Set nullptr surface again.
- pointer->SetCursor(nullptr, gfx::Point());
- base::RunLoop().RunUntilIdle();
- EXPECT_EQ(nullptr, pointer->root_surface());
- EXPECT_EQ(ui::mojom::CursorType::kNone, cursor_client->GetCursor().type());
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, OnPointerEnter) {
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerFrame()).Times(1);
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, OnPointerLeave) {
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerFrame()).Times(4);
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(delegate, OnPointerLeave(surface.get()));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().bottom_right());
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(delegate, OnPointerLeave(surface.get()));
- shell_surface.reset();
- surface.reset();
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, OnPointerMotion) {
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerFrame()).Times(6);
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(delegate, OnPointerMotion(testing::_, gfx::PointF(1, 1)));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin() +
- gfx::Vector2d(1, 1));
- std::unique_ptr<Surface> sub_surface(new Surface);
- std::unique_ptr<SubSurface> sub(
- new SubSurface(sub_surface.get(), surface.get()));
- surface->SetSubSurfacePosition(sub_surface.get(), gfx::PointF(5, 5));
- gfx::Size sub_buffer_size(5, 5);
- std::unique_ptr<Buffer> sub_buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(sub_buffer_size)));
- sub_surface->Attach(sub_buffer.get());
- sub_surface->Commit();
- surface->Commit();
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(sub_surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerLeave(surface.get()));
- EXPECT_CALL(delegate, OnPointerEnter(sub_surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(sub_surface->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(delegate, OnPointerMotion(testing::_, gfx::PointF(1, 1)));
- generator.MoveMouseTo(sub_surface->window()->GetBoundsInScreen().origin() +
- gfx::Vector2d(1, 1));
- std::unique_ptr<Surface> child_surface(new Surface);
- std::unique_ptr<ShellSurface> child_shell_surface(new ShellSurface(
- child_surface.get(), gfx::Point(9, 9), /*can_minimize=*/false,
- ash::desks_util::GetActiveDeskContainerId()));
- child_shell_surface->DisableMovement();
- child_shell_surface->SetParent(shell_surface.get());
- gfx::Size child_buffer_size(15, 15);
- std::unique_ptr<Buffer> child_buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(child_buffer_size)));
- child_surface->Attach(child_buffer.get());
- child_surface->Commit();
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(child_surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerLeave(sub_surface.get()));
- EXPECT_CALL(delegate, OnPointerEnter(child_surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(child_surface->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(delegate, OnPointerMotion(testing::_, gfx::PointF(10, 10)));
- generator.MoveMouseTo(child_surface->window()->GetBoundsInScreen().origin() +
- gfx::Vector2d(10, 10));
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, OnPointerButton) {
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerFrame()).Times(3);
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(delegate,
- OnPointerButton(testing::_, ui::EF_LEFT_MOUSE_BUTTON, true));
- EXPECT_CALL(delegate,
- OnPointerButton(testing::_, ui::EF_LEFT_MOUSE_BUTTON, false));
- generator.ClickLeftButton();
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, OnPointerScroll) {
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- gfx::Point location = surface->window()->GetBoundsInScreen().origin();
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerFrame()).Times(3);
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(location);
- {
- // Expect fling stop followed by scroll and scroll stop.
- testing::InSequence sequence;
- EXPECT_CALL(delegate,
- OnPointerScroll(testing::_, gfx::Vector2dF(1.2, 1.2), false));
- EXPECT_CALL(delegate, OnPointerScrollStop(testing::_));
- }
- generator.ScrollSequence(location, base::TimeDelta(), 1, 1, 1, 1);
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, OnPointerScrollWithThreeFinger) {
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- gfx::Point location = surface->window()->GetBoundsInScreen().origin();
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerFrame()).Times(2);
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(location);
- {
- // Expect no scroll.
- testing::InSequence sequence;
- EXPECT_CALL(delegate, OnPointerScrollStop(testing::_));
- }
- // Three fingers scroll.
- generator.ScrollSequence(location, base::TimeDelta(), 1, 1, 1,
- 3 /* num_fingers */);
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, OnPointerScrollDiscrete) {
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerFrame()).Times(2);
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(delegate,
- OnPointerScroll(testing::_, gfx::Vector2dF(1, 1), true));
- generator.MoveMouseWheel(1, 1);
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, RegisterPointerEventsOnModal) {
- // Create modal surface.
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(
- new ShellSurface(surface.get(), gfx::Point(), /*can_minimize=*/false,
- ash::kShellWindowId_SystemModalContainer));
- shell_surface->DisableMovement();
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(gfx::Size(5, 5))));
- surface->Attach(buffer.get());
- surface->Commit();
- ash::CenterWindow(shell_surface->GetWidget()->GetNativeWindow());
- // Make the window modal.
- shell_surface->SetSystemModal(true);
- EXPECT_TRUE(ash::Shell::IsSystemModalWindowOpen());
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, OnPointerFrame()).Times(testing::AnyNumber());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- // Pointer events on modal window should be registered.
- gfx::Point origin = surface->window()->GetBoundsInScreen().origin();
- {
- testing::InSequence sequence;
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(origin);
- EXPECT_CALL(delegate, OnPointerMotion(testing::_, gfx::PointF(1, 1)));
- generator.MoveMouseTo(origin + gfx::Vector2d(1, 1));
- EXPECT_CALL(delegate,
- OnPointerButton(testing::_, ui::EF_LEFT_MOUSE_BUTTON, true));
- EXPECT_CALL(delegate,
- OnPointerButton(testing::_, ui::EF_LEFT_MOUSE_BUTTON, false));
- generator.ClickLeftButton();
- EXPECT_CALL(delegate,
- OnPointerScroll(testing::_, gfx::Vector2dF(1.2, 1.2), false));
- EXPECT_CALL(delegate, OnPointerScrollStop(testing::_));
- generator.ScrollSequence(origin, base::TimeDelta(), 1, 1, 1, 1);
- }
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, IgnorePointerEventsOnNonModalWhenModalIsOpen) {
- // Create surface for non-modal window.
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(gfx::Size(10, 10))));
- surface->Attach(buffer.get());
- surface->Commit();
- // Create surface for modal window.
- std::unique_ptr<Surface> surface2(new Surface);
- std::unique_ptr<ShellSurface> shell_surface2(
- new ShellSurface(surface2.get(), gfx::Point(), /*can_minimize=*/false,
- ash::kShellWindowId_SystemModalContainer));
- shell_surface2->DisableMovement();
- std::unique_ptr<Buffer> buffer2(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(gfx::Size(5, 5))));
- surface2->Attach(buffer2.get());
- surface2->Commit();
- ash::CenterWindow(shell_surface2->GetWidget()->GetNativeWindow());
- // Make the window modal.
- shell_surface2->SetSystemModal(true);
- EXPECT_TRUE(ash::Shell::IsSystemModalWindowOpen());
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, OnPointerFrame()).Times(testing::AnyNumber());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface2.get()))
- .WillRepeatedly(testing::Return(true));
- // Check if pointer events on non-modal window are ignored.
- gfx::Point nonModalOrigin = surface->window()->GetBoundsInScreen().origin();
- {
- testing::InSequence sequence;
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0))
- .Times(0);
- generator.MoveMouseTo(nonModalOrigin);
- EXPECT_CALL(delegate, OnPointerMotion(testing::_, gfx::PointF(1, 1)))
- .Times(0);
- generator.MoveMouseTo(nonModalOrigin + gfx::Vector2d(1, 1));
- EXPECT_CALL(delegate,
- OnPointerButton(testing::_, ui::EF_LEFT_MOUSE_BUTTON, true))
- .Times(0);
- EXPECT_CALL(delegate,
- OnPointerButton(testing::_, ui::EF_LEFT_MOUSE_BUTTON, false))
- .Times(0);
- generator.ClickLeftButton();
- EXPECT_CALL(delegate,
- OnPointerScroll(testing::_, gfx::Vector2dF(1.2, 1.2), false))
- .Times(0);
- EXPECT_CALL(delegate, OnPointerScrollStop(testing::_)).Times(0);
- generator.ScrollSequence(nonModalOrigin, base::TimeDelta(), 1, 1, 1, 1);
- EXPECT_CALL(delegate, OnPointerLeave(surface.get())).Times(0);
- generator.MoveMouseTo(
- surface->window()->GetBoundsInScreen().bottom_right());
- }
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, IgnorePointerLeaveOnModal) {
- // Create modal surface.
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(
- new ShellSurface(surface.get(), gfx::Point(), /*can_minimize=*/false,
- ash::kShellWindowId_SystemModalContainer));
- shell_surface->DisableMovement();
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(gfx::Size(5, 5))));
- surface->Attach(buffer.get());
- surface->Commit();
- ash::CenterWindow(shell_surface->GetWidget()->GetNativeWindow());
- // Make the window modal.
- shell_surface->SetSystemModal(true);
- EXPECT_TRUE(ash::Shell::IsSystemModalWindowOpen());
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, OnPointerFrame()).Times(testing::AnyNumber());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- gfx::Point origin = surface->window()->GetBoundsInScreen().origin();
- {
- testing::InSequence sequence;
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(origin);
- // OnPointerLeave should not be called on the modal surface when the pointer
- // moves out of its bounds.
- EXPECT_CALL(delegate, OnPointerLeave(surface.get())).Times(0);
- generator.MoveMouseTo(
- surface->window()->GetBoundsInScreen().bottom_right());
- }
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, RegisterPointerEventsOnNonModal) {
- // Create surface for non-modal window.
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(gfx::Size(10, 10))));
- surface->Attach(buffer.get());
- surface->Commit();
- // Create another surface for a non-modal window.
- std::unique_ptr<Surface> surface2(new Surface);
- std::unique_ptr<ShellSurface> shell_surface2(
- new ShellSurface(surface2.get(), gfx::Point(), /*can_minimize=*/false,
- ash::kShellWindowId_SystemModalContainer));
- shell_surface2->DisableMovement();
- std::unique_ptr<Buffer> buffer2(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(gfx::Size(5, 5))));
- surface2->Attach(buffer2.get());
- surface2->Commit();
- ash::CenterWindow(shell_surface2->GetWidget()->GetNativeWindow());
- MockPointerDelegate delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, OnPointerFrame()).Times(testing::AnyNumber());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface2.get()))
- .WillRepeatedly(testing::Return(true));
- // Ensure second window is non-modal.
- shell_surface2->SetSystemModal(false);
- EXPECT_FALSE(ash::Shell::IsSystemModalWindowOpen());
- // Check if pointer events on first non-modal window are registered.
- gfx::Point firstWindowOrigin =
- surface->window()->GetBoundsInScreen().origin();
- {
- testing::InSequence sequence;
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(firstWindowOrigin);
- EXPECT_CALL(delegate, OnPointerMotion(testing::_, gfx::PointF(1, 1)));
- generator.MoveMouseTo(firstWindowOrigin + gfx::Vector2d(1, 1));
- EXPECT_CALL(delegate,
- OnPointerButton(testing::_, ui::EF_LEFT_MOUSE_BUTTON, true));
- EXPECT_CALL(delegate,
- OnPointerButton(testing::_, ui::EF_LEFT_MOUSE_BUTTON, false));
- generator.ClickLeftButton();
- EXPECT_CALL(delegate,
- OnPointerScroll(testing::_, gfx::Vector2dF(1.2, 1.2), false));
- EXPECT_CALL(delegate, OnPointerScrollStop(testing::_));
- generator.ScrollSequence(firstWindowOrigin, base::TimeDelta(), 1, 1, 1, 1);
- EXPECT_CALL(delegate, OnPointerLeave(surface.get()));
- generator.MoveMouseTo(
- surface->window()->GetBoundsInScreen().bottom_right());
- }
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, DragDropAbort) {
- Seat seat(std::make_unique<TestDataExchangeDelegate>());
- MockPointerDelegate pointer_delegate;
- std::unique_ptr<Pointer> pointer(new Pointer(&pointer_delegate, &seat));
- TestDataSourceDelegate data_source_delegate;
- DataSource source(&data_source_delegate);
- Surface origin, icon;
- // Make origin into a real window so the pointer can click it
- ShellSurface shell_surface(&origin);
- Buffer buffer(exo_test_helper()->CreateGpuMemoryBuffer(gfx::Size(10, 10)));
- origin.Attach(&buffer);
- origin.Commit();
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(pointer_delegate, CanAcceptPointerEventsForSurface(&origin))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(pointer_delegate, OnPointerFrame()).Times(3);
- EXPECT_CALL(pointer_delegate, OnPointerEnter(&origin, gfx::PointF(), 0));
- generator.MoveMouseTo(origin.window()->GetBoundsInScreen().origin());
- seat.StartDrag(&source, &origin, &icon, ui::mojom::DragEventSource::kMouse);
- EXPECT_TRUE(seat.get_drag_drop_operation_for_testing());
- EXPECT_CALL(pointer_delegate, OnPointerButton).Times(2);
- generator.PressLeftButton();
- EXPECT_TRUE(seat.get_drag_drop_operation_for_testing());
- generator.ReleaseLeftButton();
- EXPECT_FALSE(seat.get_drag_drop_operation_for_testing());
- EXPECT_CALL(pointer_delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- #if BUILDFLAG(IS_CHROMEOS_ASH)
- TEST_F(PointerTest, DragDropAndPointerEnterLeaveEvents) {
- Seat seat(std::make_unique<TestDataExchangeDelegate>());
- MockPointerDelegate pointer_delegate;
- std::unique_ptr<Pointer> pointer(new Pointer(&pointer_delegate, &seat));
- TestDataSourceDelegate data_source_delegate;
- DataSource source(&data_source_delegate);
- Surface origin;
- // Make origin into a real window so the pointer can click it
- ShellSurface shell_surface(&origin);
- Buffer buffer(exo_test_helper()->CreateGpuMemoryBuffer(gfx::Size(10, 10)));
- origin.Attach(&buffer);
- origin.Commit();
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(pointer_delegate, CanAcceptPointerEventsForSurface(&origin))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(pointer_delegate, OnPointerFrame()).Times(AnyNumber());
- EXPECT_CALL(pointer_delegate, OnPointerEnter(&origin, gfx::PointF(), 0));
- generator.MoveMouseTo(origin.window()->GetBoundsInScreen().origin());
- auto* drag_drop_controller = static_cast<ash::DragDropController*>(
- aura::client::GetDragDropClient(ash::Shell::GetPrimaryRootWindow()));
- ASSERT_TRUE(drag_drop_controller);
- generator.PressLeftButton();
- seat.StartDrag(&source, &origin, /*icon=*/nullptr,
- ui::mojom::DragEventSource::kMouse);
- EXPECT_TRUE(seat.get_drag_drop_operation_for_testing());
- // As soon as the runloop gets triggered, emit a mouse release event.
- drag_drop_controller->SetLoopClosureForTesting(
- base::BindLambdaForTesting([&]() {
- EXPECT_CALL(pointer_delegate, OnPointerEnter(_, _, _));
- generator.ReleaseLeftButton();
- }),
- base::DoNothing());
- EXPECT_CALL(pointer_delegate, OnPointerLeave(_));
- base::RunLoop().RunUntilIdle();
- EXPECT_CALL(pointer_delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, DragDropAndPointerEnterLeaveEvents_NoOpOnTouchDrag) {
- Seat seat(std::make_unique<TestDataExchangeDelegate>());
- MockPointerDelegate pointer_delegate;
- std::unique_ptr<Pointer> pointer(new Pointer(&pointer_delegate, &seat));
- TestDataSourceDelegate data_source_delegate;
- DataSource source(&data_source_delegate);
- Surface origin;
- // Make origin into a real window so the pointer can click it
- ShellSurface shell_surface(&origin);
- Buffer buffer(exo_test_helper()->CreateGpuMemoryBuffer(gfx::Size(10, 10)));
- origin.Attach(&buffer);
- origin.Commit();
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(pointer_delegate, CanAcceptPointerEventsForSurface(&origin))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(pointer_delegate, OnPointerFrame()).Times(AnyNumber());
- EXPECT_CALL(pointer_delegate, OnPointerEnter(&origin, gfx::PointF(), 0));
- generator.MoveMouseTo(origin.window()->GetBoundsInScreen().origin());
- auto* drag_drop_controller = static_cast<ash::DragDropController*>(
- aura::client::GetDragDropClient(ash::Shell::GetPrimaryRootWindow()));
- ASSERT_TRUE(drag_drop_controller);
- seat.StartDrag(&source, &origin, /*icon=*/nullptr,
- ui::mojom::DragEventSource::kTouch);
- EXPECT_TRUE(seat.get_drag_drop_operation_for_testing());
- // Initiate the gesture sequence.
- DispatchGesture(ui::ET_GESTURE_BEGIN, gfx::Point(10, 10));
- // As soon as the runloop gets triggered, emit a mouse release event.
- drag_drop_controller->SetLoopClosureForTesting(
- base::BindLambdaForTesting([&]() {
- EXPECT_CALL(pointer_delegate, OnPointerEnter(_, _, _)).Times(0);
- // generator.ReleaseLeftButton();
- generator.set_current_screen_location(gfx::Point(10, 10));
- generator.PressMoveAndReleaseTouchBy(50, 50);
- }),
- base::DoNothing());
- EXPECT_CALL(pointer_delegate, OnPointerLeave(_)).Times(0);
- base::RunLoop().RunUntilIdle();
- EXPECT_CALL(pointer_delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- TEST_F(PointerTest, IgnoresHandledEvents) {
- // A very dumb handler that simply marks all events as handled. This is needed
- // allows us to mark a mouse event as handled as it gets processed by the
- // event processor.
- class SetHandledHandler : public ui::EventHandler {
- void OnMouseEvent(ui::MouseEvent* event) override { event->SetHandled(); }
- };
- SetHandledHandler handler;
- ash::Shell::Get()->AddPreTargetHandler(&handler);
- Seat seat(std::make_unique<TestDataExchangeDelegate>());
- testing::NiceMock<MockPointerDelegate> pointer_delegate;
- std::unique_ptr<Pointer> pointer(new Pointer(&pointer_delegate, &seat));
- // Make origin into a real window so the touch can click it
- std::unique_ptr<ShellSurface> shell_surface =
- test::ShellSurfaceBuilder({10, 10}).BuildShellSurface();
- EXPECT_CALL(pointer_delegate, CanAcceptPointerEventsForSurface(testing::_))
- .WillRepeatedly(testing::Return(true));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- // The SetHandlerHandler should have marked the event as processed. Therefore
- // the event should simply be ignored.
- EXPECT_CALL(pointer_delegate,
- OnPointerButton(testing::_, testing::_, testing::_))
- .Times(0);
- // This event should be ignored because it has already been handled.
- auto window_point = shell_surface->surface_for_testing()
- ->window()
- ->GetBoundsInScreen()
- .CenterPoint();
- generator.MoveMouseTo(window_point);
- generator.ClickLeftButton();
- ash::Shell::Get()->RemovePreTargetHandler(&handler);
- }
- namespace {
- class PointerDragDropObserver : public WMHelper::DragDropObserver {
- public:
- PointerDragDropObserver(DropCallback closure)
- : closure_(std::move(closure)) {}
- private:
- // WMHelper::DragDropObserver overrides:
- void OnDragEntered(const ui::DropTargetEvent& event) override {}
- aura::client::DragUpdateInfo OnDragUpdated(
- const ui::DropTargetEvent& event) override {
- return aura::client::DragUpdateInfo();
- }
- void OnDragExited() override {}
- DropCallback GetDropCallback() override { return std::move(closure_); }
- DropCallback closure_;
- };
- } // namespace
- // Test for crbug.com/1307143: It ensures no "pointer enter" event is
- // processed in case the target surface is destroyed during the drop action.
- TEST_F(PointerTest,
- DragDropAndPointerEnterLeaveEvents_NoEnterOnSurfaceDestroy) {
- Seat seat(std::make_unique<TestDataExchangeDelegate>());
- MockPointerDelegate pointer_delegate;
- std::unique_ptr<Pointer> pointer(new Pointer(&pointer_delegate, &seat));
- TestDataSourceDelegate data_source_delegate;
- DataSource source(&data_source_delegate);
- std::unique_ptr<Surface> origin(new Surface());
- auto* origin_ptr = origin.get();
- // Make origin into a real window so the pointer can click it
- ShellSurface shell_surface(origin_ptr);
- Buffer buffer(exo_test_helper()->CreateGpuMemoryBuffer(gfx::Size(10, 10)));
- origin_ptr->Attach(&buffer);
- origin_ptr->Commit();
- auto closure = base::BindOnce([](std::unique_ptr<Surface> shell_surface,
- ui::mojom::DragOperation& output_drag_op) {},
- std::move(origin));
- PointerDragDropObserver drag_drop_observer(std::move(closure));
- auto* wm_helper = WMHelper::GetInstance();
- wm_helper->AddDragDropObserver(&drag_drop_observer);
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(pointer_delegate, CanAcceptPointerEventsForSurface(origin_ptr))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(pointer_delegate, OnPointerFrame()).Times(AnyNumber());
- EXPECT_CALL(pointer_delegate, OnPointerEnter(origin_ptr, gfx::PointF(), 0));
- generator.MoveMouseTo(origin_ptr->window()->GetBoundsInScreen().origin());
- auto* drag_drop_controller = static_cast<ash::DragDropController*>(
- aura::client::GetDragDropClient(ash::Shell::GetPrimaryRootWindow()));
- ASSERT_TRUE(drag_drop_controller);
- generator.PressLeftButton();
- seat.StartDrag(&source, origin_ptr, /*icon=*/nullptr,
- ui::mojom::DragEventSource::kMouse);
- EXPECT_TRUE(seat.get_drag_drop_operation_for_testing());
- // As soon as the runloop gets triggered, emit a mouse release event.
- drag_drop_controller->SetLoopClosureForTesting(
- base::BindLambdaForTesting([&]() {
- EXPECT_CALL(pointer_delegate, OnPointerEnter(_, _, _));
- generator.ReleaseLeftButton();
- }),
- base::DoNothing());
- // OnPointerLeave() gets called twice:
- // 1/ when the drag starts;
- // 2/ when the dragging window gets destroyed.
- EXPECT_CALL(pointer_delegate, OnPointerLeave(_)).Times(2);
- base::RunLoop().RunUntilIdle();
- wm_helper->RemoveDragDropObserver(&drag_drop_observer);
- EXPECT_CALL(pointer_delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- // Test for crbug.com/1307143: It ensures no "pointer enter" event is
- // processed in case the target surface parent is destroyed during the drop
- // action.
- TEST_F(PointerTest,
- DragDropAndPointerEnterLeaveEvents_NoEnterOnParentSurfaceDestroy) {
- Seat seat(std::make_unique<TestDataExchangeDelegate>());
- MockPointerDelegate pointer_delegate;
- std::unique_ptr<Pointer> pointer(new Pointer(&pointer_delegate, &seat));
- TestDataSourceDelegate data_source_delegate;
- DataSource source(&data_source_delegate);
- auto shell_surface = test::ShellSurfaceBuilder({10, 10}).BuildShellSurface();
- auto* surface = shell_surface->surface_for_testing();
- auto closure = base::BindOnce([](std::unique_ptr<ShellSurface> shell_surface,
- ui::mojom::DragOperation& output_drag_op) {},
- std::move(shell_surface));
- PointerDragDropObserver drag_drop_observer(std::move(closure));
- auto* wm_helper = WMHelper::GetInstance();
- wm_helper->AddDragDropObserver(&drag_drop_observer);
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(pointer_delegate, CanAcceptPointerEventsForSurface(testing::_))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(pointer_delegate, OnPointerFrame()).Times(AnyNumber());
- EXPECT_CALL(pointer_delegate, OnPointerEnter(surface, gfx::PointF(), 0));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin());
- auto* drag_drop_controller = static_cast<ash::DragDropController*>(
- aura::client::GetDragDropClient(ash::Shell::GetPrimaryRootWindow()));
- ASSERT_TRUE(drag_drop_controller);
- generator.PressLeftButton();
- seat.StartDrag(&source, surface, /*icon=*/nullptr,
- ui::mojom::DragEventSource::kMouse);
- EXPECT_TRUE(seat.get_drag_drop_operation_for_testing());
- // As soon as the runloop gets triggered, emit a mouse release event.
- drag_drop_controller->SetLoopClosureForTesting(
- base::BindLambdaForTesting([&]() {
- EXPECT_CALL(pointer_delegate, OnPointerEnter(_, _, _)).Times(1);
- generator.ReleaseLeftButton();
- }),
- base::DoNothing());
- // OnPointerLeave() gets called twice:
- // 1/ when the drag starts;
- // 2/ when the dragging window gets destroyed.
- EXPECT_CALL(pointer_delegate, OnPointerLeave(_)).Times(2);
- base::RunLoop().RunUntilIdle();
- wm_helper->RemoveDragDropObserver(&drag_drop_observer);
- EXPECT_CALL(pointer_delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- #endif
- TEST_F(PointerTest, OnPointerRelativeMotion) {
- auto surface = std::make_unique<Surface>();
- auto shell_surface = std::make_unique<ShellSurface>(surface.get());
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- MockRelativePointerDelegate relative_delegate;
- Seat seat;
- auto pointer = std::make_unique<Pointer>(&delegate, &seat);
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- pointer->RegisterRelativePointerDelegate(&relative_delegate);
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerFrame()).Times(9);
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(delegate, OnPointerMotion(testing::_, gfx::PointF(1, 1)));
- EXPECT_CALL(
- relative_delegate,
- OnPointerRelativeMotion(testing::_, gfx::Vector2dF(1, 1), testing::_));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin() +
- gfx::Vector2d(1, 1));
- EXPECT_CALL(delegate, OnPointerMotion(testing::_, gfx::PointF(2, 2)));
- EXPECT_CALL(
- relative_delegate,
- OnPointerRelativeMotion(testing::_, gfx::Vector2dF(1, 1), testing::_));
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin() +
- gfx::Vector2d(2, 2));
- auto sub_surface = std::make_unique<Surface>();
- auto sub = std::make_unique<SubSurface>(sub_surface.get(), surface.get());
- surface->SetSubSurfacePosition(sub_surface.get(), gfx::PointF(5, 5));
- gfx::Size sub_buffer_size(5, 5);
- auto sub_buffer = std::make_unique<Buffer>(
- exo_test_helper()->CreateGpuMemoryBuffer(sub_buffer_size));
- sub_surface->Attach(sub_buffer.get());
- sub_surface->Commit();
- surface->Commit();
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(sub_surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerLeave(surface.get()));
- EXPECT_CALL(delegate, OnPointerEnter(sub_surface.get(), gfx::PointF(), 0));
- // OnPointerMotion will not be called, because the pointer location is already
- // sent with OnPointerEnter, but we should still receive
- // OnPointerRelativeMotion.
- EXPECT_CALL(
- relative_delegate,
- OnPointerRelativeMotion(testing::_, gfx::Vector2dF(3, 3), testing::_));
- generator.MoveMouseTo(sub_surface->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(delegate, OnPointerMotion(testing::_, gfx::PointF(1, 1)));
- EXPECT_CALL(
- relative_delegate,
- OnPointerRelativeMotion(testing::_, gfx::Vector2dF(1, 1), testing::_));
- generator.MoveMouseTo(sub_surface->window()->GetBoundsInScreen().origin() +
- gfx::Vector2d(1, 1));
- const gfx::Point child_surface_origin =
- sub_surface->window()->GetBoundsInScreen().origin() +
- gfx::Vector2d(10, 10);
- auto child_surface = std::make_unique<Surface>();
- auto child_shell_surface = std::make_unique<ShellSurface>(
- child_surface.get(), child_surface_origin, /*can_minimize=*/false,
- ash::desks_util::GetActiveDeskContainerId());
- child_shell_surface->DisableMovement();
- child_shell_surface->SetParent(shell_surface.get());
- gfx::Size child_buffer_size(15, 15);
- auto child_buffer = std::make_unique<Buffer>(
- exo_test_helper()->CreateGpuMemoryBuffer(child_buffer_size));
- child_surface->Attach(child_buffer.get());
- child_surface->Commit();
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(child_surface.get()))
- .WillRepeatedly(testing::Return(true));
- EXPECT_CALL(delegate, OnPointerLeave(sub_surface.get()));
- EXPECT_CALL(delegate, OnPointerEnter(child_surface.get(), gfx::PointF(), 0));
- // OnPointerMotion will not be called, because the pointer location is already
- // sent with OnPointerEnter, but we should still receive
- // OnPointerRelativeMotion.
- EXPECT_CALL(
- relative_delegate,
- OnPointerRelativeMotion(testing::_, gfx::Vector2dF(9, 9), testing::_));
- generator.MoveMouseTo(child_surface->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(delegate, OnPointerMotion(testing::_, gfx::PointF(10, 10)));
- EXPECT_CALL(
- relative_delegate,
- OnPointerRelativeMotion(testing::_, gfx::Vector2dF(10, 10), testing::_));
- generator.MoveMouseTo(child_surface->window()->GetBoundsInScreen().origin() +
- gfx::Vector2d(10, 10));
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- EXPECT_CALL(relative_delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- // TODO(b/161755250): the ifdef is only necessary because of the feature
- // flag. This code should work fine on non-cros.
- #if BUILDFLAG(IS_CHROMEOS_ASH)
- class PointerOrdinalMotionTest : public PointerTest {
- public:
- PointerOrdinalMotionTest() {
- scoped_feature_list_.InitAndEnableFeature(
- chromeos::features::kExoOrdinalMotion);
- }
- private:
- base::test::ScopedFeatureList scoped_feature_list_;
- };
- TEST_F(PointerOrdinalMotionTest, OrdinalMotionOverridesRelativeMotion) {
- auto surface = std::make_unique<Surface>();
- auto shell_surface = std::make_unique<ShellSurface>(surface.get());
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- // Set up the pointer and move it to the origin.
- testing::NiceMock<MockPointerDelegate> delegate;
- Seat seat;
- auto pointer = std::make_unique<Pointer>(&delegate, &seat);
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- gfx::Point origin = surface->window()->GetBoundsInScreen().origin();
- generator.MoveMouseTo(origin);
- // Start sending relative motion events.
- testing::StrictMock<MockRelativePointerDelegate> relative_delegate;
- pointer->RegisterRelativePointerDelegate(&relative_delegate);
- // By default, ordinal and relative are the same.
- gfx::Point new_location = origin + gfx::Vector2d(1, 1);
- ui::MouseEvent ev1(ui::ET_MOUSE_MOVED, new_location, new_location,
- ui::EventTimeForNow(), generator.flags(), 0);
- EXPECT_CALL(relative_delegate,
- OnPointerRelativeMotion(testing::_, gfx::Vector2dF(1, 1),
- gfx::Vector2dF(1, 1)));
- generator.Dispatch(&ev1);
- // When set, ordinal overrides the relative motion.
- new_location = new_location + gfx::Vector2d(1, 1);
- ui::MouseEvent ev2(ui::ET_MOUSE_MOVED, new_location, new_location,
- ui::EventTimeForNow(), generator.flags(), 0);
- ui::MouseEvent::DispatcherApi(&ev2).set_movement(gfx::Vector2dF(99, 99));
- EXPECT_CALL(relative_delegate,
- OnPointerRelativeMotion(testing::_, gfx::Vector2dF(1, 1),
- gfx::Vector2dF(99, 99)));
- generator.Dispatch(&ev2);
- pointer->UnregisterRelativePointerDelegate(&relative_delegate);
- }
- #endif // BUILDFLAG(IS_CHROMEOS_ASH)
- #if BUILDFLAG(IS_CHROMEOS_ASH)
- TEST_F(PointerConstraintTest, ConstrainPointer) {
- EXPECT_TRUE(pointer_->ConstrainPointer(&constraint_delegate_));
- EXPECT_CALL(delegate_, OnPointerEnter(surface_, gfx::PointF(), 0));
- EXPECT_CALL(delegate_, OnPointerFrame());
- generator_->MoveMouseTo(surface_->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(delegate_, OnPointerMotion(testing::_, testing::_)).Times(0);
- generator_->MoveMouseTo(surface_->window()->GetBoundsInScreen().origin() +
- gfx::Vector2d(-1, -1));
- auto child_shell_surface = test::ShellSurfaceBuilder({15, 15})
- .SetParent(shell_surface_.get())
- .SetDisableMovement()
- .SetCanMinimize(false)
- .BuildShellSurface();
- Surface* child_surface = child_shell_surface->surface_for_testing();
- EXPECT_CALL(delegate_, CanAcceptPointerEventsForSurface(child_surface))
- .WillRepeatedly(testing::Return(true));
- generator_->MoveMouseTo(
- child_surface->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(delegate_, OnPointerLeave(surface_));
- EXPECT_CALL(delegate_, OnPointerEnter(child_surface, gfx::PointF(), 0));
- EXPECT_CALL(delegate_, OnPointerFrame());
- // Moving the cursor to a different surface should change the focus when
- // the pointer is unconstrained.
- pointer_->UnconstrainPointerByUserAction();
- generator_->MoveMouseTo(
- child_surface->window()->GetBoundsInScreen().origin());
- pointer_->OnPointerConstraintDelegateDestroying(&constraint_delegate_);
- EXPECT_CALL(delegate_, OnPointerDestroying(pointer_.get()));
- pointer_.reset();
- }
- #endif
- #if BUILDFLAG(IS_CHROMEOS_ASH)
- TEST_F(PointerConstraintTest, CanOnlyConstrainPermittedWindows) {
- std::unique_ptr<ShellSurface> shell_surface =
- test::ShellSurfaceBuilder({10, 10}).BuildShellSurface();
- EXPECT_CALL(constraint_delegate_, GetConstrainedSurface())
- .WillRepeatedly(testing::Return(shell_surface->surface_for_testing()));
- // Called once when ConstrainPointer is denied, and again when the delegate
- // is destroyed.
- EXPECT_CALL(constraint_delegate_, OnDefunct()).Times(2);
- EXPECT_FALSE(pointer_->ConstrainPointer(&constraint_delegate_));
- pointer_->OnPointerConstraintDelegateDestroying(&constraint_delegate_);
- EXPECT_CALL(delegate_, OnPointerDestroying(pointer_.get()));
- pointer_.reset();
- }
- #endif
- #if BUILDFLAG(IS_CHROMEOS_ASH)
- TEST_F(PointerConstraintTest, OneConstraintPerSurface) {
- ON_CALL(constraint_delegate_, IsPersistent())
- .WillByDefault(testing::Return(false));
- EXPECT_TRUE(pointer_->ConstrainPointer(&constraint_delegate_));
- EXPECT_CALL(delegate_, OnPointerEnter(surface_, gfx::PointF(), 0));
- EXPECT_CALL(delegate_, OnPointerFrame()).Times(testing::AtLeast(1));
- generator_->MoveMouseTo(surface_->window()->GetBoundsInScreen().origin());
- // Add a second constraint for the same surface, it should fail.
- MockPointerConstraintDelegate second_constraint;
- EXPECT_CALL(second_constraint, GetConstrainedSurface())
- .WillRepeatedly(testing::Return(surface_));
- ON_CALL(second_constraint, IsPersistent())
- .WillByDefault(testing::Return(false));
- EXPECT_CALL(second_constraint, OnAlreadyConstrained());
- EXPECT_CALL(second_constraint, OnDefunct());
- EXPECT_FALSE(pointer_->ConstrainPointer(&second_constraint));
- pointer_->OnPointerConstraintDelegateDestroying(&constraint_delegate_);
- EXPECT_CALL(delegate_, OnPointerDestroying(pointer_.get()));
- pointer_.reset();
- }
- #endif
- #if BUILDFLAG(IS_CHROMEOS_ASH)
- TEST_F(PointerConstraintTest, OneShotConstraintActivatedOnFirstFocus) {
- auto second_shell_surface = BuildShellSurfaceWhichPermitsPointerLock();
- Surface* second_surface = second_shell_surface->surface_for_testing();
- EXPECT_CALL(delegate_, CanAcceptPointerEventsForSurface(second_surface))
- .WillRepeatedly(testing::Return(true));
- focus_client_->FocusWindow(second_surface->window());
- // Assert: Can no longer activate the constraint on the first surface.
- EXPECT_FALSE(pointer_->ConstrainPointer(&constraint_delegate_));
- EXPECT_EQ(constraint_delegate_.activated_count, 0);
- // Assert: Constraint is activated when first surface gains focus.
- focus_client_->FocusWindow(surface_->window());
- EXPECT_EQ(constraint_delegate_.activated_count, 1);
- EXPECT_CALL(delegate_, OnPointerEnter(surface_, gfx::PointF(), 0));
- EXPECT_CALL(delegate_, OnPointerFrame());
- generator_->MoveMouseTo(surface_->window()->GetBoundsInScreen().origin());
- // Teardown
- pointer_->OnPointerConstraintDelegateDestroying(&constraint_delegate_);
- EXPECT_CALL(delegate_, OnPointerDestroying(pointer_.get()));
- pointer_.reset();
- }
- #endif
- #if BUILDFLAG(IS_CHROMEOS_ASH)
- TEST_F(PointerConstraintTest, UnconstrainPointerWhenSurfaceIsDestroyed) {
- EXPECT_TRUE(pointer_->ConstrainPointer(&constraint_delegate_));
- EXPECT_CALL(delegate_, OnPointerEnter(surface_, gfx::PointF(), 0));
- EXPECT_CALL(delegate_, OnPointerFrame());
- generator_->MoveMouseTo(surface_->window()->GetBoundsInScreen().origin());
- // Constraint should be broken if surface is destroyed.
- EXPECT_CALL(constraint_delegate_, OnConstraintBroken());
- EXPECT_CALL(delegate_, OnPointerLeave(surface_));
- EXPECT_CALL(delegate_, OnPointerFrame());
- shell_surface_.reset();
- EXPECT_CALL(delegate_, OnPointerDestroying(pointer_.get()));
- pointer_.reset();
- }
- #endif
- #if BUILDFLAG(IS_CHROMEOS_ASH)
- TEST_F(PointerConstraintTest, UnconstrainPointerWhenWindowLosesFocus) {
- ON_CALL(constraint_delegate_, IsPersistent())
- .WillByDefault(testing::Return(false));
- EXPECT_TRUE(pointer_->ConstrainPointer(&constraint_delegate_));
- EXPECT_CALL(delegate_, OnPointerEnter(surface_, gfx::PointF(), 0));
- EXPECT_CALL(delegate_, OnPointerFrame());
- generator_->MoveMouseTo(surface_->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(constraint_delegate_, OnConstraintBroken());
- EXPECT_CALL(constraint_delegate_, OnConstraintActivated()).Times(0);
- focus_client_->FocusWindow(nullptr);
- focus_client_->FocusWindow(surface_->window());
- pointer_->OnPointerConstraintDelegateDestroying(&constraint_delegate_);
- EXPECT_CALL(delegate_, OnPointerDestroying(pointer_.get()));
- pointer_.reset();
- }
- #endif
- #if BUILDFLAG(IS_CHROMEOS_ASH)
- TEST_F(PointerConstraintTest, PersistentConstraintActivatedOnRefocus) {
- ON_CALL(constraint_delegate_, IsPersistent())
- .WillByDefault(testing::Return(true));
- EXPECT_TRUE(pointer_->ConstrainPointer(&constraint_delegate_));
- EXPECT_CALL(delegate_, OnPointerEnter(surface_, gfx::PointF(), 0));
- EXPECT_CALL(delegate_, OnPointerFrame());
- generator_->MoveMouseTo(surface_->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(constraint_delegate_, OnConstraintBroken());
- focus_client_->FocusWindow(nullptr);
- EXPECT_CALL(constraint_delegate_, OnConstraintActivated());
- focus_client_->FocusWindow(surface_->window());
- pointer_->OnPointerConstraintDelegateDestroying(&constraint_delegate_);
- EXPECT_CALL(delegate_, OnPointerDestroying(pointer_.get()));
- pointer_.reset();
- }
- #endif
- #if BUILDFLAG(IS_CHROMEOS_ASH)
- TEST_F(PointerConstraintTest, MultipleSurfacesCanBeConstrained) {
- // Arrange: First surface + persistent constraint
- ON_CALL(constraint_delegate_, IsPersistent())
- .WillByDefault(testing::Return(true));
- EXPECT_TRUE(pointer_->ConstrainPointer(&constraint_delegate_));
- EXPECT_CALL(delegate_, OnPointerEnter(surface_, gfx::PointF(), 0));
- EXPECT_CALL(delegate_, OnPointerFrame());
- generator_->MoveMouseTo(surface_->window()->GetBoundsInScreen().origin());
- EXPECT_EQ(constraint_delegate_.activated_count, 1);
- // Arrange: Second surface + persistent constraint
- auto second_shell_surface = BuildShellSurfaceWhichPermitsPointerLock();
- Surface* second_surface = second_shell_surface->surface_for_testing();
- focus_client_->FocusWindow(second_surface->window());
- EXPECT_CALL(delegate_, CanAcceptPointerEventsForSurface(second_surface))
- .WillRepeatedly(testing::Return(true));
- testing::NiceMock<MockPointerConstraintDelegate> second_constraint;
- EXPECT_CALL(second_constraint, GetConstrainedSurface())
- .WillRepeatedly(testing::Return(second_surface));
- ON_CALL(second_constraint, IsPersistent())
- .WillByDefault(testing::Return(true));
- EXPECT_TRUE(pointer_->ConstrainPointer(&second_constraint));
- EXPECT_EQ(constraint_delegate_.activated_count, 1);
- EXPECT_EQ(second_constraint.activated_count, 1);
- // Act: Toggle focus, first surface's constraint should activate.
- focus_client_->FocusWindow(surface_->window());
- EXPECT_EQ(constraint_delegate_.activated_count, 2);
- EXPECT_EQ(second_constraint.activated_count, 1);
- // Act: Toggle focus, second surface's constraint should activate.
- focus_client_->FocusWindow(second_surface->window());
- EXPECT_EQ(constraint_delegate_.activated_count, 2);
- EXPECT_EQ(second_constraint.activated_count, 2);
- pointer_->OnPointerConstraintDelegateDestroying(&constraint_delegate_);
- pointer_->OnPointerConstraintDelegateDestroying(&second_constraint);
- EXPECT_CALL(delegate_, OnPointerDestroying(pointer_.get()));
- pointer_.reset();
- }
- #endif
- #if BUILDFLAG(IS_CHROMEOS_ASH)
- TEST_F(PointerConstraintTest, UserActionPreventsConstraint) {
- ON_CALL(constraint_delegate_, IsPersistent())
- .WillByDefault(testing::Return(false));
- EXPECT_TRUE(pointer_->ConstrainPointer(&constraint_delegate_));
- EXPECT_CALL(delegate_, OnPointerEnter(surface_, gfx::PointF(), 0));
- EXPECT_CALL(delegate_, OnPointerFrame()).Times(testing::AtLeast(1));
- generator_->MoveMouseTo(surface_->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(constraint_delegate_, OnConstraintBroken());
- pointer_->UnconstrainPointerByUserAction();
- // New constraints are no longer permitted.
- MockPointerConstraintDelegate second_constraint;
- EXPECT_CALL(second_constraint, GetConstrainedSurface())
- .WillRepeatedly(testing::Return(surface_));
- ON_CALL(second_constraint, IsPersistent())
- .WillByDefault(testing::Return(false));
- EXPECT_FALSE(pointer_->ConstrainPointer(&second_constraint));
- EXPECT_EQ(second_constraint.activated_count, 0);
- // A click event will activate the pending constraint.
- generator_->ClickLeftButton();
- EXPECT_EQ(second_constraint.activated_count, 1);
- pointer_->OnPointerConstraintDelegateDestroying(&second_constraint);
- // New constraints are now permitted too.
- MockPointerConstraintDelegate third_constraint;
- EXPECT_CALL(third_constraint, GetConstrainedSurface())
- .WillRepeatedly(testing::Return(surface_));
- ON_CALL(third_constraint, IsPersistent())
- .WillByDefault(testing::Return(false));
- EXPECT_TRUE(pointer_->ConstrainPointer(&third_constraint));
- pointer_->OnPointerConstraintDelegateDestroying(&third_constraint);
- pointer_->OnPointerConstraintDelegateDestroying(&constraint_delegate_);
- EXPECT_CALL(delegate_, OnPointerDestroying(pointer_.get()));
- pointer_.reset();
- }
- #endif
- #if BUILDFLAG(IS_CHROMEOS_ASH)
- TEST_F(PointerConstraintTest, UserCanBreakAndActivatePersistentConstraint) {
- ON_CALL(constraint_delegate_, IsPersistent())
- .WillByDefault(testing::Return(true));
- EXPECT_TRUE(pointer_->ConstrainPointer(&constraint_delegate_));
- EXPECT_EQ(constraint_delegate_.activated_count, 1);
- EXPECT_EQ(constraint_delegate_.broken_count, 0);
- EXPECT_CALL(delegate_, OnPointerEnter(surface_, gfx::PointF(), 0));
- EXPECT_CALL(delegate_, OnPointerFrame()).Times(testing::AtLeast(1));
- generator_->MoveMouseTo(surface_->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(constraint_delegate_, OnConstraintBroken());
- pointer_->UnconstrainPointerByUserAction();
- EXPECT_EQ(constraint_delegate_.activated_count, 1);
- EXPECT_EQ(constraint_delegate_.broken_count, 1);
- // Click events re-enable the constraint.
- generator_->ClickLeftButton();
- EXPECT_EQ(constraint_delegate_.activated_count, 2);
- pointer_->OnPointerConstraintDelegateDestroying(&constraint_delegate_);
- EXPECT_CALL(delegate_, OnPointerDestroying(pointer_.get()));
- pointer_.reset();
- }
- TEST_F(PointerConstraintTest, DefaultSecrityDeletegate) {
- auto default_security_delegate =
- SecurityDelegate::GetDefaultSecurityDelegate();
- auto shell_surface = test::ShellSurfaceBuilder({10, 10})
- .SetSecurityDelegate(default_security_delegate.get())
- .BuildShellSurface();
- auto* surface = shell_surface->surface_for_testing();
- focus_client_->FocusWindow(surface->window());
- MockPointerConstraintDelegate constraint_delegate;
- EXPECT_CALL(constraint_delegate, GetConstrainedSurface())
- .WillRepeatedly(testing::Return(surface));
- EXPECT_CALL(constraint_delegate, OnDefunct()).Times(1);
- EXPECT_FALSE(pointer_->ConstrainPointer(&constraint_delegate));
- ::testing::Mock::VerifyAndClearExpectations(&constraint_delegate);
- shell_surface->GetWidget()->GetNativeWindow()->SetProperty(
- aura::client::kAppType, static_cast<int>(ash::AppType::LACROS));
- EXPECT_CALL(constraint_delegate, GetConstrainedSurface())
- .WillRepeatedly(testing::Return(surface));
- EXPECT_CALL(constraint_delegate, OnDefunct()).Times(0);
- EXPECT_TRUE(pointer_->ConstrainPointer(&constraint_delegate));
- ::testing::Mock::VerifyAndClearExpectations(&constraint_delegate);
- EXPECT_CALL(constraint_delegate, GetConstrainedSurface())
- .WillRepeatedly(testing::Return(surface));
- shell_surface->GetWidget()->GetNativeWindow()->SetProperty(
- aura::client::kAppType, static_cast<int>(ash::AppType::ARC_APP));
- EXPECT_CALL(constraint_delegate, OnDefunct()).Times(0);
- EXPECT_TRUE(pointer_->ConstrainPointer(&constraint_delegate));
- ::testing::Mock::VerifyAndClearExpectations(&constraint_delegate);
- pointer_->OnPointerConstraintDelegateDestroying(&constraint_delegate);
- EXPECT_CALL(delegate_, OnPointerDestroying(pointer_.get()));
- pointer_.reset();
- }
- #endif
- TEST_F(PointerTest, PointerStylus) {
- std::unique_ptr<Surface> surface(new Surface);
- std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
- gfx::Size buffer_size(10, 10);
- std::unique_ptr<Buffer> buffer(
- new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
- surface->Attach(buffer.get());
- surface->Commit();
- MockPointerDelegate delegate;
- MockPointerStylusDelegate stylus_delegate;
- Seat seat;
- std::unique_ptr<Pointer> pointer(new Pointer(&delegate, &seat));
- ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- pointer->SetStylusDelegate(&stylus_delegate);
- EXPECT_CALL(delegate, CanAcceptPointerEventsForSurface(surface.get()))
- .WillRepeatedly(testing::Return(true));
- {
- testing::InSequence sequence;
- EXPECT_CALL(delegate, OnPointerEnter(surface.get(), gfx::PointF(), 0));
- EXPECT_CALL(delegate, OnPointerFrame());
- EXPECT_CALL(stylus_delegate,
- OnPointerToolChange(ui::EventPointerType::kMouse));
- EXPECT_CALL(delegate, OnPointerFrame());
- }
- generator.MoveMouseTo(surface->window()->GetBoundsInScreen().origin());
- EXPECT_CALL(delegate, OnPointerDestroying(pointer.get()));
- EXPECT_CALL(stylus_delegate, OnPointerDestroying(pointer.get()));
- pointer.reset();
- }
- } // namespace
- } // namespace exo
|