| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 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/keyword_editor_view.h" | |
| 6 | |
| 7 #include <gtk/gtk.h> | |
| 8 | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/string16.h" | |
| 13 #include "base/string_util.h" | |
| 14 #include "base/utf_string_conversions.h" | |
| 15 #include "chrome/browser/gtk/gtk_tree.h" | |
| 16 #include "chrome/browser/search_engines/template_url.h" | |
| 17 #include "chrome/browser/search_engines/template_url_model.h" | |
| 18 #include "chrome/browser/search_engines/template_url_table_model.h" | |
| 19 #include "chrome/test/testing_profile.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 class KeywordEditorViewTest : public testing::Test { | |
| 23 public: | |
| 24 virtual void SetUp() { | |
| 25 profile_.reset(new TestingProfile()); | |
| 26 profile_->CreateTemplateURLModel(); | |
| 27 } | |
| 28 | |
| 29 TemplateURL* AddToModel(const std::string& name, | |
| 30 const std::string& keyword, | |
| 31 bool make_default) { | |
| 32 TemplateURL* template_url = new TemplateURL(); | |
| 33 template_url->set_short_name(UTF8ToWide(name)); | |
| 34 template_url->set_keyword(UTF8ToWide(keyword)); | |
| 35 template_url->SetURL("http://example.com/{searchTerms}", 0, 0); | |
| 36 profile_->GetTemplateURLModel()->Add(template_url); | |
| 37 if (make_default) | |
| 38 profile_->GetTemplateURLModel()->SetDefaultSearchProvider(template_url); | |
| 39 return template_url; | |
| 40 } | |
| 41 | |
| 42 int GetSelectedRowNum(const KeywordEditorView& editor) { | |
| 43 GtkTreeIter iter; | |
| 44 if (!gtk_tree_selection_get_selected(editor.selection_, NULL, &iter)) | |
| 45 return -1; | |
| 46 return gtk_tree::GetRowNumForIter(GTK_TREE_MODEL(editor.list_store_), | |
| 47 &iter); | |
| 48 } | |
| 49 | |
| 50 // Get the search engines displayed in the dialog in the order they are | |
| 51 // displayed, as a comma seperated string. | |
| 52 // The headers are included as "!,_" for the first group header and "_,@,_" | |
| 53 // for the second group header (This allows the tests to ensure the headers | |
| 54 // aren't accidentally misplaced/moved.) | |
| 55 // Ex: EXPECT_STREQ("!,_,A (Default),_,@,_,B", | |
| 56 // GetDisplayedEngines(editor).c_str()); | |
| 57 std::string GetDisplayedEngines(const KeywordEditorView& editor) { | |
| 58 TableModel::Groups groups(editor.table_model_->GetGroups()); | |
| 59 std::vector<std::string> parts; | |
| 60 GtkTreeModel* tree_model = GTK_TREE_MODEL(editor.list_store_); | |
| 61 GtkTreeIter iter; | |
| 62 if (!gtk_tree_model_get_iter_first(tree_model, &iter)) | |
| 63 return std::string(); | |
| 64 while (true) { | |
| 65 gchar* name; | |
| 66 gboolean is_header; | |
| 67 gtk_tree_model_get(tree_model, &iter, | |
| 68 KeywordEditorView::COL_TITLE, &name, | |
| 69 KeywordEditorView::COL_IS_HEADER, &is_header, | |
| 70 -1); | |
| 71 if (name && UTF16ToUTF8(groups[0].title) == name) | |
| 72 parts.push_back("!"); | |
| 73 else if (name && UTF16ToUTF8(groups[1].title) == name) | |
| 74 parts.push_back("@"); | |
| 75 else if (is_header) | |
| 76 parts.push_back("_"); | |
| 77 else if (name) | |
| 78 parts.push_back(name); | |
| 79 else | |
| 80 parts.push_back("???"); | |
| 81 if (name) | |
| 82 g_free(name); | |
| 83 if (!gtk_tree_model_iter_next(tree_model, &iter)) | |
| 84 break; | |
| 85 } | |
| 86 return JoinString(parts, ','); | |
| 87 } | |
| 88 | |
| 89 protected: | |
| 90 MessageLoopForUI message_loop_; | |
| 91 scoped_ptr<TestingProfile> profile_; | |
| 92 }; | |
| 93 | |
| 94 TEST_F(KeywordEditorViewTest, Empty) { | |
| 95 KeywordEditorView editor(profile_.get()); | |
| 96 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.add_button_)); | |
| 97 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.edit_button_)); | |
| 98 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); | |
| 99 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); | |
| 100 EXPECT_STREQ("!,_,_,@,_", GetDisplayedEngines(editor).c_str()); | |
| 101 EXPECT_EQ(-1, GetSelectedRowNum(editor)); | |
| 102 } | |
| 103 | |
| 104 TEST_F(KeywordEditorViewTest, Add) { | |
| 105 AddToModel("A1", "k1", true); | |
| 106 KeywordEditorView editor(profile_.get()); | |
| 107 EXPECT_STREQ("!,_,A1 (Default),_,@,_", GetDisplayedEngines(editor).c_str()); | |
| 108 EXPECT_EQ(-1, GetSelectedRowNum(editor)); | |
| 109 | |
| 110 editor.OnEditedKeyword(NULL, ASCIIToUTF16("B"), ASCIIToUTF16("b"), | |
| 111 "example.com"); | |
| 112 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.add_button_)); | |
| 113 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.edit_button_)); | |
| 114 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); | |
| 115 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); | |
| 116 EXPECT_STREQ("!,_,A1 (Default),_,@,_,B", GetDisplayedEngines(editor).c_str()); | |
| 117 EXPECT_EQ(6, GetSelectedRowNum(editor)); | |
| 118 | |
| 119 editor.OnEditedKeyword(NULL, ASCIIToUTF16("C"), ASCIIToUTF16("c"), | |
| 120 "example.com/{searchTerms}"); | |
| 121 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.add_button_)); | |
| 122 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.edit_button_)); | |
| 123 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); | |
| 124 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); | |
| 125 EXPECT_STREQ("!,_,A1 (Default),_,@,_,B,C", | |
| 126 GetDisplayedEngines(editor).c_str()); | |
| 127 EXPECT_EQ(7, GetSelectedRowNum(editor)); | |
| 128 | |
| 129 editor.OnEditedKeyword(NULL, ASCIIToUTF16("D"), ASCIIToUTF16("d"), | |
| 130 "example.com"); | |
| 131 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.add_button_)); | |
| 132 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.edit_button_)); | |
| 133 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); | |
| 134 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); | |
| 135 EXPECT_STREQ("!,_,A1 (Default),_,@,_,B,C,D", | |
| 136 GetDisplayedEngines(editor).c_str()); | |
| 137 EXPECT_EQ(8, GetSelectedRowNum(editor)); | |
| 138 } | |
| 139 | |
| 140 TEST_F(KeywordEditorViewTest, MakeDefault) { | |
| 141 AddToModel("A", "a", true); | |
| 142 AddToModel("B", "b", false); | |
| 143 AddToModel("C", "c", false); | |
| 144 AddToModel("D", "d", false); | |
| 145 KeywordEditorView editor(profile_.get()); | |
| 146 EXPECT_STREQ("!,_,A (Default),_,@,_,B,C,D", | |
| 147 GetDisplayedEngines(editor).c_str()); | |
| 148 | |
| 149 GtkTreeIter iter; | |
| 150 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(editor.list_store_), | |
| 151 &iter, NULL, 6); | |
| 152 gtk_tree_selection_select_iter(editor.selection_, &iter); | |
| 153 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); | |
| 154 | |
| 155 gtk_button_clicked(GTK_BUTTON(editor.make_default_button_)); | |
| 156 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); | |
| 157 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); | |
| 158 EXPECT_STREQ("!,_,A,B (Default),_,@,_,C,D", | |
| 159 GetDisplayedEngines(editor).c_str()); | |
| 160 EXPECT_EQ(3, GetSelectedRowNum(editor)); | |
| 161 | |
| 162 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(editor.list_store_), | |
| 163 &iter, NULL, 8); | |
| 164 gtk_tree_selection_select_iter(editor.selection_, &iter); | |
| 165 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); | |
| 166 | |
| 167 gtk_button_clicked(GTK_BUTTON(editor.make_default_button_)); | |
| 168 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); | |
| 169 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); | |
| 170 EXPECT_STREQ("!,_,A,B,D (Default),_,@,_,C", | |
| 171 GetDisplayedEngines(editor).c_str()); | |
| 172 EXPECT_EQ(4, GetSelectedRowNum(editor)); | |
| 173 | |
| 174 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(editor.list_store_), | |
| 175 &iter, NULL, 2); | |
| 176 gtk_tree_selection_select_iter(editor.selection_, &iter); | |
| 177 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); | |
| 178 | |
| 179 gtk_button_clicked(GTK_BUTTON(editor.make_default_button_)); | |
| 180 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); | |
| 181 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); | |
| 182 EXPECT_STREQ("!,_,A (Default),B,D,_,@,_,C", | |
| 183 GetDisplayedEngines(editor).c_str()); | |
| 184 EXPECT_EQ(2, GetSelectedRowNum(editor)); | |
| 185 | |
| 186 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(editor.list_store_), | |
| 187 &iter, NULL, 4); | |
| 188 gtk_tree_selection_select_iter(editor.selection_, &iter); | |
| 189 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); | |
| 190 | |
| 191 gtk_button_clicked(GTK_BUTTON(editor.make_default_button_)); | |
| 192 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); | |
| 193 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); | |
| 194 EXPECT_STREQ("!,_,A,B,D (Default),_,@,_,C", | |
| 195 GetDisplayedEngines(editor).c_str()); | |
| 196 EXPECT_EQ(4, GetSelectedRowNum(editor)); | |
| 197 } | |
| 198 | |
| 199 TEST_F(KeywordEditorViewTest, Remove) { | |
| 200 AddToModel("A", "a", true); | |
| 201 AddToModel("B", "b", true); | |
| 202 AddToModel("C", "c", false); | |
| 203 AddToModel("D", "d", false); | |
| 204 KeywordEditorView editor(profile_.get()); | |
| 205 EXPECT_STREQ("!,_,A,B (Default),_,@,_,C,D", | |
| 206 GetDisplayedEngines(editor).c_str()); | |
| 207 | |
| 208 GtkTreeIter iter; | |
| 209 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(editor.list_store_), | |
| 210 &iter, NULL, 2); | |
| 211 gtk_tree_selection_select_iter(editor.selection_, &iter); | |
| 212 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); | |
| 213 | |
| 214 gtk_button_clicked(GTK_BUTTON(editor.remove_button_)); | |
| 215 EXPECT_STREQ("!,_,B (Default),_,@,_,C,D", | |
| 216 GetDisplayedEngines(editor).c_str()); | |
| 217 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); | |
| 218 EXPECT_EQ(2, GetSelectedRowNum(editor)); | |
| 219 | |
| 220 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(editor.list_store_), | |
| 221 &iter, NULL, 6); | |
| 222 gtk_tree_selection_select_iter(editor.selection_, &iter); | |
| 223 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); | |
| 224 | |
| 225 gtk_button_clicked(GTK_BUTTON(editor.remove_button_)); | |
| 226 EXPECT_STREQ("!,_,B (Default),_,@,_,D", | |
| 227 GetDisplayedEngines(editor).c_str()); | |
| 228 EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); | |
| 229 EXPECT_EQ(6, GetSelectedRowNum(editor)); | |
| 230 | |
| 231 gtk_button_clicked(GTK_BUTTON(editor.remove_button_)); | |
| 232 EXPECT_STREQ("!,_,B (Default),_,@,_", | |
| 233 GetDisplayedEngines(editor).c_str()); | |
| 234 EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); | |
| 235 EXPECT_EQ(2, GetSelectedRowNum(editor)); | |
| 236 } | |
| 237 | |
| 238 TEST_F(KeywordEditorViewTest, Edit) { | |
| 239 TemplateURL* a = AddToModel("A", "a", true); | |
| 240 TemplateURL* b = AddToModel("B", "b", true); | |
| 241 TemplateURL* c = AddToModel("C", "c", false); | |
| 242 TemplateURL* d = AddToModel("D", "d", false); | |
| 243 KeywordEditorView editor(profile_.get()); | |
| 244 EXPECT_STREQ("!,_,A,B (Default),_,@,_,C,D", | |
| 245 GetDisplayedEngines(editor).c_str()); | |
| 246 | |
| 247 editor.OnEditedKeyword(a, ASCIIToUTF16("AA"), ASCIIToUTF16("a"), | |
| 248 "example.com/{searchTerms}"); | |
| 249 EXPECT_STREQ("!,_,AA,B (Default),_,@,_,C,D", | |
| 250 GetDisplayedEngines(editor).c_str()); | |
| 251 | |
| 252 editor.OnEditedKeyword(b, ASCIIToUTF16("BB"), ASCIIToUTF16("b"), | |
| 253 "foo.example.com/{searchTerms}"); | |
| 254 EXPECT_STREQ("!,_,AA,BB (Default),_,@,_,C,D", | |
| 255 GetDisplayedEngines(editor).c_str()); | |
| 256 | |
| 257 editor.OnEditedKeyword(b, ASCIIToUTF16("BBB"), ASCIIToUTF16("b"), | |
| 258 "example.com"); | |
| 259 EXPECT_STREQ("!,_,AA,BBB,_,@,_,C,D", | |
| 260 GetDisplayedEngines(editor).c_str()); | |
| 261 | |
| 262 editor.OnEditedKeyword(d, ASCIIToUTF16("DD"), ASCIIToUTF16("d"), | |
| 263 "example.com"); | |
| 264 EXPECT_STREQ("!,_,AA,BBB,_,@,_,C,DD", | |
| 265 GetDisplayedEngines(editor).c_str()); | |
| 266 | |
| 267 editor.OnEditedKeyword(c, ASCIIToUTF16("CC"), ASCIIToUTF16("cc"), | |
| 268 "example.com"); | |
| 269 EXPECT_STREQ("!,_,AA,BBB,_,@,_,CC,DD", | |
| 270 GetDisplayedEngines(editor).c_str()); | |
| 271 } | |
| OLD | NEW |