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

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: Rebase + review comments Created 6 years, 6 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
« 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 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"
13 #include "chrome/browser/google/google_url_tracker_factory.h" 14 #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_infobar_delegate.h"
15 #include "chrome/browser/google/google_url_tracker_navigation_helper.h" 16 #include "chrome/browser/google/google_url_tracker_navigation_helper.h"
16 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/testing_profile.h" 18 #include "chrome/test/base/testing_profile.h"
18 #include "components/google/core/browser/google_url_tracker_client.h" 19 #include "components/google/core/browser/google_url_tracker_client.h"
19 #include "components/infobars/core/infobar.h" 20 #include "components/infobars/core/infobar.h"
20 #include "components/infobars/core/infobar_delegate.h" 21 #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" 22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "net/url_request/test_url_fetcher_factory.h" 23 #include "net/url_request/test_url_fetcher_factory.h"
24 #include "net/url_request/url_fetcher.h" 24 #include "net/url_request/url_fetcher.h"
25 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
26 26
27 class GoogleURLTrackerTest;
28
29 namespace { 27 namespace {
30 28
31 // TestInfoBarDelegate --------------------------------------------------------
32
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_manager|, since
37 // that "pointer" is really just a magic number. Thus there is no
38 // InfoBarManager ownership of the returned object; and since the caller
39 // doesn't own the returned object, we rely on |test_harness| cleaning this
40 // up eventually in GoogleURLTrackerTest::OnInfoBarClosed() to avoid leaks.
41 static infobars::InfoBar* Create(
42 GoogleURLTrackerTest* test_harness,
43 infobars::InfoBarManager* infobar_manager,
44 GoogleURLTracker* google_url_tracker,
45 const GURL& search_url);
46
47 private:
48 TestInfoBarDelegate(GoogleURLTrackerTest* test_harness,
49 infobars::InfoBarManager* infobar_manager,
50 GoogleURLTracker* google_url_tracker,
51 const GURL& search_url);
52 virtual ~TestInfoBarDelegate();
53
54 // GoogleURLTrackerInfoBarDelegate:
55 virtual void Update(const GURL& search_url) OVERRIDE;
56 virtual void Close(bool redo_search) OVERRIDE;
57
58 GoogleURLTrackerTest* test_harness_;
59 infobars::InfoBarManager* infobar_manager_;
60
61 DISALLOW_COPY_AND_ASSIGN(TestInfoBarDelegate);
62 };
63
64 // The member function definitions come after the declaration of
65 // GoogleURLTrackerTest, so they can call members on it.
66
67
68 // TestCallbackListener --------------------------------------------------- 29 // TestCallbackListener ---------------------------------------------------
69 30
70 class TestCallbackListener { 31 class TestCallbackListener {
71 public: 32 public:
72 TestCallbackListener(); 33 TestCallbackListener();
73 virtual ~TestCallbackListener(); 34 virtual ~TestCallbackListener();
74 35
75 bool HasRegisteredCallback(); 36 bool HasRegisteredCallback();
76 void RegisterCallback(GoogleURLTracker* google_url_tracker); 37 void RegisterCallback(GoogleURLTracker* google_url_tracker);
77 38
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 bool TestGoogleURLTrackerNavigationHelper::IsListeningForTabDestruction() { 155 bool TestGoogleURLTrackerNavigationHelper::IsListeningForTabDestruction() {
195 return listening_for_tab_destruction_; 156 return listening_for_tab_destruction_;
196 } 157 }
197 158
198 void TestGoogleURLTrackerNavigationHelper::OpenURL( 159 void TestGoogleURLTrackerNavigationHelper::OpenURL(
199 GURL url, 160 GURL url,
200 WindowOpenDisposition disposition, 161 WindowOpenDisposition disposition,
201 bool user_clicked_on_link) { 162 bool user_clicked_on_link) {
202 } 163 }
203 164
165
166 // TestInfoBarManager ---------------------------------------------------------
167
168 class TestInfoBarManager : public infobars::InfoBarManager {
169 public:
170 virtual int GetActiveEntryID() OVERRIDE;
171 };
172
173 int TestInfoBarManager::GetActiveEntryID() {
174 return 0;
175 }
176
204 } // namespace 177 } // namespace
205 178
206
207 // GoogleURLTrackerTest ------------------------------------------------------- 179 // GoogleURLTrackerTest -------------------------------------------------------
208 180
209 // Ths class exercises GoogleURLTracker. In order to avoid instantiating more
210 // of the Chrome infrastructure than necessary, the GoogleURLTracker functions
211 // are carefully written so that many of the functions which take
212 // NavigationController* or infobars::InfoBarManager* do not actually
213 // dereference the
214 // objects, merely use them for comparisons and lookups, e.g. in |entry_map_|.
215 // This then allows the test code here to not create any of these objects, and
216 // instead supply "pointers" that are actually reinterpret_cast<>()ed magic
217 // numbers. Then we write the necessary stubs/hooks, here and in
218 // TestInfoBarDelegate above, to make everything continue to work.
219 //
220 // Technically, the C++98 spec defines the result of casting
221 // T* -> intptr_t -> T* to be an identity, but intptr_t -> T* -> intptr_t (what
222 // we use here) is "implementation-defined". Since I've never seen a compiler
223 // break this, though, and the result would simply be a failing test rather than
224 // a bug in Chrome, we'll use it anyway.
225 class GoogleURLTrackerTest : public testing::Test { 181 class GoogleURLTrackerTest : public testing::Test {
226 public:
227 // Called by TestInfoBarDelegate::Close().
228 void OnInfoBarClosed(scoped_ptr<infobars::InfoBar> infobar,
229 infobars::InfoBarManager* infobar_manager);
230
231 protected: 182 protected:
232 GoogleURLTrackerTest(); 183 GoogleURLTrackerTest();
233 virtual ~GoogleURLTrackerTest(); 184 virtual ~GoogleURLTrackerTest();
234 185
235 // testing::Test 186 // testing::Test
236 virtual void SetUp() OVERRIDE; 187 virtual void SetUp() OVERRIDE;
237 virtual void TearDown() OVERRIDE; 188 virtual void TearDown() OVERRIDE;
238 189
239 net::TestURLFetcher* GetFetcher(); 190 net::TestURLFetcher* GetFetcher();
240 void MockSearchDomainCheckResponse(const std::string& domain); 191 void MockSearchDomainCheckResponse(const std::string& domain);
241 void RequestServerCheck(); 192 void RequestServerCheck();
242 void FinishSleep(); 193 void FinishSleep();
243 void NotifyIPAddressChanged(); 194 void NotifyIPAddressChanged();
244 GURL fetched_google_url() const { 195 GURL fetched_google_url() const {
245 return google_url_tracker_->fetched_google_url(); 196 return google_url_tracker_->fetched_google_url();
246 } 197 }
247 void set_google_url(const GURL& url) { 198 void set_google_url(const GURL& url) {
248 google_url_tracker_->google_url_ = url; 199 google_url_tracker_->google_url_ = url;
249 } 200 }
250 GURL google_url() const { return google_url_tracker_->google_url(); } 201 GURL google_url() const { return google_url_tracker_->google_url(); }
251 void SetLastPromptedGoogleURL(const GURL& url); 202 void SetLastPromptedGoogleURL(const GURL& url);
252 GURL GetLastPromptedGoogleURL(); 203 GURL GetLastPromptedGoogleURL();
253 void SetNavigationPending(intptr_t unique_id, bool is_search); 204 void SetNavigationPending(int unique_id, bool is_search);
254 void CommitNonSearch(intptr_t unique_id); 205 void CommitNonSearch(int unique_id);
255 void CommitSearch(intptr_t unique_id, const GURL& search_url); 206 void CommitSearch(int unique_id, const GURL& search_url);
256 void CloseTab(intptr_t unique_id); 207 void CloseTab(int unique_id);
257 GoogleURLTrackerMapEntry* GetMapEntry(intptr_t unique_id); 208 GoogleURLTrackerMapEntry* GetMapEntry(int unique_id);
258 GoogleURLTrackerInfoBarDelegate* GetInfoBarDelegate(intptr_t unique_id); 209 GoogleURLTrackerInfoBarDelegate* GetInfoBarDelegate(int unique_id);
259 GoogleURLTrackerNavigationHelper* GetNavigationHelper(intptr_t unique_id); 210 GoogleURLTrackerNavigationHelper* GetNavigationHelper(int unique_id);
260 void ExpectDefaultURLs() const; 211 void ExpectDefaultURLs() const;
261 void ExpectListeningForCommit(intptr_t unique_id, bool listening); 212 void ExpectListeningForCommit(int unique_id, bool listening);
262 bool listener_notified() const { return listener_.notified(); } 213 bool listener_notified() const { return listener_.notified(); }
263 void clear_listener_notified() { listener_.clear_notified(); } 214 void clear_listener_notified() { listener_.clear_notified(); }
264 215
265 private: 216 private:
266 // Since |infobar_manager| is really a magic number rather than an actual
267 // object, we don't add the created infobar to it. Instead we will simulate
268 // any helper<->infobar interaction necessary. The returned object will be
269 // cleaned up in OnInfoBarClosed().
270 infobars::InfoBar* CreateTestInfoBar(
271 infobars::InfoBarManager* infobar_manager,
272 GoogleURLTracker* google_url_tracker,
273 const GURL& search_url);
274
275 // These are required by the TestURLFetchers GoogleURLTracker will create (see 217 // These are required by the TestURLFetchers GoogleURLTracker will create (see
276 // test_url_fetcher_factory.h). 218 // test_url_fetcher_factory.h).
277 content::TestBrowserThreadBundle thread_bundle_; 219 content::TestBrowserThreadBundle thread_bundle_;
220
221 // Gets the infobar manager indexed by |unique_id|.
222 // Lazily creates WebContents instances, adds them in |infobar_manager_map_|
blundell 2014/06/02 15:02:40 This comment is stale now, right?
droger 2014/06/02 15:56:29 No.
blundell 2014/06/02 16:00:09 But you're not creating WebContents instances anym
223 // and attaches InfoBarManagers to them.
224 infobars::InfoBarManager* GetInfoBarManager(int unique_id);
225
278 // Creating this allows us to call 226 // Creating this allows us to call
279 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(). 227 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests().
280 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; 228 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
281 net::TestURLFetcherFactory fetcher_factory_; 229 net::TestURLFetcherFactory fetcher_factory_;
282 GoogleURLTrackerClient* client_; 230 GoogleURLTrackerClient* client_;
283 TestingProfile profile_; 231 TestingProfile profile_;
284 scoped_ptr<GoogleURLTracker> google_url_tracker_; 232 scoped_ptr<GoogleURLTracker> google_url_tracker_;
285 TestCallbackListener listener_; 233 TestCallbackListener listener_;
234 base::ScopedPtrHashMap<int, infobars::InfoBarManager> infobar_manager_map_;
blundell 2014/06/02 15:02:40 This is probably a big change, but do you even nee
droger 2014/06/02 15:56:29 I need something to own the InfoBarManagers, as th
blundell 2014/06/02 16:00:09 The "something" could just be like 3 (or however m
droger 2014/06/02 16:50:04 Done. Although SetNavigationPending still requires
286 // This tracks the different "tabs" a test has "opened", so we can close them 235 // This tracks the different "tabs" a test has "opened", so we can close them
287 // properly before shutting down |google_url_tracker_|, which expects that. 236 // properly before shutting down |google_url_tracker_|, which expects that.
288 std::set<int> unique_ids_seen_; 237 std::set<int> unique_ids_seen_;
289 }; 238 };
290 239
291 void GoogleURLTrackerTest::OnInfoBarClosed(
292 scoped_ptr<infobars::InfoBar> infobar,
293 infobars::InfoBarManager* infobar_manager) {
294 // First, simulate the InfoBarManager firing INFOBAR_REMOVED.
295 // TODO(droger): Replace this flow with a call to the observer method once
296 // the map entry is observing InfoBarManager. crbug.com/373243
297 infobars::InfoBar::RemovedDetails removed_details(infobar.get(), false);
298 GoogleURLTracker::EntryMap::const_iterator i =
299 google_url_tracker_->entry_map_.find(infobar_manager);
300 ASSERT_FALSE(i == google_url_tracker_->entry_map_.end());
301 GoogleURLTrackerMapEntry* map_entry = i->second;
302 ASSERT_EQ(infobar->delegate(), map_entry->infobar_delegate());
303 map_entry->Observe(
304 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
305 content::Source<infobars::InfoBarManager>(infobar_manager),
306 content::Details<infobars::InfoBar::RemovedDetails>(&removed_details));
307
308 // Second, simulate the infobar container closing the infobar in response.
309 // This happens automatically as |infobar| goes out of scope.
310 }
311
312 GoogleURLTrackerTest::GoogleURLTrackerTest() 240 GoogleURLTrackerTest::GoogleURLTrackerTest()
313 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { 241 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
314 GoogleURLTrackerFactory::GetInstance()-> 242 GoogleURLTrackerFactory::GetInstance()->
315 RegisterUserPrefsOnBrowserContextForTest(&profile_); 243 RegisterUserPrefsOnBrowserContextForTest(&profile_);
316 } 244 }
317 245
318 GoogleURLTrackerTest::~GoogleURLTrackerTest() { 246 GoogleURLTrackerTest::~GoogleURLTrackerTest() {
319 } 247 }
320 248
321 void GoogleURLTrackerTest::SetUp() { 249 void GoogleURLTrackerTest::SetUp() {
322 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); 250 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock());
323 // Ownership is passed to google_url_tracker_, but a weak pointer is kept; 251 // Ownership is passed to google_url_tracker_, but a weak pointer is kept;
324 // this is safe since GoogleURLTracker keeps the client for its lifetime. 252 // this is safe since GoogleURLTracker keeps the client for its lifetime.
325 client_ = new TestGoogleURLTrackerClient(); 253 client_ = new TestGoogleURLTrackerClient();
326 scoped_ptr<GoogleURLTrackerClient> client(client_); 254 scoped_ptr<GoogleURLTrackerClient> client(client_);
327 google_url_tracker_.reset(new GoogleURLTracker( 255 google_url_tracker_.reset(new GoogleURLTracker(
328 &profile_, client.Pass(), GoogleURLTracker::UNIT_TEST_MODE)); 256 &profile_, client.Pass(), GoogleURLTracker::UNIT_TEST_MODE));
329 google_url_tracker_->infobar_creator_ = base::Bind( 257 google_url_tracker_->infobar_creator_ =
330 &GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this)); 258 base::Bind(&GoogleURLTrackerInfoBarDelegate::Create);
blundell 2014/06/02 15:02:40 This is just calling the production creation funct
droger 2014/06/02 15:56:29 Done.
331 } 259 }
332 260
333 void GoogleURLTrackerTest::TearDown() { 261 void GoogleURLTrackerTest::TearDown() {
334 while (!unique_ids_seen_.empty()) 262 while (!unique_ids_seen_.empty())
335 CloseTab(*unique_ids_seen_.begin()); 263 CloseTab(*unique_ids_seen_.begin());
336 google_url_tracker_->Shutdown(); 264 google_url_tracker_->Shutdown();
337 } 265 }
338 266
339 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() { 267 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() {
340 // This will return the last fetcher created. If no fetchers have been 268 // This will return the last fetcher created. If no fetchers have been
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 } 301 }
374 302
375 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) { 303 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) {
376 profile_.GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, url.spec()); 304 profile_.GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, url.spec());
377 } 305 }
378 306
379 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() { 307 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() {
380 return GURL(profile_.GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); 308 return GURL(profile_.GetPrefs()->GetString(prefs::kLastPromptedGoogleURL));
381 } 309 }
382 310
383 void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id, 311 void GoogleURLTrackerTest::SetNavigationPending(int unique_id, bool is_search) {
384 bool is_search) {
385 if (is_search) { 312 if (is_search) {
386 google_url_tracker_->SearchCommitted(); 313 google_url_tracker_->SearchCommitted();
387 // Note that the call above might not have actually registered a listener 314 // Note that the call above might not have actually registered a listener
388 // for navigation starts if the searchdomaincheck response was bogus. 315 // for navigation starts if the searchdomaincheck response was bogus.
389 } 316 }
390 unique_ids_seen_.insert(unique_id); 317 unique_ids_seen_.insert(unique_id);
391 if (client_->IsListeningForNavigationStart()) { 318 if (client_->IsListeningForNavigationStart()) {
392 google_url_tracker_->OnNavigationPending( 319 google_url_tracker_->OnNavigationPending(
393 scoped_ptr<GoogleURLTrackerNavigationHelper>( 320 scoped_ptr<GoogleURLTrackerNavigationHelper>(
394 new TestGoogleURLTrackerNavigationHelper( 321 new TestGoogleURLTrackerNavigationHelper(
395 google_url_tracker_.get())), 322 google_url_tracker_.get())),
396 reinterpret_cast<infobars::InfoBarManager*>(unique_id), 323 GetInfoBarManager(unique_id),
397 unique_id); 324 unique_id);
398 } 325 }
399 } 326 }
400 327
401 void GoogleURLTrackerTest::CommitNonSearch(intptr_t unique_id) { 328 void GoogleURLTrackerTest::CommitNonSearch(int unique_id) {
402 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); 329 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
403 if (!map_entry) 330 if (!map_entry)
404 return; 331 return;
405 332
406 ExpectListeningForCommit(unique_id, false); 333 ExpectListeningForCommit(unique_id, false);
407 334
408 // The infobar should be showing; otherwise the pending non-search should 335 // The infobar should be showing; otherwise the pending non-search should
409 // have closed it. 336 // have closed it.
410 ASSERT_TRUE(map_entry->has_infobar_delegate()); 337 ASSERT_TRUE(map_entry->has_infobar_delegate());
411 338
412 // The pending_id should have been reset to 0 when the non-search became 339 // The pending_id should have been reset to 0 when the non-search became
413 // pending. 340 // pending.
414 EXPECT_EQ(0, map_entry->infobar_delegate()->pending_id()); 341 EXPECT_EQ(0, map_entry->infobar_delegate()->pending_id());
415 342
416 // Committing the navigation would close the infobar. 343 // Committing the navigation would close the infobar.
417 map_entry->infobar_delegate()->Close(false); 344 map_entry->infobar_delegate()->Close(false);
418 } 345 }
419 346
420 void GoogleURLTrackerTest::CommitSearch(intptr_t unique_id, 347 void GoogleURLTrackerTest::CommitSearch(int unique_id, const GURL& search_url) {
421 const GURL& search_url) {
422 DCHECK(search_url.is_valid()); 348 DCHECK(search_url.is_valid());
423 GoogleURLTrackerNavigationHelper* nav_helper = GetNavigationHelper(unique_id); 349 GoogleURLTrackerNavigationHelper* nav_helper = GetNavigationHelper(unique_id);
424 if (nav_helper && nav_helper->IsListeningForNavigationCommit()) { 350 if (nav_helper && nav_helper->IsListeningForNavigationCommit()) {
425 google_url_tracker_->OnNavigationCommitted( 351 google_url_tracker_->OnNavigationCommitted(GetInfoBarManager(unique_id),
426 reinterpret_cast<infobars::InfoBarManager*>(unique_id), search_url); 352 search_url);
427 } 353 }
428 } 354 }
429 355
430 void GoogleURLTrackerTest::CloseTab(intptr_t unique_id) { 356 void GoogleURLTrackerTest::CloseTab(int unique_id) {
431 unique_ids_seen_.erase(unique_id); 357 unique_ids_seen_.erase(unique_id);
432 GoogleURLTrackerNavigationHelper* nav_helper = GetNavigationHelper(unique_id); 358 GoogleURLTrackerNavigationHelper* nav_helper = GetNavigationHelper(unique_id);
433 if (nav_helper && nav_helper->IsListeningForTabDestruction()) { 359 if (nav_helper && nav_helper->IsListeningForTabDestruction()) {
434 google_url_tracker_->OnTabClosed(nav_helper); 360 google_url_tracker_->OnTabClosed(nav_helper);
435 } else { 361 } else {
436 // Closing a tab with an infobar showing would close the infobar. 362 // Closing a tab with an infobar showing would close the infobar.
437 GoogleURLTrackerInfoBarDelegate* delegate = GetInfoBarDelegate(unique_id); 363 GoogleURLTrackerInfoBarDelegate* delegate = GetInfoBarDelegate(unique_id);
438 if (delegate) 364 if (delegate)
439 delegate->Close(false); 365 delegate->Close(false);
440 } 366 }
441 } 367 }
442 368
443 GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry( 369 GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry(int unique_id) {
444 intptr_t unique_id) {
445 GoogleURLTracker::EntryMap::const_iterator i = 370 GoogleURLTracker::EntryMap::const_iterator i =
446 google_url_tracker_->entry_map_.find( 371 google_url_tracker_->entry_map_.find(GetInfoBarManager(unique_id));
447 reinterpret_cast<infobars::InfoBarManager*>(unique_id));
448 return (i == google_url_tracker_->entry_map_.end()) ? NULL : i->second; 372 return (i == google_url_tracker_->entry_map_.end()) ? NULL : i->second;
449 } 373 }
450 374
451 GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::GetInfoBarDelegate( 375 GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::GetInfoBarDelegate(
452 intptr_t unique_id) { 376 int unique_id) {
453 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); 377 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
454 return map_entry ? map_entry->infobar_delegate() : NULL; 378 return map_entry ? map_entry->infobar_delegate() : NULL;
455 } 379 }
456 380
457 GoogleURLTrackerNavigationHelper* GoogleURLTrackerTest::GetNavigationHelper( 381 GoogleURLTrackerNavigationHelper* GoogleURLTrackerTest::GetNavigationHelper(
458 intptr_t unique_id) { 382 int unique_id) {
459 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); 383 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
460 return map_entry ? map_entry->navigation_helper() : NULL; 384 return map_entry ? map_entry->navigation_helper() : NULL;
461 } 385 }
462 386
463 void GoogleURLTrackerTest::ExpectDefaultURLs() const { 387 void GoogleURLTrackerTest::ExpectDefaultURLs() const {
464 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); 388 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
465 EXPECT_EQ(GURL(), fetched_google_url()); 389 EXPECT_EQ(GURL(), fetched_google_url());
466 } 390 }
467 391
468 void GoogleURLTrackerTest::ExpectListeningForCommit(intptr_t unique_id, 392 void GoogleURLTrackerTest::ExpectListeningForCommit(int unique_id,
469 bool listening) { 393 bool listening) {
470 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); 394 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
471 if (map_entry) { 395 if (map_entry) {
472 EXPECT_EQ(listening, 396 EXPECT_EQ(listening,
473 map_entry->navigation_helper()->IsListeningForNavigationCommit()); 397 map_entry->navigation_helper()->IsListeningForNavigationCommit());
474 } else { 398 } else {
475 EXPECT_FALSE(listening); 399 EXPECT_FALSE(listening);
476 } 400 }
477 } 401 }
478 402
479 infobars::InfoBar* GoogleURLTrackerTest::CreateTestInfoBar( 403 infobars::InfoBarManager* GoogleURLTrackerTest::GetInfoBarManager(
480 infobars::InfoBarManager* infobar_manager, 404 int unique_id) {
481 GoogleURLTracker* google_url_tracker, 405 base::ScopedPtrHashMap<int, infobars::InfoBarManager>::const_iterator it =
482 const GURL& search_url) { 406 infobar_manager_map_.find(unique_id);
483 return TestInfoBarDelegate::Create( 407
484 this, infobar_manager, google_url_tracker, search_url); 408 if (it == infobar_manager_map_.end()) {
409 // Lazily create an InfoBarManager for |unique_id|.
410 scoped_ptr<infobars::InfoBarManager> infobar_manager(
411 new TestInfoBarManager);
412 it = infobar_manager_map_.set(unique_id, infobar_manager.Pass());
413 }
414
415 DCHECK(it->second);
416 return it->second;
485 } 417 }
486 418
487
488 // TestInfoBarDelegate --------------------------------------------------------
489
490 namespace {
491
492 // static
493 infobars::InfoBar* TestInfoBarDelegate::Create(
494 GoogleURLTrackerTest* test_harness,
495 infobars::InfoBarManager* infobar_manager,
496 GoogleURLTracker* google_url_tracker,
497 const GURL& search_url) {
498 return ConfirmInfoBarDelegate::CreateInfoBar(
499 scoped_ptr<ConfirmInfoBarDelegate>(new TestInfoBarDelegate(
500 test_harness,
501 infobar_manager,
502 google_url_tracker,
503 search_url))).release();
504 }
505
506 TestInfoBarDelegate::TestInfoBarDelegate(
507 GoogleURLTrackerTest* test_harness,
508 infobars::InfoBarManager* infobar_manager,
509 GoogleURLTracker* google_url_tracker,
510 const GURL& search_url)
511 : GoogleURLTrackerInfoBarDelegate(google_url_tracker,
512 search_url),
513 test_harness_(test_harness),
514 infobar_manager_(infobar_manager) {
515 }
516
517 TestInfoBarDelegate::~TestInfoBarDelegate() {
518 }
519
520 void TestInfoBarDelegate::Update(const GURL& search_url) {
521 set_search_url(search_url);
522 set_pending_id(0);
523 }
524
525 void TestInfoBarDelegate::Close(bool redo_search) {
526 test_harness_->OnInfoBarClosed(scoped_ptr<infobars::InfoBar>(infobar()),
527 infobar_manager_);
528 // WARNING: At this point |this| has been deleted!
529 }
530
531 } // namespace
532
533
534 // Tests ---------------------------------------------------------------------- 419 // Tests ----------------------------------------------------------------------
535 420
536 TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) { 421 TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) {
537 ExpectDefaultURLs(); 422 ExpectDefaultURLs();
538 FinishSleep(); 423 FinishSleep();
539 // No one called RequestServerCheck() so nothing should have happened. 424 // No one called RequestServerCheck() so nothing should have happened.
540 EXPECT_FALSE(GetFetcher()); 425 EXPECT_FALSE(GetFetcher());
541 MockSearchDomainCheckResponse("http://www.google.co.uk/"); 426 MockSearchDomainCheckResponse("http://www.google.co.uk/");
542 ExpectDefaultURLs(); 427 ExpectDefaultURLs();
543 EXPECT_FALSE(listener_notified()); 428 EXPECT_FALSE(listener_notified());
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after
1110 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2")); 995 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2"));
1111 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL); 996 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
1112 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2); 997 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2);
1113 ASSERT_FALSE(delegate2 == NULL); 998 ASSERT_FALSE(delegate2 == NULL);
1114 SetNavigationPending(1, true); 999 SetNavigationPending(1, true);
1115 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true)); 1000 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
1116 delegate2->Close(false); 1001 delegate2->Close(false);
1117 SetNavigationPending(1, false); 1002 SetNavigationPending(1, false);
1118 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false)); 1003 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
1119 } 1004 }
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