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

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

Issue 11366075: Merge 165468 - Fix a crash that could occur if the user closed a tab with an uncommitted search nav… (Closed) Base URL: svn://svn.chromium.org/chrome/branches/1312/src/
Patch Set: Created 8 years, 1 month 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
« no previous file with comments | « chrome/browser/google/google_url_tracker_map_entry.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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.h" 10 #include "base/message_loop.h"
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 } // namespace 85 } // namespace
86 86
87 87
88 // GoogleURLTrackerTest ------------------------------------------------------- 88 // GoogleURLTrackerTest -------------------------------------------------------
89 89
90 // Ths class exercises GoogleURLTracker. In order to avoid instantiating more 90 // Ths class exercises GoogleURLTracker. In order to avoid instantiating more
91 // of the Chrome infrastructure than necessary, the GoogleURLTracker functions 91 // of the Chrome infrastructure than necessary, the GoogleURLTracker functions
92 // are carefully written so that many of the functions which take WebContents*, 92 // are carefully written so that many of the functions which take WebContents*,
93 // NavigationController*, InfoBarTabHelper*, or objects containing such pointers 93 // NavigationController*, InfoBarTabHelper*, or objects containing such pointers
94 // (e.g. NotificationSource) do not actually dereference the objects, merely use 94 // (e.g. NotificationSource) do not actually dereference the objects, merely use
95 // them for comparisons and lookups, e.g. in an InfoBarMap. This then allows 95 // them for comparisons and lookups, e.g. in |entry_map_|. This then allows the
96 // the test code here to not create any of these objects, and instead supply 96 // test code here to not create any of these objects, and instead supply
97 // "pointers" that are actually reinterpret_cast<>()ed magic numbers. Then we 97 // "pointers" that are actually reinterpret_cast<>()ed magic numbers. Then we
98 // write the necessary stubs/hooks, here and in TestInfoBarDelegate above, to 98 // write the necessary stubs/hooks, here and in TestInfoBarDelegate above, to
99 // make everything continue to work. 99 // make everything continue to work.
100 // 100 //
101 // Technically, the C++98 spec defines the result of casting 101 // Technically, the C++98 spec defines the result of casting
102 // T* -> intptr_t -> T* to be an identity, but intptr_t -> T* -> intptr_t (what 102 // T* -> intptr_t -> T* to be an identity, but intptr_t -> T* -> intptr_t (what
103 // we use here) is "implementation-defined". Since I've never seen a compiler 103 // we use here) is "implementation-defined". Since I've never seen a compiler
104 // break this, though, and the result would simply be a failing test rather than 104 // break this, though, and the result would simply be a failing test rather than
105 // a bug in Chrome, we'll use it anyway. 105 // a bug in Chrome, we'll use it anyway.
106 class GoogleURLTrackerTest : public testing::Test { 106 class GoogleURLTrackerTest : public testing::Test {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 // This tracks the different "tabs" a test has "opened", so we can close them 168 // This tracks the different "tabs" a test has "opened", so we can close them
169 // properly before shutting down |google_url_tracker_|, which expects that. 169 // properly before shutting down |google_url_tracker_|, which expects that.
170 std::set<int> unique_ids_seen_; 170 std::set<int> unique_ids_seen_;
171 }; 171 };
172 172
173 void GoogleURLTrackerTest::OnInfoBarClosed( 173 void GoogleURLTrackerTest::OnInfoBarClosed(
174 GoogleURLTrackerInfoBarDelegate* infobar, 174 GoogleURLTrackerInfoBarDelegate* infobar,
175 InfoBarTabHelper* infobar_helper) { 175 InfoBarTabHelper* infobar_helper) {
176 // First, simulate the InfoBarTabHelper firing INFOBAR_REMOVED. 176 // First, simulate the InfoBarTabHelper firing INFOBAR_REMOVED.
177 InfoBarRemovedDetails removed_details(infobar, false); 177 InfoBarRemovedDetails removed_details(infobar, false);
178 GoogleURLTracker::InfoBarMap::const_iterator i = 178 GoogleURLTracker::EntryMap::const_iterator i =
179 google_url_tracker_->infobar_map_.find(infobar_helper); 179 google_url_tracker_->entry_map_.find(infobar_helper);
180 ASSERT_FALSE(i == google_url_tracker_->infobar_map_.end()); 180 ASSERT_FALSE(i == google_url_tracker_->entry_map_.end());
181 GoogleURLTrackerMapEntry* map_entry = i->second; 181 GoogleURLTrackerMapEntry* map_entry = i->second;
182 ASSERT_EQ(infobar, map_entry->infobar()); 182 ASSERT_EQ(infobar, map_entry->infobar());
183 map_entry->Observe(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, 183 map_entry->Observe(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
184 content::Source<InfoBarTabHelper>(infobar_helper), 184 content::Source<InfoBarTabHelper>(infobar_helper),
185 content::Details<InfoBarRemovedDetails>(&removed_details)); 185 content::Details<InfoBarRemovedDetails>(&removed_details));
186 186
187 // Second, simulate the infobar container closing the infobar in response. 187 // Second, simulate the infobar container closing the infobar in response.
188 infobar->InfoBarClosed(); 188 infobar->InfoBarClosed();
189 } 189 }
190 190
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 // The pending_id should have been reset to 0 when the non-search became 296 // The pending_id should have been reset to 0 when the non-search became
297 // pending. 297 // pending.
298 EXPECT_EQ(0, map_entry->infobar()->pending_id()); 298 EXPECT_EQ(0, map_entry->infobar()->pending_id());
299 299
300 // Committing the navigation would close the infobar. 300 // Committing the navigation would close the infobar.
301 map_entry->infobar()->Close(false); 301 map_entry->infobar()->Close(false);
302 } 302 }
303 303
304 void GoogleURLTrackerTest::CommitSearch(intptr_t unique_id, 304 void GoogleURLTrackerTest::CommitSearch(intptr_t unique_id,
305 const GURL& search_url) { 305 const GURL& search_url) {
306 DCHECK(search_url.is_valid());
306 if (google_url_tracker_->registrar_.IsRegistered(google_url_tracker_.get(), 307 if (google_url_tracker_->registrar_.IsRegistered(google_url_tracker_.get(),
307 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 308 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
308 content::Source<content::NavigationController>( 309 content::Source<content::NavigationController>(
309 reinterpret_cast<content::NavigationController*>(unique_id)))) { 310 reinterpret_cast<content::NavigationController*>(unique_id)))) {
310 google_url_tracker_->OnNavigationCommittedOrTabClosed( 311 google_url_tracker_->OnNavigationCommitted(
311 reinterpret_cast<InfoBarTabHelper*>(unique_id), 312 reinterpret_cast<InfoBarTabHelper*>(unique_id),
312 search_url); 313 search_url);
313 } 314 }
314 } 315 }
315 316
316 void GoogleURLTrackerTest::DoInstantNavigation(intptr_t unique_id, 317 void GoogleURLTrackerTest::DoInstantNavigation(intptr_t unique_id,
317 const GURL& search_url) { 318 const GURL& search_url) {
318 if (!search_url.is_empty()) { 319 if (!search_url.is_empty()) {
319 google_url_tracker_->SearchCommitted(); 320 google_url_tracker_->SearchCommitted();
320 // Note that the call above might not have actually registered a listener 321 // Note that the call above might not have actually registered a listener
321 // for NOTIFICATION_INSTANT_COMMITTED if the searchdomaincheck response was 322 // for NOTIFICATION_INSTANT_COMMITTED if the searchdomaincheck response was
322 // bogus. 323 // bogus.
323 } 324 }
324 unique_ids_seen_.insert(unique_id); 325 unique_ids_seen_.insert(unique_id);
325 if (google_url_tracker_->registrar_.IsRegistered(google_url_tracker_.get(), 326 if (google_url_tracker_->registrar_.IsRegistered(google_url_tracker_.get(),
326 chrome::NOTIFICATION_INSTANT_COMMITTED, 327 chrome::NOTIFICATION_INSTANT_COMMITTED,
327 content::NotificationService::AllBrowserContextsAndSources())) { 328 content::NotificationService::AllBrowserContextsAndSources())) {
328 google_url_tracker_->OnInstantCommitted( 329 google_url_tracker_->OnInstantCommitted(
329 content::Source<content::NavigationController>( 330 content::Source<content::NavigationController>(
330 reinterpret_cast<content::NavigationController*>(unique_id)), 331 reinterpret_cast<content::NavigationController*>(unique_id)),
331 content::Source<content::WebContents>( 332 content::Source<content::WebContents>(
332 reinterpret_cast<content::WebContents*>(unique_id)), 333 reinterpret_cast<content::WebContents*>(unique_id)),
333 reinterpret_cast<InfoBarTabHelper*>(unique_id), search_url); 334 reinterpret_cast<InfoBarTabHelper*>(unique_id), search_url);
334 } 335 }
335 } 336 }
336 337
337 void GoogleURLTrackerTest::CloseTab(intptr_t unique_id) { 338 void GoogleURLTrackerTest::CloseTab(intptr_t unique_id) {
338 unique_ids_seen_.erase(unique_id); 339 unique_ids_seen_.erase(unique_id);
339 InfoBarTabHelper* infobar_helper = 340 content::Source<content::WebContents> source(
340 reinterpret_cast<InfoBarTabHelper*>(unique_id); 341 reinterpret_cast<content::WebContents*>(unique_id));
341 if (google_url_tracker_->registrar_.IsRegistered( 342 if (google_url_tracker_->registrar_.IsRegistered(
342 google_url_tracker_.get(), content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 343 google_url_tracker_.get(), content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
343 content::Source<content::WebContents>( 344 source)) {
344 reinterpret_cast<content::WebContents*>(unique_id)))) { 345 google_url_tracker_->OnTabClosed(source);
345 google_url_tracker_->OnNavigationCommittedOrTabClosed(infobar_helper,
346 GURL());
347 } else { 346 } else {
348 // Closing a tab with an infobar showing would close the infobar. 347 // Closing a tab with an infobar showing would close the infobar.
349 GoogleURLTrackerInfoBarDelegate* infobar = GetInfoBar(unique_id); 348 GoogleURLTrackerInfoBarDelegate* infobar = GetInfoBar(unique_id);
350 if (infobar) 349 if (infobar)
351 infobar->Close(false); 350 infobar->Close(false);
352 } 351 }
353 } 352 }
354 353
355 GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry( 354 GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry(
356 intptr_t unique_id) { 355 intptr_t unique_id) {
357 GoogleURLTracker::InfoBarMap::const_iterator i = 356 GoogleURLTracker::EntryMap::const_iterator i =
358 google_url_tracker_->infobar_map_.find( 357 google_url_tracker_->entry_map_.find(
359 reinterpret_cast<InfoBarTabHelper*>(unique_id)); 358 reinterpret_cast<InfoBarTabHelper*>(unique_id));
360 return (i == google_url_tracker_->infobar_map_.end()) ? NULL : i->second; 359 return (i == google_url_tracker_->entry_map_.end()) ? NULL : i->second;
361 } 360 }
362 361
363 GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::GetInfoBar( 362 GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::GetInfoBar(
364 intptr_t unique_id) { 363 intptr_t unique_id) {
365 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); 364 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
366 return map_entry ? map_entry->infobar() : NULL; 365 return map_entry ? map_entry->infobar() : NULL;
367 } 366 }
368 367
369 void GoogleURLTrackerTest::ExpectDefaultURLs() const { 368 void GoogleURLTrackerTest::ExpectDefaultURLs() const {
370 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); 369 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
(...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after
1030 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2")); 1029 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2"));
1031 EXPECT_FALSE(GetInfoBar(1) == NULL); 1030 EXPECT_FALSE(GetInfoBar(1) == NULL);
1032 GoogleURLTrackerInfoBarDelegate* infobar2 = GetInfoBar(2); 1031 GoogleURLTrackerInfoBarDelegate* infobar2 = GetInfoBar(2);
1033 ASSERT_FALSE(infobar2 == NULL); 1032 ASSERT_FALSE(infobar2 == NULL);
1034 SetNavigationPending(1, true); 1033 SetNavigationPending(1, true);
1035 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true)); 1034 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
1036 infobar2->Close(false); 1035 infobar2->Close(false);
1037 SetNavigationPending(1, false); 1036 SetNavigationPending(1, false);
1038 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false)); 1037 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
1039 } 1038 }
OLDNEW
« no previous file with comments | « chrome/browser/google/google_url_tracker_map_entry.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698