diff options
author | mattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-27 23:10:43 +0000 |
---|---|---|
committer | mattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-27 23:10:43 +0000 |
commit | 2f3d9d5e876c6a51c9c0e55ebf763a9f8833ae96 (patch) | |
tree | 20ab7d19b371c95402f9c0b8df2bfcc0554622fc | |
parent | 0922baeab1134628d5898a21dbbd8973647caae0 (diff) | |
download | chromium_src-2f3d9d5e876c6a51c9c0e55ebf763a9f8833ae96.zip chromium_src-2f3d9d5e876c6a51c9c0e55ebf763a9f8833ae96.tar.gz chromium_src-2f3d9d5e876c6a51c9c0e55ebf763a9f8833ae96.tar.bz2 |
Add unittest for Gtk search engine manager dialog.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/180012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@24692 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/gtk/keyword_editor_view.cc | 17 | ||||
-rw-r--r-- | chrome/browser/gtk/keyword_editor_view.h | 20 | ||||
-rw-r--r-- | chrome/browser/gtk/keyword_editor_view_unittest.cc | 261 | ||||
-rw-r--r-- | chrome/chrome.gyp | 1 | ||||
-rw-r--r-- | chrome/test/data/valgrind/unit_tests.gtest_linux.txt | 1 |
5 files changed, 285 insertions, 15 deletions
diff --git a/chrome/browser/gtk/keyword_editor_view.cc b/chrome/browser/gtk/keyword_editor_view.cc index 763ab32..f2dc7f5 100644 --- a/chrome/browser/gtk/keyword_editor_view.cc +++ b/chrome/browser/gtk/keyword_editor_view.cc @@ -28,18 +28,6 @@ const int kDialogDefaultHeight = 450; const int kFirstGroupRowOffset = 2; const int kSecondGroupRowOffset = 5; -// Column ids for |list_store_|. -enum { - COL_FAVICON, - COL_TITLE, - COL_KEYWORD, - COL_IS_HEADER, - COL_IS_SEPARATOR, - COL_WEIGHT, - COL_WEIGHT_SET, - COL_COUNT, -}; - KeywordEditorView* instance_ = NULL; } // namespace @@ -55,6 +43,7 @@ void KeywordEditorView::Show(Profile* profile) { gtk_window_present(GTK_WINDOW(instance_->dialog_)); } else { instance_ = new KeywordEditorView(profile); + gtk_widget_show_all(instance_->dialog_); } } @@ -196,8 +185,6 @@ void KeywordEditorView::Init() { EnableControls(); - gtk_widget_show_all(dialog_); - g_signal_connect(dialog_, "response", G_CALLBACK(OnResponse), this); g_signal_connect(dialog_, "destroy", G_CALLBACK(OnWindowDestroy), this); } @@ -411,7 +398,7 @@ gboolean KeywordEditorView::OnCheckRowIsSeparator(GtkTreeModel* model, return is_separator; } -//static +// static gboolean KeywordEditorView::OnSelectionFilter(GtkTreeSelection *selection, GtkTreeModel *model, GtkTreePath *path, diff --git a/chrome/browser/gtk/keyword_editor_view.h b/chrome/browser/gtk/keyword_editor_view.h index 0ee4fee..75a8db9 100644 --- a/chrome/browser/gtk/keyword_editor_view.h +++ b/chrome/browser/gtk/keyword_editor_view.h @@ -11,6 +11,7 @@ #include "base/basictypes.h" #include "chrome/browser/search_engines/edit_search_engine_controller.h" #include "chrome/browser/search_engines/template_url_model.h" +#include "testing/gtest/include/gtest/gtest_prod.h" class KeywordEditorController; class Profile; @@ -31,6 +32,18 @@ class KeywordEditorView : public TableModelObserver, const std::wstring& keyword, const std::wstring& url); private: + // Column ids for |list_store_|. + enum { + COL_FAVICON, + COL_TITLE, + COL_KEYWORD, + COL_IS_HEADER, + COL_IS_SEPARATOR, + COL_WEIGHT, + COL_WEIGHT_SET, + COL_COUNT, + }; + explicit KeywordEditorView(Profile* profile); void Init(); @@ -134,6 +147,13 @@ class KeywordEditorView : public TableModelObserver, // |list_store_|. int model_second_group_index_; + friend class KeywordEditorViewTest; + FRIEND_TEST(KeywordEditorViewTest, Empty); + FRIEND_TEST(KeywordEditorViewTest, Add); + FRIEND_TEST(KeywordEditorViewTest, MakeDefault); + FRIEND_TEST(KeywordEditorViewTest, Remove); + FRIEND_TEST(KeywordEditorViewTest, Edit); + DISALLOW_COPY_AND_ASSIGN(KeywordEditorView); }; diff --git a/chrome/browser/gtk/keyword_editor_view_unittest.cc b/chrome/browser/gtk/keyword_editor_view_unittest.cc new file mode 100644 index 0000000..aa15362 --- /dev/null +++ b/chrome/browser/gtk/keyword_editor_view_unittest.cc @@ -0,0 +1,261 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/gtk/keyword_editor_view.h" + +#include <gtk/gtk.h> + +#include <string> +#include <vector> + +#include "base/string_util.h" +#include "chrome/browser/search_engines/template_url.h" +#include "chrome/browser/search_engines/template_url_model.h" +#include "chrome/browser/search_engines/template_url_table_model.h" +#include "chrome/common/gtk_tree.h" +#include "chrome/test/testing_profile.h" +#include "testing/gtest/include/gtest/gtest.h" + +class KeywordEditorViewTest : public testing::Test { + public: + virtual void SetUp() { + profile_.reset(new TestingProfile()); + profile_->CreateTemplateURLModel(); + } + + TemplateURL* AddToModel(const std::string& name, + const std::string& keyword, + bool make_default) { + TemplateURL* template_url = new TemplateURL(); + template_url->set_short_name(UTF8ToWide(name)); + template_url->set_keyword(UTF8ToWide(keyword)); + template_url->SetURL(L"http://example.com/{searchTerms}", 0, 0); + profile_->GetTemplateURLModel()->Add(template_url); + if (make_default) + profile_->GetTemplateURLModel()->SetDefaultSearchProvider(template_url); + return template_url; + } + + int GetSelectedRowNum(const KeywordEditorView& editor) { + GtkTreeIter iter; + if (!gtk_tree_selection_get_selected(editor.selection_, NULL, &iter)) + return -1; + return gtk_tree::GetRowNumForIter(GTK_TREE_MODEL(editor.list_store_), + &iter); + } + + // Get the search engines displayed in the dialog in the order they are + // displayed, as a comma seperated string. + // The headers are included as "!,_" for the first group header and "_,@,_" + // for the second group header (This allows the tests to ensure the headers + // aren't accidentally misplaced/moved.) + // Ex: EXPECT_STREQ("!,_,A (Default),_,@,_,B", + // GetDisplayedEngines(editor).c_str()); + std::string GetDisplayedEngines(const KeywordEditorView& editor) { + TableModel::Groups groups(editor.table_model_->GetGroups()); + std::vector<std::string> parts; + GtkTreeModel* tree_model = GTK_TREE_MODEL(editor.list_store_); + GtkTreeIter iter; + if (!gtk_tree_model_get_iter_first(tree_model, &iter)) + return std::string(); + while (true) { + gchar* name; + gboolean is_header; + gtk_tree_model_get(tree_model, &iter, + KeywordEditorView::COL_TITLE, &name, + KeywordEditorView::COL_IS_HEADER, &is_header, + -1); + if (name && WideToUTF8(groups[0].title) == name) + parts.push_back("!"); + else if (name && WideToUTF8(groups[1].title) == name) + parts.push_back("@"); + else if (is_header) + parts.push_back("_"); + else if (name) + parts.push_back(name); + else + parts.push_back("???"); + if (name) + g_free(name); + if (!gtk_tree_model_iter_next(tree_model, &iter)) + break; + } + return JoinString(parts, ','); + } + + protected: + MessageLoopForUI message_loop_; + scoped_ptr<TestingProfile> profile_; +}; + +TEST_F(KeywordEditorViewTest, Empty) { + KeywordEditorView editor(profile_.get()); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.add_button_)); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.edit_button_)); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); + EXPECT_STREQ("!,_,_,@,_", GetDisplayedEngines(editor).c_str()); + EXPECT_EQ(-1, GetSelectedRowNum(editor)); +} + +TEST_F(KeywordEditorViewTest, Add) { + AddToModel("A1", "k1", true); + KeywordEditorView editor(profile_.get()); + EXPECT_STREQ("!,_,A1 (Default),_,@,_", GetDisplayedEngines(editor).c_str()); + EXPECT_EQ(-1, GetSelectedRowNum(editor)); + + editor.OnEditedKeyword(NULL, L"B", L"b", L"example.com"); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.add_button_)); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.edit_button_)); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); + EXPECT_STREQ("!,_,A1 (Default),_,@,_,B", GetDisplayedEngines(editor).c_str()); + EXPECT_EQ(6, GetSelectedRowNum(editor)); + + editor.OnEditedKeyword(NULL, L"C", L"c", L"example.com/{searchTerms}"); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.add_button_)); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.edit_button_)); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); + EXPECT_STREQ("!,_,A1 (Default),_,@,_,B,C", + GetDisplayedEngines(editor).c_str()); + EXPECT_EQ(7, GetSelectedRowNum(editor)); + + editor.OnEditedKeyword(NULL, L"D", L"d", L"example.com"); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.add_button_)); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.edit_button_)); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); + EXPECT_STREQ("!,_,A1 (Default),_,@,_,B,C,D", + GetDisplayedEngines(editor).c_str()); + EXPECT_EQ(8, GetSelectedRowNum(editor)); +} + +TEST_F(KeywordEditorViewTest, MakeDefault) { + AddToModel("A", "a", true); + AddToModel("B", "b", false); + AddToModel("C", "c", false); + AddToModel("D", "d", false); + KeywordEditorView editor(profile_.get()); + EXPECT_STREQ("!,_,A (Default),_,@,_,B,C,D", + GetDisplayedEngines(editor).c_str()); + + GtkTreeIter iter; + gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(editor.list_store_), + &iter, NULL, 6); + gtk_tree_selection_select_iter(editor.selection_, &iter); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); + + gtk_button_clicked(GTK_BUTTON(editor.make_default_button_)); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); + EXPECT_STREQ("!,_,A,B (Default),_,@,_,C,D", + GetDisplayedEngines(editor).c_str()); + EXPECT_EQ(3, GetSelectedRowNum(editor)); + + gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(editor.list_store_), + &iter, NULL, 8); + gtk_tree_selection_select_iter(editor.selection_, &iter); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); + + gtk_button_clicked(GTK_BUTTON(editor.make_default_button_)); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); + EXPECT_STREQ("!,_,A,B,D (Default),_,@,_,C", + GetDisplayedEngines(editor).c_str()); + EXPECT_EQ(4, GetSelectedRowNum(editor)); + + gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(editor.list_store_), + &iter, NULL, 2); + gtk_tree_selection_select_iter(editor.selection_, &iter); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); + + gtk_button_clicked(GTK_BUTTON(editor.make_default_button_)); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); + EXPECT_STREQ("!,_,A (Default),B,D,_,@,_,C", + GetDisplayedEngines(editor).c_str()); + EXPECT_EQ(2, GetSelectedRowNum(editor)); + + gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(editor.list_store_), + &iter, NULL, 4); + gtk_tree_selection_select_iter(editor.selection_, &iter); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); + + gtk_button_clicked(GTK_BUTTON(editor.make_default_button_)); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.make_default_button_)); + EXPECT_STREQ("!,_,A,B,D (Default),_,@,_,C", + GetDisplayedEngines(editor).c_str()); + EXPECT_EQ(4, GetSelectedRowNum(editor)); +} + +TEST_F(KeywordEditorViewTest, Remove) { + AddToModel("A", "a", true); + AddToModel("B", "b", true); + AddToModel("C", "c", false); + AddToModel("D", "d", false); + KeywordEditorView editor(profile_.get()); + EXPECT_STREQ("!,_,A,B (Default),_,@,_,C,D", + GetDisplayedEngines(editor).c_str()); + + GtkTreeIter iter; + gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(editor.list_store_), + &iter, NULL, 2); + gtk_tree_selection_select_iter(editor.selection_, &iter); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); + + gtk_button_clicked(GTK_BUTTON(editor.remove_button_)); + EXPECT_STREQ("!,_,B (Default),_,@,_,C,D", + GetDisplayedEngines(editor).c_str()); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); + EXPECT_EQ(2, GetSelectedRowNum(editor)); + + gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(editor.list_store_), + &iter, NULL, 6); + gtk_tree_selection_select_iter(editor.selection_, &iter); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); + + gtk_button_clicked(GTK_BUTTON(editor.remove_button_)); + EXPECT_STREQ("!,_,B (Default),_,@,_,D", + GetDisplayedEngines(editor).c_str()); + EXPECT_EQ(TRUE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); + EXPECT_EQ(6, GetSelectedRowNum(editor)); + + gtk_button_clicked(GTK_BUTTON(editor.remove_button_)); + EXPECT_STREQ("!,_,B (Default),_,@,_", + GetDisplayedEngines(editor).c_str()); + EXPECT_EQ(FALSE, GTK_WIDGET_SENSITIVE(editor.remove_button_)); + EXPECT_EQ(2, GetSelectedRowNum(editor)); +} + +TEST_F(KeywordEditorViewTest, Edit) { + TemplateURL* a = AddToModel("A", "a", true); + TemplateURL* b = AddToModel("B", "b", true); + TemplateURL* c = AddToModel("C", "c", false); + TemplateURL* d = AddToModel("D", "d", false); + KeywordEditorView editor(profile_.get()); + EXPECT_STREQ("!,_,A,B (Default),_,@,_,C,D", + GetDisplayedEngines(editor).c_str()); + + editor.OnEditedKeyword(a, L"AA", L"a", L"example.com/{searchTerms}"); + EXPECT_STREQ("!,_,AA,B (Default),_,@,_,C,D", + GetDisplayedEngines(editor).c_str()); + + editor.OnEditedKeyword(b, L"BB", L"b", L"foo.example.com/{searchTerms}"); + EXPECT_STREQ("!,_,AA,BB (Default),_,@,_,C,D", + GetDisplayedEngines(editor).c_str()); + + editor.OnEditedKeyword(b, L"BBB", L"b", L"example.com"); + EXPECT_STREQ("!,_,AA,BBB,_,@,_,C,D", + GetDisplayedEngines(editor).c_str()); + + editor.OnEditedKeyword(d, L"DD", L"d", L"example.com"); + EXPECT_STREQ("!,_,AA,BBB,_,@,_,C,DD", + GetDisplayedEngines(editor).c_str()); + + editor.OnEditedKeyword(c, L"CC", L"cc", L"example.com"); + EXPECT_STREQ("!,_,AA,BBB,_,@,_,CC,DD", + GetDisplayedEngines(editor).c_str()); +} diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index 3dacf52..502c334 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -3990,6 +3990,7 @@ 'browser/google_update_settings_posix_unittest.cc', 'browser/gtk/bookmark_editor_gtk_unittest.cc', 'browser/gtk/go_button_gtk_unittest.cc', + 'browser/gtk/keyword_editor_view_unittest.cc', 'browser/gtk/options/cookies_view_unittest.cc', 'browser/gtk/options/languages_page_gtk_unittest.cc', 'browser/gtk/tabs/tab_renderer_gtk_unittest.cc', diff --git a/chrome/test/data/valgrind/unit_tests.gtest_linux.txt b/chrome/test/data/valgrind/unit_tests.gtest_linux.txt index f52ecf8..39360ae 100644 --- a/chrome/test/data/valgrind/unit_tests.gtest_linux.txt +++ b/chrome/test/data/valgrind/unit_tests.gtest_linux.txt @@ -3,3 +3,4 @@ BookmarkEditorGtkTest.* CookiesViewTest.* LanguagesPageGtkTest.* +KeywordEditorViewTest.* |