summaryrefslogtreecommitdiffstats
path: root/chrome/browser/search_engines
diff options
context:
space:
mode:
authorlevin@chromium.org <levin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-11 23:00:12 +0000
committerlevin@chromium.org <levin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-11 23:00:12 +0000
commit0b5b85c1ee9908e6b1f8dfc86806e4bf8342405a (patch)
tree2983ef5b8e1f7740dea368e059583c2e11d7c51b /chrome/browser/search_engines
parentfacac153d1eefa387a587e711e242dc7630d8ebf (diff)
downloadchromium_src-0b5b85c1ee9908e6b1f8dfc86806e4bf8342405a.zip
chromium_src-0b5b85c1ee9908e6b1f8dfc86806e4bf8342405a.tar.gz
chromium_src-0b5b85c1ee9908e6b1f8dfc86806e4bf8342405a.tar.bz2
Refactor TemplateURLFetcher to make it more testable and add more tests for it.
I also moved some basic logic from tab_contents to TemplateURLFetcher to make it easier to unit test it. BUG=38475 TEST=unit_tests.exe --gtest_filter=TemplateURLFetcherTest* Review URL: http://codereview.chromium.org/3710001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@62207 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/search_engines')
-rw-r--r--chrome/browser/search_engines/template_url_fetcher.cc97
-rw-r--r--chrome/browser/search_engines/template_url_fetcher.h6
-rwxr-xr-xchrome/browser/search_engines/template_url_fetcher_callbacks.h32
-rwxr-xr-xchrome/browser/search_engines/template_url_fetcher_ui_callbacks.cc56
-rwxr-xr-xchrome/browser/search_engines/template_url_fetcher_ui_callbacks.h47
-rw-r--r--chrome/browser/search_engines/template_url_fetcher_unittest.cc198
6 files changed, 354 insertions, 82 deletions
diff --git a/chrome/browser/search_engines/template_url_fetcher.cc b/chrome/browser/search_engines/template_url_fetcher.cc
index 0a7f352..af428c8 100644
--- a/chrome/browser/search_engines/template_url_fetcher.cc
+++ b/chrome/browser/search_engines/template_url_fetcher.cc
@@ -8,25 +8,21 @@
#include "chrome/browser/profile.h"
#include "chrome/browser/search_engines/template_url.h"
+#include "chrome/browser/search_engines/template_url_fetcher_callbacks.h"
#include "chrome/browser/search_engines/template_url_model.h"
#include "chrome/browser/search_engines/template_url_parser.h"
-#include "chrome/browser/tab_contents/tab_contents.h"
-#include "chrome/browser/tab_contents/tab_contents_delegate.h"
#include "chrome/common/net/url_fetcher.h"
-#include "chrome/common/notification_registrar.h"
-#include "chrome/common/notification_source.h"
-#include "chrome/common/notification_type.h"
#include "net/url_request/url_request_status.h"
// RequestDelegate ------------------------------------------------------------
-class TemplateURLFetcher::RequestDelegate : public URLFetcher::Delegate,
- public NotificationObserver {
+class TemplateURLFetcher::RequestDelegate : public URLFetcher::Delegate {
public:
+ // Takes ownership of |callbacks|.
RequestDelegate(TemplateURLFetcher* fetcher,
const std::wstring& keyword,
const GURL& osdd_url,
const GURL& favicon_url,
- TabContents* source,
+ TemplateURLFetcherCallbacks* callbacks,
ProviderType provider_type)
: ALLOW_THIS_IN_INITIALIZER_LIST(url_fetcher_(osdd_url,
URLFetcher::GET, this)),
@@ -35,12 +31,9 @@ class TemplateURLFetcher::RequestDelegate : public URLFetcher::Delegate,
osdd_url_(osdd_url),
favicon_url_(favicon_url),
provider_type_(provider_type),
- source_(source) {
+ callbacks_(callbacks) {
url_fetcher_.set_request_context(fetcher->profile()->GetRequestContext());
url_fetcher_.Start();
- registrar_.Add(this,
- NotificationType::TAB_CONTENTS_DESTROYED,
- Source<TabContents>(source_));
}
// URLFetcher::Delegate:
@@ -53,15 +46,6 @@ class TemplateURLFetcher::RequestDelegate : public URLFetcher::Delegate,
const ResponseCookies& cookies,
const std::string& data);
- // NotificationObserver:
- virtual void Observe(NotificationType type,
- const NotificationSource& source,
- const NotificationDetails& details) {
- DCHECK(type == NotificationType::TAB_CONTENTS_DESTROYED);
- DCHECK(source == Source<TabContents>(source_));
- source_ = NULL;
- }
-
// URL of the OSDD.
const GURL& url() const { return osdd_url_; }
@@ -81,13 +65,7 @@ class TemplateURLFetcher::RequestDelegate : public URLFetcher::Delegate,
const GURL osdd_url_;
const GURL favicon_url_;
const ProviderType provider_type_;
-
- // The TabContents where this request originated. Can be NULL if the
- // originating tab is closed. If NULL, the engine is not added.
- TabContents* source_;
-
- // Handles registering for our notifications.
- NotificationRegistrar registrar_;
+ scoped_ptr<TemplateURLFetcherCallbacks> callbacks_;
DISALLOW_COPY_AND_ASSIGN(RequestDelegate);
};
@@ -177,23 +155,19 @@ void TemplateURLFetcher::RequestDelegate::AddSearchProvider() {
break;
case EXPLICIT_PROVIDER:
- if (source_ && source_->delegate()) {
- // Confirm addition and allow user to edit default choices. It's ironic
- // that only *non*-autodetected additions get confirmed, but the user
- // expects feedback that his action did something.
- // The source TabContents' delegate takes care of adding the URL to the
- // model, which takes ownership, or of deleting it if the add is
- // cancelled.
- source_->delegate()->ConfirmAddSearchProvider(template_url_.release(),
- fetcher_->profile());
- }
+ // Confirm addition and allow user to edit default choices. It's ironic
+ // that only *non*-autodetected additions get confirmed, but the user
+ // expects feedback that his action did something.
+ // The source TabContents' delegate takes care of adding the URL to the
+ // model, which takes ownership, or of deleting it if the add is
+ // cancelled.
+ callbacks_->ConfirmAddSearchProvider(template_url_.release(),
+ fetcher_->profile());
break;
case EXPLICIT_DEFAULT_PROVIDER:
- source_->delegate()->ConfirmSetDefaultSearchProvider(
- source_,
- template_url_.release(),
- model);
+ callbacks_->ConfirmSetDefaultSearchProvider(template_url_.release(),
+ model);
break;
}
@@ -210,12 +184,35 @@ TemplateURLFetcher::TemplateURLFetcher(Profile* profile) : profile_(profile) {
TemplateURLFetcher::~TemplateURLFetcher() {
}
-void TemplateURLFetcher::ScheduleDownload(const std::wstring& keyword,
- const GURL& osdd_url,
- const GURL& favicon_url,
- TabContents* source,
- ProviderType provider_type) {
- DCHECK(!keyword.empty() && osdd_url.is_valid());
+void TemplateURLFetcher::ScheduleDownload(
+ const std::wstring& keyword,
+ const GURL& osdd_url,
+ const GURL& favicon_url,
+ TemplateURLFetcherCallbacks* callbacks,
+ ProviderType provider_type) {
+ DCHECK(osdd_url.is_valid());
+ scoped_ptr<TemplateURLFetcherCallbacks> owned_callbacks(callbacks);
+
+ // For JS added OSDD empty keyword is OK because we will generate keyword
+ // later from OSDD content.
+ if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER &&
+ keyword.empty())
+ return;
+ TemplateURLModel* url_model = profile()->GetTemplateURLModel();
+ if (!url_model)
+ return;
+ if (!url_model->loaded()) {
+ url_model->Load();
+ return;
+ }
+ const TemplateURL* template_url =
+ url_model->GetTemplateURLForKeyword(keyword);
+ if (template_url && (!template_url->safe_for_autoreplace() ||
+ template_url->originating_url() == osdd_url)) {
+ // Either there is a user created TemplateURL for this keyword, or the
+ // keyword has the same OSDD url and we've parsed it.
+ return;
+ }
// Make sure we aren't already downloading this request.
for (std::vector<RequestDelegate*>::iterator i = requests_->begin();
@@ -231,8 +228,8 @@ void TemplateURLFetcher::ScheduleDownload(const std::wstring& keyword,
}
requests_->push_back(
- new RequestDelegate(this, keyword, osdd_url, favicon_url, source,
- provider_type));
+ new RequestDelegate(this, keyword, osdd_url, favicon_url,
+ owned_callbacks.release(), provider_type));
}
void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) {
diff --git a/chrome/browser/search_engines/template_url_fetcher.h b/chrome/browser/search_engines/template_url_fetcher.h
index c254946..b1e15cc 100644
--- a/chrome/browser/search_engines/template_url_fetcher.h
+++ b/chrome/browser/search_engines/template_url_fetcher.h
@@ -12,7 +12,7 @@
class GURL;
class Profile;
class TemplateURL;
-class TabContents;
+class TemplateURLFetcherCallbacks;
// TemplateURLFetcher is responsible for downloading OpenSearch description
// documents, creating a TemplateURL from the OSDD, and adding the TemplateURL
@@ -32,11 +32,11 @@ class TemplateURLFetcher {
// If TemplateURLFetcher is not already downloading the OSDD for osdd_url,
// it is downloaded. If successful and the result can be parsed, a TemplateURL
- // is added to the TemplateURLModel.
+ // is added to the TemplateURLModel. Takes ownership of |callbacks|.
void ScheduleDownload(const std::wstring& keyword,
const GURL& osdd_url,
const GURL& favicon_url,
- TabContents* source,
+ TemplateURLFetcherCallbacks* callbacks,
ProviderType provider_type);
// The current number of outstanding requests.
diff --git a/chrome/browser/search_engines/template_url_fetcher_callbacks.h b/chrome/browser/search_engines/template_url_fetcher_callbacks.h
new file mode 100755
index 0000000..6a009cd
--- /dev/null
+++ b/chrome/browser/search_engines/template_url_fetcher_callbacks.h
@@ -0,0 +1,32 @@
+// Copyright (c) 2010 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_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_CALLBACKS_H_
+#define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_CALLBACKS_H_
+#pragma once
+
+class Profile;
+class TemplateURL;
+class TemplateURLModel;
+
+// Callbacks for the TemplateURLFetcher.
+class TemplateURLFetcherCallbacks {
+ public:
+ TemplateURLFetcherCallbacks() {}
+ virtual ~TemplateURLFetcherCallbacks() {}
+
+ // Performs the confirmation step for setting the default search engine
+ // described by |template_url|. Takes ownership of |template_url|.
+ virtual void ConfirmSetDefaultSearchProvider(
+ TemplateURL* template_url,
+ TemplateURLModel* template_url_model) = 0;
+
+ // Performs the confirmation step for adding a search engine described by
+ // |template_url|. Takes ownership of |template_url|.
+ virtual void ConfirmAddSearchProvider(
+ TemplateURL* template_url,
+ Profile* profile) = 0;
+};
+
+#endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_CALLBACKS_H_
diff --git a/chrome/browser/search_engines/template_url_fetcher_ui_callbacks.cc b/chrome/browser/search_engines/template_url_fetcher_ui_callbacks.cc
new file mode 100755
index 0000000..02e1f9c
--- /dev/null
+++ b/chrome/browser/search_engines/template_url_fetcher_ui_callbacks.cc
@@ -0,0 +1,56 @@
+// Copyright (c) 2010 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/search_engines/template_url_fetcher_ui_callbacks.h"
+
+#include "base/logging.h"
+#include "base/scoped_ptr.h"
+#include "chrome/common/notification_source.h"
+#include "chrome/common/notification_type.h"
+#include "chrome/browser/search_engines/template_url.h"
+#include "chrome/browser/tab_contents/tab_contents.h"
+#include "chrome/browser/tab_contents/tab_contents_delegate.h"
+
+TemplateURLFetcherUICallbacks::TemplateURLFetcherUICallbacks(
+ TabContents* source)
+ : source_(source) {
+ registrar_.Add(this,
+ NotificationType::TAB_CONTENTS_DESTROYED,
+ Source<TabContents>(source_));
+}
+
+TemplateURLFetcherUICallbacks::~TemplateURLFetcherUICallbacks() {
+}
+
+void TemplateURLFetcherUICallbacks::ConfirmSetDefaultSearchProvider(
+ TemplateURL* template_url,
+ TemplateURLModel* template_url_model) {
+ scoped_ptr<TemplateURL> owned_template_url(template_url);
+ if (!source_ || !source_->delegate())
+ return;
+
+ source_->delegate()->ConfirmSetDefaultSearchProvider(
+ source_,
+ owned_template_url.release(),
+ template_url_model);
+}
+void TemplateURLFetcherUICallbacks::ConfirmAddSearchProvider(
+ TemplateURL* template_url,
+ Profile* profile) {
+ scoped_ptr<TemplateURL> owned_template_url(template_url);
+ if (!source_ || !source_->delegate())
+ return;
+
+ source_->delegate()->ConfirmAddSearchProvider(owned_template_url.release(),
+ profile);
+}
+
+void TemplateURLFetcherUICallbacks::Observe(
+ NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ DCHECK(type == NotificationType::TAB_CONTENTS_DESTROYED);
+ DCHECK(source == Source<TabContents>(source_));
+ source_ = NULL;
+}
diff --git a/chrome/browser/search_engines/template_url_fetcher_ui_callbacks.h b/chrome/browser/search_engines/template_url_fetcher_ui_callbacks.h
new file mode 100755
index 0000000..f67f765
--- /dev/null
+++ b/chrome/browser/search_engines/template_url_fetcher_ui_callbacks.h
@@ -0,0 +1,47 @@
+// Copyright (c) 2010 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_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_UI_CALLBACKS_H_
+#define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_UI_CALLBACKS_H_
+#pragma once
+
+#include "base/basictypes.h"
+#include "chrome/browser/search_engines/template_url_fetcher_callbacks.h"
+#include "chrome/common/notification_observer.h"
+#include "chrome/common/notification_registrar.h"
+
+class TabContents;
+
+// Callbacks which display UI for the TemplateURLFetcher.
+class TemplateURLFetcherUICallbacks : public TemplateURLFetcherCallbacks,
+ public NotificationObserver {
+ public:
+ explicit TemplateURLFetcherUICallbacks(TabContents* source);
+ virtual ~TemplateURLFetcherUICallbacks();
+
+ // TemplateURLFetcherCallback implementation.
+ virtual void ConfirmSetDefaultSearchProvider(
+ TemplateURL* template_url,
+ TemplateURLModel* template_url_model);
+ virtual void ConfirmAddSearchProvider(
+ TemplateURL* template_url,
+ Profile* profile);
+
+ // NotificationObserver:
+ virtual void Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details);
+
+ private:
+ // The TabContents where this request originated. Can be NULL if the
+ // originating tab is closed. If NULL, the engine is not added.
+ TabContents* source_;
+
+ // Handles registering for our notifications.
+ NotificationRegistrar registrar_;
+
+ DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcherUICallbacks);
+};
+
+#endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_FETCHER_UI_CALLBACKS_H_
diff --git a/chrome/browser/search_engines/template_url_fetcher_unittest.cc b/chrome/browser/search_engines/template_url_fetcher_unittest.cc
index 57bb88c..fe5c251 100644
--- a/chrome/browser/search_engines/template_url_fetcher_unittest.cc
+++ b/chrome/browser/search_engines/template_url_fetcher_unittest.cc
@@ -8,6 +8,7 @@
#include "base/scoped_ptr.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_fetcher.h"
+#include "chrome/browser/search_engines/template_url_fetcher_callbacks.h"
#include "chrome/browser/search_engines/template_url_model.h"
#include "chrome/browser/search_engines/template_url_model_test_util.h"
#include "chrome/common/chrome_paths.h"
@@ -16,32 +17,29 @@
#include "net/test/test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
-// Quits the current message loop as soon as there is a change to the
-// TemplateURLModel.
-class QuitOnChangedObserver : public TemplateURLModelObserver {
+class TemplateURLFetcherTest;
+
+// Handles callbacks from TemplateURLFetcher.
+class TemplateURLFetcherTestCallbacks : public TemplateURLFetcherCallbacks {
public:
- explicit QuitOnChangedObserver(TemplateURLModel* model);
- virtual ~QuitOnChangedObserver();
+ explicit TemplateURLFetcherTestCallbacks(TemplateURLFetcherTest* test)
+ : test_(test) {
+ }
+ virtual ~TemplateURLFetcherTestCallbacks();
- // TemplateURLModelObserver implemementation.
- virtual void OnTemplateURLModelChanged();
+ // TemplateURLFetcherCallbacks implementation.
+ virtual void ConfirmSetDefaultSearchProvider(
+ TemplateURL* template_url,
+ TemplateURLModel* template_url_model);
+ virtual void ConfirmAddSearchProvider(
+ TemplateURL* template_url,
+ Profile* profile);
private:
- TemplateURLModel* model_;
-};
-
-QuitOnChangedObserver::QuitOnChangedObserver(TemplateURLModel* model)
- : model_(model) {
- model_->AddObserver(this);
-}
+ TemplateURLFetcherTest* test_;
-QuitOnChangedObserver::~QuitOnChangedObserver() {
- model_->RemoveObserver(this);
-}
-
-void QuitOnChangedObserver::OnTemplateURLModelChanged() {
- MessageLoop::current()->Quit();
-}
+ DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcherTestCallbacks);
+};
// Basic set-up for TemplateURLFetcher tests.
class TemplateURLFetcherTest : public testing::Test {
@@ -64,6 +62,18 @@ class TemplateURLFetcherTest : public testing::Test {
test_util_.TearDown();
}
+ // Called by ~TemplateURLFetcherTestCallbacks.
+ void DestroyedCallback(TemplateURLFetcherTestCallbacks* callbacks);
+
+ // TemplateURLFetcherCallbacks implementation. (Although not derived from
+ // this class, these methods handle those calls for the test.)
+ virtual void ConfirmSetDefaultSearchProvider(
+ TemplateURL* template_url,
+ TemplateURLModel* template_url_model);
+ virtual void ConfirmAddSearchProvider(
+ TemplateURL* template_url,
+ Profile* profile);
+
protected:
// Schedules the download of the url.
void StartDownload(const std::wstring& keyword,
@@ -77,13 +87,70 @@ class TemplateURLFetcherTest : public testing::Test {
TemplateURLModelTestUtil test_util_;
net::TestServer test_server_;
+ // The last TemplateURL to come from a callback.
+ scoped_ptr<TemplateURL> last_callback_template_url_;
+
+ // How many TemplateURLFetcherTestCallbacks have been destructed.
+ int callbacks_destroyed_;
+
+ // How many times ConfirmSetDefaultSearchProvider has been called.
+ int set_default_called_;
+
+ // How many times ConfirmAddSearchProvider has been called.
+ int add_provider_called_;
+
+ // Is the code in WaitForDownloadToFinish in a message loop waiting for a
+ // callback to finish?
+ bool waiting_for_download_;
+
private:
DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcherTest);
};
+TemplateURLFetcherTestCallbacks::~TemplateURLFetcherTestCallbacks() {
+ test_->DestroyedCallback(this);
+}
+
+void TemplateURLFetcherTestCallbacks::ConfirmSetDefaultSearchProvider(
+ TemplateURL* template_url,
+ TemplateURLModel* template_url_model) {
+ test_->ConfirmSetDefaultSearchProvider(template_url, template_url_model);
+}
+
+void TemplateURLFetcherTestCallbacks::ConfirmAddSearchProvider(
+ TemplateURL* template_url,
+ Profile* profile) {
+ test_->ConfirmAddSearchProvider(template_url, profile);
+}
+
TemplateURLFetcherTest::TemplateURLFetcherTest()
: test_server_(net::TestServer::TYPE_HTTP,
- FilePath(FILE_PATH_LITERAL("chrome/test/data"))) {
+ FilePath(FILE_PATH_LITERAL("chrome/test/data"))),
+ callbacks_destroyed_(0),
+ set_default_called_(0),
+ add_provider_called_(0),
+ waiting_for_download_(false) {
+}
+
+void TemplateURLFetcherTest::DestroyedCallback(
+ TemplateURLFetcherTestCallbacks* callbacks) {
+ callbacks_destroyed_++;
+ if (waiting_for_download_)
+ MessageLoop::current()->Quit();
+}
+
+void TemplateURLFetcherTest::ConfirmSetDefaultSearchProvider(
+ TemplateURL* template_url,
+ TemplateURLModel* template_url_model) {
+ last_callback_template_url_.reset(template_url);
+ set_default_called_++;
+}
+
+void TemplateURLFetcherTest::ConfirmAddSearchProvider(
+ TemplateURL* template_url,
+ Profile* profile) {
+ last_callback_template_url_.reset(template_url);
+ add_provider_called_++;
}
void TemplateURLFetcherTest::StartDownload(
@@ -104,26 +171,30 @@ void TemplateURLFetcherTest::StartDownload(
GURL osdd_url = test_server_.GetURL("files/" + osdd_file_name);
GURL favicon_url;
test_util_.profile()->GetTemplateURLFetcher()->ScheduleDownload(
- keyword, osdd_url, favicon_url, NULL, provider_type);
- ASSERT_EQ(1, test_util_.profile()->GetTemplateURLFetcher()->requests_count());
+ keyword, osdd_url, favicon_url, new TemplateURLFetcherTestCallbacks(this),
+ provider_type);
}
void TemplateURLFetcherTest::WaitForDownloadToFinish() {
- QuitOnChangedObserver quit_on_changed_observer(test_util_.model());
+ ASSERT_FALSE(waiting_for_download_);
+ waiting_for_download_ = true;
MessageLoop::current()->Run();
+ waiting_for_download_ = false;
}
-TEST_F(TemplateURLFetcherTest, BasicTest) {
+TEST_F(TemplateURLFetcherTest, BasicAutodetectedTest) {
std::wstring keyword(L"test");
test_util_.ChangeModelToLoadState();
- test_util_.ResetObserverCount();
ASSERT_FALSE(test_util_.model()->GetTemplateURLForKeyword(keyword));
std::string osdd_file_name("simple_open_search.xml");
StartDownload(keyword, osdd_file_name,
TemplateURLFetcher::AUTODETECTED_PROVIDER, true);
WaitForDownloadToFinish();
+ ASSERT_EQ(0, set_default_called_);
+ ASSERT_EQ(0, add_provider_called_);
+ ASSERT_EQ(1, callbacks_destroyed_);
const TemplateURL* t_url = test_util_.model()->GetTemplateURLForKeyword(
keyword);
@@ -131,13 +202,13 @@ TEST_F(TemplateURLFetcherTest, BasicTest) {
EXPECT_STREQ(L"http://example.com/%s/other_stuff",
t_url->url()->DisplayURL().c_str());
EXPECT_EQ(true, t_url->safe_for_autoreplace());
+
}
-TEST_F(TemplateURLFetcherTest, DuplicateThrownAway) {
+TEST_F(TemplateURLFetcherTest, DuplicatesThrownAway) {
std::wstring keyword(L"test");
test_util_.ChangeModelToLoadState();
- test_util_.ResetObserverCount();
ASSERT_FALSE(test_util_.model()->GetTemplateURLForKeyword(keyword));
std::string osdd_file_name("simple_open_search.xml");
@@ -167,7 +238,76 @@ TEST_F(TemplateURLFetcherTest, DuplicateThrownAway) {
1,
test_util_.profile()->GetTemplateURLFetcher()->requests_count()) <<
test_cases[i].description;
+ ASSERT_EQ(i + 1, static_cast<size_t>(callbacks_destroyed_));
}
WaitForDownloadToFinish();
+ ASSERT_EQ(1 + ARRAYSIZE_UNSAFE(test_cases),
+ static_cast<size_t>(callbacks_destroyed_));
+ ASSERT_EQ(0, set_default_called_);
+ ASSERT_EQ(0, add_provider_called_);
+}
+
+TEST_F(TemplateURLFetcherTest, BasicExplicitTest) {
+ std::wstring keyword(L"test");
+
+ test_util_.ChangeModelToLoadState();
+ ASSERT_FALSE(test_util_.model()->GetTemplateURLForKeyword(keyword));
+
+ std::string osdd_file_name("simple_open_search.xml");
+ StartDownload(keyword, osdd_file_name,
+ TemplateURLFetcher::EXPLICIT_PROVIDER, true);
+ WaitForDownloadToFinish();
+ ASSERT_EQ(0, set_default_called_);
+ ASSERT_EQ(1, add_provider_called_);
+ ASSERT_EQ(1, callbacks_destroyed_);
+
+ ASSERT_TRUE(last_callback_template_url_.get());
+ EXPECT_STREQ(L"http://example.com/%s/other_stuff",
+ last_callback_template_url_->url()->DisplayURL().c_str());
+ EXPECT_EQ(false, last_callback_template_url_->safe_for_autoreplace());
+}
+
+TEST_F(TemplateURLFetcherTest, BasicExplicitDefaultTest) {
+ std::wstring keyword(L"test");
+
+ test_util_.ChangeModelToLoadState();
+ ASSERT_FALSE(test_util_.model()->GetTemplateURLForKeyword(keyword));
+
+ std::string osdd_file_name("simple_open_search.xml");
+ StartDownload(keyword, osdd_file_name,
+ TemplateURLFetcher::EXPLICIT_DEFAULT_PROVIDER, true);
+ WaitForDownloadToFinish();
+ ASSERT_EQ(1, set_default_called_);
+ ASSERT_EQ(0, add_provider_called_);
+ ASSERT_EQ(1, callbacks_destroyed_);
+
+ ASSERT_TRUE(last_callback_template_url_.get());
+ EXPECT_STREQ(L"http://example.com/%s/other_stuff",
+ last_callback_template_url_->url()->DisplayURL().c_str());
+ EXPECT_EQ(false, last_callback_template_url_->safe_for_autoreplace());
+}
+
+TEST_F(TemplateURLFetcherTest, AutodetectedBeforeLoadTest) {
+ std::wstring keyword(L"test");
+ ASSERT_FALSE(test_util_.model()->GetTemplateURLForKeyword(keyword));
+
+ std::string osdd_file_name("simple_open_search.xml");
+ StartDownload(keyword, osdd_file_name,
+ TemplateURLFetcher::AUTODETECTED_PROVIDER, true);
+ ASSERT_EQ(0, set_default_called_);
+ ASSERT_EQ(0, add_provider_called_);
+ ASSERT_EQ(1, callbacks_destroyed_);
+}
+
+TEST_F(TemplateURLFetcherTest, ExplicitBeforeLoadTest) {
+ std::wstring keyword(L"test");
+ ASSERT_FALSE(test_util_.model()->GetTemplateURLForKeyword(keyword));
+
+ std::string osdd_file_name("simple_open_search.xml");
+ StartDownload(keyword, osdd_file_name,
+ TemplateURLFetcher::EXPLICIT_PROVIDER, true);
+ ASSERT_EQ(0, set_default_called_);
+ ASSERT_EQ(0, add_provider_called_);
+ ASSERT_EQ(1, callbacks_destroyed_);
}