Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / geolocation / chrome_geolocation_permission_context_unittest.cc
blob237a764e7dfeb1e06f60591b9a89c2fc2ad3bfca
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
3 // found in the LICENSE file.
5 #include "chrome/browser/geolocation/chrome_geolocation_permission_context.h"
7 #include <set>
8 #include <string>
9 #include <utility>
11 #include "base/bind.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/content_settings/host_content_settings_map.h"
17 #include "chrome/browser/content_settings/permission_request_id.h"
18 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
19 #include "chrome/browser/geolocation/chrome_geolocation_permission_context_factory.h"
20 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
21 #include "chrome/browser/infobars/infobar.h"
22 #include "chrome/browser/infobars/infobar_service.h"
23 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "content/public/browser/navigation_details.h"
26 #include "content/public/browser/notification_registrar.h"
27 #include "content/public/browser/notification_service.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/test/mock_render_process_host.h"
30 #include "content/public/test/test_renderer_host.h"
31 #include "content/public/test/web_contents_tester.h"
32 #include "extensions/browser/view_type_utils.h"
33 #include "testing/gtest/include/gtest/gtest.h"
35 #if defined(OS_ANDROID)
36 #include "base/prefs/pref_service.h"
37 #include "chrome/browser/android/mock_google_location_settings_helper.h"
38 #include "chrome/common/pref_names.h"
39 #endif
41 using content::MockRenderProcessHost;
44 // ClosedInfoBarTracker -------------------------------------------------------
46 // We need to track which infobars were closed.
47 class ClosedInfoBarTracker : public content::NotificationObserver {
48 public:
49 ClosedInfoBarTracker();
50 virtual ~ClosedInfoBarTracker();
52 // content::NotificationObserver:
53 virtual void Observe(int type,
54 const content::NotificationSource& source,
55 const content::NotificationDetails& details) OVERRIDE;
57 size_t size() const { return removed_infobars_.size(); }
59 bool Contains(InfoBar* infobar) const;
60 void Clear();
62 private:
63 FRIEND_TEST_ALL_PREFIXES(GeolocationPermissionContextTests, TabDestroyed);
64 content::NotificationRegistrar registrar_;
65 std::set<InfoBar*> removed_infobars_;
68 ClosedInfoBarTracker::ClosedInfoBarTracker() {
69 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
70 content::NotificationService::AllSources());
73 ClosedInfoBarTracker::~ClosedInfoBarTracker() {
76 void ClosedInfoBarTracker::Observe(
77 int type,
78 const content::NotificationSource& source,
79 const content::NotificationDetails& details) {
80 DCHECK(type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED);
81 removed_infobars_.insert(
82 content::Details<InfoBar::RemovedDetails>(details)->first);
85 bool ClosedInfoBarTracker::Contains(InfoBar* infobar) const {
86 return removed_infobars_.count(infobar) != 0;
89 void ClosedInfoBarTracker::Clear() {
90 removed_infobars_.clear();
94 // GeolocationPermissionContextTests ------------------------------------------
96 class GeolocationPermissionContextTests
97 : public ChromeRenderViewHostTestHarness {
98 protected:
99 // ChromeRenderViewHostTestHarness:
100 virtual void SetUp() OVERRIDE;
101 virtual void TearDown() OVERRIDE;
103 PermissionRequestID RequestID(int bridge_id);
104 PermissionRequestID RequestIDForTab(int tab, int bridge_id);
105 InfoBarService* infobar_service() {
106 return InfoBarService::FromWebContents(web_contents());
108 InfoBarService* infobar_service_for_tab(int tab) {
109 return InfoBarService::FromWebContents(extra_tabs_[tab]);
112 void RequestGeolocationPermission(const PermissionRequestID& id,
113 const GURL& requesting_frame);
114 void CancelGeolocationPermissionRequest(const PermissionRequestID& id,
115 const GURL& requesting_frame);
116 void PermissionResponse(const PermissionRequestID& id,
117 bool allowed);
118 void CheckPermissionMessageSent(int bridge_id, bool allowed);
119 void CheckPermissionMessageSentForTab(int tab, int bridge_id, bool allowed);
120 void CheckPermissionMessageSentInternal(MockRenderProcessHost* process,
121 int bridge_id,
122 bool allowed);
123 void AddNewTab(const GURL& url);
124 void CheckTabContentsState(const GURL& requesting_frame,
125 ContentSetting expected_content_setting);
127 scoped_refptr<ChromeGeolocationPermissionContext>
128 geolocation_permission_context_;
129 ClosedInfoBarTracker closed_infobar_tracker_;
130 ScopedVector<content::WebContents> extra_tabs_;
132 // A map between renderer child id and a pair represending the bridge id and
133 // whether the requested permission was allowed.
134 base::hash_map<int, std::pair<int, bool> > responses_;
137 PermissionRequestID GeolocationPermissionContextTests::RequestID(
138 int bridge_id) {
139 return PermissionRequestID(
140 web_contents()->GetRenderProcessHost()->GetID(),
141 web_contents()->GetRenderViewHost()->GetRoutingID(),
142 bridge_id,
146 PermissionRequestID GeolocationPermissionContextTests::RequestIDForTab(
147 int tab,
148 int bridge_id) {
149 return PermissionRequestID(
150 extra_tabs_[tab]->GetRenderProcessHost()->GetID(),
151 extra_tabs_[tab]->GetRenderViewHost()->GetRoutingID(),
152 bridge_id,
156 void GeolocationPermissionContextTests::RequestGeolocationPermission(
157 const PermissionRequestID& id,
158 const GURL& requesting_frame) {
159 geolocation_permission_context_->RequestGeolocationPermission(
160 id.render_process_id(), id.render_view_id(), id.bridge_id(),
161 requesting_frame,
162 base::Bind(&GeolocationPermissionContextTests::PermissionResponse,
163 base::Unretained(this), id));
166 void GeolocationPermissionContextTests::CancelGeolocationPermissionRequest(
167 const PermissionRequestID& id,
168 const GURL& requesting_frame) {
169 geolocation_permission_context_->CancelGeolocationPermissionRequest(
170 id.render_process_id(), id.render_view_id(), id.bridge_id(),
171 requesting_frame);
174 void GeolocationPermissionContextTests::PermissionResponse(
175 const PermissionRequestID& id,
176 bool allowed) {
177 responses_[id.render_process_id()] = std::make_pair(id.bridge_id(), allowed);
180 void GeolocationPermissionContextTests::CheckPermissionMessageSent(
181 int bridge_id,
182 bool allowed) {
183 CheckPermissionMessageSentInternal(process(), bridge_id, allowed);
186 void GeolocationPermissionContextTests::CheckPermissionMessageSentForTab(
187 int tab,
188 int bridge_id,
189 bool allowed) {
190 CheckPermissionMessageSentInternal(static_cast<MockRenderProcessHost*>(
191 extra_tabs_[tab]->GetRenderProcessHost()),
192 bridge_id, allowed);
195 void GeolocationPermissionContextTests::CheckPermissionMessageSentInternal(
196 MockRenderProcessHost* process,
197 int bridge_id,
198 bool allowed) {
199 ASSERT_EQ(responses_.count(process->GetID()), 1U);
200 EXPECT_EQ(bridge_id, responses_[process->GetID()].first);
201 EXPECT_EQ(allowed, responses_[process->GetID()].second);
202 responses_.erase(process->GetID());
205 void GeolocationPermissionContextTests::AddNewTab(const GURL& url) {
206 content::WebContents* new_tab = content::WebContents::Create(
207 content::WebContents::CreateParams(profile()));
208 new_tab->GetController().LoadURL(
209 url, content::Referrer(), content::PAGE_TRANSITION_TYPED, std::string());
210 content::RenderViewHostTester::For(new_tab->GetRenderViewHost())->
211 SendNavigate(extra_tabs_.size() + 1, url);
213 // Set up required helpers, and make this be as "tabby" as the code requires.
214 extensions::SetViewType(new_tab, extensions::VIEW_TYPE_TAB_CONTENTS);
215 InfoBarService::CreateForWebContents(new_tab);
217 extra_tabs_.push_back(new_tab);
220 void GeolocationPermissionContextTests::CheckTabContentsState(
221 const GURL& requesting_frame,
222 ContentSetting expected_content_setting) {
223 TabSpecificContentSettings* content_settings =
224 TabSpecificContentSettings::FromWebContents(web_contents());
225 const ContentSettingsUsagesState::StateMap& state_map =
226 content_settings->geolocation_usages_state().state_map();
227 EXPECT_EQ(1U, state_map.count(requesting_frame.GetOrigin()));
228 EXPECT_EQ(0U, state_map.count(requesting_frame));
229 ContentSettingsUsagesState::StateMap::const_iterator settings =
230 state_map.find(requesting_frame.GetOrigin());
231 ASSERT_FALSE(settings == state_map.end())
232 << "geolocation state not found " << requesting_frame;
233 EXPECT_EQ(expected_content_setting, settings->second);
236 void GeolocationPermissionContextTests::SetUp() {
237 ChromeRenderViewHostTestHarness::SetUp();
239 // Set up required helpers, and make this be as "tabby" as the code requires.
240 extensions::SetViewType(web_contents(), extensions::VIEW_TYPE_TAB_CONTENTS);
241 InfoBarService::CreateForWebContents(web_contents());
242 TabSpecificContentSettings::CreateForWebContents(web_contents());
243 #if defined(OS_ANDROID)
244 MockGoogleLocationSettingsHelper::SetLocationStatus(true, true);
245 #endif
246 geolocation_permission_context_ =
247 ChromeGeolocationPermissionContextFactory::GetForProfile(profile());
250 void GeolocationPermissionContextTests::TearDown() {
251 extra_tabs_.clear();
252 ChromeRenderViewHostTestHarness::TearDown();
255 // Tests ----------------------------------------------------------------------
257 TEST_F(GeolocationPermissionContextTests, SinglePermission) {
258 GURL requesting_frame("http://www.example.com/geolocation");
259 NavigateAndCommit(requesting_frame);
260 EXPECT_EQ(0U, infobar_service()->infobar_count());
261 RequestGeolocationPermission(RequestID(0), requesting_frame);
262 ASSERT_EQ(1U, infobar_service()->infobar_count());
263 InfoBar* infobar = infobar_service()->infobar_at(0);
264 ConfirmInfoBarDelegate* infobar_delegate =
265 infobar->delegate()->AsConfirmInfoBarDelegate();
266 ASSERT_TRUE(infobar_delegate);
267 infobar_delegate->Cancel();
268 infobar_service()->RemoveInfoBar(infobar);
269 EXPECT_EQ(1U, closed_infobar_tracker_.size());
270 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar));
273 #if defined(OS_ANDROID)
274 TEST_F(GeolocationPermissionContextTests, GeolocationEnabledDisabled) {
275 GURL requesting_frame("http://www.example.com/geolocation");
276 NavigateAndCommit(requesting_frame);
277 MockGoogleLocationSettingsHelper::SetLocationStatus(true, true);
278 EXPECT_EQ(0U, infobar_service()->infobar_count());
279 RequestGeolocationPermission(RequestID(0), requesting_frame);
280 EXPECT_EQ(1U, infobar_service()->infobar_count());
281 ConfirmInfoBarDelegate* infobar_delegate_0 = infobar_service()->
282 infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
283 ASSERT_TRUE(infobar_delegate_0);
284 base::string16 text_0 = infobar_delegate_0->GetButtonLabel(
285 ConfirmInfoBarDelegate::BUTTON_OK);
287 NavigateAndCommit(requesting_frame);
288 MockGoogleLocationSettingsHelper::SetLocationStatus(true, false);
289 EXPECT_EQ(0U, infobar_service()->infobar_count());
290 RequestGeolocationPermission(RequestID(0), requesting_frame);
291 EXPECT_EQ(1U, infobar_service()->infobar_count());
292 ConfirmInfoBarDelegate* infobar_delegate_1 = infobar_service()->
293 infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
294 ASSERT_TRUE(infobar_delegate_1);
295 base::string16 text_1 = infobar_delegate_1->GetButtonLabel(
296 ConfirmInfoBarDelegate::BUTTON_OK);
297 EXPECT_NE(text_0, text_1);
299 NavigateAndCommit(requesting_frame);
300 MockGoogleLocationSettingsHelper::SetLocationStatus(false, false);
301 EXPECT_EQ(0U, infobar_service()->infobar_count());
302 RequestGeolocationPermission(RequestID(0), requesting_frame);
303 EXPECT_EQ(0U, infobar_service()->infobar_count());
306 TEST_F(GeolocationPermissionContextTests, MasterEnabledGoogleAppsEnabled) {
307 GURL requesting_frame("http://www.example.com/geolocation");
308 NavigateAndCommit(requesting_frame);
309 MockGoogleLocationSettingsHelper::SetLocationStatus(true, true);
310 EXPECT_EQ(0U, infobar_service()->infobar_count());
311 RequestGeolocationPermission(RequestID(0), requesting_frame);
312 EXPECT_EQ(1U, infobar_service()->infobar_count());
313 ConfirmInfoBarDelegate* infobar_delegate = infobar_service()->
314 infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
315 ASSERT_TRUE(infobar_delegate);
316 infobar_delegate->Accept();
317 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW);
318 CheckPermissionMessageSent(0, true);
321 TEST_F(GeolocationPermissionContextTests, MasterEnabledGoogleAppsDisabled) {
322 GURL requesting_frame("http://www.example.com/geolocation");
323 NavigateAndCommit(requesting_frame);
324 MockGoogleLocationSettingsHelper::SetLocationStatus(true, false);
325 EXPECT_EQ(0U, infobar_service()->infobar_count());
326 RequestGeolocationPermission(RequestID(0), requesting_frame);
327 EXPECT_EQ(1U, infobar_service()->infobar_count());
328 ConfirmInfoBarDelegate* infobar_delegate = infobar_service()->
329 infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
330 ASSERT_TRUE(infobar_delegate);
331 infobar_delegate->Accept();
332 EXPECT_TRUE(
333 MockGoogleLocationSettingsHelper::WasGoogleLocationSettingsCalled());
335 #endif
337 TEST_F(GeolocationPermissionContextTests, QueuedPermission) {
338 GURL requesting_frame_0("http://www.example.com/geolocation");
339 GURL requesting_frame_1("http://www.example-2.com/geolocation");
340 EXPECT_EQ(CONTENT_SETTING_ASK,
341 profile()->GetHostContentSettingsMap()->GetContentSetting(
342 requesting_frame_0, requesting_frame_0,
343 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
344 EXPECT_EQ(CONTENT_SETTING_ASK,
345 profile()->GetHostContentSettingsMap()->GetContentSetting(
346 requesting_frame_1, requesting_frame_0,
347 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
349 NavigateAndCommit(requesting_frame_0);
350 EXPECT_EQ(0U, infobar_service()->infobar_count());
351 // Request permission for two frames.
352 RequestGeolocationPermission(RequestID(0), requesting_frame_0);
353 RequestGeolocationPermission(RequestID(1), requesting_frame_1);
354 // Ensure only one infobar is created.
355 ASSERT_EQ(1U, infobar_service()->infobar_count());
356 InfoBar* infobar_0 = infobar_service()->infobar_at(0);
357 ConfirmInfoBarDelegate* infobar_delegate_0 =
358 infobar_0->delegate()->AsConfirmInfoBarDelegate();
359 ASSERT_TRUE(infobar_delegate_0);
360 base::string16 text_0 = infobar_delegate_0->GetMessageText();
362 // Accept the first frame.
363 infobar_delegate_0->Accept();
364 CheckTabContentsState(requesting_frame_0, CONTENT_SETTING_ALLOW);
365 CheckPermissionMessageSent(0, true);
367 infobar_service()->RemoveInfoBar(infobar_0);
368 EXPECT_EQ(1U, closed_infobar_tracker_.size());
369 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_0));
370 closed_infobar_tracker_.Clear();
371 // Now we should have a new infobar for the second frame.
372 ASSERT_EQ(1U, infobar_service()->infobar_count());
374 InfoBar* infobar_1 = infobar_service()->infobar_at(0);
375 ConfirmInfoBarDelegate* infobar_delegate_1 =
376 infobar_1->delegate()->AsConfirmInfoBarDelegate();
377 ASSERT_TRUE(infobar_delegate_1);
378 base::string16 text_1 = infobar_delegate_1->GetMessageText();
379 EXPECT_NE(text_0, text_1);
381 // Cancel (block) this frame.
382 infobar_delegate_1->Cancel();
383 CheckTabContentsState(requesting_frame_1, CONTENT_SETTING_BLOCK);
384 CheckPermissionMessageSent(1, false);
385 infobar_service()->RemoveInfoBar(infobar_1);
386 EXPECT_EQ(1U, closed_infobar_tracker_.size());
387 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_1));
388 EXPECT_EQ(0U, infobar_service()->infobar_count());
389 // Ensure the persisted permissions are ok.
390 EXPECT_EQ(CONTENT_SETTING_ALLOW,
391 profile()->GetHostContentSettingsMap()->GetContentSetting(
392 requesting_frame_0, requesting_frame_0,
393 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
395 EXPECT_EQ(CONTENT_SETTING_BLOCK,
396 profile()->GetHostContentSettingsMap()->GetContentSetting(
397 requesting_frame_1, requesting_frame_0,
398 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
401 TEST_F(GeolocationPermissionContextTests, HashIsIgnored) {
402 GURL url_a("http://www.example.com/geolocation#a");
403 GURL url_b("http://www.example.com/geolocation#b");
405 // Navigate to the first url and check permission is requested.
406 NavigateAndCommit(url_a);
407 EXPECT_EQ(0U, infobar_service()->infobar_count());
408 RequestGeolocationPermission(RequestID(0), url_a);
409 ASSERT_EQ(1U, infobar_service()->infobar_count());
410 InfoBar* infobar = infobar_service()->infobar_at(0);
411 ConfirmInfoBarDelegate* infobar_delegate =
412 infobar->delegate()->AsConfirmInfoBarDelegate();
413 ASSERT_TRUE(infobar_delegate);
415 // Change the hash, we'll still be on the same page.
416 NavigateAndCommit(url_b);
418 // Accept.
419 infobar_delegate->Accept();
420 CheckTabContentsState(url_a, CONTENT_SETTING_ALLOW);
421 CheckTabContentsState(url_b, CONTENT_SETTING_ALLOW);
422 CheckPermissionMessageSent(0, true);
424 // Cleanup.
425 infobar_service()->RemoveInfoBar(infobar);
426 EXPECT_EQ(1U, closed_infobar_tracker_.size());
427 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar));
430 TEST_F(GeolocationPermissionContextTests, PermissionForFileScheme) {
431 GURL requesting_frame("file://example/geolocation.html");
432 NavigateAndCommit(requesting_frame);
433 EXPECT_EQ(0U, infobar_service()->infobar_count());
434 RequestGeolocationPermission(RequestID(0), requesting_frame);
435 EXPECT_EQ(1U, infobar_service()->infobar_count());
436 InfoBar* infobar = infobar_service()->infobar_at(0);
437 ConfirmInfoBarDelegate* infobar_delegate =
438 infobar->delegate()->AsConfirmInfoBarDelegate();
439 ASSERT_TRUE(infobar_delegate);
440 // Accept the frame.
441 infobar_delegate->Accept();
442 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW);
443 CheckPermissionMessageSent(0, true);
444 infobar_service()->RemoveInfoBar(infobar);
446 // Make sure the setting is not stored.
447 EXPECT_EQ(CONTENT_SETTING_ASK,
448 profile()->GetHostContentSettingsMap()->GetContentSetting(
449 requesting_frame,
450 requesting_frame,
451 CONTENT_SETTINGS_TYPE_GEOLOCATION,
452 std::string()));
455 TEST_F(GeolocationPermissionContextTests, CancelGeolocationPermissionRequest) {
456 GURL requesting_frame_0("http://www.example.com/geolocation");
457 GURL requesting_frame_1("http://www.example-2.com/geolocation");
458 EXPECT_EQ(CONTENT_SETTING_ASK,
459 profile()->GetHostContentSettingsMap()->GetContentSetting(
460 requesting_frame_0, requesting_frame_0,
461 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
463 EXPECT_EQ(CONTENT_SETTING_ASK,
464 profile()->GetHostContentSettingsMap()->GetContentSetting(
465 requesting_frame_1, requesting_frame_0,
466 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
468 NavigateAndCommit(requesting_frame_0);
469 EXPECT_EQ(0U, infobar_service()->infobar_count());
470 // Request permission for two frames.
471 RequestGeolocationPermission(RequestID(0), requesting_frame_0);
472 RequestGeolocationPermission(RequestID(1), requesting_frame_1);
473 ASSERT_EQ(1U, infobar_service()->infobar_count());
475 InfoBar* infobar_0 = infobar_service()->infobar_at(0);
476 ConfirmInfoBarDelegate* infobar_delegate_0 =
477 infobar_0->delegate()->AsConfirmInfoBarDelegate();
478 ASSERT_TRUE(infobar_delegate_0);
479 base::string16 text_0 = infobar_delegate_0->GetMessageText();
481 // Simulate the frame going away, ensure the infobar for this frame
482 // is removed and the next pending infobar is created.
483 CancelGeolocationPermissionRequest(RequestID(0), requesting_frame_0);
484 EXPECT_EQ(1U, closed_infobar_tracker_.size());
485 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_0));
486 closed_infobar_tracker_.Clear();
487 ASSERT_EQ(1U, infobar_service()->infobar_count());
489 InfoBar* infobar_1 = infobar_service()->infobar_at(0);
490 ConfirmInfoBarDelegate* infobar_delegate_1 =
491 infobar_1->delegate()->AsConfirmInfoBarDelegate();
492 ASSERT_TRUE(infobar_delegate_1);
493 base::string16 text_1 = infobar_delegate_1->GetMessageText();
494 EXPECT_NE(text_0, text_1);
496 // Allow this frame.
497 infobar_delegate_1->Accept();
498 CheckTabContentsState(requesting_frame_1, CONTENT_SETTING_ALLOW);
499 CheckPermissionMessageSent(1, true);
500 infobar_service()->RemoveInfoBar(infobar_1);
501 EXPECT_EQ(1U, closed_infobar_tracker_.size());
502 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_1));
503 EXPECT_EQ(0U, infobar_service()->infobar_count());
504 // Ensure the persisted permissions are ok.
505 EXPECT_EQ(CONTENT_SETTING_ASK,
506 profile()->GetHostContentSettingsMap()->GetContentSetting(
507 requesting_frame_0, requesting_frame_0,
508 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
510 EXPECT_EQ(CONTENT_SETTING_ALLOW,
511 profile()->GetHostContentSettingsMap()->GetContentSetting(
512 requesting_frame_1, requesting_frame_0,
513 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
516 TEST_F(GeolocationPermissionContextTests, InvalidURL) {
517 GURL invalid_embedder("about:blank");
518 GURL requesting_frame;
519 NavigateAndCommit(invalid_embedder);
520 EXPECT_EQ(0U, infobar_service()->infobar_count());
521 RequestGeolocationPermission(RequestID(0), requesting_frame);
522 EXPECT_EQ(0U, infobar_service()->infobar_count());
523 CheckPermissionMessageSent(0, false);
526 TEST_F(GeolocationPermissionContextTests, SameOriginMultipleTabs) {
527 GURL url_a("http://www.example.com/geolocation");
528 GURL url_b("http://www.example-2.com/geolocation");
529 NavigateAndCommit(url_a);
530 AddNewTab(url_b);
531 AddNewTab(url_a);
533 EXPECT_EQ(0U, infobar_service()->infobar_count());
534 RequestGeolocationPermission(RequestID(0), url_a);
535 ASSERT_EQ(1U, infobar_service()->infobar_count());
537 RequestGeolocationPermission(RequestIDForTab(0, 0), url_b);
538 EXPECT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
540 RequestGeolocationPermission(RequestIDForTab(1, 0), url_a);
541 ASSERT_EQ(1U, infobar_service_for_tab(1)->infobar_count());
543 InfoBar* removed_infobar = infobar_service_for_tab(1)->infobar_at(0);
545 // Accept the first tab.
546 InfoBar* infobar_0 = infobar_service()->infobar_at(0);
547 ConfirmInfoBarDelegate* infobar_delegate_0 =
548 infobar_0->delegate()->AsConfirmInfoBarDelegate();
549 ASSERT_TRUE(infobar_delegate_0);
550 infobar_delegate_0->Accept();
551 CheckPermissionMessageSent(0, true);
552 infobar_service()->RemoveInfoBar(infobar_0);
553 EXPECT_EQ(2U, closed_infobar_tracker_.size());
554 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_0));
555 // Now the infobar for the tab with the same origin should have gone.
556 EXPECT_EQ(0U, infobar_service_for_tab(1)->infobar_count());
557 CheckPermissionMessageSentForTab(1, 0, true);
558 EXPECT_TRUE(closed_infobar_tracker_.Contains(removed_infobar));
559 closed_infobar_tracker_.Clear();
561 // But the other tab should still have the info bar...
562 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
563 InfoBar* infobar_1 = infobar_service_for_tab(0)->infobar_at(0);
564 ConfirmInfoBarDelegate* infobar_delegate_1 =
565 infobar_1->delegate()->AsConfirmInfoBarDelegate();
566 ASSERT_TRUE(infobar_delegate_1);
567 infobar_delegate_1->Cancel();
568 infobar_service_for_tab(0)->RemoveInfoBar(infobar_1);
569 EXPECT_EQ(1U, closed_infobar_tracker_.size());
570 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_1));
573 TEST_F(GeolocationPermissionContextTests, QueuedOriginMultipleTabs) {
574 GURL url_a("http://www.example.com/geolocation");
575 GURL url_b("http://www.example-2.com/geolocation");
576 NavigateAndCommit(url_a);
577 AddNewTab(url_a);
579 EXPECT_EQ(0U, infobar_service()->infobar_count());
580 RequestGeolocationPermission(RequestID(0), url_a);
581 ASSERT_EQ(1U, infobar_service()->infobar_count());
583 RequestGeolocationPermission(RequestIDForTab(0, 0), url_a);
584 EXPECT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
586 RequestGeolocationPermission(RequestIDForTab(0, 1), url_b);
587 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
589 InfoBar* removed_infobar = infobar_service()->infobar_at(0);
591 // Accept the second tab.
592 InfoBar* infobar_0 = infobar_service_for_tab(0)->infobar_at(0);
593 ConfirmInfoBarDelegate* infobar_delegate_0 =
594 infobar_0->delegate()->AsConfirmInfoBarDelegate();
595 ASSERT_TRUE(infobar_delegate_0);
596 infobar_delegate_0->Accept();
597 CheckPermissionMessageSentForTab(0, 0, true);
598 infobar_service_for_tab(0)->RemoveInfoBar(infobar_0);
599 EXPECT_EQ(2U, closed_infobar_tracker_.size());
600 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_0));
601 // Now the infobar for the tab with the same origin should have gone.
602 EXPECT_EQ(0U, infobar_service()->infobar_count());
603 CheckPermissionMessageSent(0, true);
604 EXPECT_TRUE(closed_infobar_tracker_.Contains(removed_infobar));
605 closed_infobar_tracker_.Clear();
607 // And we should have the queued infobar displayed now.
608 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
610 // Accept the second infobar.
611 InfoBar* infobar_1 = infobar_service_for_tab(0)->infobar_at(0);
612 ConfirmInfoBarDelegate* infobar_delegate_1 =
613 infobar_1->delegate()->AsConfirmInfoBarDelegate();
614 ASSERT_TRUE(infobar_delegate_1);
615 infobar_delegate_1->Accept();
616 CheckPermissionMessageSentForTab(0, 1, true);
617 infobar_service_for_tab(0)->RemoveInfoBar(infobar_1);
618 EXPECT_EQ(1U, closed_infobar_tracker_.size());
619 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_1));
622 TEST_F(GeolocationPermissionContextTests, TabDestroyed) {
623 GURL requesting_frame_0("http://www.example.com/geolocation");
624 GURL requesting_frame_1("http://www.example-2.com/geolocation");
625 EXPECT_EQ(CONTENT_SETTING_ASK,
626 profile()->GetHostContentSettingsMap()->GetContentSetting(
627 requesting_frame_0, requesting_frame_0,
628 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
630 EXPECT_EQ(CONTENT_SETTING_ASK,
631 profile()->GetHostContentSettingsMap()->GetContentSetting(
632 requesting_frame_1, requesting_frame_0,
633 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
635 NavigateAndCommit(requesting_frame_0);
636 EXPECT_EQ(0U, infobar_service()->infobar_count());
637 // Request permission for two frames.
638 RequestGeolocationPermission(RequestID(0), requesting_frame_0);
639 RequestGeolocationPermission(RequestID(1), requesting_frame_1);
640 // Ensure only one infobar is created.
641 ASSERT_EQ(1U, infobar_service()->infobar_count());
642 InfoBar* infobar = infobar_service()->infobar_at(0);
644 // Delete the tab contents.
645 DeleteContents();
647 // During contents destruction, the infobar will have been closed, and the
648 // pending request should have been cleared without an infobar being created.
649 ASSERT_EQ(1U, closed_infobar_tracker_.size());
650 ASSERT_TRUE(closed_infobar_tracker_.Contains(infobar));
653 TEST_F(GeolocationPermissionContextTests, InfoBarUsesCommittedEntry) {
654 GURL requesting_frame_0("http://www.example.com/geolocation");
655 GURL requesting_frame_1("http://www.example-2.com/geolocation");
656 NavigateAndCommit(requesting_frame_0);
657 NavigateAndCommit(requesting_frame_1);
658 EXPECT_EQ(0U, infobar_service()->infobar_count());
659 // Go back: navigate to a pending entry before requesting geolocation
660 // permission.
661 web_contents()->GetController().GoBack();
662 // Request permission for the committed frame (not the pending one).
663 RequestGeolocationPermission(RequestID(0), requesting_frame_1);
664 // Ensure the infobar is created.
665 ASSERT_EQ(1U, infobar_service()->infobar_count());
666 InfoBarDelegate* infobar_delegate =
667 infobar_service()->infobar_at(0)->delegate();
668 ASSERT_TRUE(infobar_delegate);
669 // Ensure the infobar wouldn't expire for a navigation to the committed entry.
670 content::LoadCommittedDetails details;
671 details.entry = web_contents()->GetController().GetLastCommittedEntry();
672 EXPECT_FALSE(infobar_delegate->ShouldExpire(details));
673 // Ensure the infobar will expire when we commit the pending navigation.
674 details.entry = web_contents()->GetController().GetActiveEntry();
675 EXPECT_TRUE(infobar_delegate->ShouldExpire(details));
677 // Delete the tab contents.
678 DeleteContents();