This source file includes following definitions.
- notified
- clear_notified
- observe_nav_start_
- TestGoogleURLTrackerNavigationHelper
- SetGoogleURLTracker
- SetListeningForNavigationStart
- IsListeningForNavigationStart
- SetListeningForNavigationCommit
- IsListeningForNavigationCommit
- SetListeningForTabDestruction
- IsListeningForTabDestruction
- fetched_google_url
- set_google_url
- google_url
- observer_notified
- clear_observer_notified
- OnInfoBarClosed
- SetUp
- TearDown
- GetFetcher
- MockSearchDomainCheckResponse
- RequestServerCheck
- FinishSleep
- NotifyIPAddressChanged
- SetLastPromptedGoogleURL
- GetLastPromptedGoogleURL
- SetNavigationPending
- CommitNonSearch
- CommitSearch
- CloseTab
- GetMapEntry
- GetInfoBarDelegate
- ExpectDefaultURLs
- ExpectListeningForCommit
- CreateTestInfoBar
- Create
- infobar_service_
- Update
- Close
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/google/google_url_tracker.h"
#include <set>
#include <string>
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/google/google_url_tracker_factory.h"
#include "chrome/browser/google/google_url_tracker_infobar_delegate.h"
#include "chrome/browser/google/google_url_tracker_navigation_helper.h"
#include "chrome/browser/infobars/infobar.h"
#include "chrome/browser/infobars/infobar_delegate.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_fetcher.h"
#include "testing/gtest/include/gtest/gtest.h"
class GoogleURLTrackerTest;
namespace {
class TestInfoBarDelegate : public GoogleURLTrackerInfoBarDelegate {
public:
static InfoBar* Create(GoogleURLTrackerTest* test_harness,
InfoBarService* infobar_service,
GoogleURLTracker* google_url_tracker,
const GURL& search_url);
private:
TestInfoBarDelegate(GoogleURLTrackerTest* test_harness,
InfoBarService* infobar_service,
GoogleURLTracker* google_url_tracker,
const GURL& search_url);
virtual ~TestInfoBarDelegate();
virtual void Update(const GURL& search_url) OVERRIDE;
virtual void Close(bool redo_search) OVERRIDE;
GoogleURLTrackerTest* test_harness_;
InfoBarService* infobar_service_;
DISALLOW_COPY_AND_ASSIGN(TestInfoBarDelegate);
};
class TestNotificationObserver : public content::NotificationObserver {
public:
TestNotificationObserver();
virtual ~TestNotificationObserver();
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
bool notified() const { return notified_; }
void clear_notified() { notified_ = false; }
private:
bool notified_;
};
TestNotificationObserver::TestNotificationObserver() : notified_(false) {
}
TestNotificationObserver::~TestNotificationObserver() {
}
void TestNotificationObserver::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
notified_ = true;
}
class TestGoogleURLTrackerNavigationHelper
: public GoogleURLTrackerNavigationHelper {
public:
TestGoogleURLTrackerNavigationHelper();
virtual ~TestGoogleURLTrackerNavigationHelper();
virtual void SetGoogleURLTracker(GoogleURLTracker* tracker) OVERRIDE;
virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
virtual bool IsListeningForNavigationStart() OVERRIDE;
virtual void SetListeningForNavigationCommit(
const content::NavigationController* nav_controller,
bool listen) OVERRIDE;
virtual bool IsListeningForNavigationCommit(
const content::NavigationController* nav_controller) OVERRIDE;
virtual void SetListeningForTabDestruction(
const content::NavigationController* nav_controller,
bool listen) OVERRIDE;
virtual bool IsListeningForTabDestruction(
const content::NavigationController* nav_controller) OVERRIDE;
private:
GoogleURLTracker* tracker_;
bool observe_nav_start_;
std::set<const content::NavigationController*>
nav_controller_commit_listeners_;
std::set<const content::NavigationController*>
nav_controller_tab_close_listeners_;
};
TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper()
: tracker_(NULL),
observe_nav_start_(false) {
}
TestGoogleURLTrackerNavigationHelper::
~TestGoogleURLTrackerNavigationHelper() {
}
void TestGoogleURLTrackerNavigationHelper::SetGoogleURLTracker(
GoogleURLTracker* tracker) {
tracker_ = tracker;
}
void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationStart(
bool listen) {
observe_nav_start_ = listen;
}
bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationStart() {
return observe_nav_start_;
}
void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationCommit(
const content::NavigationController* nav_controller,
bool listen) {
if (listen)
nav_controller_commit_listeners_.insert(nav_controller);
else
nav_controller_commit_listeners_.erase(nav_controller);
}
bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit(
const content::NavigationController* nav_controller) {
return nav_controller_commit_listeners_.count(nav_controller) > 0;
}
void TestGoogleURLTrackerNavigationHelper::SetListeningForTabDestruction(
const content::NavigationController* nav_controller,
bool listen) {
if (listen)
nav_controller_tab_close_listeners_.insert(nav_controller);
else
nav_controller_tab_close_listeners_.erase(nav_controller);
}
bool TestGoogleURLTrackerNavigationHelper::IsListeningForTabDestruction(
const content::NavigationController* nav_controller) {
return nav_controller_tab_close_listeners_.count(nav_controller) > 0;
}
}
class GoogleURLTrackerTest : public testing::Test {
public:
void OnInfoBarClosed(scoped_ptr<InfoBar> infobar,
InfoBarService* infobar_service);
protected:
GoogleURLTrackerTest();
virtual ~GoogleURLTrackerTest();
virtual void SetUp() OVERRIDE;
virtual void TearDown() OVERRIDE;
net::TestURLFetcher* GetFetcher();
void MockSearchDomainCheckResponse(const std::string& domain);
void RequestServerCheck();
void FinishSleep();
void NotifyIPAddressChanged();
GURL fetched_google_url() const {
return google_url_tracker_->fetched_google_url();
}
void set_google_url(const GURL& url) {
google_url_tracker_->google_url_ = url;
}
GURL google_url() const { return google_url_tracker_->google_url(); }
void SetLastPromptedGoogleURL(const GURL& url);
GURL GetLastPromptedGoogleURL();
void SetNavigationPending(intptr_t unique_id, bool is_search);
void CommitNonSearch(intptr_t unique_id);
void CommitSearch(intptr_t unique_id, const GURL& search_url);
void CloseTab(intptr_t unique_id);
GoogleURLTrackerMapEntry* GetMapEntry(intptr_t unique_id);
GoogleURLTrackerInfoBarDelegate* GetInfoBarDelegate(intptr_t unique_id);
void ExpectDefaultURLs() const;
void ExpectListeningForCommit(intptr_t unique_id, bool listening);
bool observer_notified() const { return observer_.notified(); }
void clear_observer_notified() { observer_.clear_notified(); }
private:
InfoBar* CreateTestInfoBar(InfoBarService* infobar_service,
GoogleURLTracker* google_url_tracker,
const GURL& search_url);
content::TestBrowserThreadBundle thread_bundle_;
scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
net::TestURLFetcherFactory fetcher_factory_;
content::NotificationRegistrar registrar_;
TestNotificationObserver observer_;
GoogleURLTrackerNavigationHelper* nav_helper_;
TestingProfile profile_;
scoped_ptr<GoogleURLTracker> google_url_tracker_;
std::set<int> unique_ids_seen_;
};
void GoogleURLTrackerTest::OnInfoBarClosed(scoped_ptr<InfoBar> infobar,
InfoBarService* infobar_service) {
InfoBar::RemovedDetails removed_details(infobar.get(), false);
GoogleURLTracker::EntryMap::const_iterator i =
google_url_tracker_->entry_map_.find(infobar_service);
ASSERT_FALSE(i == google_url_tracker_->entry_map_.end());
GoogleURLTrackerMapEntry* map_entry = i->second;
ASSERT_EQ(infobar->delegate(), map_entry->infobar_delegate());
map_entry->Observe(
chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
content::Source<InfoBarService>(infobar_service),
content::Details<InfoBar::RemovedDetails>(&removed_details));
}
GoogleURLTrackerTest::GoogleURLTrackerTest()
: thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
GoogleURLTrackerFactory::GetInstance()->
RegisterUserPrefsOnBrowserContextForTest(&profile_);
}
GoogleURLTrackerTest::~GoogleURLTrackerTest() {
}
void GoogleURLTrackerTest::SetUp() {
network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock());
nav_helper_ = new TestGoogleURLTrackerNavigationHelper();
scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_);
google_url_tracker_.reset(
new GoogleURLTracker(&profile_, nav_helper.Pass(),
GoogleURLTracker::UNIT_TEST_MODE));
google_url_tracker_->infobar_creator_ = base::Bind(
&GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this));
}
void GoogleURLTrackerTest::TearDown() {
while (!unique_ids_seen_.empty())
CloseTab(*unique_ids_seen_.begin());
nav_helper_ = NULL;
google_url_tracker_.reset();
network_change_notifier_.reset();
}
net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() {
return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1);
}
void GoogleURLTrackerTest::MockSearchDomainCheckResponse(
const std::string& domain) {
net::TestURLFetcher* fetcher = GetFetcher();
if (!fetcher)
return;
fetcher_factory_.RemoveFetcherFromMap(fetcher->id());
fetcher->set_url(GURL(GoogleURLTracker::kSearchDomainCheckURL));
fetcher->set_response_code(200);
fetcher->SetResponseString(domain);
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
void GoogleURLTrackerTest::RequestServerCheck() {
if (!registrar_.IsRegistered(&observer_,
chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
content::Source<Profile>(&profile_))) {
registrar_.Add(&observer_, chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
content::Source<Profile>(&profile_));
}
google_url_tracker_->SetNeedToFetch();
}
void GoogleURLTrackerTest::FinishSleep() {
google_url_tracker_->FinishSleep();
}
void GoogleURLTrackerTest::NotifyIPAddressChanged() {
net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
base::MessageLoop::current()->RunUntilIdle();
}
void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) {
profile_.GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, url.spec());
}
GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() {
return GURL(profile_.GetPrefs()->GetString(prefs::kLastPromptedGoogleURL));
}
void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id,
bool is_search) {
if (is_search) {
google_url_tracker_->SearchCommitted();
}
unique_ids_seen_.insert(unique_id);
if (nav_helper_->IsListeningForNavigationStart()) {
google_url_tracker_->OnNavigationPending(
reinterpret_cast<content::NavigationController*>(unique_id),
reinterpret_cast<InfoBarService*>(unique_id), unique_id);
}
}
void GoogleURLTrackerTest::CommitNonSearch(intptr_t unique_id) {
GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
if (!map_entry)
return;
ExpectListeningForCommit(unique_id, false);
ASSERT_TRUE(map_entry->has_infobar_delegate());
EXPECT_EQ(0, map_entry->infobar_delegate()->pending_id());
map_entry->infobar_delegate()->Close(false);
}
void GoogleURLTrackerTest::CommitSearch(intptr_t unique_id,
const GURL& search_url) {
DCHECK(search_url.is_valid());
if (nav_helper_->IsListeningForNavigationCommit(
reinterpret_cast<content::NavigationController*>(unique_id))) {
google_url_tracker_->OnNavigationCommitted(
reinterpret_cast<InfoBarService*>(unique_id), search_url);
}
}
void GoogleURLTrackerTest::CloseTab(intptr_t unique_id) {
unique_ids_seen_.erase(unique_id);
content::NavigationController* nav_controller =
reinterpret_cast<content::NavigationController*>(unique_id);
if (nav_helper_->IsListeningForTabDestruction(nav_controller)) {
google_url_tracker_->OnTabClosed(nav_controller);
} else {
GoogleURLTrackerInfoBarDelegate* delegate = GetInfoBarDelegate(unique_id);
if (delegate)
delegate->Close(false);
}
}
GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry(
intptr_t unique_id) {
GoogleURLTracker::EntryMap::const_iterator i =
google_url_tracker_->entry_map_.find(
reinterpret_cast<InfoBarService*>(unique_id));
return (i == google_url_tracker_->entry_map_.end()) ? NULL : i->second;
}
GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::GetInfoBarDelegate(
intptr_t unique_id) {
GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
return map_entry ? map_entry->infobar_delegate() : NULL;
}
void GoogleURLTrackerTest::ExpectDefaultURLs() const {
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_EQ(GURL(), fetched_google_url());
}
void GoogleURLTrackerTest::ExpectListeningForCommit(intptr_t unique_id,
bool listening) {
GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
if (map_entry) {
EXPECT_EQ(listening, nav_helper_->IsListeningForNavigationCommit(
map_entry->navigation_controller()));
} else {
EXPECT_FALSE(listening);
}
}
InfoBar* GoogleURLTrackerTest::CreateTestInfoBar(
InfoBarService* infobar_service,
GoogleURLTracker* google_url_tracker,
const GURL& search_url) {
return TestInfoBarDelegate::Create(this, infobar_service, google_url_tracker,
search_url);
}
namespace {
InfoBar* TestInfoBarDelegate::Create(GoogleURLTrackerTest* test_harness,
InfoBarService* infobar_service,
GoogleURLTracker* google_url_tracker,
const GURL& search_url) {
return ConfirmInfoBarDelegate::CreateInfoBar(
scoped_ptr<ConfirmInfoBarDelegate>(new TestInfoBarDelegate(
test_harness, infobar_service, google_url_tracker,
search_url))).release();
}
TestInfoBarDelegate::TestInfoBarDelegate(GoogleURLTrackerTest* test_harness,
InfoBarService* infobar_service,
GoogleURLTracker* google_url_tracker,
const GURL& search_url)
: GoogleURLTrackerInfoBarDelegate(google_url_tracker, search_url),
test_harness_(test_harness),
infobar_service_(infobar_service) {
}
TestInfoBarDelegate::~TestInfoBarDelegate() {
}
void TestInfoBarDelegate::Update(const GURL& search_url) {
set_search_url(search_url);
set_pending_id(0);
}
void TestInfoBarDelegate::Close(bool redo_search) {
test_harness_->OnInfoBarClosed(scoped_ptr<InfoBar>(infobar()),
infobar_service_);
}
}
TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) {
ExpectDefaultURLs();
FinishSleep();
EXPECT_FALSE(GetFetcher());
MockSearchDomainCheckResponse("http://www.google.co.uk/");
ExpectDefaultURLs();
EXPECT_FALSE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, UpdateOnFirstRun) {
RequestServerCheck();
EXPECT_FALSE(GetFetcher());
ExpectDefaultURLs();
EXPECT_FALSE(observer_notified());
FinishSleep();
MockSearchDomainCheckResponse("http://www.google.co.uk/");
EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
EXPECT_TRUE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, DontUpdateWhenUnchanged) {
SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
RequestServerCheck();
EXPECT_FALSE(GetFetcher());
ExpectDefaultURLs();
EXPECT_FALSE(observer_notified());
FinishSleep();
MockSearchDomainCheckResponse("http://www.google.co.uk/");
EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_FALSE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, DontPromptOnBadReplies) {
SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
RequestServerCheck();
EXPECT_FALSE(GetFetcher());
ExpectDefaultURLs();
EXPECT_FALSE(observer_notified());
FinishSleep();
MockSearchDomainCheckResponse(".google.co.in");
EXPECT_EQ(GURL(), fetched_google_url());
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_FALSE(observer_notified());
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
EXPECT_TRUE(GetMapEntry(1) == NULL);
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://mail.google.com/");
EXPECT_EQ(GURL(), fetched_google_url());
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_FALSE(observer_notified());
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
EXPECT_TRUE(GetMapEntry(1) == NULL);
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://www.google.com/search");
EXPECT_EQ(GURL(), fetched_google_url());
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_FALSE(observer_notified());
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
EXPECT_TRUE(GetMapEntry(1) == NULL);
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://www.google.com/?q=foo");
EXPECT_EQ(GURL(), fetched_google_url());
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_FALSE(observer_notified());
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
EXPECT_TRUE(GetMapEntry(1) == NULL);
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://www.google.com/#anchor");
EXPECT_EQ(GURL(), fetched_google_url());
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_FALSE(observer_notified());
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
EXPECT_TRUE(GetMapEntry(1) == NULL);
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("HJ)*qF)_*&@f1");
EXPECT_EQ(GURL(), fetched_google_url());
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_FALSE(observer_notified());
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
EXPECT_TRUE(GetMapEntry(1) == NULL);
}
TEST_F(GoogleURLTrackerTest, UpdatePromptedURLOnReturnToPreviousLocation) {
SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/"));
set_google_url(GURL("http://www.google.co.uk/"));
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse("http://www.google.co.uk/");
EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_FALSE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, SilentlyAcceptSchemeChange) {
SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/"));
set_google_url(GURL("http://www.google.co.uk/"));
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse("https://www.google.co.uk/");
EXPECT_EQ(GURL("https://www.google.co.uk/"), fetched_google_url());
EXPECT_EQ(GURL("https://www.google.co.uk/"), google_url());
EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_TRUE(observer_notified());
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://www.google.co.uk/");
EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_TRUE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, RefetchOnIPAddressChange) {
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse("http://www.google.co.uk/");
EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
EXPECT_TRUE(observer_notified());
clear_observer_notified();
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://www.google.co.in/");
EXPECT_EQ(GURL("http://www.google.co.in/"), fetched_google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
EXPECT_FALSE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, DontRefetchWhenNoOneRequestsCheck) {
FinishSleep();
NotifyIPAddressChanged();
EXPECT_FALSE(GetFetcher());
MockSearchDomainCheckResponse("http://www.google.co.uk/");
ExpectDefaultURLs();
EXPECT_FALSE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, FetchOnLateRequest) {
FinishSleep();
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://www.google.co.jp/");
RequestServerCheck();
MockSearchDomainCheckResponse("http://www.google.co.uk/");
EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
EXPECT_TRUE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, DontFetchTwiceOnLateRequests) {
FinishSleep();
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://www.google.co.jp/");
RequestServerCheck();
MockSearchDomainCheckResponse("http://www.google.co.uk/");
EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
EXPECT_TRUE(observer_notified());
clear_observer_notified();
RequestServerCheck();
EXPECT_FALSE(GetFetcher());
MockSearchDomainCheckResponse("http://www.google.co.in/");
EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
EXPECT_FALSE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, SearchingDoesNothingIfNoNeedToPrompt) {
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse("http://www.google.co.uk/");
EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_TRUE(observer_notified());
clear_observer_notified();
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
EXPECT_TRUE(GetMapEntry(1) == NULL);
EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_FALSE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, TabClosedOnPendingSearch) {
SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse("http://www.google.co.jp/");
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_EQ(GURL("http://www.google.co.jp/"), fetched_google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_FALSE(observer_notified());
SetNavigationPending(1, true);
GoogleURLTrackerMapEntry* map_entry = GetMapEntry(1);
ASSERT_FALSE(map_entry == NULL);
EXPECT_FALSE(map_entry->has_infobar_delegate());
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_FALSE(observer_notified());
CloseTab(1);
EXPECT_TRUE(GetMapEntry(1) == NULL);
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_FALSE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, TabClosedOnCommittedSearch) {
SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse("http://www.google.co.jp/");
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
CloseTab(1);
EXPECT_TRUE(GetMapEntry(1) == NULL);
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_FALSE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, InfoBarClosed) {
SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse("http://www.google.co.jp/");
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
GoogleURLTrackerInfoBarDelegate* infobar = GetInfoBarDelegate(1);
ASSERT_FALSE(infobar == NULL);
infobar->Close(false);
EXPECT_TRUE(GetMapEntry(1) == NULL);
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_FALSE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, InfoBarRefused) {
SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse("http://www.google.co.jp/");
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
GoogleURLTrackerInfoBarDelegate* infobar = GetInfoBarDelegate(1);
ASSERT_FALSE(infobar == NULL);
infobar->Cancel();
EXPECT_TRUE(GetMapEntry(1) == NULL);
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
EXPECT_FALSE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, InfoBarAccepted) {
SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse("http://www.google.co.jp/");
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
GoogleURLTrackerInfoBarDelegate* infobar = GetInfoBarDelegate(1);
ASSERT_FALSE(infobar == NULL);
infobar->Accept();
EXPECT_TRUE(GetMapEntry(1) == NULL);
EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url());
EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
EXPECT_TRUE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, FetchesCanAutomaticallyCloseInfoBars) {
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse(google_url().spec());
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://www.google.co.uk/");
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.com/search?q=test"));
EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
NotifyIPAddressChanged();
MockSearchDomainCheckResponse(google_url().spec());
EXPECT_EQ(google_url(), GetLastPromptedGoogleURL());
EXPECT_TRUE(GetMapEntry(1) == NULL);
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://www.google.co.uk/");
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.com/search?q=test"));
EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
NotifyIPAddressChanged();
url_canon::Replacements<char> replacements;
const std::string& scheme("https");
replacements.SetScheme(scheme.data(),
url_parse::Component(0, scheme.length()));
GURL new_google_url(google_url().ReplaceComponents(replacements));
MockSearchDomainCheckResponse(new_google_url.spec());
EXPECT_EQ(new_google_url, GetLastPromptedGoogleURL());
EXPECT_TRUE(GetMapEntry(1) == NULL);
SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://www.google.co.jp/");
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.com/search?q=test"));
EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://www.google.co.uk/");
EXPECT_EQ(new_google_url, google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_TRUE(GetMapEntry(1) == NULL);
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://www.google.co.jp/");
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.com/search?q=test"));
EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("https://www.google.co.uk/");
EXPECT_EQ(new_google_url, google_url());
EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_TRUE(GetMapEntry(1) == NULL);
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://www.google.co.jp/");
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.com/search?q=test"));
EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("https://www.google.co.in/");
EXPECT_EQ(new_google_url, google_url());
EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_TRUE(GetMapEntry(1) == NULL);
}
TEST_F(GoogleURLTrackerTest, ResetInfoBarGoogleURLs) {
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse(google_url().spec());
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("http://www.google.co.uk/");
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.com/search?q=test"));
GoogleURLTrackerInfoBarDelegate* delegate = GetInfoBarDelegate(1);
ASSERT_FALSE(delegate == NULL);
EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
NotifyIPAddressChanged();
MockSearchDomainCheckResponse("https://www.google.co.uk/");
EXPECT_EQ(delegate, GetInfoBarDelegate(1));
EXPECT_EQ(GURL("https://www.google.co.uk/"), fetched_google_url());
}
TEST_F(GoogleURLTrackerTest, NavigationsAfterPendingSearch) {
SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse("http://www.google.co.jp/");
SetNavigationPending(1, true);
GoogleURLTrackerMapEntry* map_entry = GetMapEntry(1);
ASSERT_FALSE(map_entry == NULL);
EXPECT_FALSE(map_entry->has_infobar_delegate());
SetNavigationPending(1, false);
EXPECT_TRUE(GetMapEntry(1) == NULL);
SetNavigationPending(1, true);
map_entry = GetMapEntry(1);
ASSERT_FALSE(map_entry == NULL);
EXPECT_FALSE(map_entry->has_infobar_delegate());
SetNavigationPending(1, true);
ASSERT_EQ(map_entry, GetMapEntry(1));
EXPECT_FALSE(map_entry->has_infobar_delegate());
ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test2"));
EXPECT_TRUE(map_entry->has_infobar_delegate());
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_FALSE(observer_notified());
ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
}
TEST_F(GoogleURLTrackerTest, NavigationsAfterCommittedSearch) {
SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse("http://www.google.co.jp/");
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
GoogleURLTrackerInfoBarDelegate* delegate = GetInfoBarDelegate(1);
ASSERT_FALSE(delegate == NULL);
ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
SetNavigationPending(1, false);
ASSERT_EQ(delegate, GetInfoBarDelegate(1));
EXPECT_EQ(0, delegate->pending_id());
ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
SetNavigationPending(1, false);
ASSERT_EQ(delegate, GetInfoBarDelegate(1));
EXPECT_EQ(0, delegate->pending_id());
ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
ASSERT_NO_FATAL_FAILURE(CommitNonSearch(1));
EXPECT_TRUE(GetMapEntry(1) == NULL);
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
delegate = GetInfoBarDelegate(1);
ASSERT_FALSE(delegate == NULL);
SetNavigationPending(1, true);
ASSERT_EQ(delegate, GetInfoBarDelegate(1));
EXPECT_EQ(1, delegate->pending_id());
ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
SetNavigationPending(1, false);
ASSERT_EQ(delegate, GetInfoBarDelegate(1));
EXPECT_EQ(0, delegate->pending_id());
ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
SetNavigationPending(1, true);
ASSERT_EQ(delegate, GetInfoBarDelegate(1));
EXPECT_EQ(1, delegate->pending_id());
ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
SetNavigationPending(1, true);
ASSERT_EQ(delegate, GetInfoBarDelegate(1));
EXPECT_EQ(1, delegate->pending_id());
ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test2"));
ASSERT_EQ(delegate, GetInfoBarDelegate(1));
EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test2"),
delegate->search_url());
EXPECT_EQ(0, delegate->pending_id());
ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
EXPECT_FALSE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, MultipleMapEntries) {
SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse("http://www.google.co.jp/");
SetNavigationPending(1, true);
GoogleURLTrackerMapEntry* map_entry = GetMapEntry(1);
ASSERT_FALSE(map_entry == NULL);
EXPECT_FALSE(map_entry->has_infobar_delegate());
SetNavigationPending(2, true);
CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2"));
GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2);
ASSERT_FALSE(delegate2 == NULL);
EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test2"),
delegate2->search_url());
SetNavigationPending(3, true);
GoogleURLTrackerMapEntry* map_entry3 = GetMapEntry(3);
ASSERT_FALSE(map_entry3 == NULL);
EXPECT_FALSE(map_entry3->has_infobar_delegate());
SetNavigationPending(4, true);
CommitSearch(4, GURL("http://www.google.co.uk/search?q=test4"));
GoogleURLTrackerInfoBarDelegate* delegate4 = GetInfoBarDelegate(4);
ASSERT_FALSE(delegate4 == NULL);
EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test4"),
delegate4->search_url());
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
EXPECT_TRUE(map_entry->has_infobar_delegate());
delegate2->Close(false);
EXPECT_TRUE(GetMapEntry(2) == NULL);
EXPECT_FALSE(observer_notified());
delegate4->Accept();
EXPECT_TRUE(GetMapEntry(1) == NULL);
EXPECT_TRUE(GetMapEntry(3) == NULL);
EXPECT_TRUE(GetMapEntry(4) == NULL);
EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url());
EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
EXPECT_TRUE(observer_notified());
}
TEST_F(GoogleURLTrackerTest, IgnoreIrrelevantNavigation) {
SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
RequestServerCheck();
FinishSleep();
MockSearchDomainCheckResponse("http://www.google.co.jp/");
SetNavigationPending(1, true);
CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
SetNavigationPending(2, true);
CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2"));
EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2);
ASSERT_FALSE(delegate2 == NULL);
SetNavigationPending(1, true);
ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
delegate2->Close(false);
SetNavigationPending(1, false);
ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
}