[go: up one dir, main page]

mac: enable input sources before selecting them in ScopedKeyboardLayout

On the bots, for some reason, even the default en-US keyboard layout
is not "enabled". We theorize that this may happen if the input menu
itself has never been used on a given install. In any case, manually
enabling it appears to be harmless and fixes these tests on the bots.

(cherry picked from commit 7ff5b1f91e83879f4b6d2cb325683d30848e9ba8)

Bug: 1171531
Change-Id: I26b93de0c587c8c1378a9a3a4e6102765920978e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2657298
Reviewed-by: Leonard Grey <lgrey@chromium.org>
Reviewed-by: Avi Drissman <avi@chromium.org>
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#848571}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2670716
Reviewed-by: Zhaoyang Li <zhaoyangli@chromium.org>
Commit-Queue: Zhaoyang Li <zhaoyangli@chromium.org>
Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Auto-Submit: Zhaoyang Li <zhaoyangli@chromium.org>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/branch-heads/4324@{#2101}
Cr-Branched-From: c73b5a651d37a6c4d0b8e3262cc4015a5579c6c8-refs/heads/master@{#827102}
diff --git a/ui/events/test/keyboard_layout_mac.cc b/ui/events/test/keyboard_layout_mac.cc
index d2f08337..d5006b9f 100644
--- a/ui/events/test/keyboard_layout_mac.cc
+++ b/ui/events/test/keyboard_layout_mac.cc
@@ -40,7 +40,17 @@
 
 void ScopedKeyboardLayout::ActivateLayout(PlatformKeyboardLayout layout) {
   DCHECK(layout);
-  OSStatus result = TISSelectInputSource(layout);
+  // According to the documentation in HIToolbox's TextInputSources.h
+  // (recommended reading), TISSelectInputSource() can fail if the input source
+  // isn't "selectable" or "enabled".
+  //
+  // On the bots, for some reason, sometimes the US keyboard layout isn't
+  // "enabled" even though it is present - we aren't sure why this happens,
+  // perhaps if input sources have never been switched on this bot before? In
+  // any case, it's harmless to re-enable it here if it's already enabled.
+  OSStatus result = TISEnableInputSource(layout);
+  DCHECK_EQ(noErr, result);
+  result = TISSelectInputSource(layout);
   DCHECK_EQ(noErr, result);
 }