Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / test / chromedriver / test_util.cc
blobbfa7de407af8596e52871f25fae4951240f816ae
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() {
11 #if defined(OS_WIN)
12 layout_ = GetKeyboardLayout(NULL);
13 #elif defined(OS_MACOSX)
14 layout_.reset(TISCopyCurrentKeyboardInputSource());
15 #elif defined(OS_LINUX)
16 NOTIMPLEMENTED();
17 #endif
20 RestoreKeyboardLayoutOnDestruct::~RestoreKeyboardLayoutOnDestruct() {
21 #if defined(OS_WIN)
22 ActivateKeyboardLayout(layout_, 0);
23 #elif defined(OS_MACOSX)
24 TISSelectInputSource(layout_);
25 #elif defined(OS_LINUX)
26 NOTIMPLEMENTED();
27 #endif
30 #if defined(OS_WIN)
31 bool SwitchKeyboardLayout(const std::string& input_locale_identifier) {
32 HKL layout = LoadKeyboardLayout(
33 base::UTF8ToWide(input_locale_identifier).c_str(), 0);
34 if (!layout)
35 return false;
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)
53 return false;
54 TISInputSourceRef source = (TISInputSourceRef)CFArrayGetValueAtIndex(
55 sources, 0);
56 return TISSelectInputSource(source) == noErr;
58 #endif // defined(OS_MACOSX)