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. 4 5 #include "chrome/test/webdriver/webdriver_test_util.h" 6 7 #include "base/logging.h" 8 #include "base/strings/utf_string_conversions.h" 9 10 namespace webdriver { 11 12 RestoreKeyboardLayoutOnDestruct::RestoreKeyboardLayoutOnDestruct() { 13 #if defined(OS_WIN) 14 layout_ = GetKeyboardLayout(NULL); 15 #elif defined(OS_MACOSX) 16 layout_.reset(TISCopyCurrentKeyboardInputSource()); 17 #elif defined(OS_LINUX) 18 NOTIMPLEMENTED(); 19 #endif 20 } 21 22 RestoreKeyboardLayoutOnDestruct::~RestoreKeyboardLayoutOnDestruct() { 23 #if defined(OS_WIN) 24 ActivateKeyboardLayout(layout_, 0); 25 #elif defined(OS_MACOSX) 26 TISSelectInputSource(layout_); 27 #elif defined(OS_LINUX) 28 NOTIMPLEMENTED(); 29 #endif 30 } 31 32 #if defined(OS_WIN) 33 bool SwitchKeyboardLayout(const std::string& input_locale_identifier) { 34 HKL layout = LoadKeyboardLayout( 35 UTF8ToWide(input_locale_identifier).c_str(), 0); 36 if (!layout) 37 return false; 38 return !!ActivateKeyboardLayout(layout, 0); 39 } 40 #endif // defined(OS_WIN) 41 42 #if defined(OS_MACOSX) 43 bool SwitchKeyboardLayout(const std::string& input_source_id) { 44 base::ScopedCFTypeRef<CFMutableDictionaryRef> filter_dict( 45 CFDictionaryCreateMutable(kCFAllocatorDefault, 46 1, 47 &kCFTypeDictionaryKeyCallBacks, 48 &kCFTypeDictionaryValueCallBacks)); 49 base::ScopedCFTypeRef<CFStringRef> id_ref(CFStringCreateWithCString( 50 kCFAllocatorDefault, input_source_id.c_str(), kCFStringEncodingUTF8)); 51 CFDictionaryAddValue(filter_dict, kTISPropertyInputSourceID, id_ref); 52 base::ScopedCFTypeRef<CFArrayRef> sources( 53 TISCreateInputSourceList(filter_dict, true)); 54 if (CFArrayGetCount(sources) != 1) 55 return false; 56 TISInputSourceRef source = (TISInputSourceRef)CFArrayGetValueAtIndex( 57 sources, 0); 58 return TISSelectInputSource(source) == noErr; 59 } 60 #endif // defined(OS_WIN) 61 62 } // namespace webdriver 63