Home | History | Annotate | Download | only in chromeos
      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 
     16 using chromeos::input_method::InputMethodDescriptor;
     17 using chromeos::input_method::InputMethodDescriptors;
     18 using chromeos::input_method::MockInputMethodManager;
     19 
     20 namespace {
     21 
     22 class 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     descriptors.push_back(GetDesc("mozc", "us", "ja"));
     39     return descriptors;
     40   }
     41 
     42  private:
     43   InputMethodDescriptor GetDesc(const std::string& id,
     44                                 const std::string& raw_layout,
     45                                 const std::string& language_code) {
     46     std::vector<std::string> layouts;
     47     layouts.push_back(raw_layout);
     48     std::vector<std::string> languages;
     49     languages.push_back(language_code);
     50     return InputMethodDescriptor(id,
     51                                  "",  // name
     52                                  layouts,
     53                                  languages,
     54                                  GURL());  // options page url
     55   }
     56 };
     57 
     58 }  // namespace
     59 
     60 TEST_F(CrosLanguageOptionsHandlerTest, GetInputMethodList) {
     61   InputMethodDescriptors descriptors = CreateInputMethodDescriptors();
     62   scoped_ptr<ListValue> list(
     63       chromeos::options::CrosLanguageOptionsHandler::GetInputMethodList(
     64           descriptors));
     65   ASSERT_EQ(5U, list->GetSize());
     66 
     67   DictionaryValue* entry = NULL;
     68   DictionaryValue *language_code_set = NULL;
     69   std::string input_method_id;
     70   std::string display_name;
     71   std::string language_code;
     72 
     73   // As shown below, the list should be input method ids should appear in
     74   // the same order of the descriptors.
     75   ASSERT_TRUE(list->GetDictionary(0, &entry));
     76   ASSERT_TRUE(entry->GetString("id", &input_method_id));
     77   ASSERT_TRUE(entry->GetString("displayName", &display_name));
     78   ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set));
     79   EXPECT_EQ("xkb:us::eng", input_method_id);
     80   // Commented out as it depends on translation in generated_resources.grd
     81   // (i.e. makes the test fragile).
     82   // EXPECT_EQ("English (USA) keyboard layout", display_name);
     83   ASSERT_TRUE(language_code_set->HasKey("en-US"));
     84   ASSERT_TRUE(language_code_set->HasKey("id"));  // From kExtraLanguages.
     85   ASSERT_TRUE(language_code_set->HasKey("fil"));  // From kExtraLanguages.
     86 
     87   ASSERT_TRUE(list->GetDictionary(1, &entry));
     88   ASSERT_TRUE(entry->GetString("id", &input_method_id));
     89   ASSERT_TRUE(entry->GetString("displayName", &display_name));
     90   ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set));
     91   EXPECT_EQ("xkb:fr::fra", input_method_id);
     92   // Commented out. See above.
     93   // EXPECT_EQ("French keyboard layout", display_name);
     94   ASSERT_TRUE(language_code_set->HasKey("fr"));
     95 
     96   ASSERT_TRUE(list->GetDictionary(2, &entry));
     97   ASSERT_TRUE(entry->GetString("id", &input_method_id));
     98   ASSERT_TRUE(entry->GetString("displayName", &display_name));
     99   ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set));
    100   EXPECT_EQ("xkb:be::fra", input_method_id);
    101   // Commented out. See above.
    102   // EXPECT_EQ("Belgian keyboard layout", display_name);
    103   ASSERT_TRUE(language_code_set->HasKey("fr"));
    104 
    105   ASSERT_TRUE(list->GetDictionary(3, &entry));
    106   ASSERT_TRUE(entry->GetString("id", &input_method_id));
    107   ASSERT_TRUE(entry->GetString("displayName", &display_name));
    108   ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set));
    109   EXPECT_EQ("xkb:is::ice", input_method_id);
    110   // Commented out. See above.
    111   // EXPECT_EQ("Japanese input method (for US keyboard)", display_name);
    112   ASSERT_TRUE(language_code_set->HasKey("is"));
    113 
    114   ASSERT_TRUE(list->GetDictionary(4, &entry));
    115   ASSERT_TRUE(entry->GetString("id", &input_method_id));
    116   ASSERT_TRUE(entry->GetString("displayName", &display_name));
    117   ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set));
    118   EXPECT_EQ("mozc", input_method_id);
    119   // Commented out. See above.
    120   // EXPECT_EQ("Japanese input method (for US keyboard)", display_name);
    121   ASSERT_TRUE(language_code_set->HasKey("ja"));
    122 }
    123 
    124 TEST_F(CrosLanguageOptionsHandlerTest, GetUILanguageList) {
    125   InputMethodDescriptors descriptors = CreateInputMethodDescriptors();
    126   scoped_ptr<ListValue> list(
    127       chromeos::options::CrosLanguageOptionsHandler::GetUILanguageList(
    128           descriptors));
    129 
    130   for (size_t i = 0; i < list->GetSize(); ++i) {
    131     base::DictionaryValue* dict;
    132     ASSERT_TRUE(list->GetDictionary(i, &dict));
    133     std::string code;
    134     ASSERT_TRUE(dict->GetString("code", &code));
    135     EXPECT_NE("is", code)
    136         << "Icelandic is an example language which has input method "
    137         << "but can't use it as UI language.";
    138   }
    139 }
    140