1 // Copyright (c) 2013 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.
5 #include "chrome/test/chromedriver/test_util.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
10 RestoreKeyboardLayoutOnDestruct::RestoreKeyboardLayoutOnDestruct() {
12 layout_
= GetKeyboardLayout(NULL
);
13 #elif defined(OS_MACOSX)
14 layout_
.reset(TISCopyCurrentKeyboardInputSource());
15 #elif defined(OS_LINUX)
20 RestoreKeyboardLayoutOnDestruct::~RestoreKeyboardLayoutOnDestruct() {
22 ActivateKeyboardLayout(layout_
, 0);
23 #elif defined(OS_MACOSX)
24 TISSelectInputSource(layout_
);
25 #elif defined(OS_LINUX)
31 bool SwitchKeyboardLayout(const std::string
& input_locale_identifier
) {
32 HKL layout
= LoadKeyboardLayout(
33 base::UTF8ToWide(input_locale_identifier
).c_str(), 0);
36 return !!ActivateKeyboardLayout(layout
, 0);
38 #endif // defined(OS_WIN)
40 #if defined(OS_MACOSX)
41 bool SwitchKeyboardLayout(const std::string
& input_source_id
) {
42 base::ScopedCFTypeRef
<CFMutableDictionaryRef
> filter_dict(
43 CFDictionaryCreateMutable(kCFAllocatorDefault
,
45 &kCFTypeDictionaryKeyCallBacks
,
46 &kCFTypeDictionaryValueCallBacks
));
47 base::ScopedCFTypeRef
<CFStringRef
> id_ref(CFStringCreateWithCString(
48 kCFAllocatorDefault
, input_source_id
.c_str(), kCFStringEncodingUTF8
));
49 CFDictionaryAddValue(filter_dict
, kTISPropertyInputSourceID
, id_ref
);
50 base::ScopedCFTypeRef
<CFArrayRef
> sources(
51 TISCreateInputSourceList(filter_dict
, true));
52 if (CFArrayGetCount(sources
) != 1)
54 TISInputSourceRef source
= (TISInputSourceRef
)CFArrayGetValueAtIndex(
56 return TISSelectInputSource(source
) == noErr
;
58 #endif // defined(OS_MACOSX)