Vectorize sad tab image.
[chromium-blink-merge.git] / chrome / browser / geolocation / geolocation_permission_context_unittest.cc
blob926b85825e72e5dd6adca96efa35b3a994b858c1
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/geolocation_permission_context.h"
7 #include <set>
8 #include <string>
9 #include <utility>
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/id_map.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/test/simple_test_clock.h"
18 #include "base/time/clock.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
21 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
22 #include "chrome/browser/geolocation/geolocation_permission_context_factory.h"
23 #include "chrome/browser/infobars/infobar_service.h"
24 #include "chrome/browser/permissions/permission_request_id.h"
25 #include "chrome/browser/ui/website_settings/mock_permission_bubble_view.h"
26 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
27 #include "chrome/browser/ui/website_settings/permission_bubble_request.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
30 #include "chrome/test/base/testing_profile.h"
31 #include "components/content_settings/core/browser/host_content_settings_map.h"
32 #include "components/infobars/core/confirm_infobar_delegate.h"
33 #include "components/infobars/core/infobar.h"
34 #include "content/public/browser/browser_thread.h"
35 #include "content/public/browser/navigation_details.h"
36 #include "content/public/browser/navigation_entry.h"
37 #include "content/public/browser/notification_observer.h"
38 #include "content/public/browser/notification_registrar.h"
39 #include "content/public/browser/notification_service.h"
40 #include "content/public/browser/render_frame_host.h"
41 #include "content/public/browser/web_contents.h"
42 #include "content/public/test/mock_render_process_host.h"
43 #include "content/public/test/test_renderer_host.h"
44 #include "content/public/test/test_utils.h"
45 #include "content/public/test/web_contents_tester.h"
46 #include "testing/gtest/include/gtest/gtest.h"
48 #if defined(OS_ANDROID)
49 #include "base/prefs/pref_service.h"
50 #include "chrome/browser/android/mock_location_settings.h"
51 #include "chrome/browser/geolocation/geolocation_permission_context_android.h"
52 #endif
54 #if defined(ENABLE_EXTENSIONS)
55 #include "extensions/browser/view_type_utils.h"
56 #endif
58 using content::MockRenderProcessHost;
61 // ClosedInfoBarTracker -------------------------------------------------------
63 // We need to track which infobars were closed.
64 class ClosedInfoBarTracker : public content::NotificationObserver {
65 public:
66 ClosedInfoBarTracker();
67 ~ClosedInfoBarTracker() override;
69 // content::NotificationObserver:
70 void Observe(int type,
71 const content::NotificationSource& source,
72 const content::NotificationDetails& details) override;
74 size_t size() const { return removed_infobars_.size(); }
76 bool Contains(infobars::InfoBar* infobar) const;
77 void Clear();
79 private:
80 FRIEND_TEST_ALL_PREFIXES(GeolocationPermissionContextTests, TabDestroyed);
81 content::NotificationRegistrar registrar_;
82 std::set<infobars::InfoBar*> removed_infobars_;
85 ClosedInfoBarTracker::ClosedInfoBarTracker() {
86 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
87 content::NotificationService::AllSources());
90 ClosedInfoBarTracker::~ClosedInfoBarTracker() {
93 void ClosedInfoBarTracker::Observe(
94 int type,
95 const content::NotificationSource& source,
96 const content::NotificationDetails& details) {
97 DCHECK(type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED);
98 removed_infobars_.insert(
99 content::Details<infobars::InfoBar::RemovedDetails>(details)->first);
102 bool ClosedInfoBarTracker::Contains(infobars::InfoBar* infobar) const {
103 return removed_infobars_.count(infobar) != 0;
106 void ClosedInfoBarTracker::Clear() {
107 removed_infobars_.clear();
111 // GeolocationPermissionContextTests ------------------------------------------
113 class GeolocationPermissionContextTests
114 : public ChromeRenderViewHostTestHarness {
115 protected:
116 // ChromeRenderViewHostTestHarness:
117 void SetUp() override;
118 void TearDown() override;
120 PermissionRequestID RequestID(int request_id);
121 PermissionRequestID RequestIDForTab(int tab, int request_id);
122 InfoBarService* infobar_service() {
123 return InfoBarService::FromWebContents(web_contents());
125 InfoBarService* infobar_service_for_tab(int tab) {
126 return InfoBarService::FromWebContents(extra_tabs_[tab]);
129 void RequestGeolocationPermission(content::WebContents* web_contents,
130 const PermissionRequestID& id,
131 const GURL& requesting_frame,
132 bool user_gesture);
134 void PermissionResponse(const PermissionRequestID& id,
135 ContentSetting content_setting);
136 void CheckPermissionMessageSent(int request_id, bool allowed);
137 void CheckPermissionMessageSentForTab(int tab, int request_id, bool allowed);
138 void CheckPermissionMessageSentInternal(MockRenderProcessHost* process,
139 int request_id,
140 bool allowed);
141 void AddNewTab(const GURL& url);
142 void CheckTabContentsState(const GURL& requesting_frame,
143 ContentSetting expected_content_setting);
144 size_t GetBubblesQueueSize(PermissionBubbleManager* manager);
145 void AcceptBubble(PermissionBubbleManager* manager);
146 void DenyBubble(PermissionBubbleManager* manager);
147 void CloseBubble(PermissionBubbleManager* manager);
148 void BubbleManagerDocumentLoadCompleted();
149 void BubbleManagerDocumentLoadCompleted(content::WebContents* web_contents);
150 ContentSetting GetGeolocationContentSetting(GURL frame_0, GURL frame_1);
151 bool BubbleEnabled() const;
152 size_t GetNumberOfPrompts();
153 void AcceptPrompt();
154 base::string16 GetPromptText();
156 // owned by the browser context
157 GeolocationPermissionContext* geolocation_permission_context_;
158 ClosedInfoBarTracker closed_infobar_tracker_;
159 ScopedVector<content::WebContents> extra_tabs_;
161 // A map between renderer child id and a pair represending the bridge id and
162 // whether the requested permission was allowed.
163 base::hash_map<int, std::pair<int, bool> > responses_;
166 PermissionRequestID GeolocationPermissionContextTests::RequestID(
167 int request_id) {
168 return PermissionRequestID(
169 web_contents()->GetRenderProcessHost()->GetID(),
170 web_contents()->GetMainFrame()->GetRoutingID(),
171 request_id);
174 PermissionRequestID GeolocationPermissionContextTests::RequestIDForTab(
175 int tab,
176 int request_id) {
177 return PermissionRequestID(
178 extra_tabs_[tab]->GetRenderProcessHost()->GetID(),
179 extra_tabs_[tab]->GetMainFrame()->GetRoutingID(),
180 request_id);
183 void GeolocationPermissionContextTests::RequestGeolocationPermission(
184 content::WebContents* web_contents,
185 const PermissionRequestID& id,
186 const GURL& requesting_frame,
187 bool user_gesture) {
188 geolocation_permission_context_->RequestPermission(
189 web_contents, id, requesting_frame, user_gesture,
190 base::Bind(&GeolocationPermissionContextTests::PermissionResponse,
191 base::Unretained(this), id));
192 content::RunAllBlockingPoolTasksUntilIdle();
195 void GeolocationPermissionContextTests::PermissionResponse(
196 const PermissionRequestID& id,
197 ContentSetting content_setting) {
198 responses_[id.render_process_id()] =
199 std::make_pair(id.request_id(), content_setting == CONTENT_SETTING_ALLOW);
202 void GeolocationPermissionContextTests::CheckPermissionMessageSent(
203 int request_id,
204 bool allowed) {
205 CheckPermissionMessageSentInternal(process(), request_id, allowed);
208 void GeolocationPermissionContextTests::CheckPermissionMessageSentForTab(
209 int tab,
210 int request_id,
211 bool allowed) {
212 CheckPermissionMessageSentInternal(static_cast<MockRenderProcessHost*>(
213 extra_tabs_[tab]->GetRenderProcessHost()),
214 request_id, allowed);
217 void GeolocationPermissionContextTests::CheckPermissionMessageSentInternal(
218 MockRenderProcessHost* process,
219 int request_id,
220 bool allowed) {
221 ASSERT_EQ(responses_.count(process->GetID()), 1U);
222 EXPECT_EQ(request_id, responses_[process->GetID()].first);
223 EXPECT_EQ(allowed, responses_[process->GetID()].second);
224 responses_.erase(process->GetID());
227 void GeolocationPermissionContextTests::AddNewTab(const GURL& url) {
228 content::WebContents* new_tab = CreateTestWebContents();
229 new_tab->GetController().LoadURL(
230 url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, std::string());
231 content::NavigationEntry* entry = new_tab->GetController().GetPendingEntry();
232 content::RenderFrameHostTester::For(new_tab->GetMainFrame())
233 ->SendNavigate(extra_tabs_.size() + 1, entry->GetUniqueID(), true, url);
235 // Set up required helpers, and make this be as "tabby" as the code requires.
236 #if defined(ENABLE_EXTENSIONS)
237 extensions::SetViewType(new_tab, extensions::VIEW_TYPE_TAB_CONTENTS);
238 #endif
239 InfoBarService::CreateForWebContents(new_tab);
240 if (BubbleEnabled()) {
241 PermissionBubbleManager::CreateForWebContents(new_tab);
242 PermissionBubbleManager* permission_bubble_manager =
243 PermissionBubbleManager::FromWebContents(new_tab);
244 MockPermissionBubbleView::SetFactory(permission_bubble_manager, false);
245 permission_bubble_manager->DisplayPendingRequests();
248 extra_tabs_.push_back(new_tab);
251 void GeolocationPermissionContextTests::CheckTabContentsState(
252 const GURL& requesting_frame,
253 ContentSetting expected_content_setting) {
254 TabSpecificContentSettings* content_settings =
255 TabSpecificContentSettings::FromWebContents(web_contents());
256 const ContentSettingsUsagesState::StateMap& state_map =
257 content_settings->geolocation_usages_state().state_map();
258 EXPECT_EQ(1U, state_map.count(requesting_frame.GetOrigin()));
259 EXPECT_EQ(0U, state_map.count(requesting_frame));
260 ContentSettingsUsagesState::StateMap::const_iterator settings =
261 state_map.find(requesting_frame.GetOrigin());
262 ASSERT_FALSE(settings == state_map.end())
263 << "geolocation state not found " << requesting_frame;
264 EXPECT_EQ(expected_content_setting, settings->second);
267 void GeolocationPermissionContextTests::SetUp() {
268 ChromeRenderViewHostTestHarness::SetUp();
270 // Set up required helpers, and make this be as "tabby" as the code requires.
271 #if defined(ENABLE_EXTENSIONS)
272 extensions::SetViewType(web_contents(), extensions::VIEW_TYPE_TAB_CONTENTS);
273 #endif
274 InfoBarService::CreateForWebContents(web_contents());
275 TabSpecificContentSettings::CreateForWebContents(web_contents());
276 geolocation_permission_context_ =
277 GeolocationPermissionContextFactory::GetForProfile(profile());
278 #if defined(OS_ANDROID)
279 static_cast<GeolocationPermissionContextAndroid*>(
280 geolocation_permission_context_)
281 ->SetLocationSettingsForTesting(
282 scoped_ptr<LocationSettings>(new MockLocationSettings()));
283 MockLocationSettings::SetLocationStatus(true, true);
284 #endif
285 if (BubbleEnabled()) {
286 PermissionBubbleManager::CreateForWebContents(web_contents());
287 PermissionBubbleManager* permission_bubble_manager =
288 PermissionBubbleManager::FromWebContents(web_contents());
289 MockPermissionBubbleView::SetFactory(permission_bubble_manager, false);
290 permission_bubble_manager->DisplayPendingRequests();
294 void GeolocationPermissionContextTests::TearDown() {
295 extra_tabs_.clear();
296 ChromeRenderViewHostTestHarness::TearDown();
299 size_t GeolocationPermissionContextTests::GetBubblesQueueSize(
300 PermissionBubbleManager* manager) {
301 return manager->requests_.size();
304 void GeolocationPermissionContextTests::AcceptBubble(
305 PermissionBubbleManager* manager) {
306 manager->Accept();
309 void GeolocationPermissionContextTests::DenyBubble(
310 PermissionBubbleManager* manager) {
311 manager->Deny();
314 void GeolocationPermissionContextTests::CloseBubble(
315 PermissionBubbleManager* manager) {
316 manager->Closing();
319 void GeolocationPermissionContextTests::BubbleManagerDocumentLoadCompleted() {
320 GeolocationPermissionContextTests::BubbleManagerDocumentLoadCompleted(
321 web_contents());
324 void GeolocationPermissionContextTests::BubbleManagerDocumentLoadCompleted(
325 content::WebContents* web_contents) {
326 PermissionBubbleManager::FromWebContents(web_contents)->
327 DocumentOnLoadCompletedInMainFrame();
330 ContentSetting GeolocationPermissionContextTests::GetGeolocationContentSetting(
331 GURL frame_0, GURL frame_1) {
332 return HostContentSettingsMapFactory::GetForProfile(profile())
333 ->GetContentSetting(frame_0,
334 frame_1,
335 CONTENT_SETTINGS_TYPE_GEOLOCATION,
336 std::string());
339 bool GeolocationPermissionContextTests::BubbleEnabled() const {
340 return PermissionBubbleManager::Enabled();
343 size_t GeolocationPermissionContextTests::GetNumberOfPrompts() {
344 if (BubbleEnabled()) {
345 PermissionBubbleManager* manager =
346 PermissionBubbleManager::FromWebContents(web_contents());
347 return GetBubblesQueueSize(manager);
348 } else {
349 return infobar_service()->infobar_count();
353 void GeolocationPermissionContextTests::AcceptPrompt() {
354 if (BubbleEnabled()) {
355 PermissionBubbleManager* manager =
356 PermissionBubbleManager::FromWebContents(web_contents());
357 AcceptBubble(manager);
358 } else {
359 infobars::InfoBar* infobar = infobar_service()->infobar_at(0);
360 ConfirmInfoBarDelegate* infobar_delegate =
361 infobar->delegate()->AsConfirmInfoBarDelegate();
362 infobar_delegate->Accept();
366 base::string16 GeolocationPermissionContextTests::GetPromptText() {
367 if (BubbleEnabled()) {
368 PermissionBubbleManager* manager =
369 PermissionBubbleManager::FromWebContents(web_contents());
370 return manager->requests_.front()->GetMessageText();
372 infobars::InfoBar* infobar = infobar_service()->infobar_at(0);
373 ConfirmInfoBarDelegate* infobar_delegate =
374 infobar->delegate()->AsConfirmInfoBarDelegate();
375 return infobar_delegate->GetMessageText();
378 // Tests ----------------------------------------------------------------------
380 TEST_F(GeolocationPermissionContextTests, SinglePermissionInfobar) {
381 if (BubbleEnabled()) return;
383 GURL requesting_frame("http://www.example.com/geolocation");
384 NavigateAndCommit(requesting_frame);
385 EXPECT_EQ(0U, infobar_service()->infobar_count());
386 RequestGeolocationPermission(
387 web_contents(), RequestID(0), requesting_frame, true);
388 ASSERT_EQ(1U, infobar_service()->infobar_count());
389 infobars::InfoBar* infobar = infobar_service()->infobar_at(0);
390 ConfirmInfoBarDelegate* infobar_delegate =
391 infobar->delegate()->AsConfirmInfoBarDelegate();
392 ASSERT_TRUE(infobar_delegate);
393 infobar_delegate->Cancel();
394 infobar_service()->RemoveInfoBar(infobar);
395 EXPECT_EQ(1U, closed_infobar_tracker_.size());
396 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar));
399 TEST_F(GeolocationPermissionContextTests, SinglePermissionBubble) {
400 if (!BubbleEnabled()) return;
402 GURL requesting_frame("http://www.example.com/geolocation");
403 NavigateAndCommit(requesting_frame);
404 BubbleManagerDocumentLoadCompleted();
406 EXPECT_EQ(0U, GetNumberOfPrompts());
407 RequestGeolocationPermission(
408 web_contents(), RequestID(0), requesting_frame, true);
409 ASSERT_EQ(1U, GetNumberOfPrompts());
412 #if defined(OS_ANDROID)
413 // Infobar-only tests; Android doesn't support permission bubbles.
414 TEST_F(GeolocationPermissionContextTests, GeolocationEnabledDisabled) {
415 GURL requesting_frame("http://www.example.com/geolocation");
416 NavigateAndCommit(requesting_frame);
417 MockLocationSettings::SetLocationStatus(true, true);
418 EXPECT_EQ(0U, infobar_service()->infobar_count());
419 RequestGeolocationPermission(
420 web_contents(), RequestID(0), requesting_frame, true);
421 EXPECT_EQ(1U, infobar_service()->infobar_count());
422 ConfirmInfoBarDelegate* infobar_delegate_0 =
423 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
424 ASSERT_TRUE(infobar_delegate_0);
425 base::string16 text_0 = infobar_delegate_0->GetButtonLabel(
426 ConfirmInfoBarDelegate::BUTTON_OK);
428 Reload();
429 MockLocationSettings::SetLocationStatus(true, false);
430 EXPECT_EQ(0U, infobar_service()->infobar_count());
431 RequestGeolocationPermission(
432 web_contents(), RequestID(0), requesting_frame, true);
433 EXPECT_EQ(0U, infobar_service()->infobar_count());
436 TEST_F(GeolocationPermissionContextTests, MasterEnabledGoogleAppsEnabled) {
437 GURL requesting_frame("http://www.example.com/geolocation");
438 NavigateAndCommit(requesting_frame);
439 MockLocationSettings::SetLocationStatus(true, true);
440 EXPECT_EQ(0U, infobar_service()->infobar_count());
441 RequestGeolocationPermission(
442 web_contents(), RequestID(0), requesting_frame, true);
443 EXPECT_EQ(1U, infobar_service()->infobar_count());
444 ConfirmInfoBarDelegate* infobar_delegate =
445 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
446 ASSERT_TRUE(infobar_delegate);
447 infobar_delegate->Accept();
448 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW);
449 CheckPermissionMessageSent(0, true);
452 TEST_F(GeolocationPermissionContextTests, MasterEnabledGoogleAppsDisabled) {
453 GURL requesting_frame("http://www.example.com/geolocation");
454 NavigateAndCommit(requesting_frame);
455 MockLocationSettings::SetLocationStatus(true, false);
456 EXPECT_EQ(0U, infobar_service()->infobar_count());
457 RequestGeolocationPermission(
458 web_contents(), RequestID(0), requesting_frame, true);
459 EXPECT_EQ(0U, infobar_service()->infobar_count());
461 #endif
463 TEST_F(GeolocationPermissionContextTests, QueuedPermission) {
464 GURL requesting_frame_0("http://www.example.com/geolocation");
465 GURL requesting_frame_1("http://www.example-2.com/geolocation");
466 EXPECT_EQ(
467 CONTENT_SETTING_ASK,
468 GetGeolocationContentSetting(requesting_frame_0, requesting_frame_1));
469 EXPECT_EQ(
470 CONTENT_SETTING_ASK,
471 GetGeolocationContentSetting(requesting_frame_1, requesting_frame_1));
473 NavigateAndCommit(requesting_frame_0);
474 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
476 // Check that no permission requests have happened yet.
477 EXPECT_EQ(0U, GetNumberOfPrompts());
479 // Request permission for two frames.
480 RequestGeolocationPermission(
481 web_contents(), RequestID(0), requesting_frame_0, true);
482 RequestGeolocationPermission(
483 web_contents(), RequestID(1), requesting_frame_1, true);
484 // Ensure only one infobar is created.
485 ASSERT_EQ(1U, GetNumberOfPrompts());
486 base::string16 text_0 = GetPromptText();
488 // Accept the first frame.
489 AcceptPrompt();
490 CheckTabContentsState(requesting_frame_0, CONTENT_SETTING_ALLOW);
491 CheckPermissionMessageSent(0, true);
493 if (!BubbleEnabled()) {
494 infobars::InfoBar* infobar_0 = infobar_service()->infobar_at(0);
495 infobar_service()->RemoveInfoBar(infobar_0);
496 EXPECT_EQ(1U, closed_infobar_tracker_.size());
497 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_0));
498 closed_infobar_tracker_.Clear();
501 // Now we should have a new infobar for the second frame.
502 ASSERT_EQ(1U, GetNumberOfPrompts());
503 base::string16 text_1 = GetPromptText();
505 // Check that the messages differ.
506 EXPECT_NE(text_0, text_1);
508 // Cancel (block) this frame.
509 if (BubbleEnabled()) {
510 PermissionBubbleManager* manager =
511 PermissionBubbleManager::FromWebContents(web_contents());
512 DenyBubble(manager);
513 } else {
514 infobars::InfoBar* infobar_1 = infobar_service()->infobar_at(0);
515 infobar_1->delegate()->AsConfirmInfoBarDelegate()->Cancel();
517 CheckTabContentsState(requesting_frame_1, CONTENT_SETTING_BLOCK);
518 CheckPermissionMessageSent(1, false);
520 // Ensure the persisted permissions are ok.
521 EXPECT_EQ(
522 CONTENT_SETTING_ALLOW,
523 GetGeolocationContentSetting(requesting_frame_0, requesting_frame_0));
524 EXPECT_EQ(
525 CONTENT_SETTING_BLOCK,
526 GetGeolocationContentSetting(requesting_frame_1, requesting_frame_0));
529 TEST_F(GeolocationPermissionContextTests, HashIsIgnored) {
530 GURL url_a("http://www.example.com/geolocation#a");
531 GURL url_b("http://www.example.com/geolocation#b");
533 // Navigate to the first url.
534 NavigateAndCommit(url_a);
535 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
537 // Check permission is requested.
538 ASSERT_EQ(0U, GetNumberOfPrompts());
539 RequestGeolocationPermission(
540 web_contents(), RequestID(0), url_a, BubbleEnabled());
541 ASSERT_EQ(1U, GetNumberOfPrompts());
543 // Change the hash, we'll still be on the same page.
544 NavigateAndCommit(url_b);
545 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
547 // Accept.
548 AcceptPrompt();
549 CheckTabContentsState(url_a, CONTENT_SETTING_ALLOW);
550 CheckTabContentsState(url_b, CONTENT_SETTING_ALLOW);
551 CheckPermissionMessageSent(0, true);
553 // Cleanup.
554 if (!BubbleEnabled()) {
555 infobars::InfoBar* infobar = infobar_service()->infobar_at(0);
556 infobar_service()->RemoveInfoBar(infobar);
557 EXPECT_EQ(1U, closed_infobar_tracker_.size());
558 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar));
562 TEST_F(GeolocationPermissionContextTests, PermissionForFileScheme) {
563 // TODO(felt): The bubble is rejecting file:// permission requests.
564 // Fix and enable this test. crbug.com/444047
565 if (BubbleEnabled()) return;
567 GURL requesting_frame("file://example/geolocation.html");
568 NavigateAndCommit(requesting_frame);
569 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
571 // Check permission is requested.
572 ASSERT_EQ(0U, GetNumberOfPrompts());
573 RequestGeolocationPermission(
574 web_contents(), RequestID(0), requesting_frame, true);
575 EXPECT_EQ(1U, GetNumberOfPrompts());
577 // Accept the frame.
578 AcceptPrompt();
579 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW);
580 CheckPermissionMessageSent(0, true);
582 // Make sure the setting is not stored.
583 EXPECT_EQ(
584 CONTENT_SETTING_ASK,
585 GetGeolocationContentSetting(requesting_frame, requesting_frame));
588 TEST_F(GeolocationPermissionContextTests, CancelGeolocationPermissionRequest) {
589 GURL frame_0("http://www.example.com/geolocation");
590 GURL frame_1("http://www.example-2.com/geolocation");
591 EXPECT_EQ(
592 CONTENT_SETTING_ASK, GetGeolocationContentSetting(frame_0, frame_0));
593 EXPECT_EQ(
594 CONTENT_SETTING_ASK, GetGeolocationContentSetting(frame_1, frame_0));
596 NavigateAndCommit(frame_0);
597 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
599 ASSERT_EQ(0U, GetNumberOfPrompts());
601 // Request permission for two frames.
602 RequestGeolocationPermission(
603 web_contents(), RequestID(0), frame_0, true);
604 RequestGeolocationPermission(
605 web_contents(), RequestID(1), frame_1, true);
607 // Get the first permission request text.
608 ASSERT_EQ(1U, GetNumberOfPrompts());
609 base::string16 text_0 = GetPromptText();
610 ASSERT_FALSE(text_0.empty());
612 // Simulate the frame going away; the request should be removed.
613 if (BubbleEnabled()) {
614 PermissionBubbleManager* manager =
615 PermissionBubbleManager::FromWebContents(web_contents());
616 CloseBubble(manager);
617 } else {
618 geolocation_permission_context_->CancelPermissionRequest(web_contents(),
619 RequestID(0));
622 // Check that the next pending request is created correctly.
623 base::string16 text_1 = GetPromptText();
624 EXPECT_NE(text_0, text_1);
626 // Allow this frame and check that it worked.
627 AcceptPrompt();
628 CheckTabContentsState(frame_1, CONTENT_SETTING_ALLOW);
629 CheckPermissionMessageSent(1, true);
631 // Ensure the persisted permissions are ok.
632 EXPECT_EQ(
633 CONTENT_SETTING_ASK, GetGeolocationContentSetting(frame_0, frame_0));
634 EXPECT_EQ(
635 CONTENT_SETTING_ALLOW, GetGeolocationContentSetting(frame_1, frame_0));
638 TEST_F(GeolocationPermissionContextTests, InvalidURL) {
639 // Navigate to the first url.
640 GURL invalid_embedder("about:blank");
641 GURL requesting_frame;
642 NavigateAndCommit(invalid_embedder);
643 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
645 // Nothing should be displayed.
646 EXPECT_EQ(0U, GetNumberOfPrompts());
647 RequestGeolocationPermission(
648 web_contents(), RequestID(0), requesting_frame, true);
649 EXPECT_EQ(0U, GetNumberOfPrompts());
650 CheckPermissionMessageSent(0, false);
653 TEST_F(GeolocationPermissionContextTests, SameOriginMultipleTabs) {
654 GURL url_a("http://www.example.com/geolocation");
655 GURL url_b("http://www.example-2.com/geolocation");
656 NavigateAndCommit(url_a); // Tab A0
657 AddNewTab(url_b); // Tab B (extra_tabs_[0])
658 AddNewTab(url_a); // Tab A1 (extra_tabs_[1])
659 if (BubbleEnabled()) {
660 BubbleManagerDocumentLoadCompleted();
661 BubbleManagerDocumentLoadCompleted(extra_tabs_[0]);
662 BubbleManagerDocumentLoadCompleted(extra_tabs_[1]);
664 PermissionBubbleManager* manager_a0 =
665 PermissionBubbleManager::FromWebContents(web_contents());
666 PermissionBubbleManager* manager_b =
667 PermissionBubbleManager::FromWebContents(extra_tabs_[0]);
668 PermissionBubbleManager* manager_a1 =
669 PermissionBubbleManager::FromWebContents(extra_tabs_[1]);
671 // Request permission in all three tabs.
672 RequestGeolocationPermission(
673 web_contents(), RequestID(0), url_a, true);
674 RequestGeolocationPermission(
675 extra_tabs_[0], RequestIDForTab(0, 0), url_b, true);
676 RequestGeolocationPermission(
677 extra_tabs_[1], RequestIDForTab(1, 0), url_a, true);
678 ASSERT_EQ(1U, GetNumberOfPrompts()); // For A0.
679 if (BubbleEnabled()) {
680 ASSERT_EQ(1U, GetBubblesQueueSize(manager_b));
681 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a1));
682 } else {
683 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
684 ASSERT_EQ(1U, infobar_service_for_tab(1)->infobar_count());
687 // Accept the permission in tab A0.
688 if (BubbleEnabled()) {
689 AcceptBubble(manager_a0);
690 } else {
691 infobars::InfoBar* infobar_a0 = infobar_service()->infobar_at(0);
692 ConfirmInfoBarDelegate* infobar_delegate_a0 =
693 infobar_a0->delegate()->AsConfirmInfoBarDelegate();
694 ASSERT_TRUE(infobar_delegate_a0);
695 infobar_delegate_a0->Accept();
696 infobar_service()->RemoveInfoBar(infobar_a0);
697 EXPECT_EQ(2U, closed_infobar_tracker_.size());
698 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_a0));
700 CheckPermissionMessageSent(0, true);
701 // Because they're the same origin, this will cause tab A1's infobar to
702 // disappear. It does not cause the bubble to disappear: crbug.com/443013.
703 // TODO(felt): Update this test when the bubble's behavior is changed.
704 if (BubbleEnabled())
705 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a1));
706 else
707 CheckPermissionMessageSentForTab(1, 0, true);
709 // Either way, tab B should still have a pending permission request.
710 if (BubbleEnabled())
711 ASSERT_EQ(1U, GetBubblesQueueSize(manager_b));
712 else
713 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
716 TEST_F(GeolocationPermissionContextTests, QueuedOriginMultipleTabs) {
717 GURL url_a("http://www.example.com/geolocation");
718 GURL url_b("http://www.example-2.com/geolocation");
719 NavigateAndCommit(url_a); // Tab A0.
720 AddNewTab(url_a); // Tab A1.
721 if (BubbleEnabled()) {
722 BubbleManagerDocumentLoadCompleted();
723 BubbleManagerDocumentLoadCompleted(extra_tabs_[0]);
725 PermissionBubbleManager* manager_a0 =
726 PermissionBubbleManager::FromWebContents(web_contents());
727 PermissionBubbleManager* manager_a1 =
728 PermissionBubbleManager::FromWebContents(extra_tabs_[0]);
730 // Request permission in both tabs; the extra tab will have two permission
731 // requests from two origins.
732 RequestGeolocationPermission(
733 web_contents(), RequestID(0), url_a, true);
734 RequestGeolocationPermission(
735 extra_tabs_[0], RequestIDForTab(0, 0), url_a, true);
736 RequestGeolocationPermission(
737 extra_tabs_[0], RequestIDForTab(0, 1), url_b, true);
738 if (BubbleEnabled()) {
739 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a0));
740 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a1));
741 } else {
742 ASSERT_EQ(1U, infobar_service()->infobar_count());
743 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
746 // Accept the first request in tab A1.
747 if (BubbleEnabled()) {
748 AcceptBubble(manager_a1);
749 } else {
750 infobars::InfoBar* infobar_a1 = infobar_service_for_tab(0)->infobar_at(0);
751 ConfirmInfoBarDelegate* infobar_delegate_a1 =
752 infobar_a1->delegate()->AsConfirmInfoBarDelegate();
753 ASSERT_TRUE(infobar_delegate_a1);
754 infobar_delegate_a1->Accept();
755 infobar_service_for_tab(0)->RemoveInfoBar(infobar_a1);
756 EXPECT_EQ(2U, closed_infobar_tracker_.size());
757 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_a1));
759 CheckPermissionMessageSentForTab(0, 0, true);
761 // Because they're the same origin, this will cause tab A0's infobar to
762 // disappear. It does not cause the bubble to disappear: crbug.com/443013.
763 // TODO(felt): Update this test when the bubble's behavior is changed.
764 if (BubbleEnabled()) {
765 EXPECT_EQ(1U, GetBubblesQueueSize(manager_a0));
766 } else {
767 EXPECT_EQ(0U, infobar_service()->infobar_count());
768 CheckPermissionMessageSent(0, true);
771 // The second request should now be visible in tab A1.
772 if (BubbleEnabled())
773 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a1));
774 else
775 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
777 // Accept the second request and check that it's gone.
778 if (BubbleEnabled()) {
779 AcceptBubble(manager_a1);
780 EXPECT_EQ(0U, GetBubblesQueueSize(manager_a1));
781 } else {
782 infobars::InfoBar* infobar_1 = infobar_service_for_tab(0)->infobar_at(0);
783 ConfirmInfoBarDelegate* infobar_delegate_1 =
784 infobar_1->delegate()->AsConfirmInfoBarDelegate();
785 ASSERT_TRUE(infobar_delegate_1);
786 infobar_delegate_1->Accept();
790 TEST_F(GeolocationPermissionContextTests, TabDestroyed) {
791 GURL requesting_frame_0("http://www.example.com/geolocation");
792 GURL requesting_frame_1("http://www.example-2.com/geolocation");
793 EXPECT_EQ(
794 CONTENT_SETTING_ASK,
795 GetGeolocationContentSetting(requesting_frame_0, requesting_frame_0));
796 EXPECT_EQ(
797 CONTENT_SETTING_ASK,
798 GetGeolocationContentSetting(requesting_frame_1, requesting_frame_0));
800 NavigateAndCommit(requesting_frame_0);
801 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
803 // Request permission for two frames.
804 RequestGeolocationPermission(
805 web_contents(), RequestID(0), requesting_frame_0, false);
806 RequestGeolocationPermission(
807 web_contents(), RequestID(1), requesting_frame_1, false);
809 // Ensure only one prompt is created.
810 ASSERT_EQ(1U, GetNumberOfPrompts());
812 // Delete the tab contents.
813 if (!BubbleEnabled()) {
814 infobars::InfoBar* infobar = infobar_service()->infobar_at(0);
815 DeleteContents();
816 ASSERT_EQ(1U, closed_infobar_tracker_.size());
817 ASSERT_TRUE(closed_infobar_tracker_.Contains(infobar));
820 // The content settings should not have changed.
821 EXPECT_EQ(
822 CONTENT_SETTING_ASK,
823 GetGeolocationContentSetting(requesting_frame_0, requesting_frame_0));
824 EXPECT_EQ(
825 CONTENT_SETTING_ASK,
826 GetGeolocationContentSetting(requesting_frame_1, requesting_frame_0));
829 TEST_F(GeolocationPermissionContextTests, LastUsageAudited) {
830 GURL requesting_frame("http://www.example.com/geolocation");
831 NavigateAndCommit(requesting_frame);
832 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
834 base::SimpleTestClock* test_clock = new base::SimpleTestClock;
835 test_clock->SetNow(base::Time::UnixEpoch() +
836 base::TimeDelta::FromSeconds(10));
838 HostContentSettingsMap* map =
839 HostContentSettingsMapFactory::GetForProfile(profile());
840 map->SetPrefClockForTesting(scoped_ptr<base::Clock>(test_clock));
842 // The permission shouldn't have been used yet.
843 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(),
844 requesting_frame.GetOrigin(),
845 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
847 ASSERT_EQ(0U, GetNumberOfPrompts());
848 RequestGeolocationPermission(
849 web_contents(), RequestID(0), requesting_frame, false);
850 ASSERT_EQ(1U, GetNumberOfPrompts());
852 AcceptPrompt();
853 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW);
854 CheckPermissionMessageSent(0, true);
856 // Permission has been used at the starting time.
857 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(),
858 requesting_frame.GetOrigin(),
859 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
860 10);
862 test_clock->Advance(base::TimeDelta::FromSeconds(3));
863 RequestGeolocationPermission(
864 web_contents(), RequestID(0), requesting_frame, false);
866 // Permission has been used three seconds later.
867 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(),
868 requesting_frame.GetOrigin(),
869 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
870 13);
873 TEST_F(GeolocationPermissionContextTests, LastUsageAuditedMultipleFrames) {
874 base::SimpleTestClock* test_clock = new base::SimpleTestClock;
875 test_clock->SetNow(base::Time::UnixEpoch() +
876 base::TimeDelta::FromSeconds(10));
878 HostContentSettingsMap* map =
879 HostContentSettingsMapFactory::GetForProfile(profile());
880 map->SetPrefClockForTesting(scoped_ptr<base::Clock>(test_clock));
882 GURL requesting_frame_0("http://www.example.com/geolocation");
883 GURL requesting_frame_1("http://www.example-2.com/geolocation");
885 // The permission shouldn't have been used yet.
886 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(),
887 requesting_frame_0.GetOrigin(),
888 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
890 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(),
891 requesting_frame_0.GetOrigin(),
892 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
895 NavigateAndCommit(requesting_frame_0);
896 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
898 EXPECT_EQ(0U, GetNumberOfPrompts());
900 // Request permission for two frames.
901 RequestGeolocationPermission(
902 web_contents(), RequestID(0), requesting_frame_0, false);
903 RequestGeolocationPermission(
904 web_contents(), RequestID(1), requesting_frame_1, false);
906 // Ensure only one infobar is created.
907 ASSERT_EQ(1U, GetNumberOfPrompts());
909 // Accept the first frame.
910 AcceptPrompt();
911 if (!BubbleEnabled())
912 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0));
913 CheckTabContentsState(requesting_frame_0, CONTENT_SETTING_ALLOW);
914 CheckPermissionMessageSent(0, true);
916 // Verify that accepting the first didn't accept because it's embedded
917 // in the other.
918 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(),
919 requesting_frame_0.GetOrigin(),
920 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
921 10);
922 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(),
923 requesting_frame_0.GetOrigin(),
924 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
927 ASSERT_EQ(1U, GetNumberOfPrompts());
929 test_clock->Advance(base::TimeDelta::FromSeconds(1));
931 // Allow the second frame.
932 AcceptPrompt();
933 CheckTabContentsState(requesting_frame_1, CONTENT_SETTING_ALLOW);
934 CheckPermissionMessageSent(1, true);
935 if (!BubbleEnabled())
936 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0));
938 // Verify that the times are different.
939 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(),
940 requesting_frame_0.GetOrigin(),
941 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
942 10);
943 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(),
944 requesting_frame_0.GetOrigin(),
945 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
946 11);
948 test_clock->Advance(base::TimeDelta::FromSeconds(2));
949 RequestGeolocationPermission(
950 web_contents(), RequestID(0), requesting_frame_0, false);
952 // Verify that requesting permission in one frame doesn't update other where
953 // it is the embedder.
954 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(),
955 requesting_frame_0.GetOrigin(),
956 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
957 13);
958 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(),
959 requesting_frame_0.GetOrigin(),
960 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
961 11);