| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/google/core/browser/google_url_tracker.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/prefs/pref_service.h" | |
| 13 #include "chrome/browser/google/google_url_tracker_factory.h" | |
| 14 #include "chrome/test/base/testing_profile.h" | |
| 15 #include "components/google/core/browser/google_pref_names.h" | |
| 16 #include "components/google/core/browser/google_url_tracker_client.h" | |
| 17 #include "components/google/core/browser/google_url_tracker_infobar_delegate.h" | |
| 18 #include "components/google/core/browser/google_url_tracker_navigation_helper.h" | |
| 19 #include "components/infobars/core/infobar.h" | |
| 20 #include "components/infobars/core/infobar_delegate.h" | |
| 21 #include "content/public/test/test_browser_thread_bundle.h" | |
| 22 #include "net/url_request/test_url_fetcher_factory.h" | |
| 23 #include "net/url_request/url_fetcher.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 // TestCallbackListener --------------------------------------------------- | |
| 29 | |
| 30 class TestCallbackListener { | |
| 31 public: | |
| 32 TestCallbackListener(); | |
| 33 virtual ~TestCallbackListener(); | |
| 34 | |
| 35 bool HasRegisteredCallback(); | |
| 36 void RegisterCallback(GoogleURLTracker* google_url_tracker); | |
| 37 | |
| 38 bool notified() const { return notified_; } | |
| 39 void clear_notified() { notified_ = false; } | |
| 40 | |
| 41 private: | |
| 42 void OnGoogleURLUpdated(); | |
| 43 | |
| 44 bool notified_; | |
| 45 scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_; | |
| 46 }; | |
| 47 | |
| 48 TestCallbackListener::TestCallbackListener() : notified_(false) { | |
| 49 } | |
| 50 | |
| 51 TestCallbackListener::~TestCallbackListener() { | |
| 52 } | |
| 53 | |
| 54 void TestCallbackListener::OnGoogleURLUpdated() { | |
| 55 notified_ = true; | |
| 56 } | |
| 57 | |
| 58 bool TestCallbackListener::HasRegisteredCallback() { | |
| 59 return google_url_updated_subscription_.get(); | |
| 60 } | |
| 61 | |
| 62 void TestCallbackListener::RegisterCallback( | |
| 63 GoogleURLTracker* google_url_tracker) { | |
| 64 google_url_updated_subscription_ = | |
| 65 google_url_tracker->RegisterCallback(base::Bind( | |
| 66 &TestCallbackListener::OnGoogleURLUpdated, base::Unretained(this))); | |
| 67 } | |
| 68 | |
| 69 | |
| 70 // TestGoogleURLTrackerClient ------------------------------------------------- | |
| 71 | |
| 72 class TestGoogleURLTrackerClient : public GoogleURLTrackerClient { | |
| 73 public: | |
| 74 explicit TestGoogleURLTrackerClient(Profile* profile_); | |
| 75 virtual ~TestGoogleURLTrackerClient(); | |
| 76 | |
| 77 virtual void SetListeningForNavigationStart(bool listen) OVERRIDE; | |
| 78 virtual bool IsListeningForNavigationStart() OVERRIDE; | |
| 79 virtual bool IsBackgroundNetworkingEnabled() OVERRIDE; | |
| 80 virtual PrefService* GetPrefs() OVERRIDE; | |
| 81 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE; | |
| 82 | |
| 83 private: | |
| 84 Profile* profile_; | |
| 85 bool observe_nav_start_; | |
| 86 | |
| 87 DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerClient); | |
| 88 }; | |
| 89 | |
| 90 TestGoogleURLTrackerClient::TestGoogleURLTrackerClient(Profile* profile) | |
| 91 : profile_(profile), | |
| 92 observe_nav_start_(false) { | |
| 93 } | |
| 94 | |
| 95 TestGoogleURLTrackerClient::~TestGoogleURLTrackerClient() { | |
| 96 } | |
| 97 | |
| 98 void TestGoogleURLTrackerClient::SetListeningForNavigationStart(bool listen) { | |
| 99 observe_nav_start_ = listen; | |
| 100 } | |
| 101 | |
| 102 bool TestGoogleURLTrackerClient::IsListeningForNavigationStart() { | |
| 103 return observe_nav_start_; | |
| 104 } | |
| 105 | |
| 106 bool TestGoogleURLTrackerClient::IsBackgroundNetworkingEnabled() { | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 PrefService* TestGoogleURLTrackerClient::GetPrefs() { | |
| 111 return profile_->GetPrefs(); | |
| 112 } | |
| 113 | |
| 114 net::URLRequestContextGetter* TestGoogleURLTrackerClient::GetRequestContext() { | |
| 115 return profile_->GetRequestContext(); | |
| 116 } | |
| 117 | |
| 118 | |
| 119 // TestGoogleURLTrackerNavigationHelper --------------------------------------- | |
| 120 | |
| 121 class TestGoogleURLTrackerNavigationHelper | |
| 122 : public GoogleURLTrackerNavigationHelper { | |
| 123 public: | |
| 124 explicit TestGoogleURLTrackerNavigationHelper(GoogleURLTracker* tracker); | |
| 125 virtual ~TestGoogleURLTrackerNavigationHelper(); | |
| 126 | |
| 127 virtual void SetListeningForNavigationCommit(bool listen) OVERRIDE; | |
| 128 virtual bool IsListeningForNavigationCommit() OVERRIDE; | |
| 129 virtual void SetListeningForTabDestruction(bool listen) OVERRIDE; | |
| 130 virtual bool IsListeningForTabDestruction() OVERRIDE; | |
| 131 virtual void OpenURL(GURL url, | |
| 132 WindowOpenDisposition disposition, | |
| 133 bool user_clicked_on_link) OVERRIDE; | |
| 134 | |
| 135 private: | |
| 136 bool listening_for_nav_commit_; | |
| 137 bool listening_for_tab_destruction_; | |
| 138 | |
| 139 DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerNavigationHelper); | |
| 140 }; | |
| 141 | |
| 142 TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper( | |
| 143 GoogleURLTracker* tracker) | |
| 144 : GoogleURLTrackerNavigationHelper(tracker), | |
| 145 listening_for_nav_commit_(false), | |
| 146 listening_for_tab_destruction_(false) { | |
| 147 } | |
| 148 | |
| 149 TestGoogleURLTrackerNavigationHelper::~TestGoogleURLTrackerNavigationHelper() { | |
| 150 } | |
| 151 | |
| 152 void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationCommit( | |
| 153 bool listen) { | |
| 154 listening_for_nav_commit_ = listen; | |
| 155 } | |
| 156 | |
| 157 bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit() { | |
| 158 return listening_for_nav_commit_; | |
| 159 } | |
| 160 | |
| 161 void TestGoogleURLTrackerNavigationHelper::SetListeningForTabDestruction( | |
| 162 bool listen) { | |
| 163 listening_for_tab_destruction_ = listen; | |
| 164 } | |
| 165 | |
| 166 bool TestGoogleURLTrackerNavigationHelper::IsListeningForTabDestruction() { | |
| 167 return listening_for_tab_destruction_; | |
| 168 } | |
| 169 | |
| 170 void TestGoogleURLTrackerNavigationHelper::OpenURL( | |
| 171 GURL url, | |
| 172 WindowOpenDisposition disposition, | |
| 173 bool user_clicked_on_link) { | |
| 174 } | |
| 175 | |
| 176 // TestInfoBarManager --------------------------------------------------------- | |
| 177 | |
| 178 class TestInfoBarManager : public infobars::InfoBarManager { | |
| 179 public: | |
| 180 explicit TestInfoBarManager(int unique_id); | |
| 181 virtual ~TestInfoBarManager(); | |
| 182 virtual int GetActiveEntryID() OVERRIDE; | |
| 183 | |
| 184 private: | |
| 185 int unique_id_; | |
| 186 DISALLOW_COPY_AND_ASSIGN(TestInfoBarManager); | |
| 187 }; | |
| 188 | |
| 189 TestInfoBarManager::TestInfoBarManager(int unique_id) : unique_id_(unique_id) { | |
| 190 } | |
| 191 | |
| 192 TestInfoBarManager::~TestInfoBarManager() { | |
| 193 ShutDown(); | |
| 194 } | |
| 195 | |
| 196 int TestInfoBarManager::GetActiveEntryID() { | |
| 197 return unique_id_; | |
| 198 } | |
| 199 | |
| 200 } // namespace | |
| 201 | |
| 202 // GoogleURLTrackerTest ------------------------------------------------------- | |
| 203 | |
| 204 class GoogleURLTrackerTest : public testing::Test { | |
| 205 protected: | |
| 206 GoogleURLTrackerTest(); | |
| 207 virtual ~GoogleURLTrackerTest(); | |
| 208 | |
| 209 // testing::Test | |
| 210 virtual void SetUp() OVERRIDE; | |
| 211 virtual void TearDown() OVERRIDE; | |
| 212 | |
| 213 net::TestURLFetcher* GetFetcher(); | |
| 214 void MockSearchDomainCheckResponse(const std::string& domain); | |
| 215 void RequestServerCheck(); | |
| 216 void FinishSleep(); | |
| 217 void NotifyNetworkChanged(); | |
| 218 GURL fetched_google_url() const { | |
| 219 return google_url_tracker_->fetched_google_url(); | |
| 220 } | |
| 221 void set_google_url(const GURL& url) { | |
| 222 google_url_tracker_->google_url_ = url; | |
| 223 } | |
| 224 GURL google_url() const { return google_url_tracker_->google_url(); } | |
| 225 void SetLastPromptedGoogleURL(const GURL& url); | |
| 226 GURL GetLastPromptedGoogleURL(); | |
| 227 void SetNavigationPending(infobars::InfoBarManager* infobar_manager, | |
| 228 bool is_search); | |
| 229 void CommitNonSearch(infobars::InfoBarManager* infobar_manager); | |
| 230 void CommitSearch(infobars::InfoBarManager* infobar_manager, | |
| 231 const GURL& search_url); | |
| 232 void CloseTab(infobars::InfoBarManager* infobar_manager); | |
| 233 GoogleURLTrackerMapEntry* GetMapEntry( | |
| 234 infobars::InfoBarManager* infobar_manager); | |
| 235 GoogleURLTrackerInfoBarDelegate* GetInfoBarDelegate( | |
| 236 infobars::InfoBarManager* infobar_manager); | |
| 237 GoogleURLTrackerNavigationHelper* GetNavigationHelper( | |
| 238 infobars::InfoBarManager* infobar_manager); | |
| 239 void ExpectDefaultURLs() const; | |
| 240 void ExpectListeningForCommit(infobars::InfoBarManager* infobar_manager, | |
| 241 bool listening); | |
| 242 bool listener_notified() const { return listener_.notified(); } | |
| 243 void clear_listener_notified() { listener_.clear_notified(); } | |
| 244 | |
| 245 private: | |
| 246 // These are required by the TestURLFetchers GoogleURLTracker will create (see | |
| 247 // test_url_fetcher_factory.h). | |
| 248 content::TestBrowserThreadBundle thread_bundle_; | |
| 249 | |
| 250 // Creating this allows us to call | |
| 251 // net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(). | |
| 252 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | |
| 253 net::TestURLFetcherFactory fetcher_factory_; | |
| 254 GoogleURLTrackerClient* client_; | |
| 255 TestingProfile profile_; | |
| 256 scoped_ptr<GoogleURLTracker> google_url_tracker_; | |
| 257 TestCallbackListener listener_; | |
| 258 // This tracks the different "tabs" a test has "opened", so we can close them | |
| 259 // properly before shutting down |google_url_tracker_|, which expects that. | |
| 260 std::set<infobars::InfoBarManager*> infobar_managers_seen_; | |
| 261 }; | |
| 262 | |
| 263 GoogleURLTrackerTest::GoogleURLTrackerTest() | |
| 264 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | |
| 265 GoogleURLTrackerFactory::GetInstance()-> | |
| 266 RegisterUserPrefsOnBrowserContextForTest(&profile_); | |
| 267 } | |
| 268 | |
| 269 GoogleURLTrackerTest::~GoogleURLTrackerTest() { | |
| 270 } | |
| 271 | |
| 272 void GoogleURLTrackerTest::SetUp() { | |
| 273 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); | |
| 274 // Ownership is passed to google_url_tracker_, but a weak pointer is kept; | |
| 275 // this is safe since GoogleURLTracker keeps the client for its lifetime. | |
| 276 client_ = new TestGoogleURLTrackerClient(&profile_); | |
| 277 scoped_ptr<GoogleURLTrackerClient> client(client_); | |
| 278 google_url_tracker_.reset(new GoogleURLTracker( | |
| 279 client.Pass(), GoogleURLTracker::UNIT_TEST_MODE)); | |
| 280 } | |
| 281 | |
| 282 void GoogleURLTrackerTest::TearDown() { | |
| 283 while (!infobar_managers_seen_.empty()) | |
| 284 CloseTab(*infobar_managers_seen_.begin()); | |
| 285 google_url_tracker_->Shutdown(); | |
| 286 } | |
| 287 | |
| 288 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() { | |
| 289 // This will return the last fetcher created. If no fetchers have been | |
| 290 // created, we'll pass GetFetcherByID() "-1", and it will return NULL. | |
| 291 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1); | |
| 292 } | |
| 293 | |
| 294 void GoogleURLTrackerTest::MockSearchDomainCheckResponse( | |
| 295 const std::string& domain) { | |
| 296 net::TestURLFetcher* fetcher = GetFetcher(); | |
| 297 if (!fetcher) | |
| 298 return; | |
| 299 fetcher_factory_.RemoveFetcherFromMap(fetcher->id()); | |
| 300 fetcher->set_url(GURL(GoogleURLTracker::kSearchDomainCheckURL)); | |
| 301 fetcher->set_response_code(200); | |
| 302 fetcher->SetResponseString(domain); | |
| 303 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 304 // At this point, |fetcher| is deleted. | |
| 305 } | |
| 306 | |
| 307 void GoogleURLTrackerTest::RequestServerCheck() { | |
| 308 if (!listener_.HasRegisteredCallback()) | |
| 309 listener_.RegisterCallback(google_url_tracker_.get()); | |
| 310 google_url_tracker_->SetNeedToFetch(); | |
| 311 } | |
| 312 | |
| 313 void GoogleURLTrackerTest::FinishSleep() { | |
| 314 google_url_tracker_->FinishSleep(); | |
| 315 } | |
| 316 | |
| 317 void GoogleURLTrackerTest::NotifyNetworkChanged() { | |
| 318 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | |
| 319 net::NetworkChangeNotifier::CONNECTION_UNKNOWN); | |
| 320 // For thread safety, the NCN queues tasks to do the actual notifications, so | |
| 321 // we need to spin the message loop so the tracker will actually be notified. | |
| 322 base::MessageLoop::current()->RunUntilIdle(); | |
| 323 } | |
| 324 | |
| 325 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) { | |
| 326 profile_.GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, url.spec()); | |
| 327 } | |
| 328 | |
| 329 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() { | |
| 330 return GURL(profile_.GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); | |
| 331 } | |
| 332 | |
| 333 void GoogleURLTrackerTest::SetNavigationPending( | |
| 334 infobars::InfoBarManager* infobar_manager, | |
| 335 bool is_search) { | |
| 336 if (is_search) { | |
| 337 google_url_tracker_->SearchCommitted(); | |
| 338 // Note that the call above might not have actually registered a listener | |
| 339 // for navigation starts if the searchdomaincheck response was bogus. | |
| 340 } | |
| 341 infobar_managers_seen_.insert(infobar_manager); | |
| 342 if (client_->IsListeningForNavigationStart()) { | |
| 343 google_url_tracker_->OnNavigationPending( | |
| 344 scoped_ptr<GoogleURLTrackerNavigationHelper>( | |
| 345 new TestGoogleURLTrackerNavigationHelper( | |
| 346 google_url_tracker_.get())), | |
| 347 infobar_manager, | |
| 348 infobar_manager->GetActiveEntryID()); | |
| 349 } | |
| 350 } | |
| 351 | |
| 352 void GoogleURLTrackerTest::CommitNonSearch( | |
| 353 infobars::InfoBarManager* infobar_manager) { | |
| 354 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager); | |
| 355 if (!map_entry) | |
| 356 return; | |
| 357 | |
| 358 ExpectListeningForCommit(infobar_manager, false); | |
| 359 | |
| 360 // The infobar should be showing; otherwise the pending non-search should | |
| 361 // have closed it. | |
| 362 ASSERT_TRUE(map_entry->has_infobar_delegate()); | |
| 363 | |
| 364 // The pending_id should have been reset to 0 when the non-search became | |
| 365 // pending. | |
| 366 EXPECT_EQ(0, map_entry->infobar_delegate()->pending_id()); | |
| 367 | |
| 368 // Committing the navigation would close the infobar. | |
| 369 map_entry->infobar_delegate()->Close(false); | |
| 370 } | |
| 371 | |
| 372 void GoogleURLTrackerTest::CommitSearch( | |
| 373 infobars::InfoBarManager* infobar_manager, | |
| 374 const GURL& search_url) { | |
| 375 DCHECK(search_url.is_valid()); | |
| 376 GoogleURLTrackerNavigationHelper* nav_helper = | |
| 377 GetNavigationHelper(infobar_manager); | |
| 378 if (nav_helper && nav_helper->IsListeningForNavigationCommit()) { | |
| 379 google_url_tracker_->OnNavigationCommitted(infobar_manager, search_url); | |
| 380 } | |
| 381 } | |
| 382 | |
| 383 void GoogleURLTrackerTest::CloseTab(infobars::InfoBarManager* infobar_manager) { | |
| 384 infobar_managers_seen_.erase(infobar_manager); | |
| 385 GoogleURLTrackerNavigationHelper* nav_helper = | |
| 386 GetNavigationHelper(infobar_manager); | |
| 387 if (nav_helper && nav_helper->IsListeningForTabDestruction()) { | |
| 388 google_url_tracker_->OnTabClosed(nav_helper); | |
| 389 } else { | |
| 390 // Closing a tab with an infobar showing would close the infobar. | |
| 391 GoogleURLTrackerInfoBarDelegate* delegate = | |
| 392 GetInfoBarDelegate(infobar_manager); | |
| 393 if (delegate) | |
| 394 delegate->Close(false); | |
| 395 } | |
| 396 } | |
| 397 | |
| 398 GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry( | |
| 399 infobars::InfoBarManager* infobar_manager) { | |
| 400 GoogleURLTracker::EntryMap::const_iterator i = | |
| 401 google_url_tracker_->entry_map_.find(infobar_manager); | |
| 402 return (i == google_url_tracker_->entry_map_.end()) ? NULL : i->second; | |
| 403 } | |
| 404 | |
| 405 GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::GetInfoBarDelegate( | |
| 406 infobars::InfoBarManager* infobar_manager) { | |
| 407 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager); | |
| 408 return map_entry ? map_entry->infobar_delegate() : NULL; | |
| 409 } | |
| 410 | |
| 411 GoogleURLTrackerNavigationHelper* GoogleURLTrackerTest::GetNavigationHelper( | |
| 412 infobars::InfoBarManager* infobar_manager) { | |
| 413 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager); | |
| 414 return map_entry ? map_entry->navigation_helper() : NULL; | |
| 415 } | |
| 416 | |
| 417 void GoogleURLTrackerTest::ExpectDefaultURLs() const { | |
| 418 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 419 EXPECT_EQ(GURL(), fetched_google_url()); | |
| 420 } | |
| 421 | |
| 422 void GoogleURLTrackerTest::ExpectListeningForCommit( | |
| 423 infobars::InfoBarManager* infobar_manager, | |
| 424 bool listening) { | |
| 425 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager); | |
| 426 if (map_entry) { | |
| 427 EXPECT_EQ(listening, | |
| 428 map_entry->navigation_helper()->IsListeningForNavigationCommit()); | |
| 429 } else { | |
| 430 EXPECT_FALSE(listening); | |
| 431 } | |
| 432 } | |
| 433 | |
| 434 // Tests ---------------------------------------------------------------------- | |
| 435 | |
| 436 TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) { | |
| 437 ExpectDefaultURLs(); | |
| 438 FinishSleep(); | |
| 439 // No one called RequestServerCheck() so nothing should have happened. | |
| 440 EXPECT_FALSE(GetFetcher()); | |
| 441 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
| 442 ExpectDefaultURLs(); | |
| 443 EXPECT_FALSE(listener_notified()); | |
| 444 } | |
| 445 | |
| 446 TEST_F(GoogleURLTrackerTest, UpdateOnFirstRun) { | |
| 447 RequestServerCheck(); | |
| 448 EXPECT_FALSE(GetFetcher()); | |
| 449 ExpectDefaultURLs(); | |
| 450 EXPECT_FALSE(listener_notified()); | |
| 451 | |
| 452 FinishSleep(); | |
| 453 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
| 454 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
| 455 // GoogleURL should be updated, becase there was no last prompted URL. | |
| 456 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
| 457 EXPECT_TRUE(listener_notified()); | |
| 458 } | |
| 459 | |
| 460 TEST_F(GoogleURLTrackerTest, DontUpdateWhenUnchanged) { | |
| 461 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
| 462 | |
| 463 RequestServerCheck(); | |
| 464 EXPECT_FALSE(GetFetcher()); | |
| 465 ExpectDefaultURLs(); | |
| 466 EXPECT_FALSE(listener_notified()); | |
| 467 | |
| 468 FinishSleep(); | |
| 469 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
| 470 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
| 471 // GoogleURL should not be updated, because the fetched and prompted URLs | |
| 472 // match. | |
| 473 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 474 EXPECT_FALSE(listener_notified()); | |
| 475 } | |
| 476 | |
| 477 TEST_F(GoogleURLTrackerTest, DontPromptOnBadReplies) { | |
| 478 TestInfoBarManager infobar_manager(1); | |
| 479 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
| 480 | |
| 481 RequestServerCheck(); | |
| 482 EXPECT_FALSE(GetFetcher()); | |
| 483 ExpectDefaultURLs(); | |
| 484 EXPECT_FALSE(listener_notified()); | |
| 485 | |
| 486 // Old-style domain string. | |
| 487 FinishSleep(); | |
| 488 MockSearchDomainCheckResponse(".google.co.in"); | |
| 489 EXPECT_EQ(GURL(), fetched_google_url()); | |
| 490 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 491 EXPECT_FALSE(listener_notified()); | |
| 492 SetNavigationPending(&infobar_manager, true); | |
| 493 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 494 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 495 | |
| 496 // Bad subdomain. | |
| 497 NotifyNetworkChanged(); | |
| 498 MockSearchDomainCheckResponse("http://mail.google.com/"); | |
| 499 EXPECT_EQ(GURL(), fetched_google_url()); | |
| 500 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 501 EXPECT_FALSE(listener_notified()); | |
| 502 SetNavigationPending(&infobar_manager, true); | |
| 503 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 504 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 505 | |
| 506 // Non-empty path. | |
| 507 NotifyNetworkChanged(); | |
| 508 MockSearchDomainCheckResponse("http://www.google.com/search"); | |
| 509 EXPECT_EQ(GURL(), fetched_google_url()); | |
| 510 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 511 EXPECT_FALSE(listener_notified()); | |
| 512 SetNavigationPending(&infobar_manager, true); | |
| 513 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 514 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 515 | |
| 516 // Non-empty query. | |
| 517 NotifyNetworkChanged(); | |
| 518 MockSearchDomainCheckResponse("http://www.google.com/?q=foo"); | |
| 519 EXPECT_EQ(GURL(), fetched_google_url()); | |
| 520 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 521 EXPECT_FALSE(listener_notified()); | |
| 522 SetNavigationPending(&infobar_manager, true); | |
| 523 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 524 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 525 | |
| 526 // Non-empty ref. | |
| 527 NotifyNetworkChanged(); | |
| 528 MockSearchDomainCheckResponse("http://www.google.com/#anchor"); | |
| 529 EXPECT_EQ(GURL(), fetched_google_url()); | |
| 530 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 531 EXPECT_FALSE(listener_notified()); | |
| 532 SetNavigationPending(&infobar_manager, true); | |
| 533 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 534 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 535 | |
| 536 // Complete garbage. | |
| 537 NotifyNetworkChanged(); | |
| 538 MockSearchDomainCheckResponse("HJ)*qF)_*&@f1"); | |
| 539 EXPECT_EQ(GURL(), fetched_google_url()); | |
| 540 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 541 EXPECT_FALSE(listener_notified()); | |
| 542 SetNavigationPending(&infobar_manager, true); | |
| 543 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 544 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 545 } | |
| 546 | |
| 547 TEST_F(GoogleURLTrackerTest, UpdatePromptedURLOnReturnToPreviousLocation) { | |
| 548 SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/")); | |
| 549 set_google_url(GURL("http://www.google.co.uk/")); | |
| 550 RequestServerCheck(); | |
| 551 FinishSleep(); | |
| 552 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
| 553 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
| 554 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
| 555 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 556 EXPECT_FALSE(listener_notified()); | |
| 557 } | |
| 558 | |
| 559 TEST_F(GoogleURLTrackerTest, SilentlyAcceptSchemeChange) { | |
| 560 // We should auto-accept changes to the current Google URL that merely change | |
| 561 // the scheme, regardless of what the last prompted URL was. | |
| 562 SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/")); | |
| 563 set_google_url(GURL("http://www.google.co.uk/")); | |
| 564 RequestServerCheck(); | |
| 565 FinishSleep(); | |
| 566 MockSearchDomainCheckResponse("https://www.google.co.uk/"); | |
| 567 EXPECT_EQ(GURL("https://www.google.co.uk/"), fetched_google_url()); | |
| 568 EXPECT_EQ(GURL("https://www.google.co.uk/"), google_url()); | |
| 569 EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 570 EXPECT_TRUE(listener_notified()); | |
| 571 | |
| 572 NotifyNetworkChanged(); | |
| 573 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
| 574 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
| 575 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
| 576 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 577 EXPECT_TRUE(listener_notified()); | |
| 578 } | |
| 579 | |
| 580 TEST_F(GoogleURLTrackerTest, RefetchOnNetworkChange) { | |
| 581 RequestServerCheck(); | |
| 582 FinishSleep(); | |
| 583 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
| 584 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
| 585 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
| 586 EXPECT_TRUE(listener_notified()); | |
| 587 clear_listener_notified(); | |
| 588 | |
| 589 NotifyNetworkChanged(); | |
| 590 MockSearchDomainCheckResponse("http://www.google.co.in/"); | |
| 591 EXPECT_EQ(GURL("http://www.google.co.in/"), fetched_google_url()); | |
| 592 // Just fetching a new URL shouldn't reset things without a prompt. | |
| 593 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
| 594 EXPECT_FALSE(listener_notified()); | |
| 595 } | |
| 596 | |
| 597 TEST_F(GoogleURLTrackerTest, DontRefetchWhenNoOneRequestsCheck) { | |
| 598 FinishSleep(); | |
| 599 NotifyNetworkChanged(); | |
| 600 // No one called RequestServerCheck() so nothing should have happened. | |
| 601 EXPECT_FALSE(GetFetcher()); | |
| 602 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
| 603 ExpectDefaultURLs(); | |
| 604 EXPECT_FALSE(listener_notified()); | |
| 605 } | |
| 606 | |
| 607 TEST_F(GoogleURLTrackerTest, FetchOnLateRequest) { | |
| 608 FinishSleep(); | |
| 609 NotifyNetworkChanged(); | |
| 610 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
| 611 | |
| 612 RequestServerCheck(); | |
| 613 // The first request for a check should trigger a fetch if it hasn't happened | |
| 614 // already. | |
| 615 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
| 616 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
| 617 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
| 618 EXPECT_TRUE(listener_notified()); | |
| 619 } | |
| 620 | |
| 621 TEST_F(GoogleURLTrackerTest, DontFetchTwiceOnLateRequests) { | |
| 622 FinishSleep(); | |
| 623 NotifyNetworkChanged(); | |
| 624 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
| 625 | |
| 626 RequestServerCheck(); | |
| 627 // The first request for a check should trigger a fetch if it hasn't happened | |
| 628 // already. | |
| 629 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
| 630 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
| 631 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
| 632 EXPECT_TRUE(listener_notified()); | |
| 633 clear_listener_notified(); | |
| 634 | |
| 635 RequestServerCheck(); | |
| 636 // The second request should be ignored. | |
| 637 EXPECT_FALSE(GetFetcher()); | |
| 638 MockSearchDomainCheckResponse("http://www.google.co.in/"); | |
| 639 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
| 640 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
| 641 EXPECT_FALSE(listener_notified()); | |
| 642 } | |
| 643 | |
| 644 TEST_F(GoogleURLTrackerTest, SearchingDoesNothingIfNoNeedToPrompt) { | |
| 645 TestInfoBarManager infobar_manager(1); | |
| 646 RequestServerCheck(); | |
| 647 FinishSleep(); | |
| 648 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
| 649 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
| 650 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
| 651 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 652 EXPECT_TRUE(listener_notified()); | |
| 653 clear_listener_notified(); | |
| 654 | |
| 655 SetNavigationPending(&infobar_manager, true); | |
| 656 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 657 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 658 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
| 659 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
| 660 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 661 EXPECT_FALSE(listener_notified()); | |
| 662 } | |
| 663 | |
| 664 TEST_F(GoogleURLTrackerTest, TabClosedOnPendingSearch) { | |
| 665 TestInfoBarManager infobar_manager(1); | |
| 666 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
| 667 RequestServerCheck(); | |
| 668 FinishSleep(); | |
| 669 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
| 670 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 671 EXPECT_EQ(GURL("http://www.google.co.jp/"), fetched_google_url()); | |
| 672 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 673 EXPECT_FALSE(listener_notified()); | |
| 674 | |
| 675 SetNavigationPending(&infobar_manager, true); | |
| 676 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(&infobar_manager); | |
| 677 ASSERT_FALSE(map_entry == NULL); | |
| 678 EXPECT_FALSE(map_entry->has_infobar_delegate()); | |
| 679 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 680 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 681 EXPECT_FALSE(listener_notified()); | |
| 682 | |
| 683 CloseTab(&infobar_manager); | |
| 684 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 685 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 686 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 687 EXPECT_FALSE(listener_notified()); | |
| 688 } | |
| 689 | |
| 690 TEST_F(GoogleURLTrackerTest, TabClosedOnCommittedSearch) { | |
| 691 TestInfoBarManager infobar_manager(1); | |
| 692 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
| 693 RequestServerCheck(); | |
| 694 FinishSleep(); | |
| 695 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
| 696 | |
| 697 SetNavigationPending(&infobar_manager, true); | |
| 698 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 699 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); | |
| 700 | |
| 701 CloseTab(&infobar_manager); | |
| 702 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 703 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 704 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 705 EXPECT_FALSE(listener_notified()); | |
| 706 } | |
| 707 | |
| 708 TEST_F(GoogleURLTrackerTest, InfoBarClosed) { | |
| 709 TestInfoBarManager infobar_manager(1); | |
| 710 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
| 711 RequestServerCheck(); | |
| 712 FinishSleep(); | |
| 713 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
| 714 | |
| 715 SetNavigationPending(&infobar_manager, true); | |
| 716 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 717 GoogleURLTrackerInfoBarDelegate* infobar = | |
| 718 GetInfoBarDelegate(&infobar_manager); | |
| 719 ASSERT_FALSE(infobar == NULL); | |
| 720 | |
| 721 infobar->Close(false); | |
| 722 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 723 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 724 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 725 EXPECT_FALSE(listener_notified()); | |
| 726 } | |
| 727 | |
| 728 TEST_F(GoogleURLTrackerTest, InfoBarRefused) { | |
| 729 TestInfoBarManager infobar_manager(1); | |
| 730 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
| 731 RequestServerCheck(); | |
| 732 FinishSleep(); | |
| 733 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
| 734 | |
| 735 SetNavigationPending(&infobar_manager, true); | |
| 736 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 737 GoogleURLTrackerInfoBarDelegate* infobar = | |
| 738 GetInfoBarDelegate(&infobar_manager); | |
| 739 ASSERT_FALSE(infobar == NULL); | |
| 740 | |
| 741 infobar->Cancel(); | |
| 742 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 743 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 744 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); | |
| 745 EXPECT_FALSE(listener_notified()); | |
| 746 } | |
| 747 | |
| 748 TEST_F(GoogleURLTrackerTest, InfoBarAccepted) { | |
| 749 TestInfoBarManager infobar_manager(1); | |
| 750 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
| 751 RequestServerCheck(); | |
| 752 FinishSleep(); | |
| 753 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
| 754 | |
| 755 SetNavigationPending(&infobar_manager, true); | |
| 756 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 757 GoogleURLTrackerInfoBarDelegate* infobar = | |
| 758 GetInfoBarDelegate(&infobar_manager); | |
| 759 ASSERT_FALSE(infobar == NULL); | |
| 760 | |
| 761 infobar->Accept(); | |
| 762 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 763 EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url()); | |
| 764 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); | |
| 765 EXPECT_TRUE(listener_notified()); | |
| 766 } | |
| 767 | |
| 768 TEST_F(GoogleURLTrackerTest, FetchesCanAutomaticallyCloseInfoBars) { | |
| 769 TestInfoBarManager infobar_manager(1); | |
| 770 RequestServerCheck(); | |
| 771 FinishSleep(); | |
| 772 MockSearchDomainCheckResponse(google_url().spec()); | |
| 773 | |
| 774 // Re-fetching the accepted URL after showing an infobar for another URL | |
| 775 // should close the infobar. | |
| 776 NotifyNetworkChanged(); | |
| 777 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
| 778 SetNavigationPending(&infobar_manager, true); | |
| 779 CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); | |
| 780 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); | |
| 781 NotifyNetworkChanged(); | |
| 782 MockSearchDomainCheckResponse(google_url().spec()); | |
| 783 EXPECT_EQ(google_url(), GetLastPromptedGoogleURL()); | |
| 784 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 785 | |
| 786 // As should fetching a URL that differs from the accepted only by the scheme. | |
| 787 NotifyNetworkChanged(); | |
| 788 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
| 789 SetNavigationPending(&infobar_manager, true); | |
| 790 CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); | |
| 791 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); | |
| 792 NotifyNetworkChanged(); | |
| 793 url::Replacements<char> replacements; | |
| 794 const std::string& scheme("https"); | |
| 795 replacements.SetScheme(scheme.data(), url::Component(0, scheme.length())); | |
| 796 GURL new_google_url(google_url().ReplaceComponents(replacements)); | |
| 797 MockSearchDomainCheckResponse(new_google_url.spec()); | |
| 798 EXPECT_EQ(new_google_url, GetLastPromptedGoogleURL()); | |
| 799 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 800 | |
| 801 // As should re-fetching the last prompted URL. | |
| 802 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
| 803 NotifyNetworkChanged(); | |
| 804 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
| 805 SetNavigationPending(&infobar_manager, true); | |
| 806 CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); | |
| 807 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); | |
| 808 NotifyNetworkChanged(); | |
| 809 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
| 810 EXPECT_EQ(new_google_url, google_url()); | |
| 811 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 812 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 813 | |
| 814 // And one that differs from the last prompted URL only by the scheme. | |
| 815 NotifyNetworkChanged(); | |
| 816 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
| 817 SetNavigationPending(&infobar_manager, true); | |
| 818 CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); | |
| 819 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); | |
| 820 NotifyNetworkChanged(); | |
| 821 MockSearchDomainCheckResponse("https://www.google.co.uk/"); | |
| 822 EXPECT_EQ(new_google_url, google_url()); | |
| 823 EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 824 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 825 | |
| 826 // And fetching a different URL entirely. | |
| 827 NotifyNetworkChanged(); | |
| 828 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
| 829 SetNavigationPending(&infobar_manager, true); | |
| 830 CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); | |
| 831 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); | |
| 832 NotifyNetworkChanged(); | |
| 833 MockSearchDomainCheckResponse("https://www.google.co.in/"); | |
| 834 EXPECT_EQ(new_google_url, google_url()); | |
| 835 EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 836 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 837 } | |
| 838 | |
| 839 TEST_F(GoogleURLTrackerTest, ResetInfoBarGoogleURLs) { | |
| 840 TestInfoBarManager infobar_manager(1); | |
| 841 RequestServerCheck(); | |
| 842 FinishSleep(); | |
| 843 MockSearchDomainCheckResponse(google_url().spec()); | |
| 844 | |
| 845 NotifyNetworkChanged(); | |
| 846 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
| 847 SetNavigationPending(&infobar_manager, true); | |
| 848 CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); | |
| 849 GoogleURLTrackerInfoBarDelegate* delegate = | |
| 850 GetInfoBarDelegate(&infobar_manager); | |
| 851 ASSERT_FALSE(delegate == NULL); | |
| 852 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
| 853 | |
| 854 // If while an infobar is showing we fetch a new URL that differs from the | |
| 855 // infobar's only by scheme, the infobar should stay showing. | |
| 856 NotifyNetworkChanged(); | |
| 857 MockSearchDomainCheckResponse("https://www.google.co.uk/"); | |
| 858 EXPECT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
| 859 EXPECT_EQ(GURL("https://www.google.co.uk/"), fetched_google_url()); | |
| 860 } | |
| 861 | |
| 862 TEST_F(GoogleURLTrackerTest, NavigationsAfterPendingSearch) { | |
| 863 TestInfoBarManager infobar_manager(1); | |
| 864 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
| 865 RequestServerCheck(); | |
| 866 FinishSleep(); | |
| 867 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
| 868 | |
| 869 // A pending non-search after a pending search should delete the map entry. | |
| 870 SetNavigationPending(&infobar_manager, true); | |
| 871 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(&infobar_manager); | |
| 872 ASSERT_FALSE(map_entry == NULL); | |
| 873 EXPECT_FALSE(map_entry->has_infobar_delegate()); | |
| 874 SetNavigationPending(&infobar_manager, false); | |
| 875 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 876 | |
| 877 // A pending search after a pending search should leave the map entry alive. | |
| 878 SetNavigationPending(&infobar_manager, true); | |
| 879 map_entry = GetMapEntry(&infobar_manager); | |
| 880 ASSERT_FALSE(map_entry == NULL); | |
| 881 EXPECT_FALSE(map_entry->has_infobar_delegate()); | |
| 882 SetNavigationPending(&infobar_manager, true); | |
| 883 ASSERT_EQ(map_entry, GetMapEntry(&infobar_manager)); | |
| 884 EXPECT_FALSE(map_entry->has_infobar_delegate()); | |
| 885 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); | |
| 886 | |
| 887 // Committing this search should show an infobar. | |
| 888 CommitSearch(&infobar_manager, | |
| 889 GURL("http://www.google.co.uk/search?q=test2")); | |
| 890 EXPECT_TRUE(map_entry->has_infobar_delegate()); | |
| 891 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 892 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 893 EXPECT_FALSE(listener_notified()); | |
| 894 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); | |
| 895 } | |
| 896 | |
| 897 TEST_F(GoogleURLTrackerTest, NavigationsAfterCommittedSearch) { | |
| 898 TestInfoBarManager infobar_manager(1); | |
| 899 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
| 900 RequestServerCheck(); | |
| 901 FinishSleep(); | |
| 902 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
| 903 SetNavigationPending(&infobar_manager, true); | |
| 904 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 905 GoogleURLTrackerInfoBarDelegate* delegate = | |
| 906 GetInfoBarDelegate(&infobar_manager); | |
| 907 ASSERT_FALSE(delegate == NULL); | |
| 908 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); | |
| 909 | |
| 910 // A pending non-search on a visible infobar should basically do nothing. | |
| 911 SetNavigationPending(&infobar_manager, false); | |
| 912 ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
| 913 EXPECT_EQ(0, delegate->pending_id()); | |
| 914 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); | |
| 915 | |
| 916 // As should another pending non-search after the first. | |
| 917 SetNavigationPending(&infobar_manager, false); | |
| 918 ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
| 919 EXPECT_EQ(0, delegate->pending_id()); | |
| 920 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); | |
| 921 | |
| 922 // Committing this non-search should close the infobar. The control flow in | |
| 923 // these tests is not really comparable to in the real browser, but at least a | |
| 924 // few sanity-checks will be performed. | |
| 925 ASSERT_NO_FATAL_FAILURE(CommitNonSearch(&infobar_manager)); | |
| 926 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 927 | |
| 928 // A pending search on a visible infobar should cause the infobar to listen | |
| 929 // for the search to commit. | |
| 930 SetNavigationPending(&infobar_manager, true); | |
| 931 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 932 delegate = GetInfoBarDelegate(&infobar_manager); | |
| 933 ASSERT_FALSE(delegate == NULL); | |
| 934 SetNavigationPending(&infobar_manager, true); | |
| 935 ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
| 936 EXPECT_EQ(1, delegate->pending_id()); | |
| 937 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); | |
| 938 | |
| 939 // But a non-search after this should cancel that state. | |
| 940 SetNavigationPending(&infobar_manager, false); | |
| 941 ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
| 942 EXPECT_EQ(0, delegate->pending_id()); | |
| 943 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); | |
| 944 | |
| 945 // Another pending search after the non-search should put us back into | |
| 946 // "waiting for commit" mode. | |
| 947 SetNavigationPending(&infobar_manager, true); | |
| 948 ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
| 949 EXPECT_EQ(1, delegate->pending_id()); | |
| 950 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); | |
| 951 | |
| 952 // A second pending search after the first should not really change anything. | |
| 953 SetNavigationPending(&infobar_manager, true); | |
| 954 ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
| 955 EXPECT_EQ(1, delegate->pending_id()); | |
| 956 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); | |
| 957 | |
| 958 // Committing this search should change the visible infobar's search_url. | |
| 959 CommitSearch(&infobar_manager, | |
| 960 GURL("http://www.google.co.uk/search?q=test2")); | |
| 961 ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
| 962 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test2"), | |
| 963 delegate->search_url()); | |
| 964 EXPECT_EQ(0, delegate->pending_id()); | |
| 965 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); | |
| 966 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
| 967 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
| 968 EXPECT_FALSE(listener_notified()); | |
| 969 } | |
| 970 | |
| 971 TEST_F(GoogleURLTrackerTest, MultipleMapEntries) { | |
| 972 TestInfoBarManager infobar_manager(1); | |
| 973 TestInfoBarManager infobar_manager2(2); | |
| 974 TestInfoBarManager infobar_manager3(3); | |
| 975 TestInfoBarManager infobar_manager4(4); | |
| 976 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
| 977 RequestServerCheck(); | |
| 978 FinishSleep(); | |
| 979 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
| 980 | |
| 981 SetNavigationPending(&infobar_manager, true); | |
| 982 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(&infobar_manager); | |
| 983 ASSERT_FALSE(map_entry == NULL); | |
| 984 EXPECT_FALSE(map_entry->has_infobar_delegate()); | |
| 985 | |
| 986 SetNavigationPending(&infobar_manager2, true); | |
| 987 CommitSearch(&infobar_manager2, | |
| 988 GURL("http://www.google.co.uk/search?q=test2")); | |
| 989 GoogleURLTrackerInfoBarDelegate* delegate2 = | |
| 990 GetInfoBarDelegate(&infobar_manager2); | |
| 991 ASSERT_FALSE(delegate2 == NULL); | |
| 992 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test2"), | |
| 993 delegate2->search_url()); | |
| 994 | |
| 995 SetNavigationPending(&infobar_manager3, true); | |
| 996 GoogleURLTrackerMapEntry* map_entry3 = GetMapEntry(&infobar_manager3); | |
| 997 ASSERT_FALSE(map_entry3 == NULL); | |
| 998 EXPECT_FALSE(map_entry3->has_infobar_delegate()); | |
| 999 | |
| 1000 SetNavigationPending(&infobar_manager4, true); | |
| 1001 CommitSearch(&infobar_manager4, | |
| 1002 GURL("http://www.google.co.uk/search?q=test4")); | |
| 1003 GoogleURLTrackerInfoBarDelegate* delegate4 = | |
| 1004 GetInfoBarDelegate(&infobar_manager4); | |
| 1005 ASSERT_FALSE(delegate4 == NULL); | |
| 1006 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test4"), | |
| 1007 delegate4->search_url()); | |
| 1008 | |
| 1009 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 1010 EXPECT_TRUE(map_entry->has_infobar_delegate()); | |
| 1011 | |
| 1012 delegate2->Close(false); | |
| 1013 EXPECT_TRUE(GetMapEntry(&infobar_manager2) == NULL); | |
| 1014 EXPECT_FALSE(listener_notified()); | |
| 1015 | |
| 1016 delegate4->Accept(); | |
| 1017 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
| 1018 EXPECT_TRUE(GetMapEntry(&infobar_manager3) == NULL); | |
| 1019 EXPECT_TRUE(GetMapEntry(&infobar_manager4) == NULL); | |
| 1020 EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url()); | |
| 1021 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); | |
| 1022 EXPECT_TRUE(listener_notified()); | |
| 1023 } | |
| 1024 | |
| 1025 TEST_F(GoogleURLTrackerTest, IgnoreIrrelevantNavigation) { | |
| 1026 TestInfoBarManager infobar_manager(1); | |
| 1027 TestInfoBarManager infobar_manager2(2); | |
| 1028 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
| 1029 RequestServerCheck(); | |
| 1030 FinishSleep(); | |
| 1031 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
| 1032 | |
| 1033 // This tests a particularly gnarly sequence of events that used to cause us | |
| 1034 // to erroneously listen for a non-search navigation to commit. | |
| 1035 SetNavigationPending(&infobar_manager, true); | |
| 1036 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
| 1037 SetNavigationPending(&infobar_manager2, true); | |
| 1038 CommitSearch(&infobar_manager2, | |
| 1039 GURL("http://www.google.co.uk/search?q=test2")); | |
| 1040 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); | |
| 1041 GoogleURLTrackerInfoBarDelegate* delegate2 = | |
| 1042 GetInfoBarDelegate(&infobar_manager2); | |
| 1043 ASSERT_FALSE(delegate2 == NULL); | |
| 1044 SetNavigationPending(&infobar_manager, true); | |
| 1045 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); | |
| 1046 delegate2->Close(false); | |
| 1047 SetNavigationPending(&infobar_manager, false); | |
| 1048 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); | |
| 1049 } | |
| OLD | NEW |