1 // Copyright (c) 2011 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/webdriver/webdriver_test_util.h"
7 #include "base/logging.h"
8 #include "base/utf_string_conversions.h"
12 RestoreKeyboardLayoutOnDestruct::RestoreKeyboardLayoutOnDestruct() {
14 layout_
= GetKeyboardLayout(NULL
);
15 #elif defined(OS_MACOSX)
16 layout_
.reset(TISCopyCurrentKeyboardInputSource());
17 #elif defined(OS_LINUX)
22 RestoreKeyboardLayoutOnDestruct::~RestoreKeyboardLayoutOnDestruct() {
24 ActivateKeyboardLayout(layout_
, 0);
25 #elif defined(OS_MACOSX)
26 TISSelectInputSource(layout_
);
27 #elif defined(OS_LINUX)
33 bool SwitchKeyboardLayout(const std::string
& input_locale_identifier
) {
34 HKL layout
= LoadKeyboardLayout(
35 UTF8ToWide(input_locale_identifier
).c_str(), 0);
38 return !!ActivateKeyboardLayout(layout
, 0);
40 #endif // defined(OS_WIN)
42 #if defined(OS_MACOSX)
43 bool SwitchKeyboardLayout(const std::string
& input_source_id
) {
44 base::mac::ScopedCFTypeRef
<CFMutableDictionaryRef
> filter_dict(
45 CFDictionaryCreateMutable(kCFAllocatorDefault
,
47 &kCFTypeDictionaryKeyCallBacks
,
48 &kCFTypeDictionaryValueCallBacks
));
49 base::mac::ScopedCFTypeRef
<CFStringRef
> id_ref(CFStringCreateWithCString(
50 kCFAllocatorDefault
, input_source_id
.c_str(), kCFStringEncodingUTF8
));
51 CFDictionaryAddValue(filter_dict
, kTISPropertyInputSourceID
, id_ref
);
52 base::mac::ScopedCFTypeRef
<CFArrayRef
> sources(
53 TISCreateInputSourceList(filter_dict
, true));
54 if (CFArrayGetCount(sources
) != 1)
56 TISInputSourceRef source
= (TISInputSourceRef
)CFArrayGetValueAtIndex(
58 return TISSelectInputSource(source
) == noErr
;
60 #endif // defined(OS_WIN)
62 } // namespace webdriver