OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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/gtk/options/languages_page_gtk.h" | |
6 | |
7 #include <gtk/gtk.h> | |
8 | |
9 #include "base/message_loop.h" | |
10 #include "base/string_util.h" | |
11 #include "chrome/browser/language_combobox_model.h" | |
12 #include "chrome/browser/prefs/pref_service.h" | |
13 #include "chrome/common/pref_names.h" | |
14 #include "chrome/test/testing_profile.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 class LanguagesPageGtkTest : public testing::Test { | |
18 public: | |
19 virtual void SetUp() { | |
20 profile_.reset(new TestingProfile()); | |
21 } | |
22 | |
23 // Get the accept languages displayed in the dialog in the order they are | |
24 // displayed, as a comma seperated string. | |
25 // Ex: EXPECT_STREQ("en,ja", GetDisplayedLangs(page).c_str()); | |
26 std::string GetDisplayedLangs(const LanguagesPageGtk& page) { | |
27 std::vector<std::string> parts; | |
28 GtkTreeModel* tree_model = GTK_TREE_MODEL(page.language_order_store_); | |
29 GtkTreeIter iter; | |
30 if (!gtk_tree_model_get_iter_first(tree_model, &iter)) | |
31 return std::string(); | |
32 while (true) { | |
33 gchar* name; | |
34 gtk_tree_model_get(tree_model, &iter, LanguagesPageGtk::COL_LANG, &name, | |
35 -1); | |
36 parts.push_back(name); | |
37 g_free(name); | |
38 if (!gtk_tree_model_iter_next(tree_model, &iter)) | |
39 break; | |
40 } | |
41 return JoinString(parts, ','); | |
42 } | |
43 | |
44 std::string GetDisplayedSpellCheckerLang(const LanguagesPageGtk& page) { | |
45 gchar* text = gtk_combo_box_get_active_text( | |
46 GTK_COMBO_BOX(page.dictionary_language_combobox_)); | |
47 std::string result = text; | |
48 g_free(text); | |
49 int space_pos = result.find(' '); | |
50 if (space_pos) | |
51 result = result.substr(0, space_pos); | |
52 return result; | |
53 } | |
54 | |
55 protected: | |
56 MessageLoopForUI message_loop_; | |
57 scoped_ptr<TestingProfile> profile_; | |
58 }; | |
59 | |
60 TEST_F(LanguagesPageGtkTest, RemoveAcceptLang) { | |
61 profile_->GetPrefs()->SetString(prefs::kAcceptLanguages, "en,ja,es"); | |
62 LanguagesPageGtk page(profile_.get()); | |
63 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.add_button_)); | |
64 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
65 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
66 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
67 | |
68 GtkTreeIter iter; | |
69 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(page.language_order_store_), | |
70 &iter, NULL, 1); | |
71 gtk_tree_selection_select_iter(page.language_order_selection_, &iter); | |
72 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.add_button_)); | |
73 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
74 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
75 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
76 | |
77 gtk_button_clicked(GTK_BUTTON(page.remove_button_)); | |
78 EXPECT_STREQ("English,Spanish", GetDisplayedLangs(page).c_str()); | |
79 EXPECT_STREQ("en,es", | |
80 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
81 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.add_button_)); | |
82 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
83 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
84 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
85 EXPECT_EQ(1, page.FirstSelectedRowNum()); | |
86 | |
87 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(page.language_order_store_), | |
88 &iter, NULL, 1); | |
89 gtk_tree_selection_select_iter(page.language_order_selection_, &iter); | |
90 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.add_button_)); | |
91 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
92 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
93 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
94 | |
95 gtk_button_clicked(GTK_BUTTON(page.remove_button_)); | |
96 EXPECT_STREQ("English", GetDisplayedLangs(page).c_str()); | |
97 EXPECT_STREQ("en", | |
98 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
99 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.add_button_)); | |
100 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
101 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
102 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
103 EXPECT_EQ(0, page.FirstSelectedRowNum()); | |
104 | |
105 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(page.language_order_store_), | |
106 &iter, NULL, 0); | |
107 gtk_tree_selection_select_iter(page.language_order_selection_, &iter); | |
108 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.add_button_)); | |
109 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
110 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
111 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
112 | |
113 gtk_button_clicked(GTK_BUTTON(page.remove_button_)); | |
114 EXPECT_STREQ("", GetDisplayedLangs(page).c_str()); | |
115 EXPECT_STREQ("", | |
116 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
117 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.add_button_)); | |
118 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
119 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
120 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
121 EXPECT_EQ(-1, page.FirstSelectedRowNum()); | |
122 } | |
123 | |
124 TEST_F(LanguagesPageGtkTest, RemoveMultipleAcceptLang) { | |
125 profile_->GetPrefs()->SetString(prefs::kAcceptLanguages, "en,ja,es,fr,it"); | |
126 LanguagesPageGtk page(profile_.get()); | |
127 GtkTreeIter iter; | |
128 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(page.language_order_store_), | |
129 &iter, NULL, 1); | |
130 gtk_tree_selection_select_iter(page.language_order_selection_, &iter); | |
131 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(page.language_order_store_), | |
132 &iter, NULL, 3); | |
133 gtk_tree_selection_select_iter(page.language_order_selection_, &iter); | |
134 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(page.language_order_store_), | |
135 &iter, NULL, 4); | |
136 gtk_tree_selection_select_iter(page.language_order_selection_, &iter); | |
137 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
138 | |
139 gtk_button_clicked(GTK_BUTTON(page.remove_button_)); | |
140 EXPECT_STREQ("English,Spanish", GetDisplayedLangs(page).c_str()); | |
141 EXPECT_STREQ("en,es", | |
142 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
143 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
144 EXPECT_EQ(1, page.FirstSelectedRowNum()); | |
145 | |
146 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(page.language_order_store_), | |
147 &iter, NULL, 1); | |
148 gtk_tree_selection_select_iter(page.language_order_selection_, &iter); | |
149 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(page.language_order_store_), | |
150 &iter, NULL, 0); | |
151 gtk_tree_selection_select_iter(page.language_order_selection_, &iter); | |
152 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
153 | |
154 gtk_button_clicked(GTK_BUTTON(page.remove_button_)); | |
155 EXPECT_STREQ("", GetDisplayedLangs(page).c_str()); | |
156 EXPECT_STREQ("", | |
157 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
158 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
159 EXPECT_EQ(-1, page.FirstSelectedRowNum()); | |
160 } | |
161 | |
162 TEST_F(LanguagesPageGtkTest, MoveAcceptLang) { | |
163 profile_->GetPrefs()->SetString(prefs::kAcceptLanguages, "en,ja,es"); | |
164 LanguagesPageGtk page(profile_.get()); | |
165 EXPECT_STREQ("English,Japanese,Spanish", GetDisplayedLangs(page).c_str()); | |
166 GtkTreeIter iter; | |
167 | |
168 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(page.language_order_store_), | |
169 &iter, NULL, 0); | |
170 gtk_tree_selection_select_iter(page.language_order_selection_, &iter); | |
171 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
172 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
173 | |
174 gtk_button_clicked(GTK_BUTTON(page.move_down_button_)); | |
175 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
176 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
177 EXPECT_STREQ("Japanese,English,Spanish", GetDisplayedLangs(page).c_str()); | |
178 EXPECT_STREQ("ja,en,es", | |
179 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
180 | |
181 gtk_button_clicked(GTK_BUTTON(page.move_down_button_)); | |
182 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
183 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
184 EXPECT_STREQ("Japanese,Spanish,English", GetDisplayedLangs(page).c_str()); | |
185 EXPECT_STREQ("ja,es,en", | |
186 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
187 | |
188 gtk_button_clicked(GTK_BUTTON(page.move_up_button_)); | |
189 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
190 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
191 EXPECT_STREQ("Japanese,English,Spanish", GetDisplayedLangs(page).c_str()); | |
192 EXPECT_STREQ("ja,en,es", | |
193 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
194 | |
195 gtk_button_clicked(GTK_BUTTON(page.move_up_button_)); | |
196 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
197 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
198 EXPECT_STREQ("English,Japanese,Spanish", GetDisplayedLangs(page).c_str()); | |
199 EXPECT_STREQ("en,ja,es", | |
200 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
201 } | |
202 | |
203 TEST_F(LanguagesPageGtkTest, AddAcceptLang) { | |
204 profile_->GetPrefs()->SetString(prefs::kAcceptLanguages, ""); | |
205 LanguagesPageGtk page(profile_.get()); | |
206 EXPECT_STREQ("", GetDisplayedLangs(page).c_str()); | |
207 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.add_button_)); | |
208 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
209 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
210 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
211 | |
212 page.OnAddLanguage("en"); | |
213 EXPECT_STREQ("English", GetDisplayedLangs(page).c_str()); | |
214 EXPECT_STREQ("en", | |
215 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
216 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.add_button_)); | |
217 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
218 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
219 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
220 EXPECT_EQ(0, page.FirstSelectedRowNum()); | |
221 | |
222 page.OnAddLanguage("es"); | |
223 EXPECT_STREQ("English,Spanish", GetDisplayedLangs(page).c_str()); | |
224 EXPECT_STREQ("en,es", | |
225 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
226 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.add_button_)); | |
227 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
228 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
229 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
230 EXPECT_EQ(1, page.FirstSelectedRowNum()); | |
231 | |
232 // Duplicates should be ignored and selection should not be changed. | |
233 gtk_tree_selection_unselect_all(page.language_order_selection_); | |
234 page.OnAddLanguage("en"); | |
235 EXPECT_STREQ("English,Spanish", GetDisplayedLangs(page).c_str()); | |
236 EXPECT_STREQ("en,es", | |
237 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
238 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(page.add_button_)); | |
239 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_up_button_)); | |
240 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.move_down_button_)); | |
241 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(page.remove_button_)); | |
242 EXPECT_EQ(0, gtk_tree_selection_count_selected_rows( | |
243 page.language_order_selection_)); | |
244 } | |
245 | |
246 TEST_F(LanguagesPageGtkTest, EnableSpellChecking) { | |
247 profile_->GetPrefs()->SetBoolean(prefs::kEnableSpellCheck, false); | |
248 LanguagesPageGtk page(profile_.get()); | |
249 EXPECT_EQ(FALSE, gtk_toggle_button_get_active( | |
250 GTK_TOGGLE_BUTTON(page.enable_spellchecking_checkbox_))); | |
251 | |
252 profile_->GetPrefs()->SetBoolean(prefs::kEnableSpellCheck, true); | |
253 EXPECT_EQ(TRUE, gtk_toggle_button_get_active( | |
254 GTK_TOGGLE_BUTTON(page.enable_spellchecking_checkbox_))); | |
255 | |
256 gtk_button_clicked(GTK_BUTTON(page.enable_spellchecking_checkbox_)); | |
257 EXPECT_EQ(FALSE, gtk_toggle_button_get_active( | |
258 GTK_TOGGLE_BUTTON(page.enable_spellchecking_checkbox_))); | |
259 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck)); | |
260 | |
261 gtk_button_clicked(GTK_BUTTON(page.enable_spellchecking_checkbox_)); | |
262 EXPECT_EQ(TRUE, gtk_toggle_button_get_active( | |
263 GTK_TOGGLE_BUTTON(page.enable_spellchecking_checkbox_))); | |
264 EXPECT_EQ(true, profile_->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck)); | |
265 } | |
266 | |
267 // TODO(mattm): add EnableAutoSpellChecking test | |
268 | |
269 TEST_F(LanguagesPageGtkTest, DictionaryLanguage) { | |
270 profile_->GetPrefs()->SetString(prefs::kAcceptLanguages, "it"); | |
271 profile_->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "es"); | |
272 LanguagesPageGtk page(profile_.get()); | |
273 EXPECT_STREQ("Italian", GetDisplayedLangs(page).c_str()); | |
274 EXPECT_STREQ("it", | |
275 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
276 EXPECT_STREQ("Spanish", GetDisplayedSpellCheckerLang(page).c_str()); | |
277 int spanish_index = gtk_combo_box_get_active( | |
278 GTK_COMBO_BOX(page.dictionary_language_combobox_)); | |
279 | |
280 profile_->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "fr"); | |
281 EXPECT_STREQ("Italian", GetDisplayedLangs(page).c_str()); | |
282 EXPECT_STREQ("it", | |
283 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
284 EXPECT_STREQ("French", GetDisplayedSpellCheckerLang(page).c_str()); | |
285 int french_index = gtk_combo_box_get_active( | |
286 GTK_COMBO_BOX(page.dictionary_language_combobox_)); | |
287 | |
288 gtk_combo_box_set_active( | |
289 GTK_COMBO_BOX(page.dictionary_language_combobox_), spanish_index); | |
290 EXPECT_STREQ("Italian,Spanish", GetDisplayedLangs(page).c_str()); | |
291 EXPECT_STREQ("it,es", | |
292 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
293 EXPECT_STREQ("Spanish", GetDisplayedSpellCheckerLang(page).c_str()); | |
294 | |
295 gtk_combo_box_set_active( | |
296 GTK_COMBO_BOX(page.dictionary_language_combobox_), french_index); | |
297 EXPECT_STREQ("Italian,French", GetDisplayedLangs(page).c_str()); | |
298 EXPECT_STREQ("it,fr", | |
299 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
300 EXPECT_STREQ("French", GetDisplayedSpellCheckerLang(page).c_str()); | |
301 | |
302 gtk_combo_box_set_active( | |
303 GTK_COMBO_BOX(page.dictionary_language_combobox_), | |
304 page.dictionary_language_model_->GetIndexFromLocale("it")); | |
305 EXPECT_STREQ("Italian", GetDisplayedLangs(page).c_str()); | |
306 EXPECT_STREQ("it", | |
307 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages).c_str()); | |
308 EXPECT_STREQ("Italian", GetDisplayedSpellCheckerLang(page).c_str()); | |
309 } | |
OLD | NEW |