diff options
author | sky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-13 21:51:45 +0000 |
---|---|---|
committer | sky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-13 21:51:45 +0000 |
commit | d2c017a6aad8fac222c9e7b891b7ffab51cb396e (patch) | |
tree | 7efcc41416d158e9a4abd3e6eeb4b0baf9f517c5 /chrome | |
parent | 2364d40fd0483bb7250e36db041fe31ab4472e66 (diff) | |
download | chromium_src-d2c017a6aad8fac222c9e7b891b7ffab51cb396e.zip chromium_src-d2c017a6aad8fac222c9e7b891b7ffab51cb396e.tar.gz chromium_src-d2c017a6aad8fac222c9e7b891b7ffab51cb396e.tar.bz2 |
Tests for KeywordEditorView.
BUG=1324185
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@828 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/views/keyword_editor_view.cc | 15 | ||||
-rw-r--r-- | chrome/browser/views/keyword_editor_view.h | 18 | ||||
-rw-r--r-- | chrome/browser/views/keyword_editor_view_unittest.cc | 168 | ||||
-rw-r--r-- | chrome/test/testing_profile.cc | 4 | ||||
-rw-r--r-- | chrome/test/testing_profile.h | 9 | ||||
-rw-r--r-- | chrome/test/unit/unittests.vcproj | 8 |
6 files changed, 211 insertions, 11 deletions
diff --git a/chrome/browser/views/keyword_editor_view.cc b/chrome/browser/views/keyword_editor_view.cc index 799d2e2..f685cae 100644 --- a/chrome/browser/views/keyword_editor_view.cc +++ b/chrome/browser/views/keyword_editor_view.cc @@ -668,8 +668,15 @@ void KeywordEditorView::OnTemplateURLModelChanged() { } void KeywordEditorView::MakeDefaultSearchProvider() { - int selected_index = table_view_->FirstSelectedRow(); - const TemplateURL* keyword = &table_model_->GetTemplateURL(selected_index); + MakeDefaultSearchProvider(table_view_->FirstSelectedRow()); +} + +void KeywordEditorView::MakeDefaultSearchProvider(int index) { + if (index < 0 || index >= table_model_->RowCount()) { + NOTREACHED(); + return; + } + const TemplateURL* keyword = &table_model_->GetTemplateURL(index); const TemplateURL* current_default = url_model_->GetDefaultSearchProvider(); if (current_default == keyword) return; @@ -681,7 +688,7 @@ void KeywordEditorView::MakeDefaultSearchProvider() { // Enable the Suggest checkbox only if this engine has Suggest capability. enable_suggest_checkbox_->SetEnabled(keyword->suggestions_url() != NULL); - // The formatting of the default engine is different; notify the table that + // The formatting of the default engine is different; notify the table that // both old and new entries have changed. if (current_default != NULL) { table_model_->NotifyChanged(table_model_->IndexOfTemplateURL( @@ -691,7 +698,7 @@ void KeywordEditorView::MakeDefaultSearchProvider() { table_model_->NotifyChanged(new_index); // Make sure the new default is in the main group. - table_model_->MoveToMainGroup(selected_index); + table_model_->MoveToMainGroup(index); // And select it. table_view_->Select(new_index); diff --git a/chrome/browser/views/keyword_editor_view.h b/chrome/browser/views/keyword_editor_view.h index 4d9fa3e..b0ce22a 100644 --- a/chrome/browser/views/keyword_editor_view.h +++ b/chrome/browser/views/keyword_editor_view.h @@ -27,8 +27,8 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#ifndef CHROME_BROWSER_VIEWS_KEYWORD_EDITOR_VIEW_H__ -#define CHROME_BROWSER_VIEWS_KEYWORD_EDITOR_VIEW_H__ +#ifndef CHROME_BROWSER_VIEWS_KEYWORD_EDITOR_VIEW_H_ +#define CHROME_BROWSER_VIEWS_KEYWORD_EDITOR_VIEW_H_ #include <Windows.h> #include <map> @@ -145,6 +145,8 @@ class KeywordEditorView : public ChromeViews::View, public ChromeViews::NativeButton::Listener, public TemplateURLModelObserver, public ChromeViews::DialogDelegate { + friend class KeywordEditorViewTest; + FRIEND_TEST(KeywordEditorViewTest, MakeDefault); public: static void RegisterUserPrefs(PrefService* prefs); @@ -161,8 +163,8 @@ class KeywordEditorView : public ChromeViews::View, const std::wstring& keyword, const std::wstring& url); - // Invoked when the user modifies a TemplateURL. Update the TemplateURLModel - // and table model appropriatley. + // Invoked when the user modifies a TemplateURL. Updates the TemplateURLModel + // and table model appropriately. void ModifyTemplateURL(const TemplateURL* template_url, const std::wstring& title, const std::wstring& keyword, @@ -203,6 +205,10 @@ class KeywordEditorView : public ChromeViews::View, // Toggles whether the selected keyword is the default search provider. void MakeDefaultSearchProvider(); + // Make the TemplateURL at the specified index (into the TableModel) the + // default search provider. + void MakeDefaultSearchProvider(int index); + // The profile. Profile* profile_; @@ -222,7 +228,7 @@ class KeywordEditorView : public ChromeViews::View, ChromeViews::NativeButton* make_default_button_; ChromeViews::CheckBox* enable_suggest_checkbox_; - DISALLOW_EVIL_CONSTRUCTORS(KeywordEditorView); + DISALLOW_COPY_AND_ASSIGN(KeywordEditorView); }; -#endif // CHROME_BROWSER_VIEWS_KEYWORD_EDITOR_VIEW_H__ +#endif // CHROME_BROWSER_VIEWS_KEYWORD_EDITOR_VIEW_H_ diff --git a/chrome/browser/views/keyword_editor_view_unittest.cc b/chrome/browser/views/keyword_editor_view_unittest.cc new file mode 100644 index 0000000..78962b6 --- /dev/null +++ b/chrome/browser/views/keyword_editor_view_unittest.cc @@ -0,0 +1,168 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "chrome/browser/profile.h" +#include "chrome/browser/template_url.h" +#include "chrome/browser/template_url_model.h" +#include "chrome/browser/views/keyword_editor_view.h" +#include "chrome/test/testing_profile.h" +#include "testing/gtest/include/gtest/gtest.h" + +// Base class for keyword editor tests. Creates a profile containing an +// empty TemplateURLModel. +class KeywordEditorViewTest : public testing::Test, + public ChromeViews::TableModelObserver { + public: + virtual void SetUp() { + model_changed_count_ = items_changed_count_ = added_count_ = + removed_count_ = 0; + + profile_.reset(new TestingProfile()); + profile_->CreateTemplateURLModel(); + + model_ = profile_->GetTemplateURLModel(); + + editor_.reset(new KeywordEditorView(profile_.get())); + editor_->table_model_->SetObserver(this); + } + + virtual void OnModelChanged() { + model_changed_count_++; + } + + virtual void OnItemsChanged(int start, int length) { + items_changed_count_++; + } + + virtual void OnItemsAdded(int start, int length) { + added_count_++; + } + + virtual void OnItemsRemoved(int start, int length) { + removed_count_++; + } + + void VerifyChangeCount(int model_changed_count, int item_changed_count, + int added_count, int removed_count) { + ASSERT_EQ(model_changed_count, model_changed_count_); + ASSERT_EQ(item_changed_count, items_changed_count_); + ASSERT_EQ(added_count, added_count_); + ASSERT_EQ(removed_count, removed_count_); + ClearChangeCount(); + } + + void ClearChangeCount() { + model_changed_count_ = items_changed_count_ = added_count_ = + removed_count_ = 0; + } + + TemplateURLTableModel* table_model() const { + return editor_->table_model_.get(); + } + + protected: + scoped_ptr<TestingProfile> profile_; + scoped_ptr<KeywordEditorView> editor_; + TemplateURLModel* model_; + + int model_changed_count_; + int items_changed_count_; + int added_count_; + int removed_count_; +}; + +// Tests adding a TemplateURL. +TEST_F(KeywordEditorViewTest, Add) { + editor_->AddTemplateURL(L"a", L"b", L"http://c"); + + // Verify the observer was notified. + VerifyChangeCount(0, 0, 1, 0); + if (HasFatalFailure()) + return; + + // Verify the TableModel has the new data. + ASSERT_EQ(1, table_model()->RowCount()); + + // Verify the TemplateURLModel has the new entry. + ASSERT_EQ(1, model_->GetTemplateURLs().size()); + + // Verify the entry is what we added. + const TemplateURL* turl = model_->GetTemplateURLs()[0]; + EXPECT_EQ(L"a", turl->short_name()); + EXPECT_EQ(L"b", turl->keyword()); + EXPECT_TRUE(turl->url() != NULL); + EXPECT_TRUE(turl->url()->url() == L"http://c"); +} + +// Tests modifying a TemplateURL. +TEST_F(KeywordEditorViewTest, Modify) { + editor_->AddTemplateURL(L"a", L"b", L"http://c"); + ClearChangeCount(); + + // Modify the entry. + const TemplateURL* turl = model_->GetTemplateURLs()[0]; + editor_->ModifyTemplateURL(turl, L"a1", L"b1", L"http://c1"); + + // Make sure it was updated appropriately. + VerifyChangeCount(0, 1, 0, 0); + EXPECT_EQ(L"a1", turl->short_name()); + EXPECT_EQ(L"b1", turl->keyword()); + EXPECT_TRUE(turl->url() != NULL); + EXPECT_TRUE(turl->url()->url() == L"http://c1"); +} + +// Tests making a TemplateURL the default search provider. +TEST_F(KeywordEditorViewTest, MakeDefault) { + editor_->AddTemplateURL(L"a", L"b", L"http://c{searchTerms}"); + ClearChangeCount(); + + const TemplateURL* turl = model_->GetTemplateURLs()[0]; + editor_->MakeDefaultSearchProvider(0); + // Making an item the default sends a handful of changes. Which are sent isn't + // important, what is important is 'something' is sent. + ASSERT_TRUE(items_changed_count_ > 0 || added_count_ > 0 || + removed_count_ > 0); + ASSERT_TRUE(model_->GetDefaultSearchProvider() == turl); +} + +// Mutates the TemplateURLModel and make sure table model is updating +// appropriately. +TEST_F(KeywordEditorViewTest, MutateTemplateURLModel) { + TemplateURL* turl = new TemplateURL(); + turl->set_keyword(L"a"); + turl->set_short_name(L"b"); + model_->Add(turl); + + // Table model should have updated. + VerifyChangeCount(1, 0, 0, 0); + + // And should contain the newly added TemplateURL. + ASSERT_EQ(1, table_model()->RowCount()); + ASSERT_EQ(0, table_model()->IndexOfTemplateURL(turl)); +} diff --git a/chrome/test/testing_profile.cc b/chrome/test/testing_profile.cc index c1c3fe8..d18a75d 100644 --- a/chrome/test/testing_profile.cc +++ b/chrome/test/testing_profile.cc @@ -71,3 +71,7 @@ void TestingProfile::DestroyHistoryService() { void TestingProfile::CreateBookmarkBarModel() { bookmark_bar_model_.reset(new BookmarkBarModel(this)); } + +void TestingProfile::CreateTemplateURLModel() { + template_url_model_.reset(new TemplateURLModel(this)); +} diff --git a/chrome/test/testing_profile.h b/chrome/test/testing_profile.h index f6b01b5..05df5e5 100644 --- a/chrome/test/testing_profile.h +++ b/chrome/test/testing_profile.h @@ -37,6 +37,7 @@ #include "chrome/browser/browser_prefs.h" #include "chrome/browser/history/history.h" #include "chrome/browser/profile.h" +#include "chrome/browser/template_url_model.h" #include "chrome/common/pref_service.h" class TestingProfile : public Profile { @@ -51,6 +52,9 @@ class TestingProfile : public Profile { // NULL. void CreateBookmarkBarModel(); + // Creates a TemplateURLModel. If not invoked the TemplateURLModel is NULL. + void CreateTemplateURLModel(); + virtual std::wstring GetPath() { return std::wstring(); } @@ -90,7 +94,7 @@ class TestingProfile : public Profile { return prefs_.get(); } virtual TemplateURLModel* GetTemplateURLModel() { - return NULL; + return template_url_model_.get(); } virtual TemplateURLFetcher* GetTemplateURLFetcher() { return NULL; @@ -183,6 +187,9 @@ class TestingProfile : public Profile { // The BookmarkBarModel. Only created if CreateBookmarkBarModel is invoked. scoped_ptr<BookmarkBarModel> bookmark_bar_model_; + // The TemplateURLFetcher. Only created if CreateTemplateURLModel is invoked. + scoped_ptr<TemplateURLModel> template_url_model_; + // Do we have a history service? This defaults to the value of // history_service, but can be explicitly set. bool has_history_service_; diff --git a/chrome/test/unit/unittests.vcproj b/chrome/test/unit/unittests.vcproj index 095d594..9aa1c90 100644 --- a/chrome/test/unit/unittests.vcproj +++ b/chrome/test/unit/unittests.vcproj @@ -427,6 +427,14 @@ </File> </Filter> <Filter + Name="TestKeywordEditorView" + > + <File + RelativePath="..\..\browser\views\keyword_editor_view_unittest.cc" + > + </File> + </Filter> + <Filter Name="TestJPEGCodec" > <File |