keyboard_unittest.cc 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. // Copyright 2015 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 "components/exo/keyboard.h"
  5. #include "ash/accelerators/accelerator_controller_impl.h"
  6. #include "ash/accessibility/accessibility_controller_impl.h"
  7. #include "ash/constants/app_types.h"
  8. #include "ash/constants/ash_pref_names.h"
  9. #include "ash/public/cpp/external_arc/overlay/arc_overlay_manager.h"
  10. #include "ash/shell.h"
  11. #include "ash/test/ash_test_helper.h"
  12. #include "ash/test/test_widget_builder.h"
  13. #include "ash/test/test_window_builder.h"
  14. #include "ash/wm/desks/desks_controller.h"
  15. #include "ash/wm/desks/desks_test_util.h"
  16. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  17. #include "base/run_loop.h"
  18. #include "components/exo/buffer.h"
  19. #include "components/exo/keyboard_delegate.h"
  20. #include "components/exo/keyboard_device_configuration_delegate.h"
  21. #include "components/exo/keyboard_modifiers.h"
  22. #include "components/exo/keyboard_observer.h"
  23. #include "components/exo/seat.h"
  24. #include "components/exo/shell_surface.h"
  25. #include "components/exo/surface.h"
  26. #include "components/exo/test/exo_test_base.h"
  27. #include "components/exo/test/exo_test_helper.h"
  28. #include "components/exo/test/shell_surface_builder.h"
  29. #include "testing/gmock/include/gmock/gmock.h"
  30. #include "ui/aura/client/aura_constants.h"
  31. #include "ui/aura/client/focus_client.h"
  32. #include "ui/base/accelerators/test_accelerator_target.h"
  33. #include "ui/base/ime/dummy_text_input_client.h"
  34. #include "ui/events/devices/device_data_manager.h"
  35. #include "ui/events/event_constants.h"
  36. #include "ui/events/keycodes/dom/dom_code.h"
  37. #include "ui/events/test/event_generator.h"
  38. #include "ui/events/types/event_type.h"
  39. #include "ui/views/controls/textfield/textfield.h"
  40. namespace exo {
  41. namespace {
  42. // XKB mod masks for the default keymap.
  43. constexpr uint32_t kShiftMask = 1 << 0;
  44. constexpr uint32_t kControlMask = 1 << 2;
  45. constexpr uint32_t kAltMask = 1 << 3;
  46. constexpr uint32_t kNumLockMask = 1 << 4;
  47. using KeyboardTest = test::ExoTestBase;
  48. class MockKeyboardDelegate : public KeyboardDelegate {
  49. public:
  50. MockKeyboardDelegate() = default;
  51. // Overridden from KeyboardDelegate:
  52. MOCK_METHOD(bool, CanAcceptKeyboardEventsForSurface, (Surface*), (const));
  53. MOCK_METHOD(void,
  54. OnKeyboardEnter,
  55. (Surface*, (const base::flat_map<ui::DomCode, KeyState>&)));
  56. MOCK_METHOD(void, OnKeyboardLeave, (Surface*));
  57. MOCK_METHOD(uint32_t, OnKeyboardKey, (base::TimeTicks, ui::DomCode, bool));
  58. MOCK_METHOD(void, OnKeyboardModifiers, (const KeyboardModifiers&));
  59. MOCK_METHOD(void,
  60. OnKeyRepeatSettingsChanged,
  61. (bool, base::TimeDelta, base::TimeDelta));
  62. MOCK_METHOD(void, OnKeyboardLayoutUpdated, (base::StringPiece));
  63. };
  64. using NiceMockKeyboardDelegate = ::testing::NiceMock<MockKeyboardDelegate>;
  65. class MockKeyboardDeviceConfigurationDelegate
  66. : public KeyboardDeviceConfigurationDelegate {
  67. public:
  68. MockKeyboardDeviceConfigurationDelegate() = default;
  69. // Overridden from KeyboardDeviceConfigurationDelegate:
  70. MOCK_METHOD(void, OnKeyboardTypeChanged, (bool));
  71. };
  72. class MockKeyboardObserver : public KeyboardObserver {
  73. public:
  74. MockKeyboardObserver() = default;
  75. // Overridden from KeyboardObserver:
  76. MOCK_METHOD(void, OnKeyboardDestroying, (Keyboard*));
  77. MOCK_METHOD(void, OnKeyboardKey, (base::TimeTicks, ui::DomCode, bool));
  78. };
  79. using NiceMockKeyboardObserver = ::testing::NiceMock<MockKeyboardObserver>;
  80. class TestShellSurface : public ShellSurface {
  81. public:
  82. explicit TestShellSurface(Surface* surface) : ShellSurface(surface) {}
  83. MOCK_METHOD(bool, AcceleratorPressed, (const ui::Accelerator& accelerator));
  84. };
  85. // This event handler moves the focus to the given window when receiving a key
  86. // event.
  87. class TestEventHandler : public ui::EventHandler {
  88. public:
  89. explicit TestEventHandler(aura::Window* focus_window)
  90. : focus_window_(focus_window) {}
  91. TestEventHandler(const TestEventHandler&) = delete;
  92. TestEventHandler& operator=(const TestEventHandler&) = delete;
  93. void OnKeyEvent(ui::KeyEvent* event) override {
  94. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow())
  95. ->FocusWindow(focus_window_);
  96. }
  97. aura::Window* focus_window_;
  98. };
  99. // Verifies that switching desks via alt-tab doesn't prevent Seat from receiving
  100. // key events. https://crbug.com/1008574.
  101. TEST_F(KeyboardTest, CorrectSeatPressedKeysOnSwitchingDesks) {
  102. Seat seat;
  103. Keyboard keyboard(std::make_unique<NiceMockKeyboardDelegate>(), &seat);
  104. // Create 2 desks.
  105. auto* desks_controller = ash::DesksController::Get();
  106. desks_controller->NewDesk(ash::DesksCreationRemovalSource::kButton);
  107. ASSERT_EQ(2u, desks_controller->desks().size());
  108. ash::Desk* desk_1 = desks_controller->desks()[0].get();
  109. const ash::Desk* desk_2 = desks_controller->desks()[1].get();
  110. // Desk 1 has a normal window.
  111. auto win0 = CreateAppWindow(gfx::Rect(0, 0, 250, 100));
  112. // Desk 2 has an exo surface window.
  113. ash::ActivateDesk(desk_2);
  114. std::unique_ptr<Surface> surface(new Surface);
  115. std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
  116. gfx::Size buffer_size(10, 10);
  117. std::unique_ptr<Buffer> buffer(
  118. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  119. surface->Attach(buffer.get());
  120. surface->Commit();
  121. // Go back to desk 1, and trigger an alt-tab (releasing alt first). This would
  122. // trigger activating the exo surface window on desk 2, which would lead to a
  123. // desk switch animation. During the animation, expect that Seat gets all the
  124. // keys in `OnKeyEvent()`, and the |pressed_keys_| map is correctly updated.
  125. ash::ActivateDesk(desk_1);
  126. auto displatch_key_event = [&](ui::EventType type, ui::KeyboardCode key_code,
  127. ui::DomCode code, int flags) {
  128. ui::KeyEvent key_event{type, key_code, code, flags};
  129. seat.WillProcessEvent(&key_event);
  130. GetEventGenerator()->Dispatch(&key_event);
  131. EXPECT_EQ(type != ui::ET_KEY_RELEASED, seat.pressed_keys().count(code));
  132. seat.DidProcessEvent(&key_event);
  133. };
  134. ash::DeskSwitchAnimationWaiter waiter;
  135. displatch_key_event(ui::ET_KEY_PRESSED, ui::VKEY_MENU, ui::DomCode::ALT_LEFT,
  136. /*flags=*/0);
  137. displatch_key_event(ui::ET_KEY_PRESSED, ui::VKEY_TAB, ui::DomCode::TAB,
  138. /*flags=*/ui::EF_ALT_DOWN);
  139. displatch_key_event(ui::ET_KEY_RELEASED, ui::VKEY_MENU, ui::DomCode::ALT_LEFT,
  140. /*flags=*/0);
  141. displatch_key_event(ui::ET_KEY_RELEASED, ui::VKEY_TAB, ui::DomCode::TAB,
  142. /*flags=*/0);
  143. EXPECT_TRUE(seat.pressed_keys().empty());
  144. EXPECT_EQ(desk_2, desks_controller->GetTargetActiveDesk());
  145. waiter.Wait();
  146. }
  147. TEST_F(KeyboardTest, OnKeyboardEnter) {
  148. std::unique_ptr<Surface> surface(new Surface);
  149. std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
  150. gfx::Size buffer_size(10, 10);
  151. std::unique_ptr<Buffer> buffer(
  152. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  153. surface->Attach(buffer.get());
  154. surface->Commit();
  155. Seat seat;
  156. // Pressing key before Keyboard instance is created and surface has
  157. // received focus.
  158. ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
  159. seat.set_physical_code_for_currently_processing_event_for_testing(
  160. ui::DomCode::US_A);
  161. generator.PressKey(ui::VKEY_A, ui::EF_SHIFT_DOWN);
  162. aura::client::FocusClient* focus_client =
  163. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  164. focus_client->FocusWindow(surface->window());
  165. // Keyboard should try to set initial focus to surface.
  166. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  167. auto* delegate_ptr = delegate.get();
  168. EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
  169. .WillOnce(testing::Return(false));
  170. Keyboard keyboard(std::move(delegate), &seat);
  171. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  172. // Set up expectation for the key release.
  173. EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
  174. .WillOnce(testing::Return(true));
  175. EXPECT_CALL(
  176. *delegate_ptr,
  177. OnKeyboardEnter(
  178. surface.get(),
  179. base::flat_map<ui::DomCode, KeyState>(
  180. {{ui::DomCode::US_A, KeyState{ui::DomCode::US_A, false}}})));
  181. focus_client->FocusWindow(nullptr);
  182. focus_client->FocusWindow(surface->window());
  183. // Surface should maintain keyboard focus when moved to top-level window.
  184. focus_client->FocusWindow(surface->window()->GetToplevelWindow());
  185. // Release key after surface lost focus.
  186. focus_client->FocusWindow(nullptr);
  187. generator.ReleaseKey(ui::VKEY_A, ui::EF_SHIFT_DOWN);
  188. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  189. // Key should no longer be pressed when focus returns.
  190. EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
  191. .WillOnce(testing::Return(true));
  192. EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(KeyboardModifiers{
  193. kShiftMask | kNumLockMask, 0, 0, 0}));
  194. EXPECT_CALL(
  195. *delegate_ptr,
  196. OnKeyboardEnter(surface.get(), base::flat_map<ui::DomCode, KeyState>()));
  197. focus_client->FocusWindow(surface->window()->GetToplevelWindow());
  198. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  199. }
  200. TEST_F(KeyboardTest, OnKeyboardLeave) {
  201. std::unique_ptr<Surface> surface(new Surface);
  202. std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
  203. gfx::Size buffer_size(10, 10);
  204. std::unique_ptr<Buffer> buffer(
  205. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  206. surface->Attach(buffer.get());
  207. surface->Commit();
  208. aura::client::FocusClient* focus_client =
  209. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  210. focus_client->FocusWindow(nullptr);
  211. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  212. auto* delegate_ptr = delegate.get();
  213. Seat seat;
  214. auto keyboard = std::make_unique<Keyboard>(std::move(delegate), &seat);
  215. ON_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
  216. .WillByDefault(testing::Return(true));
  217. EXPECT_CALL(*delegate_ptr,
  218. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  219. EXPECT_CALL(
  220. *delegate_ptr,
  221. OnKeyboardEnter(surface.get(), base::flat_map<ui::DomCode, KeyState>()));
  222. focus_client->FocusWindow(surface->window());
  223. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  224. EXPECT_CALL(*delegate_ptr, OnKeyboardLeave(surface.get()));
  225. focus_client->FocusWindow(nullptr);
  226. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  227. EXPECT_CALL(*delegate_ptr,
  228. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  229. EXPECT_CALL(
  230. *delegate_ptr,
  231. OnKeyboardEnter(surface.get(), base::flat_map<ui::DomCode, KeyState>()));
  232. focus_client->FocusWindow(surface->window());
  233. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  234. EXPECT_CALL(*delegate_ptr, OnKeyboardLeave(surface.get()));
  235. shell_surface.reset();
  236. surface.reset();
  237. // Verify before destroying keyboard to make sure the expected call
  238. // is made on the methods above, rather than in the destructor.
  239. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  240. }
  241. TEST_F(KeyboardTest, OnKeyboardKey) {
  242. std::unique_ptr<Surface> surface(new Surface);
  243. std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
  244. gfx::Size buffer_size(10, 10);
  245. std::unique_ptr<Buffer> buffer(
  246. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  247. surface->Attach(buffer.get());
  248. surface->Commit();
  249. aura::client::FocusClient* focus_client =
  250. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  251. focus_client->FocusWindow(nullptr);
  252. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  253. auto* delegate_ptr = delegate.get();
  254. NiceMockKeyboardObserver observer;
  255. Seat seat;
  256. Keyboard keyboard(std::move(delegate), &seat);
  257. keyboard.AddObserver(&observer);
  258. EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
  259. .WillOnce(testing::Return(true));
  260. EXPECT_CALL(*delegate_ptr,
  261. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  262. EXPECT_CALL(
  263. *delegate_ptr,
  264. OnKeyboardEnter(surface.get(), base::flat_map<ui::DomCode, KeyState>()));
  265. focus_client->FocusWindow(surface->window());
  266. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  267. ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
  268. // This should only generate a press event for KEY_A.
  269. {
  270. testing::InSequence s;
  271. EXPECT_CALL(observer, OnKeyboardKey(testing::_, ui::DomCode::US_A, true));
  272. EXPECT_CALL(*delegate_ptr,
  273. OnKeyboardKey(testing::_, ui::DomCode::US_A, true));
  274. }
  275. seat.set_physical_code_for_currently_processing_event_for_testing(
  276. ui::DomCode::US_A);
  277. generator.PressKey(ui::VKEY_A, 0);
  278. testing::Mock::VerifyAndClearExpectations(&observer);
  279. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  280. // This should not generate another press event for KEY_A.
  281. generator.PressKey(ui::VKEY_A, 0);
  282. testing::Mock::VerifyAndClearExpectations(&observer);
  283. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  284. // This should only generate a single release event for KEY_A.
  285. {
  286. testing::InSequence s;
  287. EXPECT_CALL(observer, OnKeyboardKey(testing::_, ui::DomCode::US_A, false));
  288. EXPECT_CALL(*delegate_ptr,
  289. OnKeyboardKey(testing::_, ui::DomCode::US_A, false));
  290. }
  291. generator.ReleaseKey(ui::VKEY_A, 0);
  292. testing::Mock::VerifyAndClearExpectations(&observer);
  293. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  294. // Test key event rewriting. In this case, ARROW_DOWN is rewritten to KEY_END
  295. // as a result of ALT being pressed.
  296. {
  297. testing::InSequence s;
  298. EXPECT_CALL(observer, OnKeyboardKey(testing::_, ui::DomCode::END, true));
  299. EXPECT_CALL(*delegate_ptr,
  300. OnKeyboardKey(testing::_, ui::DomCode::END, true));
  301. }
  302. EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(KeyboardModifiers{
  303. kAltMask | kNumLockMask, 0, 0, 0}));
  304. seat.set_physical_code_for_currently_processing_event_for_testing(
  305. ui::DomCode::ARROW_DOWN);
  306. generator.PressKey(ui::VKEY_END, ui::EF_ALT_DOWN);
  307. testing::Mock::VerifyAndClearExpectations(&observer);
  308. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  309. // This should generate a release event for KEY_END as that is the key
  310. // associated with the key press.
  311. {
  312. testing::InSequence s;
  313. EXPECT_CALL(observer, OnKeyboardKey(testing::_, ui::DomCode::END, false));
  314. EXPECT_CALL(*delegate_ptr,
  315. OnKeyboardKey(testing::_, ui::DomCode::END, false));
  316. }
  317. EXPECT_CALL(*delegate_ptr,
  318. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  319. generator.ReleaseKey(ui::VKEY_DOWN, 0);
  320. testing::Mock::VerifyAndClearExpectations(&observer);
  321. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  322. // Press accelerator after surface lost focus.
  323. EXPECT_CALL(*delegate_ptr, OnKeyboardLeave(surface.get()));
  324. focus_client->FocusWindow(nullptr);
  325. seat.set_physical_code_for_currently_processing_event_for_testing(
  326. ui::DomCode::US_W);
  327. generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
  328. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  329. // Key should be pressed when focus returns.
  330. EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
  331. .WillOnce(testing::Return(true));
  332. EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(KeyboardModifiers{
  333. kControlMask | kNumLockMask, 0, 0, 0}));
  334. EXPECT_CALL(
  335. *delegate_ptr,
  336. OnKeyboardEnter(
  337. surface.get(),
  338. base::flat_map<ui::DomCode, KeyState>(
  339. {{ui::DomCode::US_W, KeyState{ui::DomCode::US_W, false}}})));
  340. focus_client->FocusWindow(surface->window());
  341. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  342. // Releasing accelerator when surface has focus should generate event.
  343. {
  344. testing::InSequence s;
  345. EXPECT_CALL(observer, OnKeyboardKey(testing::_, ui::DomCode::US_W, false));
  346. EXPECT_CALL(*delegate_ptr,
  347. OnKeyboardKey(testing::_, ui::DomCode::US_W, false));
  348. }
  349. generator.ReleaseKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
  350. testing::Mock::VerifyAndClearExpectations(&observer);
  351. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  352. // Key events should be ignored when the focused window is not an
  353. // exo::Surface.
  354. std::unique_ptr<aura::Window> window =
  355. ash::ChildTestWindowBuilder(shell_surface->GetWidget()->GetNativeWindow(),
  356. gfx::Rect(buffer_size))
  357. .Build();
  358. // Moving the focus away will reset the focused surface.
  359. EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
  360. .Times(0);
  361. focus_client->FocusWindow(window.get());
  362. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  363. EXPECT_FALSE(seat.GetFocusedSurface());
  364. EXPECT_FALSE(keyboard.focused_surface_for_testing());
  365. EXPECT_CALL(observer,
  366. OnKeyboardKey(testing::_, ui::DomCode::ARROW_LEFT, true))
  367. .Times(0);
  368. EXPECT_CALL(*delegate_ptr,
  369. OnKeyboardKey(testing::_, ui::DomCode::ARROW_LEFT, true))
  370. .Times(0);
  371. seat.set_physical_code_for_currently_processing_event_for_testing(
  372. ui::DomCode::ARROW_LEFT);
  373. generator.PressKey(ui::VKEY_LEFT, 0);
  374. testing::Mock::VerifyAndClearExpectations(&observer);
  375. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  376. EXPECT_CALL(observer,
  377. OnKeyboardKey(testing::_, ui::DomCode::ARROW_LEFT, false))
  378. .Times(0);
  379. EXPECT_CALL(*delegate_ptr,
  380. OnKeyboardKey(testing::_, ui::DomCode::ARROW_LEFT, false))
  381. .Times(0);
  382. generator.ReleaseKey(ui::VKEY_LEFT, 0);
  383. // Verify before destroying keyboard to make sure the expected call
  384. // is made on the methods above, rather than in the destructor.
  385. testing::Mock::VerifyAndClearExpectations(&observer);
  386. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  387. }
  388. TEST_F(KeyboardTest, OnKeyboardKey_NotSendKeyIfConsumedByIme) {
  389. std::unique_ptr<Surface> surface(new Surface);
  390. std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
  391. gfx::Size buffer_size(10, 10);
  392. std::unique_ptr<Buffer> buffer(
  393. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  394. surface->Attach(buffer.get());
  395. surface->Commit();
  396. aura::client::FocusClient* focus_client =
  397. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  398. focus_client->FocusWindow(nullptr);
  399. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  400. auto* delegate_ptr = delegate.get();
  401. NiceMockKeyboardObserver observer;
  402. Seat seat;
  403. Keyboard keyboard(std::move(delegate), &seat);
  404. keyboard.AddObserver(&observer);
  405. EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
  406. .WillOnce(testing::Return(true));
  407. EXPECT_CALL(*delegate_ptr,
  408. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  409. EXPECT_CALL(
  410. *delegate_ptr,
  411. OnKeyboardEnter(surface.get(), base::flat_map<ui::DomCode, KeyState>()));
  412. focus_client->FocusWindow(surface->window());
  413. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  414. ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
  415. views::Widget* widget =
  416. views::Widget::GetTopLevelWidgetForNativeView(surface->window());
  417. ui::InputMethod* input_method = widget->GetInputMethod();
  418. ui::DummyTextInputClient client{ui::TEXT_INPUT_TYPE_TEXT};
  419. input_method->SetFocusedTextInputClient(&client);
  420. // If a text field is focused, a pressed key event is not sent to a client
  421. // because a key event should be consumed by the IME.
  422. // However, the observer should receive OnKeyboardKey, always.
  423. EXPECT_CALL(observer, OnKeyboardKey(testing::_, ui::DomCode::US_A, true));
  424. EXPECT_CALL(*delegate_ptr, OnKeyboardKey(testing::_, ui::DomCode::US_A, true))
  425. .Times(0);
  426. seat.set_physical_code_for_currently_processing_event_for_testing(
  427. ui::DomCode::US_A);
  428. generator.PressKey(ui::VKEY_A, 0);
  429. testing::Mock::VerifyAndClearExpectations(&observer);
  430. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  431. // TODO(yhanada): The below EXPECT_CALL fails because exo::Keyboard currently
  432. // sends a key release event for the keys which exo::Keyboard sent a pressed
  433. // event for. It might causes a never-ending key repeat in the client.
  434. // EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_A, false));
  435. EXPECT_CALL(observer, OnKeyboardKey(testing::_, ui::DomCode::US_A, false));
  436. generator.ReleaseKey(ui::VKEY_A, 0);
  437. testing::Mock::VerifyAndClearExpectations(&observer);
  438. // Any key event should be sent to a client if a key event skips IME.
  439. surface->window()->SetProperty(aura::client::kSkipImeProcessing, true);
  440. {
  441. testing::InSequence s;
  442. EXPECT_CALL(observer, OnKeyboardKey(testing::_, ui::DomCode::US_C, true));
  443. EXPECT_CALL(*delegate_ptr,
  444. OnKeyboardKey(testing::_, ui::DomCode::US_C, true));
  445. }
  446. seat.set_physical_code_for_currently_processing_event_for_testing(
  447. ui::DomCode::US_C);
  448. generator.PressKey(ui::VKEY_C, 0);
  449. testing::Mock::VerifyAndClearExpectations(&observer);
  450. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  451. {
  452. testing::InSequence s;
  453. EXPECT_CALL(observer, OnKeyboardKey(testing::_, ui::DomCode::US_C, false));
  454. EXPECT_CALL(*delegate_ptr,
  455. OnKeyboardKey(testing::_, ui::DomCode::US_C, false));
  456. }
  457. generator.ReleaseKey(ui::VKEY_C, 0);
  458. testing::Mock::VerifyAndClearExpectations(&observer);
  459. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  460. input_method->SetFocusedTextInputClient(nullptr);
  461. }
  462. TEST_F(KeyboardTest, OnKeyboardKey_KeyboardInhibit) {
  463. std::unique_ptr<Surface> surface(new Surface());
  464. std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
  465. gfx::Size buffer_size(10, 10);
  466. std::unique_ptr<Buffer> buffer(
  467. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  468. surface->Attach(buffer.get());
  469. surface->Commit();
  470. // Set lacros attribute now for testing. This can be removed, when
  471. // all clients are migrated into this model.
  472. surface->window()->SetProperty(aura::client::kAppType,
  473. static_cast<int>(ash::AppType::LACROS));
  474. aura::client::FocusClient* focus_client =
  475. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  476. focus_client->FocusWindow(nullptr);
  477. // Register accelerator to be triggered.
  478. ui::TestAcceleratorTarget accelerator_target;
  479. {
  480. ui::Accelerator accelerator(ui::VKEY_P,
  481. ui::EF_ALT_DOWN | ui::EF_SHIFT_DOWN);
  482. ash::AcceleratorControllerImpl* controller =
  483. ash::Shell::Get()->accelerator_controller();
  484. controller->Register({accelerator}, &accelerator_target);
  485. }
  486. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  487. auto* delegate_ptr = delegate.get();
  488. NiceMockKeyboardObserver observer;
  489. Seat seat;
  490. Keyboard keyboard(std::move(delegate), &seat);
  491. keyboard.AddObserver(&observer);
  492. keyboard.SetNeedKeyboardKeyAcks(true);
  493. EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
  494. .WillOnce(testing::Return(true));
  495. EXPECT_CALL(*delegate_ptr,
  496. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  497. EXPECT_CALL(
  498. *delegate_ptr,
  499. OnKeyboardEnter(surface.get(), base::flat_map<ui::DomCode, KeyState>()));
  500. focus_client->FocusWindow(surface->window());
  501. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  502. ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
  503. // This should only generate a press event for KEY_P.
  504. accelerator_target.ResetCounts();
  505. EXPECT_CALL(observer,
  506. OnKeyboardKey(testing::_, ui::DomCode::US_P, testing::_))
  507. .Times(0);
  508. EXPECT_CALL(*delegate_ptr,
  509. OnKeyboardKey(testing::_, ui::DomCode::US_P, testing::_))
  510. .Times(0);
  511. seat.set_physical_code_for_currently_processing_event_for_testing(
  512. ui::DomCode::US_P);
  513. generator.PressKey(ui::VKEY_P, ui::EF_ALT_DOWN | ui::EF_SHIFT_DOWN);
  514. EXPECT_EQ(1, accelerator_target.accelerator_count());
  515. testing::Mock::VerifyAndClearExpectations(&observer);
  516. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  517. // Set keyboard-shortcut-inhibited, so the key event should be sent to app.
  518. surface->SetKeyboardShortcutsInhibited(true);
  519. accelerator_target.ResetCounts();
  520. {
  521. testing::InSequence s;
  522. EXPECT_CALL(observer, OnKeyboardKey(testing::_, ui::DomCode::US_P, true));
  523. EXPECT_CALL(*delegate_ptr,
  524. OnKeyboardKey(testing::_, ui::DomCode::US_P, true));
  525. }
  526. seat.set_physical_code_for_currently_processing_event_for_testing(
  527. ui::DomCode::US_P);
  528. generator.PressKey(ui::VKEY_P, ui::EF_ALT_DOWN | ui::EF_SHIFT_DOWN);
  529. EXPECT_EQ(0, accelerator_target.accelerator_count());
  530. testing::Mock::VerifyAndClearExpectations(&observer);
  531. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  532. }
  533. TEST_F(KeyboardTest, FocusWithArcOverlay) {
  534. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  535. // Just allow any surface to receive focus.
  536. EXPECT_CALL(*delegate, CanAcceptKeyboardEventsForSurface(::testing::_))
  537. .WillRepeatedly(testing::Return(true));
  538. Seat seat;
  539. Keyboard keyboard(std::move(delegate), &seat);
  540. // TODO(oshima): Create a TestExoWindowBuilder.
  541. class TestPropertyResolver : public exo::WMHelper::AppPropertyResolver {
  542. public:
  543. TestPropertyResolver() = default;
  544. ~TestPropertyResolver() override = default;
  545. void PopulateProperties(
  546. const Params& params,
  547. ui::PropertyHandler& out_properties_container) override {
  548. out_properties_container.SetProperty(
  549. aura::client::kAppType, static_cast<int>(ash::AppType::ARC_APP));
  550. }
  551. };
  552. WMHelper::GetInstance()->RegisterAppPropertyResolver(
  553. std::make_unique<TestPropertyResolver>());
  554. ash::ArcOverlayManager arc_overlay_manager_;
  555. auto* widget1 = ash::TestWidgetBuilder()
  556. .SetBounds(gfx::Rect(200, 200))
  557. .BuildOwnedByNativeWidget();
  558. views::Textfield* textfield1 = new views::Textfield();
  559. widget1->GetContentsView()->AddChildView(textfield1);
  560. textfield1->SetBounds(0, 0, 100, 100);
  561. auto* widget2 = ash::TestWidgetBuilder()
  562. .SetBounds(gfx::Rect(200, 200))
  563. .BuildOwnedByNativeWidget();
  564. auto hold = arc_overlay_manager_.RegisterHostWindow(
  565. "test", widget1->GetNativeWindow());
  566. std::unique_ptr<Surface> surface(new Surface);
  567. std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
  568. gfx::Size buffer_size(10, 10);
  569. std::unique_ptr<Buffer> buffer(
  570. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  571. surface->SetClientSurfaceId("billing_id:test");
  572. surface->Attach(buffer.get());
  573. surface->Commit();
  574. EXPECT_TRUE(shell_surface->GetWidget());
  575. // The overlay should have the focus when created.
  576. EXPECT_EQ(keyboard.focused_surface_for_testing(), surface.get());
  577. widget2->Activate();
  578. EXPECT_FALSE(keyboard.focused_surface_for_testing());
  579. // Activating the host widget should set the focus back to the overlay.
  580. widget1->Activate();
  581. EXPECT_EQ(keyboard.focused_surface_for_testing(), surface.get());
  582. constexpr char kFocusedViewClassName[] = "OverlayNativeViewHost";
  583. EXPECT_STREQ(kFocusedViewClassName,
  584. widget1->GetFocusManager()->GetFocusedView()->GetClassName());
  585. // Tabbing should not move the focus away from the overlay.
  586. ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
  587. generator.PressKey(ui::VKEY_TAB, 0);
  588. EXPECT_STREQ(kFocusedViewClassName,
  589. widget1->GetFocusManager()->GetFocusedView()->GetClassName());
  590. EXPECT_EQ(keyboard.focused_surface_for_testing(), surface.get());
  591. hold.RunAndReset();
  592. widget1->CloseNow();
  593. }
  594. TEST_F(KeyboardTest, OnKeyboardModifiers) {
  595. std::unique_ptr<Surface> surface(new Surface);
  596. std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
  597. gfx::Size buffer_size(10, 10);
  598. std::unique_ptr<Buffer> buffer(
  599. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  600. surface->Attach(buffer.get());
  601. surface->Commit();
  602. aura::client::FocusClient* focus_client =
  603. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  604. focus_client->FocusWindow(nullptr);
  605. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  606. auto* delegate_ptr = delegate.get();
  607. Seat seat;
  608. Keyboard keyboard(std::move(delegate), &seat);
  609. EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
  610. .WillOnce(testing::Return(true));
  611. EXPECT_CALL(*delegate_ptr,
  612. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  613. EXPECT_CALL(
  614. *delegate_ptr,
  615. OnKeyboardEnter(surface.get(), base::flat_map<ui::DomCode, KeyState>()));
  616. focus_client->FocusWindow(surface->window());
  617. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  618. ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
  619. // This should generate a modifier event.
  620. EXPECT_CALL(*delegate_ptr,
  621. OnKeyboardKey(testing::_, ui::DomCode::US_A, true));
  622. EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(KeyboardModifiers{
  623. kShiftMask | kNumLockMask, 0, 0, 0}));
  624. seat.set_physical_code_for_currently_processing_event_for_testing(
  625. ui::DomCode::US_A);
  626. generator.PressKey(ui::VKEY_A, ui::EF_SHIFT_DOWN);
  627. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  628. // This should generate another modifier event.
  629. EXPECT_CALL(*delegate_ptr,
  630. OnKeyboardKey(testing::_, ui::DomCode::US_B, true));
  631. EXPECT_CALL(*delegate_ptr,
  632. OnKeyboardModifiers(KeyboardModifiers{
  633. kShiftMask | kAltMask | kNumLockMask, 0, 0, 0}));
  634. seat.set_physical_code_for_currently_processing_event_for_testing(
  635. ui::DomCode::US_B);
  636. generator.PressKey(ui::VKEY_B, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN);
  637. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  638. // This should generate a third modifier event.
  639. EXPECT_CALL(*delegate_ptr,
  640. OnKeyboardKey(testing::_, ui::DomCode::US_B, false));
  641. EXPECT_CALL(*delegate_ptr,
  642. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  643. generator.ReleaseKey(ui::VKEY_B, 0);
  644. // Verify before destroying keyboard to make sure the expected call
  645. // is made on the methods above, rather than in the destructor.
  646. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  647. }
  648. TEST_F(KeyboardTest, OnKeyboardTypeChanged) {
  649. std::unique_ptr<Surface> surface(new Surface);
  650. std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
  651. gfx::Size buffer_size(10, 10);
  652. std::unique_ptr<Buffer> buffer(
  653. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  654. surface->Attach(buffer.get());
  655. surface->Commit();
  656. aura::client::FocusClient* focus_client =
  657. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  658. focus_client->FocusWindow(nullptr);
  659. ui::DeviceHotplugEventObserver* device_data_manager =
  660. ui::DeviceDataManager::GetInstance();
  661. ASSERT_TRUE(device_data_manager != nullptr);
  662. // Make sure that DeviceDataManager has one external keyboard...
  663. const std::vector<ui::InputDevice> keyboards{
  664. ui::InputDevice(2, ui::InputDeviceType::INPUT_DEVICE_USB, "keyboard")};
  665. device_data_manager->OnKeyboardDevicesUpdated(keyboards);
  666. // and a touch screen.
  667. const std::vector<ui::TouchscreenDevice> touch_screen{
  668. ui::TouchscreenDevice(3, ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
  669. "touch", gfx::Size(600, 400), 1)};
  670. device_data_manager->OnTouchscreenDevicesUpdated(touch_screen);
  671. ash::TabletModeController* tablet_mode_controller =
  672. ash::Shell::Get()->tablet_mode_controller();
  673. tablet_mode_controller->SetEnabledForTest(true);
  674. Seat seat;
  675. auto keyboard = std::make_unique<Keyboard>(
  676. std::make_unique<NiceMockKeyboardDelegate>(), &seat);
  677. MockKeyboardDeviceConfigurationDelegate configuration_delegate;
  678. EXPECT_CALL(configuration_delegate, OnKeyboardTypeChanged(true));
  679. keyboard->SetDeviceConfigurationDelegate(&configuration_delegate);
  680. EXPECT_TRUE(keyboard->HasDeviceConfigurationDelegate());
  681. testing::Mock::VerifyAndClearExpectations(&configuration_delegate);
  682. // Removing all keyboard devices in tablet mode calls
  683. // OnKeyboardTypeChanged() with false.
  684. EXPECT_CALL(configuration_delegate, OnKeyboardTypeChanged(false));
  685. device_data_manager->OnKeyboardDevicesUpdated(
  686. std::vector<ui::InputDevice>({}));
  687. testing::Mock::VerifyAndClearExpectations(&configuration_delegate);
  688. // Re-adding keyboards calls OnKeyboardTypeChanged() with true.
  689. EXPECT_CALL(configuration_delegate, OnKeyboardTypeChanged(true));
  690. device_data_manager->OnKeyboardDevicesUpdated(keyboards);
  691. testing::Mock::VerifyAndClearExpectations(&configuration_delegate);
  692. keyboard.reset();
  693. tablet_mode_controller->SetEnabledForTest(false);
  694. }
  695. TEST_F(KeyboardTest, OnKeyboardTypeChanged_AccessibilityKeyboard) {
  696. std::unique_ptr<Surface> surface(new Surface);
  697. std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
  698. gfx::Size buffer_size(10, 10);
  699. std::unique_ptr<Buffer> buffer(
  700. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  701. surface->Attach(buffer.get());
  702. surface->Commit();
  703. aura::client::FocusClient* focus_client =
  704. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  705. focus_client->FocusWindow(nullptr);
  706. ui::DeviceHotplugEventObserver* device_data_manager =
  707. ui::DeviceDataManager::GetInstance();
  708. ASSERT_TRUE(device_data_manager != nullptr);
  709. // Make sure that DeviceDataManager has one external keyboard.
  710. const std::vector<ui::InputDevice> keyboards{
  711. ui::InputDevice(2, ui::InputDeviceType::INPUT_DEVICE_USB, "keyboard")};
  712. device_data_manager->OnKeyboardDevicesUpdated(keyboards);
  713. Seat seat;
  714. Keyboard keyboard(std::make_unique<NiceMockKeyboardDelegate>(), &seat);
  715. MockKeyboardDeviceConfigurationDelegate configuration_delegate;
  716. EXPECT_CALL(configuration_delegate, OnKeyboardTypeChanged(true));
  717. keyboard.SetDeviceConfigurationDelegate(&configuration_delegate);
  718. EXPECT_TRUE(keyboard.HasDeviceConfigurationDelegate());
  719. testing::Mock::VerifyAndClearExpectations(&configuration_delegate);
  720. ash::AccessibilityControllerImpl* accessibility_controller =
  721. ash::Shell::Get()->accessibility_controller();
  722. // Enable a11y keyboard calls OnKeyboardTypeChanged() with false.
  723. EXPECT_CALL(configuration_delegate, OnKeyboardTypeChanged(false));
  724. accessibility_controller->virtual_keyboard().SetEnabled(true);
  725. testing::Mock::VerifyAndClearExpectations(&configuration_delegate);
  726. // Disable a11y keyboard calls OnKeyboardTypeChanged() with true.
  727. EXPECT_CALL(configuration_delegate, OnKeyboardTypeChanged(true));
  728. accessibility_controller->virtual_keyboard().SetEnabled(false);
  729. // Verify before destroying keyboard to make sure the expected call
  730. // is made on the methods above, rather than in the destructor.
  731. testing::Mock::VerifyAndClearExpectations(&configuration_delegate);
  732. }
  733. constexpr base::TimeDelta kDelta50Ms = base::Milliseconds(50);
  734. constexpr base::TimeDelta kDelta500Ms = base::Milliseconds(500);
  735. constexpr base::TimeDelta kDelta1000Ms = base::Milliseconds(1000);
  736. TEST_F(KeyboardTest, KeyRepeatSettingsLoadDefaults) {
  737. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  738. EXPECT_CALL(*delegate,
  739. OnKeyRepeatSettingsChanged(true, kDelta500Ms, kDelta50Ms));
  740. Seat seat;
  741. Keyboard keyboard(std::move(delegate), &seat);
  742. }
  743. TEST_F(KeyboardTest, KeyRepeatSettingsLoadInitially) {
  744. std::string email = "user0@tray";
  745. SetUserPref(email, ash::prefs::kXkbAutoRepeatEnabled, base::Value(true));
  746. SetUserPref(email, ash::prefs::kXkbAutoRepeatDelay, base::Value(1000));
  747. SetUserPref(email, ash::prefs::kXkbAutoRepeatInterval, base::Value(1000));
  748. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  749. auto* delegate_ptr = delegate.get();
  750. EXPECT_CALL(*delegate_ptr,
  751. OnKeyRepeatSettingsChanged(true, kDelta1000Ms, kDelta1000Ms));
  752. Seat seat;
  753. Keyboard keyboard(std::move(delegate), &seat);
  754. // Verify before destroying keyboard to make sure the expected call
  755. // is made on the methods above, rather than in the destructor.
  756. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  757. }
  758. TEST_F(KeyboardTest, KeyRepeatSettingsUpdateAtRuntime) {
  759. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  760. auto* delegate_ptr = delegate.get();
  761. // Initially load defaults.
  762. EXPECT_CALL(*delegate_ptr,
  763. OnKeyRepeatSettingsChanged(testing::_, testing::_, testing::_));
  764. Seat seat;
  765. Keyboard keyboard(std::move(delegate), &seat);
  766. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  767. // Make sure that setting prefs triggers the corresponding delegate calls.
  768. const std::string email = "user0@tray";
  769. EXPECT_CALL(*delegate_ptr,
  770. OnKeyRepeatSettingsChanged(false, testing::_, testing::_));
  771. SetUserPref(email, ash::prefs::kXkbAutoRepeatEnabled, base::Value(false));
  772. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  773. EXPECT_CALL(*delegate_ptr,
  774. OnKeyRepeatSettingsChanged(false, kDelta1000Ms, testing::_));
  775. SetUserPref(email, ash::prefs::kXkbAutoRepeatDelay, base::Value(1000));
  776. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  777. EXPECT_CALL(*delegate_ptr,
  778. OnKeyRepeatSettingsChanged(false, kDelta1000Ms, kDelta1000Ms));
  779. SetUserPref(email, ash::prefs::kXkbAutoRepeatInterval, base::Value(1000));
  780. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  781. }
  782. TEST_F(KeyboardTest, KeyRepeatSettingsIgnoredForNonActiveUser) {
  783. // Simulate two users, with the first user as active.
  784. CreateUserSessions(2);
  785. // Key repeat settings should be sent exactly once, for the default values.
  786. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  787. auto* delegate_ptr = delegate.get();
  788. EXPECT_CALL(*delegate_ptr,
  789. OnKeyRepeatSettingsChanged(true, kDelta500Ms, kDelta50Ms));
  790. Seat seat;
  791. Keyboard keyboard(std::move(delegate), &seat);
  792. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  793. // Set prefs for non-active user; no calls should result.
  794. EXPECT_CALL(*delegate_ptr,
  795. OnKeyRepeatSettingsChanged(testing::_, testing::_, testing::_))
  796. .Times(0);
  797. const std::string email = "user1@tray";
  798. SetUserPref(email, ash::prefs::kXkbAutoRepeatEnabled, base::Value(true));
  799. SetUserPref(email, ash::prefs::kXkbAutoRepeatDelay, base::Value(1000));
  800. SetUserPref(email, ash::prefs::kXkbAutoRepeatInterval, base::Value(1000));
  801. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  802. }
  803. TEST_F(KeyboardTest, KeyRepeatSettingsUpdateOnProfileChange) {
  804. // Simulate two users, with the first user as active.
  805. CreateUserSessions(2);
  806. // Second user has different preferences.
  807. std::string email = "user1@tray";
  808. SetUserPref(email, ash::prefs::kXkbAutoRepeatEnabled, base::Value(true));
  809. SetUserPref(email, ash::prefs::kXkbAutoRepeatDelay, base::Value(1000));
  810. SetUserPref(email, ash::prefs::kXkbAutoRepeatInterval, base::Value(1000));
  811. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  812. auto* delegate_ptr = delegate.get();
  813. // Initially, load default prefs for first user.
  814. EXPECT_CALL(*delegate_ptr,
  815. OnKeyRepeatSettingsChanged(true, kDelta500Ms, kDelta50Ms));
  816. Seat seat;
  817. Keyboard keyboard(std::move(delegate), &seat);
  818. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  819. // Switching user should load new prefs.
  820. EXPECT_CALL(*delegate_ptr,
  821. OnKeyRepeatSettingsChanged(true, kDelta1000Ms, kDelta1000Ms));
  822. SimulateUserLogin(email, user_manager::UserType::USER_TYPE_REGULAR);
  823. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  824. }
  825. TEST_F(KeyboardTest, KeyboardLayout) {
  826. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  827. auto* delegate_ptr = delegate.get();
  828. // Initially, update to the current keyboard layout.
  829. EXPECT_CALL(*delegate_ptr, OnKeyboardLayoutUpdated(testing::_));
  830. Seat seat;
  831. Keyboard keyboard(std::move(delegate), &seat);
  832. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  833. // Updating the keyboard layout should trigger the delegate call.
  834. EXPECT_CALL(*delegate_ptr, OnKeyboardLayoutUpdated(testing::_));
  835. keyboard.OnKeyboardLayoutNameChanged("ja-jp");
  836. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  837. }
  838. TEST_F(KeyboardTest, KeyboardObserver) {
  839. // Declare before the keyboard so the mock verification happens
  840. // after the keyboard destruction.
  841. MockKeyboardObserver observer1;
  842. MockKeyboardObserver observer2;
  843. Seat seat;
  844. Keyboard keyboard(std::make_unique<NiceMockKeyboardDelegate>(), &seat);
  845. keyboard.AddObserver(&observer1);
  846. keyboard.AddObserver(&observer2);
  847. EXPECT_TRUE(keyboard.HasObserver(&observer1));
  848. EXPECT_TRUE(keyboard.HasObserver(&observer2));
  849. testing::Mock::VerifyAndClearExpectations(&observer1);
  850. testing::Mock::VerifyAndClearExpectations(&observer2);
  851. keyboard.RemoveObserver(&observer1);
  852. EXPECT_FALSE(keyboard.HasObserver(&observer1));
  853. EXPECT_TRUE(keyboard.HasObserver(&observer2));
  854. testing::Mock::VerifyAndClearExpectations(&observer1);
  855. testing::Mock::VerifyAndClearExpectations(&observer2);
  856. // Called from the destructor of Keyboard.
  857. EXPECT_CALL(observer1, OnKeyboardDestroying(&keyboard)).Times(0);
  858. EXPECT_CALL(observer2, OnKeyboardDestroying(&keyboard));
  859. }
  860. TEST_F(KeyboardTest, NeedKeyboardKeyAcks) {
  861. std::unique_ptr<Surface> surface(new Surface);
  862. std::unique_ptr<ShellSurface> shell_surface(new ShellSurface(surface.get()));
  863. gfx::Size buffer_size(10, 10);
  864. std::unique_ptr<Buffer> buffer(
  865. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  866. surface->Attach(buffer.get());
  867. surface->Commit();
  868. aura::client::FocusClient* focus_client =
  869. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  870. focus_client->FocusWindow(nullptr);
  871. Seat seat;
  872. Keyboard keyboard(std::make_unique<NiceMockKeyboardDelegate>(), &seat);
  873. EXPECT_FALSE(keyboard.AreKeyboardKeyAcksNeeded());
  874. keyboard.SetNeedKeyboardKeyAcks(true);
  875. EXPECT_TRUE(keyboard.AreKeyboardKeyAcksNeeded());
  876. keyboard.SetNeedKeyboardKeyAcks(false);
  877. EXPECT_FALSE(keyboard.AreKeyboardKeyAcksNeeded());
  878. }
  879. TEST_F(KeyboardTest, AckKeyboardKey) {
  880. std::unique_ptr<Surface> surface(new Surface);
  881. auto shell_surface = std::make_unique<TestShellSurface>(surface.get());
  882. gfx::Size buffer_size(10, 10);
  883. std::unique_ptr<Buffer> buffer(
  884. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  885. surface->Attach(buffer.get());
  886. surface->Commit();
  887. aura::client::FocusClient* focus_client =
  888. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  889. focus_client->FocusWindow(nullptr);
  890. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  891. auto* delegate_ptr = delegate.get();
  892. Seat seat;
  893. Keyboard keyboard(std::move(delegate), &seat);
  894. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  895. EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
  896. .WillOnce(testing::Return(true));
  897. EXPECT_CALL(*delegate_ptr,
  898. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  899. EXPECT_CALL(
  900. *delegate_ptr,
  901. OnKeyboardEnter(surface.get(), base::flat_map<ui::DomCode, KeyState>()));
  902. focus_client->FocusWindow(surface->window());
  903. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  904. // If we don't set NeedKeyboardAckKeys to true, accelerators are always passed
  905. // to ShellSurface.
  906. ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
  907. // Press KEY_W with Ctrl.
  908. EXPECT_CALL(*shell_surface.get(), AcceleratorPressed(ui::Accelerator(
  909. ui::VKEY_W, ui::EF_CONTROL_DOWN,
  910. ui::Accelerator::KeyState::PRESSED)))
  911. .WillOnce(testing::Return(true));
  912. seat.set_physical_code_for_currently_processing_event_for_testing(
  913. ui::DomCode::US_W);
  914. generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
  915. // Release KEY_W.
  916. generator.ReleaseKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
  917. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  918. testing::Mock::VerifyAndClearExpectations(shell_surface.get());
  919. // If we set NeedKeyboardAckKeys to true, only unhandled accelerators are
  920. // passed to ShellSurface.
  921. keyboard.SetNeedKeyboardKeyAcks(true);
  922. // Press KEY_W with Ctrl.
  923. EXPECT_CALL(*delegate_ptr, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
  924. .WillOnce(testing::Return(1));
  925. generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
  926. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  927. // Send ack for the key press.
  928. EXPECT_CALL(*shell_surface.get(), AcceleratorPressed(ui::Accelerator(
  929. ui::VKEY_W, ui::EF_CONTROL_DOWN,
  930. ui::Accelerator::KeyState::PRESSED)))
  931. .WillOnce(testing::Return(true));
  932. keyboard.AckKeyboardKey(1, false /* handled */);
  933. testing::Mock::VerifyAndClearExpectations(shell_surface.get());
  934. // Release KEY_W.
  935. EXPECT_CALL(*delegate_ptr,
  936. OnKeyboardKey(testing::_, ui::DomCode::US_W, false))
  937. .WillOnce(testing::Return(2));
  938. generator.ReleaseKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
  939. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  940. // Send ack for the key release.
  941. keyboard.AckKeyboardKey(2, false /* handled */);
  942. // Press KEY_W with Ctrl again.
  943. EXPECT_CALL(*delegate_ptr, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
  944. .WillOnce(testing::Return(3));
  945. generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
  946. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  947. // Send ack for the key press.
  948. // AcceleratorPressed is not called when the accelerator is already handled.
  949. keyboard.AckKeyboardKey(3, true /* handled */);
  950. // A repeat key event should not be sent to the client and also should not
  951. // invoke the accelerator.
  952. EXPECT_CALL(*shell_surface.get(), AcceleratorPressed(ui::Accelerator(
  953. ui::VKEY_W, ui::EF_CONTROL_DOWN,
  954. ui::Accelerator::KeyState::PRESSED)))
  955. .Times(0);
  956. generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN | ui::EF_IS_REPEAT);
  957. testing::Mock::VerifyAndClearExpectations(shell_surface.get());
  958. // Another key press event while holding the key is also ignored and should
  959. // not invoke the accelerator.
  960. EXPECT_CALL(*shell_surface.get(), AcceleratorPressed(ui::Accelerator(
  961. ui::VKEY_W, ui::EF_CONTROL_DOWN,
  962. ui::Accelerator::KeyState::PRESSED)))
  963. .Times(0);
  964. generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
  965. testing::Mock::VerifyAndClearExpectations(shell_surface.get());
  966. // Release the key and reset modifier_flags.
  967. EXPECT_CALL(*delegate_ptr,
  968. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  969. EXPECT_CALL(*delegate_ptr,
  970. OnKeyboardKey(testing::_, ui::DomCode::US_W, false));
  971. generator.ReleaseKey(ui::VKEY_W, 0);
  972. // Verify before destroying keyboard to make sure the expected call
  973. // is made on the methods above, rather than in the destructor.
  974. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  975. }
  976. TEST_F(KeyboardTest, AckKeyboardKeyMoveFocus) {
  977. std::unique_ptr<Surface> surface(new Surface);
  978. auto shell_surface = std::make_unique<TestShellSurface>(surface.get());
  979. gfx::Size buffer_size(10, 10);
  980. std::unique_ptr<Buffer> buffer(
  981. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  982. surface->Attach(buffer.get());
  983. surface->Commit();
  984. aura::client::FocusClient* focus_client =
  985. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  986. focus_client->FocusWindow(nullptr);
  987. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  988. auto* delegate_ptr = delegate.get();
  989. Seat seat;
  990. Keyboard keyboard(std::move(delegate), &seat);
  991. EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
  992. .WillOnce(testing::Return(true));
  993. EXPECT_CALL(*delegate_ptr,
  994. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  995. EXPECT_CALL(
  996. *delegate_ptr,
  997. OnKeyboardEnter(surface.get(), base::flat_map<ui::DomCode, KeyState>()));
  998. focus_client->FocusWindow(surface->window());
  999. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  1000. ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
  1001. keyboard.SetNeedKeyboardKeyAcks(true);
  1002. // Press KEY_W with Ctrl.
  1003. EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(KeyboardModifiers{
  1004. kControlMask | kNumLockMask, 0, 0, 0}))
  1005. .Times(1);
  1006. EXPECT_CALL(*delegate_ptr, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
  1007. .WillOnce(testing::Return(1));
  1008. seat.set_physical_code_for_currently_processing_event_for_testing(
  1009. ui::DomCode::US_W);
  1010. generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
  1011. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  1012. // Move focus from the window
  1013. EXPECT_CALL(*delegate_ptr, OnKeyboardLeave(surface.get()));
  1014. focus_client->FocusWindow(nullptr);
  1015. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  1016. // Send ack for the key press. |AcceleratorPressed()| should not be called.
  1017. keyboard.AckKeyboardKey(1, false /* handled */);
  1018. }
  1019. TEST_F(KeyboardTest, AckKeyboardKeyExpired) {
  1020. std::unique_ptr<Surface> surface(new Surface);
  1021. auto shell_surface = std::make_unique<TestShellSurface>(surface.get());
  1022. gfx::Size buffer_size(10, 10);
  1023. std::unique_ptr<Buffer> buffer(
  1024. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  1025. surface->Attach(buffer.get());
  1026. surface->Commit();
  1027. aura::client::FocusClient* focus_client =
  1028. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  1029. focus_client->FocusWindow(nullptr);
  1030. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  1031. auto* delegate_ptr = delegate.get();
  1032. Seat seat;
  1033. Keyboard keyboard(std::move(delegate), &seat);
  1034. EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
  1035. .WillOnce(testing::Return(true));
  1036. EXPECT_CALL(*delegate_ptr,
  1037. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  1038. EXPECT_CALL(
  1039. *delegate_ptr,
  1040. OnKeyboardEnter(surface.get(), base::flat_map<ui::DomCode, KeyState>()));
  1041. focus_client->FocusWindow(surface->window());
  1042. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  1043. ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
  1044. keyboard.SetNeedKeyboardKeyAcks(true);
  1045. // Press KEY_W with Ctrl.
  1046. EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(KeyboardModifiers{
  1047. kControlMask | kNumLockMask, 0, 0, 0}));
  1048. EXPECT_CALL(*delegate_ptr, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
  1049. .WillOnce(testing::Return(1));
  1050. seat.set_physical_code_for_currently_processing_event_for_testing(
  1051. ui::DomCode::US_W);
  1052. generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
  1053. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  1054. // Keyboard processes pending events as if it is handled when it expires,
  1055. // so |AcceleratorPressed()| should not be called.
  1056. EXPECT_CALL(*shell_surface.get(), AcceleratorPressed(ui::Accelerator(
  1057. ui::VKEY_W, ui::EF_CONTROL_DOWN,
  1058. ui::Accelerator::KeyState::PRESSED)))
  1059. .Times(0);
  1060. // Wait until |ProcessExpiredPendingKeyAcks| is fired.
  1061. base::RunLoop run_loop;
  1062. base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
  1063. FROM_HERE, run_loop.QuitClosure(), base::Milliseconds(1000));
  1064. run_loop.Run();
  1065. base::RunLoop().RunUntilIdle();
  1066. // Send ack for the key press as if it was not handled. In the normal case,
  1067. // |AcceleratorPressed()| should be called, but since the timeout passed, the
  1068. // key should have been treated as handled already and removed from the
  1069. // pending_key_acks_ map. Since the event is no longer in the map,
  1070. // |AcceleratorPressed()| should not be called.
  1071. keyboard.AckKeyboardKey(1, false /* handled */);
  1072. // Release the key and reset modifier_flags.
  1073. EXPECT_CALL(*delegate_ptr,
  1074. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  1075. EXPECT_CALL(*delegate_ptr,
  1076. OnKeyboardKey(testing::_, ui::DomCode::US_W, false));
  1077. generator.ReleaseKey(ui::VKEY_W, 0);
  1078. // Verify before destroying keyboard to make sure the expected call
  1079. // is made on the methods above, rather than in the destructor.
  1080. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  1081. }
  1082. // Test for crbug.com/753539. If action for an accelerator moves the focus to
  1083. // another window, it causes clearing the map of pending key acks in Keyboard.
  1084. // We can't assume that an iterator of the map is valid after processing an
  1085. // accelerator.
  1086. class TestShellSurfaceWithMovingFocusAccelerator : public ShellSurface {
  1087. public:
  1088. explicit TestShellSurfaceWithMovingFocusAccelerator(Surface* surface)
  1089. : ShellSurface(surface) {}
  1090. bool AcceleratorPressed(const ui::Accelerator& accelerator) override {
  1091. aura::client::FocusClient* focus_client =
  1092. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  1093. focus_client->FocusWindow(nullptr);
  1094. return true;
  1095. }
  1096. };
  1097. TEST_F(KeyboardTest, AckKeyboardKeyExpiredWithMovingFocusAccelerator) {
  1098. std::unique_ptr<Surface> surface(new Surface);
  1099. auto shell_surface =
  1100. std::make_unique<TestShellSurfaceWithMovingFocusAccelerator>(
  1101. surface.get());
  1102. gfx::Size buffer_size(10, 10);
  1103. std::unique_ptr<Buffer> buffer(
  1104. new Buffer(exo_test_helper()->CreateGpuMemoryBuffer(buffer_size)));
  1105. surface->Attach(buffer.get());
  1106. surface->Commit();
  1107. aura::client::FocusClient* focus_client =
  1108. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  1109. focus_client->FocusWindow(nullptr);
  1110. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  1111. auto* delegate_ptr = delegate.get();
  1112. Seat seat;
  1113. Keyboard keyboard(std::move(delegate), &seat);
  1114. EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
  1115. .WillOnce(testing::Return(true));
  1116. EXPECT_CALL(*delegate_ptr,
  1117. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  1118. EXPECT_CALL(
  1119. *delegate_ptr,
  1120. OnKeyboardEnter(surface.get(), base::flat_map<ui::DomCode, KeyState>()));
  1121. focus_client->FocusWindow(surface->window());
  1122. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  1123. ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
  1124. keyboard.SetNeedKeyboardKeyAcks(true);
  1125. // Press KEY_W with Ctrl.
  1126. EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(KeyboardModifiers{
  1127. kControlMask | kNumLockMask, 0, 0, 0}));
  1128. EXPECT_CALL(*delegate_ptr, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
  1129. .WillOnce(testing::Return(1));
  1130. seat.set_physical_code_for_currently_processing_event_for_testing(
  1131. ui::DomCode::US_W);
  1132. generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
  1133. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  1134. EXPECT_CALL(*delegate_ptr, OnKeyboardLeave(surface.get()));
  1135. // Send ack as unhandled. This will call |AcceleratorPressed| and move the
  1136. // focus.
  1137. keyboard.AckKeyboardKey(1, false /* handled */);
  1138. // Wait until |ProcessExpiredPendingKeyAcks| is fired.
  1139. base::RunLoop run_loop;
  1140. base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
  1141. FROM_HERE, run_loop.QuitClosure(), base::Milliseconds(1000));
  1142. run_loop.Run();
  1143. base::RunLoop().RunUntilIdle();
  1144. // Verify before destroying keyboard to make sure the expected call
  1145. // is made on the methods above, rather than in the destructor.
  1146. testing::Mock::VerifyAndClearExpectations(&delegate);
  1147. }
  1148. TEST_F(KeyboardTest, OnKeyboardKey_ChangeFocusInPreTargetHandler) {
  1149. auto shell_surface = test::ShellSurfaceBuilder({10, 10}).BuildShellSurface();
  1150. auto* surface = shell_surface->surface_for_testing();
  1151. auto normal_window = CreateAppWindow(gfx::Rect(0, 0, 100, 100));
  1152. TestEventHandler handler{shell_surface->GetWidget()->GetNativeView()};
  1153. aura::client::FocusClient* focus_client =
  1154. aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  1155. focus_client->FocusWindow(nullptr);
  1156. auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
  1157. auto* delegate_ptr = delegate.get();
  1158. NiceMockKeyboardObserver observer;
  1159. Seat seat;
  1160. Keyboard keyboard(std::move(delegate), &seat);
  1161. keyboard.AddObserver(&observer);
  1162. // Focus the non-exo window.
  1163. focus_client->FocusWindow(normal_window.get());
  1164. ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
  1165. // Keyboard should not get a key event sent to the non-exo window.
  1166. generator.PressKey(ui::VKEY_A, 0);
  1167. generator.ReleaseKey(ui::VKEY_A, 0);
  1168. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  1169. // Sending a key event causes a focus change.
  1170. // It calls OnKeyboardEnter, but OnKeyboardKey should not be called because
  1171. // the event's target is |normal_window|.
  1172. wm_helper()->AddPreTargetHandler(&handler);
  1173. EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface))
  1174. .WillOnce(testing::Return(true));
  1175. EXPECT_CALL(*delegate_ptr,
  1176. OnKeyboardModifiers(KeyboardModifiers{kNumLockMask, 0, 0, 0}));
  1177. EXPECT_CALL(
  1178. *delegate_ptr,
  1179. OnKeyboardEnter(surface, base::flat_map<ui::DomCode, KeyState>()));
  1180. generator.PressKey(ui::VKEY_A, 0);
  1181. EXPECT_EQ(shell_surface->GetWidget()->GetNativeView(),
  1182. focus_client->GetFocusedWindow());
  1183. testing::Mock::VerifyAndClearExpectations(delegate_ptr);
  1184. wm_helper()->RemovePreTargetHandler(&handler);
  1185. }
  1186. } // namespace
  1187. } // namespace exo