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

Side by Side Diff: chrome/browser/geolocation/geolocation_permission_context_unittest.cc

Issue 787033004: Update geolocation permission tests for the permission bubble (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Tim's nits Created 5 years, 10 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
OLDNEW
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 "chrome/browser/geolocation/geolocation_permission_context.h" 5 #include "chrome/browser/geolocation/geolocation_permission_context.h"
6 6
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/command_line.h"
12 #include "base/containers/hash_tables.h" 13 #include "base/containers/hash_tables.h"
13 #include "base/id_map.h" 14 #include "base/id_map.h"
14 #include "base/memory/scoped_vector.h" 15 #include "base/memory/scoped_vector.h"
15 #include "base/synchronization/waitable_event.h" 16 #include "base/synchronization/waitable_event.h"
16 #include "base/test/simple_test_clock.h" 17 #include "base/test/simple_test_clock.h"
17 #include "base/time/clock.h" 18 #include "base/time/clock.h"
18 #include "chrome/browser/chrome_notification_types.h" 19 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/content_settings/tab_specific_content_settings.h" 20 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
20 #include "chrome/browser/geolocation/geolocation_permission_context_factory.h" 21 #include "chrome/browser/geolocation/geolocation_permission_context_factory.h"
21 #include "chrome/browser/infobars/infobar_service.h" 22 #include "chrome/browser/infobars/infobar_service.h"
23 #include "chrome/browser/ui/website_settings/mock_permission_bubble_view.h"
24 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
25 #include "chrome/browser/ui/website_settings/permission_bubble_request.h"
26 #include "chrome/common/chrome_switches.h"
22 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 27 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
23 #include "chrome/test/base/testing_profile.h" 28 #include "chrome/test/base/testing_profile.h"
24 #include "components/content_settings/core/browser/host_content_settings_map.h" 29 #include "components/content_settings/core/browser/host_content_settings_map.h"
25 #include "components/content_settings/core/common/permission_request_id.h" 30 #include "components/content_settings/core/common/permission_request_id.h"
26 #include "components/infobars/core/confirm_infobar_delegate.h" 31 #include "components/infobars/core/confirm_infobar_delegate.h"
27 #include "components/infobars/core/infobar.h" 32 #include "components/infobars/core/infobar.h"
28 #include "content/public/browser/browser_thread.h" 33 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/navigation_details.h" 34 #include "content/public/browser/navigation_details.h"
30 #include "content/public/browser/notification_observer.h" 35 #include "content/public/browser/notification_observer.h"
31 #include "content/public/browser/notification_registrar.h" 36 #include "content/public/browser/notification_registrar.h"
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 PermissionRequestID RequestIDForTab(int tab, int bridge_id); 118 PermissionRequestID RequestIDForTab(int tab, int bridge_id);
114 InfoBarService* infobar_service() { 119 InfoBarService* infobar_service() {
115 return InfoBarService::FromWebContents(web_contents()); 120 return InfoBarService::FromWebContents(web_contents());
116 } 121 }
117 InfoBarService* infobar_service_for_tab(int tab) { 122 InfoBarService* infobar_service_for_tab(int tab) {
118 return InfoBarService::FromWebContents(extra_tabs_[tab]); 123 return InfoBarService::FromWebContents(extra_tabs_[tab]);
119 } 124 }
120 125
121 void RequestGeolocationPermission(content::WebContents* web_contents, 126 void RequestGeolocationPermission(content::WebContents* web_contents,
122 const PermissionRequestID& id, 127 const PermissionRequestID& id,
123 const GURL& requesting_frame); 128 const GURL& requesting_frame,
129 bool user_gesture);
124 130
125 void PermissionResponse(const PermissionRequestID& id, 131 void PermissionResponse(const PermissionRequestID& id,
126 bool allowed); 132 bool allowed);
127 void CheckPermissionMessageSent(int bridge_id, bool allowed); 133 void CheckPermissionMessageSent(int bridge_id, bool allowed);
128 void CheckPermissionMessageSentForTab(int tab, int bridge_id, bool allowed); 134 void CheckPermissionMessageSentForTab(int tab, int bridge_id, bool allowed);
129 void CheckPermissionMessageSentInternal(MockRenderProcessHost* process, 135 void CheckPermissionMessageSentInternal(MockRenderProcessHost* process,
130 int bridge_id, 136 int bridge_id,
131 bool allowed); 137 bool allowed);
132 void AddNewTab(const GURL& url); 138 void AddNewTab(const GURL& url);
133 void CheckTabContentsState(const GURL& requesting_frame, 139 void CheckTabContentsState(const GURL& requesting_frame,
134 ContentSetting expected_content_setting); 140 ContentSetting expected_content_setting);
141 base::string16 GetFirstRequestText(PermissionBubbleManager* manager);
142 size_t GetBubblesQueueSize(PermissionBubbleManager* manager);
143 void AcceptBubble(PermissionBubbleManager* manager);
144 void DenyBubble(PermissionBubbleManager* manager);
145 void CloseBubble(PermissionBubbleManager* manager);
146 void BubbleManagerDocumentLoadCompleted();
147 void BubbleManagerDocumentLoadCompleted(content::WebContents* web_contents);
148 ContentSetting GetGeolocationContentSetting(GURL frame_0, GURL frame_1);
135 149
136 // owned by the browser context 150 // owned by the browser context
137 GeolocationPermissionContext* geolocation_permission_context_; 151 GeolocationPermissionContext* geolocation_permission_context_;
138 ClosedInfoBarTracker closed_infobar_tracker_; 152 ClosedInfoBarTracker closed_infobar_tracker_;
153 MockPermissionBubbleView bubble_view_;
139 ScopedVector<content::WebContents> extra_tabs_; 154 ScopedVector<content::WebContents> extra_tabs_;
140 155
141 // A map between renderer child id and a pair represending the bridge id and 156 // A map between renderer child id and a pair represending the bridge id and
142 // whether the requested permission was allowed. 157 // whether the requested permission was allowed.
143 base::hash_map<int, std::pair<int, bool> > responses_; 158 base::hash_map<int, std::pair<int, bool> > responses_;
144 }; 159 };
145 160
146 PermissionRequestID GeolocationPermissionContextTests::RequestID( 161 PermissionRequestID GeolocationPermissionContextTests::RequestID(
147 int bridge_id) { 162 int bridge_id) {
148 return PermissionRequestID( 163 return PermissionRequestID(
149 web_contents()->GetRenderProcessHost()->GetID(), 164 web_contents()->GetRenderProcessHost()->GetID(),
150 web_contents()->GetRenderViewHost()->GetRoutingID(), 165 web_contents()->GetRenderViewHost()->GetRoutingID(),
151 bridge_id, 166 bridge_id,
152 GURL()); 167 GURL());
153 } 168 }
154 169
155 PermissionRequestID GeolocationPermissionContextTests::RequestIDForTab( 170 PermissionRequestID GeolocationPermissionContextTests::RequestIDForTab(
156 int tab, 171 int tab,
157 int bridge_id) { 172 int bridge_id) {
158 return PermissionRequestID( 173 return PermissionRequestID(
159 extra_tabs_[tab]->GetRenderProcessHost()->GetID(), 174 extra_tabs_[tab]->GetRenderProcessHost()->GetID(),
160 extra_tabs_[tab]->GetRenderViewHost()->GetRoutingID(), 175 extra_tabs_[tab]->GetRenderViewHost()->GetRoutingID(),
161 bridge_id, 176 bridge_id,
162 GURL()); 177 GURL());
163 } 178 }
164 179
165 void GeolocationPermissionContextTests::RequestGeolocationPermission( 180 void GeolocationPermissionContextTests::RequestGeolocationPermission(
166 content::WebContents* web_contents, 181 content::WebContents* web_contents,
167 const PermissionRequestID& id, 182 const PermissionRequestID& id,
168 const GURL& requesting_frame) { 183 const GURL& requesting_frame,
184 bool user_gesture) {
169 geolocation_permission_context_->RequestPermission( 185 geolocation_permission_context_->RequestPermission(
170 web_contents, id, requesting_frame, false, 186 web_contents, id, requesting_frame, user_gesture,
171 base::Bind(&GeolocationPermissionContextTests::PermissionResponse, 187 base::Bind(&GeolocationPermissionContextTests::PermissionResponse,
172 base::Unretained(this), id)); 188 base::Unretained(this), id));
173 content::RunAllBlockingPoolTasksUntilIdle(); 189 content::RunAllBlockingPoolTasksUntilIdle();
174 } 190 }
175 191
176 void GeolocationPermissionContextTests::PermissionResponse( 192 void GeolocationPermissionContextTests::PermissionResponse(
177 const PermissionRequestID& id, 193 const PermissionRequestID& id,
178 bool allowed) { 194 bool allowed) {
179 responses_[id.render_process_id()] = std::make_pair(id.bridge_id(), allowed); 195 responses_[id.render_process_id()] = std::make_pair(id.bridge_id(), allowed);
180 } 196 }
(...skipping 29 matching lines...) Expand all
210 new_tab->GetController().LoadURL( 226 new_tab->GetController().LoadURL(
211 url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, std::string()); 227 url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, std::string());
212 content::RenderFrameHostTester::For(new_tab->GetMainFrame()) 228 content::RenderFrameHostTester::For(new_tab->GetMainFrame())
213 ->SendNavigate(extra_tabs_.size() + 1, url); 229 ->SendNavigate(extra_tabs_.size() + 1, url);
214 230
215 // Set up required helpers, and make this be as "tabby" as the code requires. 231 // Set up required helpers, and make this be as "tabby" as the code requires.
216 #if defined(ENABLE_EXTENSIONS) 232 #if defined(ENABLE_EXTENSIONS)
217 extensions::SetViewType(new_tab, extensions::VIEW_TYPE_TAB_CONTENTS); 233 extensions::SetViewType(new_tab, extensions::VIEW_TYPE_TAB_CONTENTS);
218 #endif 234 #endif
219 InfoBarService::CreateForWebContents(new_tab); 235 InfoBarService::CreateForWebContents(new_tab);
236 PermissionBubbleManager::CreateForWebContents(new_tab);
237 PermissionBubbleManager::FromWebContents(new_tab)->SetView(&bubble_view_);
220 238
221 extra_tabs_.push_back(new_tab); 239 extra_tabs_.push_back(new_tab);
222 } 240 }
223 241
224 void GeolocationPermissionContextTests::CheckTabContentsState( 242 void GeolocationPermissionContextTests::CheckTabContentsState(
225 const GURL& requesting_frame, 243 const GURL& requesting_frame,
226 ContentSetting expected_content_setting) { 244 ContentSetting expected_content_setting) {
227 TabSpecificContentSettings* content_settings = 245 TabSpecificContentSettings* content_settings =
228 TabSpecificContentSettings::FromWebContents(web_contents()); 246 TabSpecificContentSettings::FromWebContents(web_contents());
229 const ContentSettingsUsagesState::StateMap& state_map = 247 const ContentSettingsUsagesState::StateMap& state_map =
(...skipping 18 matching lines...) Expand all
248 TabSpecificContentSettings::CreateForWebContents(web_contents()); 266 TabSpecificContentSettings::CreateForWebContents(web_contents());
249 geolocation_permission_context_ = 267 geolocation_permission_context_ =
250 GeolocationPermissionContextFactory::GetForProfile(profile()); 268 GeolocationPermissionContextFactory::GetForProfile(profile());
251 #if defined(OS_ANDROID) 269 #if defined(OS_ANDROID)
252 static_cast<GeolocationPermissionContextAndroid*>( 270 static_cast<GeolocationPermissionContextAndroid*>(
253 geolocation_permission_context_) 271 geolocation_permission_context_)
254 ->SetLocationSettingsForTesting( 272 ->SetLocationSettingsForTesting(
255 scoped_ptr<LocationSettings>(new MockLocationSettings())); 273 scoped_ptr<LocationSettings>(new MockLocationSettings()));
256 MockLocationSettings::SetLocationStatus(true, true); 274 MockLocationSettings::SetLocationStatus(true, true);
257 #endif 275 #endif
276 PermissionBubbleManager::CreateForWebContents(web_contents());
277 PermissionBubbleManager::FromWebContents(web_contents())->SetView(
278 &bubble_view_);
258 } 279 }
259 280
260 void GeolocationPermissionContextTests::TearDown() { 281 void GeolocationPermissionContextTests::TearDown() {
261 extra_tabs_.clear(); 282 extra_tabs_.clear();
262 ChromeRenderViewHostTestHarness::TearDown(); 283 ChromeRenderViewHostTestHarness::TearDown();
263 } 284 }
264 285
286 base::string16 GeolocationPermissionContextTests::GetFirstRequestText(
287 PermissionBubbleManager* manager) {
288 return manager->requests_.front()->GetMessageText();
markusheintz_ 2015/02/02 10:46:52 This is only used once in the I actually think t
felt 2015/02/02 13:03:09 Done.
289 }
290
291 size_t GeolocationPermissionContextTests::GetBubblesQueueSize(
292 PermissionBubbleManager* manager) {
293 return manager->requests_.size();
294 }
295
296 void GeolocationPermissionContextTests::AcceptBubble(
297 PermissionBubbleManager* manager) {
298 manager->Accept();
299 }
300
301 void GeolocationPermissionContextTests::DenyBubble(
302 PermissionBubbleManager* manager) {
303 manager->Deny();
304 }
305
306 void GeolocationPermissionContextTests::CloseBubble(
307 PermissionBubbleManager* manager) {
308 manager->Closing();
309 }
310
311 void GeolocationPermissionContextTests::BubbleManagerDocumentLoadCompleted() {
312 GeolocationPermissionContextTests::BubbleManagerDocumentLoadCompleted(
313 web_contents());
314 }
315
316 void GeolocationPermissionContextTests::BubbleManagerDocumentLoadCompleted(
317 content::WebContents* web_contents) {
318 PermissionBubbleManager::FromWebContents(web_contents)->
319 DocumentOnLoadCompletedInMainFrame();
320 }
321
322 ContentSetting GeolocationPermissionContextTests::GetGeolocationContentSetting(
323 GURL frame_0, GURL frame_1) {
324 return profile()->GetHostContentSettingsMap()->GetContentSetting(
325 frame_0, frame_1, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string());
326 }
327
328 // Needed to parameterize the tests for both infobars & permission bubbles.
329 class GeolocationPermissionContextParamTests :
330 public GeolocationPermissionContextTests,
331 public ::testing::WithParamInterface<bool> {
332 protected:
333 GeolocationPermissionContextParamTests() {}
334 ~GeolocationPermissionContextParamTests() override {}
335
336 bool BubbleEnabled() const {
337 return GetParam();
338 }
339
340 void SetUp() override {
341 GeolocationPermissionContextTests::SetUp();
342 #if !defined(OS_ANDROID)
343 if (BubbleEnabled()) {
344 base::CommandLine::ForCurrentProcess()->AppendSwitch(
345 switches::kEnablePermissionsBubbles);
346 EXPECT_TRUE(PermissionBubbleManager::Enabled());
347 } else {
348 base::CommandLine::ForCurrentProcess()->AppendSwitch(
349 switches::kDisablePermissionsBubbles);
350 EXPECT_FALSE(PermissionBubbleManager::Enabled());
351 }
352 #endif
353 }
354
355 size_t GetNumberOfPrompts() {
356 if (BubbleEnabled()) {
357 PermissionBubbleManager* manager =
358 PermissionBubbleManager::FromWebContents(web_contents());
359 return GetBubblesQueueSize(manager);
360 } else {
361 return infobar_service()->infobar_count();
362 }
363 }
364
365 void AcceptPrompt() {
366 if (BubbleEnabled()) {
367 PermissionBubbleManager* manager =
368 PermissionBubbleManager::FromWebContents(web_contents());
369 AcceptBubble(manager);
370 } else {
371 infobars::InfoBar* infobar = infobar_service()->infobar_at(0);
372 ConfirmInfoBarDelegate* infobar_delegate =
373 infobar->delegate()->AsConfirmInfoBarDelegate();
374 infobar_delegate->Accept();
375 }
376 }
377
378 base::string16 GetPromptText() {
379 if (BubbleEnabled()) {
380 PermissionBubbleManager* manager =
381 PermissionBubbleManager::FromWebContents(web_contents());
382 return GetFirstRequestText(manager);
383 }
384 infobars::InfoBar* infobar = infobar_service()->infobar_at(0);
385 ConfirmInfoBarDelegate* infobar_delegate =
386 infobar->delegate()->AsConfirmInfoBarDelegate();
387 return infobar_delegate->GetMessageText();
388 }
389 private:
390 DISALLOW_COPY_AND_ASSIGN(GeolocationPermissionContextParamTests);
391 };
392
265 // Tests ---------------------------------------------------------------------- 393 // Tests ----------------------------------------------------------------------
266 394
267 TEST_F(GeolocationPermissionContextTests, SinglePermission) { 395 TEST_P(GeolocationPermissionContextParamTests, SinglePermissionInfobar) {
396 if (BubbleEnabled()) return;
397
268 GURL requesting_frame("http://www.example.com/geolocation"); 398 GURL requesting_frame("http://www.example.com/geolocation");
269 NavigateAndCommit(requesting_frame); 399 NavigateAndCommit(requesting_frame);
270 EXPECT_EQ(0U, infobar_service()->infobar_count()); 400 EXPECT_EQ(0U, infobar_service()->infobar_count());
271 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame); 401 RequestGeolocationPermission(
402 web_contents(), RequestID(0), requesting_frame, true);
272 ASSERT_EQ(1U, infobar_service()->infobar_count()); 403 ASSERT_EQ(1U, infobar_service()->infobar_count());
273 infobars::InfoBar* infobar = infobar_service()->infobar_at(0); 404 infobars::InfoBar* infobar = infobar_service()->infobar_at(0);
274 ConfirmInfoBarDelegate* infobar_delegate = 405 ConfirmInfoBarDelegate* infobar_delegate =
275 infobar->delegate()->AsConfirmInfoBarDelegate(); 406 infobar->delegate()->AsConfirmInfoBarDelegate();
276 ASSERT_TRUE(infobar_delegate); 407 ASSERT_TRUE(infobar_delegate);
277 infobar_delegate->Cancel(); 408 infobar_delegate->Cancel();
278 infobar_service()->RemoveInfoBar(infobar); 409 infobar_service()->RemoveInfoBar(infobar);
279 EXPECT_EQ(1U, closed_infobar_tracker_.size()); 410 EXPECT_EQ(1U, closed_infobar_tracker_.size());
280 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar)); 411 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar));
281 } 412 }
282 413
414 TEST_P(GeolocationPermissionContextParamTests, SinglePermissionBubble) {
415 if (!BubbleEnabled()) return;
416
417 GURL requesting_frame("http://www.example.com/geolocation");
418 NavigateAndCommit(requesting_frame);
419 BubbleManagerDocumentLoadCompleted();
420
421 EXPECT_EQ(0U, GetNumberOfPrompts());
422 RequestGeolocationPermission(
423 web_contents(), RequestID(0), requesting_frame, true);
424 ASSERT_EQ(1U, GetNumberOfPrompts());
425 }
426
283 #if defined(OS_ANDROID) 427 #if defined(OS_ANDROID)
428 // Infobar-only tests; Android doesn't support permission bubbles.
284 TEST_F(GeolocationPermissionContextTests, GeolocationEnabledDisabled) { 429 TEST_F(GeolocationPermissionContextTests, GeolocationEnabledDisabled) {
285 GURL requesting_frame("http://www.example.com/geolocation"); 430 GURL requesting_frame("http://www.example.com/geolocation");
286 NavigateAndCommit(requesting_frame); 431 NavigateAndCommit(requesting_frame);
287 MockLocationSettings::SetLocationStatus(true, true); 432 MockLocationSettings::SetLocationStatus(true, true);
288 EXPECT_EQ(0U, infobar_service()->infobar_count()); 433 EXPECT_EQ(0U, infobar_service()->infobar_count());
289 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame); 434 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame);
290 EXPECT_EQ(1U, infobar_service()->infobar_count()); 435 EXPECT_EQ(1U, infobar_service()->infobar_count());
291 ConfirmInfoBarDelegate* infobar_delegate_0 = 436 ConfirmInfoBarDelegate* infobar_delegate_0 =
292 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate(); 437 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
293 ASSERT_TRUE(infobar_delegate_0); 438 ASSERT_TRUE(infobar_delegate_0);
(...skipping 25 matching lines...) Expand all
319 TEST_F(GeolocationPermissionContextTests, MasterEnabledGoogleAppsDisabled) { 464 TEST_F(GeolocationPermissionContextTests, MasterEnabledGoogleAppsDisabled) {
320 GURL requesting_frame("http://www.example.com/geolocation"); 465 GURL requesting_frame("http://www.example.com/geolocation");
321 NavigateAndCommit(requesting_frame); 466 NavigateAndCommit(requesting_frame);
322 MockLocationSettings::SetLocationStatus(true, false); 467 MockLocationSettings::SetLocationStatus(true, false);
323 EXPECT_EQ(0U, infobar_service()->infobar_count()); 468 EXPECT_EQ(0U, infobar_service()->infobar_count());
324 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame); 469 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame);
325 EXPECT_EQ(0U, infobar_service()->infobar_count()); 470 EXPECT_EQ(0U, infobar_service()->infobar_count());
326 } 471 }
327 #endif 472 #endif
328 473
329 TEST_F(GeolocationPermissionContextTests, QueuedPermission) { 474 TEST_P(GeolocationPermissionContextParamTests, QueuedPermission) {
330 GURL requesting_frame_0("http://www.example.com/geolocation"); 475 GURL requesting_frame_0("http://www.example.com/geolocation");
331 GURL requesting_frame_1("http://www.example-2.com/geolocation"); 476 GURL requesting_frame_1("http://www.example-2.com/geolocation");
332 EXPECT_EQ(CONTENT_SETTING_ASK, 477 EXPECT_EQ(
333 profile()->GetHostContentSettingsMap()->GetContentSetting( 478 CONTENT_SETTING_ASK,
334 requesting_frame_0, requesting_frame_0, 479 GetGeolocationContentSetting(requesting_frame_0, requesting_frame_1));
335 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string())); 480 EXPECT_EQ(
336 EXPECT_EQ(CONTENT_SETTING_ASK, 481 CONTENT_SETTING_ASK,
337 profile()->GetHostContentSettingsMap()->GetContentSetting( 482 GetGeolocationContentSetting(requesting_frame_1, requesting_frame_1));
338 requesting_frame_1, requesting_frame_0,
339 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
340 483
341 NavigateAndCommit(requesting_frame_0); 484 NavigateAndCommit(requesting_frame_0);
342 EXPECT_EQ(0U, infobar_service()->infobar_count()); 485 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
486
487 // Check that no permission requests have happened yet.
488 EXPECT_EQ(0U, GetNumberOfPrompts());
489
343 // Request permission for two frames. 490 // Request permission for two frames.
344 RequestGeolocationPermission( 491 RequestGeolocationPermission(
345 web_contents(), RequestID(0), requesting_frame_0); 492 web_contents(), RequestID(0), requesting_frame_0, true);
346 RequestGeolocationPermission( 493 RequestGeolocationPermission(
347 web_contents(), RequestID(1), requesting_frame_1); 494 web_contents(), RequestID(1), requesting_frame_1, true);
348 // Ensure only one infobar is created. 495 // Ensure only one infobar is created.
349 ASSERT_EQ(1U, infobar_service()->infobar_count()); 496 ASSERT_EQ(1U, GetNumberOfPrompts());
350 infobars::InfoBar* infobar_0 = infobar_service()->infobar_at(0); 497 base::string16 text_0 = GetPromptText();
351 ConfirmInfoBarDelegate* infobar_delegate_0 =
352 infobar_0->delegate()->AsConfirmInfoBarDelegate();
353 ASSERT_TRUE(infobar_delegate_0);
354 base::string16 text_0 = infobar_delegate_0->GetMessageText();
355 498
356 // Accept the first frame. 499 // Accept the first frame.
357 infobar_delegate_0->Accept(); 500 AcceptPrompt();
358 CheckTabContentsState(requesting_frame_0, CONTENT_SETTING_ALLOW); 501 CheckTabContentsState(requesting_frame_0, CONTENT_SETTING_ALLOW);
359 CheckPermissionMessageSent(0, true); 502 CheckPermissionMessageSent(0, true);
360 503
361 infobar_service()->RemoveInfoBar(infobar_0); 504 if (!BubbleEnabled()) {
362 EXPECT_EQ(1U, closed_infobar_tracker_.size()); 505 infobars::InfoBar* infobar_0 = infobar_service()->infobar_at(0);
363 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_0)); 506 infobar_service()->RemoveInfoBar(infobar_0);
364 closed_infobar_tracker_.Clear(); 507 EXPECT_EQ(1U, closed_infobar_tracker_.size());
508 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_0));
509 closed_infobar_tracker_.Clear();
510 }
511
365 // Now we should have a new infobar for the second frame. 512 // Now we should have a new infobar for the second frame.
366 ASSERT_EQ(1U, infobar_service()->infobar_count()); 513 ASSERT_EQ(1U, GetNumberOfPrompts());
367 514 base::string16 text_1 = GetPromptText();
368 infobars::InfoBar* infobar_1 = infobar_service()->infobar_at(0); 515
369 ConfirmInfoBarDelegate* infobar_delegate_1 = 516 // Check that the messages differ.
370 infobar_1->delegate()->AsConfirmInfoBarDelegate();
371 ASSERT_TRUE(infobar_delegate_1);
372 base::string16 text_1 = infobar_delegate_1->GetMessageText();
373 EXPECT_NE(text_0, text_1); 517 EXPECT_NE(text_0, text_1);
374 518
375 // Cancel (block) this frame. 519 // Cancel (block) this frame.
376 infobar_delegate_1->Cancel(); 520 if (BubbleEnabled()) {
521 PermissionBubbleManager* manager =
522 PermissionBubbleManager::FromWebContents(web_contents());
523 DenyBubble(manager);
524 } else {
525 infobars::InfoBar* infobar_1 = infobar_service()->infobar_at(0);
526 infobar_1->delegate()->AsConfirmInfoBarDelegate()->Cancel();
527 }
377 CheckTabContentsState(requesting_frame_1, CONTENT_SETTING_BLOCK); 528 CheckTabContentsState(requesting_frame_1, CONTENT_SETTING_BLOCK);
378 CheckPermissionMessageSent(1, false); 529 CheckPermissionMessageSent(1, false);
379 infobar_service()->RemoveInfoBar(infobar_1); 530
380 EXPECT_EQ(1U, closed_infobar_tracker_.size());
381 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_1));
382 EXPECT_EQ(0U, infobar_service()->infobar_count());
383 // Ensure the persisted permissions are ok. 531 // Ensure the persisted permissions are ok.
384 EXPECT_EQ(CONTENT_SETTING_ALLOW, 532 EXPECT_EQ(
385 profile()->GetHostContentSettingsMap()->GetContentSetting( 533 CONTENT_SETTING_ALLOW,
386 requesting_frame_0, requesting_frame_0, 534 GetGeolocationContentSetting(requesting_frame_0, requesting_frame_0));
387 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string())); 535 EXPECT_EQ(
388 536 CONTENT_SETTING_BLOCK,
389 EXPECT_EQ(CONTENT_SETTING_BLOCK, 537 GetGeolocationContentSetting(requesting_frame_1, requesting_frame_0));
390 profile()->GetHostContentSettingsMap()->GetContentSetting( 538 }
391 requesting_frame_1, requesting_frame_0, 539
392 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string())); 540 TEST_P(GeolocationPermissionContextParamTests, HashIsIgnored) {
393 }
394
395 TEST_F(GeolocationPermissionContextTests, HashIsIgnored) {
396 GURL url_a("http://www.example.com/geolocation#a"); 541 GURL url_a("http://www.example.com/geolocation#a");
397 GURL url_b("http://www.example.com/geolocation#b"); 542 GURL url_b("http://www.example.com/geolocation#b");
398 543
399 // Navigate to the first url and check permission is requested. 544 // Navigate to the first url.
400 NavigateAndCommit(url_a); 545 NavigateAndCommit(url_a);
401 EXPECT_EQ(0U, infobar_service()->infobar_count()); 546 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
402 RequestGeolocationPermission(web_contents(), RequestID(0), url_a); 547
403 ASSERT_EQ(1U, infobar_service()->infobar_count()); 548 // Check permission is requested.
404 infobars::InfoBar* infobar = infobar_service()->infobar_at(0); 549 ASSERT_EQ(0U, GetNumberOfPrompts());
405 ConfirmInfoBarDelegate* infobar_delegate = 550 RequestGeolocationPermission(
406 infobar->delegate()->AsConfirmInfoBarDelegate(); 551 web_contents(), RequestID(0), url_a, BubbleEnabled());
407 ASSERT_TRUE(infobar_delegate); 552 ASSERT_EQ(1U, GetNumberOfPrompts());
408 553
409 // Change the hash, we'll still be on the same page. 554 // Change the hash, we'll still be on the same page.
410 NavigateAndCommit(url_b); 555 NavigateAndCommit(url_b);
556 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
411 557
412 // Accept. 558 // Accept.
413 infobar_delegate->Accept(); 559 AcceptPrompt();
414 CheckTabContentsState(url_a, CONTENT_SETTING_ALLOW); 560 CheckTabContentsState(url_a, CONTENT_SETTING_ALLOW);
415 CheckTabContentsState(url_b, CONTENT_SETTING_ALLOW); 561 CheckTabContentsState(url_b, CONTENT_SETTING_ALLOW);
416 CheckPermissionMessageSent(0, true); 562 CheckPermissionMessageSent(0, true);
417 563
418 // Cleanup. 564 // Cleanup.
419 infobar_service()->RemoveInfoBar(infobar); 565 if (!BubbleEnabled()) {
420 EXPECT_EQ(1U, closed_infobar_tracker_.size()); 566 infobars::InfoBar* infobar = infobar_service()->infobar_at(0);
421 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar)); 567 infobar_service()->RemoveInfoBar(infobar);
422 } 568 EXPECT_EQ(1U, closed_infobar_tracker_.size());
423 569 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar));
424 TEST_F(GeolocationPermissionContextTests, PermissionForFileScheme) { 570 }
571 }
572
573 TEST_P(GeolocationPermissionContextParamTests, PermissionForFileScheme) {
574 // TODO(felt): The bubble is rejecting file:// permission requests.
575 // Fix and enable this test. crbug.com/444047
576 if (BubbleEnabled()) return;
577
425 GURL requesting_frame("file://example/geolocation.html"); 578 GURL requesting_frame("file://example/geolocation.html");
426 NavigateAndCommit(requesting_frame); 579 NavigateAndCommit(requesting_frame);
427 EXPECT_EQ(0U, infobar_service()->infobar_count()); 580 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
428 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame); 581
429 EXPECT_EQ(1U, infobar_service()->infobar_count()); 582 // Check permission is requested.
430 infobars::InfoBar* infobar = infobar_service()->infobar_at(0); 583 ASSERT_EQ(0U, GetNumberOfPrompts());
431 ConfirmInfoBarDelegate* infobar_delegate = 584 RequestGeolocationPermission(
432 infobar->delegate()->AsConfirmInfoBarDelegate(); 585 web_contents(), RequestID(0), requesting_frame, true);
433 ASSERT_TRUE(infobar_delegate); 586 EXPECT_EQ(1U, GetNumberOfPrompts());
587
434 // Accept the frame. 588 // Accept the frame.
435 infobar_delegate->Accept(); 589 AcceptPrompt();
436 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW); 590 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW);
437 CheckPermissionMessageSent(0, true); 591 CheckPermissionMessageSent(0, true);
438 infobar_service()->RemoveInfoBar(infobar);
439 592
440 // Make sure the setting is not stored. 593 // Make sure the setting is not stored.
441 EXPECT_EQ(CONTENT_SETTING_ASK, 594 EXPECT_EQ(
442 profile()->GetHostContentSettingsMap()->GetContentSetting( 595 CONTENT_SETTING_ASK,
443 requesting_frame, 596 GetGeolocationContentSetting(requesting_frame, requesting_frame));
444 requesting_frame, 597 }
445 CONTENT_SETTINGS_TYPE_GEOLOCATION, 598
446 std::string())); 599 TEST_P(GeolocationPermissionContextParamTests,
447 } 600 CancelGeolocationPermissionRequest) {
448 601 GURL frame_0("http://www.example.com/geolocation");
449 TEST_F(GeolocationPermissionContextTests, CancelGeolocationPermissionRequest) { 602 GURL frame_1("http://www.example-2.com/geolocation");
450 GURL requesting_frame_0("http://www.example.com/geolocation"); 603 EXPECT_EQ(
451 GURL requesting_frame_1("http://www.example-2.com/geolocation"); 604 CONTENT_SETTING_ASK, GetGeolocationContentSetting(frame_0, frame_0));
452 EXPECT_EQ(CONTENT_SETTING_ASK, 605 EXPECT_EQ(
453 profile()->GetHostContentSettingsMap()->GetContentSetting( 606 CONTENT_SETTING_ASK, GetGeolocationContentSetting(frame_1, frame_0));
454 requesting_frame_0, requesting_frame_0, 607
455 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string())); 608 NavigateAndCommit(frame_0);
456 609 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
457 EXPECT_EQ(CONTENT_SETTING_ASK, 610
458 profile()->GetHostContentSettingsMap()->GetContentSetting( 611 ASSERT_EQ(0U, GetNumberOfPrompts());
459 requesting_frame_1, requesting_frame_0,
460 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
461
462 NavigateAndCommit(requesting_frame_0);
463 EXPECT_EQ(0U, infobar_service()->infobar_count());
464 612
465 // Request permission for two frames. 613 // Request permission for two frames.
466 RequestGeolocationPermission( 614 RequestGeolocationPermission(
467 web_contents(), RequestID(0), requesting_frame_0); 615 web_contents(), RequestID(0), frame_0, true);
468 RequestGeolocationPermission( 616 RequestGeolocationPermission(
469 web_contents(), RequestID(1), requesting_frame_1); 617 web_contents(), RequestID(1), frame_1, true);
470 ASSERT_EQ(1U, infobar_service()->infobar_count()); 618
471 619 // Get the first permission request text.
472 infobars::InfoBar* infobar_0 = infobar_service()->infobar_at(0); 620 ASSERT_EQ(1U, GetNumberOfPrompts());
473 ConfirmInfoBarDelegate* infobar_delegate_0 = 621 base::string16 text_0 = GetPromptText();
474 infobar_0->delegate()->AsConfirmInfoBarDelegate(); 622 ASSERT_FALSE(text_0.empty());
475 ASSERT_TRUE(infobar_delegate_0); 623
476 base::string16 text_0 = infobar_delegate_0->GetMessageText(); 624 // Simulate the frame going away; the request should be removed.
477 625 if (BubbleEnabled()) {
478 // Simulate the frame going away, ensure the infobar for this frame 626 PermissionBubbleManager* manager =
479 // is removed and the next pending infobar is created. 627 PermissionBubbleManager::FromWebContents(web_contents());
480 geolocation_permission_context_->CancelPermissionRequest(web_contents(), 628 CloseBubble(manager);
481 RequestID(0)); 629 } else {
482 EXPECT_EQ(1U, closed_infobar_tracker_.size()); 630 geolocation_permission_context_->CancelPermissionRequest(web_contents(),
483 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_0)); 631 RequestID(0));
484 closed_infobar_tracker_.Clear(); 632 }
485 ASSERT_EQ(1U, infobar_service()->infobar_count()); 633
486 634 // Check that the next pending request is created correctly.
487 infobars::InfoBar* infobar_1 = infobar_service()->infobar_at(0); 635 base::string16 text_1 = GetPromptText();
488 ConfirmInfoBarDelegate* infobar_delegate_1 =
489 infobar_1->delegate()->AsConfirmInfoBarDelegate();
490 ASSERT_TRUE(infobar_delegate_1);
491 base::string16 text_1 = infobar_delegate_1->GetMessageText();
492 EXPECT_NE(text_0, text_1); 636 EXPECT_NE(text_0, text_1);
493 637
494 // Allow this frame. 638 // Allow this frame and check that it worked.
495 infobar_delegate_1->Accept(); 639 AcceptPrompt();
496 CheckTabContentsState(requesting_frame_1, CONTENT_SETTING_ALLOW); 640 CheckTabContentsState(frame_1, CONTENT_SETTING_ALLOW);
497 CheckPermissionMessageSent(1, true); 641 CheckPermissionMessageSent(1, true);
498 infobar_service()->RemoveInfoBar(infobar_1); 642
499 EXPECT_EQ(1U, closed_infobar_tracker_.size());
500 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_1));
501 EXPECT_EQ(0U, infobar_service()->infobar_count());
502 // Ensure the persisted permissions are ok. 643 // Ensure the persisted permissions are ok.
503 EXPECT_EQ(CONTENT_SETTING_ASK, 644 EXPECT_EQ(
504 profile()->GetHostContentSettingsMap()->GetContentSetting( 645 CONTENT_SETTING_ASK, GetGeolocationContentSetting(frame_0, frame_0));
505 requesting_frame_0, requesting_frame_0, 646 EXPECT_EQ(
506 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string())); 647 CONTENT_SETTING_ALLOW, GetGeolocationContentSetting(frame_1, frame_0));
507 648 }
508 EXPECT_EQ(CONTENT_SETTING_ALLOW, 649
509 profile()->GetHostContentSettingsMap()->GetContentSetting( 650 TEST_P(GeolocationPermissionContextParamTests, InvalidURL) {
510 requesting_frame_1, requesting_frame_0, 651 // Navigate to the first url.
511 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
512 }
513
514 TEST_F(GeolocationPermissionContextTests, InvalidURL) {
515 GURL invalid_embedder("about:blank"); 652 GURL invalid_embedder("about:blank");
516 GURL requesting_frame; 653 GURL requesting_frame;
517 NavigateAndCommit(invalid_embedder); 654 NavigateAndCommit(invalid_embedder);
518 EXPECT_EQ(0U, infobar_service()->infobar_count()); 655 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
519 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame); 656
520 EXPECT_EQ(0U, infobar_service()->infobar_count()); 657 // Nothing should be displayed.
658 EXPECT_EQ(0U, GetNumberOfPrompts());
659 RequestGeolocationPermission(
660 web_contents(), RequestID(0), requesting_frame, true);
661 EXPECT_EQ(0U, GetNumberOfPrompts());
521 CheckPermissionMessageSent(0, false); 662 CheckPermissionMessageSent(0, false);
522 } 663 }
523 664
524 TEST_F(GeolocationPermissionContextTests, SameOriginMultipleTabs) { 665 TEST_P(GeolocationPermissionContextParamTests, SameOriginMultipleTabs) {
525 GURL url_a("http://www.example.com/geolocation"); 666 GURL url_a("http://www.example.com/geolocation");
526 GURL url_b("http://www.example-2.com/geolocation"); 667 GURL url_b("http://www.example-2.com/geolocation");
527 NavigateAndCommit(url_a); 668 NavigateAndCommit(url_a); // Tab A0
528 AddNewTab(url_b); 669 AddNewTab(url_b); // Tab B (extra_tabs_[0])
529 AddNewTab(url_a); 670 AddNewTab(url_a); // Tab A1 (extra_tabs_[1])
530 671 if (BubbleEnabled()) {
531 EXPECT_EQ(0U, infobar_service()->infobar_count()); 672 BubbleManagerDocumentLoadCompleted();
532 RequestGeolocationPermission(web_contents(), RequestID(0), url_a); 673 BubbleManagerDocumentLoadCompleted(extra_tabs_[0]);
533 ASSERT_EQ(1U, infobar_service()->infobar_count()); 674 BubbleManagerDocumentLoadCompleted(extra_tabs_[1]);
534 675 }
535 RequestGeolocationPermission(extra_tabs_[0], RequestIDForTab(0, 0), url_b); 676 PermissionBubbleManager* manager_a0 =
536 EXPECT_EQ(1U, infobar_service_for_tab(0)->infobar_count()); 677 PermissionBubbleManager::FromWebContents(web_contents());
537 678 PermissionBubbleManager* manager_b =
538 RequestGeolocationPermission(extra_tabs_[1], RequestIDForTab(1, 0), url_a); 679 PermissionBubbleManager::FromWebContents(extra_tabs_[0]);
539 ASSERT_EQ(1U, infobar_service_for_tab(1)->infobar_count()); 680 PermissionBubbleManager* manager_a1 =
540 681 PermissionBubbleManager::FromWebContents(extra_tabs_[1]);
541 infobars::InfoBar* removed_infobar = 682
542 infobar_service_for_tab(1)->infobar_at(0); 683 // Request permission in all three tabs.
543 684 RequestGeolocationPermission(
544 // Accept the first tab. 685 web_contents(), RequestID(0), url_a, true);
545 infobars::InfoBar* infobar_0 = infobar_service()->infobar_at(0); 686 RequestGeolocationPermission(
546 ConfirmInfoBarDelegate* infobar_delegate_0 = 687 extra_tabs_[0], RequestIDForTab(0, 0), url_b, true);
547 infobar_0->delegate()->AsConfirmInfoBarDelegate(); 688 RequestGeolocationPermission(
548 ASSERT_TRUE(infobar_delegate_0); 689 extra_tabs_[1], RequestIDForTab(1, 0), url_a, true);
549 infobar_delegate_0->Accept(); 690 ASSERT_EQ(1U, GetNumberOfPrompts()); // For A0.
691 if (BubbleEnabled()) {
692 ASSERT_EQ(1U, GetBubblesQueueSize(manager_b));
693 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a1));
694 } else {
695 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
696 ASSERT_EQ(1U, infobar_service_for_tab(1)->infobar_count());
697 }
698
699 // Accept the permission in tab A0.
700 if (BubbleEnabled()) {
701 AcceptBubble(manager_a0);
702 } else {
703 infobars::InfoBar* infobar_a0 = infobar_service()->infobar_at(0);
704 ConfirmInfoBarDelegate* infobar_delegate_a0 =
705 infobar_a0->delegate()->AsConfirmInfoBarDelegate();
706 ASSERT_TRUE(infobar_delegate_a0);
707 infobar_delegate_a0->Accept();
708 infobar_service()->RemoveInfoBar(infobar_a0);
709 EXPECT_EQ(2U, closed_infobar_tracker_.size());
710 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_a0));
711 }
550 CheckPermissionMessageSent(0, true); 712 CheckPermissionMessageSent(0, true);
551 infobar_service()->RemoveInfoBar(infobar_0); 713 // Because they're the same origin, this will cause tab A1's infobar to
552 EXPECT_EQ(2U, closed_infobar_tracker_.size()); 714 // disappear. It does not cause the bubble to disappear: crbug.com/443013.
553 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_0)); 715 // TODO(felt): Update this test when the bubble's behavior is changed.
554 // Now the infobar for the tab with the same origin should have gone. 716 if (BubbleEnabled())
555 EXPECT_EQ(0U, infobar_service_for_tab(1)->infobar_count()); 717 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a1));
556 CheckPermissionMessageSentForTab(1, 0, true); 718 else
557 EXPECT_TRUE(closed_infobar_tracker_.Contains(removed_infobar)); 719 CheckPermissionMessageSentForTab(1, 0, true);
558 closed_infobar_tracker_.Clear(); 720
559 721 // Either way, tab B should still have a pending permission request.
560 // But the other tab should still have the info bar... 722 if (BubbleEnabled())
561 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count()); 723 ASSERT_EQ(1U, GetBubblesQueueSize(manager_b));
562 infobars::InfoBar* infobar_1 = infobar_service_for_tab(0)->infobar_at(0); 724 else
563 ConfirmInfoBarDelegate* infobar_delegate_1 = 725 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
564 infobar_1->delegate()->AsConfirmInfoBarDelegate(); 726 }
565 ASSERT_TRUE(infobar_delegate_1); 727
566 infobar_delegate_1->Cancel(); 728 TEST_P(GeolocationPermissionContextParamTests, QueuedOriginMultipleTabs) {
567 infobar_service_for_tab(0)->RemoveInfoBar(infobar_1);
568 EXPECT_EQ(1U, closed_infobar_tracker_.size());
569 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_1));
570 }
571
572 TEST_F(GeolocationPermissionContextTests, QueuedOriginMultipleTabs) {
573 GURL url_a("http://www.example.com/geolocation"); 729 GURL url_a("http://www.example.com/geolocation");
574 GURL url_b("http://www.example-2.com/geolocation"); 730 GURL url_b("http://www.example-2.com/geolocation");
575 NavigateAndCommit(url_a); 731 NavigateAndCommit(url_a); // Tab A0.
576 AddNewTab(url_a); 732 AddNewTab(url_a); // Tab A1.
577 733 if (BubbleEnabled()) {
578 EXPECT_EQ(0U, infobar_service()->infobar_count()); 734 BubbleManagerDocumentLoadCompleted();
579 RequestGeolocationPermission(web_contents(), RequestID(0), url_a); 735 BubbleManagerDocumentLoadCompleted(extra_tabs_[0]);
580 ASSERT_EQ(1U, infobar_service()->infobar_count()); 736 }
581 737 PermissionBubbleManager* manager_a0 =
582 RequestGeolocationPermission(extra_tabs_[0], RequestIDForTab(0, 0), url_a); 738 PermissionBubbleManager::FromWebContents(web_contents());
583 EXPECT_EQ(1U, infobar_service_for_tab(0)->infobar_count()); 739 PermissionBubbleManager* manager_a1 =
584 740 PermissionBubbleManager::FromWebContents(extra_tabs_[0]);
585 RequestGeolocationPermission(extra_tabs_[0], RequestIDForTab(0, 1), url_b); 741 infobars::InfoBar* infobar_a0;
586 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count()); 742
587 743 // Request permission in both tabs; the extra tab will have two permission
588 infobars::InfoBar* removed_infobar = infobar_service()->infobar_at(0); 744 // requests from two origins.
589 745 RequestGeolocationPermission(
590 // Accept the second tab. 746 web_contents(), RequestID(0), url_a, true);
591 infobars::InfoBar* infobar_0 = infobar_service_for_tab(0)->infobar_at(0); 747 RequestGeolocationPermission(
592 ConfirmInfoBarDelegate* infobar_delegate_0 = 748 extra_tabs_[0], RequestIDForTab(0, 0), url_a, true);
593 infobar_0->delegate()->AsConfirmInfoBarDelegate(); 749 RequestGeolocationPermission(
594 ASSERT_TRUE(infobar_delegate_0); 750 extra_tabs_[0], RequestIDForTab(0, 1), url_b, true);
595 infobar_delegate_0->Accept(); 751 if (BubbleEnabled()) {
752 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a0));
753 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a1));
754 } else {
755 ASSERT_EQ(1U, infobar_service()->infobar_count());
756 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
757 infobar_a0 = infobar_service()->infobar_at(0); // Save for later.
758 }
759
760 // Accept the first request in tab A1.
761 if (BubbleEnabled()) {
762 AcceptBubble(manager_a1);
763 } else {
764 infobars::InfoBar* infobar_a1 = infobar_service_for_tab(0)->infobar_at(0);
765 ConfirmInfoBarDelegate* infobar_delegate_a1 =
766 infobar_a1->delegate()->AsConfirmInfoBarDelegate();
767 ASSERT_TRUE(infobar_delegate_a1);
768 infobar_delegate_a1->Accept();
769 infobar_service_for_tab(0)->RemoveInfoBar(infobar_a1);
770 EXPECT_EQ(2U, closed_infobar_tracker_.size());
771 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_a1));
772 }
596 CheckPermissionMessageSentForTab(0, 0, true); 773 CheckPermissionMessageSentForTab(0, 0, true);
597 infobar_service_for_tab(0)->RemoveInfoBar(infobar_0); 774
598 EXPECT_EQ(2U, closed_infobar_tracker_.size()); 775 // Because they're the same origin, this will cause tab A0's infobar to
599 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_0)); 776 // disappear. It does not cause the bubble to disappear: crbug.com/443013.
600 // Now the infobar for the tab with the same origin should have gone. 777 // TODO(felt): Update this test when the bubble's behavior is changed.
601 EXPECT_EQ(0U, infobar_service()->infobar_count()); 778 if (BubbleEnabled()) {
602 CheckPermissionMessageSent(0, true); 779 EXPECT_EQ(1U, GetBubblesQueueSize(manager_a0));
603 EXPECT_TRUE(closed_infobar_tracker_.Contains(removed_infobar)); 780 } else {
604 closed_infobar_tracker_.Clear(); 781 EXPECT_EQ(0U, infobar_service()->infobar_count());
605 782 CheckPermissionMessageSent(0, true);
606 // And we should have the queued infobar displayed now. 783 }
607 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count()); 784
608 785 // The second request should now be visible in tab A1.
609 // Accept the second infobar. 786 if (BubbleEnabled())
610 infobars::InfoBar* infobar_1 = infobar_service_for_tab(0)->infobar_at(0); 787 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a1));
611 ConfirmInfoBarDelegate* infobar_delegate_1 = 788 else
612 infobar_1->delegate()->AsConfirmInfoBarDelegate(); 789 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
613 ASSERT_TRUE(infobar_delegate_1); 790
614 infobar_delegate_1->Accept(); 791 // Accept the second request and check that it's gone.
615 CheckPermissionMessageSentForTab(0, 1, true); 792 if (BubbleEnabled()) {
616 infobar_service_for_tab(0)->RemoveInfoBar(infobar_1); 793 AcceptBubble(manager_a1);
617 EXPECT_EQ(1U, closed_infobar_tracker_.size()); 794 EXPECT_EQ(0U, GetBubblesQueueSize(manager_a1));
618 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_1)); 795 } else {
619 } 796 infobars::InfoBar* infobar_1 = infobar_service_for_tab(0)->infobar_at(0);
620 797 ConfirmInfoBarDelegate* infobar_delegate_1 =
621 TEST_F(GeolocationPermissionContextTests, TabDestroyed) { 798 infobar_1->delegate()->AsConfirmInfoBarDelegate();
799 ASSERT_TRUE(infobar_delegate_1);
800 infobar_delegate_1->Accept();
801 }
802 }
803
804 TEST_P(GeolocationPermissionContextParamTests, TabDestroyed) {
622 GURL requesting_frame_0("http://www.example.com/geolocation"); 805 GURL requesting_frame_0("http://www.example.com/geolocation");
623 GURL requesting_frame_1("http://www.example-2.com/geolocation"); 806 GURL requesting_frame_1("http://www.example-2.com/geolocation");
624 EXPECT_EQ(CONTENT_SETTING_ASK, 807 EXPECT_EQ(
625 profile()->GetHostContentSettingsMap()->GetContentSetting( 808 CONTENT_SETTING_ASK,
626 requesting_frame_0, requesting_frame_0, 809 GetGeolocationContentSetting(requesting_frame_0, requesting_frame_0));
627 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string())); 810 EXPECT_EQ(
628 811 CONTENT_SETTING_ASK,
629 EXPECT_EQ(CONTENT_SETTING_ASK, 812 GetGeolocationContentSetting(requesting_frame_1, requesting_frame_0));
630 profile()->GetHostContentSettingsMap()->GetContentSetting(
631 requesting_frame_1, requesting_frame_0,
632 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
633 813
634 NavigateAndCommit(requesting_frame_0); 814 NavigateAndCommit(requesting_frame_0);
635 EXPECT_EQ(0U, infobar_service()->infobar_count()); 815 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
816
636 // Request permission for two frames. 817 // Request permission for two frames.
637 RequestGeolocationPermission( 818 RequestGeolocationPermission(
638 web_contents(), RequestID(0), requesting_frame_0); 819 web_contents(), RequestID(0), requesting_frame_0, false);
639 RequestGeolocationPermission( 820 RequestGeolocationPermission(
640 web_contents(), RequestID(1), requesting_frame_1); 821 web_contents(), RequestID(1), requesting_frame_1, false);
641 // Ensure only one infobar is created. 822
642 ASSERT_EQ(1U, infobar_service()->infobar_count()); 823 // Ensure only one prompt is created.
643 infobars::InfoBar* infobar = infobar_service()->infobar_at(0); 824 ASSERT_EQ(1U, GetNumberOfPrompts());
644 825
645 // Delete the tab contents. 826 // Delete the tab contents.
646 DeleteContents(); 827 if (!BubbleEnabled()) {
647 828 infobars::InfoBar* infobar = infobar_service()->infobar_at(0);
648 // During contents destruction, the infobar will have been closed, and the 829 DeleteContents();
649 // pending request should have been cleared without an infobar being created. 830 ASSERT_EQ(1U, closed_infobar_tracker_.size());
650 ASSERT_EQ(1U, closed_infobar_tracker_.size()); 831 ASSERT_TRUE(closed_infobar_tracker_.Contains(infobar));
651 ASSERT_TRUE(closed_infobar_tracker_.Contains(infobar)); 832 }
652 } 833
653 834 // The content settings should not have changed.
654 TEST_F(GeolocationPermissionContextTests, LastUsageAudited) { 835 EXPECT_EQ(
836 CONTENT_SETTING_ASK,
837 GetGeolocationContentSetting(requesting_frame_0, requesting_frame_0));
838 EXPECT_EQ(
839 CONTENT_SETTING_ASK,
840 GetGeolocationContentSetting(requesting_frame_1, requesting_frame_0));
841 }
842
843 TEST_P(GeolocationPermissionContextParamTests, LastUsageAudited) {
655 GURL requesting_frame("http://www.example.com/geolocation"); 844 GURL requesting_frame("http://www.example.com/geolocation");
656 NavigateAndCommit(requesting_frame); 845 NavigateAndCommit(requesting_frame);
846 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
657 847
658 base::SimpleTestClock* test_clock = new base::SimpleTestClock; 848 base::SimpleTestClock* test_clock = new base::SimpleTestClock;
659 test_clock->SetNow(base::Time::UnixEpoch() + 849 test_clock->SetNow(base::Time::UnixEpoch() +
660 base::TimeDelta::FromSeconds(10)); 850 base::TimeDelta::FromSeconds(10));
661 851
662 HostContentSettingsMap* map = profile()->GetHostContentSettingsMap(); 852 HostContentSettingsMap* map = profile()->GetHostContentSettingsMap();
663 map->SetPrefClockForTesting(scoped_ptr<base::Clock>(test_clock)); 853 map->SetPrefClockForTesting(scoped_ptr<base::Clock>(test_clock));
664 854
665 // The permission shouldn't have been used yet. 855 // The permission shouldn't have been used yet.
666 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(), 856 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(),
667 requesting_frame.GetOrigin(), 857 requesting_frame.GetOrigin(),
668 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), 858 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
669 0); 859 0);
860 ASSERT_EQ(0U, GetNumberOfPrompts());
861 RequestGeolocationPermission(
862 web_contents(), RequestID(0), requesting_frame, false);
863 ASSERT_EQ(1U, GetNumberOfPrompts());
670 864
671 EXPECT_EQ(0U, infobar_service()->infobar_count()); 865 AcceptPrompt();
672 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame);
673 ASSERT_EQ(1U, infobar_service()->infobar_count());
674 infobars::InfoBar* infobar = infobar_service()->infobar_at(0);
675 ConfirmInfoBarDelegate* infobar_delegate =
676 infobar->delegate()->AsConfirmInfoBarDelegate();
677 ASSERT_TRUE(infobar_delegate);
678 infobar_delegate->Accept();
679 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW); 866 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW);
680 CheckPermissionMessageSent(0, true); 867 CheckPermissionMessageSent(0, true);
681 868
682 // Permission has been used at the starting time. 869 // Permission has been used at the starting time.
683 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(), 870 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(),
684 requesting_frame.GetOrigin(), 871 requesting_frame.GetOrigin(),
685 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), 872 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
686 10); 873 10);
687 874
688 test_clock->Advance(base::TimeDelta::FromSeconds(3)); 875 test_clock->Advance(base::TimeDelta::FromSeconds(3));
689 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame); 876 RequestGeolocationPermission(
877 web_contents(), RequestID(0), requesting_frame, false);
690 878
691 // Permission has been used three seconds later. 879 // Permission has been used three seconds later.
692 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(), 880 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(),
693 requesting_frame.GetOrigin(), 881 requesting_frame.GetOrigin(),
694 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), 882 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
695 13); 883 13);
696 } 884 }
697 885
698 TEST_F(GeolocationPermissionContextTests, LastUsageAuditedMultipleFrames) { 886 TEST_P(GeolocationPermissionContextParamTests, LastUsageAuditedMultipleFrames) {
699 base::SimpleTestClock* test_clock = new base::SimpleTestClock; 887 base::SimpleTestClock* test_clock = new base::SimpleTestClock;
700 test_clock->SetNow(base::Time::UnixEpoch() + 888 test_clock->SetNow(base::Time::UnixEpoch() +
701 base::TimeDelta::FromSeconds(10)); 889 base::TimeDelta::FromSeconds(10));
702 890
703 HostContentSettingsMap* map = profile()->GetHostContentSettingsMap(); 891 HostContentSettingsMap* map = profile()->GetHostContentSettingsMap();
704 map->SetPrefClockForTesting(scoped_ptr<base::Clock>(test_clock)); 892 map->SetPrefClockForTesting(scoped_ptr<base::Clock>(test_clock));
705 893
706 GURL requesting_frame_0("http://www.example.com/geolocation"); 894 GURL requesting_frame_0("http://www.example.com/geolocation");
707 GURL requesting_frame_1("http://www.example-2.com/geolocation"); 895 GURL requesting_frame_1("http://www.example-2.com/geolocation");
708 896
709 // The permission shouldn't have been used yet. 897 // The permission shouldn't have been used yet.
710 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(), 898 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(),
711 requesting_frame_0.GetOrigin(), 899 requesting_frame_0.GetOrigin(),
712 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), 900 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
713 0); 901 0);
714 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(), 902 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(),
715 requesting_frame_0.GetOrigin(), 903 requesting_frame_0.GetOrigin(),
716 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), 904 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
717 0); 905 0);
718 906
719 NavigateAndCommit(requesting_frame_0); 907 NavigateAndCommit(requesting_frame_0);
720 EXPECT_EQ(0U, infobar_service()->infobar_count()); 908 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
909
910 EXPECT_EQ(0U, GetNumberOfPrompts());
721 911
722 // Request permission for two frames. 912 // Request permission for two frames.
723 RequestGeolocationPermission( 913 RequestGeolocationPermission(
724 web_contents(), RequestID(0), requesting_frame_0); 914 web_contents(), RequestID(0), requesting_frame_0, false);
725 RequestGeolocationPermission( 915 RequestGeolocationPermission(
726 web_contents(), RequestID(1), requesting_frame_1); 916 web_contents(), RequestID(1), requesting_frame_1, false);
727 917
728 // Ensure only one infobar is created. 918 // Ensure only one infobar is created.
729 ASSERT_EQ(1U, infobar_service()->infobar_count()); 919 ASSERT_EQ(1U, GetNumberOfPrompts());
730 infobars::InfoBar* infobar_0 = infobar_service()->infobar_at(0);
731 ConfirmInfoBarDelegate* infobar_delegate_0 =
732 infobar_0->delegate()->AsConfirmInfoBarDelegate();
733 920
734 // Accept the first frame. 921 // Accept the first frame.
735 infobar_delegate_0->Accept(); 922 AcceptPrompt();
923 if (!BubbleEnabled())
924 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0));
736 CheckTabContentsState(requesting_frame_0, CONTENT_SETTING_ALLOW); 925 CheckTabContentsState(requesting_frame_0, CONTENT_SETTING_ALLOW);
737 CheckPermissionMessageSent(0, true); 926 CheckPermissionMessageSent(0, true);
738 infobar_service()->RemoveInfoBar(infobar_0);
739 927
740 // Verify that accepting the first didn't accept because it's embedder 928 // Verify that accepting the first didn't accept because it's embedded
741 // in the other. 929 // in the other.
742 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(), 930 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(),
743 requesting_frame_0.GetOrigin(), 931 requesting_frame_0.GetOrigin(),
744 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), 932 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
745 10); 933 10);
746 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(), 934 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(),
747 requesting_frame_0.GetOrigin(), 935 requesting_frame_0.GetOrigin(),
748 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), 936 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
749 0); 937 0);
750 938
751 ASSERT_EQ(1U, infobar_service()->infobar_count()); 939 ASSERT_EQ(1U, GetNumberOfPrompts());
752 infobars::InfoBar* infobar_1 = infobar_service()->infobar_at(0);
753 ConfirmInfoBarDelegate* infobar_delegate_1 =
754 infobar_1->delegate()->AsConfirmInfoBarDelegate();
755 940
756 test_clock->Advance(base::TimeDelta::FromSeconds(1)); 941 test_clock->Advance(base::TimeDelta::FromSeconds(1));
757 942
758 // Allow the second frame. 943 // Allow the second frame.
759 infobar_delegate_1->Accept(); 944 AcceptPrompt();
760 CheckTabContentsState(requesting_frame_1, CONTENT_SETTING_ALLOW); 945 CheckTabContentsState(requesting_frame_1, CONTENT_SETTING_ALLOW);
761 CheckPermissionMessageSent(1, true); 946 CheckPermissionMessageSent(1, true);
762 infobar_service()->RemoveInfoBar(infobar_1); 947 if (!BubbleEnabled())
948 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0));
763 949
764 // Verify that the times are different. 950 // Verify that the times are different.
765 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(), 951 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(),
766 requesting_frame_0.GetOrigin(), 952 requesting_frame_0.GetOrigin(),
767 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), 953 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
768 10); 954 10);
769 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(), 955 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(),
770 requesting_frame_0.GetOrigin(), 956 requesting_frame_0.GetOrigin(),
771 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), 957 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
772 11); 958 11);
773 959
774 test_clock->Advance(base::TimeDelta::FromSeconds(2)); 960 test_clock->Advance(base::TimeDelta::FromSeconds(2));
775 RequestGeolocationPermission( 961 RequestGeolocationPermission(
776 web_contents(), RequestID(0), requesting_frame_0); 962 web_contents(), RequestID(0), requesting_frame_0, false);
777 963
778 // Verify that requesting permission in one frame doesn't update other where 964 // Verify that requesting permission in one frame doesn't update other where
779 // it is the embedder. 965 // it is the embedder.
780 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(), 966 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(),
781 requesting_frame_0.GetOrigin(), 967 requesting_frame_0.GetOrigin(),
782 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), 968 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
783 13); 969 13);
784 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(), 970 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(),
785 requesting_frame_0.GetOrigin(), 971 requesting_frame_0.GetOrigin(),
786 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), 972 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(),
787 11); 973 11);
788 } 974 }
975
976 INSTANTIATE_TEST_CASE_P(GeolocationPermissionContextTestsWithAndWithoutBubbles,
977 GeolocationPermissionContextParamTests,
978 ::testing::Values(false, true));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698