diff options
author | ivankr@chromium.org <ivankr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-18 09:36:22 +0000 |
---|---|---|
committer | ivankr@chromium.org <ivankr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-18 09:36:22 +0000 |
commit | 63b59a811e7eb68625da7c94085e85c3b19f6fd0 (patch) | |
tree | 4a65027dc6f1d4e176b74ec534df46c061aa65e3 /chrome/browser/protector | |
parent | d399866c2fe77410afed5be486f2b72581823bed (diff) | |
download | chromium_src-63b59a811e7eb68625da7c94085e85c3b19f6fd0.zip chromium_src-63b59a811e7eb68625da7c94085e85c3b19f6fd0.tar.gz chromium_src-63b59a811e7eb68625da7c94085e85c3b19f6fd0.tar.bz2 |
Initial set of browser_tests for Protector and DefaultSearchProviderChange.
Some slight changes is Protector/BaseSettingChange: removed the OnBeforeRemoved callback (without calling any callback before the removal).
BUG=None
TEST=browser_tests:ProtectorTest.*,browser_tests:DefaultSearchProviderChange.*
Review URL: https://chromiumcodereview.appspot.com/9236001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@118083 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/protector')
-rw-r--r-- | chrome/browser/protector/base_setting_change.cc | 11 | ||||
-rw-r--r-- | chrome/browser/protector/base_setting_change.h | 10 | ||||
-rw-r--r-- | chrome/browser/protector/default_search_provider_change.cc | 21 | ||||
-rw-r--r-- | chrome/browser/protector/default_search_provider_change_browsertest.cc | 462 | ||||
-rw-r--r-- | chrome/browser/protector/mock_protector.cc | 17 | ||||
-rw-r--r-- | chrome/browser/protector/mock_protector.h | 31 | ||||
-rw-r--r-- | chrome/browser/protector/mock_setting_change.cc | 39 | ||||
-rw-r--r-- | chrome/browser/protector/mock_setting_change.h | 39 | ||||
-rw-r--r-- | chrome/browser/protector/protector.cc | 2 | ||||
-rw-r--r-- | chrome/browser/protector/protector.h | 14 | ||||
-rw-r--r-- | chrome/browser/protector/protector_browsertest.cc | 114 |
11 files changed, 727 insertions, 33 deletions
diff --git a/chrome/browser/protector/base_setting_change.cc b/chrome/browser/protector/base_setting_change.cc index acd04ac..a622db0 100644 --- a/chrome/browser/protector/base_setting_change.cc +++ b/chrome/browser/protector/base_setting_change.cc @@ -1,15 +1,17 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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/protector/base_setting_change.h" #include "base/logging.h" +#include "chrome/browser/protector/protector.h" namespace protector { BaseSettingChange::BaseSettingChange() - : protector_(NULL) { + : profile_(NULL), + protector_(NULL) { } BaseSettingChange::~BaseSettingChange() { @@ -18,6 +20,8 @@ BaseSettingChange::~BaseSettingChange() { bool BaseSettingChange::Init(Protector* protector) { DCHECK(protector); protector_ = protector; + profile_ = protector->profile(); + DCHECK(profile_); return true; } @@ -30,7 +34,4 @@ void BaseSettingChange::Discard() { void BaseSettingChange::Timeout() { } -void BaseSettingChange::OnBeforeRemoved() { -} - } // namespace protector diff --git a/chrome/browser/protector/base_setting_change.h b/chrome/browser/protector/base_setting_change.h index 9a710c4..14d83bd 100644 --- a/chrome/browser/protector/base_setting_change.h +++ b/chrome/browser/protector/base_setting_change.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -12,6 +12,7 @@ #include "base/basictypes.h" #include "base/string16.h" +class Profile; class TemplateURL; namespace protector { @@ -40,9 +41,6 @@ class BaseSettingChange { // Indicates that user has ignored this change and timeout has passed. virtual void Timeout(); - // Called before the change is removed from the protector instance. - virtual void OnBeforeRemoved() = 0; - // Returns the resource ID of the badge icon. virtual int GetBadgeIconID() const = 0; @@ -65,10 +63,14 @@ class BaseSettingChange { // Returns text for the button to discard the change with |Discard|. virtual string16 GetDiscardButtonText() const = 0; + // Profile instance we've been associated with by an |Init| call. + Profile* profile() { return profile_; } + // Protector instance we've been associated with by an |Init| call. Protector* protector() { return protector_; } private: + Profile* profile_; Protector* protector_; DISALLOW_COPY_AND_ASSIGN(BaseSettingChange); diff --git a/chrome/browser/protector/default_search_provider_change.cc b/chrome/browser/protector/default_search_provider_change.cc index 047e9b0..1ce6346 100644 --- a/chrome/browser/protector/default_search_provider_change.cc +++ b/chrome/browser/protector/default_search_provider_change.cc @@ -91,7 +91,6 @@ class DefaultSearchProviderChange : public BaseSettingChange, virtual void Apply() OVERRIDE; virtual void Discard() OVERRIDE; virtual void Timeout() OVERRIDE; - virtual void OnBeforeRemoved() OVERRIDE; virtual int GetBadgeIconID() const OVERRIDE; virtual int GetMenuItemIconID() const OVERRIDE; virtual int GetBubbleIconID() const OVERRIDE; @@ -127,9 +126,6 @@ class DefaultSearchProviderChange : public BaseSettingChange, // related to. TemplateURLService* GetTemplateURLService(); - // Stops observing the TemplateURLService changes. - void StopObservingTemplateURLService(); - // Histogram ID of the new search provider. int new_histogram_id_; // Indicates that the default search was restored to the prepopulated default @@ -169,6 +165,7 @@ DefaultSearchProviderChange::DefaultSearchProviderChange( } DefaultSearchProviderChange::~DefaultSearchProviderChange() { + GetTemplateURLService()->RemoveObserver(this); } bool DefaultSearchProviderChange::Init(Protector* protector) { @@ -225,7 +222,7 @@ bool DefaultSearchProviderChange::Init(Protector* protector) { new_id_ = new_search_provider_->id(); registrar_.Add( this, chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, - content::Source<Profile>(protector->profile()->GetOriginalProfile())); + content::Source<Profile>(profile()->GetOriginalProfile())); } return true; @@ -237,7 +234,7 @@ void DefaultSearchProviderChange::Apply() { new_histogram_id_, kProtectorMaxSearchProviderID); - StopObservingTemplateURLService(); + GetTemplateURLService()->RemoveObserver(this); if (new_search_provider_) { GetTemplateURLService()->SetDefaultSearchProvider(new_search_provider_); } else { @@ -252,7 +249,7 @@ void DefaultSearchProviderChange::Discard() { new_histogram_id_, kProtectorMaxSearchProviderID); - StopObservingTemplateURLService(); + GetTemplateURLService()->RemoveObserver(this); if (is_fallback_) { // Open settings page in case the old setting is invalid. OpenSearchEngineSettings(); @@ -268,10 +265,6 @@ void DefaultSearchProviderChange::Timeout() { kProtectorMaxSearchProviderID); } -void DefaultSearchProviderChange::OnBeforeRemoved() { - StopObservingTemplateURLService(); -} - int DefaultSearchProviderChange::GetBadgeIconID() const { return IDR_SEARCH_ENGINE_CHANGE_BADGE; } @@ -396,15 +389,11 @@ void DefaultSearchProviderChange::OpenSearchEngineSettings() { TemplateURLService* DefaultSearchProviderChange::GetTemplateURLService() { TemplateURLService* url_service = - TemplateURLServiceFactory::GetForProfile(protector()->profile()); + TemplateURLServiceFactory::GetForProfile(profile()); DCHECK(url_service); return url_service; } -void DefaultSearchProviderChange::StopObservingTemplateURLService() { - GetTemplateURLService()->RemoveObserver(this); -} - BaseSettingChange* CreateDefaultSearchProviderChange( const TemplateURL* actual, TemplateURL* backup) { diff --git a/chrome/browser/protector/default_search_provider_change_browsertest.cc b/chrome/browser/protector/default_search_provider_change_browsertest.cc new file mode 100644 index 0000000..75dd5a4 --- /dev/null +++ b/chrome/browser/protector/default_search_provider_change_browsertest.cc @@ -0,0 +1,462 @@ +// Copyright (c) 2012 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 "base/memory/scoped_ptr.h" +#include "base/message_loop.h" +#include "base/utf_string_conversions.h" +#include "chrome/browser/protector/mock_protector.h" +#include "chrome/browser/search_engines/template_url.h" +#include "chrome/browser/search_engines/template_url_prepopulate_data.h" +#include "chrome/browser/search_engines/template_url_service.h" +#include "chrome/browser/search_engines/template_url_service_factory.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/common/url_constants.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "chrome/test/base/ui_test_utils.h" +#include "googleurl/src/gurl.h" +#include "grit/generated_resources.h" +#include "ui/base/l10n/l10n_util.h" + +using ::testing::Invoke; +using ::testing::NiceMock; +using ::testing::Return; + +namespace protector { + +namespace { + +// Keyword names and URLs used for testing. + +const string16 example_info = ASCIIToUTF16("Info"); +const string16 example_info_long = ASCIIToUTF16("Example.info"); +const std::string http_example_info = "http://example.info/%s"; +const string16 example_com = ASCIIToUTF16("Com"); +const string16 example_com_long = ASCIIToUTF16("Example.com"); +const std::string http_example_com = "http://example.com/%s"; +const string16 example_net = ASCIIToUTF16("Net"); +const std::string http_example_net = "http://example.net/%s"; + +}; + +class DefaultSearchProviderChangeTest : public InProcessBrowserTest { + public: + virtual void SetUpOnMainThread() { + mock_protector_.reset(new MockProtector(browser()->profile())); + + // Ensure that TemplateURLService is loaded. + turl_service_ = + TemplateURLServiceFactory::GetForProfile(browser()->profile()); + ui_test_utils::WaitForTemplateURLServiceToLoad(turl_service_); + + prepopulated_url_.reset( + TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL)); + } + + TemplateURL* MakeTemplateURL(const string16& short_name, + const string16& keyword, + const std::string& search_url) { + TemplateURL* url = new TemplateURL; + url->set_short_name(short_name); + if (keyword.empty()) + url->set_autogenerate_keyword(true); + else + url->set_keyword(keyword); + url->SetURL(search_url, 0, 0); + return url; + } + + const TemplateURL* FindTemplateURL(const std::string& search_url) { + TemplateURLService::TemplateURLVector urls = + turl_service_->GetTemplateURLs(); + for (TemplateURLService::TemplateURLVector::const_iterator + it = urls.begin(); it != urls.end(); ++it) { + if ((*it)->url()->url() == search_url) + return *it; + } + return NULL; + } + + // Adds a copy of |turl| that will be owned by TemplateURLService. + void AddCopy(TemplateURL* turl) { + TemplateURL* turl_copy = new TemplateURL(*turl); + turl_service_->Add(turl_copy); + } + + void AddAndSetDefault(TemplateURL* t_url) { + turl_service_->Add(t_url); + turl_service_->SetDefaultSearchProvider(t_url); + } + + string16 GetBubbleMessage(const string16& short_name = string16()) { + return short_name.empty() ? + l10n_util::GetStringUTF16(IDS_SEARCH_ENGINE_CHANGE_MESSAGE) : + l10n_util::GetStringFUTF16(IDS_SEARCH_ENGINE_CHANGE_NO_BACKUP_MESSAGE, + short_name); + } + + string16 GetChangeSearchButtonText(const string16& short_name = string16()) { + return short_name.empty() ? + l10n_util::GetStringUTF16(IDS_CHANGE_SEARCH_ENGINE_NO_NAME) : + l10n_util::GetStringFUTF16(IDS_CHANGE_SEARCH_ENGINE, short_name); + } + + string16 GetKeepSearchButtonText(const string16& short_name = string16()) { + return short_name.empty() ? + l10n_util::GetStringUTF16(IDS_KEEP_SETTING) : + l10n_util::GetStringFUTF16(IDS_KEEP_SEARCH_ENGINE, short_name); + } + + string16 GetOpenSettingsButtonText() { + return l10n_util::GetStringUTF16(IDS_SELECT_SEARCH_ENGINE); + } + + void ExpectSettingsOpened(const std::string& subpage) { + GURL settings_url(chrome::kChromeUISettingsURL + subpage); + EXPECT_CALL(*mock_protector_.get(), OpenTab(settings_url)); + } + + protected: + scoped_ptr<MockProtector> mock_protector_; + TemplateURLService* turl_service_; + scoped_ptr<TemplateURL> prepopulated_url_; +}; + +// Tests below call both Apply and Discard on a single change instance. +// This is test-only and should not be happening in real code. + +// |backup_url| in further test cases is owned by DefaultSearchProviderChange +// instance, while other TemplateURLs are owned by TemplateURLService. + +IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupValid) { + // Most common case: current default search provider exists, backup is valid, + // they are different. + TemplateURL* backup_url = + MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); + TemplateURL* current_url = + MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); + + AddCopy(backup_url); + AddAndSetDefault(current_url); + + scoped_ptr<BaseSettingChange> change( + CreateDefaultSearchProviderChange(current_url, backup_url)); + ASSERT_TRUE(change.get()); + ASSERT_TRUE(change->Init(mock_protector_.get())); + + // Verify that backup is active. + EXPECT_EQ(FindTemplateURL(http_example_info), + turl_service_->GetDefaultSearchProvider()); + + // Verify text messages. + EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); + EXPECT_EQ(GetChangeSearchButtonText(example_com), + change->GetApplyButtonText()); + EXPECT_EQ(GetKeepSearchButtonText(example_info), + change->GetDiscardButtonText()); + + // Discard does nothing - backup was already active. + change->Discard(); + EXPECT_EQ(FindTemplateURL(http_example_info), + turl_service_->GetDefaultSearchProvider()); + + // Verify that Apply switches back to |current_url|. + change->Apply(); + EXPECT_EQ(FindTemplateURL(http_example_com), + turl_service_->GetDefaultSearchProvider()); +} + +IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupValidLongNames) { + // Verify that search provider names that are too long are not displayed. + TemplateURL* backup_url = + MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); + TemplateURL* backup_url_long = + MakeTemplateURL(example_info_long, ASCIIToUTF16("a"), http_example_info); + TemplateURL* current_url = + MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); + TemplateURL* current_url_long = + MakeTemplateURL(example_com_long, ASCIIToUTF16("b"), http_example_com); + + { + // Backup name too long. + AddCopy(backup_url_long); + AddAndSetDefault(current_url); + + scoped_ptr<BaseSettingChange> change( + CreateDefaultSearchProviderChange(current_url, backup_url_long)); + ASSERT_TRUE(change.get()); + ASSERT_TRUE(change->Init(mock_protector_.get())); + + // Verify text messages. + EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); + EXPECT_EQ(GetChangeSearchButtonText(example_com), + change->GetApplyButtonText()); + EXPECT_EQ(GetKeepSearchButtonText(), change->GetDiscardButtonText()); + } + + { + // Current name too long. + AddCopy(backup_url); + AddAndSetDefault(current_url_long); + + scoped_ptr<BaseSettingChange> change( + CreateDefaultSearchProviderChange(current_url_long, backup_url)); + ASSERT_TRUE(change.get()); + ASSERT_TRUE(change->Init(mock_protector_.get())); + + // Verify text messages. + EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); + EXPECT_EQ(GetChangeSearchButtonText(), change->GetApplyButtonText()); + EXPECT_EQ(GetKeepSearchButtonText(example_info), + change->GetDiscardButtonText()); + } +} + +IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupInvalid) { + // Backup is invalid, current search provider exists, fallback to the + // prepopulated default search, which exists among keywords. + TemplateURL* current_url = + MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); + + AddAndSetDefault(current_url); + + // Prepopulated default search must exist. + ASSERT_TRUE(FindTemplateURL(prepopulated_url_->url()->url())); + + scoped_ptr<BaseSettingChange> change( + CreateDefaultSearchProviderChange(current_url, NULL)); + ASSERT_TRUE(change.get()); + ASSERT_TRUE(change->Init(mock_protector_.get())); + + // Verify that the prepopulated default search is active. + EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), + turl_service_->GetDefaultSearchProvider()); + + // Verify text messages. + EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), + change->GetBubbleMessage()); + EXPECT_EQ(GetChangeSearchButtonText(example_com), + change->GetApplyButtonText()); + EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); + + // Verify that search engine settings are opened by Discard. + ExpectSettingsOpened(chrome::kSearchEnginesSubPage); + change->Discard(); + EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), + turl_service_->GetDefaultSearchProvider()); + + // Verify that Apply switches back to |current_url|. + change->Apply(); + EXPECT_EQ(FindTemplateURL(http_example_com), + turl_service_->GetDefaultSearchProvider()); +} + +IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, + BackupInvalidFallbackRemoved) { + // Backup is invalid, current search provider exists, fallback to the + // prepopulated default search, which was removed from keywords (will be + // added). + TemplateURL* current_url = + MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); + + AddAndSetDefault(current_url); + + const TemplateURL* prepopulated_default = + FindTemplateURL(prepopulated_url_->url()->url()); + // Prepopulated default search must exist, remove it. + ASSERT_TRUE(prepopulated_default); + turl_service_->Remove(prepopulated_default); + + scoped_ptr<BaseSettingChange> change( + CreateDefaultSearchProviderChange(current_url, NULL)); + ASSERT_TRUE(change.get()); + ASSERT_TRUE(change->Init(mock_protector_.get())); + + // Verify that the prepopulated default search is active. + EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), + turl_service_->GetDefaultSearchProvider()); + + // Verify text messages. + EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), + change->GetBubbleMessage()); + EXPECT_EQ(GetChangeSearchButtonText(example_com), + change->GetApplyButtonText()); + EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); + + // Verify that search engine settings are opened by Discard. + ExpectSettingsOpened(chrome::kSearchEnginesSubPage); + change->Discard(); + EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), + turl_service_->GetDefaultSearchProvider()); + + // Verify that Apply switches back to |current_url|. + change->Apply(); + EXPECT_EQ(FindTemplateURL(http_example_com), + turl_service_->GetDefaultSearchProvider()); +} + +IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, + BackupValidCurrentRemoved) { + // Backup is valid, no current search provider. + TemplateURL* backup_url = + MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); + + AddCopy(backup_url); + turl_service_->SetDefaultSearchProvider(NULL); + + scoped_ptr<BaseSettingChange> change( + CreateDefaultSearchProviderChange(NULL, backup_url)); + ASSERT_TRUE(change.get()); + ASSERT_TRUE(change->Init(mock_protector_.get())); + + // Verify that backup is active. + EXPECT_EQ(FindTemplateURL(http_example_info), + turl_service_->GetDefaultSearchProvider()); + + // Verify text messages. + EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); + EXPECT_EQ(GetOpenSettingsButtonText(), change->GetApplyButtonText()); + EXPECT_EQ(GetKeepSearchButtonText(example_info), + change->GetDiscardButtonText()); + + // Discard does nothing - backup was already active. + change->Discard(); + EXPECT_EQ(FindTemplateURL(http_example_info), + turl_service_->GetDefaultSearchProvider()); + + // Verify that search engine settings are opened by Apply (no other changes). + ExpectSettingsOpened(chrome::kSearchEnginesSubPage); + change->Apply(); + EXPECT_EQ(FindTemplateURL(http_example_info), + turl_service_->GetDefaultSearchProvider()); +} + +IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, + BackupInvalidCurrentRemoved) { + // Backup is invalid, no current search provider, fallback to the prepopulated + // default search. + turl_service_->SetDefaultSearchProvider(NULL); + + scoped_ptr<BaseSettingChange> change( + CreateDefaultSearchProviderChange(NULL, NULL)); + ASSERT_TRUE(change.get()); + ASSERT_TRUE(change->Init(mock_protector_.get())); + + // Verify that the prepopulated default search is active. + EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), + turl_service_->GetDefaultSearchProvider()); + + // Verify text messages. + EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), + change->GetBubbleMessage()); + EXPECT_EQ(string16(), change->GetApplyButtonText()); + EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); + + // Verify that search engine settings are opened by Discard. + ExpectSettingsOpened(chrome::kSearchEnginesSubPage); + change->Discard(); + EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()->url()), + turl_service_->GetDefaultSearchProvider()); +} + +IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, + BackupInvalidFallbackSameAsCurrent) { + // Backup is invalid, fallback to the prepopulated default search which is + // same as the current search provider. + const TemplateURL* current_url = turl_service_->GetDefaultSearchProvider(); + + // Verify that current search provider is same as the prepopulated default. + ASSERT_TRUE(current_url); + ASSERT_EQ(current_url, FindTemplateURL(prepopulated_url_->url()->url())); + + scoped_ptr<BaseSettingChange> change( + CreateDefaultSearchProviderChange(current_url, NULL)); + ASSERT_TRUE(change.get()); + ASSERT_TRUE(change->Init(mock_protector_.get())); + + // Verify that the default search has not changed. + EXPECT_EQ(current_url, turl_service_->GetDefaultSearchProvider()); + + // Verify text messages. + EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), + change->GetBubbleMessage()); + EXPECT_EQ(string16(), change->GetApplyButtonText()); + EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); + + // Verify that search engine settings are opened by Discard. + ExpectSettingsOpened(chrome::kSearchEnginesSubPage); + change->Discard(); + EXPECT_EQ(current_url, turl_service_->GetDefaultSearchProvider()); +} + + +IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, + DefaultSearchProviderChangedByUser) { + // Default search provider is changed by user while the error is active. + // Setup is the same as in BackupValid test case. + TemplateURL* backup_url = + MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); + TemplateURL* current_url = + MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); + TemplateURL* new_url = + MakeTemplateURL(example_net, ASCIIToUTF16("c"), http_example_net); + + AddCopy(backup_url); + AddAndSetDefault(current_url); + + scoped_ptr<BaseSettingChange> change( + CreateDefaultSearchProviderChange(current_url, backup_url)); + ASSERT_TRUE(change.get()); + ASSERT_TRUE(change->Init(mock_protector_.get())); + + // Verify that backup is active. + EXPECT_EQ(FindTemplateURL(http_example_info), + turl_service_->GetDefaultSearchProvider()); + + // Verify that changing search provider externally dismissed the change. + EXPECT_CALL(*mock_protector_.get(), DismissChange()); + AddAndSetDefault(new_url); +} + +IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, + CurrentSearchProviderRemovedByUser) { + // Current search provider is removed by user while the error is active. + // Setup is the same as in BackupValid test case. + TemplateURL* backup_url = + MakeTemplateURL(example_info, ASCIIToUTF16("a"), http_example_info); + TemplateURL* current_url = + MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); + + AddCopy(backup_url); + AddAndSetDefault(current_url); + + scoped_ptr<BaseSettingChange> change( + CreateDefaultSearchProviderChange(current_url, backup_url)); + ASSERT_TRUE(change.get()); + ASSERT_TRUE(change->Init(mock_protector_.get())); + + // Verify that backup is active. + EXPECT_EQ(FindTemplateURL(http_example_info), + turl_service_->GetDefaultSearchProvider()); + + turl_service_->Remove(current_url); + + // Verify that text messages altered after removing |current_url|. + EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); + EXPECT_EQ(GetOpenSettingsButtonText(), change->GetApplyButtonText()); + EXPECT_EQ(GetKeepSearchButtonText(example_info), + change->GetDiscardButtonText()); + + // Verify that search engine settings are opened by Apply. + ExpectSettingsOpened(chrome::kSearchEnginesSubPage); + change->Apply(); + EXPECT_EQ(FindTemplateURL(http_example_info), + turl_service_->GetDefaultSearchProvider()); + + // Discard does nothing - backup was already active. + change->Discard(); + EXPECT_EQ(FindTemplateURL(http_example_info), + turl_service_->GetDefaultSearchProvider()); +} + +} // namespace protector diff --git a/chrome/browser/protector/mock_protector.cc b/chrome/browser/protector/mock_protector.cc new file mode 100644 index 0000000..868ed93 --- /dev/null +++ b/chrome/browser/protector/mock_protector.cc @@ -0,0 +1,17 @@ +// Copyright (c) 2012 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/protector/mock_protector.h" + +#include "googleurl/src/gurl.h" + +namespace protector { + +MockProtector::MockProtector(Profile* profile) : Protector(profile) { +} + +MockProtector::~MockProtector() { +} + +} // namespace protector diff --git a/chrome/browser/protector/mock_protector.h b/chrome/browser/protector/mock_protector.h new file mode 100644 index 0000000..513d6a5 --- /dev/null +++ b/chrome/browser/protector/mock_protector.h @@ -0,0 +1,31 @@ +// Copyright (c) 2012 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. + +#ifndef CHROME_BROWSER_PROTECTOR_MOCK_PROTECTOR_H_ +#define CHROME_BROWSER_PROTECTOR_MOCK_PROTECTOR_H_ +#pragma once + +#include "chrome/browser/protector/protector.h" +#include "testing/gmock/include/gmock/gmock.h" + +namespace protector { + +class MockProtector : public Protector { + public: + explicit MockProtector(Profile* profile); + virtual ~MockProtector(); + + MOCK_METHOD1(ShowChange, void(BaseSettingChange*)); + MOCK_METHOD0(DismissChange, void()); + MOCK_METHOD1(OpenTab, void(const GURL&)); + + MOCK_METHOD0(OnApplyChange, void()); + MOCK_METHOD0(OnDiscardChange, void()); + MOCK_METHOD0(OnDecisionTimeout, void()); + MOCK_METHOD0(OnRemovedFromProfile, void()); +}; + +} // namespace protector + +#endif // CHROME_BROWSER_PROTECTOR_MOCK_PROTECTOR_H_ diff --git a/chrome/browser/protector/mock_setting_change.cc b/chrome/browser/protector/mock_setting_change.cc new file mode 100644 index 0000000..63726ec --- /dev/null +++ b/chrome/browser/protector/mock_setting_change.cc @@ -0,0 +1,39 @@ +// Copyright (c) 2012 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/protector/mock_setting_change.h" + +#include "base/utf_string_conversions.h" +#include "grit/theme_resources.h" +#include "grit/theme_resources_standard.h" + +using ::testing::Return; + +namespace protector { + +MockSettingChange::MockSettingChange() { + ON_CALL(*this, GetBadgeIconID()).WillByDefault(Return(IDR_UPDATE_BADGE4)); + ON_CALL(*this, GetMenuItemIconID()).WillByDefault(Return(IDR_UPDATE_MENU4)); + ON_CALL(*this, GetBubbleIconID()).WillByDefault(Return(IDR_INPUT_ALERT)); + + ON_CALL(*this, GetBubbleTitle()). + WillByDefault(Return(UTF8ToUTF16("Title"))); + ON_CALL(*this, GetBubbleMessage()). + WillByDefault(Return(UTF8ToUTF16("Message"))); + ON_CALL(*this, GetApplyButtonText()). + WillByDefault(Return(UTF8ToUTF16("Apply"))); + ON_CALL(*this, GetDiscardButtonText()). + WillByDefault(Return(UTF8ToUTF16("Discard"))); +} + +MockSettingChange::~MockSettingChange() { +} + +bool MockSettingChange::Init(Protector* protector) { + if (!BaseSettingChange::Init(protector)) + return false; + return MockInit(protector); +} + +} // namespace protector diff --git a/chrome/browser/protector/mock_setting_change.h b/chrome/browser/protector/mock_setting_change.h new file mode 100644 index 0000000..1d29801 --- /dev/null +++ b/chrome/browser/protector/mock_setting_change.h @@ -0,0 +1,39 @@ +// Copyright (c) 2012 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. + +#ifndef CHROME_BROWSER_PROTECTOR_MOCK_SETTING_CHANGE_H_ +#define CHROME_BROWSER_PROTECTOR_MOCK_SETTING_CHANGE_H_ +#pragma once + +#include "base/compiler_specific.h" +#include "chrome/browser/protector/base_setting_change.h" +#include "testing/gmock/include/gmock/gmock.h" + +namespace protector { + +class MockSettingChange : public BaseSettingChange { + public: + MockSettingChange(); + virtual ~MockSettingChange(); + + virtual bool Init(Protector* protector) OVERRIDE; + + MOCK_METHOD1(MockInit, bool(Protector* protector)); + MOCK_METHOD0(Apply, void()); + MOCK_METHOD0(Discard, void()); + MOCK_METHOD0(Timeout, void()); + + MOCK_CONST_METHOD0(GetBadgeIconID, int()); + MOCK_CONST_METHOD0(GetMenuItemIconID, int()); + MOCK_CONST_METHOD0(GetBubbleIconID, int()); + + MOCK_CONST_METHOD0(GetBubbleTitle, string16()); + MOCK_CONST_METHOD0(GetBubbleMessage, string16()); + MOCK_CONST_METHOD0(GetApplyButtonText, string16()); + MOCK_CONST_METHOD0(GetDiscardButtonText, string16()); +}; + +} // namespace protector + +#endif // CHROME_BROWSER_PROTECTOR_MOCK_SETTING_CHANGE_H_ diff --git a/chrome/browser/protector/protector.cc b/chrome/browser/protector/protector.cc index 60d7a1b..d80ce91 100644 --- a/chrome/browser/protector/protector.cc +++ b/chrome/browser/protector/protector.cc @@ -28,8 +28,6 @@ Protector::Protector(Profile* profile) } Protector::~Protector() { - if (change_.get()) - change_->OnBeforeRemoved(); } void Protector::OpenTab(const GURL& url) { diff --git a/chrome/browser/protector/protector.h b/chrome/browser/protector/protector.h index 803a2af..3ded4a3 100644 --- a/chrome/browser/protector/protector.h +++ b/chrome/browser/protector/protector.h @@ -28,16 +28,16 @@ class Protector : public SettingsChangeGlobalErrorDelegate { public: explicit Protector(Profile* profile); - // Opens a tab with specified URL in the browser window we've shown error - // bubble for. - void OpenTab(const GURL& url); - // Shows global error about the specified change. Owns |change|. - void ShowChange(BaseSettingChange* change); + virtual void ShowChange(BaseSettingChange* change); // Silently discards any change previously shown (without calling Discard), // removes global error and deletes itself. - void DismissChange(); + virtual void DismissChange(); + + // Opens a tab with specified URL in the browser window we've shown error + // bubble for. + virtual void OpenTab(const GURL& url); // Returns the Profile instance we've shown error bubble for. Profile* profile() { return profile_; } @@ -50,6 +50,8 @@ class Protector : public SettingsChangeGlobalErrorDelegate { private: friend class base::DeleteHelper<Protector>; + friend class MockProtector; + friend class ProtectorTest; // The object can only be allocated and destroyed on heap. virtual ~Protector(); diff --git a/chrome/browser/protector/protector_browsertest.cc b/chrome/browser/protector/protector_browsertest.cc new file mode 100644 index 0000000..32b2474 --- /dev/null +++ b/chrome/browser/protector/protector_browsertest.cc @@ -0,0 +1,114 @@ +// Copyright (c) 2012 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 "base/memory/scoped_ptr.h" +#include "base/message_loop.h" +#include "chrome/app/chrome_command_ids.h" +#include "chrome/browser/protector/mock_setting_change.h" +#include "chrome/browser/protector/protector.h" +#include "chrome/browser/protector/settings_change_global_error.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/global_error.h" +#include "chrome/browser/ui/global_error_service.h" +#include "chrome/browser/ui/global_error_service_factory.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "chrome/test/base/ui_test_utils.h" + +using ::testing::Invoke; +using ::testing::NiceMock; +using ::testing::Return; + +namespace protector { + +class ProtectorTest : public InProcessBrowserTest { + public: + virtual void SetUpOnMainThread() { + // Protect owns the change and deletes himself. + protector_ = new Protector(browser()->profile()); + mock_change_ = new NiceMock<MockSettingChange>(); + } + + protected: + GlobalError* GetGlobalError() { + return protector_->error_.get(); + } + + void ExpectGlobalErrorActive(bool active) { + GlobalError* error = + GlobalErrorServiceFactory::GetForProfile(browser()->profile())-> + GetGlobalErrorByMenuItemCommandID(IDC_SHOW_SETTINGS_CHANGES); + EXPECT_EQ(active, static_cast<bool>(error)); + } + + Protector* protector_; + MockSettingChange* mock_change_; +}; + +IN_PROC_BROWSER_TEST_F(ProtectorTest, ChangeInitError) { + EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(false)); + protector_->ShowChange(mock_change_); + ExpectGlobalErrorActive(false); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(false); +} + +IN_PROC_BROWSER_TEST_F(ProtectorTest, ShowAndDismiss) { + EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(true)); + protector_->ShowChange(mock_change_); + ui_test_utils::RunAllPendingInMessageLoop(); + ASSERT_TRUE(GetGlobalError()); + ExpectGlobalErrorActive(true); + protector_->DismissChange(); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(false); +} + +IN_PROC_BROWSER_TEST_F(ProtectorTest, ShowAndApply) { + EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(true)); + protector_->ShowChange(mock_change_); + ui_test_utils::RunAllPendingInMessageLoop(); + GlobalError* error = GetGlobalError(); + ASSERT_TRUE(error); + ExpectGlobalErrorActive(true); + EXPECT_CALL(*mock_change_, Apply()); + // Pressing Cancel applies the change. + error->BubbleViewCancelButtonPressed(); + error->BubbleViewDidClose(); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(false); +} + +IN_PROC_BROWSER_TEST_F(ProtectorTest, ShowAndDiscard) { + EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(true)); + protector_->ShowChange(mock_change_); + ui_test_utils::RunAllPendingInMessageLoop(); + GlobalError* error = GetGlobalError(); + ASSERT_TRUE(error); + ExpectGlobalErrorActive(true); + EXPECT_CALL(*mock_change_, Discard()); + // Pressing Apply discards the change. + error->BubbleViewAcceptButtonPressed(); + error->BubbleViewDidClose(); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(false); +} + +IN_PROC_BROWSER_TEST_F(ProtectorTest, BubbleClosedInsideApply) { + EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(true)); + protector_->ShowChange(mock_change_); + ui_test_utils::RunAllPendingInMessageLoop(); + GlobalError* error = GetGlobalError(); + ASSERT_TRUE(error); + ExpectGlobalErrorActive(true); + EXPECT_CALL(*mock_change_, Apply()). + WillOnce(Invoke(error, &GlobalError::BubbleViewDidClose)); + // Pressing Cancel applies the change. + error->BubbleViewCancelButtonPressed(); + ui_test_utils::RunAllPendingInMessageLoop(); + ExpectGlobalErrorActive(false); +} + +// TODO(ivankr): Timeout test. + +} // namespace protector |