Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: chrome/browser/google/google_url_tracker_unittest.cc

Issue 290453005: Remove Infobars notifications from GoogleURLTrackerMapEntry (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review comments Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/containers/scoped_ptr_hash_map.h"
11 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
12 #include "chrome/browser/chrome_notification_types.h" 14 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/google/google_url_tracker_factory.h" 15 #include "chrome/browser/google/google_url_tracker_factory.h"
14 #include "chrome/browser/google/google_url_tracker_infobar_delegate.h" 16 #include "chrome/browser/google/google_url_tracker_infobar_delegate.h"
15 #include "chrome/browser/google/google_url_tracker_navigation_helper.h" 17 #include "chrome/browser/google/google_url_tracker_navigation_helper.h"
18 #include "chrome/browser/infobars/infobar_service.h"
16 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
17 #include "chrome/test/base/testing_profile.h" 21 #include "chrome/test/base/testing_profile.h"
18 #include "components/google/core/browser/google_url_tracker_client.h" 22 #include "components/google/core/browser/google_url_tracker_client.h"
19 #include "components/infobars/core/infobar.h" 23 #include "components/infobars/core/infobar.h"
20 #include "components/infobars/core/infobar_delegate.h" 24 #include "components/infobars/core/infobar_delegate.h"
25 #include "content/public/browser/notification_observer.h"
26 #include "content/public/browser/notification_registrar.h"
21 #include "content/public/browser/notification_service.h" 27 #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" 28 #include "net/url_request/test_url_fetcher_factory.h"
24 #include "net/url_request/url_fetcher.h" 29 #include "net/url_request/url_fetcher.h"
25 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
26 31
27 class GoogleURLTrackerTest; 32 class GoogleURLTrackerTest;
28 33
29 namespace { 34 namespace {
30 35
31 // TestInfoBarDelegate -------------------------------------------------------- 36 // TestInfoBarDelegate --------------------------------------------------------
32 37
33 class TestInfoBarDelegate : public GoogleURLTrackerInfoBarDelegate { 38 class TestInfoBarDelegate : public GoogleURLTrackerInfoBarDelegate {
34 public: 39 public:
35 // Creates a test infobar and delegate and returns the infobar. Unlike the 40 // 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 41 // parent class, this does not add the infobar to |infobar_service|.
37 // that "pointer" is really just a magic number. Thus there is no 42 // Thus there is no InfoBarService ownership of the returned object; and since
38 // InfoBarService ownership of the returned object; and since the caller 43 // the caller doesn't own the returned object, we rely on |test_harness|
39 // doesn't own the returned object, we rely on |test_harness| cleaning this up 44 // cleaning this up eventually in GoogleURLTrackerTest::OnInfoBarClosed() to
40 // eventually in GoogleURLTrackerTest::OnInfoBarClosed() to avoid leaks. 45 // avoid leaks.
46 // TODO(blundell): Simplify this test to use InfoBarManager instead of
47 // InfoBarService, see crbug.com/373233.
41 static infobars::InfoBar* Create(GoogleURLTrackerTest* test_harness, 48 static infobars::InfoBar* Create(GoogleURLTrackerTest* test_harness,
42 InfoBarService* infobar_service, 49 InfoBarService* infobar_service,
43 GoogleURLTracker* google_url_tracker, 50 GoogleURLTracker* google_url_tracker,
44 const GURL& search_url); 51 const GURL& search_url);
45 52
46 private: 53 private:
47 TestInfoBarDelegate(GoogleURLTrackerTest* test_harness, 54 TestInfoBarDelegate(GoogleURLTrackerTest* test_harness,
48 InfoBarService* infobar_service, 55 InfoBarService* infobar_service,
49 GoogleURLTracker* google_url_tracker, 56 GoogleURLTracker* google_url_tracker,
50 const GURL& search_url); 57 const GURL& search_url);
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 } 214 }
208 215
209 } // namespace 216 } // namespace
210 217
211 218
212 // GoogleURLTrackerTest ------------------------------------------------------- 219 // GoogleURLTrackerTest -------------------------------------------------------
213 220
214 // Ths class exercises GoogleURLTracker. In order to avoid instantiating more 221 // Ths class exercises GoogleURLTracker. In order to avoid instantiating more
215 // of the Chrome infrastructure than necessary, the GoogleURLTracker functions 222 // of the Chrome infrastructure than necessary, the GoogleURLTracker functions
216 // are carefully written so that many of the functions which take 223 // are carefully written so that many of the functions which take
217 // NavigationController* or InfoBarService* do not actually dereference the 224 // NavigationController* do not actually dereference the objects, merely use
218 // objects, merely use them for comparisons and lookups, e.g. in |entry_map_|. 225 // 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 226 // 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 227 // instead supply "pointers" that are actually reinterpret_cast<>()ed magic
221 // numbers. Then we write the necessary stubs/hooks, here and in 228 // numbers. Then we write the necessary stubs/hooks, here and in
222 // TestInfoBarDelegate above, to make everything continue to work. 229 // TestInfoBarDelegate above, to make everything continue to work.
223 // 230 //
224 // Technically, the C++98 spec defines the result of casting 231 // 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 232 // 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 233 // 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 234 // break this, though, and the result would simply be a failing test rather than
228 // a bug in Chrome, we'll use it anyway. 235 // a bug in Chrome, we'll use it anyway.
229 class GoogleURLTrackerTest : public testing::Test { 236 class GoogleURLTrackerTest : public ChromeRenderViewHostTestHarness {
230 public: 237 public:
231 // Called by TestInfoBarDelegate::Close(). 238 // Called by TestInfoBarDelegate::Close().
232 void OnInfoBarClosed(scoped_ptr<infobars::InfoBar> infobar, 239 void OnInfoBarClosed(scoped_ptr<infobars::InfoBar> infobar,
233 InfoBarService* infobar_service); 240 InfoBarService* infobar_service);
234 241
235 protected: 242 protected:
236 GoogleURLTrackerTest(); 243 GoogleURLTrackerTest();
237 virtual ~GoogleURLTrackerTest(); 244 virtual ~GoogleURLTrackerTest();
238 245
239 // testing::Test 246 // testing::Test
(...skipping 19 matching lines...) Expand all
259 void CommitSearch(intptr_t unique_id, const GURL& search_url); 266 void CommitSearch(intptr_t unique_id, const GURL& search_url);
260 void CloseTab(intptr_t unique_id); 267 void CloseTab(intptr_t unique_id);
261 GoogleURLTrackerMapEntry* GetMapEntry(intptr_t unique_id); 268 GoogleURLTrackerMapEntry* GetMapEntry(intptr_t unique_id);
262 GoogleURLTrackerInfoBarDelegate* GetInfoBarDelegate(intptr_t unique_id); 269 GoogleURLTrackerInfoBarDelegate* GetInfoBarDelegate(intptr_t unique_id);
263 void ExpectDefaultURLs() const; 270 void ExpectDefaultURLs() const;
264 void ExpectListeningForCommit(intptr_t unique_id, bool listening); 271 void ExpectListeningForCommit(intptr_t unique_id, bool listening);
265 bool listener_notified() const { return listener_.notified(); } 272 bool listener_notified() const { return listener_.notified(); }
266 void clear_listener_notified() { listener_.clear_notified(); } 273 void clear_listener_notified() { listener_.clear_notified(); }
267 274
268 private: 275 private:
269 // Since |infobar_service| is really a magic number rather than an actual 276 // Infobars are not added to |infobar_service|. Instead we will simulate any
270 // object, we don't add the created infobar to it. Instead we will simulate 277 // helper<->infobar interaction necessary. The returned object will be
271 // any helper<->infobar interaction necessary. The returned object will be
272 // cleaned up in OnInfoBarClosed(). 278 // cleaned up in OnInfoBarClosed().
Peter Kasting 2014/05/28 22:27:19 If you're going to have a real infobar service, wh
droger 2014/06/02 13:29:46 Done.
273 infobars::InfoBar* CreateTestInfoBar(InfoBarService* infobar_service, 279 infobars::InfoBar* CreateTestInfoBar(InfoBarService* infobar_service,
274 GoogleURLTracker* google_url_tracker, 280 GoogleURLTracker* google_url_tracker,
275 const GURL& search_url); 281 const GURL& search_url);
276 282
277 // These are required by the TestURLFetchers GoogleURLTracker will create (see 283 // Gets the infobar service indexed by |unique_id|.
278 // test_url_fetcher_factory.h). 284 // Lazily creates WebContents instances, adds them in |web_contents_map_| and
279 content::TestBrowserThreadBundle thread_bundle_; 285 // attaches InfoBarServices to them.
286 InfoBarService* GetInfoBarService(intptr_t unique_id);
287
280 // Creating this allows us to call 288 // Creating this allows us to call
281 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(). 289 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests().
282 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; 290 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
283 net::TestURLFetcherFactory fetcher_factory_; 291 net::TestURLFetcherFactory fetcher_factory_;
284 GoogleURLTrackerClient* client_; 292 GoogleURLTrackerClient* client_;
285 GoogleURLTrackerNavigationHelper* nav_helper_; 293 GoogleURLTrackerNavigationHelper* nav_helper_;
286 TestingProfile profile_;
287 scoped_ptr<GoogleURLTracker> google_url_tracker_; 294 scoped_ptr<GoogleURLTracker> google_url_tracker_;
288 TestCallbackListener listener_; 295 TestCallbackListener listener_;
296 base::ScopedPtrHashMap<intptr_t, content::WebContents> web_contents_map_;
289 // This tracks the different "tabs" a test has "opened", so we can close them 297 // 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. 298 // properly before shutting down |google_url_tracker_|, which expects that.
291 std::set<int> unique_ids_seen_; 299 std::set<int> unique_ids_seen_;
292 }; 300 };
293 301
294 void GoogleURLTrackerTest::OnInfoBarClosed( 302 void GoogleURLTrackerTest::OnInfoBarClosed(
295 scoped_ptr<infobars::InfoBar> infobar, 303 scoped_ptr<infobars::InfoBar> infobar,
296 InfoBarService* infobar_service) { 304 InfoBarService* infobar_service) {
297 // First, simulate the InfoBarService firing INFOBAR_REMOVED. 305 // First, simulate the InfoBarService firing OnInfoBarRemoved().
298 infobars::InfoBar::RemovedDetails removed_details(infobar.get(), false);
299 GoogleURLTracker::EntryMap::const_iterator i = 306 GoogleURLTracker::EntryMap::const_iterator i =
300 google_url_tracker_->entry_map_.find(infobar_service); 307 google_url_tracker_->entry_map_.find(infobar_service);
301 ASSERT_FALSE(i == google_url_tracker_->entry_map_.end()); 308 ASSERT_FALSE(i == google_url_tracker_->entry_map_.end());
302 GoogleURLTrackerMapEntry* map_entry = i->second; 309 GoogleURLTrackerMapEntry* map_entry = i->second;
303 ASSERT_EQ(infobar->delegate(), map_entry->infobar_delegate()); 310 ASSERT_EQ(infobar->delegate(), map_entry->infobar_delegate());
304 map_entry->Observe( 311 map_entry->OnInfoBarRemoved(infobar.get(), false);
305 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
306 content::Source<InfoBarService>(infobar_service),
307 content::Details<infobars::InfoBar::RemovedDetails>(&removed_details));
308 312
309 // Second, simulate the infobar container closing the infobar in response. 313 // Second, simulate the infobar container closing the infobar in response.
310 // This happens automatically as |infobar| goes out of scope. 314 // This happens automatically as |infobar| goes out of scope.
311 } 315 }
312 316
313 GoogleURLTrackerTest::GoogleURLTrackerTest() 317 GoogleURLTrackerTest::GoogleURLTrackerTest() {
314 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { 318 ChromeRenderViewHostTestHarness::SetUp();
Peter Kasting 2014/05/28 22:27:19 If we're going to have both constructor/destructor
315 GoogleURLTrackerFactory::GetInstance()-> 319 GoogleURLTrackerFactory::GetInstance()
316 RegisterUserPrefsOnBrowserContextForTest(&profile_); 320 ->RegisterUserPrefsOnBrowserContextForTest(profile());
Peter Kasting 2014/05/28 22:27:19 Nit: I know not everyone agrees with this, but I t
317 } 321 }
318 322
319 GoogleURLTrackerTest::~GoogleURLTrackerTest() { 323 GoogleURLTrackerTest::~GoogleURLTrackerTest() {
324 ChromeRenderViewHostTestHarness::TearDown();
320 } 325 }
321 326
322 void GoogleURLTrackerTest::SetUp() { 327 void GoogleURLTrackerTest::SetUp() {
323 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); 328 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock());
324 // Ownership is passed to google_url_tracker_, but weak pointers are kept; 329 // Ownership is passed to google_url_tracker_, but weak pointers are kept;
325 // this is safe since GoogleURLTracker keeps these objects for its lifetime. 330 // this is safe since GoogleURLTracker keeps these objects for its lifetime.
326 client_ = new TestGoogleURLTrackerClient(); 331 client_ = new TestGoogleURLTrackerClient();
327 nav_helper_ = new TestGoogleURLTrackerNavigationHelper(); 332 nav_helper_ = new TestGoogleURLTrackerNavigationHelper();
328 scoped_ptr<GoogleURLTrackerClient> client(client_); 333 scoped_ptr<GoogleURLTrackerClient> client(client_);
329 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_); 334 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_);
330 google_url_tracker_.reset( 335 google_url_tracker_.reset(
331 new GoogleURLTracker(&profile_, 336 new GoogleURLTracker(profile(),
332 client.Pass(), 337 client.Pass(),
333 nav_helper.Pass(), 338 nav_helper.Pass(),
334 GoogleURLTracker::UNIT_TEST_MODE)); 339 GoogleURLTracker::UNIT_TEST_MODE));
335 google_url_tracker_->infobar_creator_ = base::Bind( 340 google_url_tracker_->infobar_creator_ = base::Bind(
336 &GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this)); 341 &GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this));
337 } 342 }
338 343
339 void GoogleURLTrackerTest::TearDown() { 344 void GoogleURLTrackerTest::TearDown() {
340 while (!unique_ids_seen_.empty()) 345 while (!unique_ids_seen_.empty())
341 CloseTab(*unique_ids_seen_.begin()); 346 CloseTab(*unique_ids_seen_.begin());
347 web_contents_map_.clear();
Peter Kasting 2014/05/28 22:27:19 Is this necessary?
342 } 348 }
343 349
344 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() { 350 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() {
345 // This will return the last fetcher created. If no fetchers have been 351 // This will return the last fetcher created. If no fetchers have been
346 // created, we'll pass GetFetcherByID() "-1", and it will return NULL. 352 // created, we'll pass GetFetcherByID() "-1", and it will return NULL.
347 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1); 353 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1);
348 } 354 }
349 355
350 void GoogleURLTrackerTest::MockSearchDomainCheckResponse( 356 void GoogleURLTrackerTest::MockSearchDomainCheckResponse(
351 const std::string& domain) { 357 const std::string& domain) {
(...skipping 19 matching lines...) Expand all
371 } 377 }
372 378
373 void GoogleURLTrackerTest::NotifyIPAddressChanged() { 379 void GoogleURLTrackerTest::NotifyIPAddressChanged() {
374 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); 380 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
375 // For thread safety, the NCN queues tasks to do the actual notifications, so 381 // 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. 382 // we need to spin the message loop so the tracker will actually be notified.
377 base::MessageLoop::current()->RunUntilIdle(); 383 base::MessageLoop::current()->RunUntilIdle();
378 } 384 }
379 385
380 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) { 386 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) {
381 profile_.GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, url.spec()); 387 profile()->GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, url.spec());
382 } 388 }
383 389
384 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() { 390 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() {
385 return GURL(profile_.GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); 391 return GURL(profile()->GetPrefs()->GetString(prefs::kLastPromptedGoogleURL));
386 } 392 }
387 393
388 void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id, 394 void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id,
389 bool is_search) { 395 bool is_search) {
390 if (is_search) { 396 if (is_search) {
391 google_url_tracker_->SearchCommitted(); 397 google_url_tracker_->SearchCommitted();
392 // Note that the call above might not have actually registered a listener 398 // Note that the call above might not have actually registered a listener
393 // for navigation starts if the searchdomaincheck response was bogus. 399 // for navigation starts if the searchdomaincheck response was bogus.
394 } 400 }
395 unique_ids_seen_.insert(unique_id); 401 unique_ids_seen_.insert(unique_id);
396 if (client_->IsListeningForNavigationStart()) { 402 if (client_->IsListeningForNavigationStart()) {
397 google_url_tracker_->OnNavigationPending( 403 google_url_tracker_->OnNavigationPending(
398 reinterpret_cast<content::NavigationController*>(unique_id), 404 reinterpret_cast<content::NavigationController*>(unique_id),
399 reinterpret_cast<InfoBarService*>(unique_id), unique_id); 405 GetInfoBarService(unique_id), unique_id);
400 } 406 }
401 } 407 }
402 408
403 void GoogleURLTrackerTest::CommitNonSearch(intptr_t unique_id) { 409 void GoogleURLTrackerTest::CommitNonSearch(intptr_t unique_id) {
404 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); 410 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
405 if (!map_entry) 411 if (!map_entry)
406 return; 412 return;
407 413
408 ExpectListeningForCommit(unique_id, false); 414 ExpectListeningForCommit(unique_id, false);
409 415
410 // The infobar should be showing; otherwise the pending non-search should 416 // The infobar should be showing; otherwise the pending non-search should
411 // have closed it. 417 // have closed it.
412 ASSERT_TRUE(map_entry->has_infobar_delegate()); 418 ASSERT_TRUE(map_entry->has_infobar_delegate());
413 419
414 // The pending_id should have been reset to 0 when the non-search became 420 // The pending_id should have been reset to 0 when the non-search became
415 // pending. 421 // pending.
416 EXPECT_EQ(0, map_entry->infobar_delegate()->pending_id()); 422 EXPECT_EQ(0, map_entry->infobar_delegate()->pending_id());
417 423
418 // Committing the navigation would close the infobar. 424 // Committing the navigation would close the infobar.
419 map_entry->infobar_delegate()->Close(false); 425 map_entry->infobar_delegate()->Close(false);
420 } 426 }
421 427
422 void GoogleURLTrackerTest::CommitSearch(intptr_t unique_id, 428 void GoogleURLTrackerTest::CommitSearch(intptr_t unique_id,
423 const GURL& search_url) { 429 const GURL& search_url) {
424 DCHECK(search_url.is_valid()); 430 DCHECK(search_url.is_valid());
425 if (nav_helper_->IsListeningForNavigationCommit( 431 if (nav_helper_->IsListeningForNavigationCommit(
426 reinterpret_cast<content::NavigationController*>(unique_id))) { 432 reinterpret_cast<content::NavigationController*>(unique_id))) {
427 google_url_tracker_->OnNavigationCommitted( 433 google_url_tracker_->OnNavigationCommitted(GetInfoBarService(unique_id),
428 reinterpret_cast<InfoBarService*>(unique_id), search_url); 434 search_url);
429 } 435 }
430 } 436 }
431 437
432 void GoogleURLTrackerTest::CloseTab(intptr_t unique_id) { 438 void GoogleURLTrackerTest::CloseTab(intptr_t unique_id) {
433 unique_ids_seen_.erase(unique_id); 439 unique_ids_seen_.erase(unique_id);
434 content::NavigationController* nav_controller = 440 content::NavigationController* nav_controller =
435 reinterpret_cast<content::NavigationController*>(unique_id); 441 reinterpret_cast<content::NavigationController*>(unique_id);
436 if (nav_helper_->IsListeningForTabDestruction(nav_controller)) { 442 if (nav_helper_->IsListeningForTabDestruction(nav_controller)) {
437 google_url_tracker_->OnTabClosed(nav_controller); 443 google_url_tracker_->OnTabClosed(nav_controller);
438 } else { 444 } else {
439 // Closing a tab with an infobar showing would close the infobar. 445 // Closing a tab with an infobar showing would close the infobar.
440 GoogleURLTrackerInfoBarDelegate* delegate = GetInfoBarDelegate(unique_id); 446 GoogleURLTrackerInfoBarDelegate* delegate = GetInfoBarDelegate(unique_id);
441 if (delegate) 447 if (delegate)
442 delegate->Close(false); 448 delegate->Close(false);
443 } 449 }
444 } 450 }
445 451
446 GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry( 452 GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry(
447 intptr_t unique_id) { 453 intptr_t unique_id) {
448 GoogleURLTracker::EntryMap::const_iterator i = 454 GoogleURLTracker::EntryMap::const_iterator i =
449 google_url_tracker_->entry_map_.find( 455 google_url_tracker_->entry_map_.find(GetInfoBarService(unique_id));
450 reinterpret_cast<InfoBarService*>(unique_id));
451 return (i == google_url_tracker_->entry_map_.end()) ? NULL : i->second; 456 return (i == google_url_tracker_->entry_map_.end()) ? NULL : i->second;
452 } 457 }
453 458
454 GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::GetInfoBarDelegate( 459 GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::GetInfoBarDelegate(
455 intptr_t unique_id) { 460 intptr_t unique_id) {
456 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); 461 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
457 return map_entry ? map_entry->infobar_delegate() : NULL; 462 return map_entry ? map_entry->infobar_delegate() : NULL;
458 } 463 }
459 464
460 void GoogleURLTrackerTest::ExpectDefaultURLs() const { 465 void GoogleURLTrackerTest::ExpectDefaultURLs() const {
(...skipping 13 matching lines...) Expand all
474 } 479 }
475 480
476 infobars::InfoBar* GoogleURLTrackerTest::CreateTestInfoBar( 481 infobars::InfoBar* GoogleURLTrackerTest::CreateTestInfoBar(
477 InfoBarService* infobar_service, 482 InfoBarService* infobar_service,
478 GoogleURLTracker* google_url_tracker, 483 GoogleURLTracker* google_url_tracker,
479 const GURL& search_url) { 484 const GURL& search_url) {
480 return TestInfoBarDelegate::Create(this, infobar_service, google_url_tracker, 485 return TestInfoBarDelegate::Create(this, infobar_service, google_url_tracker,
481 search_url); 486 search_url);
482 } 487 }
483 488
489 InfoBarService* GoogleURLTrackerTest::GetInfoBarService(intptr_t unique_id) {
490 base::ScopedPtrHashMap<intptr_t, content::WebContents>::const_iterator it =
491 web_contents_map_.find(unique_id);
492
493 if (it == web_contents_map_.end()) {
494 // Lazily create an InfoBarService for |unique_id|.
495 scoped_ptr<content::WebContents> web_contents(CreateTestWebContents());
496 InfoBarService::CreateForWebContents(web_contents.get());
497 it = web_contents_map_.set(unique_id, web_contents.Pass());
498 }
499
500 InfoBarService* service = InfoBarService::FromWebContents(it->second);
501 DCHECK(service);
502 return service;
503 }
484 504
485 // TestInfoBarDelegate -------------------------------------------------------- 505 // TestInfoBarDelegate --------------------------------------------------------
486 506
487 namespace { 507 namespace {
488 508
489 // static 509 // static
490 infobars::InfoBar* TestInfoBarDelegate::Create( 510 infobars::InfoBar* TestInfoBarDelegate::Create(
491 GoogleURLTrackerTest* test_harness, 511 GoogleURLTrackerTest* test_harness,
492 InfoBarService* infobar_service, 512 InfoBarService* infobar_service,
493 GoogleURLTracker* google_url_tracker, 513 GoogleURLTracker* google_url_tracker,
(...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after
1103 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2")); 1123 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2"));
1104 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL); 1124 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
1105 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2); 1125 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2);
1106 ASSERT_FALSE(delegate2 == NULL); 1126 ASSERT_FALSE(delegate2 == NULL);
1107 SetNavigationPending(1, true); 1127 SetNavigationPending(1, true);
1108 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true)); 1128 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
1109 delegate2->Close(false); 1129 delegate2->Close(false);
1110 SetNavigationPending(1, false); 1130 SetNavigationPending(1, false);
1111 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false)); 1131 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
1112 } 1132 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698