OLD | NEW |
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 <set> | 5 #include <set> |
6 #include <string> | 6 #include <string> |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/hash_tables.h" | 10 #include "base/hash_tables.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include "content/test/test_renderer_host.h" | 29 #include "content/test/test_renderer_host.h" |
30 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
31 | 31 |
32 using content::BrowserThread; | 32 using content::BrowserThread; |
33 using content::MockRenderProcessHost; | 33 using content::MockRenderProcessHost; |
34 using content::RenderViewHostTester; | 34 using content::RenderViewHostTester; |
35 using content::WebContents; | 35 using content::WebContents; |
36 | 36 |
37 // ClosedDelegateTracker ------------------------------------------------------ | 37 // ClosedDelegateTracker ------------------------------------------------------ |
38 | 38 |
39 namespace { | |
40 | |
41 // We need to track which infobars were closed. | 39 // We need to track which infobars were closed. |
42 class ClosedDelegateTracker : public content::NotificationObserver { | 40 class ClosedDelegateTracker : public content::NotificationObserver { |
43 public: | 41 public: |
44 ClosedDelegateTracker(); | 42 ClosedDelegateTracker(); |
45 virtual ~ClosedDelegateTracker(); | 43 virtual ~ClosedDelegateTracker(); |
46 | 44 |
47 // content::NotificationObserver: | 45 // content::NotificationObserver: |
48 virtual void Observe(int type, | 46 virtual void Observe(int type, |
49 const content::NotificationSource& source, | 47 const content::NotificationSource& source, |
50 const content::NotificationDetails& details); | 48 const content::NotificationDetails& details); |
51 | 49 |
52 size_t size() const { | 50 size_t size() const { |
53 return removed_infobar_delegates_.size(); | 51 return removed_infobar_delegates_.size(); |
54 } | 52 } |
55 | 53 |
56 bool Contains(InfoBarDelegate* delegate) const; | 54 bool Contains(InfoBarDelegate* delegate) const; |
57 void Clear(); | 55 void Clear(); |
58 | 56 |
59 private: | 57 private: |
| 58 FRIEND_TEST_ALL_PREFIXES(GeolocationPermissionContextTests, TabDestroyed); |
60 content::NotificationRegistrar registrar_; | 59 content::NotificationRegistrar registrar_; |
61 std::set<InfoBarDelegate*> removed_infobar_delegates_; | 60 std::set<InfoBarDelegate*> removed_infobar_delegates_; |
62 }; | 61 }; |
63 | 62 |
64 ClosedDelegateTracker::ClosedDelegateTracker() { | 63 ClosedDelegateTracker::ClosedDelegateTracker() { |
65 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | 64 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
66 content::NotificationService::AllSources()); | 65 content::NotificationService::AllSources()); |
67 } | 66 } |
68 | 67 |
69 ClosedDelegateTracker::~ClosedDelegateTracker() { | 68 ClosedDelegateTracker::~ClosedDelegateTracker() { |
70 } | 69 } |
71 | 70 |
72 void ClosedDelegateTracker::Observe( | 71 void ClosedDelegateTracker::Observe( |
73 int type, | 72 int type, |
74 const content::NotificationSource& source, | 73 const content::NotificationSource& source, |
75 const content::NotificationDetails& details) { | 74 const content::NotificationDetails& details) { |
76 DCHECK(type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED); | 75 DCHECK(type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED); |
77 removed_infobar_delegates_.insert( | 76 removed_infobar_delegates_.insert( |
78 content::Details<InfoBarRemovedDetails>(details)->first); | 77 content::Details<InfoBarRemovedDetails>(details)->first); |
79 } | 78 } |
80 | 79 |
81 bool ClosedDelegateTracker::Contains(InfoBarDelegate* delegate) const { | 80 bool ClosedDelegateTracker::Contains(InfoBarDelegate* delegate) const { |
82 return removed_infobar_delegates_.count(delegate) != 0; | 81 return removed_infobar_delegates_.count(delegate) != 0; |
83 } | 82 } |
84 | 83 |
85 void ClosedDelegateTracker::Clear() { | 84 void ClosedDelegateTracker::Clear() { |
86 removed_infobar_delegates_.clear(); | 85 removed_infobar_delegates_.clear(); |
87 } | 86 } |
88 | 87 |
89 } // namespace | |
90 | |
91 | |
92 // GeolocationPermissionContextTests ------------------------------------------ | 88 // GeolocationPermissionContextTests ------------------------------------------ |
93 | 89 |
94 // This class sets up GeolocationArbitrator. | 90 // This class sets up GeolocationArbitrator. |
95 class GeolocationPermissionContextTests : public TabContentsWrapperTestHarness { | 91 class GeolocationPermissionContextTests : public TabContentsWrapperTestHarness { |
96 public: | 92 public: |
97 GeolocationPermissionContextTests(); | 93 GeolocationPermissionContextTests(); |
98 | 94 |
99 protected: | 95 protected: |
100 virtual ~GeolocationPermissionContextTests(); | 96 virtual ~GeolocationPermissionContextTests(); |
101 | 97 |
(...skipping 30 matching lines...) Expand all Loading... |
132 void CheckTabContentsState(const GURL& requesting_frame, | 128 void CheckTabContentsState(const GURL& requesting_frame, |
133 ContentSetting expected_content_setting); | 129 ContentSetting expected_content_setting); |
134 | 130 |
135 scoped_refptr<ChromeGeolocationPermissionContext> | 131 scoped_refptr<ChromeGeolocationPermissionContext> |
136 geolocation_permission_context_; | 132 geolocation_permission_context_; |
137 ClosedDelegateTracker closed_delegate_tracker_; | 133 ClosedDelegateTracker closed_delegate_tracker_; |
138 ScopedVector<TabContentsWrapper> extra_tabs_; | 134 ScopedVector<TabContentsWrapper> extra_tabs_; |
139 | 135 |
140 private: | 136 private: |
141 // TabContentsWrapperTestHarness: | 137 // TabContentsWrapperTestHarness: |
142 virtual void SetUp(); | 138 virtual void SetUp() OVERRIDE; |
143 virtual void TearDown(); | 139 virtual void TearDown() OVERRIDE; |
144 | 140 |
145 content::TestBrowserThread ui_thread_; | 141 content::TestBrowserThread ui_thread_; |
146 content::MockGeolocation mock_geolocation_; | 142 content::MockGeolocation mock_geolocation_; |
147 | 143 |
148 // A map between renderer child id and a pair represending the bridge id and | 144 // A map between renderer child id and a pair represending the bridge id and |
149 // whether the requested permission was allowed. | 145 // whether the requested permission was allowed. |
150 base::hash_map<int, std::pair<int, bool> > responses_; | 146 base::hash_map<int, std::pair<int, bool> > responses_; |
151 }; | 147 }; |
152 | 148 |
153 GeolocationPermissionContextTests::GeolocationPermissionContextTests() | 149 GeolocationPermissionContextTests::GeolocationPermissionContextTests() |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 } | 228 } |
233 | 229 |
234 void GeolocationPermissionContextTests::SetUp() { | 230 void GeolocationPermissionContextTests::SetUp() { |
235 TabContentsWrapperTestHarness::SetUp(); | 231 TabContentsWrapperTestHarness::SetUp(); |
236 mock_geolocation_.Setup(); | 232 mock_geolocation_.Setup(); |
237 geolocation_permission_context_ = | 233 geolocation_permission_context_ = |
238 new ChromeGeolocationPermissionContext(profile()); | 234 new ChromeGeolocationPermissionContext(profile()); |
239 } | 235 } |
240 | 236 |
241 void GeolocationPermissionContextTests::TearDown() { | 237 void GeolocationPermissionContextTests::TearDown() { |
| 238 extra_tabs_.reset(); |
242 mock_geolocation_.TearDown(); | 239 mock_geolocation_.TearDown(); |
243 TabContentsWrapperTestHarness::TearDown(); | 240 TabContentsWrapperTestHarness::TearDown(); |
244 } | 241 } |
245 | 242 |
246 | 243 |
247 // Tests ---------------------------------------------------------------------- | 244 // Tests ---------------------------------------------------------------------- |
248 | 245 |
249 TEST_F(GeolocationPermissionContextTests, SinglePermission) { | 246 TEST_F(GeolocationPermissionContextTests, SinglePermission) { |
250 GURL requesting_frame("http://www.example.com/geolocation"); | 247 GURL requesting_frame("http://www.example.com/geolocation"); |
251 NavigateAndCommit(requesting_frame); | 248 NavigateAndCommit(requesting_frame); |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 | 461 |
465 // But the other tab should still have the info bar... | 462 // But the other tab should still have the info bar... |
466 ASSERT_EQ(1U, extra_tabs_[0]->infobar_tab_helper()->infobar_count()); | 463 ASSERT_EQ(1U, extra_tabs_[0]->infobar_tab_helper()->infobar_count()); |
467 ConfirmInfoBarDelegate* infobar_1 = extra_tabs_[0]->infobar_tab_helper()-> | 464 ConfirmInfoBarDelegate* infobar_1 = extra_tabs_[0]->infobar_tab_helper()-> |
468 GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate(); | 465 GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate(); |
469 infobar_1->Cancel(); | 466 infobar_1->Cancel(); |
470 extra_tabs_[0]->infobar_tab_helper()->RemoveInfoBar(infobar_1); | 467 extra_tabs_[0]->infobar_tab_helper()->RemoveInfoBar(infobar_1); |
471 EXPECT_EQ(1U, closed_delegate_tracker_.size()); | 468 EXPECT_EQ(1U, closed_delegate_tracker_.size()); |
472 EXPECT_TRUE(closed_delegate_tracker_.Contains(infobar_1)); | 469 EXPECT_TRUE(closed_delegate_tracker_.Contains(infobar_1)); |
473 infobar_1->InfoBarClosed(); | 470 infobar_1->InfoBarClosed(); |
474 | |
475 extra_tabs_.reset(); | |
476 } | 471 } |
477 | 472 |
478 TEST_F(GeolocationPermissionContextTests, QueuedOriginMultipleTabs) { | 473 TEST_F(GeolocationPermissionContextTests, QueuedOriginMultipleTabs) { |
479 GURL url_a("http://www.example.com/geolocation"); | 474 GURL url_a("http://www.example.com/geolocation"); |
480 GURL url_b("http://www.example-2.com/geolocation"); | 475 GURL url_b("http://www.example-2.com/geolocation"); |
481 NavigateAndCommit(url_a); | 476 NavigateAndCommit(url_a); |
482 AddNewTab(url_a); | 477 AddNewTab(url_a); |
483 | 478 |
484 EXPECT_EQ(0U, infobar_tab_helper()->infobar_count()); | 479 EXPECT_EQ(0U, infobar_tab_helper()->infobar_count()); |
485 RequestGeolocationPermission( | 480 RequestGeolocationPermission( |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 // Accept the second infobar. | 516 // Accept the second infobar. |
522 ConfirmInfoBarDelegate* infobar_1 = extra_tabs_[0]->infobar_tab_helper()-> | 517 ConfirmInfoBarDelegate* infobar_1 = extra_tabs_[0]->infobar_tab_helper()-> |
523 GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate(); | 518 GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate(); |
524 ASSERT_TRUE(infobar_1); | 519 ASSERT_TRUE(infobar_1); |
525 infobar_1->Accept(); | 520 infobar_1->Accept(); |
526 CheckPermissionMessageSentForTab(0, bridge_id() + 1, true); | 521 CheckPermissionMessageSentForTab(0, bridge_id() + 1, true); |
527 extra_tabs_[0]->infobar_tab_helper()->RemoveInfoBar(infobar_1); | 522 extra_tabs_[0]->infobar_tab_helper()->RemoveInfoBar(infobar_1); |
528 EXPECT_EQ(1U, closed_delegate_tracker_.size()); | 523 EXPECT_EQ(1U, closed_delegate_tracker_.size()); |
529 EXPECT_TRUE(closed_delegate_tracker_.Contains(infobar_1)); | 524 EXPECT_TRUE(closed_delegate_tracker_.Contains(infobar_1)); |
530 infobar_1->InfoBarClosed(); | 525 infobar_1->InfoBarClosed(); |
531 | |
532 extra_tabs_.reset(); | |
533 } | 526 } |
534 | 527 |
535 TEST_F(GeolocationPermissionContextTests, TabDestroyed) { | 528 TEST_F(GeolocationPermissionContextTests, TabDestroyed) { |
536 GURL requesting_frame_0("http://www.example.com/geolocation"); | 529 GURL requesting_frame_0("http://www.example.com/geolocation"); |
537 GURL requesting_frame_1("http://www.example-2.com/geolocation"); | 530 GURL requesting_frame_1("http://www.example-2.com/geolocation"); |
538 EXPECT_EQ( | 531 EXPECT_EQ( |
539 CONTENT_SETTING_ASK, | 532 CONTENT_SETTING_ASK, |
540 profile()->GetHostContentSettingsMap()->GetContentSetting( | 533 profile()->GetHostContentSettingsMap()->GetContentSetting( |
541 requesting_frame_0, | 534 requesting_frame_0, |
542 requesting_frame_0, | 535 requesting_frame_0, |
(...skipping 17 matching lines...) Expand all Loading... |
560 process_id(), render_id(), bridge_id() + 1, requesting_frame_1); | 553 process_id(), render_id(), bridge_id() + 1, requesting_frame_1); |
561 // Ensure only one infobar is created. | 554 // Ensure only one infobar is created. |
562 ASSERT_EQ(1U, infobar_tab_helper()->infobar_count()); | 555 ASSERT_EQ(1U, infobar_tab_helper()->infobar_count()); |
563 ConfirmInfoBarDelegate* infobar_0 = infobar_tab_helper()-> | 556 ConfirmInfoBarDelegate* infobar_0 = infobar_tab_helper()-> |
564 GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate(); | 557 GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate(); |
565 ASSERT_TRUE(infobar_0); | 558 ASSERT_TRUE(infobar_0); |
566 | 559 |
567 // Delete the tab contents. | 560 // Delete the tab contents. |
568 DeleteContents(); | 561 DeleteContents(); |
569 infobar_0->InfoBarClosed(); | 562 infobar_0->InfoBarClosed(); |
| 563 |
| 564 // During contents destruction, the infobar will have been closed, and a |
| 565 // second (with it's own new delegate) will have been created. In Chromium, |
| 566 // this would be properly deleted by the InfoBarContainer, but in this unit |
| 567 // test, the closest thing we have to that is the ClosedDelegateTracker. |
| 568 ASSERT_EQ(2U, closed_delegate_tracker_.size()); |
| 569 ASSERT_TRUE(closed_delegate_tracker_.Contains(infobar_0)); |
| 570 closed_delegate_tracker_.removed_infobar_delegates_.erase(infobar_0); |
| 571 (*closed_delegate_tracker_.removed_infobar_delegates_.begin())-> |
| 572 InfoBarClosed(); |
570 } | 573 } |
571 | 574 |
572 TEST_F(GeolocationPermissionContextTests, InfoBarUsesCommittedEntry) { | 575 TEST_F(GeolocationPermissionContextTests, InfoBarUsesCommittedEntry) { |
573 GURL requesting_frame_0("http://www.example.com/geolocation"); | 576 GURL requesting_frame_0("http://www.example.com/geolocation"); |
574 GURL requesting_frame_1("http://www.example-2.com/geolocation"); | 577 GURL requesting_frame_1("http://www.example-2.com/geolocation"); |
575 NavigateAndCommit(requesting_frame_0); | 578 NavigateAndCommit(requesting_frame_0); |
576 NavigateAndCommit(requesting_frame_1); | 579 NavigateAndCommit(requesting_frame_1); |
577 EXPECT_EQ(0U, infobar_tab_helper()->infobar_count()); | 580 EXPECT_EQ(0U, infobar_tab_helper()->infobar_count()); |
578 // Go back: navigate to a pending entry before requesting geolocation | 581 // Go back: navigate to a pending entry before requesting geolocation |
579 // permission. | 582 // permission. |
(...skipping 11 matching lines...) Expand all Loading... |
591 ASSERT_FALSE(infobar_0->ShouldExpire(details)); | 594 ASSERT_FALSE(infobar_0->ShouldExpire(details)); |
592 // Commit the "GoBack()" above, and ensure the infobar is now expired. | 595 // Commit the "GoBack()" above, and ensure the infobar is now expired. |
593 contents()->CommitPendingNavigation(); | 596 contents()->CommitPendingNavigation(); |
594 details.entry = contents()->GetController().GetLastCommittedEntry(); | 597 details.entry = contents()->GetController().GetLastCommittedEntry(); |
595 ASSERT_TRUE(infobar_0->ShouldExpire(details)); | 598 ASSERT_TRUE(infobar_0->ShouldExpire(details)); |
596 | 599 |
597 // Delete the tab contents. | 600 // Delete the tab contents. |
598 DeleteContents(); | 601 DeleteContents(); |
599 infobar_0->InfoBarClosed(); | 602 infobar_0->InfoBarClosed(); |
600 } | 603 } |
| 604 |
| 605 |
OLD | NEW |