cros_language_options_handler_unittest.cc revision f2477e01787aa58f445919b809d89e252beef54f
1// Copyright (c) 2012 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/browser/ui/webui/options/language_options_handler.h" 6 7#include <string> 8 9#include "base/values.h" 10#include "chrome/browser/chromeos/input_method/input_method_configuration.h" 11#include "chrome/browser/chromeos/input_method/mock_input_method_manager.h" 12#include "chrome/browser/ui/webui/options/chromeos/cros_language_options_handler.h" 13#include "chromeos/ime/input_method_descriptor.h" 14#include "testing/gtest/include/gtest/gtest.h" 15 16using chromeos::input_method::InputMethodDescriptor; 17using chromeos::input_method::InputMethodDescriptors; 18using chromeos::input_method::MockInputMethodManager; 19 20namespace { 21 22class CrosLanguageOptionsHandlerTest : public testing::Test { 23 public: 24 CrosLanguageOptionsHandlerTest() { 25 chromeos::input_method::InitializeForTesting(new MockInputMethodManager); 26 } 27 virtual ~CrosLanguageOptionsHandlerTest() { 28 chromeos::input_method::Shutdown(); 29 } 30 31 protected: 32 InputMethodDescriptors CreateInputMethodDescriptors() { 33 InputMethodDescriptors descriptors; 34 descriptors.push_back(GetDesc("xkb:us::eng", "us", "en-US")); 35 descriptors.push_back(GetDesc("xkb:fr::fra", "fr", "fr")); 36 descriptors.push_back(GetDesc("xkb:be::fra", "be", "fr")); 37 descriptors.push_back(GetDesc("xkb:is::ice", "is", "is")); 38 return descriptors; 39 } 40 41 private: 42 InputMethodDescriptor GetDesc(const std::string& id, 43 const std::string& raw_layout, 44 const std::string& language_code) { 45 std::vector<std::string> layouts; 46 layouts.push_back(raw_layout); 47 std::vector<std::string> languages; 48 languages.push_back(language_code); 49 return InputMethodDescriptor( 50 id, std::string(), layouts, languages, true, GURL(), GURL()); 51 } 52}; 53 54} // namespace 55 56TEST_F(CrosLanguageOptionsHandlerTest, GetInputMethodList) { 57 InputMethodDescriptors descriptors = CreateInputMethodDescriptors(); 58 scoped_ptr<ListValue> list( 59 chromeos::options::CrosLanguageOptionsHandler::GetInputMethodList( 60 descriptors)); 61 ASSERT_EQ(4U, list->GetSize()); 62 63 DictionaryValue* entry = NULL; 64 DictionaryValue *language_code_set = NULL; 65 std::string input_method_id; 66 std::string display_name; 67 std::string language_code; 68 69 // As shown below, the list should be input method ids should appear in 70 // the same order of the descriptors. 71 ASSERT_TRUE(list->GetDictionary(0, &entry)); 72 ASSERT_TRUE(entry->GetString("id", &input_method_id)); 73 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 74 ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set)); 75 EXPECT_EQ("xkb:us::eng", input_method_id); 76 // Commented out as it depends on translation in generated_resources.grd 77 // (i.e. makes the test fragile). 78 // EXPECT_EQ("English (USA) keyboard layout", display_name); 79 ASSERT_TRUE(language_code_set->HasKey("en-US")); 80 81 ASSERT_TRUE(list->GetDictionary(1, &entry)); 82 ASSERT_TRUE(entry->GetString("id", &input_method_id)); 83 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 84 ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set)); 85 EXPECT_EQ("xkb:fr::fra", input_method_id); 86 // Commented out. See above. 87 // EXPECT_EQ("French keyboard layout", display_name); 88 ASSERT_TRUE(language_code_set->HasKey("fr")); 89 90 ASSERT_TRUE(list->GetDictionary(2, &entry)); 91 ASSERT_TRUE(entry->GetString("id", &input_method_id)); 92 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 93 ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set)); 94 EXPECT_EQ("xkb:be::fra", input_method_id); 95 // Commented out. See above. 96 // EXPECT_EQ("Belgian keyboard layout", display_name); 97 ASSERT_TRUE(language_code_set->HasKey("fr")); 98 99 ASSERT_TRUE(list->GetDictionary(3, &entry)); 100 ASSERT_TRUE(entry->GetString("id", &input_method_id)); 101 ASSERT_TRUE(entry->GetString("displayName", &display_name)); 102 ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set)); 103 EXPECT_EQ("xkb:is::ice", input_method_id); 104 // Commented out. See above. 105 // EXPECT_EQ("Japanese input method (for US keyboard)", display_name); 106 ASSERT_TRUE(language_code_set->HasKey("is")); 107} 108 109TEST_F(CrosLanguageOptionsHandlerTest, GetUILanguageList) { 110 InputMethodDescriptors descriptors = CreateInputMethodDescriptors(); 111 scoped_ptr<ListValue> list( 112 chromeos::options::CrosLanguageOptionsHandler::GetUILanguageList( 113 descriptors)); 114 115 for (size_t i = 0; i < list->GetSize(); ++i) { 116 base::DictionaryValue* dict; 117 ASSERT_TRUE(list->GetDictionary(i, &dict)); 118 std::string code; 119 ASSERT_TRUE(dict->GetString("code", &code)); 120 EXPECT_NE("is", code) 121 << "Icelandic is an example language which has input method " 122 << "but can't use it as UI language."; 123 } 124} 125