Chromium Code Reviews| Index: chrome/browser/google/google_url_tracker_unittest.cc |
| diff --git a/chrome/browser/google/google_url_tracker_unittest.cc b/chrome/browser/google/google_url_tracker_unittest.cc |
| index 1ebe61369cfad3fc5d09681fe5f7d55e12707313..03c00a036e30619ed6a43ed9c36f5bac15a982a0 100644 |
| --- a/chrome/browser/google/google_url_tracker_unittest.cc |
| +++ b/chrome/browser/google/google_url_tracker_unittest.cc |
| @@ -1,4 +1,4 @@ |
| -// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Copyright 2012 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| @@ -13,12 +13,15 @@ |
| #include "chrome/browser/google/google_url_tracker_factory.h" |
| #include "chrome/browser/google/google_url_tracker_infobar_delegate.h" |
| #include "chrome/browser/google/google_url_tracker_navigation_helper.h" |
| +#include "chrome/browser/infobars/infobar_service.h" |
| #include "chrome/common/pref_names.h" |
| +#include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| #include "chrome/test/base/testing_profile.h" |
| #include "components/infobars/core/infobar.h" |
| #include "components/infobars/core/infobar_delegate.h" |
| +#include "content/public/browser/notification_observer.h" |
| +#include "content/public/browser/notification_registrar.h" |
| #include "content/public/browser/notification_service.h" |
| -#include "content/public/test/test_browser_thread_bundle.h" |
| #include "net/url_request/test_url_fetcher_factory.h" |
| #include "net/url_request/url_fetcher.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -32,11 +35,11 @@ namespace { |
| class TestInfoBarDelegate : public GoogleURLTrackerInfoBarDelegate { |
| public: |
| // Creates a test infobar and delegate and returns the infobar. Unlike the |
| - // parent class, this does not add the infobar to |infobar_service|, since |
| - // that "pointer" is really just a magic number. Thus there is no |
| - // InfoBarService ownership of the returned object; and since the caller |
| - // doesn't own the returned object, we rely on |test_harness| cleaning this up |
| - // eventually in GoogleURLTrackerTest::OnInfoBarClosed() to avoid leaks. |
| + // parent class, this does not add the infobar to |infobar_service|. |
| + // Thus there is no InfoBarService ownership of the returned object; and since |
| + // the caller doesn't own the returned object, we rely on |test_harness| |
| + // cleaning this up eventually in GoogleURLTrackerTest::OnInfoBarClosed() to |
| + // avoid leaks. |
| static infobars::InfoBar* Create(GoogleURLTrackerTest* test_harness, |
| InfoBarService* infobar_service, |
| GoogleURLTracker* google_url_tracker, |
| @@ -184,8 +187,8 @@ bool TestGoogleURLTrackerNavigationHelper::IsListeningForTabDestruction( |
| // Ths class exercises GoogleURLTracker. In order to avoid instantiating more |
| // of the Chrome infrastructure than necessary, the GoogleURLTracker functions |
| // are carefully written so that many of the functions which take |
| -// NavigationController* or InfoBarService* do not actually dereference the |
| -// objects, merely use them for comparisons and lookups, e.g. in |entry_map_|. |
| +// NavigationController* do not actually dereference the objects, merely use |
| +// them for comparisons and lookups, e.g. in |entry_map_|. |
| // This then allows the test code here to not create any of these objects, and |
| // instead supply "pointers" that are actually reinterpret_cast<>()ed magic |
| // numbers. Then we write the necessary stubs/hooks, here and in |
| @@ -196,7 +199,7 @@ bool TestGoogleURLTrackerNavigationHelper::IsListeningForTabDestruction( |
| // we use here) is "implementation-defined". Since I've never seen a compiler |
| // break this, though, and the result would simply be a failing test rather than |
| // a bug in Chrome, we'll use it anyway. |
| -class GoogleURLTrackerTest : public testing::Test { |
| +class GoogleURLTrackerTest : public ChromeRenderViewHostTestHarness { |
| public: |
| // Called by TestInfoBarDelegate::Close(). |
| void OnInfoBarClosed(scoped_ptr<infobars::InfoBar> infobar, |
| @@ -236,17 +239,18 @@ class GoogleURLTrackerTest : public testing::Test { |
| void clear_observer_notified() { observer_.clear_notified(); } |
| private: |
| - // Since |infobar_service| is really a magic number rather than an actual |
| - // object, we don't add the created infobar to it. Instead we will simulate |
| - // any helper<->infobar interaction necessary. The returned object will be |
| + // Infobars are not added to |infobar_service|. Instead we will simulate any |
| + // helper<->infobar interaction necessary. The returned object will be |
| // cleaned up in OnInfoBarClosed(). |
| infobars::InfoBar* CreateTestInfoBar(InfoBarService* infobar_service, |
| GoogleURLTracker* google_url_tracker, |
| const GURL& search_url); |
| - // These are required by the TestURLFetchers GoogleURLTracker will create (see |
| - // test_url_fetcher_factory.h). |
| - content::TestBrowserThreadBundle thread_bundle_; |
| + // Gets the infobar service indexed by |unique_id|. |
| + // Lazily creates WebContents instances, adds them in |web_contents_| and |
| + // attaches InfoBarServices to them. |
| + InfoBarService* GetInfoBarService(intptr_t unique_id); |
| + |
| // Creating this allows us to call |
| // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(). |
| scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
| @@ -254,8 +258,8 @@ class GoogleURLTrackerTest : public testing::Test { |
| content::NotificationRegistrar registrar_; |
| TestNotificationObserver observer_; |
| GoogleURLTrackerNavigationHelper* nav_helper_; |
| - TestingProfile profile_; |
| scoped_ptr<GoogleURLTracker> google_url_tracker_; |
| + ScopedVector<content::WebContents> web_contents_; |
| // This tracks the different "tabs" a test has "opened", so we can close them |
| // properly before shutting down |google_url_tracker_|, which expects that. |
| std::set<int> unique_ids_seen_; |
| @@ -264,29 +268,26 @@ class GoogleURLTrackerTest : public testing::Test { |
| void GoogleURLTrackerTest::OnInfoBarClosed( |
| scoped_ptr<infobars::InfoBar> infobar, |
| InfoBarService* infobar_service) { |
| - // First, simulate the InfoBarService firing INFOBAR_REMOVED. |
| - infobars::InfoBar::RemovedDetails removed_details(infobar.get(), false); |
| + // First, simulate the InfoBarService firing OnInfoBarRemoved(). |
| GoogleURLTracker::EntryMap::const_iterator i = |
| google_url_tracker_->entry_map_.find(infobar_service); |
| ASSERT_FALSE(i == google_url_tracker_->entry_map_.end()); |
| GoogleURLTrackerMapEntry* map_entry = i->second; |
| ASSERT_EQ(infobar->delegate(), map_entry->infobar_delegate()); |
| - map_entry->Observe( |
| - chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
| - content::Source<InfoBarService>(infobar_service), |
| - content::Details<infobars::InfoBar::RemovedDetails>(&removed_details)); |
| + map_entry->OnInfoBarRemoved(infobar.get(), false); |
| // Second, simulate the infobar container closing the infobar in response. |
| // This happens automatically as |infobar| goes out of scope. |
| } |
| -GoogleURLTrackerTest::GoogleURLTrackerTest() |
| - : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| - GoogleURLTrackerFactory::GetInstance()-> |
| - RegisterUserPrefsOnBrowserContextForTest(&profile_); |
| +GoogleURLTrackerTest::GoogleURLTrackerTest() { |
| + ChromeRenderViewHostTestHarness::SetUp(); |
| + GoogleURLTrackerFactory::GetInstance() |
| + ->RegisterUserPrefsOnBrowserContextForTest(profile()); |
| } |
| GoogleURLTrackerTest::~GoogleURLTrackerTest() { |
| + ChromeRenderViewHostTestHarness::TearDown(); |
| } |
| void GoogleURLTrackerTest::SetUp() { |
| @@ -295,9 +296,8 @@ void GoogleURLTrackerTest::SetUp() { |
| // this is safe since GoogleURLTracker keeps the observer for its lifetime. |
| nav_helper_ = new TestGoogleURLTrackerNavigationHelper(); |
| scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_); |
| - google_url_tracker_.reset( |
| - new GoogleURLTracker(&profile_, nav_helper.Pass(), |
| - GoogleURLTracker::UNIT_TEST_MODE)); |
| + google_url_tracker_.reset(new GoogleURLTracker( |
| + profile(), nav_helper.Pass(), GoogleURLTracker::UNIT_TEST_MODE)); |
| google_url_tracker_->infobar_creator_ = base::Bind( |
| &GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this)); |
| } |
| @@ -309,6 +309,7 @@ void GoogleURLTrackerTest::TearDown() { |
| nav_helper_ = NULL; |
| google_url_tracker_.reset(); |
| network_change_notifier_.reset(); |
| + web_contents_.clear(); |
| } |
| net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() { |
| @@ -333,9 +334,9 @@ void GoogleURLTrackerTest::MockSearchDomainCheckResponse( |
| void GoogleURLTrackerTest::RequestServerCheck() { |
| if (!registrar_.IsRegistered(&observer_, |
| chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
| - content::Source<Profile>(&profile_))) { |
| + content::Source<Profile>(profile()))) { |
| registrar_.Add(&observer_, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
| - content::Source<Profile>(&profile_)); |
| + content::Source<Profile>(profile())); |
| } |
| google_url_tracker_->SetNeedToFetch(); |
| } |
| @@ -352,11 +353,11 @@ void GoogleURLTrackerTest::NotifyIPAddressChanged() { |
| } |
| void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) { |
| - profile_.GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, url.spec()); |
| + profile()->GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, url.spec()); |
| } |
| GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() { |
| - return GURL(profile_.GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); |
| + return GURL(profile()->GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); |
| } |
| void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id, |
| @@ -370,7 +371,7 @@ void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id, |
| if (nav_helper_->IsListeningForNavigationStart()) { |
| google_url_tracker_->OnNavigationPending( |
| reinterpret_cast<content::NavigationController*>(unique_id), |
| - reinterpret_cast<InfoBarService*>(unique_id), unique_id); |
| + GetInfoBarService(unique_id), unique_id); |
| } |
| } |
| @@ -398,8 +399,8 @@ void GoogleURLTrackerTest::CommitSearch(intptr_t unique_id, |
| DCHECK(search_url.is_valid()); |
| if (nav_helper_->IsListeningForNavigationCommit( |
| reinterpret_cast<content::NavigationController*>(unique_id))) { |
| - google_url_tracker_->OnNavigationCommitted( |
| - reinterpret_cast<InfoBarService*>(unique_id), search_url); |
| + google_url_tracker_->OnNavigationCommitted(GetInfoBarService(unique_id), |
| + search_url); |
| } |
| } |
| @@ -420,8 +421,7 @@ void GoogleURLTrackerTest::CloseTab(intptr_t unique_id) { |
| GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry( |
| intptr_t unique_id) { |
| GoogleURLTracker::EntryMap::const_iterator i = |
| - google_url_tracker_->entry_map_.find( |
| - reinterpret_cast<InfoBarService*>(unique_id)); |
| + google_url_tracker_->entry_map_.find(GetInfoBarService(unique_id)); |
| return (i == google_url_tracker_->entry_map_.end()) ? NULL : i->second; |
| } |
| @@ -455,6 +455,23 @@ infobars::InfoBar* GoogleURLTrackerTest::CreateTestInfoBar( |
| search_url); |
| } |
| +InfoBarService* GoogleURLTrackerTest::GetInfoBarService(intptr_t unique_id) { |
| + DCHECK(unique_id >= 0); |
| + |
| + if ((size_t)unique_id >= web_contents_.size()) |
|
blundell
2014/05/15 15:52:47
oof. maybe a map instead of a vector?
droger
2014/05/15 15:57:40
That was my first idea too, but:
Unfortunately the
droger
2014/05/15 15:59:38
As the test only actually needs 4 instances, maybe
droger
2014/05/26 15:22:48
I found a base::ScopedPtrHashMap that I didn't kno
|
| + web_contents_.resize(unique_id + 1); |
| + |
| + if (!web_contents_[unique_id]) { |
| + content::WebContents* web_contents = CreateTestWebContents(); |
| + InfoBarService::CreateForWebContents(web_contents); |
| + web_contents_[unique_id] = web_contents; |
| + } |
| + |
| + InfoBarService* service = |
| + InfoBarService::FromWebContents(web_contents_[unique_id]); |
| + DCHECK(service); |
| + return service; |
| +} |
| // TestInfoBarDelegate -------------------------------------------------------- |