Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/google/google_url_tracker.h" | 5 #include "chrome/browser/google/google_url_tracker.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
| 13 #include "chrome/browser/google/google_url_tracker_factory.h" | 13 #include "chrome/browser/google/google_url_tracker_factory.h" |
| 14 #include "chrome/browser/google/google_url_tracker_infobar_delegate.h" | 14 #include "chrome/browser/google/google_url_tracker_infobar_delegate.h" |
| 15 #include "chrome/browser/google/google_url_tracker_navigation_helper.h" | 15 #include "chrome/browser/google/google_url_tracker_navigation_helper.h" |
| 16 #include "chrome/browser/infobars/infobar_service.h" | |
| 16 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
| 17 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
| 18 #include "components/infobars/core/infobar.h" | 20 #include "components/infobars/core/infobar.h" |
| 19 #include "components/infobars/core/infobar_delegate.h" | 21 #include "components/infobars/core/infobar_delegate.h" |
| 22 #include "content/public/browser/notification_observer.h" | |
| 23 #include "content/public/browser/notification_registrar.h" | |
| 20 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/test/test_browser_thread_bundle.h" | |
| 22 #include "net/url_request/test_url_fetcher_factory.h" | 25 #include "net/url_request/test_url_fetcher_factory.h" |
| 23 #include "net/url_request/url_fetcher.h" | 26 #include "net/url_request/url_fetcher.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 28 |
| 26 class GoogleURLTrackerTest; | 29 class GoogleURLTrackerTest; |
| 27 | 30 |
| 28 namespace { | 31 namespace { |
| 29 | 32 |
| 30 // TestInfoBarDelegate -------------------------------------------------------- | 33 // TestInfoBarDelegate -------------------------------------------------------- |
| 31 | 34 |
| 32 class TestInfoBarDelegate : public GoogleURLTrackerInfoBarDelegate { | 35 class TestInfoBarDelegate : public GoogleURLTrackerInfoBarDelegate { |
| 33 public: | 36 public: |
| 34 // Creates a test infobar and delegate and returns the infobar. Unlike the | 37 // Creates a test infobar and delegate and returns the infobar. Unlike the |
| 35 // parent class, this does not add the infobar to |infobar_service|, since | 38 // parent class, this does not add the infobar to |infobar_service|. |
| 36 // that "pointer" is really just a magic number. Thus there is no | 39 // Thus there is no InfoBarService ownership of the returned object; and since |
| 37 // InfoBarService ownership of the returned object; and since the caller | 40 // the caller doesn't own the returned object, we rely on |test_harness| |
| 38 // doesn't own the returned object, we rely on |test_harness| cleaning this up | 41 // cleaning this up eventually in GoogleURLTrackerTest::OnInfoBarClosed() to |
| 39 // eventually in GoogleURLTrackerTest::OnInfoBarClosed() to avoid leaks. | 42 // avoid leaks. |
| 40 static infobars::InfoBar* Create(GoogleURLTrackerTest* test_harness, | 43 static infobars::InfoBar* Create(GoogleURLTrackerTest* test_harness, |
| 41 InfoBarService* infobar_service, | 44 InfoBarService* infobar_service, |
| 42 GoogleURLTracker* google_url_tracker, | 45 GoogleURLTracker* google_url_tracker, |
| 43 const GURL& search_url); | 46 const GURL& search_url); |
| 44 | 47 |
| 45 private: | 48 private: |
| 46 TestInfoBarDelegate(GoogleURLTrackerTest* test_harness, | 49 TestInfoBarDelegate(GoogleURLTrackerTest* test_harness, |
| 47 InfoBarService* infobar_service, | 50 InfoBarService* infobar_service, |
| 48 GoogleURLTracker* google_url_tracker, | 51 GoogleURLTracker* google_url_tracker, |
| 49 const GURL& search_url); | 52 const GURL& search_url); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 } | 180 } |
| 178 | 181 |
| 179 } // namespace | 182 } // namespace |
| 180 | 183 |
| 181 | 184 |
| 182 // GoogleURLTrackerTest ------------------------------------------------------- | 185 // GoogleURLTrackerTest ------------------------------------------------------- |
| 183 | 186 |
| 184 // Ths class exercises GoogleURLTracker. In order to avoid instantiating more | 187 // Ths class exercises GoogleURLTracker. In order to avoid instantiating more |
| 185 // of the Chrome infrastructure than necessary, the GoogleURLTracker functions | 188 // of the Chrome infrastructure than necessary, the GoogleURLTracker functions |
| 186 // are carefully written so that many of the functions which take | 189 // are carefully written so that many of the functions which take |
| 187 // NavigationController* or InfoBarService* do not actually dereference the | 190 // NavigationController* do not actually dereference the objects, merely use |
| 188 // objects, merely use them for comparisons and lookups, e.g. in |entry_map_|. | 191 // them for comparisons and lookups, e.g. in |entry_map_|. |
| 189 // This then allows the test code here to not create any of these objects, and | 192 // This then allows the test code here to not create any of these objects, and |
| 190 // instead supply "pointers" that are actually reinterpret_cast<>()ed magic | 193 // instead supply "pointers" that are actually reinterpret_cast<>()ed magic |
| 191 // numbers. Then we write the necessary stubs/hooks, here and in | 194 // numbers. Then we write the necessary stubs/hooks, here and in |
| 192 // TestInfoBarDelegate above, to make everything continue to work. | 195 // TestInfoBarDelegate above, to make everything continue to work. |
| 193 // | 196 // |
| 194 // Technically, the C++98 spec defines the result of casting | 197 // Technically, the C++98 spec defines the result of casting |
| 195 // T* -> intptr_t -> T* to be an identity, but intptr_t -> T* -> intptr_t (what | 198 // T* -> intptr_t -> T* to be an identity, but intptr_t -> T* -> intptr_t (what |
| 196 // we use here) is "implementation-defined". Since I've never seen a compiler | 199 // we use here) is "implementation-defined". Since I've never seen a compiler |
| 197 // break this, though, and the result would simply be a failing test rather than | 200 // break this, though, and the result would simply be a failing test rather than |
| 198 // a bug in Chrome, we'll use it anyway. | 201 // a bug in Chrome, we'll use it anyway. |
| 199 class GoogleURLTrackerTest : public testing::Test { | 202 class GoogleURLTrackerTest : public ChromeRenderViewHostTestHarness { |
| 200 public: | 203 public: |
| 201 // Called by TestInfoBarDelegate::Close(). | 204 // Called by TestInfoBarDelegate::Close(). |
| 202 void OnInfoBarClosed(scoped_ptr<infobars::InfoBar> infobar, | 205 void OnInfoBarClosed(scoped_ptr<infobars::InfoBar> infobar, |
| 203 InfoBarService* infobar_service); | 206 InfoBarService* infobar_service); |
| 204 | 207 |
| 205 protected: | 208 protected: |
| 206 GoogleURLTrackerTest(); | 209 GoogleURLTrackerTest(); |
| 207 virtual ~GoogleURLTrackerTest(); | 210 virtual ~GoogleURLTrackerTest(); |
| 208 | 211 |
| 209 // testing::Test | 212 // testing::Test |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 229 void CommitSearch(intptr_t unique_id, const GURL& search_url); | 232 void CommitSearch(intptr_t unique_id, const GURL& search_url); |
| 230 void CloseTab(intptr_t unique_id); | 233 void CloseTab(intptr_t unique_id); |
| 231 GoogleURLTrackerMapEntry* GetMapEntry(intptr_t unique_id); | 234 GoogleURLTrackerMapEntry* GetMapEntry(intptr_t unique_id); |
| 232 GoogleURLTrackerInfoBarDelegate* GetInfoBarDelegate(intptr_t unique_id); | 235 GoogleURLTrackerInfoBarDelegate* GetInfoBarDelegate(intptr_t unique_id); |
| 233 void ExpectDefaultURLs() const; | 236 void ExpectDefaultURLs() const; |
| 234 void ExpectListeningForCommit(intptr_t unique_id, bool listening); | 237 void ExpectListeningForCommit(intptr_t unique_id, bool listening); |
| 235 bool observer_notified() const { return observer_.notified(); } | 238 bool observer_notified() const { return observer_.notified(); } |
| 236 void clear_observer_notified() { observer_.clear_notified(); } | 239 void clear_observer_notified() { observer_.clear_notified(); } |
| 237 | 240 |
| 238 private: | 241 private: |
| 239 // Since |infobar_service| is really a magic number rather than an actual | 242 // Infobars are not added to |infobar_service|. Instead we will simulate any |
| 240 // object, we don't add the created infobar to it. Instead we will simulate | 243 // helper<->infobar interaction necessary. The returned object will be |
| 241 // any helper<->infobar interaction necessary. The returned object will be | |
| 242 // cleaned up in OnInfoBarClosed(). | 244 // cleaned up in OnInfoBarClosed(). |
| 243 infobars::InfoBar* CreateTestInfoBar(InfoBarService* infobar_service, | 245 infobars::InfoBar* CreateTestInfoBar(InfoBarService* infobar_service, |
| 244 GoogleURLTracker* google_url_tracker, | 246 GoogleURLTracker* google_url_tracker, |
| 245 const GURL& search_url); | 247 const GURL& search_url); |
| 246 | 248 |
| 247 // These are required by the TestURLFetchers GoogleURLTracker will create (see | 249 // Gets the infobar service indexed by |unique_id|. |
| 248 // test_url_fetcher_factory.h). | 250 // Lazily creates WebContents instances, adds them in |web_contents_| and |
| 249 content::TestBrowserThreadBundle thread_bundle_; | 251 // attaches InfoBarServices to them. |
| 252 InfoBarService* GetInfoBarService(intptr_t unique_id); | |
| 253 | |
| 250 // Creating this allows us to call | 254 // Creating this allows us to call |
| 251 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(). | 255 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(). |
| 252 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 256 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
| 253 net::TestURLFetcherFactory fetcher_factory_; | 257 net::TestURLFetcherFactory fetcher_factory_; |
| 254 content::NotificationRegistrar registrar_; | 258 content::NotificationRegistrar registrar_; |
| 255 TestNotificationObserver observer_; | 259 TestNotificationObserver observer_; |
| 256 GoogleURLTrackerNavigationHelper* nav_helper_; | 260 GoogleURLTrackerNavigationHelper* nav_helper_; |
| 257 TestingProfile profile_; | |
| 258 scoped_ptr<GoogleURLTracker> google_url_tracker_; | 261 scoped_ptr<GoogleURLTracker> google_url_tracker_; |
| 262 ScopedVector<content::WebContents> web_contents_; | |
| 259 // This tracks the different "tabs" a test has "opened", so we can close them | 263 // This tracks the different "tabs" a test has "opened", so we can close them |
| 260 // properly before shutting down |google_url_tracker_|, which expects that. | 264 // properly before shutting down |google_url_tracker_|, which expects that. |
| 261 std::set<int> unique_ids_seen_; | 265 std::set<int> unique_ids_seen_; |
| 262 }; | 266 }; |
| 263 | 267 |
| 264 void GoogleURLTrackerTest::OnInfoBarClosed( | 268 void GoogleURLTrackerTest::OnInfoBarClosed( |
| 265 scoped_ptr<infobars::InfoBar> infobar, | 269 scoped_ptr<infobars::InfoBar> infobar, |
| 266 InfoBarService* infobar_service) { | 270 InfoBarService* infobar_service) { |
| 267 // First, simulate the InfoBarService firing INFOBAR_REMOVED. | 271 // First, simulate the InfoBarService firing OnInfoBarRemoved(). |
| 268 infobars::InfoBar::RemovedDetails removed_details(infobar.get(), false); | |
| 269 GoogleURLTracker::EntryMap::const_iterator i = | 272 GoogleURLTracker::EntryMap::const_iterator i = |
| 270 google_url_tracker_->entry_map_.find(infobar_service); | 273 google_url_tracker_->entry_map_.find(infobar_service); |
| 271 ASSERT_FALSE(i == google_url_tracker_->entry_map_.end()); | 274 ASSERT_FALSE(i == google_url_tracker_->entry_map_.end()); |
| 272 GoogleURLTrackerMapEntry* map_entry = i->second; | 275 GoogleURLTrackerMapEntry* map_entry = i->second; |
| 273 ASSERT_EQ(infobar->delegate(), map_entry->infobar_delegate()); | 276 ASSERT_EQ(infobar->delegate(), map_entry->infobar_delegate()); |
| 274 map_entry->Observe( | 277 map_entry->OnInfoBarRemoved(infobar.get(), false); |
| 275 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
| 276 content::Source<InfoBarService>(infobar_service), | |
| 277 content::Details<infobars::InfoBar::RemovedDetails>(&removed_details)); | |
| 278 | 278 |
| 279 // Second, simulate the infobar container closing the infobar in response. | 279 // Second, simulate the infobar container closing the infobar in response. |
| 280 // This happens automatically as |infobar| goes out of scope. | 280 // This happens automatically as |infobar| goes out of scope. |
| 281 } | 281 } |
| 282 | 282 |
| 283 GoogleURLTrackerTest::GoogleURLTrackerTest() | 283 GoogleURLTrackerTest::GoogleURLTrackerTest() { |
| 284 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 284 ChromeRenderViewHostTestHarness::SetUp(); |
| 285 GoogleURLTrackerFactory::GetInstance()-> | 285 GoogleURLTrackerFactory::GetInstance() |
| 286 RegisterUserPrefsOnBrowserContextForTest(&profile_); | 286 ->RegisterUserPrefsOnBrowserContextForTest(profile()); |
| 287 } | 287 } |
| 288 | 288 |
| 289 GoogleURLTrackerTest::~GoogleURLTrackerTest() { | 289 GoogleURLTrackerTest::~GoogleURLTrackerTest() { |
| 290 ChromeRenderViewHostTestHarness::TearDown(); | |
| 290 } | 291 } |
| 291 | 292 |
| 292 void GoogleURLTrackerTest::SetUp() { | 293 void GoogleURLTrackerTest::SetUp() { |
| 293 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); | 294 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); |
| 294 // Ownership is passed to google_url_tracker_, but a weak pointer is kept; | 295 // Ownership is passed to google_url_tracker_, but a weak pointer is kept; |
| 295 // this is safe since GoogleURLTracker keeps the observer for its lifetime. | 296 // this is safe since GoogleURLTracker keeps the observer for its lifetime. |
| 296 nav_helper_ = new TestGoogleURLTrackerNavigationHelper(); | 297 nav_helper_ = new TestGoogleURLTrackerNavigationHelper(); |
| 297 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_); | 298 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_); |
| 298 google_url_tracker_.reset( | 299 google_url_tracker_.reset(new GoogleURLTracker( |
| 299 new GoogleURLTracker(&profile_, nav_helper.Pass(), | 300 profile(), nav_helper.Pass(), GoogleURLTracker::UNIT_TEST_MODE)); |
| 300 GoogleURLTracker::UNIT_TEST_MODE)); | |
| 301 google_url_tracker_->infobar_creator_ = base::Bind( | 301 google_url_tracker_->infobar_creator_ = base::Bind( |
| 302 &GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this)); | 302 &GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this)); |
| 303 } | 303 } |
| 304 | 304 |
| 305 void GoogleURLTrackerTest::TearDown() { | 305 void GoogleURLTrackerTest::TearDown() { |
| 306 while (!unique_ids_seen_.empty()) | 306 while (!unique_ids_seen_.empty()) |
| 307 CloseTab(*unique_ids_seen_.begin()); | 307 CloseTab(*unique_ids_seen_.begin()); |
| 308 | 308 |
| 309 nav_helper_ = NULL; | 309 nav_helper_ = NULL; |
| 310 google_url_tracker_.reset(); | 310 google_url_tracker_.reset(); |
| 311 network_change_notifier_.reset(); | 311 network_change_notifier_.reset(); |
| 312 web_contents_.clear(); | |
| 312 } | 313 } |
| 313 | 314 |
| 314 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() { | 315 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() { |
| 315 // This will return the last fetcher created. If no fetchers have been | 316 // This will return the last fetcher created. If no fetchers have been |
| 316 // created, we'll pass GetFetcherByID() "-1", and it will return NULL. | 317 // created, we'll pass GetFetcherByID() "-1", and it will return NULL. |
| 317 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1); | 318 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1); |
| 318 } | 319 } |
| 319 | 320 |
| 320 void GoogleURLTrackerTest::MockSearchDomainCheckResponse( | 321 void GoogleURLTrackerTest::MockSearchDomainCheckResponse( |
| 321 const std::string& domain) { | 322 const std::string& domain) { |
| 322 net::TestURLFetcher* fetcher = GetFetcher(); | 323 net::TestURLFetcher* fetcher = GetFetcher(); |
| 323 if (!fetcher) | 324 if (!fetcher) |
| 324 return; | 325 return; |
| 325 fetcher_factory_.RemoveFetcherFromMap(fetcher->id()); | 326 fetcher_factory_.RemoveFetcherFromMap(fetcher->id()); |
| 326 fetcher->set_url(GURL(GoogleURLTracker::kSearchDomainCheckURL)); | 327 fetcher->set_url(GURL(GoogleURLTracker::kSearchDomainCheckURL)); |
| 327 fetcher->set_response_code(200); | 328 fetcher->set_response_code(200); |
| 328 fetcher->SetResponseString(domain); | 329 fetcher->SetResponseString(domain); |
| 329 fetcher->delegate()->OnURLFetchComplete(fetcher); | 330 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 330 // At this point, |fetcher| is deleted. | 331 // At this point, |fetcher| is deleted. |
| 331 } | 332 } |
| 332 | 333 |
| 333 void GoogleURLTrackerTest::RequestServerCheck() { | 334 void GoogleURLTrackerTest::RequestServerCheck() { |
| 334 if (!registrar_.IsRegistered(&observer_, | 335 if (!registrar_.IsRegistered(&observer_, |
| 335 chrome::NOTIFICATION_GOOGLE_URL_UPDATED, | 336 chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
| 336 content::Source<Profile>(&profile_))) { | 337 content::Source<Profile>(profile()))) { |
| 337 registrar_.Add(&observer_, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, | 338 registrar_.Add(&observer_, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
| 338 content::Source<Profile>(&profile_)); | 339 content::Source<Profile>(profile())); |
| 339 } | 340 } |
| 340 google_url_tracker_->SetNeedToFetch(); | 341 google_url_tracker_->SetNeedToFetch(); |
| 341 } | 342 } |
| 342 | 343 |
| 343 void GoogleURLTrackerTest::FinishSleep() { | 344 void GoogleURLTrackerTest::FinishSleep() { |
| 344 google_url_tracker_->FinishSleep(); | 345 google_url_tracker_->FinishSleep(); |
| 345 } | 346 } |
| 346 | 347 |
| 347 void GoogleURLTrackerTest::NotifyIPAddressChanged() { | 348 void GoogleURLTrackerTest::NotifyIPAddressChanged() { |
| 348 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | 349 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 349 // For thread safety, the NCN queues tasks to do the actual notifications, so | 350 // For thread safety, the NCN queues tasks to do the actual notifications, so |
| 350 // we need to spin the message loop so the tracker will actually be notified. | 351 // we need to spin the message loop so the tracker will actually be notified. |
| 351 base::MessageLoop::current()->RunUntilIdle(); | 352 base::MessageLoop::current()->RunUntilIdle(); |
| 352 } | 353 } |
| 353 | 354 |
| 354 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) { | 355 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) { |
| 355 profile_.GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, url.spec()); | 356 profile()->GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, url.spec()); |
| 356 } | 357 } |
| 357 | 358 |
| 358 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() { | 359 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() { |
| 359 return GURL(profile_.GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); | 360 return GURL(profile()->GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); |
| 360 } | 361 } |
| 361 | 362 |
| 362 void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id, | 363 void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id, |
| 363 bool is_search) { | 364 bool is_search) { |
| 364 if (is_search) { | 365 if (is_search) { |
| 365 google_url_tracker_->SearchCommitted(); | 366 google_url_tracker_->SearchCommitted(); |
| 366 // Note that the call above might not have actually registered a listener | 367 // Note that the call above might not have actually registered a listener |
| 367 // for navigation starts if the searchdomaincheck response was bogus. | 368 // for navigation starts if the searchdomaincheck response was bogus. |
| 368 } | 369 } |
| 369 unique_ids_seen_.insert(unique_id); | 370 unique_ids_seen_.insert(unique_id); |
| 370 if (nav_helper_->IsListeningForNavigationStart()) { | 371 if (nav_helper_->IsListeningForNavigationStart()) { |
| 371 google_url_tracker_->OnNavigationPending( | 372 google_url_tracker_->OnNavigationPending( |
| 372 reinterpret_cast<content::NavigationController*>(unique_id), | 373 reinterpret_cast<content::NavigationController*>(unique_id), |
| 373 reinterpret_cast<InfoBarService*>(unique_id), unique_id); | 374 GetInfoBarService(unique_id), unique_id); |
| 374 } | 375 } |
| 375 } | 376 } |
| 376 | 377 |
| 377 void GoogleURLTrackerTest::CommitNonSearch(intptr_t unique_id) { | 378 void GoogleURLTrackerTest::CommitNonSearch(intptr_t unique_id) { |
| 378 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); | 379 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); |
| 379 if (!map_entry) | 380 if (!map_entry) |
| 380 return; | 381 return; |
| 381 | 382 |
| 382 ExpectListeningForCommit(unique_id, false); | 383 ExpectListeningForCommit(unique_id, false); |
| 383 | 384 |
| 384 // The infobar should be showing; otherwise the pending non-search should | 385 // The infobar should be showing; otherwise the pending non-search should |
| 385 // have closed it. | 386 // have closed it. |
| 386 ASSERT_TRUE(map_entry->has_infobar_delegate()); | 387 ASSERT_TRUE(map_entry->has_infobar_delegate()); |
| 387 | 388 |
| 388 // The pending_id should have been reset to 0 when the non-search became | 389 // The pending_id should have been reset to 0 when the non-search became |
| 389 // pending. | 390 // pending. |
| 390 EXPECT_EQ(0, map_entry->infobar_delegate()->pending_id()); | 391 EXPECT_EQ(0, map_entry->infobar_delegate()->pending_id()); |
| 391 | 392 |
| 392 // Committing the navigation would close the infobar. | 393 // Committing the navigation would close the infobar. |
| 393 map_entry->infobar_delegate()->Close(false); | 394 map_entry->infobar_delegate()->Close(false); |
| 394 } | 395 } |
| 395 | 396 |
| 396 void GoogleURLTrackerTest::CommitSearch(intptr_t unique_id, | 397 void GoogleURLTrackerTest::CommitSearch(intptr_t unique_id, |
| 397 const GURL& search_url) { | 398 const GURL& search_url) { |
| 398 DCHECK(search_url.is_valid()); | 399 DCHECK(search_url.is_valid()); |
| 399 if (nav_helper_->IsListeningForNavigationCommit( | 400 if (nav_helper_->IsListeningForNavigationCommit( |
| 400 reinterpret_cast<content::NavigationController*>(unique_id))) { | 401 reinterpret_cast<content::NavigationController*>(unique_id))) { |
| 401 google_url_tracker_->OnNavigationCommitted( | 402 google_url_tracker_->OnNavigationCommitted(GetInfoBarService(unique_id), |
| 402 reinterpret_cast<InfoBarService*>(unique_id), search_url); | 403 search_url); |
| 403 } | 404 } |
| 404 } | 405 } |
| 405 | 406 |
| 406 void GoogleURLTrackerTest::CloseTab(intptr_t unique_id) { | 407 void GoogleURLTrackerTest::CloseTab(intptr_t unique_id) { |
| 407 unique_ids_seen_.erase(unique_id); | 408 unique_ids_seen_.erase(unique_id); |
| 408 content::NavigationController* nav_controller = | 409 content::NavigationController* nav_controller = |
| 409 reinterpret_cast<content::NavigationController*>(unique_id); | 410 reinterpret_cast<content::NavigationController*>(unique_id); |
| 410 if (nav_helper_->IsListeningForTabDestruction(nav_controller)) { | 411 if (nav_helper_->IsListeningForTabDestruction(nav_controller)) { |
| 411 google_url_tracker_->OnTabClosed(nav_controller); | 412 google_url_tracker_->OnTabClosed(nav_controller); |
| 412 } else { | 413 } else { |
| 413 // Closing a tab with an infobar showing would close the infobar. | 414 // Closing a tab with an infobar showing would close the infobar. |
| 414 GoogleURLTrackerInfoBarDelegate* delegate = GetInfoBarDelegate(unique_id); | 415 GoogleURLTrackerInfoBarDelegate* delegate = GetInfoBarDelegate(unique_id); |
| 415 if (delegate) | 416 if (delegate) |
| 416 delegate->Close(false); | 417 delegate->Close(false); |
| 417 } | 418 } |
| 418 } | 419 } |
| 419 | 420 |
| 420 GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry( | 421 GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry( |
| 421 intptr_t unique_id) { | 422 intptr_t unique_id) { |
| 422 GoogleURLTracker::EntryMap::const_iterator i = | 423 GoogleURLTracker::EntryMap::const_iterator i = |
| 423 google_url_tracker_->entry_map_.find( | 424 google_url_tracker_->entry_map_.find(GetInfoBarService(unique_id)); |
| 424 reinterpret_cast<InfoBarService*>(unique_id)); | |
| 425 return (i == google_url_tracker_->entry_map_.end()) ? NULL : i->second; | 425 return (i == google_url_tracker_->entry_map_.end()) ? NULL : i->second; |
| 426 } | 426 } |
| 427 | 427 |
| 428 GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::GetInfoBarDelegate( | 428 GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::GetInfoBarDelegate( |
| 429 intptr_t unique_id) { | 429 intptr_t unique_id) { |
| 430 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); | 430 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); |
| 431 return map_entry ? map_entry->infobar_delegate() : NULL; | 431 return map_entry ? map_entry->infobar_delegate() : NULL; |
| 432 } | 432 } |
| 433 | 433 |
| 434 void GoogleURLTrackerTest::ExpectDefaultURLs() const { | 434 void GoogleURLTrackerTest::ExpectDefaultURLs() const { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 448 } | 448 } |
| 449 | 449 |
| 450 infobars::InfoBar* GoogleURLTrackerTest::CreateTestInfoBar( | 450 infobars::InfoBar* GoogleURLTrackerTest::CreateTestInfoBar( |
| 451 InfoBarService* infobar_service, | 451 InfoBarService* infobar_service, |
| 452 GoogleURLTracker* google_url_tracker, | 452 GoogleURLTracker* google_url_tracker, |
| 453 const GURL& search_url) { | 453 const GURL& search_url) { |
| 454 return TestInfoBarDelegate::Create(this, infobar_service, google_url_tracker, | 454 return TestInfoBarDelegate::Create(this, infobar_service, google_url_tracker, |
| 455 search_url); | 455 search_url); |
| 456 } | 456 } |
| 457 | 457 |
| 458 InfoBarService* GoogleURLTrackerTest::GetInfoBarService(intptr_t unique_id) { | |
| 459 DCHECK(unique_id >= 0); | |
| 460 | |
| 461 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
| |
| 462 web_contents_.resize(unique_id + 1); | |
| 463 | |
| 464 if (!web_contents_[unique_id]) { | |
| 465 content::WebContents* web_contents = CreateTestWebContents(); | |
| 466 InfoBarService::CreateForWebContents(web_contents); | |
| 467 web_contents_[unique_id] = web_contents; | |
| 468 } | |
| 469 | |
| 470 InfoBarService* service = | |
| 471 InfoBarService::FromWebContents(web_contents_[unique_id]); | |
| 472 DCHECK(service); | |
| 473 return service; | |
| 474 } | |
| 458 | 475 |
| 459 // TestInfoBarDelegate -------------------------------------------------------- | 476 // TestInfoBarDelegate -------------------------------------------------------- |
| 460 | 477 |
| 461 namespace { | 478 namespace { |
| 462 | 479 |
| 463 // static | 480 // static |
| 464 infobars::InfoBar* TestInfoBarDelegate::Create( | 481 infobars::InfoBar* TestInfoBarDelegate::Create( |
| 465 GoogleURLTrackerTest* test_harness, | 482 GoogleURLTrackerTest* test_harness, |
| 466 InfoBarService* infobar_service, | 483 InfoBarService* infobar_service, |
| 467 GoogleURLTracker* google_url_tracker, | 484 GoogleURLTracker* google_url_tracker, |
| (...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1077 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2")); | 1094 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2")); |
| 1078 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL); | 1095 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL); |
| 1079 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2); | 1096 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2); |
| 1080 ASSERT_FALSE(delegate2 == NULL); | 1097 ASSERT_FALSE(delegate2 == NULL); |
| 1081 SetNavigationPending(1, true); | 1098 SetNavigationPending(1, true); |
| 1082 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true)); | 1099 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true)); |
| 1083 delegate2->Close(false); | 1100 delegate2->Close(false); |
| 1084 SetNavigationPending(1, false); | 1101 SetNavigationPending(1, false); |
| 1085 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false)); | 1102 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false)); |
| 1086 } | 1103 } |
| OLD | NEW |