- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / options / chromeos / cros_language_options_handler_unittest.cc
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     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(id,
50                                  "",  // name
51                                  layouts,
52                                  languages,
53                                  true,  // use on login screen.
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(4U, 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
85   ASSERT_TRUE(list->GetDictionary(1, &entry));
86   ASSERT_TRUE(entry->GetString("id", &input_method_id));
87   ASSERT_TRUE(entry->GetString("displayName", &display_name));
88   ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set));
89   EXPECT_EQ("xkb:fr::fra", input_method_id);
90   // Commented out. See above.
91   // EXPECT_EQ("French keyboard layout", display_name);
92   ASSERT_TRUE(language_code_set->HasKey("fr"));
93
94   ASSERT_TRUE(list->GetDictionary(2, &entry));
95   ASSERT_TRUE(entry->GetString("id", &input_method_id));
96   ASSERT_TRUE(entry->GetString("displayName", &display_name));
97   ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set));
98   EXPECT_EQ("xkb:be::fra", input_method_id);
99   // Commented out. See above.
100   // EXPECT_EQ("Belgian keyboard layout", display_name);
101   ASSERT_TRUE(language_code_set->HasKey("fr"));
102
103   ASSERT_TRUE(list->GetDictionary(3, &entry));
104   ASSERT_TRUE(entry->GetString("id", &input_method_id));
105   ASSERT_TRUE(entry->GetString("displayName", &display_name));
106   ASSERT_TRUE(entry->GetDictionary("languageCodeSet", &language_code_set));
107   EXPECT_EQ("xkb:is::ice", input_method_id);
108   // Commented out. See above.
109   // EXPECT_EQ("Japanese input method (for US keyboard)", display_name);
110   ASSERT_TRUE(language_code_set->HasKey("is"));
111 }
112
113 TEST_F(CrosLanguageOptionsHandlerTest, GetUILanguageList) {
114   InputMethodDescriptors descriptors = CreateInputMethodDescriptors();
115   scoped_ptr<ListValue> list(
116       chromeos::options::CrosLanguageOptionsHandler::GetUILanguageList(
117           descriptors));
118
119   for (size_t i = 0; i < list->GetSize(); ++i) {
120     base::DictionaryValue* dict;
121     ASSERT_TRUE(list->GetDictionary(i, &dict));
122     std::string code;
123     ASSERT_TRUE(dict->GetString("code", &code));
124     EXPECT_NE("is", code)
125         << "Icelandic is an example language which has input method "
126         << "but can't use it as UI language.";
127   }
128 }