window_event_dispatcher_unittest.cc 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/aura/window_event_dispatcher.h"
  5. #include <stddef.h>
  6. #include <utility>
  7. #include <vector>
  8. #include "base/bind.h"
  9. #include "base/containers/contains.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/run_loop.h"
  12. #include "base/task/single_thread_task_runner.h"
  13. #include "base/test/bind.h"
  14. #include "base/test/metrics/histogram_tester.h"
  15. #include "base/threading/thread_task_runner_handle.h"
  16. #include "build/build_config.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. #include "ui/aura/client/aura_constants.h"
  19. #include "ui/aura/client/capture_client.h"
  20. #include "ui/aura/client/event_client.h"
  21. #include "ui/aura/client/focus_client.h"
  22. #include "ui/aura/env.h"
  23. #include "ui/aura/test/aura_test_base.h"
  24. #include "ui/aura/test/env_test_helper.h"
  25. #include "ui/aura/test/test_cursor_client.h"
  26. #include "ui/aura/test/test_screen.h"
  27. #include "ui/aura/test/test_window_delegate.h"
  28. #include "ui/aura/test/test_windows.h"
  29. #include "ui/aura/window.h"
  30. #include "ui/aura/window_targeter.h"
  31. #include "ui/aura/window_tracker.h"
  32. #include "ui/base/hit_test.h"
  33. #include "ui/base/ime/mock_input_method.h"
  34. #include "ui/base/ui_base_features.h"
  35. #include "ui/display/screen.h"
  36. #include "ui/events/event.h"
  37. #include "ui/events/event_handler.h"
  38. #include "ui/events/event_utils.h"
  39. #include "ui/events/gesture_detection/gesture_configuration.h"
  40. #include "ui/events/keycodes/dom/dom_code.h"
  41. #include "ui/events/keycodes/keyboard_codes.h"
  42. #include "ui/events/test/event_generator.h"
  43. #include "ui/events/test/test_event_handler.h"
  44. #include "ui/gfx/geometry/point.h"
  45. #include "ui/gfx/geometry/rect.h"
  46. #include "ui/gfx/geometry/transform.h"
  47. #include "ui/platform_window/platform_window_init_properties.h"
  48. #include "ui/wm/core/capture_controller.h"
  49. namespace aura {
  50. namespace {
  51. // A delegate that always returns a non-client component for hit tests.
  52. class NonClientDelegate : public test::TestWindowDelegate {
  53. public:
  54. NonClientDelegate()
  55. : non_client_count_(0), mouse_event_count_(0), mouse_event_flags_(0x0) {}
  56. NonClientDelegate(const NonClientDelegate&) = delete;
  57. NonClientDelegate& operator=(const NonClientDelegate&) = delete;
  58. ~NonClientDelegate() override = default;
  59. int non_client_count() const { return non_client_count_; }
  60. const gfx::Point& non_client_location() const { return non_client_location_; }
  61. int mouse_event_count() const { return mouse_event_count_; }
  62. const gfx::Point& mouse_event_location() const {
  63. return mouse_event_location_;
  64. }
  65. int mouse_event_flags() const { return mouse_event_flags_; }
  66. int GetNonClientComponent(const gfx::Point& location) const override {
  67. NonClientDelegate* self = const_cast<NonClientDelegate*>(this);
  68. self->non_client_count_++;
  69. self->non_client_location_ = location;
  70. return HTMENU;
  71. }
  72. void OnMouseEvent(ui::MouseEvent* event) override {
  73. mouse_event_count_++;
  74. mouse_event_location_ = event->location();
  75. mouse_event_flags_ = event->flags();
  76. event->SetHandled();
  77. }
  78. private:
  79. int non_client_count_;
  80. gfx::Point non_client_location_;
  81. int mouse_event_count_;
  82. gfx::Point mouse_event_location_;
  83. int mouse_event_flags_;
  84. };
  85. // A simple event handler that consumes key events.
  86. class ConsumeKeyHandler : public ui::test::TestEventHandler {
  87. public:
  88. ConsumeKeyHandler() {}
  89. ConsumeKeyHandler(const ConsumeKeyHandler&) = delete;
  90. ConsumeKeyHandler& operator=(const ConsumeKeyHandler&) = delete;
  91. ~ConsumeKeyHandler() override {}
  92. // Overridden from ui::EventHandler:
  93. void OnKeyEvent(ui::KeyEvent* event) override {
  94. ui::test::TestEventHandler::OnKeyEvent(event);
  95. event->StopPropagation();
  96. }
  97. };
  98. // ImeKeyEventDispatcher that tracks the events passed to PostIME phase.
  99. class TestImeKeyEventDispatcher : public ui::ImeKeyEventDispatcher {
  100. public:
  101. TestImeKeyEventDispatcher() = default;
  102. ~TestImeKeyEventDispatcher() override = default;
  103. // ui::ImeKeyEventDispatcher:
  104. ui::EventDispatchDetails DispatchKeyEventPostIME(
  105. ui::KeyEvent* event) override {
  106. ++dispatched_event_count_;
  107. return ui::EventDispatchDetails();
  108. }
  109. int dispatched_event_count() const { return dispatched_event_count_; }
  110. private:
  111. int dispatched_event_count_{0};
  112. };
  113. bool IsFocusedWindow(aura::Window* window) {
  114. return client::GetFocusClient(window)->GetFocusedWindow() == window;
  115. }
  116. } // namespace
  117. using WindowEventDispatcherTest = test::AuraTestBase;
  118. TEST_F(WindowEventDispatcherTest, OnHostMouseEvent) {
  119. // Create two non-overlapping windows so we don't have to worry about which
  120. // is on top.
  121. std::unique_ptr<NonClientDelegate> delegate1(new NonClientDelegate());
  122. std::unique_ptr<NonClientDelegate> delegate2(new NonClientDelegate());
  123. const int kWindowWidth = 123;
  124. const int kWindowHeight = 45;
  125. gfx::Rect bounds1(100, 200, kWindowWidth, kWindowHeight);
  126. gfx::Rect bounds2(300, 400, kWindowWidth, kWindowHeight);
  127. std::unique_ptr<aura::Window> window1(CreateTestWindowWithDelegate(
  128. delegate1.get(), -1234, bounds1, root_window()));
  129. std::unique_ptr<aura::Window> window2(CreateTestWindowWithDelegate(
  130. delegate2.get(), -5678, bounds2, root_window()));
  131. // Send a mouse event to window1.
  132. gfx::Point point(101, 201);
  133. ui::MouseEvent event1(ui::ET_MOUSE_PRESSED, point, point,
  134. ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
  135. ui::EF_LEFT_MOUSE_BUTTON);
  136. DispatchEventUsingWindowDispatcher(&event1);
  137. // Event was tested for non-client area for the target window.
  138. EXPECT_EQ(1, delegate1->non_client_count());
  139. EXPECT_EQ(0, delegate2->non_client_count());
  140. // The non-client component test was in local coordinates.
  141. EXPECT_EQ(gfx::Point(1, 1), delegate1->non_client_location());
  142. // Mouse event was received by target window.
  143. EXPECT_EQ(1, delegate1->mouse_event_count());
  144. EXPECT_EQ(0, delegate2->mouse_event_count());
  145. // Event was in local coordinates.
  146. EXPECT_EQ(gfx::Point(1, 1), delegate1->mouse_event_location());
  147. // Non-client flag was set.
  148. EXPECT_TRUE(delegate1->mouse_event_flags() & ui::EF_IS_NON_CLIENT);
  149. }
  150. TEST_F(WindowEventDispatcherTest, RepostEvent) {
  151. // Test RepostEvent in RootWindow. It only works for Mouse Press and touch
  152. // press.
  153. EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
  154. gfx::Point point(10, 10);
  155. ui::MouseEvent event(ui::ET_MOUSE_PRESSED, point, point,
  156. ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
  157. ui::EF_LEFT_MOUSE_BUTTON);
  158. host()->dispatcher()->RepostEvent(&event);
  159. RunAllPendingInMessageLoop();
  160. EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
  161. ui::TouchEvent touch_pressed_event(
  162. ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), ui::EventTimeForNow(),
  163. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  164. host()->dispatcher()->RepostEvent(&touch_pressed_event);
  165. RunAllPendingInMessageLoop();
  166. EXPECT_TRUE(Env::GetInstance()->is_touch_down());
  167. }
  168. // Check that we correctly track whether any touch devices are down in response
  169. // to touch press and release events with two WindowTreeHost.
  170. TEST_F(WindowEventDispatcherTest, TouchDownState) {
  171. std::unique_ptr<WindowTreeHost> second_host = WindowTreeHost::Create(
  172. ui::PlatformWindowInitProperties{gfx::Rect(20, 30, 100, 50)});
  173. second_host->InitHost();
  174. second_host->window()->Show();
  175. ui::TouchEvent touch_pressed_event1(
  176. ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), ui::EventTimeForNow(),
  177. ui::PointerDetails(ui::EventPointerType::kTouch, 1));
  178. ui::TouchEvent touch_pressed_event2(
  179. ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), ui::EventTimeForNow(),
  180. ui::PointerDetails(ui::EventPointerType::kTouch, 2));
  181. ui::TouchEvent touch_released_event1(
  182. ui::ET_TOUCH_RELEASED, gfx::Point(10, 10), ui::EventTimeForNow(),
  183. ui::PointerDetails(ui::EventPointerType::kTouch, 1));
  184. ui::TouchEvent touch_released_event2(
  185. ui::ET_TOUCH_RELEASED, gfx::Point(10, 10), ui::EventTimeForNow(),
  186. ui::PointerDetails(ui::EventPointerType::kTouch, 2));
  187. EXPECT_FALSE(Env::GetInstance()->is_touch_down());
  188. host()->dispatcher()->OnEventFromSource(&touch_pressed_event1);
  189. EXPECT_TRUE(Env::GetInstance()->is_touch_down());
  190. second_host->dispatcher()->OnEventFromSource(&touch_pressed_event2);
  191. EXPECT_TRUE(Env::GetInstance()->is_touch_down());
  192. host()->dispatcher()->OnEventFromSource(&touch_released_event1);
  193. EXPECT_TRUE(Env::GetInstance()->is_touch_down());
  194. second_host->dispatcher()->OnEventFromSource(&touch_released_event2);
  195. EXPECT_FALSE(Env::GetInstance()->is_touch_down());
  196. }
  197. // Check that we correctly track the state of the mouse buttons in response to
  198. // button press and release events.
  199. TEST_F(WindowEventDispatcherTest, MouseButtonState) {
  200. EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
  201. gfx::Point location;
  202. std::unique_ptr<ui::MouseEvent> event;
  203. // Press the left button.
  204. event = std::make_unique<ui::MouseEvent>(
  205. ui::ET_MOUSE_PRESSED, location, location, ui::EventTimeForNow(),
  206. ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
  207. DispatchEventUsingWindowDispatcher(event.get());
  208. EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
  209. // Additionally press the right.
  210. event = std::make_unique<ui::MouseEvent>(
  211. ui::ET_MOUSE_PRESSED, location, location, ui::EventTimeForNow(),
  212. ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON,
  213. ui::EF_RIGHT_MOUSE_BUTTON);
  214. DispatchEventUsingWindowDispatcher(event.get());
  215. EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
  216. // Release the left button.
  217. event = std::make_unique<ui::MouseEvent>(
  218. ui::ET_MOUSE_RELEASED, location, location, ui::EventTimeForNow(),
  219. ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
  220. DispatchEventUsingWindowDispatcher(event.get());
  221. EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
  222. // Release the right button. We should ignore the Shift-is-down flag.
  223. event = std::make_unique<ui::MouseEvent>(
  224. ui::ET_MOUSE_RELEASED, location, location, ui::EventTimeForNow(),
  225. ui::EF_SHIFT_DOWN, ui::EF_RIGHT_MOUSE_BUTTON);
  226. DispatchEventUsingWindowDispatcher(event.get());
  227. EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
  228. // Press the middle button.
  229. event = std::make_unique<ui::MouseEvent>(
  230. ui::ET_MOUSE_PRESSED, location, location, ui::EventTimeForNow(),
  231. ui::EF_MIDDLE_MOUSE_BUTTON, ui::EF_MIDDLE_MOUSE_BUTTON);
  232. DispatchEventUsingWindowDispatcher(event.get());
  233. EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
  234. }
  235. TEST_F(WindowEventDispatcherTest, TranslatedEvent) {
  236. std::unique_ptr<Window> w1(test::CreateTestWindowWithDelegate(
  237. NULL, 1, gfx::Rect(50, 50, 100, 100), root_window()));
  238. gfx::Point origin(100, 100);
  239. ui::MouseEvent root(ui::ET_MOUSE_PRESSED, origin, origin,
  240. ui::EventTimeForNow(), 0, 0);
  241. EXPECT_EQ("100,100", root.location().ToString());
  242. EXPECT_EQ("100,100", root.root_location().ToString());
  243. ui::MouseEvent translated_event(root, static_cast<Window*>(root_window()),
  244. w1.get(), ui::ET_MOUSE_ENTERED, root.flags());
  245. EXPECT_EQ("50,50", translated_event.location().ToString());
  246. EXPECT_EQ("100,100", translated_event.root_location().ToString());
  247. }
  248. namespace {
  249. class TestEventClient : public client::EventClient {
  250. public:
  251. static const int kNonLockWindowId = 100;
  252. static const int kLockWindowId = 200;
  253. explicit TestEventClient(Window* root_window)
  254. : root_window_(root_window), lock_(false) {
  255. client::SetEventClient(root_window_, this);
  256. Window* lock_window =
  257. test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_);
  258. lock_window->SetId(kLockWindowId);
  259. Window* non_lock_window =
  260. test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_);
  261. non_lock_window->SetId(kNonLockWindowId);
  262. }
  263. TestEventClient(const TestEventClient&) = delete;
  264. TestEventClient& operator=(const TestEventClient&) = delete;
  265. ~TestEventClient() override { client::SetEventClient(root_window_, NULL); }
  266. // Starts/stops locking. Locking prevents windows other than those inside
  267. // the lock container from receiving events, getting focus etc.
  268. void Lock() { lock_ = true; }
  269. void Unlock() { lock_ = false; }
  270. Window* GetLockWindow() {
  271. return const_cast<Window*>(
  272. static_cast<const TestEventClient*>(this)->GetLockWindow());
  273. }
  274. const Window* GetLockWindow() const {
  275. return root_window_->GetChildById(kLockWindowId);
  276. }
  277. Window* GetNonLockWindow() {
  278. return root_window_->GetChildById(kNonLockWindowId);
  279. }
  280. private:
  281. // Overridden from client::EventClient:
  282. bool GetCanProcessEventsWithinSubtree(const Window* window) const override {
  283. return lock_ ? window->Contains(GetLockWindow()) ||
  284. GetLockWindow()->Contains(window)
  285. : true;
  286. }
  287. ui::EventTarget* GetToplevelEventTarget() override { return NULL; }
  288. raw_ptr<Window> root_window_;
  289. bool lock_;
  290. };
  291. } // namespace
  292. TEST_F(WindowEventDispatcherTest, GetCanProcessEventsWithinSubtree) {
  293. TestEventClient client(root_window());
  294. test::TestWindowDelegate d;
  295. ui::test::TestEventHandler nonlock_ef;
  296. ui::test::TestEventHandler lock_ef;
  297. client.GetNonLockWindow()->AddPreTargetHandler(&nonlock_ef);
  298. client.GetLockWindow()->AddPreTargetHandler(&lock_ef);
  299. Window* w1 = test::CreateTestWindowWithBounds(gfx::Rect(10, 10, 20, 20),
  300. client.GetNonLockWindow());
  301. w1->SetId(1);
  302. Window* w2 = test::CreateTestWindowWithBounds(gfx::Rect(30, 30, 20, 20),
  303. client.GetNonLockWindow());
  304. w2->SetId(2);
  305. std::unique_ptr<Window> w3(test::CreateTestWindowWithDelegate(
  306. &d, 3, gfx::Rect(30, 30, 20, 20), client.GetLockWindow()));
  307. w1->Focus();
  308. EXPECT_TRUE(IsFocusedWindow(w1));
  309. client.Lock();
  310. // Since we're locked, the attempt to focus w2 will be ignored.
  311. w2->Focus();
  312. EXPECT_TRUE(IsFocusedWindow(w1));
  313. EXPECT_FALSE(IsFocusedWindow(w2));
  314. {
  315. // Attempting to send a key event to w1 (not in the lock container) should
  316. // cause focus to be reset.
  317. ui::test::EventGenerator generator(root_window());
  318. generator.PressKey(ui::VKEY_SPACE, 0);
  319. EXPECT_EQ(NULL, client::GetFocusClient(w1)->GetFocusedWindow());
  320. EXPECT_FALSE(IsFocusedWindow(w1));
  321. }
  322. {
  323. // Events sent to a window not in the lock container will not be processed.
  324. // i.e. never sent to the non-lock container's event filter.
  325. ui::test::EventGenerator generator(root_window(), w1);
  326. generator.ClickLeftButton();
  327. EXPECT_EQ(0, nonlock_ef.num_mouse_events());
  328. // Events sent to a window in the lock container will be processed.
  329. ui::test::EventGenerator generator3(root_window(), w3.get());
  330. generator3.PressLeftButton();
  331. EXPECT_EQ(1, lock_ef.num_mouse_events());
  332. }
  333. // Prevent w3 from being deleted by the hierarchy since its delegate is owned
  334. // by this scope.
  335. w3->parent()->RemoveChild(w3.get());
  336. client.GetNonLockWindow()->RemovePreTargetHandler(&nonlock_ef);
  337. client.GetLockWindow()->RemovePreTargetHandler(&lock_ef);
  338. }
  339. TEST_F(WindowEventDispatcherTest, DontIgnoreUnknownKeys) {
  340. ui::Event::Properties properties;
  341. properties.emplace(ui::kPropertyKeyboardImeFlag,
  342. std::vector<uint8_t>{ui::kPropertyKeyboardImeIgnoredFlag});
  343. ConsumeKeyHandler handler;
  344. root_window()->AddPreTargetHandler(&handler);
  345. ui::KeyEvent unknown_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN, ui::EF_NONE);
  346. unknown_event.SetProperties(properties);
  347. DispatchEventUsingWindowDispatcher(&unknown_event);
  348. EXPECT_TRUE(unknown_event.handled());
  349. EXPECT_EQ(1, handler.num_key_events());
  350. handler.Reset();
  351. ui::KeyEvent known_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
  352. known_event.SetProperties(properties);
  353. DispatchEventUsingWindowDispatcher(&known_event);
  354. EXPECT_TRUE(known_event.handled());
  355. EXPECT_EQ(1, handler.num_key_events());
  356. handler.Reset();
  357. ui::KeyEvent ime_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN,
  358. ui::EF_IME_FABRICATED_KEY);
  359. ime_event.SetProperties(properties);
  360. DispatchEventUsingWindowDispatcher(&ime_event);
  361. EXPECT_TRUE(ime_event.handled());
  362. EXPECT_EQ(1, handler.num_key_events());
  363. handler.Reset();
  364. ui::KeyEvent unknown_key_with_char_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN,
  365. ui::EF_NONE);
  366. unknown_key_with_char_event.set_character(0x00e4 /* "ä" */);
  367. unknown_key_with_char_event.SetProperties(properties);
  368. DispatchEventUsingWindowDispatcher(&unknown_key_with_char_event);
  369. EXPECT_TRUE(unknown_key_with_char_event.handled());
  370. EXPECT_EQ(1, handler.num_key_events());
  371. root_window()->RemovePreTargetHandler(&handler);
  372. }
  373. TEST_F(WindowEventDispatcherTest, NoDelegateWindowReceivesKeyEvents) {
  374. std::unique_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
  375. w1->Show();
  376. w1->Focus();
  377. ui::test::TestEventHandler handler;
  378. w1->AddPreTargetHandler(&handler);
  379. ui::KeyEvent key_press(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
  380. ui::Event::Properties properties;
  381. properties.emplace(ui::kPropertyKeyboardImeFlag,
  382. std::vector<uint8_t>{ui::kPropertyKeyboardImeIgnoredFlag});
  383. key_press.SetProperties(properties);
  384. DispatchEventUsingWindowDispatcher(&key_press);
  385. EXPECT_TRUE(key_press.handled());
  386. EXPECT_EQ(1, handler.num_key_events());
  387. w1->RemovePreTargetHandler(&handler);
  388. }
  389. // Tests that touch-events that are beyond the bounds of the root-window do get
  390. // propagated to the event filters correctly with the root as the target.
  391. TEST_F(WindowEventDispatcherTest, TouchEventsOutsideBounds) {
  392. ui::test::TestEventHandler handler;
  393. root_window()->AddPreTargetHandler(&handler);
  394. gfx::Point position = root_window()->bounds().origin();
  395. position.Offset(-10, -10);
  396. ui::TouchEvent press(ui::ET_TOUCH_PRESSED, position, ui::EventTimeForNow(),
  397. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  398. DispatchEventUsingWindowDispatcher(&press);
  399. EXPECT_EQ(1, handler.num_touch_events());
  400. position = root_window()->bounds().origin();
  401. position.Offset(root_window()->bounds().width() + 10,
  402. root_window()->bounds().height() + 10);
  403. ui::TouchEvent release(ui::ET_TOUCH_RELEASED, position, ui::EventTimeForNow(),
  404. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  405. DispatchEventUsingWindowDispatcher(&release);
  406. EXPECT_EQ(2, handler.num_touch_events());
  407. root_window()->RemovePreTargetHandler(&handler);
  408. }
  409. // Tests that scroll events are dispatched correctly.
  410. TEST_F(WindowEventDispatcherTest, ScrollEventDispatch) {
  411. base::TimeTicks now = ui::EventTimeForNow();
  412. ui::test::TestEventHandler handler;
  413. root_window()->AddPreTargetHandler(&handler);
  414. test::TestWindowDelegate delegate;
  415. std::unique_ptr<Window> w1(CreateNormalWindow(1, root_window(), &delegate));
  416. w1->SetBounds(gfx::Rect(20, 20, 40, 40));
  417. // A scroll event on the root-window itself is dispatched.
  418. ui::ScrollEvent scroll1(ui::ET_SCROLL, gfx::Point(10, 10), now, 0, 0, -10, 0,
  419. -10, 2);
  420. DispatchEventUsingWindowDispatcher(&scroll1);
  421. EXPECT_EQ(1, handler.num_scroll_events());
  422. // Scroll event on a window should be dispatched properly.
  423. ui::ScrollEvent scroll2(ui::ET_SCROLL, gfx::Point(25, 30), now, 0, -10, 0,
  424. -10, 0, 2);
  425. DispatchEventUsingWindowDispatcher(&scroll2);
  426. EXPECT_EQ(2, handler.num_scroll_events());
  427. root_window()->RemovePreTargetHandler(&handler);
  428. }
  429. TEST_F(WindowEventDispatcherTest, PreDispatchKeyEventToIme) {
  430. ui::MockInputMethod mock_ime(nullptr);
  431. TestImeKeyEventDispatcher dispatcher;
  432. mock_ime.SetImeKeyEventDispatcher(&dispatcher);
  433. host()->SetSharedInputMethod(&mock_ime);
  434. ConsumeKeyHandler handler;
  435. std::unique_ptr<Window> w(CreateNormalWindow(1, root_window(), nullptr));
  436. w->AddPostTargetHandler(&handler);
  437. w->Show();
  438. w->Focus();
  439. // The dispatched event went to IME before the event target.
  440. ui::KeyEvent key_press(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
  441. DispatchEventUsingWindowDispatcher(&key_press);
  442. EXPECT_EQ(0, handler.num_key_events());
  443. EXPECT_EQ(1, dispatcher.dispatched_event_count());
  444. // However, for the window with kSkipImeProcessing
  445. // The event went to the event target at first.
  446. w->SetProperty(client::kSkipImeProcessing, true);
  447. ui::KeyEvent key_release(ui::ET_KEY_RELEASED, ui::VKEY_A, ui::EF_NONE);
  448. DispatchEventUsingWindowDispatcher(&key_release);
  449. EXPECT_EQ(1, handler.num_key_events());
  450. EXPECT_EQ(1, dispatcher.dispatched_event_count());
  451. }
  452. namespace {
  453. // ui::EventHandler that tracks the types of events it's seen.
  454. class EventFilterRecorder : public ui::EventHandler {
  455. public:
  456. typedef std::vector<ui::EventType> Events;
  457. typedef std::vector<gfx::Point> EventLocations;
  458. typedef std::vector<int> EventFlags;
  459. EventFilterRecorder()
  460. : wait_until_event_(ui::ET_UNKNOWN),
  461. last_touch_may_cause_scrolling_(false) {}
  462. EventFilterRecorder(const EventFilterRecorder&) = delete;
  463. EventFilterRecorder& operator=(const EventFilterRecorder&) = delete;
  464. const Events& events() const { return events_; }
  465. const EventLocations& mouse_locations() const { return mouse_locations_; }
  466. gfx::Point mouse_location(int i) const { return mouse_locations_[i]; }
  467. const EventLocations& touch_locations() const { return touch_locations_; }
  468. const EventLocations& gesture_locations() const { return gesture_locations_; }
  469. const EventFlags& mouse_event_flags() const { return mouse_event_flags_; }
  470. void WaitUntilReceivedEvent(ui::EventType type) {
  471. wait_until_event_ = type;
  472. run_loop_ = std::make_unique<base::RunLoop>();
  473. run_loop_->Run();
  474. }
  475. Events GetAndResetEvents() {
  476. Events events = events_;
  477. Reset();
  478. return events;
  479. }
  480. void Reset() {
  481. events_.clear();
  482. mouse_locations_.clear();
  483. touch_locations_.clear();
  484. gesture_locations_.clear();
  485. mouse_event_flags_.clear();
  486. last_touch_may_cause_scrolling_ = false;
  487. }
  488. // ui::EventHandler overrides:
  489. void OnEvent(ui::Event* event) override {
  490. ui::EventHandler::OnEvent(event);
  491. events_.push_back(event->type());
  492. if (wait_until_event_ == event->type() && run_loop_) {
  493. run_loop_->Quit();
  494. wait_until_event_ = ui::ET_UNKNOWN;
  495. }
  496. }
  497. void OnMouseEvent(ui::MouseEvent* event) override {
  498. mouse_locations_.push_back(event->location());
  499. mouse_event_flags_.push_back(event->flags());
  500. }
  501. void OnTouchEvent(ui::TouchEvent* event) override {
  502. touch_locations_.push_back(event->location());
  503. last_touch_may_cause_scrolling_ = event->may_cause_scrolling();
  504. }
  505. void OnGestureEvent(ui::GestureEvent* event) override {
  506. gesture_locations_.push_back(event->location());
  507. }
  508. bool HasReceivedEvent(ui::EventType type) {
  509. return base::Contains(events_, type);
  510. }
  511. bool LastTouchMayCauseScrolling() const {
  512. return last_touch_may_cause_scrolling_;
  513. }
  514. private:
  515. std::unique_ptr<base::RunLoop> run_loop_;
  516. ui::EventType wait_until_event_;
  517. Events events_;
  518. EventLocations mouse_locations_;
  519. EventLocations touch_locations_;
  520. EventLocations gesture_locations_;
  521. EventFlags mouse_event_flags_;
  522. bool last_touch_may_cause_scrolling_;
  523. };
  524. // Converts an EventType to a string.
  525. std::string EventTypeToString(ui::EventType type) {
  526. switch (type) {
  527. case ui::ET_TOUCH_RELEASED:
  528. return "TOUCH_RELEASED";
  529. case ui::ET_TOUCH_CANCELLED:
  530. return "TOUCH_CANCELLED";
  531. case ui::ET_TOUCH_PRESSED:
  532. return "TOUCH_PRESSED";
  533. case ui::ET_TOUCH_MOVED:
  534. return "TOUCH_MOVED";
  535. case ui::ET_MOUSE_PRESSED:
  536. return "MOUSE_PRESSED";
  537. case ui::ET_MOUSE_DRAGGED:
  538. return "MOUSE_DRAGGED";
  539. case ui::ET_MOUSE_RELEASED:
  540. return "MOUSE_RELEASED";
  541. case ui::ET_MOUSE_MOVED:
  542. return "MOUSE_MOVED";
  543. case ui::ET_MOUSE_ENTERED:
  544. return "MOUSE_ENTERED";
  545. case ui::ET_MOUSE_EXITED:
  546. return "MOUSE_EXITED";
  547. case ui::ET_GESTURE_SCROLL_BEGIN:
  548. return "GESTURE_SCROLL_BEGIN";
  549. case ui::ET_GESTURE_SCROLL_END:
  550. return "GESTURE_SCROLL_END";
  551. case ui::ET_GESTURE_SCROLL_UPDATE:
  552. return "GESTURE_SCROLL_UPDATE";
  553. case ui::ET_GESTURE_PINCH_BEGIN:
  554. return "GESTURE_PINCH_BEGIN";
  555. case ui::ET_GESTURE_PINCH_END:
  556. return "GESTURE_PINCH_END";
  557. case ui::ET_GESTURE_PINCH_UPDATE:
  558. return "GESTURE_PINCH_UPDATE";
  559. case ui::ET_GESTURE_TAP:
  560. return "GESTURE_TAP";
  561. case ui::ET_GESTURE_TAP_DOWN:
  562. return "GESTURE_TAP_DOWN";
  563. case ui::ET_GESTURE_TAP_CANCEL:
  564. return "GESTURE_TAP_CANCEL";
  565. case ui::ET_GESTURE_SHOW_PRESS:
  566. return "GESTURE_SHOW_PRESS";
  567. case ui::ET_GESTURE_BEGIN:
  568. return "GESTURE_BEGIN";
  569. case ui::ET_GESTURE_END:
  570. return "GESTURE_END";
  571. default:
  572. // We should explicitly require each event type.
  573. NOTREACHED() << "Received unexpected event: " << type;
  574. break;
  575. }
  576. return "";
  577. }
  578. std::string EventTypesToString(const EventFilterRecorder::Events& events) {
  579. std::string result;
  580. for (size_t i = 0; i < events.size(); ++i) {
  581. if (i != 0)
  582. result += " ";
  583. result += EventTypeToString(events[i]);
  584. }
  585. return result;
  586. }
  587. } // namespace
  588. #if BUILDFLAG(IS_WIN) && defined(ARCH_CPU_X86)
  589. #define MAYBE(x) DISABLED_##x
  590. #else
  591. #define MAYBE(x) x
  592. #endif
  593. // Verifies a repost mouse event targets the window with capture (if there is
  594. // one).
  595. // Flaky on 32-bit Windows bots. http://crbug.com/388290
  596. TEST_F(WindowEventDispatcherTest, MAYBE(RepostTargetsCaptureWindow)) {
  597. // Set capture on |window| generate a mouse event (that is reposted) and not
  598. // over |window| and verify |window| gets it (|window| gets it because it has
  599. // capture).
  600. EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
  601. EventFilterRecorder recorder;
  602. std::unique_ptr<Window> window(CreateNormalWindow(1, root_window(), NULL));
  603. window->SetBounds(gfx::Rect(20, 20, 40, 30));
  604. window->AddPreTargetHandler(&recorder);
  605. window->SetCapture();
  606. const ui::MouseEvent press_event(
  607. ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
  608. ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
  609. host()->dispatcher()->RepostEvent(&press_event);
  610. RunAllPendingInMessageLoop(); // Necessitated by RepostEvent().
  611. // Mouse moves/enters may be generated. We only care about a pressed.
  612. EXPECT_TRUE(EventTypesToString(recorder.events()).find("MOUSE_PRESSED") !=
  613. std::string::npos)
  614. << EventTypesToString(recorder.events());
  615. window->RemovePreTargetHandler(&recorder);
  616. }
  617. TEST_F(WindowEventDispatcherTest, MouseMovesHeld) {
  618. EventFilterRecorder recorder;
  619. root_window()->AddPreTargetHandler(&recorder);
  620. test::TestWindowDelegate delegate;
  621. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  622. &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
  623. ui::MouseEvent mouse_move_event(ui::ET_MOUSE_MOVED, gfx::Point(0, 0),
  624. gfx::Point(0, 0), ui::EventTimeForNow(), 0,
  625. 0);
  626. DispatchEventUsingWindowDispatcher(&mouse_move_event);
  627. // Discard MOUSE_ENTER.
  628. recorder.Reset();
  629. host()->dispatcher()->HoldPointerMoves();
  630. // Check that we don't immediately dispatch the MOUSE_DRAGGED event.
  631. ui::MouseEvent mouse_dragged_event(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0),
  632. gfx::Point(0, 0), ui::EventTimeForNow(), 0,
  633. 0);
  634. DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
  635. EXPECT_TRUE(recorder.events().empty());
  636. // Check that we do dispatch the held MOUSE_DRAGGED event before another type
  637. // of event.
  638. ui::MouseEvent mouse_pressed_event(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0),
  639. gfx::Point(0, 0), ui::EventTimeForNow(), 0,
  640. 0);
  641. DispatchEventUsingWindowDispatcher(&mouse_pressed_event);
  642. EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
  643. EventTypesToString(recorder.events()));
  644. recorder.Reset();
  645. // Check that we coalesce held MOUSE_DRAGGED events. Note that here (and
  646. // elsewhere in this test) we re-define each event prior to dispatch so that
  647. // it has the correct state (phase, handled, target, etc.).
  648. mouse_dragged_event =
  649. ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
  650. ui::EventTimeForNow(), 0, 0);
  651. ui::MouseEvent mouse_dragged_event2(ui::ET_MOUSE_DRAGGED, gfx::Point(10, 10),
  652. gfx::Point(10, 10), ui::EventTimeForNow(),
  653. 0, 0);
  654. DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
  655. DispatchEventUsingWindowDispatcher(&mouse_dragged_event2);
  656. EXPECT_TRUE(recorder.events().empty());
  657. mouse_pressed_event =
  658. ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), gfx::Point(0, 0),
  659. ui::EventTimeForNow(), 0, 0);
  660. DispatchEventUsingWindowDispatcher(&mouse_pressed_event);
  661. EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
  662. EventTypesToString(recorder.events()));
  663. recorder.Reset();
  664. // Check that on ReleasePointerMoves, held events are not dispatched
  665. // immediately, but posted instead.
  666. mouse_dragged_event =
  667. ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
  668. ui::EventTimeForNow(), 0, 0);
  669. DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
  670. host()->dispatcher()->ReleasePointerMoves();
  671. EXPECT_TRUE(recorder.events().empty());
  672. RunAllPendingInMessageLoop();
  673. EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(recorder.events()));
  674. recorder.Reset();
  675. // However if another message comes in before the dispatch of the posted
  676. // event, check that the posted event is dispatched before this new event.
  677. host()->dispatcher()->HoldPointerMoves();
  678. mouse_dragged_event =
  679. ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
  680. ui::EventTimeForNow(), 0, 0);
  681. DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
  682. host()->dispatcher()->ReleasePointerMoves();
  683. mouse_pressed_event =
  684. ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), gfx::Point(0, 0),
  685. ui::EventTimeForNow(), 0, 0);
  686. DispatchEventUsingWindowDispatcher(&mouse_pressed_event);
  687. EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
  688. EventTypesToString(recorder.events()));
  689. recorder.Reset();
  690. RunAllPendingInMessageLoop();
  691. EXPECT_TRUE(recorder.events().empty());
  692. // Check that if the other message is another MOUSE_DRAGGED, we still coalesce
  693. // them.
  694. host()->dispatcher()->HoldPointerMoves();
  695. mouse_dragged_event =
  696. ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
  697. ui::EventTimeForNow(), 0, 0);
  698. DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
  699. host()->dispatcher()->ReleasePointerMoves();
  700. mouse_dragged_event2 =
  701. ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(10, 10),
  702. gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
  703. DispatchEventUsingWindowDispatcher(&mouse_dragged_event2);
  704. EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(recorder.events()));
  705. recorder.Reset();
  706. RunAllPendingInMessageLoop();
  707. EXPECT_TRUE(recorder.events().empty());
  708. // Check that synthetic mouse move event has a right location when issued
  709. // while holding pointer moves.
  710. mouse_dragged_event =
  711. ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
  712. ui::EventTimeForNow(), 0, 0);
  713. mouse_dragged_event2 =
  714. ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(10, 10),
  715. gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
  716. ui::MouseEvent mouse_dragged_event3(ui::ET_MOUSE_DRAGGED, gfx::Point(28, 28),
  717. gfx::Point(28, 28), ui::EventTimeForNow(),
  718. 0, 0);
  719. host()->dispatcher()->HoldPointerMoves();
  720. DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
  721. DispatchEventUsingWindowDispatcher(&mouse_dragged_event2);
  722. window->SetBounds(gfx::Rect(15, 15, 80, 80));
  723. DispatchEventUsingWindowDispatcher(&mouse_dragged_event3);
  724. RunAllPendingInMessageLoop();
  725. EXPECT_TRUE(recorder.events().empty());
  726. host()->dispatcher()->ReleasePointerMoves();
  727. RunAllPendingInMessageLoop();
  728. EXPECT_EQ("MOUSE_MOVED", EventTypesToString(recorder.events()));
  729. EXPECT_EQ(gfx::Point(13, 13), recorder.mouse_location(0));
  730. recorder.Reset();
  731. root_window()->RemovePreTargetHandler(&recorder);
  732. }
  733. TEST_F(WindowEventDispatcherTest, TouchMovesHeld) {
  734. EventFilterRecorder recorder;
  735. root_window()->AddPreTargetHandler(&recorder);
  736. test::TestWindowDelegate delegate;
  737. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  738. &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
  739. // Starting the touch and throwing out the first few events, since the system
  740. // is going to generate synthetic mouse events that are not relevant to the
  741. // test.
  742. ui::TouchEvent touch_pressed_event(
  743. ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), ui::EventTimeForNow(),
  744. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  745. DispatchEventUsingWindowDispatcher(&touch_pressed_event);
  746. recorder.WaitUntilReceivedEvent(ui::ET_GESTURE_SHOW_PRESS);
  747. recorder.Reset();
  748. host()->dispatcher()->HoldPointerMoves();
  749. // Check that we don't immediately dispatch the TOUCH_MOVED event.
  750. ui::TouchEvent touch_moved_event(
  751. ui::ET_TOUCH_MOVED, gfx::Point(10, 10), ui::EventTimeForNow(),
  752. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  753. ui::TouchEvent touch_moved_event2(
  754. ui::ET_TOUCH_MOVED, gfx::Point(11, 10), ui::EventTimeForNow(),
  755. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  756. ui::TouchEvent touch_moved_event3(
  757. ui::ET_TOUCH_MOVED, gfx::Point(12, 10), ui::EventTimeForNow(),
  758. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  759. DispatchEventUsingWindowDispatcher(&touch_moved_event);
  760. EXPECT_TRUE(recorder.events().empty());
  761. // Check that on ReleasePointerMoves, held events are not dispatched
  762. // immediately, but posted instead.
  763. DispatchEventUsingWindowDispatcher(&touch_moved_event2);
  764. host()->dispatcher()->ReleasePointerMoves();
  765. EXPECT_TRUE(recorder.events().empty());
  766. RunAllPendingInMessageLoop();
  767. EXPECT_EQ("TOUCH_MOVED", EventTypesToString(recorder.events()));
  768. recorder.Reset();
  769. // If another touch event occurs then the held touch should be dispatched
  770. // immediately before it.
  771. ui::TouchEvent touch_released_event(
  772. ui::ET_TOUCH_RELEASED, gfx::Point(10, 10), ui::EventTimeForNow(),
  773. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  774. recorder.Reset();
  775. host()->dispatcher()->HoldPointerMoves();
  776. DispatchEventUsingWindowDispatcher(&touch_moved_event3);
  777. DispatchEventUsingWindowDispatcher(&touch_released_event);
  778. EXPECT_EQ("TOUCH_MOVED TOUCH_RELEASED GESTURE_TAP GESTURE_END",
  779. EventTypesToString(recorder.events()));
  780. recorder.Reset();
  781. host()->dispatcher()->ReleasePointerMoves();
  782. RunAllPendingInMessageLoop();
  783. EXPECT_TRUE(recorder.events().empty());
  784. root_window()->RemovePreTargetHandler(&recorder);
  785. }
  786. // Tests that mouse move event has a right location
  787. // when there isn't the target window
  788. TEST_F(WindowEventDispatcherTest, MouseEventWithoutTargetWindow) {
  789. EventFilterRecorder recorder_first;
  790. EventFilterRecorder recorder_second;
  791. test::TestWindowDelegate delegate;
  792. std::unique_ptr<aura::Window> window_first(CreateTestWindowWithDelegate(
  793. &delegate, 1, gfx::Rect(20, 10, 10, 20), root_window()));
  794. window_first->Show();
  795. window_first->AddPreTargetHandler(&recorder_first);
  796. std::unique_ptr<aura::Window> window_second(CreateTestWindowWithDelegate(
  797. &delegate, 2, gfx::Rect(20, 30, 10, 20), root_window()));
  798. window_second->Show();
  799. window_second->AddPreTargetHandler(&recorder_second);
  800. const gfx::Point event_location(22, 33);
  801. ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location,
  802. ui::EventTimeForNow(), 0, 0);
  803. DispatchEventUsingWindowDispatcher(&mouse);
  804. EXPECT_TRUE(recorder_first.events().empty());
  805. EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED",
  806. EventTypesToString(recorder_second.events()));
  807. ASSERT_EQ(2u, recorder_second.mouse_locations().size());
  808. EXPECT_EQ(gfx::Point(2, 3).ToString(),
  809. recorder_second.mouse_locations()[0].ToString());
  810. window_first->RemovePreTargetHandler(&recorder_first);
  811. window_second->RemovePreTargetHandler(&recorder_second);
  812. }
  813. // Tests that a mouse exit is dispatched to the last mouse location when
  814. // the window is hiddden.
  815. TEST_F(WindowEventDispatcherTest, DispatchMouseExitWhenHidingWindow) {
  816. EventFilterRecorder recorder;
  817. test::TestWindowDelegate delegate;
  818. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  819. &delegate, 1, gfx::Rect(10, 10, 50, 50), root_window()));
  820. window->Show();
  821. window->AddPreTargetHandler(&recorder);
  822. // Dispatch a mouse move event into the window.
  823. const gfx::Point event_location(22, 33);
  824. ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location,
  825. ui::EventTimeForNow(), 0, 0);
  826. DispatchEventUsingWindowDispatcher(&mouse);
  827. EXPECT_FALSE(recorder.events().empty());
  828. recorder.Reset();
  829. // Hide the window and verify a mouse exit event's location.
  830. window->Hide();
  831. EXPECT_FALSE(recorder.events().empty());
  832. EXPECT_EQ("MOUSE_EXITED", EventTypesToString(recorder.events()));
  833. ASSERT_EQ(1u, recorder.mouse_locations().size());
  834. EXPECT_EQ(gfx::Point(12, 23).ToString(),
  835. recorder.mouse_locations()[0].ToString());
  836. window->RemovePreTargetHandler(&recorder);
  837. }
  838. TEST_F(WindowEventDispatcherTest, HeldMovesDispatchMouseExitWhenHidingWindow) {
  839. EventFilterRecorder recorder;
  840. test::TestWindowDelegate delegate;
  841. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  842. &delegate, 1, gfx::Rect(10, 10, 50, 50), root_window()));
  843. window->Show();
  844. window->AddPreTargetHandler(&recorder);
  845. // Dispatch a mouse move event into the window.
  846. const gfx::Point event_location(22, 33);
  847. ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location,
  848. ui::EventTimeForNow(), 0, 0);
  849. DispatchEventUsingWindowDispatcher(&mouse);
  850. EXPECT_FALSE(recorder.events().empty());
  851. recorder.Reset();
  852. // Hide the window and verify a mouse exit event's location.
  853. host()->dispatcher()->HoldPointerMoves();
  854. window->Hide();
  855. host()->dispatcher()->ReleasePointerMoves();
  856. EXPECT_FALSE(recorder.events().empty());
  857. EXPECT_EQ("MOUSE_EXITED", EventTypesToString(recorder.events()));
  858. ASSERT_EQ(1u, recorder.mouse_locations().size());
  859. EXPECT_EQ(gfx::Point(12, 23).ToString(),
  860. recorder.mouse_locations()[0].ToString());
  861. window->RemovePreTargetHandler(&recorder);
  862. }
  863. // Tests that a mouse-exit event is not synthesized during shutdown.
  864. TEST_F(WindowEventDispatcherTest, NoMouseExitInShutdown) {
  865. EventFilterRecorder recorder;
  866. test::TestWindowDelegate delegate;
  867. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  868. &delegate, 1, gfx::Rect(10, 10, 50, 50), root_window()));
  869. window->Show();
  870. window->AddPreTargetHandler(&recorder);
  871. // Simulate mouse move into the window.
  872. const gfx::Point event_location = window->bounds().CenterPoint();
  873. ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location,
  874. ui::EventTimeForNow(), 0, 0);
  875. DispatchEventUsingWindowDispatcher(&mouse);
  876. EXPECT_FALSE(recorder.events().empty());
  877. recorder.Reset();
  878. // Simulate shutdown.
  879. host()->dispatcher()->Shutdown();
  880. // Hiding the window does not generate a mouse-exit event.
  881. window->Hide();
  882. EXPECT_TRUE(recorder.events().empty());
  883. window->RemovePreTargetHandler(&recorder);
  884. }
  885. // Verifies that a direct call to ProcessedTouchEvent() does not cause a crash.
  886. TEST_F(WindowEventDispatcherTest, CallToProcessedTouchEvent) {
  887. test::TestWindowDelegate delegate;
  888. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  889. &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
  890. host()->dispatcher()->ProcessedTouchEvent(
  891. 0, window.get(), ui::ER_UNHANDLED,
  892. false /* is_source_touch_event_set_blocking */);
  893. }
  894. // This event handler requests the dispatcher to start holding pointer-move
  895. // events when it receives the first scroll-update gesture.
  896. class HoldPointerOnScrollHandler : public ui::test::TestEventHandler {
  897. public:
  898. HoldPointerOnScrollHandler(WindowEventDispatcher* dispatcher,
  899. EventFilterRecorder* filter)
  900. : dispatcher_(dispatcher), filter_(filter), holding_moves_(false) {}
  901. HoldPointerOnScrollHandler(const HoldPointerOnScrollHandler&) = delete;
  902. HoldPointerOnScrollHandler& operator=(const HoldPointerOnScrollHandler&) =
  903. delete;
  904. ~HoldPointerOnScrollHandler() override = default;
  905. private:
  906. // ui::test::TestEventHandler:
  907. void OnGestureEvent(ui::GestureEvent* gesture) override {
  908. if (!holding_moves_ && gesture->type() == ui::ET_GESTURE_SCROLL_UPDATE) {
  909. holding_moves_ = true;
  910. dispatcher_->HoldPointerMoves();
  911. filter_->Reset();
  912. } else if (gesture->type() == ui::ET_GESTURE_SCROLL_END) {
  913. dispatcher_->ReleasePointerMoves();
  914. holding_moves_ = false;
  915. }
  916. }
  917. raw_ptr<WindowEventDispatcher> dispatcher_;
  918. raw_ptr<EventFilterRecorder> filter_;
  919. bool holding_moves_;
  920. };
  921. // Tests that touch-move events don't contribute to an in-progress scroll
  922. // gesture if touch-move events are being held by the dispatcher.
  923. TEST_F(WindowEventDispatcherTest, TouchMovesHeldOnScroll) {
  924. EventFilterRecorder recorder;
  925. root_window()->AddPreTargetHandler(&recorder);
  926. test::TestWindowDelegate delegate;
  927. HoldPointerOnScrollHandler handler(host()->dispatcher(), &recorder);
  928. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  929. &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
  930. window->AddPreTargetHandler(&handler);
  931. ui::test::EventGenerator generator(root_window());
  932. generator.GestureScrollSequence(gfx::Point(60, 60), gfx::Point(10, 60),
  933. base::Milliseconds(100), 25);
  934. // |handler| will have reset |filter| and started holding the touch-move
  935. // events when scrolling started. At the end of the scroll (i.e. upon
  936. // touch-release), the held touch-move event will have been dispatched first,
  937. // along with the subsequent events (i.e. touch-release, scroll-end, and
  938. // gesture-end).
  939. const EventFilterRecorder::Events& events = recorder.events();
  940. EXPECT_EQ(
  941. "TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
  942. "GESTURE_SCROLL_END GESTURE_END",
  943. EventTypesToString(events));
  944. ASSERT_EQ(2u, recorder.touch_locations().size());
  945. EXPECT_EQ(gfx::Point(-40, 10).ToString(),
  946. recorder.touch_locations()[0].ToString());
  947. EXPECT_EQ(gfx::Point(-40, 10).ToString(),
  948. recorder.touch_locations()[1].ToString());
  949. window->RemovePreTargetHandler(&handler);
  950. root_window()->RemovePreTargetHandler(&recorder);
  951. }
  952. // Tests that a 'held' touch-event does contribute to gesture event when it is
  953. // dispatched.
  954. TEST_F(WindowEventDispatcherTest, HeldTouchMoveContributesToGesture) {
  955. EventFilterRecorder recorder;
  956. root_window()->AddPreTargetHandler(&recorder);
  957. const gfx::Point location(20, 20);
  958. ui::TouchEvent press(ui::ET_TOUCH_PRESSED, location, ui::EventTimeForNow(),
  959. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  960. DispatchEventUsingWindowDispatcher(&press);
  961. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_PRESSED));
  962. recorder.Reset();
  963. host()->dispatcher()->HoldPointerMoves();
  964. ui::TouchEvent move(ui::ET_TOUCH_MOVED, location + gfx::Vector2d(100, 100),
  965. ui::EventTimeForNow(),
  966. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  967. DispatchEventUsingWindowDispatcher(&move);
  968. EXPECT_FALSE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
  969. EXPECT_FALSE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN));
  970. recorder.Reset();
  971. host()->dispatcher()->ReleasePointerMoves();
  972. EXPECT_FALSE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
  973. RunAllPendingInMessageLoop();
  974. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
  975. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN));
  976. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE));
  977. root_window()->RemovePreTargetHandler(&recorder);
  978. }
  979. // Tests that synthetic mouse events are ignored when mouse
  980. // events are disabled.
  981. TEST_F(WindowEventDispatcherTest, DispatchSyntheticMouseEvents) {
  982. EventFilterRecorder recorder;
  983. root_window()->AddPreTargetHandler(&recorder);
  984. test::TestWindowDelegate delegate;
  985. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  986. &delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
  987. window->Show();
  988. window->SetCapture();
  989. test::TestCursorClient cursor_client(root_window());
  990. // Dispatch a non-synthetic mouse event when mouse events are enabled.
  991. ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
  992. gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
  993. DispatchEventUsingWindowDispatcher(&mouse1);
  994. EXPECT_FALSE(recorder.events().empty());
  995. recorder.Reset();
  996. // Dispatch a synthetic mouse event when mouse events are enabled.
  997. ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
  998. gfx::Point(10, 10), ui::EventTimeForNow(),
  999. ui::EF_IS_SYNTHESIZED, 0);
  1000. DispatchEventUsingWindowDispatcher(&mouse2);
  1001. EXPECT_FALSE(recorder.events().empty());
  1002. recorder.Reset();
  1003. // Dispatch a synthetic mouse event when mouse events are disabled.
  1004. cursor_client.DisableMouseEvents();
  1005. DispatchEventUsingWindowDispatcher(&mouse2);
  1006. EXPECT_TRUE(recorder.events().empty());
  1007. root_window()->RemovePreTargetHandler(&recorder);
  1008. }
  1009. // Tests that a mouse-move event is not synthesized when a mouse-button is down.
  1010. TEST_F(WindowEventDispatcherTest, DoNotSynthesizeWhileButtonDown) {
  1011. EventFilterRecorder recorder;
  1012. test::TestWindowDelegate delegate;
  1013. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  1014. &delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
  1015. window->Show();
  1016. window->AddPreTargetHandler(&recorder);
  1017. // Dispatch a non-synthetic mouse event when mouse events are enabled.
  1018. ui::MouseEvent mouse1(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
  1019. gfx::Point(10, 10), ui::EventTimeForNow(),
  1020. ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
  1021. DispatchEventUsingWindowDispatcher(&mouse1);
  1022. ASSERT_EQ(1u, recorder.events().size());
  1023. EXPECT_EQ(ui::ET_MOUSE_PRESSED, recorder.events()[0]);
  1024. window->RemovePreTargetHandler(&recorder);
  1025. recorder.Reset();
  1026. // Move |window| away from underneath the cursor.
  1027. root_window()->AddPreTargetHandler(&recorder);
  1028. window->SetBounds(gfx::Rect(30, 30, 100, 100));
  1029. EXPECT_TRUE(recorder.events().empty());
  1030. RunAllPendingInMessageLoop();
  1031. EXPECT_TRUE(recorder.events().empty());
  1032. root_window()->RemovePreTargetHandler(&recorder);
  1033. }
  1034. // Tests that a mouse-press event is not dispatched during shutdown.
  1035. TEST_F(WindowEventDispatcherTest, DoNotDispatchInShutdown) {
  1036. EventFilterRecorder recorder;
  1037. test::TestWindowDelegate delegate;
  1038. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  1039. &delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
  1040. window->Show();
  1041. window->AddPreTargetHandler(&recorder);
  1042. // Simulate shutdown.
  1043. host()->dispatcher()->Shutdown();
  1044. // Attempt to dispatch a mouse press.
  1045. const gfx::Point center = window->bounds().CenterPoint();
  1046. ui::MouseEvent press(ui::ET_MOUSE_PRESSED, center, center,
  1047. ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
  1048. ui::EF_LEFT_MOUSE_BUTTON);
  1049. DispatchEventUsingWindowDispatcher(&press);
  1050. // Event was not dispatched.
  1051. EXPECT_TRUE(recorder.events().empty());
  1052. window->RemovePreTargetHandler(&recorder);
  1053. }
  1054. #if BUILDFLAG(IS_WIN) && defined(ARCH_CPU_X86)
  1055. #define MAYBE(x) DISABLED_##x
  1056. #else
  1057. #define MAYBE(x) x
  1058. #endif
  1059. // Tests synthetic mouse events generated when window bounds changes such that
  1060. // the cursor previously outside the window becomes inside, or vice versa.
  1061. // Do not synthesize events if the window ignores events or is invisible.
  1062. // Flaky on 32-bit Windows bots. http://crbug.com/388272
  1063. TEST_F(WindowEventDispatcherTest,
  1064. MAYBE(SynthesizeMouseEventsOnWindowBoundsChanged)) {
  1065. test::TestCursorClient cursor_client(root_window());
  1066. cursor_client.ShowCursor();
  1067. test::TestWindowDelegate delegate;
  1068. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  1069. &delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
  1070. window->Show();
  1071. window->SetCapture();
  1072. EventFilterRecorder recorder;
  1073. window->AddPreTargetHandler(&recorder);
  1074. // Dispatch a non-synthetic mouse event to place cursor inside window bounds.
  1075. ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
  1076. gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
  1077. DispatchEventUsingWindowDispatcher(&mouse);
  1078. EXPECT_FALSE(recorder.events().empty());
  1079. recorder.Reset();
  1080. // Update the window bounds so that cursor is now outside the window.
  1081. // This should trigger a synthetic MOVED event.
  1082. gfx::Rect bounds1(20, 20, 100, 100);
  1083. window->SetBounds(bounds1);
  1084. RunAllPendingInMessageLoop();
  1085. ASSERT_FALSE(recorder.events().empty());
  1086. ASSERT_FALSE(recorder.mouse_event_flags().empty());
  1087. EXPECT_EQ(ui::ET_MOUSE_MOVED, recorder.events().back());
  1088. EXPECT_EQ(ui::EF_IS_SYNTHESIZED, recorder.mouse_event_flags().back());
  1089. recorder.Reset();
  1090. // Update the window bounds so that cursor is back inside the window.
  1091. // The origin of window bounds change so this should trigger a synthetic
  1092. // event.
  1093. gfx::Rect bounds2(5, 5, 100, 100);
  1094. window->SetBounds(bounds2);
  1095. RunAllPendingInMessageLoop();
  1096. ASSERT_FALSE(recorder.events().empty());
  1097. ASSERT_FALSE(recorder.mouse_event_flags().empty());
  1098. EXPECT_EQ(ui::ET_MOUSE_MOVED, recorder.events().back());
  1099. EXPECT_EQ(ui::EF_IS_SYNTHESIZED, recorder.mouse_event_flags().back());
  1100. recorder.Reset();
  1101. // Update the window bounds so that cursor is still inside the window.
  1102. // The origin of window bounds doesn't change so this should not trigger
  1103. // a synthetic event.
  1104. gfx::Rect bounds3(5, 5, 200, 200);
  1105. window->SetBounds(bounds3);
  1106. RunAllPendingInMessageLoop();
  1107. EXPECT_TRUE(recorder.events().empty());
  1108. recorder.Reset();
  1109. // Set window to ignore events.
  1110. window->SetEventTargetingPolicy(EventTargetingPolicy::kNone);
  1111. // Update the window bounds so that cursor is from outside to inside the
  1112. // window. This should not trigger a synthetic event.
  1113. window->SetBounds(bounds1);
  1114. RunAllPendingInMessageLoop();
  1115. EXPECT_TRUE(recorder.events().empty());
  1116. recorder.Reset();
  1117. window->SetBounds(bounds2);
  1118. RunAllPendingInMessageLoop();
  1119. EXPECT_TRUE(recorder.events().empty());
  1120. recorder.Reset();
  1121. // Set window to accept events but invisible.
  1122. window->SetEventTargetingPolicy(EventTargetingPolicy::kTargetAndDescendants);
  1123. window->Hide();
  1124. recorder.Reset();
  1125. // Update the window bounds so that cursor is outside the window.
  1126. // This should not trigger a synthetic event.
  1127. window->SetBounds(bounds1);
  1128. RunAllPendingInMessageLoop();
  1129. EXPECT_TRUE(recorder.events().empty());
  1130. // Hide the cursor. None of the following scenario should trigger
  1131. // a synthetic event.
  1132. cursor_client.HideCursor();
  1133. window->Show();
  1134. RunAllPendingInMessageLoop();
  1135. EXPECT_TRUE(recorder.events().empty());
  1136. window->SetBounds(bounds2);
  1137. RunAllPendingInMessageLoop();
  1138. EXPECT_TRUE(recorder.events().empty());
  1139. window->SetBounds(bounds1);
  1140. RunAllPendingInMessageLoop();
  1141. EXPECT_TRUE(recorder.events().empty());
  1142. cursor_client.ShowCursor();
  1143. window->SetBounds(bounds2);
  1144. RunAllPendingInMessageLoop();
  1145. ASSERT_FALSE(recorder.events().empty());
  1146. ASSERT_FALSE(recorder.mouse_event_flags().empty());
  1147. EXPECT_EQ(ui::ET_MOUSE_MOVED, recorder.events().back());
  1148. EXPECT_EQ(ui::EF_IS_SYNTHESIZED, recorder.mouse_event_flags().back());
  1149. recorder.Reset();
  1150. window->RemovePreTargetHandler(&recorder);
  1151. }
  1152. // Tests that a mouse exit is dispatched to the last known cursor location
  1153. // when the cursor becomes invisible.
  1154. TEST_F(WindowEventDispatcherTest, DispatchMouseExitWhenCursorHidden) {
  1155. EventFilterRecorder recorder;
  1156. root_window()->AddPreTargetHandler(&recorder);
  1157. test::TestWindowDelegate delegate;
  1158. gfx::Point window_origin(7, 18);
  1159. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  1160. &delegate, 1234, gfx::Rect(window_origin, gfx::Size(100, 100)),
  1161. root_window()));
  1162. window->Show();
  1163. // Dispatch a mouse move event into the window.
  1164. gfx::Point mouse_location(gfx::Point(15, 25));
  1165. ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, mouse_location, mouse_location,
  1166. ui::EventTimeForNow(), 0, 0);
  1167. EXPECT_TRUE(recorder.events().empty());
  1168. DispatchEventUsingWindowDispatcher(&mouse1);
  1169. EXPECT_FALSE(recorder.events().empty());
  1170. recorder.Reset();
  1171. // Hide the cursor and verify a mouse exit was dispatched.
  1172. host()->OnCursorVisibilityChanged(false);
  1173. EXPECT_FALSE(recorder.events().empty());
  1174. EXPECT_EQ("MOUSE_EXITED", EventTypesToString(recorder.events()));
  1175. // Verify the mouse exit was dispatched at the correct location
  1176. // (in the correct coordinate space).
  1177. int translated_x = mouse_location.x() - window_origin.x();
  1178. int translated_y = mouse_location.y() - window_origin.y();
  1179. gfx::Point translated_point(translated_x, translated_y);
  1180. EXPECT_EQ(recorder.mouse_location(0).ToString(), translated_point.ToString());
  1181. // Verify the mouse exit with ui::EF_CURSOR_HIDE flags.
  1182. EXPECT_TRUE(recorder.mouse_event_flags()[0] & ui::EF_CURSOR_HIDE);
  1183. root_window()->RemovePreTargetHandler(&recorder);
  1184. }
  1185. // Tests that a synthetic mouse exit is dispatched to the last known cursor
  1186. // location after mouse events are disabled on the cursor client.
  1187. TEST_F(WindowEventDispatcherTest,
  1188. DispatchSyntheticMouseExitAfterMouseEventsDisabled) {
  1189. EventFilterRecorder recorder;
  1190. root_window()->AddPreTargetHandler(&recorder);
  1191. test::TestWindowDelegate delegate;
  1192. gfx::Point window_origin(7, 18);
  1193. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  1194. &delegate, 1234, gfx::Rect(window_origin, gfx::Size(100, 100)),
  1195. root_window()));
  1196. window->Show();
  1197. // Dispatch a mouse move event into the window.
  1198. gfx::Point mouse_location(gfx::Point(15, 25));
  1199. ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, mouse_location, mouse_location,
  1200. ui::EventTimeForNow(), 0, 0);
  1201. EXPECT_TRUE(recorder.events().empty());
  1202. DispatchEventUsingWindowDispatcher(&mouse1);
  1203. EXPECT_FALSE(recorder.events().empty());
  1204. recorder.Reset();
  1205. test::TestCursorClient cursor_client(root_window());
  1206. cursor_client.DisableMouseEvents();
  1207. gfx::Point mouse_exit_location(gfx::Point(150, 150));
  1208. ui::MouseEvent mouse2(ui::ET_MOUSE_EXITED, gfx::Point(150, 150),
  1209. gfx::Point(150, 150), ui::EventTimeForNow(),
  1210. ui::EF_IS_SYNTHESIZED, 0);
  1211. DispatchEventUsingWindowDispatcher(&mouse2);
  1212. EXPECT_FALSE(recorder.events().empty());
  1213. // We get the mouse exited event twice in our filter. Once during the
  1214. // predispatch phase and during the actual dispatch.
  1215. EXPECT_EQ("MOUSE_EXITED MOUSE_EXITED", EventTypesToString(recorder.events()));
  1216. // Verify the mouse exit was dispatched at the correct location
  1217. // (in the correct coordinate space).
  1218. int translated_x = mouse_exit_location.x() - window_origin.x();
  1219. int translated_y = mouse_exit_location.y() - window_origin.y();
  1220. gfx::Point translated_point(translated_x, translated_y);
  1221. EXPECT_EQ(recorder.mouse_location(0).ToString(), translated_point.ToString());
  1222. root_window()->RemovePreTargetHandler(&recorder);
  1223. }
  1224. class DeletingEventFilter : public ui::EventHandler {
  1225. public:
  1226. DeletingEventFilter() : delete_during_pre_handle_(false) {}
  1227. DeletingEventFilter(const DeletingEventFilter&) = delete;
  1228. DeletingEventFilter& operator=(const DeletingEventFilter&) = delete;
  1229. ~DeletingEventFilter() override = default;
  1230. void Reset(bool delete_during_pre_handle) {
  1231. delete_during_pre_handle_ = delete_during_pre_handle;
  1232. }
  1233. private:
  1234. // Overridden from ui::EventHandler:
  1235. void OnKeyEvent(ui::KeyEvent* event) override {
  1236. if (delete_during_pre_handle_)
  1237. delete event->target();
  1238. }
  1239. void OnMouseEvent(ui::MouseEvent* event) override {
  1240. if (delete_during_pre_handle_)
  1241. delete event->target();
  1242. }
  1243. bool delete_during_pre_handle_;
  1244. };
  1245. class DeletingWindowDelegate : public test::TestWindowDelegate {
  1246. public:
  1247. DeletingWindowDelegate()
  1248. : window_(nullptr), delete_during_handle_(false), got_event_(false) {}
  1249. DeletingWindowDelegate(const DeletingWindowDelegate&) = delete;
  1250. DeletingWindowDelegate& operator=(const DeletingWindowDelegate&) = delete;
  1251. ~DeletingWindowDelegate() override = default;
  1252. void Reset(Window* window, bool delete_during_handle) {
  1253. window_ = window;
  1254. delete_during_handle_ = delete_during_handle;
  1255. got_event_ = false;
  1256. }
  1257. bool got_event() const { return got_event_; }
  1258. private:
  1259. // Overridden from WindowDelegate:
  1260. void OnKeyEvent(ui::KeyEvent* event) override {
  1261. if (delete_during_handle_)
  1262. delete window_;
  1263. got_event_ = true;
  1264. }
  1265. void OnMouseEvent(ui::MouseEvent* event) override {
  1266. if (delete_during_handle_)
  1267. delete window_;
  1268. got_event_ = true;
  1269. }
  1270. raw_ptr<Window> window_;
  1271. bool delete_during_handle_;
  1272. bool got_event_;
  1273. };
  1274. TEST_F(WindowEventDispatcherTest, DeleteWindowDuringDispatch) {
  1275. // Verifies that we can delete a window during each phase of event handling.
  1276. // Deleting the window should not cause a crash, only prevent further
  1277. // processing from occurring.
  1278. std::unique_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
  1279. DeletingWindowDelegate d11;
  1280. Window* w11 = CreateNormalWindow(11, w1.get(), &d11);
  1281. WindowTracker tracker;
  1282. DeletingEventFilter w1_filter;
  1283. w1->AddPreTargetHandler(&w1_filter);
  1284. client::GetFocusClient(w1.get())->FocusWindow(w11);
  1285. ui::test::EventGenerator generator(root_window(), w11);
  1286. // First up, no one deletes anything.
  1287. tracker.Add(w11);
  1288. d11.Reset(w11, false);
  1289. generator.PressLeftButton();
  1290. EXPECT_TRUE(tracker.Contains(w11));
  1291. EXPECT_TRUE(d11.got_event());
  1292. generator.ReleaseLeftButton();
  1293. // Delegate deletes w11. This will prevent the post-handle step from applying.
  1294. w1_filter.Reset(false);
  1295. d11.Reset(w11, true);
  1296. generator.PressKey(ui::VKEY_A, 0);
  1297. EXPECT_FALSE(tracker.Contains(w11));
  1298. EXPECT_TRUE(d11.got_event());
  1299. // Pre-handle step deletes w11. This will prevent the delegate and the post-
  1300. // handle steps from applying.
  1301. w11 = CreateNormalWindow(11, w1.get(), &d11);
  1302. w1_filter.Reset(true);
  1303. d11.Reset(w11, false);
  1304. generator.PressLeftButton();
  1305. EXPECT_FALSE(tracker.Contains(w11));
  1306. EXPECT_FALSE(d11.got_event());
  1307. w1->RemovePreTargetHandler(&w1_filter);
  1308. }
  1309. namespace {
  1310. // A window delegate that detaches the parent of the target's parent window when
  1311. // it receives a tap event.
  1312. class DetachesParentOnTapDelegate : public test::TestWindowDelegate {
  1313. public:
  1314. DetachesParentOnTapDelegate() {}
  1315. DetachesParentOnTapDelegate(const DetachesParentOnTapDelegate&) = delete;
  1316. DetachesParentOnTapDelegate& operator=(const DetachesParentOnTapDelegate&) =
  1317. delete;
  1318. ~DetachesParentOnTapDelegate() override {}
  1319. private:
  1320. void OnGestureEvent(ui::GestureEvent* event) override {
  1321. if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
  1322. event->SetHandled();
  1323. return;
  1324. }
  1325. if (event->type() == ui::ET_GESTURE_TAP) {
  1326. Window* parent = static_cast<Window*>(event->target())->parent();
  1327. parent->parent()->RemoveChild(parent);
  1328. event->SetHandled();
  1329. }
  1330. }
  1331. };
  1332. } // namespace
  1333. // Tests that the gesture recognizer is reset for all child windows when a
  1334. // window hides. No expectations, just checks that the test does not crash.
  1335. TEST_F(WindowEventDispatcherTest,
  1336. GestureRecognizerResetsTargetWhenParentHides) {
  1337. std::unique_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
  1338. DetachesParentOnTapDelegate delegate;
  1339. std::unique_ptr<Window> parent(CreateNormalWindow(22, w1.get(), NULL));
  1340. Window* child = CreateNormalWindow(11, parent.get(), &delegate);
  1341. ui::test::EventGenerator generator(root_window(), child);
  1342. generator.GestureTapAt(gfx::Point(40, 40));
  1343. }
  1344. namespace {
  1345. // A window delegate that processes nested gestures on tap.
  1346. class NestedGestureDelegate : public test::TestWindowDelegate {
  1347. public:
  1348. NestedGestureDelegate(ui::test::EventGenerator* generator,
  1349. const gfx::Point tap_location)
  1350. : generator_(generator),
  1351. tap_location_(tap_location),
  1352. gesture_end_count_(0) {}
  1353. NestedGestureDelegate(const NestedGestureDelegate&) = delete;
  1354. NestedGestureDelegate& operator=(const NestedGestureDelegate&) = delete;
  1355. ~NestedGestureDelegate() override {}
  1356. int gesture_end_count() const { return gesture_end_count_; }
  1357. private:
  1358. void OnGestureEvent(ui::GestureEvent* event) override {
  1359. switch (event->type()) {
  1360. case ui::ET_GESTURE_TAP_DOWN:
  1361. event->SetHandled();
  1362. break;
  1363. case ui::ET_GESTURE_TAP:
  1364. if (generator_)
  1365. generator_->GestureTapAt(tap_location_);
  1366. event->SetHandled();
  1367. break;
  1368. case ui::ET_GESTURE_END:
  1369. ++gesture_end_count_;
  1370. break;
  1371. default:
  1372. break;
  1373. }
  1374. }
  1375. raw_ptr<ui::test::EventGenerator> generator_;
  1376. const gfx::Point tap_location_;
  1377. int gesture_end_count_;
  1378. };
  1379. } // namespace
  1380. // Tests that gesture end is delivered after nested gesture processing.
  1381. TEST_F(WindowEventDispatcherTest, GestureEndDeliveredAfterNestedGestures) {
  1382. NestedGestureDelegate d1(NULL, gfx::Point());
  1383. std::unique_ptr<Window> w1(CreateNormalWindow(1, root_window(), &d1));
  1384. w1->SetBounds(gfx::Rect(0, 0, 100, 100));
  1385. ui::test::EventGenerator nested_generator(root_window(), w1.get());
  1386. NestedGestureDelegate d2(&nested_generator, w1->bounds().CenterPoint());
  1387. std::unique_ptr<Window> w2(CreateNormalWindow(1, root_window(), &d2));
  1388. w2->SetBounds(gfx::Rect(100, 0, 100, 100));
  1389. // Tap on w2 which triggers nested gestures for w1.
  1390. ui::test::EventGenerator generator(root_window(), w2.get());
  1391. generator.GestureTapAt(w2->bounds().CenterPoint());
  1392. // Both windows should get their gesture end events.
  1393. EXPECT_EQ(1, d1.gesture_end_count());
  1394. EXPECT_EQ(1, d2.gesture_end_count());
  1395. }
  1396. // Tests whether we can repost the Tap down gesture event.
  1397. TEST_F(WindowEventDispatcherTest, RepostTapdownGestureTest) {
  1398. EventFilterRecorder recorder;
  1399. root_window()->AddPreTargetHandler(&recorder);
  1400. test::TestWindowDelegate delegate;
  1401. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  1402. &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
  1403. ui::GestureEventDetails details(ui::ET_GESTURE_TAP_DOWN);
  1404. gfx::Point point(10, 10);
  1405. ui::GestureEvent event(point.x(), point.y(), 0, ui::EventTimeForNow(),
  1406. details);
  1407. host()->dispatcher()->RepostEvent(&event);
  1408. RunAllPendingInMessageLoop();
  1409. // TODO(rbyers): Currently disabled - crbug.com/170987
  1410. EXPECT_FALSE(EventTypesToString(recorder.events()).find("GESTURE_TAP_DOWN") !=
  1411. std::string::npos);
  1412. recorder.Reset();
  1413. root_window()->RemovePreTargetHandler(&recorder);
  1414. }
  1415. // This class inherits from the EventFilterRecorder class which provides a
  1416. // facility to record events. This class additionally provides a facility to
  1417. // repost the ET_GESTURE_TAP_DOWN gesture to the target window and records
  1418. // events after that.
  1419. class RepostGestureEventRecorder : public EventFilterRecorder {
  1420. public:
  1421. RepostGestureEventRecorder(aura::Window* repost_source,
  1422. aura::Window* repost_target)
  1423. : repost_source_(repost_source),
  1424. repost_target_(repost_target),
  1425. reposted_(false),
  1426. done_cleanup_(false) {}
  1427. RepostGestureEventRecorder(const RepostGestureEventRecorder&) = delete;
  1428. RepostGestureEventRecorder& operator=(const RepostGestureEventRecorder&) =
  1429. delete;
  1430. ~RepostGestureEventRecorder() override {}
  1431. void OnTouchEvent(ui::TouchEvent* event) override {
  1432. if (reposted_ && event->type() == ui::ET_TOUCH_PRESSED) {
  1433. done_cleanup_ = true;
  1434. Reset();
  1435. }
  1436. EventFilterRecorder::OnTouchEvent(event);
  1437. }
  1438. void OnGestureEvent(ui::GestureEvent* event) override {
  1439. EXPECT_EQ(done_cleanup_ ? repost_target_.get() : repost_source_.get(),
  1440. event->target());
  1441. if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
  1442. if (!reposted_) {
  1443. EXPECT_NE(repost_target_, event->target());
  1444. reposted_ = true;
  1445. repost_target_->GetHost()->dispatcher()->RepostEvent(event);
  1446. // Ensure that the reposted gesture event above goes to the
  1447. // repost_target_;
  1448. repost_source_->GetRootWindow()->RemoveChild(repost_source_);
  1449. return;
  1450. }
  1451. }
  1452. EventFilterRecorder::OnGestureEvent(event);
  1453. }
  1454. // Ignore mouse events as they don't fire at all times. This causes
  1455. // the GestureRepostEventOrder test to fail randomly.
  1456. void OnMouseEvent(ui::MouseEvent* event) override {}
  1457. private:
  1458. raw_ptr<aura::Window> repost_source_;
  1459. raw_ptr<aura::Window> repost_target_;
  1460. // set to true if we reposted the ET_GESTURE_TAP_DOWN event.
  1461. bool reposted_;
  1462. // set true if we're done cleaning up after hiding repost_source_;
  1463. bool done_cleanup_;
  1464. };
  1465. // Tests whether events which are generated after the reposted gesture event
  1466. // are received after that. In this case the scroll sequence events should
  1467. // be received after the reposted gesture event.
  1468. TEST_F(WindowEventDispatcherTest, GestureRepostEventOrder) {
  1469. // Expected events at the end for the repost_target window defined below.
  1470. const char kExpectedTargetEvents[] =
  1471. // TODO)(rbyers): Gesture event reposting is disabled - crbug.com/279039.
  1472. // "GESTURE_BEGIN GESTURE_TAP_DOWN "
  1473. "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
  1474. "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
  1475. "TOUCH_MOVED "
  1476. "GESTURE_SCROLL_UPDATE TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
  1477. "GESTURE_SCROLL_END GESTURE_END";
  1478. // We create two windows.
  1479. // The first window (repost_source) is the one to which the initial tap
  1480. // gesture is sent. It reposts this event to the second window
  1481. // (repost_target).
  1482. // We then generate the scroll sequence for repost_target and look for two
  1483. // ET_GESTURE_TAP_DOWN events in the event list at the end.
  1484. test::TestWindowDelegate delegate;
  1485. std::unique_ptr<aura::Window> repost_target(CreateTestWindowWithDelegate(
  1486. &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
  1487. std::unique_ptr<aura::Window> repost_source(CreateTestWindowWithDelegate(
  1488. &delegate, 1, gfx::Rect(0, 0, 50, 50), root_window()));
  1489. RepostGestureEventRecorder repost_event_recorder(repost_source.get(),
  1490. repost_target.get());
  1491. root_window()->AddPreTargetHandler(&repost_event_recorder);
  1492. // Generate a tap down gesture for the repost_source. This will be reposted
  1493. // to repost_target.
  1494. ui::test::EventGenerator repost_generator(root_window(), repost_source.get());
  1495. repost_generator.GestureTapAt(gfx::Point(40, 40));
  1496. RunAllPendingInMessageLoop();
  1497. ui::test::EventGenerator scroll_generator(root_window(), repost_target.get());
  1498. scroll_generator.GestureScrollSequence(
  1499. gfx::Point(80, 80), gfx::Point(100, 100), base::Milliseconds(100), 3);
  1500. RunAllPendingInMessageLoop();
  1501. int tap_down_count = 0;
  1502. for (size_t i = 0; i < repost_event_recorder.events().size(); ++i) {
  1503. if (repost_event_recorder.events()[i] == ui::ET_GESTURE_TAP_DOWN)
  1504. ++tap_down_count;
  1505. }
  1506. // We expect two tap down events. One from the repost and the other one from
  1507. // the scroll sequence posted above.
  1508. // TODO(rbyers): Currently disabled - crbug.com/170987
  1509. EXPECT_EQ(1, tap_down_count);
  1510. EXPECT_EQ(kExpectedTargetEvents,
  1511. EventTypesToString(repost_event_recorder.events()));
  1512. root_window()->RemovePreTargetHandler(&repost_event_recorder);
  1513. }
  1514. // An event filter that deletes the specified object when sees a mouse-exited
  1515. // event.
  1516. template <class T>
  1517. class OnMouseExitDeletingEventFilter : public EventFilterRecorder {
  1518. public:
  1519. explicit OnMouseExitDeletingEventFilter(T* object_to_delete)
  1520. : object_to_delete_(object_to_delete) {}
  1521. OnMouseExitDeletingEventFilter() : object_to_delete_(nullptr) {}
  1522. OnMouseExitDeletingEventFilter(const OnMouseExitDeletingEventFilter&) =
  1523. delete;
  1524. OnMouseExitDeletingEventFilter& operator=(
  1525. const OnMouseExitDeletingEventFilter&) = delete;
  1526. ~OnMouseExitDeletingEventFilter() override {}
  1527. void set_object_to_delete(T* object_to_delete) {
  1528. object_to_delete_ = object_to_delete;
  1529. }
  1530. void set_delete_closure(base::OnceClosure delete_closure) {
  1531. delete_closure_ = std::move(delete_closure);
  1532. }
  1533. private:
  1534. // Overridden from ui::EventFilterRecorder.
  1535. void OnMouseEvent(ui::MouseEvent* event) override {
  1536. EventFilterRecorder::OnMouseEvent(event);
  1537. if (object_to_delete_ && event->type() == ui::ET_MOUSE_EXITED) {
  1538. if (delete_closure_)
  1539. std::move(delete_closure_).Run();
  1540. delete object_to_delete_;
  1541. object_to_delete_ = nullptr;
  1542. }
  1543. }
  1544. // Closure that is run prior to |object_to_delete_| being deleted.
  1545. base::OnceClosure delete_closure_;
  1546. raw_ptr<T> object_to_delete_;
  1547. };
  1548. // Tests that RootWindow drops mouse-moved event that is supposed to be sent to
  1549. // a child, but the child is destroyed because of the synthesized mouse-exit
  1550. // event generated on the previous mouse_moved_handler_.
  1551. TEST_F(WindowEventDispatcherTest, DeleteWindowDuringMouseMovedDispatch) {
  1552. // Create window 1 and set its event filter. Window 1 will take ownership of
  1553. // the event filter.
  1554. std::unique_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
  1555. OnMouseExitDeletingEventFilter<Window> w1_filter;
  1556. w1->AddPreTargetHandler(&w1_filter);
  1557. w1->SetBounds(gfx::Rect(20, 20, 60, 60));
  1558. EXPECT_EQ(NULL, host()->dispatcher()->mouse_moved_handler());
  1559. ui::test::EventGenerator generator(root_window(), w1.get());
  1560. // Move mouse over window 1 to set it as the |mouse_moved_handler_| for the
  1561. // root window.
  1562. generator.MoveMouseTo(51, 51);
  1563. EXPECT_EQ(w1.get(), host()->dispatcher()->mouse_moved_handler());
  1564. // Create window 2 under the mouse cursor and stack it above window 1.
  1565. Window* w2 = CreateNormalWindow(2, root_window(), NULL);
  1566. w2->SetBounds(gfx::Rect(30, 30, 40, 40));
  1567. root_window()->StackChildAbove(w2, w1.get());
  1568. // Set window 2 as the window that is to be deleted when a mouse-exited event
  1569. // happens on window 1.
  1570. w1_filter.set_object_to_delete(w2);
  1571. // Move mouse over window 2. This should generate a mouse-exited event for
  1572. // window 1 resulting in deletion of window 2. The original mouse-moved event
  1573. // that was targeted to window 2 should be dropped since window 2 is
  1574. // destroyed. This test passes if no crash happens.
  1575. generator.MoveMouseTo(52, 52);
  1576. EXPECT_EQ(NULL, host()->dispatcher()->mouse_moved_handler());
  1577. // Check events received by window 1.
  1578. EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED MOUSE_EXITED",
  1579. EventTypesToString(w1_filter.events()));
  1580. w1->RemovePreTargetHandler(&w1_filter);
  1581. }
  1582. // Tests the case where the event dispatcher is deleted during the pre-dispatch
  1583. // phase of dispatching and event.
  1584. TEST_F(WindowEventDispatcherTest, DeleteDispatcherDuringPreDispatch) {
  1585. // Create a host for the window hierarchy. This host will be destroyed later
  1586. // on.
  1587. WindowTreeHost* host =
  1588. WindowTreeHost::Create(
  1589. ui::PlatformWindowInitProperties{gfx::Rect(0, 0, 100, 100)})
  1590. .release();
  1591. host->InitHost();
  1592. host->window()->Show();
  1593. // Create two windows.
  1594. Window* w1 = CreateNormalWindow(1, host->window(), nullptr);
  1595. w1->SetBounds(gfx::Rect(20, 20, 60, 60));
  1596. Window* w2 = CreateNormalWindow(2, host->window(), nullptr);
  1597. w2->SetBounds(gfx::Rect(80, 20, 120, 60));
  1598. EXPECT_EQ(nullptr, host->dispatcher()->mouse_moved_handler());
  1599. ui::test::EventGenerator generator(host->window(), w1);
  1600. // Move mouse over window 1 to set it as the |mouse_moved_handler_| for the
  1601. // root window.
  1602. generator.MoveMouseTo(40, 40);
  1603. EXPECT_EQ(w1, host->dispatcher()->mouse_moved_handler());
  1604. // Set appropriate event filters for the two windows with the window tree host
  1605. // as the object that is to be deleted when a mouse-exited event happens on
  1606. // window 1. The windows will take ownership of the event filters.
  1607. OnMouseExitDeletingEventFilter<WindowTreeHost> w1_filter(host);
  1608. w1->AddPreTargetHandler(&w1_filter);
  1609. EventFilterRecorder w2_filter;
  1610. w2->AddPreTargetHandler(&w2_filter);
  1611. w1_filter.set_delete_closure(
  1612. base::BindLambdaForTesting([&w1_filter, &w2_filter, &w1, &w2]() {
  1613. w1->RemovePreTargetHandler(&w1_filter);
  1614. w2->RemovePreTargetHandler(&w2_filter);
  1615. }));
  1616. // Move mouse over window 2. This should generate a mouse-exited event for
  1617. // window 1 resulting in deletion of window tree host and its event
  1618. // dispatcher. The event dispatching should abort since the dispatcher is
  1619. // destroyed. This test passes if no crash happens.
  1620. // Here we can't use EventGenerator since it expects that the dispatcher is
  1621. // not destroyed at the end of the dispatch.
  1622. ui::MouseEvent mouse_move(ui::ET_MOUSE_MOVED, gfx::Point(20, 20),
  1623. gfx::Point(20, 20), base::TimeTicks(), 0, 0);
  1624. ui::EventDispatchDetails details =
  1625. host->dispatcher()->DispatchEvent(w2, &mouse_move);
  1626. EXPECT_TRUE(details.dispatcher_destroyed);
  1627. // Check events received by the two windows.
  1628. EXPECT_EQ("MOUSE_EXITED", EventTypesToString(w1_filter.events()));
  1629. EXPECT_EQ(std::string(), EventTypesToString(w2_filter.events()));
  1630. }
  1631. namespace {
  1632. // Used to track if OnWindowDestroying() is invoked and if there is a valid
  1633. // RootWindow at such time.
  1634. class ValidRootDuringDestructionWindowObserver : public aura::WindowObserver {
  1635. public:
  1636. ValidRootDuringDestructionWindowObserver(bool* got_destroying,
  1637. bool* has_valid_root)
  1638. : got_destroying_(got_destroying), has_valid_root_(has_valid_root) {}
  1639. ValidRootDuringDestructionWindowObserver(
  1640. const ValidRootDuringDestructionWindowObserver&) = delete;
  1641. ValidRootDuringDestructionWindowObserver& operator=(
  1642. const ValidRootDuringDestructionWindowObserver&) = delete;
  1643. // WindowObserver:
  1644. void OnWindowDestroying(aura::Window* window) override {
  1645. *got_destroying_ = true;
  1646. *has_valid_root_ = (window->GetRootWindow() != NULL);
  1647. }
  1648. private:
  1649. raw_ptr<bool> got_destroying_;
  1650. raw_ptr<bool> has_valid_root_;
  1651. };
  1652. } // namespace
  1653. // Verifies GetRootWindow() from ~Window returns a valid root.
  1654. TEST_F(WindowEventDispatcherTest, ValidRootDuringDestruction) {
  1655. bool got_destroying = false;
  1656. bool has_valid_root = false;
  1657. ValidRootDuringDestructionWindowObserver observer(&got_destroying,
  1658. &has_valid_root);
  1659. {
  1660. std::unique_ptr<WindowTreeHost> host = WindowTreeHost::Create(
  1661. ui::PlatformWindowInitProperties{gfx::Rect(0, 0, 100, 100)});
  1662. host->InitHost();
  1663. host->window()->Show();
  1664. // Owned by WindowEventDispatcher.
  1665. Window* w1 = CreateNormalWindow(1, host->window(), NULL);
  1666. w1->AddObserver(&observer);
  1667. }
  1668. EXPECT_TRUE(got_destroying);
  1669. EXPECT_TRUE(has_valid_root);
  1670. }
  1671. namespace {
  1672. // See description above DontResetHeldEvent for details.
  1673. class DontResetHeldEventWindowDelegate : public test::TestWindowDelegate {
  1674. public:
  1675. explicit DontResetHeldEventWindowDelegate(aura::Window* root)
  1676. : root_(root), mouse_event_count_(0) {}
  1677. DontResetHeldEventWindowDelegate(const DontResetHeldEventWindowDelegate&) =
  1678. delete;
  1679. DontResetHeldEventWindowDelegate& operator=(
  1680. const DontResetHeldEventWindowDelegate&) = delete;
  1681. ~DontResetHeldEventWindowDelegate() override = default;
  1682. int mouse_event_count() const { return mouse_event_count_; }
  1683. // TestWindowDelegate:
  1684. void OnMouseEvent(ui::MouseEvent* event) override {
  1685. if ((event->flags() & ui::EF_SHIFT_DOWN) != 0 &&
  1686. mouse_event_count_++ == 0) {
  1687. ui::MouseEvent mouse_event(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
  1688. gfx::Point(10, 10), ui::EventTimeForNow(),
  1689. ui::EF_SHIFT_DOWN, 0);
  1690. root_->GetHost()->dispatcher()->RepostEvent(&mouse_event);
  1691. }
  1692. }
  1693. private:
  1694. raw_ptr<Window> root_;
  1695. int mouse_event_count_;
  1696. };
  1697. } // namespace
  1698. // Verifies RootWindow doesn't reset |RootWindow::held_repostable_event_| after
  1699. // dispatching. This is done by using DontResetHeldEventWindowDelegate, which
  1700. // tracks the number of events with ui::EF_SHIFT_DOWN set (all reposted events
  1701. // have EF_SHIFT_DOWN). When the first event is seen RepostEvent() is used to
  1702. // schedule another reposted event.
  1703. TEST_F(WindowEventDispatcherTest, DontResetHeldEvent) {
  1704. DontResetHeldEventWindowDelegate delegate(root_window());
  1705. std::unique_ptr<Window> w1(CreateNormalWindow(1, root_window(), &delegate));
  1706. w1->SetBounds(gfx::Rect(0, 0, 40, 40));
  1707. ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
  1708. gfx::Point(10, 10), ui::EventTimeForNow(),
  1709. ui::EF_SHIFT_DOWN, 0);
  1710. root_window()->GetHost()->dispatcher()->RepostEvent(&pressed);
  1711. ui::MouseEvent pressed2(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
  1712. gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
  1713. // Dispatch an event to flush event scheduled by way of RepostEvent().
  1714. DispatchEventUsingWindowDispatcher(&pressed2);
  1715. // Delegate should have seen reposted event (identified by way of
  1716. // EF_SHIFT_DOWN). Dispatch another event to flush the second
  1717. // RepostedEvent().
  1718. EXPECT_EQ(1, delegate.mouse_event_count());
  1719. DispatchEventUsingWindowDispatcher(&pressed2);
  1720. EXPECT_EQ(2, delegate.mouse_event_count());
  1721. }
  1722. namespace {
  1723. // See description above DeleteHostFromHeldMouseEvent for details.
  1724. class DeleteHostFromHeldMouseEventDelegate : public test::TestWindowDelegate {
  1725. public:
  1726. explicit DeleteHostFromHeldMouseEventDelegate(WindowTreeHost* host)
  1727. : host_(host), got_mouse_event_(false), got_destroy_(false) {}
  1728. DeleteHostFromHeldMouseEventDelegate(
  1729. const DeleteHostFromHeldMouseEventDelegate&) = delete;
  1730. DeleteHostFromHeldMouseEventDelegate& operator=(
  1731. const DeleteHostFromHeldMouseEventDelegate&) = delete;
  1732. ~DeleteHostFromHeldMouseEventDelegate() override = default;
  1733. bool got_mouse_event() const { return got_mouse_event_; }
  1734. bool got_destroy() const { return got_destroy_; }
  1735. // TestWindowDelegate:
  1736. void OnMouseEvent(ui::MouseEvent* event) override {
  1737. if ((event->flags() & ui::EF_SHIFT_DOWN) != 0) {
  1738. got_mouse_event_ = true;
  1739. delete host_;
  1740. }
  1741. }
  1742. void OnWindowDestroyed(Window* window) override { got_destroy_ = true; }
  1743. private:
  1744. raw_ptr<WindowTreeHost> host_;
  1745. bool got_mouse_event_;
  1746. bool got_destroy_;
  1747. };
  1748. } // namespace
  1749. // Verifies if a WindowTreeHost is deleted from dispatching a held mouse event
  1750. // we don't crash.
  1751. TEST_F(WindowEventDispatcherTest, DeleteHostFromHeldMouseEvent) {
  1752. // Should be deleted by |delegate|.
  1753. WindowTreeHost* h2 = WindowTreeHost::Create(ui::PlatformWindowInitProperties{
  1754. gfx::Rect(0, 0, 100, 100)})
  1755. .release();
  1756. h2->InitHost();
  1757. h2->window()->Show();
  1758. DeleteHostFromHeldMouseEventDelegate delegate(h2);
  1759. // Owned by |h2|.
  1760. Window* w1 = CreateNormalWindow(1, h2->window(), &delegate);
  1761. w1->SetBounds(gfx::Rect(0, 0, 40, 40));
  1762. ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
  1763. gfx::Point(10, 10), ui::EventTimeForNow(),
  1764. ui::EF_SHIFT_DOWN, 0);
  1765. h2->dispatcher()->RepostEvent(&pressed);
  1766. // RunAllPendingInMessageLoop() to make sure the |pressed| is run.
  1767. RunAllPendingInMessageLoop();
  1768. EXPECT_TRUE(delegate.got_mouse_event());
  1769. EXPECT_TRUE(delegate.got_destroy());
  1770. }
  1771. TEST_F(WindowEventDispatcherTest, WindowHideCancelsActiveTouches) {
  1772. EventFilterRecorder recorder;
  1773. root_window()->AddPreTargetHandler(&recorder);
  1774. test::TestWindowDelegate delegate;
  1775. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  1776. &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
  1777. gfx::Point position1 = root_window()->bounds().origin();
  1778. ui::TouchEvent press(ui::ET_TOUCH_PRESSED, position1, ui::EventTimeForNow(),
  1779. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  1780. DispatchEventUsingWindowDispatcher(&press);
  1781. EXPECT_EQ("TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN",
  1782. EventTypesToString(recorder.GetAndResetEvents()));
  1783. window->Hide();
  1784. EXPECT_EQ(ui::ET_TOUCH_CANCELLED, recorder.events()[0]);
  1785. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_TAP_CANCEL));
  1786. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_END));
  1787. EXPECT_EQ(3U, recorder.events().size());
  1788. root_window()->RemovePreTargetHandler(&recorder);
  1789. }
  1790. TEST_F(WindowEventDispatcherTest, WindowHideCancelsActiveGestures) {
  1791. EventFilterRecorder recorder;
  1792. root_window()->AddPreTargetHandler(&recorder);
  1793. test::TestWindowDelegate delegate;
  1794. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  1795. &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
  1796. gfx::Point position1 = root_window()->bounds().origin();
  1797. gfx::Point position2 = root_window()->bounds().CenterPoint();
  1798. ui::TouchEvent press(ui::ET_TOUCH_PRESSED, position1, ui::EventTimeForNow(),
  1799. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  1800. DispatchEventUsingWindowDispatcher(&press);
  1801. ui::TouchEvent move(ui::ET_TOUCH_MOVED, position2, ui::EventTimeForNow(),
  1802. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  1803. DispatchEventUsingWindowDispatcher(&move);
  1804. ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, position1, ui::EventTimeForNow(),
  1805. ui::PointerDetails(ui::EventPointerType::kTouch, 1));
  1806. DispatchEventUsingWindowDispatcher(&press2);
  1807. // TODO(tdresser): once the unified Gesture Recognizer has stuck, remove the
  1808. // special casing here. See crbug.com/332418 for details.
  1809. std::string expected =
  1810. "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
  1811. "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
  1812. "TOUCH_PRESSED GESTURE_BEGIN GESTURE_PINCH_BEGIN";
  1813. std::string expected_ugr =
  1814. "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
  1815. "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
  1816. "TOUCH_PRESSED GESTURE_BEGIN";
  1817. std::string events_string = EventTypesToString(recorder.GetAndResetEvents());
  1818. EXPECT_TRUE((expected == events_string) || (expected_ugr == events_string));
  1819. window->Hide();
  1820. expected =
  1821. "TOUCH_CANCELLED GESTURE_PINCH_END GESTURE_END TOUCH_CANCELLED "
  1822. "GESTURE_SCROLL_END GESTURE_END";
  1823. expected_ugr =
  1824. "TOUCH_CANCELLED GESTURE_SCROLL_END GESTURE_END TOUCH_CANCELLED "
  1825. "GESTURE_END";
  1826. events_string = EventTypesToString(recorder.GetAndResetEvents());
  1827. EXPECT_TRUE((expected == events_string) || (expected_ugr == events_string));
  1828. root_window()->RemovePreTargetHandler(&recorder);
  1829. }
  1830. // Places two windows side by side. Starts a pinch in one window, then sets
  1831. // capture to the other window. Ensures that subsequent pinch events are
  1832. // sent to the window which gained capture.
  1833. TEST_F(WindowEventDispatcherTest, TouchpadPinchEventsRetargetOnCapture) {
  1834. EventFilterRecorder recorder1;
  1835. EventFilterRecorder recorder2;
  1836. std::unique_ptr<Window> window1(
  1837. CreateNormalWindow(1, root_window(), nullptr));
  1838. window1->SetBounds(gfx::Rect(0, 0, 40, 40));
  1839. std::unique_ptr<Window> window2(
  1840. CreateNormalWindow(2, root_window(), nullptr));
  1841. window2->SetBounds(gfx::Rect(40, 0, 40, 40));
  1842. window1->AddPreTargetHandler(&recorder1);
  1843. window2->AddPreTargetHandler(&recorder2);
  1844. gfx::Point position1 = window1->bounds().CenterPoint();
  1845. ui::GestureEventDetails begin_details(ui::ET_GESTURE_PINCH_BEGIN);
  1846. begin_details.set_device_type(ui::GestureDeviceType::DEVICE_TOUCHPAD);
  1847. ui::GestureEvent begin(position1.x(), position1.y(), 0, ui::EventTimeForNow(),
  1848. begin_details);
  1849. DispatchEventUsingWindowDispatcher(&begin);
  1850. window2->SetCapture();
  1851. ui::GestureEventDetails update_details(ui::ET_GESTURE_PINCH_UPDATE);
  1852. update_details.set_device_type(ui::GestureDeviceType::DEVICE_TOUCHPAD);
  1853. ui::GestureEvent update(position1.x(), position1.y(), 0,
  1854. ui::EventTimeForNow(), update_details);
  1855. DispatchEventUsingWindowDispatcher(&update);
  1856. ui::GestureEventDetails end_details(ui::ET_GESTURE_PINCH_END);
  1857. end_details.set_device_type(ui::GestureDeviceType::DEVICE_TOUCHPAD);
  1858. ui::GestureEvent end(position1.x(), position1.y(), 0, ui::EventTimeForNow(),
  1859. end_details);
  1860. DispatchEventUsingWindowDispatcher(&end);
  1861. EXPECT_EQ("GESTURE_PINCH_BEGIN", EventTypesToString(recorder1.events()));
  1862. EXPECT_EQ("GESTURE_PINCH_UPDATE GESTURE_PINCH_END",
  1863. EventTypesToString(recorder2.events()));
  1864. window1->RemovePreTargetHandler(&recorder1);
  1865. window2->RemovePreTargetHandler(&recorder2);
  1866. }
  1867. // Places two windows side by side. Presses down on one window, and starts a
  1868. // scroll. Sets capture on the other window and ensures that the "ending" events
  1869. // aren't sent to the window which gained capture.
  1870. TEST_F(WindowEventDispatcherTest, EndingEventDoesntRetarget) {
  1871. EventFilterRecorder recorder1;
  1872. EventFilterRecorder recorder2;
  1873. std::unique_ptr<Window> window1(CreateNormalWindow(1, root_window(), NULL));
  1874. window1->SetBounds(gfx::Rect(0, 0, 40, 40));
  1875. std::unique_ptr<Window> window2(CreateNormalWindow(2, root_window(), NULL));
  1876. window2->SetBounds(gfx::Rect(40, 0, 40, 40));
  1877. window1->AddPreTargetHandler(&recorder1);
  1878. window2->AddPreTargetHandler(&recorder2);
  1879. gfx::Point position = window1->bounds().origin();
  1880. ui::TouchEvent press(ui::ET_TOUCH_PRESSED, position, ui::EventTimeForNow(),
  1881. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  1882. DispatchEventUsingWindowDispatcher(&press);
  1883. gfx::Point position2 = window1->bounds().CenterPoint();
  1884. ui::TouchEvent move(ui::ET_TOUCH_MOVED, position2, ui::EventTimeForNow(),
  1885. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  1886. DispatchEventUsingWindowDispatcher(&move);
  1887. window2->SetCapture();
  1888. EXPECT_EQ(
  1889. "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_MOVED "
  1890. "GESTURE_TAP_CANCEL GESTURE_SCROLL_BEGIN GESTURE_SCROLL_UPDATE "
  1891. "TOUCH_CANCELLED GESTURE_SCROLL_END GESTURE_END",
  1892. EventTypesToString(recorder1.events()));
  1893. EXPECT_TRUE(recorder2.events().empty());
  1894. window1->RemovePreTargetHandler(&recorder1);
  1895. window2->RemovePreTargetHandler(&recorder2);
  1896. }
  1897. namespace {
  1898. // This class creates and manages a window which is destroyed as soon as
  1899. // capture is lost. This is the case for the drag and drop capture window.
  1900. class CaptureWindowTracker : public test::TestWindowDelegate {
  1901. public:
  1902. CaptureWindowTracker() {}
  1903. CaptureWindowTracker(const CaptureWindowTracker&) = delete;
  1904. CaptureWindowTracker& operator=(const CaptureWindowTracker&) = delete;
  1905. ~CaptureWindowTracker() override {}
  1906. void CreateCaptureWindow(aura::Window* root_window) {
  1907. capture_window_.reset(test::CreateTestWindowWithDelegate(
  1908. this, -1234, gfx::Rect(20, 20, 20, 20), root_window));
  1909. capture_window_->SetCapture();
  1910. }
  1911. void reset() { capture_window_.reset(); }
  1912. void OnCaptureLost() override { capture_window_.reset(); }
  1913. void OnWindowDestroyed(Window* window) override {
  1914. TestWindowDelegate::OnWindowDestroyed(window);
  1915. capture_window_.reset();
  1916. }
  1917. aura::Window* capture_window() { return capture_window_.get(); }
  1918. private:
  1919. std::unique_ptr<aura::Window> capture_window_;
  1920. };
  1921. } // namespace
  1922. // Verifies handling loss of capture by the capture window being hidden.
  1923. TEST_F(WindowEventDispatcherTest, CaptureWindowHidden) {
  1924. CaptureWindowTracker capture_window_tracker;
  1925. capture_window_tracker.CreateCaptureWindow(root_window());
  1926. capture_window_tracker.capture_window()->Hide();
  1927. EXPECT_EQ(NULL, capture_window_tracker.capture_window());
  1928. }
  1929. // Verifies handling loss of capture by the capture window being destroyed.
  1930. TEST_F(WindowEventDispatcherTest, CaptureWindowDestroyed) {
  1931. CaptureWindowTracker capture_window_tracker;
  1932. capture_window_tracker.CreateCaptureWindow(root_window());
  1933. capture_window_tracker.reset();
  1934. EXPECT_EQ(NULL, capture_window_tracker.capture_window());
  1935. }
  1936. namespace {
  1937. class RunLoopHandler : public ui::EventHandler {
  1938. public:
  1939. explicit RunLoopHandler(aura::Window* target)
  1940. : run_loop_(base::RunLoop::Type::kNestableTasksAllowed), target_(target) {
  1941. target_->AddPreTargetHandler(this);
  1942. }
  1943. RunLoopHandler(const RunLoopHandler&) = delete;
  1944. RunLoopHandler& operator=(const RunLoopHandler&) = delete;
  1945. ~RunLoopHandler() override { target_->RemovePreTargetHandler(this); }
  1946. int num_scroll_updates() const { return num_scroll_updates_; }
  1947. private:
  1948. // ui::EventHandler:
  1949. void OnGestureEvent(ui::GestureEvent* event) override {
  1950. if (event->type() != ui::ET_GESTURE_SCROLL_UPDATE)
  1951. return;
  1952. num_scroll_updates_++;
  1953. if (running_) {
  1954. run_loop_.QuitWhenIdle();
  1955. } else {
  1956. running_ = true;
  1957. run_loop_.Run();
  1958. }
  1959. }
  1960. base::RunLoop run_loop_;
  1961. bool running_ = false;
  1962. int num_scroll_updates_ = 0;
  1963. raw_ptr<aura::Window> target_;
  1964. };
  1965. } // namespace
  1966. TEST_F(WindowEventDispatcherTest, HeldTouchMoveWithRunLoop) {
  1967. RunLoopHandler handler(root_window());
  1968. host()->dispatcher()->HoldPointerMoves();
  1969. gfx::Point point = root_window()->GetBoundsInScreen().CenterPoint();
  1970. ui::TouchEvent ev0(ui::ET_TOUCH_PRESSED, point, ui::EventTimeForNow(),
  1971. ui::PointerDetails());
  1972. DispatchEventUsingWindowDispatcher(&ev0);
  1973. point.Offset(10, 10);
  1974. ui::TouchEvent ev1(ui::ET_TOUCH_MOVED, point, ui::EventTimeForNow(),
  1975. ui::PointerDetails());
  1976. DispatchEventUsingWindowDispatcher(&ev1);
  1977. // The move event is held, so SCROLL_UPDATE does not happen yet.
  1978. EXPECT_EQ(0, handler.num_scroll_updates());
  1979. // ReleasePointerMoves() will post DispatchHeldEvent() asynchronously.
  1980. host()->dispatcher()->ReleasePointerMoves();
  1981. point.Offset(10, 10);
  1982. // Schedule another move event which should cause another SCROLL_UPDATE and
  1983. // quit the run_loop within the handler.
  1984. base::ThreadTaskRunnerHandle::Get()->PostTask(
  1985. FROM_HERE, base::BindLambdaForTesting([&]() {
  1986. ui::TouchEvent ev2(ui::ET_TOUCH_MOVED, point, base::TimeTicks::Now(),
  1987. ui::PointerDetails());
  1988. DispatchEventUsingWindowDispatcher(&ev2);
  1989. }));
  1990. // Wait for both DispatchHeldEvent() and dispatch of |ev2|.
  1991. base::RunLoop(base::RunLoop::Type::kNestableTasksAllowed).RunUntilIdle();
  1992. // Makes sure that the run_loop ran and then ended.
  1993. EXPECT_EQ(2, handler.num_scroll_updates());
  1994. }
  1995. class ExitMessageLoopOnMousePress : public ui::test::TestEventHandler {
  1996. public:
  1997. ExitMessageLoopOnMousePress() {}
  1998. ExitMessageLoopOnMousePress(const ExitMessageLoopOnMousePress&) = delete;
  1999. ExitMessageLoopOnMousePress& operator=(const ExitMessageLoopOnMousePress&) =
  2000. delete;
  2001. ~ExitMessageLoopOnMousePress() override {}
  2002. protected:
  2003. void OnMouseEvent(ui::MouseEvent* event) override {
  2004. ui::test::TestEventHandler::OnMouseEvent(event);
  2005. if (event->type() == ui::ET_MOUSE_PRESSED)
  2006. base::RunLoop::QuitCurrentWhenIdleDeprecated();
  2007. }
  2008. };
  2009. class WindowEventDispatcherTestWithMessageLoop
  2010. : public WindowEventDispatcherTest {
  2011. public:
  2012. WindowEventDispatcherTestWithMessageLoop() {}
  2013. WindowEventDispatcherTestWithMessageLoop(
  2014. const WindowEventDispatcherTestWithMessageLoop&) = delete;
  2015. WindowEventDispatcherTestWithMessageLoop& operator=(
  2016. const WindowEventDispatcherTestWithMessageLoop&) = delete;
  2017. ~WindowEventDispatcherTestWithMessageLoop() override {}
  2018. void RunTest() {
  2019. // Reset any event the window may have received when bringing up the window
  2020. // (e.g. mouse-move events if the mouse cursor is over the window).
  2021. handler_.Reset();
  2022. base::RunLoop loop(base::RunLoop::Type::kNestableTasksAllowed);
  2023. // Start a nested message-loop, post an event to be dispatched, and then
  2024. // terminate the message-loop. When the message-loop unwinds and gets back,
  2025. // the reposted event should not have fired.
  2026. std::unique_ptr<ui::MouseEvent> mouse(new ui::MouseEvent(
  2027. ui::ET_MOUSE_PRESSED, gfx::Point(10, 10), gfx::Point(10, 10),
  2028. ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE));
  2029. base::ThreadTaskRunnerHandle::Get()->PostTask(
  2030. FROM_HERE,
  2031. base::BindOnce(
  2032. &WindowEventDispatcherTestWithMessageLoop::RepostEventHelper,
  2033. host()->dispatcher(), std::move(mouse)));
  2034. base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
  2035. loop.QuitWhenIdleClosure());
  2036. loop.Run();
  2037. EXPECT_EQ(0, handler_.num_mouse_events());
  2038. // Let the current message-loop run. The event-handler will terminate the
  2039. // message-loop when it receives the reposted event.
  2040. }
  2041. protected:
  2042. void SetUp() override {
  2043. WindowEventDispatcherTest::SetUp();
  2044. window_.reset(CreateNormalWindow(1, root_window(), NULL));
  2045. window_->AddPreTargetHandler(&handler_);
  2046. }
  2047. void TearDown() override {
  2048. window_->RemovePreTargetHandler(&handler_);
  2049. window_.reset();
  2050. WindowEventDispatcherTest::TearDown();
  2051. }
  2052. private:
  2053. // Used to avoid a copying |event| when binding to a closure.
  2054. static void RepostEventHelper(WindowEventDispatcher* dispatcher,
  2055. std::unique_ptr<ui::MouseEvent> event) {
  2056. dispatcher->RepostEvent(event.get());
  2057. }
  2058. std::unique_ptr<Window> window_;
  2059. ExitMessageLoopOnMousePress handler_;
  2060. };
  2061. TEST_F(WindowEventDispatcherTestWithMessageLoop, EventRepostedInNonNestedLoop) {
  2062. ASSERT_FALSE(base::RunLoop::IsRunningOnCurrentThread());
  2063. // Perform the test in a callback, so that it runs after the message-loop
  2064. // starts.
  2065. base::ThreadTaskRunnerHandle::Get()->PostTask(
  2066. FROM_HERE,
  2067. base::BindOnce(&WindowEventDispatcherTestWithMessageLoop::RunTest,
  2068. base::Unretained(this)));
  2069. base::RunLoop().Run();
  2070. }
  2071. class WindowEventDispatcherTestInHighDPI : public WindowEventDispatcherTest {
  2072. public:
  2073. WindowEventDispatcherTestInHighDPI() {}
  2074. ~WindowEventDispatcherTestInHighDPI() override {}
  2075. void DispatchEvent(ui::Event* event) {
  2076. DispatchEventUsingWindowDispatcher(event);
  2077. }
  2078. protected:
  2079. void SetUp() override {
  2080. WindowEventDispatcherTest::SetUp();
  2081. test_screen()->SetDeviceScaleFactor(2.f);
  2082. }
  2083. };
  2084. TEST_F(WindowEventDispatcherTestInHighDPI, EventLocationTransform) {
  2085. test::TestWindowDelegate delegate;
  2086. std::unique_ptr<aura::Window> child(test::CreateTestWindowWithDelegate(
  2087. &delegate, 1234, gfx::Rect(20, 20, 100, 100), root_window()));
  2088. child->Show();
  2089. ui::test::TestEventHandler handler_child;
  2090. ui::test::TestEventHandler handler_root;
  2091. root_window()->AddPreTargetHandler(&handler_root);
  2092. child->AddPreTargetHandler(&handler_child);
  2093. {
  2094. ui::MouseEvent move(ui::ET_MOUSE_MOVED, gfx::Point(30, 30),
  2095. gfx::Point(30, 30), ui::EventTimeForNow(), ui::EF_NONE,
  2096. ui::EF_NONE);
  2097. DispatchEventUsingWindowDispatcher(&move);
  2098. EXPECT_EQ(0, handler_child.num_mouse_events());
  2099. EXPECT_EQ(1, handler_root.num_mouse_events());
  2100. }
  2101. {
  2102. ui::MouseEvent move(ui::ET_MOUSE_MOVED, gfx::Point(50, 50),
  2103. gfx::Point(50, 50), ui::EventTimeForNow(), ui::EF_NONE,
  2104. ui::EF_NONE);
  2105. DispatchEventUsingWindowDispatcher(&move);
  2106. // The child receives an ENTER, and a MOVED event.
  2107. EXPECT_EQ(2, handler_child.num_mouse_events());
  2108. // The root receives both the ENTER and the MOVED events dispatched to
  2109. // |child|, as well as an EXIT event.
  2110. EXPECT_EQ(3, handler_root.num_mouse_events());
  2111. }
  2112. child->RemovePreTargetHandler(&handler_child);
  2113. root_window()->RemovePreTargetHandler(&handler_root);
  2114. }
  2115. TEST_F(WindowEventDispatcherTestInHighDPI, TouchMovesHeldOnScroll) {
  2116. EventFilterRecorder recorder;
  2117. root_window()->AddPreTargetHandler(&recorder);
  2118. test::TestWindowDelegate delegate;
  2119. HoldPointerOnScrollHandler handler(host()->dispatcher(), &recorder);
  2120. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  2121. &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
  2122. window->AddPreTargetHandler(&handler);
  2123. ui::test::EventGenerator generator(root_window());
  2124. generator.GestureScrollSequence(gfx::Point(120, 120), gfx::Point(20, 120),
  2125. base::Milliseconds(100), 25);
  2126. // |handler| will have reset |filter| and started holding the touch-move
  2127. // events when scrolling started. At the end of the scroll (i.e. upon
  2128. // touch-release), the held touch-move event will have been dispatched first,
  2129. // along with the subsequent events (i.e. touch-release, scroll-end, and
  2130. // gesture-end).
  2131. const EventFilterRecorder::Events& events = recorder.events();
  2132. EXPECT_EQ(
  2133. "TOUCH_MOVED GESTURE_SCROLL_UPDATE TOUCH_RELEASED "
  2134. "GESTURE_SCROLL_END GESTURE_END",
  2135. EventTypesToString(events));
  2136. ASSERT_EQ(2u, recorder.touch_locations().size());
  2137. EXPECT_EQ(gfx::Point(-40, 10).ToString(),
  2138. recorder.touch_locations()[0].ToString());
  2139. EXPECT_EQ(gfx::Point(-40, 10).ToString(),
  2140. recorder.touch_locations()[1].ToString());
  2141. root_window()->RemovePreTargetHandler(&recorder);
  2142. window->RemovePreTargetHandler(&handler);
  2143. }
  2144. // This handler triggers a nested run loop when it receives a right click
  2145. // event, and runs a single callback in the nested run loop.
  2146. class TriggerNestedLoopOnRightMousePress : public ui::test::TestEventHandler {
  2147. public:
  2148. explicit TriggerNestedLoopOnRightMousePress(
  2149. const base::RepeatingClosure& callback)
  2150. : callback_(callback) {}
  2151. TriggerNestedLoopOnRightMousePress(
  2152. const TriggerNestedLoopOnRightMousePress&) = delete;
  2153. TriggerNestedLoopOnRightMousePress& operator=(
  2154. const TriggerNestedLoopOnRightMousePress&) = delete;
  2155. ~TriggerNestedLoopOnRightMousePress() override {}
  2156. const gfx::Point mouse_move_location() const { return mouse_move_location_; }
  2157. private:
  2158. void OnMouseEvent(ui::MouseEvent* mouse) override {
  2159. TestEventHandler::OnMouseEvent(mouse);
  2160. if (mouse->type() == ui::ET_MOUSE_PRESSED &&
  2161. mouse->IsOnlyRightMouseButton()) {
  2162. base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
  2163. scoped_refptr<base::TaskRunner> task_runner =
  2164. base::ThreadTaskRunnerHandle::Get();
  2165. if (!callback_.is_null())
  2166. task_runner->PostTask(FROM_HERE, callback_);
  2167. task_runner->PostTask(FROM_HERE, run_loop.QuitClosure());
  2168. run_loop.Run();
  2169. } else if (mouse->type() == ui::ET_MOUSE_MOVED) {
  2170. mouse_move_location_ = mouse->location();
  2171. }
  2172. }
  2173. base::RepeatingClosure callback_;
  2174. gfx::Point mouse_move_location_;
  2175. };
  2176. // Tests that if dispatching a 'held' event triggers a nested run loop, then
  2177. // the events that are dispatched from the nested run loop are transformed
  2178. // correctly.
  2179. TEST_F(WindowEventDispatcherTestInHighDPI,
  2180. EventsTransformedInRepostedEventTriggeredNestedLoop) {
  2181. std::unique_ptr<Window> window(CreateNormalWindow(1, root_window(), NULL));
  2182. // Make sure the window is visible.
  2183. RunAllPendingInMessageLoop();
  2184. ui::MouseEvent mouse_move(ui::ET_MOUSE_MOVED, gfx::Point(80, 80),
  2185. gfx::Point(80, 80), ui::EventTimeForNow(),
  2186. ui::EF_NONE, ui::EF_NONE);
  2187. base::RepeatingClosure callback_on_right_click = base::BindRepeating(
  2188. base::IgnoreResult(&WindowEventDispatcherTestInHighDPI::DispatchEvent),
  2189. base::Unretained(this), base::Unretained(&mouse_move));
  2190. TriggerNestedLoopOnRightMousePress handler(callback_on_right_click);
  2191. window->AddPreTargetHandler(&handler);
  2192. std::unique_ptr<ui::MouseEvent> mouse(
  2193. new ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10),
  2194. gfx::Point(10, 10), ui::EventTimeForNow(),
  2195. ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON));
  2196. host()->dispatcher()->RepostEvent(mouse.get());
  2197. EXPECT_EQ(0, handler.num_mouse_events());
  2198. base::RunLoop run_loop;
  2199. run_loop.RunUntilIdle();
  2200. // The window should receive the mouse-press and the mouse-move events.
  2201. EXPECT_EQ(2, handler.num_mouse_events());
  2202. // The mouse-move event location should be transformed because of the DSF
  2203. // before it reaches the window.
  2204. EXPECT_EQ(gfx::Point(40, 40).ToString(),
  2205. handler.mouse_move_location().ToString());
  2206. EXPECT_EQ(gfx::Point(40, 40).ToString(),
  2207. Env::GetInstance()->last_mouse_location().ToString());
  2208. window->RemovePreTargetHandler(&handler);
  2209. }
  2210. class SelfDestructDelegate : public test::TestWindowDelegate {
  2211. public:
  2212. SelfDestructDelegate() {}
  2213. SelfDestructDelegate(const SelfDestructDelegate&) = delete;
  2214. SelfDestructDelegate& operator=(const SelfDestructDelegate&) = delete;
  2215. ~SelfDestructDelegate() override {}
  2216. void OnMouseEvent(ui::MouseEvent* event) override { window_.reset(); }
  2217. void set_window(std::unique_ptr<aura::Window> window) {
  2218. window_ = std::move(window);
  2219. }
  2220. bool has_window() const { return !!window_.get(); }
  2221. private:
  2222. std::unique_ptr<aura::Window> window_;
  2223. };
  2224. TEST_F(WindowEventDispatcherTest, SynthesizedLocatedEvent) {
  2225. ui::test::EventGenerator generator(root_window());
  2226. generator.MoveMouseTo(10, 10);
  2227. EXPECT_EQ("10,10", Env::GetInstance()->last_mouse_location().ToString());
  2228. // Synthesized event should not update the mouse location.
  2229. ui::MouseEvent mouseev(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(),
  2230. ui::EventTimeForNow(), ui::EF_IS_SYNTHESIZED, 0);
  2231. generator.Dispatch(&mouseev);
  2232. EXPECT_EQ("10,10", Env::GetInstance()->last_mouse_location().ToString());
  2233. generator.MoveMouseTo(0, 0);
  2234. EXPECT_EQ("0,0", Env::GetInstance()->last_mouse_location().ToString());
  2235. // Make sure the location gets updated when a syntheiszed enter
  2236. // event destroyed the window.
  2237. SelfDestructDelegate delegate;
  2238. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  2239. &delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
  2240. delegate.set_window(std::move(window));
  2241. EXPECT_TRUE(delegate.has_window());
  2242. generator.MoveMouseTo(100, 100);
  2243. EXPECT_FALSE(delegate.has_window());
  2244. EXPECT_EQ("100,100", Env::GetInstance()->last_mouse_location().ToString());
  2245. }
  2246. // Tests that the window which has capture can get destroyed as a result of
  2247. // ui::ET_MOUSE_CAPTURE_CHANGED event dispatched in
  2248. // WindowEventDispatcher::UpdateCapture without causing a "use after free".
  2249. TEST_F(WindowEventDispatcherTest, DestroyWindowOnCaptureChanged) {
  2250. SelfDestructDelegate delegate;
  2251. std::unique_ptr<aura::Window> window_first(CreateTestWindowWithDelegate(
  2252. &delegate, 1, gfx::Rect(20, 10, 10, 20), root_window()));
  2253. Window* window_first_raw = window_first.get();
  2254. window_first->Show();
  2255. window_first->SetCapture();
  2256. delegate.set_window(std::move(window_first));
  2257. EXPECT_TRUE(delegate.has_window());
  2258. std::unique_ptr<aura::Window> window_second(
  2259. test::CreateTestWindowWithId(2, root_window()));
  2260. window_second->Show();
  2261. client::CaptureDelegate* capture_delegate = host()->dispatcher();
  2262. capture_delegate->UpdateCapture(window_first_raw, window_second.get());
  2263. EXPECT_FALSE(delegate.has_window());
  2264. }
  2265. class StaticFocusClient : public client::FocusClient {
  2266. public:
  2267. explicit StaticFocusClient(Window* focused) : focused_(focused) {}
  2268. StaticFocusClient(const StaticFocusClient&) = delete;
  2269. StaticFocusClient& operator=(const StaticFocusClient&) = delete;
  2270. ~StaticFocusClient() override = default;
  2271. private:
  2272. // client::FocusClient:
  2273. void AddObserver(client::FocusChangeObserver* observer) override {}
  2274. void RemoveObserver(client::FocusChangeObserver* observer) override {}
  2275. void FocusWindow(Window* window) override {}
  2276. void ResetFocusWithinActiveWindow(Window* window) override {}
  2277. Window* GetFocusedWindow() override { return focused_; }
  2278. raw_ptr<Window> focused_;
  2279. };
  2280. // Tests that host-cancel-mode event can be dispatched to a dispatcher safely
  2281. // when the focused window does not live in the dispatcher's tree.
  2282. TEST_F(WindowEventDispatcherTest, HostCancelModeWithFocusedWindowOutside) {
  2283. test::TestWindowDelegate delegate;
  2284. std::unique_ptr<Window> focused(CreateTestWindowWithDelegate(
  2285. &delegate, 123, gfx::Rect(20, 30, 100, 50), NULL));
  2286. StaticFocusClient focus_client(focused.get());
  2287. client::SetFocusClient(root_window(), &focus_client);
  2288. EXPECT_FALSE(root_window()->Contains(focused.get()));
  2289. EXPECT_EQ(focused.get(),
  2290. client::GetFocusClient(root_window())->GetFocusedWindow());
  2291. host()->dispatcher()->DispatchCancelModeEvent();
  2292. EXPECT_EQ(focused.get(),
  2293. client::GetFocusClient(root_window())->GetFocusedWindow());
  2294. }
  2295. // Dispatches a mouse-move event to |target| when it receives a mouse-move
  2296. // event.
  2297. class DispatchEventHandler : public ui::EventHandler {
  2298. public:
  2299. explicit DispatchEventHandler(Window* target)
  2300. : target_(target), dispatched_(false) {}
  2301. DispatchEventHandler(const DispatchEventHandler&) = delete;
  2302. DispatchEventHandler& operator=(const DispatchEventHandler&) = delete;
  2303. ~DispatchEventHandler() override = default;
  2304. bool dispatched() const { return dispatched_; }
  2305. private:
  2306. // ui::EventHandler:
  2307. void OnMouseEvent(ui::MouseEvent* mouse) override {
  2308. if (mouse->type() == ui::ET_MOUSE_MOVED) {
  2309. ui::MouseEvent move(ui::ET_MOUSE_MOVED, target_->bounds().CenterPoint(),
  2310. target_->bounds().CenterPoint(),
  2311. ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
  2312. ui::EventDispatchDetails details =
  2313. target_->GetHost()->dispatcher()->OnEventFromSource(&move);
  2314. ASSERT_FALSE(details.dispatcher_destroyed);
  2315. EXPECT_FALSE(details.target_destroyed);
  2316. EXPECT_EQ(target_, move.target());
  2317. dispatched_ = true;
  2318. }
  2319. ui::EventHandler::OnMouseEvent(mouse);
  2320. }
  2321. raw_ptr<Window> target_;
  2322. bool dispatched_;
  2323. };
  2324. // Moves |window| to |root_window| when it receives a mouse-move event.
  2325. class MoveWindowHandler : public ui::EventHandler {
  2326. public:
  2327. MoveWindowHandler(Window* window, Window* root_window)
  2328. : window_to_move_(window), root_window_to_move_to_(root_window) {}
  2329. MoveWindowHandler(const MoveWindowHandler&) = delete;
  2330. MoveWindowHandler& operator=(const MoveWindowHandler&) = delete;
  2331. ~MoveWindowHandler() override = default;
  2332. private:
  2333. // ui::EventHandler:
  2334. void OnMouseEvent(ui::MouseEvent* mouse) override {
  2335. if (mouse->type() == ui::ET_MOUSE_MOVED) {
  2336. root_window_to_move_to_->AddChild(window_to_move_);
  2337. }
  2338. ui::EventHandler::OnMouseEvent(mouse);
  2339. }
  2340. raw_ptr<Window> window_to_move_;
  2341. raw_ptr<Window> root_window_to_move_to_;
  2342. };
  2343. // Tests that nested event dispatch works correctly if the target of the older
  2344. // event being dispatched is moved to a different dispatcher in response to an
  2345. // event in the inner loop.
  2346. TEST_F(WindowEventDispatcherTest, NestedEventDispatchTargetMoved) {
  2347. std::unique_ptr<WindowTreeHost> second_host = WindowTreeHost::Create(
  2348. ui::PlatformWindowInitProperties{gfx::Rect(20, 30, 100, 50)});
  2349. second_host->InitHost();
  2350. second_host->window()->Show();
  2351. Window* second_root = second_host->window();
  2352. // Create two windows parented to |root_window()|.
  2353. test::TestWindowDelegate delegate;
  2354. std::unique_ptr<Window> first(CreateTestWindowWithDelegate(
  2355. &delegate, 123, gfx::Rect(20, 10, 10, 20), root_window()));
  2356. std::unique_ptr<Window> second(CreateTestWindowWithDelegate(
  2357. &delegate, 234, gfx::Rect(40, 10, 50, 20), root_window()));
  2358. // Setup a handler on |first| so that it dispatches an event to |second| when
  2359. // |first| receives an event.
  2360. DispatchEventHandler dispatch_event(second.get());
  2361. first->AddPreTargetHandler(&dispatch_event);
  2362. // Setup a handler on |second| so that it moves |first| into |second_root|
  2363. // when |second| receives an event.
  2364. MoveWindowHandler move_window(first.get(), second_root);
  2365. second->AddPreTargetHandler(&move_window);
  2366. // Some sanity checks: |first| is inside |root_window()|'s tree.
  2367. EXPECT_EQ(root_window(), first->GetRootWindow());
  2368. // The two root windows are different.
  2369. EXPECT_NE(root_window(), second_root);
  2370. // Dispatch an event to |first|.
  2371. ui::MouseEvent move(ui::ET_MOUSE_MOVED, first->bounds().CenterPoint(),
  2372. first->bounds().CenterPoint(), ui::EventTimeForNow(),
  2373. ui::EF_NONE, ui::EF_NONE);
  2374. ui::EventDispatchDetails details =
  2375. host()->dispatcher()->OnEventFromSource(&move);
  2376. ASSERT_FALSE(details.dispatcher_destroyed);
  2377. EXPECT_TRUE(details.target_destroyed);
  2378. EXPECT_EQ(first.get(), move.target());
  2379. EXPECT_TRUE(dispatch_event.dispatched());
  2380. EXPECT_EQ(second_root, first->GetRootWindow());
  2381. first->RemovePreTargetHandler(&dispatch_event);
  2382. second->RemovePreTargetHandler(&move_window);
  2383. }
  2384. class AlwaysMouseDownInputStateLookup : public InputStateLookup {
  2385. public:
  2386. AlwaysMouseDownInputStateLookup() {}
  2387. AlwaysMouseDownInputStateLookup(const AlwaysMouseDownInputStateLookup&) =
  2388. delete;
  2389. AlwaysMouseDownInputStateLookup& operator=(
  2390. const AlwaysMouseDownInputStateLookup&) = delete;
  2391. ~AlwaysMouseDownInputStateLookup() override {}
  2392. private:
  2393. // InputStateLookup:
  2394. bool IsMouseButtonDown() const override { return true; }
  2395. };
  2396. TEST_F(WindowEventDispatcherTest,
  2397. CursorVisibilityChangedWhileCaptureWindowInAnotherDispatcher) {
  2398. test::EventCountDelegate delegate;
  2399. std::unique_ptr<Window> window(CreateTestWindowWithDelegate(
  2400. &delegate, 123, gfx::Rect(20, 10, 10, 20), root_window()));
  2401. window->Show();
  2402. std::unique_ptr<WindowTreeHost> second_host = WindowTreeHost::Create(
  2403. ui::PlatformWindowInitProperties{gfx::Rect(20, 30, 100, 50)});
  2404. second_host->InitHost();
  2405. second_host->window()->Show();
  2406. WindowEventDispatcher* second_dispatcher = second_host->dispatcher();
  2407. // Install an InputStateLookup on the Env that always claims that a
  2408. // mouse-button is down.
  2409. test::EnvTestHelper(Env::GetInstance())
  2410. .SetInputStateLookup(std::unique_ptr<InputStateLookup>(
  2411. new AlwaysMouseDownInputStateLookup()));
  2412. window->SetCapture();
  2413. // Because the mouse button is down, setting the capture on |window| will set
  2414. // it as the mouse-move handler for |root_window()|.
  2415. EXPECT_EQ(window.get(), host()->dispatcher()->mouse_moved_handler());
  2416. // This does not set |window| as the mouse-move handler for the second
  2417. // dispatcher.
  2418. EXPECT_EQ(NULL, second_dispatcher->mouse_moved_handler());
  2419. // However, some capture-client updates the capture in each root-window on a
  2420. // capture. Emulate that here. Because of this, the second dispatcher also has
  2421. // |window| as the mouse-move handler.
  2422. client::CaptureDelegate* second_capture_delegate = second_dispatcher;
  2423. second_capture_delegate->UpdateCapture(NULL, window.get());
  2424. EXPECT_EQ(window.get(), second_dispatcher->mouse_moved_handler());
  2425. // Reset the mouse-event counts for |window|.
  2426. delegate.GetMouseMotionCountsAndReset();
  2427. // Notify both hosts that the cursor is now hidden. This should send a single
  2428. // mouse-exit event to |window|.
  2429. host()->OnCursorVisibilityChanged(false);
  2430. second_host->OnCursorVisibilityChanged(false);
  2431. EXPECT_EQ("0 0 1", delegate.GetMouseMotionCountsAndReset());
  2432. }
  2433. TEST_F(WindowEventDispatcherTest,
  2434. RedirectedEventToDifferentDispatcherLocation) {
  2435. std::unique_ptr<WindowTreeHost> second_host = WindowTreeHost::Create(
  2436. ui::PlatformWindowInitProperties{gfx::Rect(20, 30, 100, 50)});
  2437. second_host->InitHost();
  2438. second_host->window()->Show();
  2439. // AuraTestBase sets up a DefaultCaptureClient for root_window(), but that
  2440. // can't deal with capture between different root windows. Instead we need to
  2441. // use the wm::CaptureController instance that also exists, which can handle
  2442. // this situation. Exchange the capture clients and put the old one back at
  2443. // the end.
  2444. client::CaptureClient* const old_capture_client =
  2445. client::GetCaptureClient(root_window());
  2446. {
  2447. wm::ScopedCaptureClient scoped_capture_first(root_window());
  2448. wm::ScopedCaptureClient scoped_capture_second(second_host->window());
  2449. test::EventCountDelegate delegate;
  2450. std::unique_ptr<Window> window_first(CreateTestWindowWithDelegate(
  2451. &delegate, 123, gfx::Rect(20, 10, 10, 20), root_window()));
  2452. window_first->Show();
  2453. std::unique_ptr<Window> window_second(CreateTestWindowWithDelegate(
  2454. &delegate, 12, gfx::Rect(10, 10, 20, 30), second_host->window()));
  2455. window_second->Show();
  2456. window_second->SetCapture();
  2457. // Send an event to the first host. Make sure it goes to |window_second| in
  2458. // |second_host| instead (since it has capture).
  2459. EventFilterRecorder recorder_first;
  2460. window_first->AddPreTargetHandler(&recorder_first);
  2461. EventFilterRecorder recorder_second;
  2462. window_second->AddPreTargetHandler(&recorder_second);
  2463. const gfx::Point event_location(25, 15);
  2464. ui::MouseEvent mouse(ui::ET_MOUSE_PRESSED, event_location, event_location,
  2465. ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
  2466. ui::EF_LEFT_MOUSE_BUTTON);
  2467. DispatchEventUsingWindowDispatcher(&mouse);
  2468. EXPECT_TRUE(recorder_first.events().empty());
  2469. ASSERT_EQ(1u, recorder_second.events().size());
  2470. EXPECT_EQ(ui::ET_MOUSE_PRESSED, recorder_second.events()[0]);
  2471. EXPECT_EQ(event_location.ToString(),
  2472. recorder_second.mouse_locations()[0].ToString());
  2473. window_first->RemovePreTargetHandler(&recorder_first);
  2474. window_second->RemovePreTargetHandler(&recorder_second);
  2475. }
  2476. client::SetCaptureClient(root_window(), old_capture_client);
  2477. }
  2478. class AsyncWindowDelegate : public test::TestWindowDelegate {
  2479. public:
  2480. AsyncWindowDelegate(WindowEventDispatcher* dispatcher)
  2481. : dispatcher_(dispatcher), window_(nullptr) {}
  2482. AsyncWindowDelegate(const AsyncWindowDelegate&) = delete;
  2483. AsyncWindowDelegate& operator=(const AsyncWindowDelegate&) = delete;
  2484. void set_window(Window* window) { window_ = window; }
  2485. private:
  2486. void OnTouchEvent(ui::TouchEvent* event) override {
  2487. // Convert touch event back to root window coordinates.
  2488. event->ConvertLocationToTarget(window_.get(), window_->GetRootWindow());
  2489. event->DisableSynchronousHandling();
  2490. dispatcher_->ProcessedTouchEvent(
  2491. event->unique_event_id(), window_, ui::ER_UNHANDLED,
  2492. false /* is_source_touch_event_set_blocking */);
  2493. event->StopPropagation();
  2494. }
  2495. raw_ptr<WindowEventDispatcher> dispatcher_;
  2496. raw_ptr<Window> window_;
  2497. };
  2498. // Tests that gesture events dispatched through the asynchronous flow have
  2499. // co-ordinates in the right co-ordinate space.
  2500. TEST_F(WindowEventDispatcherTest, GestureEventCoordinates) {
  2501. const float kX = 67.3f;
  2502. const float kY = 97.8f;
  2503. const int kWindowOffset = 50;
  2504. EventFilterRecorder recorder;
  2505. root_window()->AddPreTargetHandler(&recorder);
  2506. AsyncWindowDelegate delegate(host()->dispatcher());
  2507. HoldPointerOnScrollHandler handler(host()->dispatcher(), &recorder);
  2508. std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
  2509. &delegate, 1, gfx::Rect(kWindowOffset, kWindowOffset, 100, 100),
  2510. root_window()));
  2511. window->AddPreTargetHandler(&handler);
  2512. delegate.set_window(window.get());
  2513. ui::TouchEvent touch_pressed_event(
  2514. ui::ET_TOUCH_PRESSED, gfx::Point(), ui::EventTimeForNow(),
  2515. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  2516. touch_pressed_event.set_location_f(gfx::PointF(kX, kY));
  2517. touch_pressed_event.set_root_location_f(gfx::PointF(kX, kY));
  2518. DispatchEventUsingWindowDispatcher(&touch_pressed_event);
  2519. ASSERT_EQ(1u, recorder.touch_locations().size());
  2520. EXPECT_EQ(gfx::Point(kX - kWindowOffset, kY - kWindowOffset).ToString(),
  2521. recorder.touch_locations()[0].ToString());
  2522. ASSERT_EQ(2u, recorder.gesture_locations().size());
  2523. EXPECT_EQ(gfx::Point(kX - kWindowOffset, kY - kWindowOffset).ToString(),
  2524. recorder.gesture_locations()[0].ToString());
  2525. root_window()->RemovePreTargetHandler(&recorder);
  2526. window->RemovePreTargetHandler(&handler);
  2527. }
  2528. // Tests that a scroll-generating touch-event is marked as such.
  2529. TEST_F(WindowEventDispatcherTest, TouchMovesMarkedWhenCausingScroll) {
  2530. EventFilterRecorder recorder;
  2531. root_window()->AddPreTargetHandler(&recorder);
  2532. const gfx::Point location(20, 20);
  2533. ui::TouchEvent press(ui::ET_TOUCH_PRESSED, location, ui::EventTimeForNow(),
  2534. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  2535. DispatchEventUsingWindowDispatcher(&press);
  2536. EXPECT_FALSE(recorder.LastTouchMayCauseScrolling());
  2537. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_PRESSED));
  2538. recorder.Reset();
  2539. ui::TouchEvent move(ui::ET_TOUCH_MOVED, location + gfx::Vector2d(100, 100),
  2540. ui::EventTimeForNow(),
  2541. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  2542. DispatchEventUsingWindowDispatcher(&move);
  2543. EXPECT_TRUE(recorder.LastTouchMayCauseScrolling());
  2544. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
  2545. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_BEGIN));
  2546. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE));
  2547. recorder.Reset();
  2548. ui::TouchEvent move2(ui::ET_TOUCH_MOVED, location + gfx::Vector2d(200, 200),
  2549. ui::EventTimeForNow(),
  2550. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  2551. DispatchEventUsingWindowDispatcher(&move2);
  2552. EXPECT_TRUE(recorder.LastTouchMayCauseScrolling());
  2553. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_MOVED));
  2554. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_UPDATE));
  2555. recorder.Reset();
  2556. // Delay the release to avoid fling generation.
  2557. ui::TouchEvent release(ui::ET_TOUCH_RELEASED,
  2558. location + gfx::Vector2d(200, 200),
  2559. ui::EventTimeForNow() + base::Seconds(1),
  2560. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  2561. DispatchEventUsingWindowDispatcher(&release);
  2562. EXPECT_TRUE(recorder.LastTouchMayCauseScrolling());
  2563. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_TOUCH_RELEASED));
  2564. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_GESTURE_SCROLL_END));
  2565. root_window()->RemovePreTargetHandler(&recorder);
  2566. }
  2567. // OnCursorMovedToRootLocation() is sometimes called instead of
  2568. // WindowTreeHost::MoveCursorTo() when the cursor did not move but the
  2569. // cursor's position in root coordinates has changed (e.g. when the displays's
  2570. // scale factor changed). Test that hover effects are properly updated.
  2571. TEST_F(WindowEventDispatcherTest, OnCursorMovedToRootLocationUpdatesHover) {
  2572. WindowEventDispatcher* dispatcher = host()->dispatcher();
  2573. test::TestCursorClient cursor_client(root_window());
  2574. cursor_client.ShowCursor();
  2575. std::unique_ptr<Window> w(CreateNormalWindow(1, root_window(), nullptr));
  2576. w->SetBounds(gfx::Rect(20, 20, 20, 20));
  2577. w->Show();
  2578. // Move the cursor off of |w|.
  2579. dispatcher->OnCursorMovedToRootLocation(gfx::Point(100, 100));
  2580. EventFilterRecorder recorder;
  2581. w->AddPreTargetHandler(&recorder);
  2582. dispatcher->OnCursorMovedToRootLocation(gfx::Point(22, 22));
  2583. RunAllPendingInMessageLoop();
  2584. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_MOUSE_ENTERED));
  2585. recorder.Reset();
  2586. // The cursor should not be over |w| after changing the device scale factor to
  2587. // 2x. A ET_MOUSE_EXITED event should have been sent to |w|.
  2588. test_screen()->SetDeviceScaleFactor(2.f);
  2589. dispatcher->OnCursorMovedToRootLocation(gfx::Point(11, 11));
  2590. RunAllPendingInMessageLoop();
  2591. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_MOUSE_EXITED));
  2592. recorder.Reset();
  2593. // Hide the cursor, synthetic event will not be sent.
  2594. cursor_client.HideCursor();
  2595. dispatcher->OnCursorMovedToRootLocation(gfx::Point(22, 22));
  2596. RunAllPendingInMessageLoop();
  2597. EXPECT_TRUE(recorder.events().empty());
  2598. // Cursor is hidden when locked, but synthetic move event still be dispatched.
  2599. cursor_client.LockCursor();
  2600. dispatcher->OnCursorMovedToRootLocation(gfx::Point(33, 33));
  2601. RunAllPendingInMessageLoop();
  2602. EXPECT_TRUE(recorder.HasReceivedEvent(ui::ET_MOUSE_MOVED));
  2603. recorder.Reset();
  2604. w->RemovePreTargetHandler(&recorder);
  2605. }
  2606. TEST_F(WindowEventDispatcherTest, TouchEventWithScaledWindow) {
  2607. WindowEventDispatcher* dispatcher = host()->dispatcher();
  2608. EventFilterRecorder root_recorder;
  2609. root_window()->AddPreTargetHandler(&root_recorder);
  2610. test::TestWindowDelegate delegate;
  2611. std::unique_ptr<Window> child(
  2612. CreateNormalWindow(1, root_window(), &delegate));
  2613. const gfx::Point child_position(-10, -10);
  2614. const gfx::Rect& root_bounds = root_window()->bounds();
  2615. gfx::Rect child_bounds(child_position,
  2616. gfx::ScaleToCeiledSize(root_bounds.size(), 2));
  2617. child->SetBounds(child_bounds);
  2618. gfx::Transform transform;
  2619. transform.Scale(0.5, 0.5);
  2620. child->SetTransform(transform);
  2621. EventFilterRecorder child_recorder;
  2622. child->AddPreTargetHandler(&child_recorder);
  2623. std::string expected_events =
  2624. "TOUCH_PRESSED GESTURE_BEGIN GESTURE_TAP_DOWN TOUCH_RELEASED "
  2625. "GESTURE_SHOW_PRESS GESTURE_TAP GESTURE_END";
  2626. {
  2627. // Touch events are outside of the root window, but inside of the child
  2628. // window.
  2629. const gfx::Point touch_position(-5, -5);
  2630. ui::TouchEvent pressed_event(
  2631. ui::ET_TOUCH_PRESSED, touch_position, ui::EventTimeForNow(),
  2632. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  2633. ui::TouchEvent released_event(
  2634. ui::ET_TOUCH_RELEASED, touch_position, ui::EventTimeForNow(),
  2635. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  2636. dispatcher->OnEventFromSource(&pressed_event);
  2637. dispatcher->OnEventFromSource(&released_event);
  2638. EXPECT_EQ(expected_events, EventTypesToString(root_recorder.events()));
  2639. EXPECT_EQ("", EventTypesToString(child_recorder.events()));
  2640. root_recorder.Reset();
  2641. child_recorder.Reset();
  2642. }
  2643. {
  2644. // |touch_position| value is in the bounds of both the root window and the
  2645. // child window.
  2646. const gfx::Point touch_position(5, 5);
  2647. ui::TouchEvent pressed_event(
  2648. ui::ET_TOUCH_PRESSED, touch_position, ui::EventTimeForNow(),
  2649. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  2650. ui::TouchEvent released_event(
  2651. ui::ET_TOUCH_RELEASED, touch_position, ui::EventTimeForNow(),
  2652. ui::PointerDetails(ui::EventPointerType::kTouch, 0));
  2653. dispatcher->OnEventFromSource(&pressed_event);
  2654. dispatcher->OnEventFromSource(&released_event);
  2655. EXPECT_EQ(expected_events, EventTypesToString(root_recorder.events()));
  2656. EXPECT_EQ(expected_events, EventTypesToString(child_recorder.events()));
  2657. root_recorder.Reset();
  2658. child_recorder.Reset();
  2659. }
  2660. child->RemovePreTargetHandler(&child_recorder);
  2661. root_window()->RemovePreTargetHandler(&root_recorder);
  2662. }
  2663. // A test case for crbug.com/1099985
  2664. TEST_F(WindowEventDispatcherTest, TargetIsDestroyedByHeldEvent) {
  2665. EventFilterRecorder recorder;
  2666. root_window()->AddPreTargetHandler(&recorder);
  2667. // Create a window which should be a target of all MouseEvent in this tests.
  2668. test::TestWindowDelegate delegate;
  2669. std::unique_ptr<aura::Window> mouse_target(CreateTestWindowWithDelegate(
  2670. &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
  2671. // Create a window which has a focus, so should receive all KeyEvents.
  2672. ConsumeKeyHandler key_handler;
  2673. // Not using std::unique_ptr<> intentionally
  2674. aura::Window* focused(test::CreateTestWindowWithBounds(
  2675. gfx::Rect(200, 200, 100, 100), root_window()));
  2676. focused->SetProperty(client::kSkipImeProcessing, true);
  2677. focused->AddPostTargetHandler(&key_handler);
  2678. focused->Show();
  2679. focused->Focus();
  2680. // Make sure that the key event goes to the |focused| window.
  2681. ui::KeyEvent key_press(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
  2682. DispatchEventUsingWindowDispatcher(&key_press);
  2683. EXPECT_EQ(1, key_handler.num_key_events());
  2684. key_handler.Reset();
  2685. ui::MouseEvent mouse_move_event(ui::ET_MOUSE_MOVED, gfx::Point(1, 1),
  2686. gfx::Point(1, 1), ui::EventTimeForNow(), 0,
  2687. 0);
  2688. DispatchEventUsingWindowDispatcher(&mouse_move_event);
  2689. // Discard MOUSE_ENTER.
  2690. recorder.Reset();
  2691. host()->dispatcher()->HoldPointerMoves();
  2692. // The dragged event should not be sent to the |target| window because
  2693. // WindowEventDispatcher is holding it now.
  2694. ui::MouseEvent mouse_dragged_event(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0),
  2695. gfx::Point(0, 0), ui::EventTimeForNow(), 0,
  2696. 0);
  2697. DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
  2698. EXPECT_TRUE(recorder.events().empty());
  2699. // Create a event handler which destroys the |focused| window when it sees any
  2700. // mouse event.
  2701. class Handler : public ui::test::TestEventHandler {
  2702. public:
  2703. explicit Handler(aura::Window* focused) : focused_(focused) {}
  2704. ~Handler() override = default;
  2705. Handler(const Handler&) = delete;
  2706. Handler& operator=(const Handler&) = delete;
  2707. // Overridden from ui::EventHandler:
  2708. void OnMouseEvent(ui::MouseEvent* event) override {
  2709. ui::test::TestEventHandler::OnMouseEvent(event);
  2710. LOG(ERROR) << "|focused_| is being deleted";
  2711. // !!!
  2712. delete focused_;
  2713. }
  2714. private:
  2715. raw_ptr<aura::Window> focused_;
  2716. };
  2717. Handler mouse_handler(focused);
  2718. mouse_target->AddPostTargetHandler(&mouse_handler);
  2719. // Sending a key event should stop the hold and the mouse event goes to the
  2720. // |target| window.
  2721. // The key event should not be sent to the handler because the focused window
  2722. // is destroyed before the event is dispatched.
  2723. ui::KeyEvent key_press2(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
  2724. DispatchEventUsingWindowDispatcher(&key_press2);
  2725. EXPECT_EQ(1u, recorder.events().size());
  2726. EXPECT_EQ(0, key_handler.num_key_events());
  2727. EXPECT_EQ(1, mouse_handler.num_mouse_events());
  2728. root_window()->RemovePreTargetHandler(&recorder);
  2729. }
  2730. } // namespace aura