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. 4 5#include "chrome/test/chromedriver/test_util.h" 6 7#include "base/logging.h" 8#include "base/strings/utf_string_conversions.h" 9 10RestoreKeyboardLayoutOnDestruct::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 18} 19 20RestoreKeyboardLayoutOnDestruct::~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 28} 29 30#if defined(OS_WIN) 31bool SwitchKeyboardLayout(const std::string& input_locale_identifier) { 32 HKL layout = LoadKeyboardLayout( 33 UTF8ToWide(input_locale_identifier).c_str(), 0); 34 if (!layout) 35 return false; 36 return !!ActivateKeyboardLayout(layout, 0); 37} 38#endif // defined(OS_WIN) 39 40#if defined(OS_MACOSX) 41bool SwitchKeyboardLayout(const std::string& input_source_id) { 42 base::ScopedCFTypeRef<CFMutableDictionaryRef> filter_dict( 43 CFDictionaryCreateMutable(kCFAllocatorDefault, 44 1, 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; 57} 58#endif // defined(OS_MACOSX) 59