1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "base/command_line.h"
10 #include "base/functional/bind.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/run_loop.h"
13 #include "base/task/sequenced_task_runner.h"
14 #include "base/test/bind.h"
15 #include "base/test/metrics/histogram_tester.h"
16 #include "base/test/scoped_feature_list.h"
17 #include "build/build_config.h"
18 #include "components/permissions/features.h"
19 #include "components/permissions/permission_request.h"
20 #include "components/permissions/permission_request_manager.h"
21 #include "components/permissions/permission_ui_selector.h"
22 #include "components/permissions/permission_uma_util.h"
23 #include "components/permissions/permission_util.h"
24 #include "components/permissions/request_type.h"
25 #include "components/permissions/test/mock_permission_prompt_factory.h"
26 #include "components/permissions/test/mock_permission_request.h"
27 #include "components/permissions/test/test_permissions_client.h"
28 #include "content/public/test/test_renderer_host.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30 #include "third_party/abseil-cpp/absl/types/optional.h"
31 #include "ui/events/base_event_utils.h"
32 #include "ui/events/event.h"
34 namespace permissions {
37 using QuietUiReason = PermissionUiSelector::QuietUiReason;
40 class PermissionRequestManagerTest : public content::RenderViewHostTestHarness {
42 PermissionRequestManagerTest()
43 : RenderViewHostTestHarness(
44 base::test::TaskEnvironment::TimeSource::MOCK_TIME),
45 request1_(RequestType::kGeolocation,
46 PermissionRequestGestureType::GESTURE),
47 request2_(RequestType::kMultipleDownloads,
48 PermissionRequestGestureType::NO_GESTURE),
49 request_mic_(RequestType::kMicStream,
50 PermissionRequestGestureType::NO_GESTURE),
51 request_camera_(RequestType::kCameraStream,
52 PermissionRequestGestureType::NO_GESTURE),
53 #if !BUILDFLAG(IS_ANDROID)
54 request_ptz_(RequestType::kCameraPanTiltZoom,
55 PermissionRequestGestureType::NO_GESTURE),
57 iframe_request_same_domain_(GURL("https://www.google.com/some/url"),
58 RequestType::kMidiSysex),
59 iframe_request_other_domain_(GURL("https://www.youtube.com"),
60 RequestType::kClipboard),
61 iframe_request_camera_other_domain_(GURL("https://www.youtube.com"),
62 RequestType::kStorageAccess),
63 iframe_request_mic_other_domain_(GURL("https://www.youtube.com"),
64 RequestType::kMicStream) {
67 void SetUp() override {
68 content::RenderViewHostTestHarness::SetUp();
69 SetContents(CreateTestWebContents());
70 NavigateAndCommit(GURL(MockPermissionRequest::kDefaultOrigin));
72 PermissionRequestManager::CreateForWebContents(web_contents());
73 manager_ = PermissionRequestManager::FromWebContents(web_contents());
74 manager_->set_enabled_app_level_notification_permission_for_testing(true);
75 prompt_factory_ = std::make_unique<MockPermissionPromptFactory>(manager_);
78 void TearDown() override {
79 prompt_factory_ = nullptr;
80 content::RenderViewHostTestHarness::TearDown();
85 task_environment()->RunUntilIdle();
90 task_environment()->RunUntilIdle();
95 task_environment()->RunUntilIdle();
98 void OpenHelpCenterLink() {
99 #if !BUILDFLAG(IS_ANDROID)
100 const ui::MouseEvent event(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
101 ui::EventTimeForNow(), 0, 0);
102 #else // BUILDFLAG(IS_ANDROID)
103 const ui::TouchEvent event(
104 ui::ET_TOUCH_MOVED, gfx::PointF(), gfx::PointF(), ui::EventTimeForNow(),
105 ui::PointerDetails(ui::EventPointerType::kTouch, 1));
107 manager_->OpenHelpCenterLink(event);
108 task_environment()->RunUntilIdle();
111 void WaitForFrameLoad() {
112 // PermissionRequestManager ignores all parameters. Yay?
113 manager_->DOMContentLoaded(nullptr);
114 task_environment()->RunUntilIdle();
117 void WaitForBubbleToBeShown() {
118 manager_->DocumentOnLoadCompletedInPrimaryMainFrame();
119 task_environment()->RunUntilIdle();
122 void MockTabSwitchAway() {
123 manager_->OnVisibilityChanged(content::Visibility::HIDDEN);
126 void MockTabSwitchBack() {
127 manager_->OnVisibilityChanged(content::Visibility::VISIBLE);
130 virtual void NavigationEntryCommitted(
131 const content::LoadCommittedDetails& details) {
132 manager_->NavigationEntryCommitted(details);
135 std::unique_ptr<MockPermissionRequest> CreateAndAddRequest(
138 int expected_request_count) {
139 std::unique_ptr<MockPermissionRequest> request =
140 std::make_unique<MockPermissionRequest>(
141 type, PermissionRequestGestureType::GESTURE);
142 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), request.get());
143 WaitForBubbleToBeShown();
144 if (should_be_seen) {
145 EXPECT_TRUE(prompt_factory_->RequestTypeSeen(type));
147 EXPECT_FALSE(prompt_factory_->RequestTypeSeen(type));
149 EXPECT_EQ(prompt_factory_->TotalRequestCount(), expected_request_count);
154 void WaitAndAcceptPromptForRequest(MockPermissionRequest* request) {
155 WaitForBubbleToBeShown();
157 EXPECT_FALSE(request->finished());
158 EXPECT_TRUE(prompt_factory_->is_visible());
159 ASSERT_EQ(prompt_factory_->request_count(), 1);
162 EXPECT_TRUE(request->granted());
166 MockPermissionRequest request1_;
167 MockPermissionRequest request2_;
168 MockPermissionRequest request_mic_;
169 MockPermissionRequest request_camera_;
170 #if !BUILDFLAG(IS_ANDROID)
171 MockPermissionRequest request_ptz_;
173 MockPermissionRequest iframe_request_same_domain_;
174 MockPermissionRequest iframe_request_other_domain_;
175 MockPermissionRequest iframe_request_camera_other_domain_;
176 MockPermissionRequest iframe_request_mic_other_domain_;
177 raw_ptr<PermissionRequestManager, DanglingUntriaged> manager_;
178 std::unique_ptr<MockPermissionPromptFactory> prompt_factory_;
179 TestPermissionsClient client_;
180 base::test::ScopedFeatureList feature_list_;
183 ////////////////////////////////////////////////////////////////////////////////
185 ////////////////////////////////////////////////////////////////////////////////
187 TEST_F(PermissionRequestManagerTest, NoRequests) {
188 WaitForBubbleToBeShown();
189 EXPECT_FALSE(prompt_factory_->is_visible());
192 TEST_F(PermissionRequestManagerTest, SingleRequest) {
193 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
194 WaitForBubbleToBeShown();
196 EXPECT_TRUE(prompt_factory_->is_visible());
197 ASSERT_EQ(prompt_factory_->request_count(), 1);
200 EXPECT_TRUE(request1_.granted());
203 TEST_F(PermissionRequestManagerTest, SequentialRequests) {
204 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
205 WaitForBubbleToBeShown();
206 EXPECT_TRUE(prompt_factory_->is_visible());
209 EXPECT_TRUE(request1_.granted());
210 EXPECT_FALSE(prompt_factory_->is_visible());
212 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
213 WaitForBubbleToBeShown();
214 EXPECT_TRUE(prompt_factory_->is_visible());
216 EXPECT_FALSE(prompt_factory_->is_visible());
217 EXPECT_TRUE(request2_.granted());
220 TEST_F(PermissionRequestManagerTest, ForgetRequestsOnPageNavigation) {
221 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
222 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
223 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
224 &iframe_request_other_domain_);
225 WaitForBubbleToBeShown();
227 EXPECT_TRUE(prompt_factory_->is_visible());
228 ASSERT_EQ(prompt_factory_->request_count(), 1);
230 NavigateAndCommit(GURL("http://www2.google.com/"));
231 WaitForBubbleToBeShown();
233 EXPECT_FALSE(prompt_factory_->is_visible());
234 EXPECT_TRUE(request1_.finished());
235 EXPECT_TRUE(request2_.finished());
236 EXPECT_TRUE(iframe_request_other_domain_.finished());
239 TEST_F(PermissionRequestManagerTest, RequestsDontNeedUserGesture) {
241 WaitForBubbleToBeShown();
242 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
243 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
244 &iframe_request_other_domain_);
245 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
246 task_environment()->RunUntilIdle();
248 EXPECT_TRUE(prompt_factory_->is_visible());
251 TEST_F(PermissionRequestManagerTest, RequestsNotSupported) {
252 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
253 WaitForBubbleToBeShown();
255 EXPECT_TRUE(request1_.granted());
257 manager_->set_web_contents_supports_permission_requests(false);
259 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
260 EXPECT_TRUE(request2_.cancelled());
263 ////////////////////////////////////////////////////////////////////////////////
265 ////////////////////////////////////////////////////////////////////////////////
267 // Android is the only platform that does not support the permission chip.
268 #if BUILDFLAG(IS_ANDROID)
269 // Most requests should never be grouped.
270 // Grouping for chip feature is tested in ThreeRequestsStackOrderChip.
271 TEST_F(PermissionRequestManagerTest, TwoRequestsUngrouped) {
272 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
273 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
275 WaitForBubbleToBeShown();
276 EXPECT_TRUE(prompt_factory_->is_visible());
277 ASSERT_EQ(prompt_factory_->request_count(), 1);
279 EXPECT_TRUE(request1_.granted());
281 WaitForBubbleToBeShown();
282 EXPECT_TRUE(prompt_factory_->is_visible());
283 ASSERT_EQ(prompt_factory_->request_count(), 1);
285 EXPECT_TRUE(request2_.granted());
287 ASSERT_EQ(prompt_factory_->show_count(), 2);
290 // Tests for non-Android platforms which support the permission chip.
291 #else // BUILDFLAG(IS_ANDROID)
292 TEST_F(PermissionRequestManagerTest, ThreeRequestsStackOrderChip) {
293 // Test new permissions order, requests shouldn't be grouped.
294 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
295 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
296 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
297 WaitForBubbleToBeShown();
299 EXPECT_TRUE(prompt_factory_->is_visible());
300 EXPECT_EQ(prompt_factory_->request_count(), 1);
302 EXPECT_TRUE(request_mic_.granted());
303 EXPECT_FALSE(request2_.granted());
304 EXPECT_FALSE(request1_.granted());
305 WaitForBubbleToBeShown();
307 EXPECT_TRUE(prompt_factory_->is_visible());
308 EXPECT_EQ(prompt_factory_->request_count(), 1);
310 EXPECT_TRUE(request2_.granted());
311 EXPECT_FALSE(request1_.granted());
312 WaitForBubbleToBeShown();
314 EXPECT_TRUE(prompt_factory_->is_visible());
315 EXPECT_EQ(prompt_factory_->request_count(), 1);
317 EXPECT_TRUE(request1_.granted());
320 // Test new permissions order by adding requests one at a time.
321 TEST_F(PermissionRequestManagerTest, ThreeRequestsOneByOneStackOrderChip) {
322 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
323 WaitForBubbleToBeShown();
325 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
326 WaitForBubbleToBeShown();
328 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
329 WaitForBubbleToBeShown();
331 EXPECT_TRUE(prompt_factory_->is_visible());
332 EXPECT_EQ(prompt_factory_->request_count(), 1);
334 EXPECT_TRUE(request_mic_.granted());
335 EXPECT_FALSE(request2_.granted());
336 EXPECT_FALSE(request1_.granted());
337 WaitForBubbleToBeShown();
339 EXPECT_TRUE(prompt_factory_->is_visible());
340 EXPECT_EQ(prompt_factory_->request_count(), 1);
342 EXPECT_TRUE(request2_.granted());
343 EXPECT_FALSE(request1_.granted());
344 WaitForBubbleToBeShown();
346 EXPECT_TRUE(prompt_factory_->is_visible());
347 EXPECT_EQ(prompt_factory_->request_count(), 1);
349 EXPECT_TRUE(request1_.granted());
351 #endif // BUILDFLAG(IS_ANDROID)
353 // Only mic/camera requests from the same origin should be grouped.
354 TEST_F(PermissionRequestManagerTest, MicCameraGrouped) {
355 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
356 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
357 WaitForBubbleToBeShown();
359 EXPECT_TRUE(prompt_factory_->is_visible());
360 ASSERT_EQ(prompt_factory_->request_count(), 2);
363 EXPECT_TRUE(request_mic_.granted());
364 EXPECT_TRUE(request_camera_.granted());
367 // If mic/camera requests come from different origins, they should not be
369 TEST_F(PermissionRequestManagerTest, MicCameraDifferentOrigins) {
370 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
371 &iframe_request_mic_other_domain_);
372 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
373 WaitForBubbleToBeShown();
375 EXPECT_TRUE(prompt_factory_->is_visible());
376 ASSERT_EQ(prompt_factory_->request_count(), 1);
379 #if !BUILDFLAG(IS_ANDROID)
380 // Only camera/ptz requests from the same origin should be grouped.
381 TEST_F(PermissionRequestManagerTest, CameraPtzGrouped) {
382 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
383 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_ptz_);
384 WaitForBubbleToBeShown();
386 EXPECT_TRUE(prompt_factory_->is_visible());
387 ASSERT_EQ(prompt_factory_->request_count(), 2);
390 EXPECT_TRUE(request_camera_.granted());
391 EXPECT_TRUE(request_ptz_.granted());
394 TEST_F(PermissionRequestManagerTest, CameraPtzDifferentOrigins) {
395 // If camera/ptz requests come from different origins, they should not be
397 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
398 &iframe_request_camera_other_domain_);
399 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_ptz_);
400 WaitForBubbleToBeShown();
402 EXPECT_TRUE(prompt_factory_->is_visible());
403 ASSERT_EQ(prompt_factory_->request_count(), 1);
406 // Only mic/camera/ptz requests from the same origin should be grouped.
407 TEST_F(PermissionRequestManagerTest, MicCameraPtzGrouped) {
408 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
409 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
410 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_ptz_);
411 WaitForBubbleToBeShown();
413 EXPECT_TRUE(prompt_factory_->is_visible());
414 ASSERT_EQ(prompt_factory_->request_count(), 3);
417 EXPECT_TRUE(request_mic_.granted());
418 EXPECT_TRUE(request_camera_.granted());
419 EXPECT_TRUE(request_ptz_.granted());
422 // If mic/camera/ptz requests come from different origins, they should not be
424 TEST_F(PermissionRequestManagerTest, MicCameraPtzDifferentOrigins) {
425 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
426 &iframe_request_mic_other_domain_);
427 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
428 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_ptz_);
429 WaitForBubbleToBeShown();
431 // Requests should be split into two groups and each one will contain less
432 // than 3 requests (1 request + 2 request for current logic and 2 requests + 1
433 // request for chip).
434 EXPECT_TRUE(prompt_factory_->is_visible());
435 ASSERT_LT(prompt_factory_->request_count(), 3);
438 EXPECT_TRUE(prompt_factory_->is_visible());
439 ASSERT_LT(prompt_factory_->request_count(), 3);
441 #endif // !BUILDFLAG(IS_ANDROID)
443 // Tests mix of grouped media requests and non-groupable request.
444 TEST_F(PermissionRequestManagerTest, MixOfMediaAndNotMediaRequests) {
445 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
446 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
447 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
448 WaitForBubbleToBeShown();
450 // Requests should be split into two groups and each one will contain less
451 // than 3 requests (1 request + 2 request for current logic and 2 requests + 1
452 // request for chip).
453 EXPECT_TRUE(prompt_factory_->is_visible());
454 ASSERT_LT(prompt_factory_->request_count(), 3);
456 WaitForBubbleToBeShown();
458 EXPECT_TRUE(prompt_factory_->is_visible());
459 ASSERT_LT(prompt_factory_->request_count(), 3);
463 TEST_F(PermissionRequestManagerTest, OpenHelpCenterLink) {
464 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
465 &iframe_request_camera_other_domain_);
466 WaitForBubbleToBeShown();
467 EXPECT_TRUE(prompt_factory_->is_visible());
469 OpenHelpCenterLink();
473 TEST_F(PermissionRequestManagerTest, OpenHelpCenterLink_RequestNotSupported) {
474 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
475 WaitForBubbleToBeShown();
476 EXPECT_TRUE(prompt_factory_->is_visible());
478 EXPECT_DEATH_IF_SUPPORTED(OpenHelpCenterLink(), "");
481 ////////////////////////////////////////////////////////////////////////////////
483 ////////////////////////////////////////////////////////////////////////////////
485 #if BUILDFLAG(IS_ANDROID)
486 TEST_F(PermissionRequestManagerTest, TwoRequestsTabSwitch) {
487 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
488 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
489 WaitForBubbleToBeShown();
491 EXPECT_TRUE(prompt_factory_->is_visible());
492 ASSERT_EQ(prompt_factory_->request_count(), 2);
495 EXPECT_TRUE(prompt_factory_->is_visible());
498 WaitForBubbleToBeShown();
499 EXPECT_TRUE(prompt_factory_->is_visible());
500 ASSERT_EQ(prompt_factory_->request_count(), 2);
503 EXPECT_TRUE(request_mic_.granted());
504 EXPECT_TRUE(request_camera_.granted());
506 #endif // BUILDFLAG(IS_ANDROID)
508 TEST_F(PermissionRequestManagerTest, PermissionRequestWhileTabSwitchedAway) {
510 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
511 WaitForBubbleToBeShown();
512 EXPECT_FALSE(prompt_factory_->is_visible());
515 WaitForBubbleToBeShown();
516 EXPECT_TRUE(prompt_factory_->is_visible());
519 ////////////////////////////////////////////////////////////////////////////////
520 // Duplicated requests
521 ////////////////////////////////////////////////////////////////////////////////
523 TEST_F(PermissionRequestManagerTest, SameRequestRejected) {
524 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
525 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
526 EXPECT_FALSE(request1_.finished());
528 WaitForBubbleToBeShown();
529 EXPECT_TRUE(prompt_factory_->is_visible());
530 ASSERT_EQ(prompt_factory_->request_count(), 1);
532 task_environment()->RunUntilIdle();
533 EXPECT_TRUE(request1_.granted());
534 EXPECT_FALSE(prompt_factory_->is_visible());
537 TEST_F(PermissionRequestManagerTest, WeakDuplicateRequests) {
538 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
539 WaitForBubbleToBeShown();
540 auto dupe_request_1 = request1_.CreateDuplicateRequest();
541 auto dupe_request_2 = request1_.CreateDuplicateRequest();
542 auto dupe_request_3 = request1_.CreateDuplicateRequest();
543 auto dupe_request_4 = request1_.CreateDuplicateRequest();
544 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
545 dupe_request_1.get());
546 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
547 dupe_request_2.get());
548 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
549 dupe_request_3.get());
550 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
551 dupe_request_4.get());
552 dupe_request_1.reset();
553 dupe_request_3.reset();
554 EXPECT_EQ(4ul, manager_->duplicate_requests_.front().size());
555 EXPECT_EQ(1ul, manager_->duplicate_requests_.size());
556 auto request_list = manager_->FindDuplicateRequestList(&request1_);
557 EXPECT_NE(request_list, manager_->duplicate_requests_.end());
558 EXPECT_EQ(3ul, manager_->duplicate_requests_.front().size());
559 dupe_request_4.reset();
560 manager_->VisitDuplicateRequests(
562 [](const base::WeakPtr<PermissionRequest>& weak_request) {}),
564 EXPECT_EQ(1ul, manager_->duplicate_requests_.front().size());
565 EXPECT_EQ(1ul, manager_->duplicate_requests_.size());
567 EXPECT_EQ(0ul, manager_->duplicate_requests_.size());
570 class QuicklyDeletedRequest : public PermissionRequest {
572 QuicklyDeletedRequest(const GURL& requesting_origin,
573 RequestType request_type,
574 PermissionRequestGestureType gesture_type)
575 : PermissionRequest(requesting_origin,
577 gesture_type == PermissionRequestGestureType::GESTURE,
578 base::BindLambdaForTesting(
579 [](ContentSetting result,
581 bool is_final_decision) { NOTREACHED(); }),
582 base::NullCallback()) {}
584 static std::unique_ptr<QuicklyDeletedRequest> CreateRequest(
585 MockPermissionRequest* request) {
586 return std::make_unique<QuicklyDeletedRequest>(request->requesting_origin(),
587 request->request_type(),
588 request->GetGestureType());
592 TEST_F(PermissionRequestManagerTest, WeakDuplicateRequestsAccept) {
593 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
594 WaitForBubbleToBeShown();
595 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
596 auto dupe_request_1 = QuicklyDeletedRequest::CreateRequest(&request1_);
597 auto dupe_request_2 = request1_.CreateDuplicateRequest();
598 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
599 dupe_request_1.get());
600 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
601 dupe_request_2.get());
602 auto dupe_request_3 = QuicklyDeletedRequest::CreateRequest(&request2_);
603 auto dupe_request_4 = request2_.CreateDuplicateRequest();
604 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
605 dupe_request_3.get());
606 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
607 dupe_request_4.get());
608 dupe_request_1.reset();
609 dupe_request_3.reset();
610 EXPECT_EQ(2ul, manager_->duplicate_requests_.size());
611 EXPECT_EQ(2ul, manager_->duplicate_requests_.front().size());
612 EXPECT_EQ(2ul, manager_->duplicate_requests_.back().size());
613 WaitForBubbleToBeShown();
615 EXPECT_EQ(1ul, manager_->duplicate_requests_.size());
616 WaitForBubbleToBeShown();
618 EXPECT_EQ(0ul, manager_->duplicate_requests_.size());
621 TEST_F(PermissionRequestManagerTest, DuplicateRequest) {
622 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
623 WaitForBubbleToBeShown();
624 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
626 auto dupe_request = request1_.CreateDuplicateRequest();
627 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
629 EXPECT_FALSE(dupe_request->finished());
630 EXPECT_FALSE(request1_.finished());
632 auto dupe_request2 = request2_.CreateDuplicateRequest();
633 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
634 dupe_request2.get());
635 EXPECT_FALSE(dupe_request2->finished());
636 EXPECT_FALSE(request2_.finished());
638 WaitForBubbleToBeShown();
640 if (PermissionUtil::DoesPlatformSupportChip()) {
641 EXPECT_TRUE(dupe_request2->finished());
642 EXPECT_TRUE(request2_.finished());
644 EXPECT_TRUE(dupe_request->finished());
645 EXPECT_TRUE(request1_.finished());
648 WaitForBubbleToBeShown();
650 if (PermissionUtil::DoesPlatformSupportChip()) {
651 EXPECT_TRUE(dupe_request->finished());
652 EXPECT_TRUE(request1_.finished());
654 EXPECT_TRUE(dupe_request2->finished());
655 EXPECT_TRUE(request2_.finished());
659 ////////////////////////////////////////////////////////////////////////////////
660 // Requests from iframes
661 ////////////////////////////////////////////////////////////////////////////////
663 TEST_F(PermissionRequestManagerTest, MainFrameNoRequestIFrameRequest) {
664 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
665 &iframe_request_same_domain_);
666 WaitForBubbleToBeShown();
669 EXPECT_TRUE(prompt_factory_->is_visible());
671 EXPECT_TRUE(iframe_request_same_domain_.finished());
674 TEST_F(PermissionRequestManagerTest, MainFrameAndIFrameRequestSameDomain) {
675 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
676 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
677 &iframe_request_same_domain_);
679 WaitForBubbleToBeShown();
681 EXPECT_TRUE(prompt_factory_->is_visible());
682 ASSERT_EQ(1, prompt_factory_->request_count());
684 if (PermissionUtil::DoesPlatformSupportChip()) {
685 EXPECT_TRUE(iframe_request_same_domain_.finished());
686 EXPECT_FALSE(request1_.finished());
688 EXPECT_TRUE(request1_.finished());
689 EXPECT_FALSE(iframe_request_same_domain_.finished());
692 WaitForBubbleToBeShown();
693 EXPECT_TRUE(prompt_factory_->is_visible());
694 ASSERT_EQ(1, prompt_factory_->request_count());
697 EXPECT_FALSE(prompt_factory_->is_visible());
698 if (PermissionUtil::DoesPlatformSupportChip()) {
699 EXPECT_TRUE(request1_.finished());
701 EXPECT_TRUE(iframe_request_same_domain_.finished());
705 TEST_F(PermissionRequestManagerTest, MainFrameAndIFrameRequestOtherDomain) {
706 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
707 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
708 &iframe_request_other_domain_);
710 WaitForBubbleToBeShown();
712 EXPECT_TRUE(prompt_factory_->is_visible());
714 if (PermissionUtil::DoesPlatformSupportChip()) {
715 EXPECT_TRUE(iframe_request_other_domain_.finished());
716 EXPECT_FALSE(request1_.finished());
718 EXPECT_TRUE(request1_.finished());
719 EXPECT_FALSE(iframe_request_other_domain_.finished());
722 EXPECT_TRUE(prompt_factory_->is_visible());
724 EXPECT_TRUE(iframe_request_other_domain_.finished());
725 if (PermissionUtil::DoesPlatformSupportChip()) {
726 EXPECT_TRUE(request1_.finished());
728 EXPECT_TRUE(iframe_request_other_domain_.finished());
732 TEST_F(PermissionRequestManagerTest, IFrameRequestWhenMainRequestVisible) {
733 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
734 WaitForBubbleToBeShown();
735 EXPECT_TRUE(prompt_factory_->is_visible());
737 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
738 &iframe_request_same_domain_);
740 ASSERT_EQ(prompt_factory_->request_count(), 1);
742 if (PermissionUtil::DoesPlatformSupportChip()) {
743 EXPECT_TRUE(iframe_request_same_domain_.finished());
744 EXPECT_FALSE(request1_.finished());
746 EXPECT_TRUE(request1_.finished());
747 EXPECT_FALSE(iframe_request_same_domain_.finished());
750 EXPECT_TRUE(prompt_factory_->is_visible());
751 ASSERT_EQ(prompt_factory_->request_count(), 1);
753 EXPECT_TRUE(iframe_request_same_domain_.finished());
754 if (PermissionUtil::DoesPlatformSupportChip()) {
755 EXPECT_TRUE(request1_.finished());
757 EXPECT_TRUE(iframe_request_same_domain_.finished());
761 TEST_F(PermissionRequestManagerTest,
762 IFrameRequestOtherDomainWhenMainRequestVisible) {
763 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
764 WaitForBubbleToBeShown();
765 EXPECT_TRUE(prompt_factory_->is_visible());
767 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
768 &iframe_request_other_domain_);
771 if (PermissionUtil::DoesPlatformSupportChip()) {
772 EXPECT_TRUE(iframe_request_other_domain_.finished());
773 EXPECT_FALSE(request1_.finished());
775 EXPECT_TRUE(request1_.finished());
776 EXPECT_FALSE(iframe_request_other_domain_.finished());
779 EXPECT_TRUE(prompt_factory_->is_visible());
781 if (PermissionUtil::DoesPlatformSupportChip()) {
782 EXPECT_TRUE(request1_.finished());
784 EXPECT_TRUE(iframe_request_other_domain_.finished());
788 ////////////////////////////////////////////////////////////////////////////////
790 ////////////////////////////////////////////////////////////////////////////////
792 // This code path (calling Accept on a non-merged bubble, with no accepted
793 // permission) would never be used in actual Chrome, but its still tested for
795 TEST_F(PermissionRequestManagerTest, UMAForSimpleDeniedBubbleAlternatePath) {
796 base::HistogramTester histograms;
798 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
799 WaitForBubbleToBeShown();
800 // No need to test UMA for showing prompts again, they were tested in
801 // UMAForSimpleAcceptedBubble.
804 histograms.ExpectUniqueSample(PermissionUmaUtil::kPermissionsPromptDenied,
805 static_cast<base::HistogramBase::Sample>(
806 RequestTypeForUma::PERMISSION_GEOLOCATION),
810 TEST_F(PermissionRequestManagerTest, UMAForTabSwitching) {
811 base::HistogramTester histograms;
813 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
814 WaitForBubbleToBeShown();
815 histograms.ExpectUniqueSample(PermissionUmaUtil::kPermissionsPromptShown,
816 static_cast<base::HistogramBase::Sample>(
817 RequestTypeForUma::PERMISSION_GEOLOCATION),
822 histograms.ExpectUniqueSample(PermissionUmaUtil::kPermissionsPromptShown,
823 static_cast<base::HistogramBase::Sample>(
824 RequestTypeForUma::PERMISSION_GEOLOCATION),
828 ////////////////////////////////////////////////////////////////////////////////
830 ////////////////////////////////////////////////////////////////////////////////
832 // Simulate a PermissionUiSelector that simply returns a predefined |ui_to_use|
834 class MockNotificationPermissionUiSelector : public PermissionUiSelector {
836 explicit MockNotificationPermissionUiSelector(
837 absl::optional<QuietUiReason> quiet_ui_reason,
838 absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
839 prediction_likelihood,
840 absl::optional<base::TimeDelta> async_delay)
841 : quiet_ui_reason_(quiet_ui_reason),
842 prediction_likelihood_(prediction_likelihood),
843 async_delay_(async_delay) {}
845 void SelectUiToUse(PermissionRequest* request,
846 DecisionMadeCallback callback) override {
847 selected_ui_to_use_ = true;
848 Decision decision(quiet_ui_reason_, Decision::ShowNoWarning());
850 base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
851 FROM_HERE, base::BindOnce(std::move(callback), decision),
852 async_delay_.value());
854 std::move(callback).Run(decision);
858 bool IsPermissionRequestSupported(RequestType request_type) override {
859 return request_type == RequestType::kNotifications ||
860 request_type == RequestType::kGeolocation;
863 absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
864 PredictedGrantLikelihoodForUKM() override {
865 return prediction_likelihood_;
868 static void CreateForManager(
869 PermissionRequestManager* manager,
870 absl::optional<QuietUiReason> quiet_ui_reason,
871 absl::optional<base::TimeDelta> async_delay,
872 absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
873 prediction_likelihood = absl::nullopt) {
874 manager->add_permission_ui_selector_for_testing(
875 std::make_unique<MockNotificationPermissionUiSelector>(
876 quiet_ui_reason, prediction_likelihood, async_delay));
879 bool selected_ui_to_use() const { return selected_ui_to_use_; }
882 absl::optional<QuietUiReason> quiet_ui_reason_;
883 absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
884 prediction_likelihood_;
885 absl::optional<base::TimeDelta> async_delay_;
886 bool selected_ui_to_use_ = false;
889 // Same as the MockNotificationPermissionUiSelector but handling only the
890 // Camera stream request type
891 class MockCameraStreamPermissionUiSelector
892 : public MockNotificationPermissionUiSelector {
894 explicit MockCameraStreamPermissionUiSelector(
895 absl::optional<QuietUiReason> quiet_ui_reason,
896 absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
897 prediction_likelihood,
898 absl::optional<base::TimeDelta> async_delay)
899 : MockNotificationPermissionUiSelector(quiet_ui_reason,
900 prediction_likelihood,
903 bool IsPermissionRequestSupported(RequestType request_type) override {
904 return request_type == RequestType::kCameraStream;
907 static void CreateForManager(
908 PermissionRequestManager* manager,
909 absl::optional<QuietUiReason> quiet_ui_reason,
910 absl::optional<base::TimeDelta> async_delay,
911 absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
912 prediction_likelihood = absl::nullopt) {
913 manager->add_permission_ui_selector_for_testing(
914 std::make_unique<MockCameraStreamPermissionUiSelector>(
915 quiet_ui_reason, prediction_likelihood, async_delay));
919 TEST_F(PermissionRequestManagerTest,
920 UiSelectorNotUsedForPermissionsOtherThanNotification) {
921 manager_->clear_permission_ui_selector_for_testing();
922 MockNotificationPermissionUiSelector::CreateForManager(
923 manager_, PermissionUiSelector::QuietUiReason::kEnabledInPrefs,
924 absl::nullopt /* async_delay */);
926 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
927 WaitForBubbleToBeShown();
929 ASSERT_TRUE(prompt_factory_->is_visible());
930 ASSERT_TRUE(prompt_factory_->RequestTypeSeen(request_camera_.request_type()));
931 EXPECT_FALSE(manager_->ShouldCurrentRequestUseQuietUI());
934 EXPECT_TRUE(request_camera_.granted());
937 TEST_F(PermissionRequestManagerTest, UiSelectorUsedForNotifications) {
939 absl::optional<PermissionUiSelector::QuietUiReason> quiet_ui_reason;
940 absl::optional<base::TimeDelta> async_delay;
942 {QuietUiReason::kEnabledInPrefs, absl::make_optional<base::TimeDelta>()},
943 {PermissionUiSelector::Decision::UseNormalUi(),
944 absl::make_optional<base::TimeDelta>()},
945 {QuietUiReason::kEnabledInPrefs, absl::nullopt},
946 {PermissionUiSelector::Decision::UseNormalUi(), absl::nullopt},
949 for (const auto& test : kTests) {
950 manager_->clear_permission_ui_selector_for_testing();
951 MockNotificationPermissionUiSelector::CreateForManager(
952 manager_, test.quiet_ui_reason, test.async_delay);
954 MockPermissionRequest request(RequestType::kNotifications,
955 PermissionRequestGestureType::GESTURE);
957 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
958 WaitForBubbleToBeShown();
960 EXPECT_TRUE(prompt_factory_->is_visible());
961 EXPECT_TRUE(prompt_factory_->RequestTypeSeen(request.request_type()));
962 EXPECT_EQ(!!test.quiet_ui_reason,
963 manager_->ShouldCurrentRequestUseQuietUI());
966 EXPECT_TRUE(request.granted());
970 TEST_F(PermissionRequestManagerTest,
971 UiSelectionHappensSeparatelyForEachRequest) {
972 manager_->clear_permission_ui_selector_for_testing();
973 MockNotificationPermissionUiSelector::CreateForManager(
974 manager_, QuietUiReason::kEnabledInPrefs,
975 absl::make_optional<base::TimeDelta>());
976 MockPermissionRequest request1(RequestType::kNotifications,
977 PermissionRequestGestureType::GESTURE);
978 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1);
979 WaitForBubbleToBeShown();
980 EXPECT_TRUE(manager_->ShouldCurrentRequestUseQuietUI());
983 MockPermissionRequest request2(RequestType::kNotifications,
984 PermissionRequestGestureType::GESTURE);
985 manager_->clear_permission_ui_selector_for_testing();
986 MockNotificationPermissionUiSelector::CreateForManager(
987 manager_, PermissionUiSelector::Decision::UseNormalUi(),
988 absl::make_optional<base::TimeDelta>());
989 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2);
990 WaitForBubbleToBeShown();
991 EXPECT_FALSE(manager_->ShouldCurrentRequestUseQuietUI());
995 TEST_F(PermissionRequestManagerTest, SkipNextUiSelector) {
996 manager_->clear_permission_ui_selector_for_testing();
997 MockNotificationPermissionUiSelector::CreateForManager(
998 manager_, QuietUiReason::kEnabledInPrefs,
999 /* async_delay */ absl::nullopt);
1000 MockNotificationPermissionUiSelector::CreateForManager(
1001 manager_, PermissionUiSelector::Decision::UseNormalUi(),
1002 /* async_delay */ absl::nullopt);
1003 MockPermissionRequest request1(RequestType::kNotifications,
1004 PermissionRequestGestureType::GESTURE);
1005 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1);
1006 WaitForBubbleToBeShown();
1007 auto* next_selector =
1008 manager_->get_permission_ui_selectors_for_testing().back().get();
1009 EXPECT_FALSE(static_cast<MockNotificationPermissionUiSelector*>(next_selector)
1010 ->selected_ui_to_use());
1011 EXPECT_TRUE(manager_->ShouldCurrentRequestUseQuietUI());
1015 TEST_F(PermissionRequestManagerTest, MultipleUiSelectors) {
1017 std::vector<absl::optional<QuietUiReason>> quiet_ui_reasons;
1018 std::vector<bool> simulate_delayed_decision;
1019 absl::optional<QuietUiReason> expected_reason;
1021 // Simple sync selectors, first one should take priority.
1022 {{QuietUiReason::kTriggeredByCrowdDeny, QuietUiReason::kEnabledInPrefs},
1024 QuietUiReason::kTriggeredByCrowdDeny},
1025 {{QuietUiReason::kTriggeredDueToDisruptiveBehavior,
1026 QuietUiReason::kEnabledInPrefs},
1028 QuietUiReason::kTriggeredDueToDisruptiveBehavior},
1029 {{QuietUiReason::kTriggeredDueToDisruptiveBehavior,
1030 QuietUiReason::kServicePredictedVeryUnlikelyGrant},
1032 QuietUiReason::kTriggeredDueToDisruptiveBehavior},
1033 {{QuietUiReason::kTriggeredDueToDisruptiveBehavior,
1034 QuietUiReason::kTriggeredByCrowdDeny},
1036 QuietUiReason::kTriggeredDueToDisruptiveBehavior},
1037 // First selector is async but should still take priority even if it
1039 {{QuietUiReason::kTriggeredByCrowdDeny, QuietUiReason::kEnabledInPrefs},
1041 QuietUiReason::kTriggeredByCrowdDeny},
1042 {{QuietUiReason::kTriggeredDueToDisruptiveBehavior,
1043 QuietUiReason::kEnabledInPrefs},
1045 QuietUiReason::kTriggeredDueToDisruptiveBehavior},
1046 // The first selector that has a quiet ui decision should be used.
1047 {{absl::nullopt, absl::nullopt,
1048 QuietUiReason::kTriggeredDueToAbusiveContent,
1049 QuietUiReason::kEnabledInPrefs},
1050 {false, true, true, false},
1051 QuietUiReason::kTriggeredDueToAbusiveContent},
1052 // If all selectors return a normal ui, it should use a normal ui.
1053 {{absl::nullopt, absl::nullopt}, {false, true}, absl::nullopt},
1055 // Use a bunch of selectors both async and sync.
1056 {{absl::nullopt, absl::nullopt, absl::nullopt, absl::nullopt,
1057 absl::nullopt, QuietUiReason::kTriggeredDueToAbusiveRequests,
1058 absl::nullopt, QuietUiReason::kEnabledInPrefs},
1059 {false, true, false, true, true, true, false, false},
1060 QuietUiReason::kTriggeredDueToAbusiveRequests},
1061 // Use a bunch of selectors all sync.
1062 {{absl::nullopt, absl::nullopt, absl::nullopt, absl::nullopt,
1063 absl::nullopt, QuietUiReason::kTriggeredDueToAbusiveRequests,
1064 absl::nullopt, QuietUiReason::kEnabledInPrefs},
1065 {false, false, false, false, false, false, false, false},
1066 QuietUiReason::kTriggeredDueToAbusiveRequests},
1067 // Use a bunch of selectors all async.
1068 {{absl::nullopt, absl::nullopt, absl::nullopt, absl::nullopt,
1069 absl::nullopt, QuietUiReason::kTriggeredDueToAbusiveRequests,
1070 absl::nullopt, QuietUiReason::kEnabledInPrefs},
1071 {true, true, true, true, true, true, true, true},
1072 QuietUiReason::kTriggeredDueToAbusiveRequests},
1073 // Use a bunch of selectors both async and sync.
1074 {{absl::nullopt, absl::nullopt, absl::nullopt, absl::nullopt,
1075 absl::nullopt, QuietUiReason::kTriggeredDueToDisruptiveBehavior,
1076 absl::nullopt, QuietUiReason::kEnabledInPrefs},
1077 {true, false, false, true, true, true, false, false},
1078 QuietUiReason::kTriggeredDueToDisruptiveBehavior},
1081 for (const auto& test : kTests) {
1082 manager_->clear_permission_ui_selector_for_testing();
1083 for (size_t i = 0; i < test.quiet_ui_reasons.size(); ++i) {
1084 MockNotificationPermissionUiSelector::CreateForManager(
1085 manager_, test.quiet_ui_reasons[i],
1086 test.simulate_delayed_decision[i]
1087 ? absl::make_optional<base::TimeDelta>()
1091 MockPermissionRequest request(RequestType::kNotifications,
1092 PermissionRequestGestureType::GESTURE);
1094 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1095 WaitForBubbleToBeShown();
1097 EXPECT_TRUE(prompt_factory_->is_visible());
1098 EXPECT_TRUE(prompt_factory_->RequestTypeSeen(request.request_type()));
1099 if (test.expected_reason.has_value()) {
1100 EXPECT_EQ(test.expected_reason, manager_->ReasonForUsingQuietUi());
1102 EXPECT_FALSE(manager_->ShouldCurrentRequestUseQuietUI());
1106 EXPECT_TRUE(request.granted());
1110 TEST_F(PermissionRequestManagerTest, SelectorsPredictionLikelihood) {
1111 using PredictionLikelihood = PermissionUmaUtil::PredictionGrantLikelihood;
1112 const auto VeryLikely = PredictionLikelihood::
1113 PermissionPrediction_Likelihood_DiscretizedLikelihood_VERY_LIKELY;
1114 const auto Neutral = PredictionLikelihood::
1115 PermissionPrediction_Likelihood_DiscretizedLikelihood_NEUTRAL;
1118 std::vector<bool> enable_quiet_uis;
1119 std::vector<absl::optional<PredictionLikelihood>> prediction_likelihoods;
1120 absl::optional<PredictionLikelihood> expected_prediction_likelihood;
1122 // Sanity check: prediction likelihood is populated correctly.
1123 {{true}, {VeryLikely}, VeryLikely},
1124 {{false}, {Neutral}, Neutral},
1126 // Prediction likelihood is populated only if the selector was considered.
1127 {{true, true}, {absl::nullopt, VeryLikely}, absl::nullopt},
1128 {{false, true}, {absl::nullopt, VeryLikely}, VeryLikely},
1129 {{false, false}, {absl::nullopt, VeryLikely}, VeryLikely},
1131 // First considered selector is preserved.
1132 {{true, true}, {Neutral, VeryLikely}, Neutral},
1133 {{false, true}, {Neutral, VeryLikely}, Neutral},
1134 {{false, false}, {Neutral, VeryLikely}, Neutral},
1137 for (const auto& test : kTests) {
1138 manager_->clear_permission_ui_selector_for_testing();
1139 for (size_t i = 0; i < test.enable_quiet_uis.size(); ++i) {
1140 MockNotificationPermissionUiSelector::CreateForManager(
1142 test.enable_quiet_uis[i]
1143 ? absl::optional<QuietUiReason>(QuietUiReason::kEnabledInPrefs)
1145 absl::nullopt /* async_delay */, test.prediction_likelihoods[i]);
1148 MockPermissionRequest request(RequestType::kNotifications,
1149 PermissionRequestGestureType::GESTURE);
1151 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1152 WaitForBubbleToBeShown();
1154 EXPECT_TRUE(prompt_factory_->is_visible());
1155 EXPECT_TRUE(prompt_factory_->RequestTypeSeen(request.request_type()));
1156 EXPECT_EQ(test.expected_prediction_likelihood,
1157 manager_->prediction_grant_likelihood_for_testing());
1160 EXPECT_TRUE(request.granted());
1164 TEST_F(PermissionRequestManagerTest, SelectorRequestTypes) {
1166 RequestType request_type;
1167 bool should_request_use_quiet_ui;
1169 {RequestType::kNotifications, true},
1170 {RequestType::kGeolocation, true},
1171 {RequestType::kCameraStream, false},
1173 manager_->clear_permission_ui_selector_for_testing();
1174 MockNotificationPermissionUiSelector::CreateForManager(
1175 manager_, QuietUiReason::kEnabledInPrefs,
1176 absl::make_optional<base::TimeDelta>());
1177 for (const auto& test : kTests) {
1178 MockPermissionRequest request(test.request_type,
1179 PermissionRequestGestureType::GESTURE);
1180 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1181 WaitForBubbleToBeShown();
1182 EXPECT_EQ(test.should_request_use_quiet_ui,
1183 manager_->ShouldCurrentRequestUseQuietUI());
1186 // Adding a mock PermissionUiSelector that handles Camera stream.
1187 MockCameraStreamPermissionUiSelector::CreateForManager(
1188 manager_, QuietUiReason::kEnabledInPrefs,
1189 absl::make_optional<base::TimeDelta>());
1190 // Now the RequestType::kCameraStream should show a quiet UI as well
1191 MockPermissionRequest request2(RequestType::kCameraStream,
1192 PermissionRequestGestureType::GESTURE);
1193 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2);
1194 WaitForBubbleToBeShown();
1195 EXPECT_TRUE(manager_->ShouldCurrentRequestUseQuietUI());
1199 ////////////////////////////////////////////////////////////////////////////////
1200 // Quiet UI chip. Low priority for Notifications & Geolocation.
1201 ////////////////////////////////////////////////////////////////////////////////
1203 TEST_F(PermissionRequestManagerTest, NotificationsSingleBubbleAndChipRequest) {
1204 MockPermissionRequest request(RequestType::kNotifications,
1205 PermissionRequestGestureType::GESTURE);
1207 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1208 WaitForBubbleToBeShown();
1210 EXPECT_TRUE(prompt_factory_->is_visible());
1211 ASSERT_EQ(prompt_factory_->request_count(), 1);
1214 EXPECT_TRUE(request.granted());
1215 EXPECT_EQ(prompt_factory_->show_count(), 1);
1218 // Android is the only platform that does not support the permission chip.
1219 #if BUILDFLAG(IS_ANDROID)
1220 // Quiet UI feature is disabled. Chip is disabled. No low priority requests, the
1221 // first request is always shown.
1223 // Permissions requested in order:
1224 // 1. Notification (non abusive)
1228 // Prompt display order:
1229 // 1. Notification request shown
1230 // 2. Geolocation request shown
1231 // 3. Camera request shown
1232 TEST_F(PermissionRequestManagerTest,
1233 NotificationsGeolocationCameraBubbleRequest) {
1234 std::unique_ptr<MockPermissionRequest> request_notifications =
1235 CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
1237 std::unique_ptr<MockPermissionRequest> request_geolocation =
1238 CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/false,
1240 std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1241 RequestType::kCameraStream, /*should_be_seen=*/false, 1);
1243 for (auto* kRequest : {request_notifications.get(), request_geolocation.get(),
1244 request_camera.get()}) {
1245 WaitAndAcceptPromptForRequest(kRequest);
1248 EXPECT_EQ(prompt_factory_->show_count(), 3);
1251 // Tests for non-Android platforms which support the permission chip.
1252 #else // BUILDFLAG(IS_ANDROID)
1253 // Quiet UI feature is disabled, no low priority requests, the last request is
1256 // Permissions requested in order:
1261 // Prompt display order:
1262 // 1. Camera request shown but is preempted
1263 // 2. Clipboard request shown but is preempted
1264 // 3. MIDI request shown
1265 // 4. Clipboard request shown again
1266 // 5. Camera request shown again
1267 TEST_F(PermissionRequestManagerTest,
1268 CameraNotificationsGeolocationChipRequest) {
1269 std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1270 RequestType::kCameraStream, /*should_be_seen=*/true, 1);
1271 std::unique_ptr<MockPermissionRequest> request_clipboard =
1272 CreateAndAddRequest(RequestType::kClipboard, /*should_be_seen=*/true, 2);
1273 std::unique_ptr<MockPermissionRequest> request_midi =
1274 CreateAndAddRequest(RequestType::kMidi, /*should_be_seen=*/true, 3);
1276 for (auto* kRequest :
1277 {request_midi.get(), request_clipboard.get(), request_camera.get()}) {
1278 WaitAndAcceptPromptForRequest(kRequest);
1281 EXPECT_EQ(prompt_factory_->show_count(), 5);
1284 // Verifies order of simultaneous requests, with quiet chip enabled.
1285 // Simultaneous new requests are coming while we are waiting for UI selector
1288 // Permissions requested in order:
1289 // 1. Geolocation, UI selector takes 2 seconds to decide.
1290 // 2. Notification then mic. Notification will preempt geolocation
1292 // Prompt display order:
1296 TEST_F(PermissionRequestManagerTest, NewHighPriorityRequestDuringUIDecision) {
1297 manager_->clear_permission_ui_selector_for_testing();
1298 MockNotificationPermissionUiSelector::CreateForManager(
1299 manager_, QuietUiReason::kTriggeredDueToAbusiveRequests,
1300 absl::make_optional<base::TimeDelta>(base::Seconds(2)));
1301 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1303 task_environment()->FastForwardBy(base::Seconds(1));
1305 MockPermissionRequest request(RequestType::kClipboard,
1306 PermissionRequestGestureType::GESTURE);
1307 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1308 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1309 WaitForBubbleToBeShown();
1310 manager_->clear_permission_ui_selector_for_testing();
1312 EXPECT_TRUE(prompt_factory_->is_visible());
1313 EXPECT_EQ(prompt_factory_->request_count(), 1);
1315 EXPECT_TRUE(request_mic_.granted());
1316 EXPECT_FALSE(request.granted());
1317 EXPECT_FALSE(request1_.granted());
1318 WaitForBubbleToBeShown();
1320 EXPECT_TRUE(prompt_factory_->is_visible());
1321 EXPECT_EQ(prompt_factory_->request_count(), 1);
1323 EXPECT_TRUE(request.granted());
1324 EXPECT_FALSE(request1_.granted());
1325 WaitForBubbleToBeShown();
1327 EXPECT_TRUE(prompt_factory_->is_visible());
1328 EXPECT_EQ(prompt_factory_->request_count(), 1);
1330 EXPECT_TRUE(request1_.granted());
1333 // Verifies that the quiet UI chip is not ignored if another request came in
1334 // less than 8.5 seconds after.
1335 // Permissions requested in order:
1336 // 1. Notification (abusive)
1337 // 2. After less than 8.5 seconds Geolocation
1339 // Prompt display order:
1340 // 1. Notifications request shown but is preempted because of quiet UI.
1341 // 2. Geolocation request shown
1342 // 3. Notifications request shown again
1343 TEST_F(PermissionRequestManagerTest,
1344 AbusiveNotificationsGeolocationQuietUIChipRequest) {
1345 MockNotificationPermissionUiSelector::CreateForManager(
1347 PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1348 absl::nullopt /* async_delay */);
1350 std::unique_ptr<MockPermissionRequest> request_notifications =
1351 CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
1354 // Less then 8.5 seconds.
1355 manager_->set_current_request_first_display_time_for_testing(
1356 base::Time::Now() - base::Milliseconds(5000));
1358 std::unique_ptr<MockPermissionRequest> request_geolocation =
1359 CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
1362 WaitAndAcceptPromptForRequest(request_geolocation.get());
1363 WaitAndAcceptPromptForRequest(request_notifications.get());
1365 EXPECT_EQ(prompt_factory_->show_count(), 3);
1368 // Verifies that the quiet UI chip is ignored if another request came in more
1369 // than 8.5 seconds after.
1371 // Permissions requested in order:
1372 // 1. Notification (abusive)
1373 // 2. After more than 8.5 seconds Geolocation
1375 // Prompt display order:
1376 // 1. Notifications request shown but is preempted because of quiet UI.
1377 // 2. Geolocation request shown
1378 TEST_F(PermissionRequestManagerTest, AbusiveNotificationsShownLongEnough) {
1379 MockNotificationPermissionUiSelector::CreateForManager(
1381 PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1382 absl::nullopt /* async_delay */);
1384 std::unique_ptr<MockPermissionRequest> request_notifications =
1385 CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
1388 // More then 8.5 seconds.
1389 manager_->set_current_request_first_display_time_for_testing(
1390 base::Time::Now() - base::Milliseconds(9000));
1392 std::unique_ptr<MockPermissionRequest> request_geolocation =
1393 CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
1396 // The second permission was requested after 8.5 second window, the quiet UI
1397 // Notifiations request for an abusive origin is automatically ignored.
1398 EXPECT_FALSE(request_notifications->granted());
1399 EXPECT_TRUE(request_notifications->finished());
1401 WaitAndAcceptPromptForRequest(request_geolocation.get());
1403 EXPECT_EQ(prompt_factory_->show_count(), 2);
1406 // Verifies that the quiet UI chip is not ignored if another request came in
1407 // more than 8.5 seconds after. Verify different requests priority. Camera
1408 // request is shown despite being requested last.
1410 // Permissions requested in order:
1411 // 1. Notification (abusive)
1412 // 2. After less than 8.5 seconds Geolocation
1415 // Prompt display order:
1416 // 1. Notifications request shown but is preempted because of quiet UI.
1417 // 2. Geolocation request shown but is preempted because of low priority.
1418 // 3. Camera request shown
1419 // 4. Geolocation request shown again
1420 // 5. Notifications quiet UI request shown again
1421 TEST_F(PermissionRequestManagerTest,
1422 AbusiveNotificationsShownLongEnoughCamera) {
1423 MockNotificationPermissionUiSelector::CreateForManager(
1425 PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1426 absl::nullopt /* async_delay */);
1428 std::unique_ptr<MockPermissionRequest> request_notifications =
1429 CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
1431 // Less then 8.5 seconds.
1432 manager_->set_current_request_first_display_time_for_testing(
1433 base::Time::Now() - base::Milliseconds(5000));
1435 std::unique_ptr<MockPermissionRequest> request_geolocation =
1436 CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
1438 std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1439 RequestType::kCameraStream, /*should_be_seen=*/true, 3);
1441 // The second permission was requested in 8.5 second window, the quiet UI
1442 // Notifiations request for an abusive origin is not automatically ignored.
1443 EXPECT_FALSE(request_notifications->granted());
1444 EXPECT_FALSE(request_notifications->finished());
1446 for (auto* kRequest : {request_camera.get(), request_geolocation.get(),
1447 request_notifications.get()}) {
1448 WaitAndAcceptPromptForRequest(kRequest);
1451 EXPECT_EQ(prompt_factory_->show_count(), 5);
1454 // Verifies that the quiet UI chip is not ignored if another request came in
1455 // more than 8.5 seconds after. Verify different requests priority. Camera
1456 // request is not preemted.
1458 // Permissions requested in order:
1460 // 2. Notification (abusive)
1461 // 3. After less than 8.5 seconds Geolocation
1463 // Prompt display order:
1464 // 1. Camera request shown
1465 // 2. Geolocation request shown
1466 // 3. Camera request shown
1467 TEST_F(PermissionRequestManagerTest, CameraAbusiveNotificationsGeolocation) {
1468 MockNotificationPermissionUiSelector::CreateForManager(
1470 PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1471 absl::nullopt /* async_delay */);
1473 std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1474 RequestType::kCameraStream, /*should_be_seen=*/true, 1);
1476 // Quiet UI is not shown because Camera has higher priority.
1477 std::unique_ptr<MockPermissionRequest> request_notifications =
1478 CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/false,
1480 // Less then 8.5 seconds.
1481 manager_->set_current_request_first_display_time_for_testing(
1482 base::Time::Now() - base::Milliseconds(5000));
1484 // Geolocation is not shown because Camera has higher priority.
1485 std::unique_ptr<MockPermissionRequest> request_geolocation =
1486 CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/false,
1489 // The second permission after quiet UI was requested in 8.5 second window,
1490 // the quiet UI Notifiations request for an abusive origin is not
1491 // automatically ignored.
1492 EXPECT_FALSE(request_notifications->granted());
1493 EXPECT_FALSE(request_notifications->finished());
1495 for (auto* kRequest : {request_camera.get(), request_geolocation.get(),
1496 request_notifications.get()}) {
1497 WaitAndAcceptPromptForRequest(kRequest);
1500 EXPECT_EQ(prompt_factory_->show_count(), 3);
1503 // Verifies that the quiet UI chip is not ignored if another request came in
1504 // more than 8.5 seconds after. Verify different requests priority. Camera
1505 // request is not preemted.
1507 // Permissions requested in order:
1509 // 2. Notification (abusive)
1510 // 3. After less than 8.5 seconds Geolocation
1513 // Prompt display order:
1514 // 1. Camera request shown
1515 // 2. MIDI request shown (or MIDI and then Camera, the order depends on
1516 // `PermissionUtil::DoesPlatformSupportChip()`)
1517 // 3. Geolocation request shown
1518 // 4. Notifications request shown
1519 // If Chip is enabled MIDI will replace Camera, hence 5 prompts will be
1520 // shown. Otherwise 4.
1521 TEST_F(PermissionRequestManagerTest,
1522 CameraAbusiveNotificationsGeolocationMIDI) {
1523 MockNotificationPermissionUiSelector::CreateForManager(
1525 PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1526 absl::nullopt /* async_delay */);
1528 std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1529 RequestType::kCameraStream, /*should_be_seen=*/true, 1);
1531 // Quiet UI is not shown because Camera has higher priority.
1532 std::unique_ptr<MockPermissionRequest> request_notifications =
1533 CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/false,
1535 // Less then 8.5 seconds.
1536 manager_->set_current_request_first_display_time_for_testing(
1537 base::Time::Now() - base::Milliseconds(5000));
1539 // Geolocation is not shown because Camera has higher priority.
1540 std::unique_ptr<MockPermissionRequest> request_geolocation =
1541 CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/false,
1544 std::unique_ptr<MockPermissionRequest> request_midi;
1546 // Since the chip is enabled, MIDI should be shown.
1547 request_midi = CreateAndAddRequest(RequestType::kMidiSysex,
1548 /*should_be_seen=*/true, 2);
1550 // The second permission after quiet UI was requested in 8.5 second window,
1551 // the quiet UI Notifiations request for an abusive origin is not
1552 // automatically ignored.
1553 EXPECT_FALSE(request_notifications->granted());
1554 EXPECT_FALSE(request_notifications->finished());
1556 WaitAndAcceptPromptForRequest(request_midi.get());
1557 WaitAndAcceptPromptForRequest(request_camera.get());
1558 WaitAndAcceptPromptForRequest(request_geolocation.get());
1559 WaitAndAcceptPromptForRequest(request_notifications.get());
1561 EXPECT_EQ(prompt_factory_->show_count(), 5);
1564 // Verifies that non abusive chip behaves similar to others when Quiet UI Chip
1567 // Permissions requested in order:
1569 // 2. Notification (non abusive)
1570 // 3. After less than 8.5 seconds Geolocation
1573 // Prompt display order:
1574 // 1. Camera request shown
1575 // 2. MIDI request shown (or MIDI and then Camera, the order depends on
1576 // `PermissionUtil::DoesPlatformSupportChip()`)
1577 // 3. Geolocation request shown
1578 // 4. Notifications request shown
1579 // If Chip is enabled MIDI will replace Camera, hence 5 prompts will be
1580 // shown. Otherwise 4.
1581 TEST_F(PermissionRequestManagerTest,
1582 CameraNonAbusiveNotificationsGeolocationMIDI) {
1583 std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1584 RequestType::kCameraStream, /*should_be_seen=*/true, 1);
1586 // Quiet UI is not shown because Camera has higher priority.
1587 std::unique_ptr<MockPermissionRequest> request_notifications =
1588 CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/false,
1590 // Less then 8.5 seconds.
1591 manager_->set_current_request_first_display_time_for_testing(
1592 base::Time::Now() - base::Milliseconds(5000));
1594 // Geolocation is not shown because Camera has higher priority.
1595 std::unique_ptr<MockPermissionRequest> request_geolocation =
1596 CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/false,
1599 std::unique_ptr<MockPermissionRequest> request_midi;
1601 // If Chip is enabled, MIDI should be shown, otherwise MIDI should not be
1603 request_midi = CreateAndAddRequest(RequestType::kMidiSysex,
1604 /*should_be_seen=*/true, 2);
1606 // The second permission after quiet UI was requested in 8.5 second window,
1607 // the quiet UI Notifiations request for an abusive origin is not
1608 // automatically ignored.
1609 EXPECT_FALSE(request_notifications->granted());
1610 EXPECT_FALSE(request_notifications->finished());
1612 WaitAndAcceptPromptForRequest(request_midi.get());
1613 WaitAndAcceptPromptForRequest(request_camera.get());
1614 WaitAndAcceptPromptForRequest(request_geolocation.get());
1615 WaitAndAcceptPromptForRequest(request_notifications.get());
1617 EXPECT_EQ(prompt_factory_->show_count(), 5);
1620 // Verifies order of requests with mixed low-high priority requests input, with
1621 // both chip and quiet chip enabled. New permissions are added and accepted one
1624 // Permissions requested in order:
1625 // 1. Multiple Download (high)
1626 // 2. Geolocation (low)
1629 // Prompt display order:
1631 // 2. Multiple Download
1633 TEST_F(PermissionRequestManagerTest, Mixed1Low2HighPriorityRequests) {
1634 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
1635 WaitForBubbleToBeShown();
1637 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1638 WaitForBubbleToBeShown();
1640 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1641 WaitForBubbleToBeShown();
1643 EXPECT_TRUE(prompt_factory_->is_visible());
1644 EXPECT_EQ(prompt_factory_->request_count(), 1);
1646 EXPECT_TRUE(request_mic_.granted());
1647 EXPECT_FALSE(request2_.granted());
1648 EXPECT_FALSE(request1_.granted());
1649 WaitForBubbleToBeShown();
1651 EXPECT_TRUE(prompt_factory_->is_visible());
1652 EXPECT_EQ(prompt_factory_->request_count(), 1);
1654 EXPECT_TRUE(request2_.granted());
1655 EXPECT_FALSE(request1_.granted());
1656 WaitForBubbleToBeShown();
1658 EXPECT_TRUE(prompt_factory_->is_visible());
1659 EXPECT_EQ(prompt_factory_->request_count(), 1);
1661 EXPECT_TRUE(request1_.granted());
1664 // Verifies order of requests with mixed low-high priority requests input, with
1665 // both chip and quiet chip enabled. New permissions are added and accepted one
1668 // Permissions requested in order:
1669 // 1. Geolocation (low)
1671 // 3. Notification (low)
1673 // Prompt display order:
1677 TEST_F(PermissionRequestManagerTest, Mixed2Low1HighRequests) {
1678 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1679 WaitForBubbleToBeShown();
1681 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1682 WaitForBubbleToBeShown();
1684 MockPermissionRequest request(RequestType::kNotifications,
1685 PermissionRequestGestureType::GESTURE);
1686 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1687 WaitForBubbleToBeShown();
1689 EXPECT_TRUE(prompt_factory_->is_visible());
1690 EXPECT_EQ(prompt_factory_->request_count(), 1);
1692 EXPECT_TRUE(request_mic_.granted());
1693 EXPECT_FALSE(request.granted());
1694 EXPECT_FALSE(request1_.granted());
1695 WaitForBubbleToBeShown();
1697 EXPECT_TRUE(prompt_factory_->is_visible());
1698 EXPECT_EQ(prompt_factory_->request_count(), 1);
1700 EXPECT_TRUE(request.granted());
1701 EXPECT_FALSE(request1_.granted());
1702 WaitForBubbleToBeShown();
1704 EXPECT_TRUE(prompt_factory_->is_visible());
1705 EXPECT_EQ(prompt_factory_->request_count(), 1);
1707 EXPECT_TRUE(request1_.granted());
1710 // Verifies order of requests with mixed low-high priority requests input, added
1711 // simultaneously, with both chip and quiet chip enabled.
1713 // Permissions requested in order:
1714 // 1. Geolocation (low)
1716 // 3. Notification (low)
1718 // Prompt display order:
1722 TEST_F(PermissionRequestManagerTest, MultipleSimultaneous2Low1HighRequests) {
1723 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1724 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1725 MockPermissionRequest request(RequestType::kNotifications,
1726 PermissionRequestGestureType::GESTURE);
1727 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1728 WaitForBubbleToBeShown();
1730 EXPECT_TRUE(prompt_factory_->is_visible());
1731 EXPECT_EQ(prompt_factory_->request_count(), 1);
1733 EXPECT_TRUE(request_mic_.granted());
1734 EXPECT_FALSE(request.granted());
1735 EXPECT_FALSE(request1_.granted());
1736 WaitForBubbleToBeShown();
1738 EXPECT_TRUE(prompt_factory_->is_visible());
1739 EXPECT_EQ(prompt_factory_->request_count(), 1);
1741 EXPECT_TRUE(request.granted());
1742 EXPECT_FALSE(request1_.granted());
1743 WaitForBubbleToBeShown();
1745 EXPECT_TRUE(prompt_factory_->is_visible());
1746 EXPECT_EQ(prompt_factory_->request_count(), 1);
1748 EXPECT_TRUE(request1_.granted());
1751 // Verifies order of requests with mixed low-high priority requests input,
1752 // added simultaneously, with both chip and quiet chip enabled.
1754 // Permissions requested in order:
1756 // 2. Geolocation (low)
1758 // 4. Notification (low)
1759 // 5. Multiple Download (high)
1761 // Prompt display order:
1762 // 1. Multiple Download
1767 TEST_F(PermissionRequestManagerTest, MultipleSimultaneous2Low3HighRequests) {
1768 MockPermissionRequest request_midi(RequestType::kMidiSysex,
1769 PermissionRequestGestureType::GESTURE);
1770 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_midi);
1771 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1772 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1773 MockPermissionRequest request_notification(
1774 RequestType::kNotifications, PermissionRequestGestureType::GESTURE);
1775 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
1776 &request_notification);
1777 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
1778 WaitForBubbleToBeShown();
1780 EXPECT_TRUE(prompt_factory_->is_visible());
1781 EXPECT_EQ(prompt_factory_->request_count(), 1);
1783 EXPECT_FALSE(request_mic_.granted());
1784 EXPECT_TRUE(request2_.granted());
1785 EXPECT_FALSE(request_notification.granted());
1786 EXPECT_FALSE(request1_.granted());
1787 EXPECT_FALSE(request_midi.granted());
1788 WaitForBubbleToBeShown();
1790 EXPECT_TRUE(prompt_factory_->is_visible());
1791 EXPECT_EQ(prompt_factory_->request_count(), 1);
1793 EXPECT_TRUE(request_mic_.granted());
1794 EXPECT_FALSE(request_notification.granted());
1795 EXPECT_FALSE(request1_.granted());
1796 EXPECT_FALSE(request_midi.granted());
1797 WaitForBubbleToBeShown();
1799 EXPECT_TRUE(prompt_factory_->is_visible());
1800 EXPECT_EQ(prompt_factory_->request_count(), 1);
1802 EXPECT_FALSE(request_notification.granted());
1803 EXPECT_FALSE(request1_.granted());
1804 EXPECT_TRUE(request_midi.granted());
1805 WaitForBubbleToBeShown();
1807 EXPECT_TRUE(prompt_factory_->is_visible());
1808 EXPECT_EQ(prompt_factory_->request_count(), 1);
1810 EXPECT_TRUE(request_notification.granted());
1811 EXPECT_FALSE(request1_.granted());
1812 WaitForBubbleToBeShown();
1814 EXPECT_TRUE(prompt_factory_->is_visible());
1815 EXPECT_EQ(prompt_factory_->request_count(), 1);
1817 EXPECT_TRUE(request1_.granted());
1820 // Verifies order of requests with mixed low-high priority requests input, added
1821 // simultaneously several times, with both chip and quiet chip enabled.
1823 // Permissions requested in order:
1824 // 1. Geolocation(low) then Notification(low)
1825 // 2. Mic (high) then multiple downloads (high)
1826 // Prompt display order:
1827 // 1. Multiple Download
1831 TEST_F(PermissionRequestManagerTest, MultipleSimultaneous2Low2HighRequests) {
1832 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1833 MockPermissionRequest request(RequestType::kNotifications,
1834 PermissionRequestGestureType::GESTURE);
1835 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1836 WaitForBubbleToBeShown();
1838 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1839 manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
1840 WaitForBubbleToBeShown();
1842 EXPECT_TRUE(prompt_factory_->is_visible());
1843 EXPECT_EQ(prompt_factory_->request_count(), 1);
1845 EXPECT_TRUE(request2_.granted());
1846 EXPECT_FALSE(request_mic_.granted());
1847 EXPECT_FALSE(request.granted());
1848 EXPECT_FALSE(request1_.granted());
1849 WaitForBubbleToBeShown();
1851 EXPECT_TRUE(prompt_factory_->is_visible());
1852 EXPECT_EQ(prompt_factory_->request_count(), 1);
1854 EXPECT_TRUE(request_mic_.granted());
1855 EXPECT_FALSE(request.granted());
1856 EXPECT_FALSE(request1_.granted());
1857 WaitForBubbleToBeShown();
1859 EXPECT_TRUE(prompt_factory_->is_visible());
1860 EXPECT_EQ(prompt_factory_->request_count(), 1);
1862 EXPECT_TRUE(request.granted());
1863 EXPECT_FALSE(request1_.granted());
1864 WaitForBubbleToBeShown();
1866 EXPECT_TRUE(prompt_factory_->is_visible());
1867 EXPECT_EQ(prompt_factory_->request_count(), 1);
1869 EXPECT_TRUE(request1_.granted());
1872 #endif // BUILDFLAG(IS_ANDROID)
1874 } // namespace permissions