[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / google / google_url_tracker_unittest.cc
blob6b2fedf80f4973e100919f6b575e168c3f55f8eb
1 // Copyright (c) 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.
5 #include "chrome/browser/google/google_url_tracker.h"
7 #include <set>
8 #include <string>
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/pref_service.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/google/google_url_tracker_factory.h"
14 #include "chrome/browser/google/google_url_tracker_infobar_delegate.h"
15 #include "chrome/browser/google/google_url_tracker_navigation_helper.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "components/google/core/browser/google_url_tracker_client.h"
19 #include "components/infobars/core/infobar.h"
20 #include "components/infobars/core/infobar_delegate.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "net/url_request/test_url_fetcher_factory.h"
24 #include "net/url_request/url_fetcher.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 class GoogleURLTrackerTest;
29 namespace {
31 // TestInfoBarDelegate --------------------------------------------------------
33 class TestInfoBarDelegate : public GoogleURLTrackerInfoBarDelegate {
34 public:
35 // Creates a test infobar and delegate and returns the infobar. Unlike the
36 // parent class, this does not add the infobar to |infobar_service|, since
37 // that "pointer" is really just a magic number. Thus there is no
38 // InfoBarService ownership of the returned object; and since the caller
39 // doesn't own the returned object, we rely on |test_harness| cleaning this up
40 // eventually in GoogleURLTrackerTest::OnInfoBarClosed() to avoid leaks.
41 static infobars::InfoBar* Create(GoogleURLTrackerTest* test_harness,
42 InfoBarService* infobar_service,
43 GoogleURLTracker* google_url_tracker,
44 const GURL& search_url);
46 private:
47 TestInfoBarDelegate(GoogleURLTrackerTest* test_harness,
48 InfoBarService* infobar_service,
49 GoogleURLTracker* google_url_tracker,
50 const GURL& search_url);
51 virtual ~TestInfoBarDelegate();
53 // GoogleURLTrackerInfoBarDelegate:
54 virtual void Update(const GURL& search_url) OVERRIDE;
55 virtual void Close(bool redo_search) OVERRIDE;
57 GoogleURLTrackerTest* test_harness_;
58 InfoBarService* infobar_service_;
60 DISALLOW_COPY_AND_ASSIGN(TestInfoBarDelegate);
63 // The member function definitions come after the declaration of
64 // GoogleURLTrackerTest, so they can call members on it.
67 // TestCallbackListener ---------------------------------------------------
69 class TestCallbackListener {
70 public:
71 TestCallbackListener();
72 virtual ~TestCallbackListener();
74 bool HasRegisteredCallback();
75 void RegisterCallback(GoogleURLTracker* google_url_tracker);
77 bool notified() const { return notified_; }
78 void clear_notified() { notified_ = false; }
80 private:
81 void OnGoogleURLUpdated(GURL old_url, GURL new_url);
83 bool notified_;
84 scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_;
87 TestCallbackListener::TestCallbackListener() : notified_(false) {
90 TestCallbackListener::~TestCallbackListener() {
93 void TestCallbackListener::OnGoogleURLUpdated(GURL old_url, GURL new_url) {
94 notified_ = true;
97 bool TestCallbackListener::HasRegisteredCallback() {
98 return google_url_updated_subscription_.get();
101 void TestCallbackListener::RegisterCallback(
102 GoogleURLTracker* google_url_tracker) {
103 google_url_updated_subscription_ =
104 google_url_tracker->RegisterCallback(base::Bind(
105 &TestCallbackListener::OnGoogleURLUpdated, base::Unretained(this)));
109 // TestGoogleURLTrackerClient -------------------------------------------------
111 class TestGoogleURLTrackerClient : public GoogleURLTrackerClient {
112 public:
113 TestGoogleURLTrackerClient();
114 virtual ~TestGoogleURLTrackerClient();
116 virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
117 virtual bool IsListeningForNavigationStart() OVERRIDE;
119 private:
120 bool observe_nav_start_;
122 DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerClient);
125 TestGoogleURLTrackerClient::TestGoogleURLTrackerClient()
126 : observe_nav_start_(false) {
129 TestGoogleURLTrackerClient::~TestGoogleURLTrackerClient() {
132 void TestGoogleURLTrackerClient::SetListeningForNavigationStart(bool listen) {
133 observe_nav_start_ = listen;
136 bool TestGoogleURLTrackerClient::IsListeningForNavigationStart() {
137 return observe_nav_start_;
140 // TestGoogleURLTrackerNavigationHelper ---------------------------------------
142 class TestGoogleURLTrackerNavigationHelper
143 : public GoogleURLTrackerNavigationHelper {
144 public:
145 TestGoogleURLTrackerNavigationHelper();
146 virtual ~TestGoogleURLTrackerNavigationHelper();
148 virtual void SetGoogleURLTracker(GoogleURLTracker* tracker) OVERRIDE;
149 virtual void SetListeningForNavigationCommit(
150 const content::NavigationController* nav_controller,
151 bool listen) OVERRIDE;
152 virtual bool IsListeningForNavigationCommit(
153 const content::NavigationController* nav_controller) OVERRIDE;
154 virtual void SetListeningForTabDestruction(
155 const content::NavigationController* nav_controller,
156 bool listen) OVERRIDE;
157 virtual bool IsListeningForTabDestruction(
158 const content::NavigationController* nav_controller) OVERRIDE;
160 private:
161 GoogleURLTracker* tracker_;
162 std::set<const content::NavigationController*>
163 nav_controller_commit_listeners_;
164 std::set<const content::NavigationController*>
165 nav_controller_tab_close_listeners_;
168 TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper()
169 : tracker_(NULL) {
172 TestGoogleURLTrackerNavigationHelper::
173 ~TestGoogleURLTrackerNavigationHelper() {
176 void TestGoogleURLTrackerNavigationHelper::SetGoogleURLTracker(
177 GoogleURLTracker* tracker) {
178 tracker_ = tracker;
181 void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationCommit(
182 const content::NavigationController* nav_controller,
183 bool listen) {
184 if (listen)
185 nav_controller_commit_listeners_.insert(nav_controller);
186 else
187 nav_controller_commit_listeners_.erase(nav_controller);
190 bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit(
191 const content::NavigationController* nav_controller) {
192 return nav_controller_commit_listeners_.count(nav_controller) > 0;
195 void TestGoogleURLTrackerNavigationHelper::SetListeningForTabDestruction(
196 const content::NavigationController* nav_controller,
197 bool listen) {
198 if (listen)
199 nav_controller_tab_close_listeners_.insert(nav_controller);
200 else
201 nav_controller_tab_close_listeners_.erase(nav_controller);
204 bool TestGoogleURLTrackerNavigationHelper::IsListeningForTabDestruction(
205 const content::NavigationController* nav_controller) {
206 return nav_controller_tab_close_listeners_.count(nav_controller) > 0;
209 } // namespace
212 // GoogleURLTrackerTest -------------------------------------------------------
214 // Ths class exercises GoogleURLTracker. In order to avoid instantiating more
215 // of the Chrome infrastructure than necessary, the GoogleURLTracker functions
216 // are carefully written so that many of the functions which take
217 // NavigationController* or InfoBarService* do not actually dereference the
218 // objects, merely use them for comparisons and lookups, e.g. in |entry_map_|.
219 // This then allows the test code here to not create any of these objects, and
220 // instead supply "pointers" that are actually reinterpret_cast<>()ed magic
221 // numbers. Then we write the necessary stubs/hooks, here and in
222 // TestInfoBarDelegate above, to make everything continue to work.
224 // Technically, the C++98 spec defines the result of casting
225 // T* -> intptr_t -> T* to be an identity, but intptr_t -> T* -> intptr_t (what
226 // we use here) is "implementation-defined". Since I've never seen a compiler
227 // break this, though, and the result would simply be a failing test rather than
228 // a bug in Chrome, we'll use it anyway.
229 class GoogleURLTrackerTest : public testing::Test {
230 public:
231 // Called by TestInfoBarDelegate::Close().
232 void OnInfoBarClosed(scoped_ptr<infobars::InfoBar> infobar,
233 InfoBarService* infobar_service);
235 protected:
236 GoogleURLTrackerTest();
237 virtual ~GoogleURLTrackerTest();
239 // testing::Test
240 virtual void SetUp() OVERRIDE;
241 virtual void TearDown() OVERRIDE;
243 net::TestURLFetcher* GetFetcher();
244 void MockSearchDomainCheckResponse(const std::string& domain);
245 void RequestServerCheck();
246 void FinishSleep();
247 void NotifyIPAddressChanged();
248 GURL fetched_google_url() const {
249 return google_url_tracker_->fetched_google_url();
251 void set_google_url(const GURL& url) {
252 google_url_tracker_->google_url_ = url;
254 GURL google_url() const { return google_url_tracker_->google_url(); }
255 void SetLastPromptedGoogleURL(const GURL& url);
256 GURL GetLastPromptedGoogleURL();
257 void SetNavigationPending(intptr_t unique_id, bool is_search);
258 void CommitNonSearch(intptr_t unique_id);
259 void CommitSearch(intptr_t unique_id, const GURL& search_url);
260 void CloseTab(intptr_t unique_id);
261 GoogleURLTrackerMapEntry* GetMapEntry(intptr_t unique_id);
262 GoogleURLTrackerInfoBarDelegate* GetInfoBarDelegate(intptr_t unique_id);
263 void ExpectDefaultURLs() const;
264 void ExpectListeningForCommit(intptr_t unique_id, bool listening);
265 bool listener_notified() const { return listener_.notified(); }
266 void clear_listener_notified() { listener_.clear_notified(); }
268 private:
269 // Since |infobar_service| is really a magic number rather than an actual
270 // object, we don't add the created infobar to it. Instead we will simulate
271 // any helper<->infobar interaction necessary. The returned object will be
272 // cleaned up in OnInfoBarClosed().
273 infobars::InfoBar* CreateTestInfoBar(InfoBarService* infobar_service,
274 GoogleURLTracker* google_url_tracker,
275 const GURL& search_url);
277 // These are required by the TestURLFetchers GoogleURLTracker will create (see
278 // test_url_fetcher_factory.h).
279 content::TestBrowserThreadBundle thread_bundle_;
280 // Creating this allows us to call
281 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests().
282 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
283 net::TestURLFetcherFactory fetcher_factory_;
284 GoogleURLTrackerClient* client_;
285 GoogleURLTrackerNavigationHelper* nav_helper_;
286 TestingProfile profile_;
287 scoped_ptr<GoogleURLTracker> google_url_tracker_;
288 TestCallbackListener listener_;
289 // This tracks the different "tabs" a test has "opened", so we can close them
290 // properly before shutting down |google_url_tracker_|, which expects that.
291 std::set<int> unique_ids_seen_;
294 void GoogleURLTrackerTest::OnInfoBarClosed(
295 scoped_ptr<infobars::InfoBar> infobar,
296 InfoBarService* infobar_service) {
297 // First, simulate the InfoBarService firing INFOBAR_REMOVED.
298 infobars::InfoBar::RemovedDetails removed_details(infobar.get(), false);
299 GoogleURLTracker::EntryMap::const_iterator i =
300 google_url_tracker_->entry_map_.find(infobar_service);
301 ASSERT_FALSE(i == google_url_tracker_->entry_map_.end());
302 GoogleURLTrackerMapEntry* map_entry = i->second;
303 ASSERT_EQ(infobar->delegate(), map_entry->infobar_delegate());
304 map_entry->Observe(
305 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
306 content::Source<InfoBarService>(infobar_service),
307 content::Details<infobars::InfoBar::RemovedDetails>(&removed_details));
309 // Second, simulate the infobar container closing the infobar in response.
310 // This happens automatically as |infobar| goes out of scope.
313 GoogleURLTrackerTest::GoogleURLTrackerTest()
314 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
315 GoogleURLTrackerFactory::GetInstance()->
316 RegisterUserPrefsOnBrowserContextForTest(&profile_);
319 GoogleURLTrackerTest::~GoogleURLTrackerTest() {
322 void GoogleURLTrackerTest::SetUp() {
323 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock());
324 // Ownership is passed to google_url_tracker_, but weak pointers are kept;
325 // this is safe since GoogleURLTracker keeps these objects for its lifetime.
326 client_ = new TestGoogleURLTrackerClient();
327 nav_helper_ = new TestGoogleURLTrackerNavigationHelper();
328 scoped_ptr<GoogleURLTrackerClient> client(client_);
329 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_);
330 google_url_tracker_.reset(
331 new GoogleURLTracker(&profile_,
332 client.Pass(),
333 nav_helper.Pass(),
334 GoogleURLTracker::UNIT_TEST_MODE));
335 google_url_tracker_->infobar_creator_ = base::Bind(
336 &GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this));
339 void GoogleURLTrackerTest::TearDown() {
340 while (!unique_ids_seen_.empty())
341 CloseTab(*unique_ids_seen_.begin());
344 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() {
345 // This will return the last fetcher created. If no fetchers have been
346 // created, we'll pass GetFetcherByID() "-1", and it will return NULL.
347 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1);
350 void GoogleURLTrackerTest::MockSearchDomainCheckResponse(
351 const std::string& domain) {
352 net::TestURLFetcher* fetcher = GetFetcher();
353 if (!fetcher)
354 return;
355 fetcher_factory_.RemoveFetcherFromMap(fetcher->id());
356 fetcher->set_url(GURL(GoogleURLTracker::kSearchDomainCheckURL));
357 fetcher->set_response_code(200);
358 fetcher->SetResponseString(domain);
359 fetcher->delegate()->OnURLFetchComplete(fetcher);
360 // At this point, |fetcher| is deleted.
363 void GoogleURLTrackerTest::RequestServerCheck() {
364 if (!listener_.HasRegisteredCallback())
365 listener_.RegisterCallback(google_url_tracker_.get());
366 google_url_tracker_->SetNeedToFetch();
369 void GoogleURLTrackerTest::FinishSleep() {
370 google_url_tracker_->FinishSleep();
373 void GoogleURLTrackerTest::NotifyIPAddressChanged() {
374 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
375 // For thread safety, the NCN queues tasks to do the actual notifications, so
376 // we need to spin the message loop so the tracker will actually be notified.
377 base::MessageLoop::current()->RunUntilIdle();
380 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) {
381 profile_.GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, url.spec());
384 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() {
385 return GURL(profile_.GetPrefs()->GetString(prefs::kLastPromptedGoogleURL));
388 void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id,
389 bool is_search) {
390 if (is_search) {
391 google_url_tracker_->SearchCommitted();
392 // Note that the call above might not have actually registered a listener
393 // for navigation starts if the searchdomaincheck response was bogus.
395 unique_ids_seen_.insert(unique_id);
396 if (client_->IsListeningForNavigationStart()) {
397 google_url_tracker_->OnNavigationPending(
398 reinterpret_cast<content::NavigationController*>(unique_id),
399 reinterpret_cast<InfoBarService*>(unique_id), unique_id);
403 void GoogleURLTrackerTest::CommitNonSearch(intptr_t unique_id) {
404 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
405 if (!map_entry)
406 return;
408 ExpectListeningForCommit(unique_id, false);
410 // The infobar should be showing; otherwise the pending non-search should
411 // have closed it.
412 ASSERT_TRUE(map_entry->has_infobar_delegate());
414 // The pending_id should have been reset to 0 when the non-search became
415 // pending.
416 EXPECT_EQ(0, map_entry->infobar_delegate()->pending_id());
418 // Committing the navigation would close the infobar.
419 map_entry->infobar_delegate()->Close(false);
422 void GoogleURLTrackerTest::CommitSearch(intptr_t unique_id,
423 const GURL& search_url) {
424 DCHECK(search_url.is_valid());
425 if (nav_helper_->IsListeningForNavigationCommit(
426 reinterpret_cast<content::NavigationController*>(unique_id))) {
427 google_url_tracker_->OnNavigationCommitted(
428 reinterpret_cast<InfoBarService*>(unique_id), search_url);
432 void GoogleURLTrackerTest::CloseTab(intptr_t unique_id) {
433 unique_ids_seen_.erase(unique_id);
434 content::NavigationController* nav_controller =
435 reinterpret_cast<content::NavigationController*>(unique_id);
436 if (nav_helper_->IsListeningForTabDestruction(nav_controller)) {
437 google_url_tracker_->OnTabClosed(nav_controller);
438 } else {
439 // Closing a tab with an infobar showing would close the infobar.
440 GoogleURLTrackerInfoBarDelegate* delegate = GetInfoBarDelegate(unique_id);
441 if (delegate)
442 delegate->Close(false);
446 GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry(
447 intptr_t unique_id) {
448 GoogleURLTracker::EntryMap::const_iterator i =
449 google_url_tracker_->entry_map_.find(
450 reinterpret_cast<InfoBarService*>(unique_id));
451 return (i == google_url_tracker_->entry_map_.end()) ? NULL : i->second;
454 GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::GetInfoBarDelegate(
455 intptr_t unique_id) {
456 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
457 return map_entry ? map_entry->infobar_delegate() : NULL;
460 void GoogleURLTrackerTest::ExpectDefaultURLs() const {
461 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
462 EXPECT_EQ(GURL(), fetched_google_url());
465 void GoogleURLTrackerTest::ExpectListeningForCommit(intptr_t unique_id,
466 bool listening) {
467 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
468 if (map_entry) {
469 EXPECT_EQ(listening, nav_helper_->IsListeningForNavigationCommit(
470 map_entry->navigation_controller()));
471 } else {
472 EXPECT_FALSE(listening);
476 infobars::InfoBar* GoogleURLTrackerTest::CreateTestInfoBar(
477 InfoBarService* infobar_service,
478 GoogleURLTracker* google_url_tracker,
479 const GURL& search_url) {
480 return TestInfoBarDelegate::Create(this, infobar_service, google_url_tracker,
481 search_url);
485 // TestInfoBarDelegate --------------------------------------------------------
487 namespace {
489 // static
490 infobars::InfoBar* TestInfoBarDelegate::Create(
491 GoogleURLTrackerTest* test_harness,
492 InfoBarService* infobar_service,
493 GoogleURLTracker* google_url_tracker,
494 const GURL& search_url) {
495 return ConfirmInfoBarDelegate::CreateInfoBar(
496 scoped_ptr<ConfirmInfoBarDelegate>(new TestInfoBarDelegate(
497 test_harness, infobar_service, google_url_tracker,
498 search_url))).release();
501 TestInfoBarDelegate::TestInfoBarDelegate(GoogleURLTrackerTest* test_harness,
502 InfoBarService* infobar_service,
503 GoogleURLTracker* google_url_tracker,
504 const GURL& search_url)
505 : GoogleURLTrackerInfoBarDelegate(google_url_tracker, search_url),
506 test_harness_(test_harness),
507 infobar_service_(infobar_service) {
510 TestInfoBarDelegate::~TestInfoBarDelegate() {
513 void TestInfoBarDelegate::Update(const GURL& search_url) {
514 set_search_url(search_url);
515 set_pending_id(0);
518 void TestInfoBarDelegate::Close(bool redo_search) {
519 test_harness_->OnInfoBarClosed(scoped_ptr<infobars::InfoBar>(infobar()),
520 infobar_service_);
521 // WARNING: At this point |this| has been deleted!
524 } // namespace
527 // Tests ----------------------------------------------------------------------
529 TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) {
530 ExpectDefaultURLs();
531 FinishSleep();
532 // No one called RequestServerCheck() so nothing should have happened.
533 EXPECT_FALSE(GetFetcher());
534 MockSearchDomainCheckResponse("http://www.google.co.uk/");
535 ExpectDefaultURLs();
536 EXPECT_FALSE(listener_notified());
539 TEST_F(GoogleURLTrackerTest, UpdateOnFirstRun) {
540 RequestServerCheck();
541 EXPECT_FALSE(GetFetcher());
542 ExpectDefaultURLs();
543 EXPECT_FALSE(listener_notified());
545 FinishSleep();
546 MockSearchDomainCheckResponse("http://www.google.co.uk/");
547 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
548 // GoogleURL should be updated, becase there was no last prompted URL.
549 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
550 EXPECT_TRUE(listener_notified());
553 TEST_F(GoogleURLTrackerTest, DontUpdateWhenUnchanged) {
554 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
556 RequestServerCheck();
557 EXPECT_FALSE(GetFetcher());
558 ExpectDefaultURLs();
559 EXPECT_FALSE(listener_notified());
561 FinishSleep();
562 MockSearchDomainCheckResponse("http://www.google.co.uk/");
563 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
564 // GoogleURL should not be updated, because the fetched and prompted URLs
565 // match.
566 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
567 EXPECT_FALSE(listener_notified());
570 TEST_F(GoogleURLTrackerTest, DontPromptOnBadReplies) {
571 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
573 RequestServerCheck();
574 EXPECT_FALSE(GetFetcher());
575 ExpectDefaultURLs();
576 EXPECT_FALSE(listener_notified());
578 // Old-style domain string.
579 FinishSleep();
580 MockSearchDomainCheckResponse(".google.co.in");
581 EXPECT_EQ(GURL(), fetched_google_url());
582 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
583 EXPECT_FALSE(listener_notified());
584 SetNavigationPending(1, true);
585 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
586 EXPECT_TRUE(GetMapEntry(1) == NULL);
588 // Bad subdomain.
589 NotifyIPAddressChanged();
590 MockSearchDomainCheckResponse("http://mail.google.com/");
591 EXPECT_EQ(GURL(), fetched_google_url());
592 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
593 EXPECT_FALSE(listener_notified());
594 SetNavigationPending(1, true);
595 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
596 EXPECT_TRUE(GetMapEntry(1) == NULL);
598 // Non-empty path.
599 NotifyIPAddressChanged();
600 MockSearchDomainCheckResponse("http://www.google.com/search");
601 EXPECT_EQ(GURL(), fetched_google_url());
602 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
603 EXPECT_FALSE(listener_notified());
604 SetNavigationPending(1, true);
605 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
606 EXPECT_TRUE(GetMapEntry(1) == NULL);
608 // Non-empty query.
609 NotifyIPAddressChanged();
610 MockSearchDomainCheckResponse("http://www.google.com/?q=foo");
611 EXPECT_EQ(GURL(), fetched_google_url());
612 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
613 EXPECT_FALSE(listener_notified());
614 SetNavigationPending(1, true);
615 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
616 EXPECT_TRUE(GetMapEntry(1) == NULL);
618 // Non-empty ref.
619 NotifyIPAddressChanged();
620 MockSearchDomainCheckResponse("http://www.google.com/#anchor");
621 EXPECT_EQ(GURL(), fetched_google_url());
622 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
623 EXPECT_FALSE(listener_notified());
624 SetNavigationPending(1, true);
625 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
626 EXPECT_TRUE(GetMapEntry(1) == NULL);
628 // Complete garbage.
629 NotifyIPAddressChanged();
630 MockSearchDomainCheckResponse("HJ)*qF)_*&@f1");
631 EXPECT_EQ(GURL(), fetched_google_url());
632 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
633 EXPECT_FALSE(listener_notified());
634 SetNavigationPending(1, true);
635 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
636 EXPECT_TRUE(GetMapEntry(1) == NULL);
639 TEST_F(GoogleURLTrackerTest, UpdatePromptedURLOnReturnToPreviousLocation) {
640 SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/"));
641 set_google_url(GURL("http://www.google.co.uk/"));
642 RequestServerCheck();
643 FinishSleep();
644 MockSearchDomainCheckResponse("http://www.google.co.uk/");
645 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
646 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
647 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
648 EXPECT_FALSE(listener_notified());
651 TEST_F(GoogleURLTrackerTest, SilentlyAcceptSchemeChange) {
652 // We should auto-accept changes to the current Google URL that merely change
653 // the scheme, regardless of what the last prompted URL was.
654 SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/"));
655 set_google_url(GURL("http://www.google.co.uk/"));
656 RequestServerCheck();
657 FinishSleep();
658 MockSearchDomainCheckResponse("https://www.google.co.uk/");
659 EXPECT_EQ(GURL("https://www.google.co.uk/"), fetched_google_url());
660 EXPECT_EQ(GURL("https://www.google.co.uk/"), google_url());
661 EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL());
662 EXPECT_TRUE(listener_notified());
664 NotifyIPAddressChanged();
665 MockSearchDomainCheckResponse("http://www.google.co.uk/");
666 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
667 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
668 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
669 EXPECT_TRUE(listener_notified());
672 TEST_F(GoogleURLTrackerTest, RefetchOnIPAddressChange) {
673 RequestServerCheck();
674 FinishSleep();
675 MockSearchDomainCheckResponse("http://www.google.co.uk/");
676 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
677 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
678 EXPECT_TRUE(listener_notified());
679 clear_listener_notified();
681 NotifyIPAddressChanged();
682 MockSearchDomainCheckResponse("http://www.google.co.in/");
683 EXPECT_EQ(GURL("http://www.google.co.in/"), fetched_google_url());
684 // Just fetching a new URL shouldn't reset things without a prompt.
685 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
686 EXPECT_FALSE(listener_notified());
689 TEST_F(GoogleURLTrackerTest, DontRefetchWhenNoOneRequestsCheck) {
690 FinishSleep();
691 NotifyIPAddressChanged();
692 // No one called RequestServerCheck() so nothing should have happened.
693 EXPECT_FALSE(GetFetcher());
694 MockSearchDomainCheckResponse("http://www.google.co.uk/");
695 ExpectDefaultURLs();
696 EXPECT_FALSE(listener_notified());
699 TEST_F(GoogleURLTrackerTest, FetchOnLateRequest) {
700 FinishSleep();
701 NotifyIPAddressChanged();
702 MockSearchDomainCheckResponse("http://www.google.co.jp/");
704 RequestServerCheck();
705 // The first request for a check should trigger a fetch if it hasn't happened
706 // already.
707 MockSearchDomainCheckResponse("http://www.google.co.uk/");
708 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
709 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
710 EXPECT_TRUE(listener_notified());
713 TEST_F(GoogleURLTrackerTest, DontFetchTwiceOnLateRequests) {
714 FinishSleep();
715 NotifyIPAddressChanged();
716 MockSearchDomainCheckResponse("http://www.google.co.jp/");
718 RequestServerCheck();
719 // The first request for a check should trigger a fetch if it hasn't happened
720 // already.
721 MockSearchDomainCheckResponse("http://www.google.co.uk/");
722 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
723 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
724 EXPECT_TRUE(listener_notified());
725 clear_listener_notified();
727 RequestServerCheck();
728 // The second request should be ignored.
729 EXPECT_FALSE(GetFetcher());
730 MockSearchDomainCheckResponse("http://www.google.co.in/");
731 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
732 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
733 EXPECT_FALSE(listener_notified());
736 TEST_F(GoogleURLTrackerTest, SearchingDoesNothingIfNoNeedToPrompt) {
737 RequestServerCheck();
738 FinishSleep();
739 MockSearchDomainCheckResponse("http://www.google.co.uk/");
740 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
741 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
742 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
743 EXPECT_TRUE(listener_notified());
744 clear_listener_notified();
746 SetNavigationPending(1, true);
747 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
748 EXPECT_TRUE(GetMapEntry(1) == NULL);
749 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
750 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
751 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
752 EXPECT_FALSE(listener_notified());
755 TEST_F(GoogleURLTrackerTest, TabClosedOnPendingSearch) {
756 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
757 RequestServerCheck();
758 FinishSleep();
759 MockSearchDomainCheckResponse("http://www.google.co.jp/");
760 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
761 EXPECT_EQ(GURL("http://www.google.co.jp/"), fetched_google_url());
762 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
763 EXPECT_FALSE(listener_notified());
765 SetNavigationPending(1, true);
766 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(1);
767 ASSERT_FALSE(map_entry == NULL);
768 EXPECT_FALSE(map_entry->has_infobar_delegate());
769 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
770 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
771 EXPECT_FALSE(listener_notified());
773 CloseTab(1);
774 EXPECT_TRUE(GetMapEntry(1) == NULL);
775 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
776 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
777 EXPECT_FALSE(listener_notified());
780 TEST_F(GoogleURLTrackerTest, TabClosedOnCommittedSearch) {
781 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
782 RequestServerCheck();
783 FinishSleep();
784 MockSearchDomainCheckResponse("http://www.google.co.jp/");
786 SetNavigationPending(1, true);
787 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
788 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
790 CloseTab(1);
791 EXPECT_TRUE(GetMapEntry(1) == NULL);
792 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
793 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
794 EXPECT_FALSE(listener_notified());
797 TEST_F(GoogleURLTrackerTest, InfoBarClosed) {
798 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
799 RequestServerCheck();
800 FinishSleep();
801 MockSearchDomainCheckResponse("http://www.google.co.jp/");
803 SetNavigationPending(1, true);
804 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
805 GoogleURLTrackerInfoBarDelegate* infobar = GetInfoBarDelegate(1);
806 ASSERT_FALSE(infobar == NULL);
808 infobar->Close(false);
809 EXPECT_TRUE(GetMapEntry(1) == NULL);
810 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
811 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
812 EXPECT_FALSE(listener_notified());
815 TEST_F(GoogleURLTrackerTest, InfoBarRefused) {
816 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
817 RequestServerCheck();
818 FinishSleep();
819 MockSearchDomainCheckResponse("http://www.google.co.jp/");
821 SetNavigationPending(1, true);
822 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
823 GoogleURLTrackerInfoBarDelegate* infobar = GetInfoBarDelegate(1);
824 ASSERT_FALSE(infobar == NULL);
826 infobar->Cancel();
827 EXPECT_TRUE(GetMapEntry(1) == NULL);
828 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
829 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
830 EXPECT_FALSE(listener_notified());
833 TEST_F(GoogleURLTrackerTest, InfoBarAccepted) {
834 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
835 RequestServerCheck();
836 FinishSleep();
837 MockSearchDomainCheckResponse("http://www.google.co.jp/");
839 SetNavigationPending(1, true);
840 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
841 GoogleURLTrackerInfoBarDelegate* infobar = GetInfoBarDelegate(1);
842 ASSERT_FALSE(infobar == NULL);
844 infobar->Accept();
845 EXPECT_TRUE(GetMapEntry(1) == NULL);
846 EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url());
847 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
848 EXPECT_TRUE(listener_notified());
851 TEST_F(GoogleURLTrackerTest, FetchesCanAutomaticallyCloseInfoBars) {
852 RequestServerCheck();
853 FinishSleep();
854 MockSearchDomainCheckResponse(google_url().spec());
856 // Re-fetching the accepted URL after showing an infobar for another URL
857 // should close the infobar.
858 NotifyIPAddressChanged();
859 MockSearchDomainCheckResponse("http://www.google.co.uk/");
860 SetNavigationPending(1, true);
861 CommitSearch(1, GURL("http://www.google.com/search?q=test"));
862 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
863 NotifyIPAddressChanged();
864 MockSearchDomainCheckResponse(google_url().spec());
865 EXPECT_EQ(google_url(), GetLastPromptedGoogleURL());
866 EXPECT_TRUE(GetMapEntry(1) == NULL);
868 // As should fetching a URL that differs from the accepted only by the scheme.
869 NotifyIPAddressChanged();
870 MockSearchDomainCheckResponse("http://www.google.co.uk/");
871 SetNavigationPending(1, true);
872 CommitSearch(1, GURL("http://www.google.com/search?q=test"));
873 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
874 NotifyIPAddressChanged();
875 url::Replacements<char> replacements;
876 const std::string& scheme("https");
877 replacements.SetScheme(scheme.data(), url::Component(0, scheme.length()));
878 GURL new_google_url(google_url().ReplaceComponents(replacements));
879 MockSearchDomainCheckResponse(new_google_url.spec());
880 EXPECT_EQ(new_google_url, GetLastPromptedGoogleURL());
881 EXPECT_TRUE(GetMapEntry(1) == NULL);
883 // As should re-fetching the last prompted URL.
884 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
885 NotifyIPAddressChanged();
886 MockSearchDomainCheckResponse("http://www.google.co.jp/");
887 SetNavigationPending(1, true);
888 CommitSearch(1, GURL("http://www.google.com/search?q=test"));
889 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
890 NotifyIPAddressChanged();
891 MockSearchDomainCheckResponse("http://www.google.co.uk/");
892 EXPECT_EQ(new_google_url, google_url());
893 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
894 EXPECT_TRUE(GetMapEntry(1) == NULL);
896 // And one that differs from the last prompted URL only by the scheme.
897 NotifyIPAddressChanged();
898 MockSearchDomainCheckResponse("http://www.google.co.jp/");
899 SetNavigationPending(1, true);
900 CommitSearch(1, GURL("http://www.google.com/search?q=test"));
901 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
902 NotifyIPAddressChanged();
903 MockSearchDomainCheckResponse("https://www.google.co.uk/");
904 EXPECT_EQ(new_google_url, google_url());
905 EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL());
906 EXPECT_TRUE(GetMapEntry(1) == NULL);
908 // And fetching a different URL entirely.
909 NotifyIPAddressChanged();
910 MockSearchDomainCheckResponse("http://www.google.co.jp/");
911 SetNavigationPending(1, true);
912 CommitSearch(1, GURL("http://www.google.com/search?q=test"));
913 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
914 NotifyIPAddressChanged();
915 MockSearchDomainCheckResponse("https://www.google.co.in/");
916 EXPECT_EQ(new_google_url, google_url());
917 EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL());
918 EXPECT_TRUE(GetMapEntry(1) == NULL);
921 TEST_F(GoogleURLTrackerTest, ResetInfoBarGoogleURLs) {
922 RequestServerCheck();
923 FinishSleep();
924 MockSearchDomainCheckResponse(google_url().spec());
926 NotifyIPAddressChanged();
927 MockSearchDomainCheckResponse("http://www.google.co.uk/");
928 SetNavigationPending(1, true);
929 CommitSearch(1, GURL("http://www.google.com/search?q=test"));
930 GoogleURLTrackerInfoBarDelegate* delegate = GetInfoBarDelegate(1);
931 ASSERT_FALSE(delegate == NULL);
932 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
934 // If while an infobar is showing we fetch a new URL that differs from the
935 // infobar's only by scheme, the infobar should stay showing.
936 NotifyIPAddressChanged();
937 MockSearchDomainCheckResponse("https://www.google.co.uk/");
938 EXPECT_EQ(delegate, GetInfoBarDelegate(1));
939 EXPECT_EQ(GURL("https://www.google.co.uk/"), fetched_google_url());
942 TEST_F(GoogleURLTrackerTest, NavigationsAfterPendingSearch) {
943 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
944 RequestServerCheck();
945 FinishSleep();
946 MockSearchDomainCheckResponse("http://www.google.co.jp/");
948 // A pending non-search after a pending search should delete the map entry.
949 SetNavigationPending(1, true);
950 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(1);
951 ASSERT_FALSE(map_entry == NULL);
952 EXPECT_FALSE(map_entry->has_infobar_delegate());
953 SetNavigationPending(1, false);
954 EXPECT_TRUE(GetMapEntry(1) == NULL);
956 // A pending search after a pending search should leave the map entry alive.
957 SetNavigationPending(1, true);
958 map_entry = GetMapEntry(1);
959 ASSERT_FALSE(map_entry == NULL);
960 EXPECT_FALSE(map_entry->has_infobar_delegate());
961 SetNavigationPending(1, true);
962 ASSERT_EQ(map_entry, GetMapEntry(1));
963 EXPECT_FALSE(map_entry->has_infobar_delegate());
964 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
966 // Committing this search should show an infobar.
967 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test2"));
968 EXPECT_TRUE(map_entry->has_infobar_delegate());
969 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
970 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
971 EXPECT_FALSE(listener_notified());
972 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
975 TEST_F(GoogleURLTrackerTest, NavigationsAfterCommittedSearch) {
976 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
977 RequestServerCheck();
978 FinishSleep();
979 MockSearchDomainCheckResponse("http://www.google.co.jp/");
980 SetNavigationPending(1, true);
981 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
982 GoogleURLTrackerInfoBarDelegate* delegate = GetInfoBarDelegate(1);
983 ASSERT_FALSE(delegate == NULL);
984 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
986 // A pending non-search on a visible infobar should basically do nothing.
987 SetNavigationPending(1, false);
988 ASSERT_EQ(delegate, GetInfoBarDelegate(1));
989 EXPECT_EQ(0, delegate->pending_id());
990 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
992 // As should another pending non-search after the first.
993 SetNavigationPending(1, false);
994 ASSERT_EQ(delegate, GetInfoBarDelegate(1));
995 EXPECT_EQ(0, delegate->pending_id());
996 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
998 // Committing this non-search should close the infobar. The control flow in
999 // these tests is not really comparable to in the real browser, but at least a
1000 // few sanity-checks will be performed.
1001 ASSERT_NO_FATAL_FAILURE(CommitNonSearch(1));
1002 EXPECT_TRUE(GetMapEntry(1) == NULL);
1004 // A pending search on a visible infobar should cause the infobar to listen
1005 // for the search to commit.
1006 SetNavigationPending(1, true);
1007 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
1008 delegate = GetInfoBarDelegate(1);
1009 ASSERT_FALSE(delegate == NULL);
1010 SetNavigationPending(1, true);
1011 ASSERT_EQ(delegate, GetInfoBarDelegate(1));
1012 EXPECT_EQ(1, delegate->pending_id());
1013 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
1015 // But a non-search after this should cancel that state.
1016 SetNavigationPending(1, false);
1017 ASSERT_EQ(delegate, GetInfoBarDelegate(1));
1018 EXPECT_EQ(0, delegate->pending_id());
1019 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
1021 // Another pending search after the non-search should put us back into
1022 // "waiting for commit" mode.
1023 SetNavigationPending(1, true);
1024 ASSERT_EQ(delegate, GetInfoBarDelegate(1));
1025 EXPECT_EQ(1, delegate->pending_id());
1026 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
1028 // A second pending search after the first should not really change anything.
1029 SetNavigationPending(1, true);
1030 ASSERT_EQ(delegate, GetInfoBarDelegate(1));
1031 EXPECT_EQ(1, delegate->pending_id());
1032 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
1034 // Committing this search should change the visible infobar's search_url.
1035 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test2"));
1036 ASSERT_EQ(delegate, GetInfoBarDelegate(1));
1037 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test2"),
1038 delegate->search_url());
1039 EXPECT_EQ(0, delegate->pending_id());
1040 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
1041 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
1042 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
1043 EXPECT_FALSE(listener_notified());
1046 TEST_F(GoogleURLTrackerTest, MultipleMapEntries) {
1047 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
1048 RequestServerCheck();
1049 FinishSleep();
1050 MockSearchDomainCheckResponse("http://www.google.co.jp/");
1052 SetNavigationPending(1, true);
1053 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(1);
1054 ASSERT_FALSE(map_entry == NULL);
1055 EXPECT_FALSE(map_entry->has_infobar_delegate());
1057 SetNavigationPending(2, true);
1058 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2"));
1059 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2);
1060 ASSERT_FALSE(delegate2 == NULL);
1061 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test2"),
1062 delegate2->search_url());
1064 SetNavigationPending(3, true);
1065 GoogleURLTrackerMapEntry* map_entry3 = GetMapEntry(3);
1066 ASSERT_FALSE(map_entry3 == NULL);
1067 EXPECT_FALSE(map_entry3->has_infobar_delegate());
1069 SetNavigationPending(4, true);
1070 CommitSearch(4, GURL("http://www.google.co.uk/search?q=test4"));
1071 GoogleURLTrackerInfoBarDelegate* delegate4 = GetInfoBarDelegate(4);
1072 ASSERT_FALSE(delegate4 == NULL);
1073 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test4"),
1074 delegate4->search_url());
1076 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
1077 EXPECT_TRUE(map_entry->has_infobar_delegate());
1079 delegate2->Close(false);
1080 EXPECT_TRUE(GetMapEntry(2) == NULL);
1081 EXPECT_FALSE(listener_notified());
1083 delegate4->Accept();
1084 EXPECT_TRUE(GetMapEntry(1) == NULL);
1085 EXPECT_TRUE(GetMapEntry(3) == NULL);
1086 EXPECT_TRUE(GetMapEntry(4) == NULL);
1087 EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url());
1088 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
1089 EXPECT_TRUE(listener_notified());
1092 TEST_F(GoogleURLTrackerTest, IgnoreIrrelevantNavigation) {
1093 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
1094 RequestServerCheck();
1095 FinishSleep();
1096 MockSearchDomainCheckResponse("http://www.google.co.jp/");
1098 // This tests a particularly gnarly sequence of events that used to cause us
1099 // to erroneously listen for a non-search navigation to commit.
1100 SetNavigationPending(1, true);
1101 CommitSearch(1, GURL("http://www.google.co.uk/search?q=test"));
1102 SetNavigationPending(2, true);
1103 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2"));
1104 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
1105 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2);
1106 ASSERT_FALSE(delegate2 == NULL);
1107 SetNavigationPending(1, true);
1108 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
1109 delegate2->Close(false);
1110 SetNavigationPending(1, false);
1111 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));