diff options
Diffstat (limited to 'chrome/browser/geolocation/geolocation_permission_context_unittest.cc')
-rw-r--r-- | chrome/browser/geolocation/geolocation_permission_context_unittest.cc | 127 |
1 files changed, 125 insertions, 2 deletions
diff --git a/chrome/browser/geolocation/geolocation_permission_context_unittest.cc b/chrome/browser/geolocation/geolocation_permission_context_unittest.cc index b17f337..e8d631d 100644 --- a/chrome/browser/geolocation/geolocation_permission_context_unittest.cc +++ b/chrome/browser/geolocation/geolocation_permission_context_unittest.cc @@ -4,6 +4,7 @@ #include "chrome/browser/geolocation/geolocation_permission_context.h" +#include "base/scoped_vector.h" #include "chrome/browser/geolocation/location_arbitrator.h" #include "chrome/browser/geolocation/location_provider.h" #include "chrome/browser/geolocation/mock_location_provider.h" @@ -76,10 +77,16 @@ class GeolocationPermissionContextTests : public RenderViewHostTestHarness { int process_id() { return contents()->render_view_host()->process()->id(); } + int process_id_for_tab(int tab) { + return extra_tabs_[tab]->render_view_host()->process()->id(); + } int render_id() { return contents()->render_view_host()->routing_id(); } + int render_id_for_tab(int tab) { + return extra_tabs_[tab]->render_view_host()->routing_id(); + } int bridge_id() { // Bridge id is not relevant at this level. @@ -87,17 +94,38 @@ class GeolocationPermissionContextTests : public RenderViewHostTestHarness { } void CheckPermissionMessageSent(int bridge_id, bool allowed) { + CheckPermissionMessageSentInternal(process(), bridge_id, allowed); + } + void CheckPermissionMessageSentForTab(int tab, int bridge_id, bool allowed) { + CheckPermissionMessageSentInternal( + static_cast<MockRenderProcessHost*>( + extra_tabs_[tab]->render_view_host()->process()), + bridge_id, allowed); + } + + void CheckPermissionMessageSentInternal(MockRenderProcessHost* process, + int bridge_id, + bool allowed) { MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); MessageLoop::current()->Run(); const IPC::Message* message = - process()->sink().GetFirstMessageMatching( + process->sink().GetFirstMessageMatching( ViewMsg_Geolocation_PermissionSet::ID); ASSERT_TRUE(message); ViewMsg_Geolocation_PermissionSet::Param param; ViewMsg_Geolocation_PermissionSet::Read(message, ¶m); EXPECT_EQ(bridge_id, param.a); EXPECT_EQ(allowed, param.b); - process()->sink().ClearMessages(); + process->sink().ClearMessages(); + } + + void AddNewTab(const GURL& url) { + TestTabContentsWithPendingInfoBar* new_tab = + new TestTabContentsWithPendingInfoBar(profile(), NULL); + new_tab->controller().LoadURL(url, GURL(), PageTransition::TYPED); + static_cast<TestRenderViewHost*>(new_tab->render_manager()-> + current_host())->SendNavigate(extra_tabs_.size() + 1, url); + extra_tabs_.push_back(new_tab); } void CheckTabContentsState(const GURL& requesting_frame, @@ -120,6 +148,7 @@ class GeolocationPermissionContextTests : public RenderViewHostTestHarness { ChromeThread ui_thread_; TestTabContentsWithPendingInfoBar* tab_contents_with_pending_infobar_; scoped_refptr<GeolocationPermissionContext> geolocation_permission_context_; + ScopedVector<TestTabContentsWithPendingInfoBar> extra_tabs_; }; TEST_F(GeolocationPermissionContextTests, SinglePermission) { @@ -154,6 +183,7 @@ TEST_F(GeolocationPermissionContextTests, QueuedPermission) { EXPECT_EQ(1, contents()->infobar_delegate_count()); ConfirmInfoBarDelegate* infobar_0 = contents()->GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate(); + ASSERT_TRUE(infobar_0); std::wstring text_0 = infobar_0->GetMessageText(); // Accept the first frame. @@ -170,6 +200,7 @@ TEST_F(GeolocationPermissionContextTests, QueuedPermission) { ConfirmInfoBarDelegate* infobar_1 = contents()->GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate(); + ASSERT_TRUE(infobar_1); std::wstring text_1 = infobar_1->GetMessageText(); EXPECT_NE(text_0, text_1); @@ -216,6 +247,7 @@ TEST_F(GeolocationPermissionContextTests, CancelGeolocationPermissionRequest) { ConfirmInfoBarDelegate* infobar_0 = contents()->GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate(); + ASSERT_TRUE(infobar_0); std::wstring text_0 = infobar_0->GetMessageText(); // Simulate the frame going away, ensure the infobar for this frame @@ -229,6 +261,7 @@ TEST_F(GeolocationPermissionContextTests, CancelGeolocationPermissionRequest) { ConfirmInfoBarDelegate* infobar_1 = contents()->GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate(); + ASSERT_TRUE(infobar_1); std::wstring text_1 = infobar_1->GetMessageText(); EXPECT_NE(text_0, text_1); @@ -261,6 +294,7 @@ TEST_F(GeolocationPermissionContextTests, StopUpdating) { EXPECT_EQ(1, contents()->infobar_delegate_count()); ConfirmInfoBarDelegate* infobar_0 = contents()->GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate(); + ASSERT_TRUE(infobar_0); geolocation_permission_context_->StopUpdatingRequested( process_id(), render_id(), bridge_id()); @@ -284,3 +318,92 @@ TEST_F(GeolocationPermissionContextTests, InvalidURL) { EXPECT_EQ(0, contents()->infobar_delegate_count()); CheckPermissionMessageSent(bridge_id(), false); } + +TEST_F(GeolocationPermissionContextTests, SameOriginMultipleTabs) { + GURL url_a("http://www.example.com/geolocation"); + GURL url_b("http://www.example-2.com/geolocation"); + NavigateAndCommit(url_a); + AddNewTab(url_b); + AddNewTab(url_a); + + EXPECT_EQ(0, contents()->infobar_delegate_count()); + geolocation_permission_context_->RequestGeolocationPermission( + process_id(), render_id(), bridge_id(), url_a); + EXPECT_EQ(1, contents()->infobar_delegate_count()); + + geolocation_permission_context_->RequestGeolocationPermission( + process_id_for_tab(0), render_id_for_tab(0), bridge_id(), url_b); + EXPECT_EQ(1, extra_tabs_[0]->infobar_delegate_count()); + + geolocation_permission_context_->RequestGeolocationPermission( + process_id_for_tab(1), render_id_for_tab(1), bridge_id(), url_a); + EXPECT_EQ(1, extra_tabs_[1]->infobar_delegate_count()); + + // Accept the first tab. + ConfirmInfoBarDelegate* infobar_0 = + contents()->GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate(); + ASSERT_TRUE(infobar_0); + infobar_0->Accept(); + CheckPermissionMessageSent(bridge_id(), true); + contents()->RemoveInfoBar(infobar_0); + EXPECT_EQ(infobar_0, + tab_contents_with_pending_infobar_->removed_infobar_delegate_); + infobar_0->InfoBarClosed(); + // Now the infobar for the tab with the same origin should have gone. + EXPECT_EQ(0, extra_tabs_[1]->infobar_delegate_count()); + CheckPermissionMessageSentForTab(1, bridge_id(), true); + + // But the other tab should still have the info bar... + EXPECT_EQ(1, extra_tabs_[0]->infobar_delegate_count()); + extra_tabs_.reset(); +} + +TEST_F(GeolocationPermissionContextTests, QueuedOriginMultipleTabs) { + GURL url_a("http://www.example.com/geolocation"); + GURL url_b("http://www.example-2.com/geolocation"); + NavigateAndCommit(url_a); + AddNewTab(url_a); + + EXPECT_EQ(0, contents()->infobar_delegate_count()); + geolocation_permission_context_->RequestGeolocationPermission( + process_id(), render_id(), bridge_id(), url_a); + EXPECT_EQ(1, contents()->infobar_delegate_count()); + + geolocation_permission_context_->RequestGeolocationPermission( + process_id_for_tab(0), render_id_for_tab(0), bridge_id(), url_a); + EXPECT_EQ(1, extra_tabs_[0]->infobar_delegate_count()); + + geolocation_permission_context_->RequestGeolocationPermission( + process_id_for_tab(0), render_id_for_tab(0), bridge_id() + 1, url_b); + EXPECT_EQ(1, extra_tabs_[0]->infobar_delegate_count()); + + // Accept the second tab. + ConfirmInfoBarDelegate* infobar_0 = + extra_tabs_[0]->GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate(); + ASSERT_TRUE(infobar_0); + infobar_0->Accept(); + CheckPermissionMessageSentForTab(0, bridge_id(), true); + extra_tabs_[0]->RemoveInfoBar(infobar_0); + EXPECT_EQ(infobar_0, + extra_tabs_[0]->removed_infobar_delegate_); + infobar_0->InfoBarClosed(); + // Now the infobar for the tab with the same origin should have gone. + EXPECT_EQ(0, contents()->infobar_delegate_count()); + CheckPermissionMessageSent(bridge_id(), true); + + // And we should have the queued infobar displayed now. + EXPECT_EQ(1, extra_tabs_[0]->infobar_delegate_count()); + + // Accept the second infobar. + ConfirmInfoBarDelegate* infobar_1 = + extra_tabs_[0]->GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate(); + ASSERT_TRUE(infobar_1); + infobar_1->Accept(); + CheckPermissionMessageSentForTab(0, bridge_id() + 1, true); + extra_tabs_[0]->RemoveInfoBar(infobar_1); + EXPECT_EQ(infobar_1, + extra_tabs_[0]->removed_infobar_delegate_); + infobar_1->InfoBarClosed(); + + extra_tabs_.reset(); +} |