Upload upstream chromium 120.0.6099.5
[platform/framework/web/chromium-efl.git] / components / permissions / permission_request_manager_unittest.cc
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.
4
5 #include <stddef.h>
6 #include <memory>
7 #include <string>
8
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"
33
34 namespace permissions {
35
36 namespace {
37 using QuietUiReason = PermissionUiSelector::QuietUiReason;
38 }
39
40 class PermissionRequestManagerTest : public content::RenderViewHostTestHarness {
41  public:
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),
56 #endif
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) {
65   }
66
67   void SetUp() override {
68     content::RenderViewHostTestHarness::SetUp();
69     SetContents(CreateTestWebContents());
70     NavigateAndCommit(GURL(MockPermissionRequest::kDefaultOrigin));
71
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_);
76   }
77
78   void TearDown() override {
79     prompt_factory_ = nullptr;
80     content::RenderViewHostTestHarness::TearDown();
81   }
82
83   void Accept() {
84     manager_->Accept();
85     task_environment()->RunUntilIdle();
86   }
87
88   void Deny() {
89     manager_->Deny();
90     task_environment()->RunUntilIdle();
91   }
92
93   void Closing() {
94     manager_->Dismiss();
95     task_environment()->RunUntilIdle();
96   }
97
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));
106 #endif
107     manager_->OpenHelpCenterLink(event);
108     task_environment()->RunUntilIdle();
109   }
110
111   void WaitForFrameLoad() {
112     // PermissionRequestManager ignores all parameters. Yay?
113     manager_->DOMContentLoaded(nullptr);
114     task_environment()->RunUntilIdle();
115   }
116
117   void WaitForBubbleToBeShown() {
118     manager_->DocumentOnLoadCompletedInPrimaryMainFrame();
119     task_environment()->RunUntilIdle();
120   }
121
122   void MockTabSwitchAway() {
123     manager_->OnVisibilityChanged(content::Visibility::HIDDEN);
124   }
125
126   void MockTabSwitchBack() {
127     manager_->OnVisibilityChanged(content::Visibility::VISIBLE);
128   }
129
130   virtual void NavigationEntryCommitted(
131       const content::LoadCommittedDetails& details) {
132     manager_->NavigationEntryCommitted(details);
133   }
134
135   std::unique_ptr<MockPermissionRequest> CreateAndAddRequest(
136       RequestType type,
137       bool should_be_seen,
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));
146     } else {
147       EXPECT_FALSE(prompt_factory_->RequestTypeSeen(type));
148     }
149     EXPECT_EQ(prompt_factory_->TotalRequestCount(), expected_request_count);
150
151     return request;
152   }
153
154   void WaitAndAcceptPromptForRequest(MockPermissionRequest* request) {
155     WaitForBubbleToBeShown();
156
157     EXPECT_FALSE(request->finished());
158     EXPECT_TRUE(prompt_factory_->is_visible());
159     ASSERT_EQ(prompt_factory_->request_count(), 1);
160
161     Accept();
162     EXPECT_TRUE(request->granted());
163   }
164
165  protected:
166   MockPermissionRequest request1_;
167   MockPermissionRequest request2_;
168   MockPermissionRequest request_mic_;
169   MockPermissionRequest request_camera_;
170 #if !BUILDFLAG(IS_ANDROID)
171   MockPermissionRequest request_ptz_;
172 #endif
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_;
181 };
182
183 ////////////////////////////////////////////////////////////////////////////////
184 // General
185 ////////////////////////////////////////////////////////////////////////////////
186
187 TEST_F(PermissionRequestManagerTest, NoRequests) {
188   WaitForBubbleToBeShown();
189   EXPECT_FALSE(prompt_factory_->is_visible());
190 }
191
192 TEST_F(PermissionRequestManagerTest, SingleRequest) {
193   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
194   WaitForBubbleToBeShown();
195
196   EXPECT_TRUE(prompt_factory_->is_visible());
197   ASSERT_EQ(prompt_factory_->request_count(), 1);
198
199   Accept();
200   EXPECT_TRUE(request1_.granted());
201 }
202
203 TEST_F(PermissionRequestManagerTest, SequentialRequests) {
204   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
205   WaitForBubbleToBeShown();
206   EXPECT_TRUE(prompt_factory_->is_visible());
207
208   Accept();
209   EXPECT_TRUE(request1_.granted());
210   EXPECT_FALSE(prompt_factory_->is_visible());
211
212   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
213   WaitForBubbleToBeShown();
214   EXPECT_TRUE(prompt_factory_->is_visible());
215   Accept();
216   EXPECT_FALSE(prompt_factory_->is_visible());
217   EXPECT_TRUE(request2_.granted());
218 }
219
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();
226
227   EXPECT_TRUE(prompt_factory_->is_visible());
228   ASSERT_EQ(prompt_factory_->request_count(), 1);
229
230   NavigateAndCommit(GURL("http://www2.google.com/"));
231   WaitForBubbleToBeShown();
232
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());
237 }
238
239 TEST_F(PermissionRequestManagerTest, RequestsDontNeedUserGesture) {
240   WaitForFrameLoad();
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();
247
248   EXPECT_TRUE(prompt_factory_->is_visible());
249 }
250
251 TEST_F(PermissionRequestManagerTest, RequestsNotSupported) {
252   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
253   WaitForBubbleToBeShown();
254   Accept();
255   EXPECT_TRUE(request1_.granted());
256
257   manager_->set_web_contents_supports_permission_requests(false);
258
259   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
260   EXPECT_TRUE(request2_.cancelled());
261 }
262
263 ////////////////////////////////////////////////////////////////////////////////
264 // Requests grouping
265 ////////////////////////////////////////////////////////////////////////////////
266
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_);
274
275   WaitForBubbleToBeShown();
276   EXPECT_TRUE(prompt_factory_->is_visible());
277   ASSERT_EQ(prompt_factory_->request_count(), 1);
278   Accept();
279   EXPECT_TRUE(request1_.granted());
280
281   WaitForBubbleToBeShown();
282   EXPECT_TRUE(prompt_factory_->is_visible());
283   ASSERT_EQ(prompt_factory_->request_count(), 1);
284   Accept();
285   EXPECT_TRUE(request2_.granted());
286
287   ASSERT_EQ(prompt_factory_->show_count(), 2);
288 }
289
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();
298
299   EXPECT_TRUE(prompt_factory_->is_visible());
300   EXPECT_EQ(prompt_factory_->request_count(), 1);
301   Accept();
302   EXPECT_TRUE(request_mic_.granted());
303   EXPECT_FALSE(request2_.granted());
304   EXPECT_FALSE(request1_.granted());
305   WaitForBubbleToBeShown();
306
307   EXPECT_TRUE(prompt_factory_->is_visible());
308   EXPECT_EQ(prompt_factory_->request_count(), 1);
309   Accept();
310   EXPECT_TRUE(request2_.granted());
311   EXPECT_FALSE(request1_.granted());
312   WaitForBubbleToBeShown();
313
314   EXPECT_TRUE(prompt_factory_->is_visible());
315   EXPECT_EQ(prompt_factory_->request_count(), 1);
316   Accept();
317   EXPECT_TRUE(request1_.granted());
318 }
319
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();
324
325   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
326   WaitForBubbleToBeShown();
327
328   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
329   WaitForBubbleToBeShown();
330
331   EXPECT_TRUE(prompt_factory_->is_visible());
332   EXPECT_EQ(prompt_factory_->request_count(), 1);
333   Accept();
334   EXPECT_TRUE(request_mic_.granted());
335   EXPECT_FALSE(request2_.granted());
336   EXPECT_FALSE(request1_.granted());
337   WaitForBubbleToBeShown();
338
339   EXPECT_TRUE(prompt_factory_->is_visible());
340   EXPECT_EQ(prompt_factory_->request_count(), 1);
341   Accept();
342   EXPECT_TRUE(request2_.granted());
343   EXPECT_FALSE(request1_.granted());
344   WaitForBubbleToBeShown();
345
346   EXPECT_TRUE(prompt_factory_->is_visible());
347   EXPECT_EQ(prompt_factory_->request_count(), 1);
348   Accept();
349   EXPECT_TRUE(request1_.granted());
350 }
351 #endif  // BUILDFLAG(IS_ANDROID)
352
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();
358
359   EXPECT_TRUE(prompt_factory_->is_visible());
360   ASSERT_EQ(prompt_factory_->request_count(), 2);
361
362   Accept();
363   EXPECT_TRUE(request_mic_.granted());
364   EXPECT_TRUE(request_camera_.granted());
365 }
366
367 // If mic/camera requests come from different origins, they should not be
368 // grouped.
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();
374
375   EXPECT_TRUE(prompt_factory_->is_visible());
376   ASSERT_EQ(prompt_factory_->request_count(), 1);
377 }
378
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();
385
386   EXPECT_TRUE(prompt_factory_->is_visible());
387   ASSERT_EQ(prompt_factory_->request_count(), 2);
388
389   Accept();
390   EXPECT_TRUE(request_camera_.granted());
391   EXPECT_TRUE(request_ptz_.granted());
392 }
393
394 TEST_F(PermissionRequestManagerTest, CameraPtzDifferentOrigins) {
395   // If camera/ptz requests come from different origins, they should not be
396   // grouped.
397   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
398                        &iframe_request_camera_other_domain_);
399   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_ptz_);
400   WaitForBubbleToBeShown();
401
402   EXPECT_TRUE(prompt_factory_->is_visible());
403   ASSERT_EQ(prompt_factory_->request_count(), 1);
404 }
405
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();
412
413   EXPECT_TRUE(prompt_factory_->is_visible());
414   ASSERT_EQ(prompt_factory_->request_count(), 3);
415
416   Accept();
417   EXPECT_TRUE(request_mic_.granted());
418   EXPECT_TRUE(request_camera_.granted());
419   EXPECT_TRUE(request_ptz_.granted());
420 }
421
422 // If mic/camera/ptz requests come from different origins, they should not be
423 // grouped.
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();
430
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);
436   Accept();
437
438   EXPECT_TRUE(prompt_factory_->is_visible());
439   ASSERT_LT(prompt_factory_->request_count(), 3);
440 }
441 #endif  // !BUILDFLAG(IS_ANDROID)
442
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();
449
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);
455   Accept();
456   WaitForBubbleToBeShown();
457
458   EXPECT_TRUE(prompt_factory_->is_visible());
459   ASSERT_LT(prompt_factory_->request_count(), 3);
460   Accept();
461 }
462
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());
468
469   OpenHelpCenterLink();
470   SUCCEED();
471 }
472
473 TEST_F(PermissionRequestManagerTest, OpenHelpCenterLink_RequestNotSupported) {
474   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
475   WaitForBubbleToBeShown();
476   EXPECT_TRUE(prompt_factory_->is_visible());
477
478   EXPECT_DEATH_IF_SUPPORTED(OpenHelpCenterLink(), "");
479 }
480
481 ////////////////////////////////////////////////////////////////////////////////
482 // Tab switching
483 ////////////////////////////////////////////////////////////////////////////////
484
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();
490
491   EXPECT_TRUE(prompt_factory_->is_visible());
492   ASSERT_EQ(prompt_factory_->request_count(), 2);
493
494   MockTabSwitchAway();
495   EXPECT_TRUE(prompt_factory_->is_visible());
496
497   MockTabSwitchBack();
498   WaitForBubbleToBeShown();
499   EXPECT_TRUE(prompt_factory_->is_visible());
500   ASSERT_EQ(prompt_factory_->request_count(), 2);
501
502   Accept();
503   EXPECT_TRUE(request_mic_.granted());
504   EXPECT_TRUE(request_camera_.granted());
505 }
506 #endif  // BUILDFLAG(IS_ANDROID)
507
508 TEST_F(PermissionRequestManagerTest, PermissionRequestWhileTabSwitchedAway) {
509   MockTabSwitchAway();
510   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
511   WaitForBubbleToBeShown();
512   EXPECT_FALSE(prompt_factory_->is_visible());
513
514   MockTabSwitchBack();
515   WaitForBubbleToBeShown();
516   EXPECT_TRUE(prompt_factory_->is_visible());
517 }
518
519 ////////////////////////////////////////////////////////////////////////////////
520 // Duplicated requests
521 ////////////////////////////////////////////////////////////////////////////////
522
523 TEST_F(PermissionRequestManagerTest, SameRequestRejected) {
524   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
525   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
526   EXPECT_FALSE(request1_.finished());
527
528   WaitForBubbleToBeShown();
529   EXPECT_TRUE(prompt_factory_->is_visible());
530   ASSERT_EQ(prompt_factory_->request_count(), 1);
531   Accept();
532   task_environment()->RunUntilIdle();
533   EXPECT_TRUE(request1_.granted());
534   EXPECT_FALSE(prompt_factory_->is_visible());
535 }
536
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(
561       base::BindRepeating(
562           [](const base::WeakPtr<PermissionRequest>& weak_request) {}),
563       &request1_);
564   EXPECT_EQ(1ul, manager_->duplicate_requests_.front().size());
565   EXPECT_EQ(1ul, manager_->duplicate_requests_.size());
566   Accept();
567   EXPECT_EQ(0ul, manager_->duplicate_requests_.size());
568 }
569
570 class QuicklyDeletedRequest : public PermissionRequest {
571  public:
572   QuicklyDeletedRequest(const GURL& requesting_origin,
573                         RequestType request_type,
574                         PermissionRequestGestureType gesture_type)
575       : PermissionRequest(requesting_origin,
576                           request_type,
577                           gesture_type == PermissionRequestGestureType::GESTURE,
578                           base::BindLambdaForTesting(
579                               [](ContentSetting result,
580                                  bool is_one_time,
581                                  bool is_final_decision) { NOTREACHED(); }),
582                           base::NullCallback()) {}
583
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());
589   }
590 };
591
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();
614   Accept();
615   EXPECT_EQ(1ul, manager_->duplicate_requests_.size());
616   WaitForBubbleToBeShown();
617   Accept();
618   EXPECT_EQ(0ul, manager_->duplicate_requests_.size());
619 }
620
621 TEST_F(PermissionRequestManagerTest, DuplicateRequest) {
622   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
623   WaitForBubbleToBeShown();
624   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
625
626   auto dupe_request = request1_.CreateDuplicateRequest();
627   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
628                        dupe_request.get());
629   EXPECT_FALSE(dupe_request->finished());
630   EXPECT_FALSE(request1_.finished());
631
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());
637
638   WaitForBubbleToBeShown();
639   Accept();
640   if (PermissionUtil::DoesPlatformSupportChip()) {
641     EXPECT_TRUE(dupe_request2->finished());
642     EXPECT_TRUE(request2_.finished());
643   } else {
644     EXPECT_TRUE(dupe_request->finished());
645     EXPECT_TRUE(request1_.finished());
646   }
647
648   WaitForBubbleToBeShown();
649   Accept();
650   if (PermissionUtil::DoesPlatformSupportChip()) {
651     EXPECT_TRUE(dupe_request->finished());
652     EXPECT_TRUE(request1_.finished());
653   } else {
654     EXPECT_TRUE(dupe_request2->finished());
655     EXPECT_TRUE(request2_.finished());
656   }
657 }
658
659 ////////////////////////////////////////////////////////////////////////////////
660 // Requests from iframes
661 ////////////////////////////////////////////////////////////////////////////////
662
663 TEST_F(PermissionRequestManagerTest, MainFrameNoRequestIFrameRequest) {
664   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
665                        &iframe_request_same_domain_);
666   WaitForBubbleToBeShown();
667   WaitForFrameLoad();
668
669   EXPECT_TRUE(prompt_factory_->is_visible());
670   Closing();
671   EXPECT_TRUE(iframe_request_same_domain_.finished());
672 }
673
674 TEST_F(PermissionRequestManagerTest, MainFrameAndIFrameRequestSameDomain) {
675   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
676   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
677                        &iframe_request_same_domain_);
678   WaitForFrameLoad();
679   WaitForBubbleToBeShown();
680
681   EXPECT_TRUE(prompt_factory_->is_visible());
682   ASSERT_EQ(1, prompt_factory_->request_count());
683   Closing();
684   if (PermissionUtil::DoesPlatformSupportChip()) {
685     EXPECT_TRUE(iframe_request_same_domain_.finished());
686     EXPECT_FALSE(request1_.finished());
687   } else {
688     EXPECT_TRUE(request1_.finished());
689     EXPECT_FALSE(iframe_request_same_domain_.finished());
690   }
691
692   WaitForBubbleToBeShown();
693   EXPECT_TRUE(prompt_factory_->is_visible());
694   ASSERT_EQ(1, prompt_factory_->request_count());
695
696   Closing();
697   EXPECT_FALSE(prompt_factory_->is_visible());
698   if (PermissionUtil::DoesPlatformSupportChip()) {
699     EXPECT_TRUE(request1_.finished());
700   } else {
701     EXPECT_TRUE(iframe_request_same_domain_.finished());
702   }
703 }
704
705 TEST_F(PermissionRequestManagerTest, MainFrameAndIFrameRequestOtherDomain) {
706   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
707   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
708                        &iframe_request_other_domain_);
709   WaitForFrameLoad();
710   WaitForBubbleToBeShown();
711
712   EXPECT_TRUE(prompt_factory_->is_visible());
713   Closing();
714   if (PermissionUtil::DoesPlatformSupportChip()) {
715     EXPECT_TRUE(iframe_request_other_domain_.finished());
716     EXPECT_FALSE(request1_.finished());
717   } else {
718     EXPECT_TRUE(request1_.finished());
719     EXPECT_FALSE(iframe_request_other_domain_.finished());
720   }
721
722   EXPECT_TRUE(prompt_factory_->is_visible());
723   Closing();
724   EXPECT_TRUE(iframe_request_other_domain_.finished());
725   if (PermissionUtil::DoesPlatformSupportChip()) {
726     EXPECT_TRUE(request1_.finished());
727   } else {
728     EXPECT_TRUE(iframe_request_other_domain_.finished());
729   }
730 }
731
732 TEST_F(PermissionRequestManagerTest, IFrameRequestWhenMainRequestVisible) {
733   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
734   WaitForBubbleToBeShown();
735   EXPECT_TRUE(prompt_factory_->is_visible());
736
737   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
738                        &iframe_request_same_domain_);
739   WaitForFrameLoad();
740   ASSERT_EQ(prompt_factory_->request_count(), 1);
741   Closing();
742   if (PermissionUtil::DoesPlatformSupportChip()) {
743     EXPECT_TRUE(iframe_request_same_domain_.finished());
744     EXPECT_FALSE(request1_.finished());
745   } else {
746     EXPECT_TRUE(request1_.finished());
747     EXPECT_FALSE(iframe_request_same_domain_.finished());
748   }
749
750   EXPECT_TRUE(prompt_factory_->is_visible());
751   ASSERT_EQ(prompt_factory_->request_count(), 1);
752   Closing();
753   EXPECT_TRUE(iframe_request_same_domain_.finished());
754   if (PermissionUtil::DoesPlatformSupportChip()) {
755     EXPECT_TRUE(request1_.finished());
756   } else {
757     EXPECT_TRUE(iframe_request_same_domain_.finished());
758   }
759 }
760
761 TEST_F(PermissionRequestManagerTest,
762        IFrameRequestOtherDomainWhenMainRequestVisible) {
763   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
764   WaitForBubbleToBeShown();
765   EXPECT_TRUE(prompt_factory_->is_visible());
766
767   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
768                        &iframe_request_other_domain_);
769   WaitForFrameLoad();
770   Closing();
771   if (PermissionUtil::DoesPlatformSupportChip()) {
772     EXPECT_TRUE(iframe_request_other_domain_.finished());
773     EXPECT_FALSE(request1_.finished());
774   } else {
775     EXPECT_TRUE(request1_.finished());
776     EXPECT_FALSE(iframe_request_other_domain_.finished());
777   }
778
779   EXPECT_TRUE(prompt_factory_->is_visible());
780   Closing();
781   if (PermissionUtil::DoesPlatformSupportChip()) {
782     EXPECT_TRUE(request1_.finished());
783   } else {
784     EXPECT_TRUE(iframe_request_other_domain_.finished());
785   }
786 }
787
788 ////////////////////////////////////////////////////////////////////////////////
789 // UMA logging
790 ////////////////////////////////////////////////////////////////////////////////
791
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
794 // completeness.
795 TEST_F(PermissionRequestManagerTest, UMAForSimpleDeniedBubbleAlternatePath) {
796   base::HistogramTester histograms;
797
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.
802
803   Deny();
804   histograms.ExpectUniqueSample(PermissionUmaUtil::kPermissionsPromptDenied,
805                                 static_cast<base::HistogramBase::Sample>(
806                                     RequestTypeForUma::PERMISSION_GEOLOCATION),
807                                 1);
808 }
809
810 TEST_F(PermissionRequestManagerTest, UMAForTabSwitching) {
811   base::HistogramTester histograms;
812
813   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
814   WaitForBubbleToBeShown();
815   histograms.ExpectUniqueSample(PermissionUmaUtil::kPermissionsPromptShown,
816                                 static_cast<base::HistogramBase::Sample>(
817                                     RequestTypeForUma::PERMISSION_GEOLOCATION),
818                                 1);
819
820   MockTabSwitchAway();
821   MockTabSwitchBack();
822   histograms.ExpectUniqueSample(PermissionUmaUtil::kPermissionsPromptShown,
823                                 static_cast<base::HistogramBase::Sample>(
824                                     RequestTypeForUma::PERMISSION_GEOLOCATION),
825                                 1);
826 }
827
828 ////////////////////////////////////////////////////////////////////////////////
829 // UI selectors
830 ////////////////////////////////////////////////////////////////////////////////
831
832 // Simulate a PermissionUiSelector that simply returns a predefined |ui_to_use|
833 // every time.
834 class MockNotificationPermissionUiSelector : public PermissionUiSelector {
835  public:
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) {}
844
845   void SelectUiToUse(PermissionRequest* request,
846                      DecisionMadeCallback callback) override {
847     selected_ui_to_use_ = true;
848     Decision decision(quiet_ui_reason_, Decision::ShowNoWarning());
849     if (async_delay_) {
850       base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
851           FROM_HERE, base::BindOnce(std::move(callback), decision),
852           async_delay_.value());
853     } else {
854       std::move(callback).Run(decision);
855     }
856   }
857
858   bool IsPermissionRequestSupported(RequestType request_type) override {
859     return request_type == RequestType::kNotifications ||
860            request_type == RequestType::kGeolocation;
861   }
862
863   absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
864   PredictedGrantLikelihoodForUKM() override {
865     return prediction_likelihood_;
866   }
867
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));
877   }
878
879   bool selected_ui_to_use() const { return selected_ui_to_use_; }
880
881  private:
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;
887 };
888
889 // Same as the MockNotificationPermissionUiSelector but handling only the
890 // Camera stream request type
891 class MockCameraStreamPermissionUiSelector
892     : public MockNotificationPermissionUiSelector {
893  public:
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,
901                                              async_delay) {}
902
903   bool IsPermissionRequestSupported(RequestType request_type) override {
904     return request_type == RequestType::kCameraStream;
905   }
906
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));
916   }
917 };
918
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 */);
925
926   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
927   WaitForBubbleToBeShown();
928
929   ASSERT_TRUE(prompt_factory_->is_visible());
930   ASSERT_TRUE(prompt_factory_->RequestTypeSeen(request_camera_.request_type()));
931   EXPECT_FALSE(manager_->ShouldCurrentRequestUseQuietUI());
932   Accept();
933
934   EXPECT_TRUE(request_camera_.granted());
935 }
936
937 TEST_F(PermissionRequestManagerTest, UiSelectorUsedForNotifications) {
938   const struct {
939     absl::optional<PermissionUiSelector::QuietUiReason> quiet_ui_reason;
940     absl::optional<base::TimeDelta> async_delay;
941   } kTests[] = {
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},
947   };
948
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);
953
954     MockPermissionRequest request(RequestType::kNotifications,
955                                   PermissionRequestGestureType::GESTURE);
956
957     manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
958     WaitForBubbleToBeShown();
959
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());
964     Accept();
965
966     EXPECT_TRUE(request.granted());
967   }
968 }
969
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());
981   Accept();
982
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());
992   Accept();
993 }
994
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());
1012   Accept();
1013 }
1014
1015 TEST_F(PermissionRequestManagerTest, MultipleUiSelectors) {
1016   const struct {
1017     std::vector<absl::optional<QuietUiReason>> quiet_ui_reasons;
1018     std::vector<bool> simulate_delayed_decision;
1019     absl::optional<QuietUiReason> expected_reason;
1020   } kTests[] = {
1021       // Simple sync selectors, first one should take priority.
1022       {{QuietUiReason::kTriggeredByCrowdDeny, QuietUiReason::kEnabledInPrefs},
1023        {false, false},
1024        QuietUiReason::kTriggeredByCrowdDeny},
1025       {{QuietUiReason::kTriggeredDueToDisruptiveBehavior,
1026         QuietUiReason::kEnabledInPrefs},
1027        {false, false},
1028        QuietUiReason::kTriggeredDueToDisruptiveBehavior},
1029       {{QuietUiReason::kTriggeredDueToDisruptiveBehavior,
1030         QuietUiReason::kServicePredictedVeryUnlikelyGrant},
1031        {false, false},
1032        QuietUiReason::kTriggeredDueToDisruptiveBehavior},
1033       {{QuietUiReason::kTriggeredDueToDisruptiveBehavior,
1034         QuietUiReason::kTriggeredByCrowdDeny},
1035        {false, false},
1036        QuietUiReason::kTriggeredDueToDisruptiveBehavior},
1037       // First selector is async but should still take priority even if it
1038       // returns later.
1039       {{QuietUiReason::kTriggeredByCrowdDeny, QuietUiReason::kEnabledInPrefs},
1040        {true, false},
1041        QuietUiReason::kTriggeredByCrowdDeny},
1042       {{QuietUiReason::kTriggeredDueToDisruptiveBehavior,
1043         QuietUiReason::kEnabledInPrefs},
1044        {true, false},
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},
1054
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},
1079   };
1080
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>()
1088               : absl::nullopt);
1089     }
1090
1091     MockPermissionRequest request(RequestType::kNotifications,
1092                                   PermissionRequestGestureType::GESTURE);
1093
1094     manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1095     WaitForBubbleToBeShown();
1096
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());
1101     } else {
1102       EXPECT_FALSE(manager_->ShouldCurrentRequestUseQuietUI());
1103     }
1104
1105     Accept();
1106     EXPECT_TRUE(request.granted());
1107   }
1108 }
1109
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;
1116
1117   const struct {
1118     std::vector<bool> enable_quiet_uis;
1119     std::vector<absl::optional<PredictionLikelihood>> prediction_likelihoods;
1120     absl::optional<PredictionLikelihood> expected_prediction_likelihood;
1121   } kTests[] = {
1122       // Sanity check: prediction likelihood is populated correctly.
1123       {{true}, {VeryLikely}, VeryLikely},
1124       {{false}, {Neutral}, Neutral},
1125
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},
1130
1131       // First considered selector is preserved.
1132       {{true, true}, {Neutral, VeryLikely}, Neutral},
1133       {{false, true}, {Neutral, VeryLikely}, Neutral},
1134       {{false, false}, {Neutral, VeryLikely}, Neutral},
1135   };
1136
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(
1141           manager_,
1142           test.enable_quiet_uis[i]
1143               ? absl::optional<QuietUiReason>(QuietUiReason::kEnabledInPrefs)
1144               : absl::nullopt,
1145           absl::nullopt /* async_delay */, test.prediction_likelihoods[i]);
1146     }
1147
1148     MockPermissionRequest request(RequestType::kNotifications,
1149                                   PermissionRequestGestureType::GESTURE);
1150
1151     manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1152     WaitForBubbleToBeShown();
1153
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());
1158
1159     Accept();
1160     EXPECT_TRUE(request.granted());
1161   }
1162 }
1163
1164 TEST_F(PermissionRequestManagerTest, SelectorRequestTypes) {
1165   const struct {
1166     RequestType request_type;
1167     bool should_request_use_quiet_ui;
1168   } kTests[] = {
1169       {RequestType::kNotifications, true},
1170       {RequestType::kGeolocation, true},
1171       {RequestType::kCameraStream, false},
1172   };
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());
1184     Accept();
1185   }
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());
1196   Accept();
1197 }
1198
1199 ////////////////////////////////////////////////////////////////////////////////
1200 // Quiet UI chip. Low priority for Notifications & Geolocation.
1201 ////////////////////////////////////////////////////////////////////////////////
1202
1203 TEST_F(PermissionRequestManagerTest, NotificationsSingleBubbleAndChipRequest) {
1204   MockPermissionRequest request(RequestType::kNotifications,
1205                                 PermissionRequestGestureType::GESTURE);
1206
1207   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1208   WaitForBubbleToBeShown();
1209
1210   EXPECT_TRUE(prompt_factory_->is_visible());
1211   ASSERT_EQ(prompt_factory_->request_count(), 1);
1212
1213   Accept();
1214   EXPECT_TRUE(request.granted());
1215   EXPECT_EQ(prompt_factory_->show_count(), 1);
1216 }
1217
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.
1222 //
1223 // Permissions requested in order:
1224 // 1. Notification (non abusive)
1225 // 2. Geolocation
1226 // 3. Camera
1227 //
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,
1236                           1);
1237   std::unique_ptr<MockPermissionRequest> request_geolocation =
1238       CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/false,
1239                           1);
1240   std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1241       RequestType::kCameraStream, /*should_be_seen=*/false, 1);
1242
1243   for (auto* kRequest : {request_notifications.get(), request_geolocation.get(),
1244                          request_camera.get()}) {
1245     WaitAndAcceptPromptForRequest(kRequest);
1246   }
1247
1248   EXPECT_EQ(prompt_factory_->show_count(), 3);
1249 }
1250
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
1254 // always shown.
1255 //
1256 // Permissions requested in order:
1257 // 1. Camera
1258 // 2. Clipboard
1259 // 3. MIDI
1260 //
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);
1275
1276   for (auto* kRequest :
1277        {request_midi.get(), request_clipboard.get(), request_camera.get()}) {
1278     WaitAndAcceptPromptForRequest(kRequest);
1279   }
1280
1281   EXPECT_EQ(prompt_factory_->show_count(), 5);
1282 }
1283
1284 // Verifies order of simultaneous requests, with quiet chip enabled.
1285 // Simultaneous new requests are coming while we are waiting for UI selector
1286 // decisions.
1287 //
1288 // Permissions requested in order:
1289 // 1. Geolocation, UI selector takes 2 seconds to decide.
1290 // 2. Notification then mic. Notification will preempt geolocation
1291 //
1292 // Prompt display order:
1293 // 1. Mic
1294 // 2. Clipboard
1295 // 3. Geolocation
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_);
1302
1303   task_environment()->FastForwardBy(base::Seconds(1));
1304
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();
1311
1312   EXPECT_TRUE(prompt_factory_->is_visible());
1313   EXPECT_EQ(prompt_factory_->request_count(), 1);
1314   Accept();
1315   EXPECT_TRUE(request_mic_.granted());
1316   EXPECT_FALSE(request.granted());
1317   EXPECT_FALSE(request1_.granted());
1318   WaitForBubbleToBeShown();
1319
1320   EXPECT_TRUE(prompt_factory_->is_visible());
1321   EXPECT_EQ(prompt_factory_->request_count(), 1);
1322   Accept();
1323   EXPECT_TRUE(request.granted());
1324   EXPECT_FALSE(request1_.granted());
1325   WaitForBubbleToBeShown();
1326
1327   EXPECT_TRUE(prompt_factory_->is_visible());
1328   EXPECT_EQ(prompt_factory_->request_count(), 1);
1329   Accept();
1330   EXPECT_TRUE(request1_.granted());
1331 }
1332
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
1338 //
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(
1346       manager_,
1347       PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1348       absl::nullopt /* async_delay */);
1349
1350   std::unique_ptr<MockPermissionRequest> request_notifications =
1351       CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
1352                           1);
1353
1354   // Less then 8.5 seconds.
1355   manager_->set_current_request_first_display_time_for_testing(
1356       base::Time::Now() - base::Milliseconds(5000));
1357
1358   std::unique_ptr<MockPermissionRequest> request_geolocation =
1359       CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
1360                           2);
1361
1362   WaitAndAcceptPromptForRequest(request_geolocation.get());
1363   WaitAndAcceptPromptForRequest(request_notifications.get());
1364
1365   EXPECT_EQ(prompt_factory_->show_count(), 3);
1366 }
1367
1368 // Verifies that the quiet UI chip is ignored if another request came in more
1369 // than 8.5 seconds after.
1370 //
1371 // Permissions requested in order:
1372 // 1. Notification (abusive)
1373 // 2. After more than 8.5 seconds Geolocation
1374 //
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(
1380       manager_,
1381       PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1382       absl::nullopt /* async_delay */);
1383
1384   std::unique_ptr<MockPermissionRequest> request_notifications =
1385       CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
1386                           1);
1387
1388   // More then 8.5 seconds.
1389   manager_->set_current_request_first_display_time_for_testing(
1390       base::Time::Now() - base::Milliseconds(9000));
1391
1392   std::unique_ptr<MockPermissionRequest> request_geolocation =
1393       CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
1394                           2);
1395
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());
1400
1401   WaitAndAcceptPromptForRequest(request_geolocation.get());
1402
1403   EXPECT_EQ(prompt_factory_->show_count(), 2);
1404 }
1405
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.
1409 //
1410 // Permissions requested in order:
1411 // 1. Notification (abusive)
1412 // 2. After less than 8.5 seconds Geolocation
1413 // 3. Camera
1414 //
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(
1424       manager_,
1425       PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1426       absl::nullopt /* async_delay */);
1427
1428   std::unique_ptr<MockPermissionRequest> request_notifications =
1429       CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
1430                           1);
1431   // Less then 8.5 seconds.
1432   manager_->set_current_request_first_display_time_for_testing(
1433       base::Time::Now() - base::Milliseconds(5000));
1434
1435   std::unique_ptr<MockPermissionRequest> request_geolocation =
1436       CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
1437                           2);
1438   std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1439       RequestType::kCameraStream, /*should_be_seen=*/true, 3);
1440
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());
1445
1446   for (auto* kRequest : {request_camera.get(), request_geolocation.get(),
1447                          request_notifications.get()}) {
1448     WaitAndAcceptPromptForRequest(kRequest);
1449   }
1450
1451   EXPECT_EQ(prompt_factory_->show_count(), 5);
1452 }
1453
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.
1457 //
1458 // Permissions requested in order:
1459 // 1. Camera
1460 // 2. Notification (abusive)
1461 // 3. After less than 8.5 seconds Geolocation
1462 //
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(
1469       manager_,
1470       PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1471       absl::nullopt /* async_delay */);
1472
1473   std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1474       RequestType::kCameraStream, /*should_be_seen=*/true, 1);
1475
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,
1479                           1);
1480   // Less then 8.5 seconds.
1481   manager_->set_current_request_first_display_time_for_testing(
1482       base::Time::Now() - base::Milliseconds(5000));
1483
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,
1487                           1);
1488
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());
1494
1495   for (auto* kRequest : {request_camera.get(), request_geolocation.get(),
1496                          request_notifications.get()}) {
1497     WaitAndAcceptPromptForRequest(kRequest);
1498   }
1499
1500   EXPECT_EQ(prompt_factory_->show_count(), 3);
1501 }
1502
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.
1506 //
1507 // Permissions requested in order:
1508 // 1. Camera
1509 // 2. Notification (abusive)
1510 // 3. After less than 8.5 seconds Geolocation
1511 // 4. MIDI
1512 //
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(
1524       manager_,
1525       PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1526       absl::nullopt /* async_delay */);
1527
1528   std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1529       RequestType::kCameraStream, /*should_be_seen=*/true, 1);
1530
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,
1534                           1);
1535   // Less then 8.5 seconds.
1536   manager_->set_current_request_first_display_time_for_testing(
1537       base::Time::Now() - base::Milliseconds(5000));
1538
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,
1542                           1);
1543
1544   std::unique_ptr<MockPermissionRequest> request_midi;
1545
1546   // Since the chip is enabled, MIDI should be shown.
1547   request_midi = CreateAndAddRequest(RequestType::kMidiSysex,
1548                                      /*should_be_seen=*/true, 2);
1549
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());
1555
1556   WaitAndAcceptPromptForRequest(request_midi.get());
1557   WaitAndAcceptPromptForRequest(request_camera.get());
1558   WaitAndAcceptPromptForRequest(request_geolocation.get());
1559   WaitAndAcceptPromptForRequest(request_notifications.get());
1560
1561   EXPECT_EQ(prompt_factory_->show_count(), 5);
1562 }
1563
1564 // Verifies that non abusive chip behaves similar to others when Quiet UI Chip
1565 // is enabled.
1566 //
1567 // Permissions requested in order:
1568 // 1. Camera
1569 // 2. Notification (non abusive)
1570 // 3. After less than 8.5 seconds Geolocation
1571 // 4. MIDI
1572 //
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);
1585
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,
1589                           1);
1590   // Less then 8.5 seconds.
1591   manager_->set_current_request_first_display_time_for_testing(
1592       base::Time::Now() - base::Milliseconds(5000));
1593
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,
1597                           1);
1598
1599   std::unique_ptr<MockPermissionRequest> request_midi;
1600
1601   // If Chip is enabled, MIDI should be shown, otherwise MIDI should not be
1602   // shown.
1603     request_midi = CreateAndAddRequest(RequestType::kMidiSysex,
1604                                        /*should_be_seen=*/true, 2);
1605
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());
1611
1612   WaitAndAcceptPromptForRequest(request_midi.get());
1613   WaitAndAcceptPromptForRequest(request_camera.get());
1614   WaitAndAcceptPromptForRequest(request_geolocation.get());
1615   WaitAndAcceptPromptForRequest(request_notifications.get());
1616
1617   EXPECT_EQ(prompt_factory_->show_count(), 5);
1618 }
1619
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
1622 // by one.
1623 //
1624 // Permissions requested in order:
1625 // 1. Multiple Download (high)
1626 // 2. Geolocation (low)
1627 // 3. Mic (high)
1628 //
1629 // Prompt display order:
1630 // 1. Mic
1631 // 2. Multiple Download
1632 // 3. Geolocation
1633 TEST_F(PermissionRequestManagerTest, Mixed1Low2HighPriorityRequests) {
1634   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
1635   WaitForBubbleToBeShown();
1636
1637   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1638   WaitForBubbleToBeShown();
1639
1640   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1641   WaitForBubbleToBeShown();
1642
1643   EXPECT_TRUE(prompt_factory_->is_visible());
1644   EXPECT_EQ(prompt_factory_->request_count(), 1);
1645   Accept();
1646   EXPECT_TRUE(request_mic_.granted());
1647   EXPECT_FALSE(request2_.granted());
1648   EXPECT_FALSE(request1_.granted());
1649   WaitForBubbleToBeShown();
1650
1651   EXPECT_TRUE(prompt_factory_->is_visible());
1652   EXPECT_EQ(prompt_factory_->request_count(), 1);
1653   Accept();
1654   EXPECT_TRUE(request2_.granted());
1655   EXPECT_FALSE(request1_.granted());
1656   WaitForBubbleToBeShown();
1657
1658   EXPECT_TRUE(prompt_factory_->is_visible());
1659   EXPECT_EQ(prompt_factory_->request_count(), 1);
1660   Accept();
1661   EXPECT_TRUE(request1_.granted());
1662 }
1663
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
1666 // by one.
1667 //
1668 // Permissions requested in order:
1669 // 1. Geolocation (low)
1670 // 2. Mic (high)
1671 // 3. Notification (low)
1672 //
1673 // Prompt display order:
1674 // 1. Mic
1675 // 2. Notification
1676 // 3. Geolocation
1677 TEST_F(PermissionRequestManagerTest, Mixed2Low1HighRequests) {
1678   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1679   WaitForBubbleToBeShown();
1680
1681   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1682   WaitForBubbleToBeShown();
1683
1684   MockPermissionRequest request(RequestType::kNotifications,
1685                                 PermissionRequestGestureType::GESTURE);
1686   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1687   WaitForBubbleToBeShown();
1688
1689   EXPECT_TRUE(prompt_factory_->is_visible());
1690   EXPECT_EQ(prompt_factory_->request_count(), 1);
1691   Accept();
1692   EXPECT_TRUE(request_mic_.granted());
1693   EXPECT_FALSE(request.granted());
1694   EXPECT_FALSE(request1_.granted());
1695   WaitForBubbleToBeShown();
1696
1697   EXPECT_TRUE(prompt_factory_->is_visible());
1698   EXPECT_EQ(prompt_factory_->request_count(), 1);
1699   Accept();
1700   EXPECT_TRUE(request.granted());
1701   EXPECT_FALSE(request1_.granted());
1702   WaitForBubbleToBeShown();
1703
1704   EXPECT_TRUE(prompt_factory_->is_visible());
1705   EXPECT_EQ(prompt_factory_->request_count(), 1);
1706   Accept();
1707   EXPECT_TRUE(request1_.granted());
1708 }
1709
1710 // Verifies order of requests with mixed low-high priority requests input, added
1711 // simultaneously, with both chip and quiet chip enabled.
1712 //
1713 // Permissions requested in order:
1714 // 1. Geolocation (low)
1715 // 2. Mic (high)
1716 // 3. Notification (low)
1717 //
1718 // Prompt display order:
1719 // 1. Mic
1720 // 2. Notification
1721 // 3. Geolocation
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();
1729
1730   EXPECT_TRUE(prompt_factory_->is_visible());
1731   EXPECT_EQ(prompt_factory_->request_count(), 1);
1732   Accept();
1733   EXPECT_TRUE(request_mic_.granted());
1734   EXPECT_FALSE(request.granted());
1735   EXPECT_FALSE(request1_.granted());
1736   WaitForBubbleToBeShown();
1737
1738   EXPECT_TRUE(prompt_factory_->is_visible());
1739   EXPECT_EQ(prompt_factory_->request_count(), 1);
1740   Accept();
1741   EXPECT_TRUE(request.granted());
1742   EXPECT_FALSE(request1_.granted());
1743   WaitForBubbleToBeShown();
1744
1745   EXPECT_TRUE(prompt_factory_->is_visible());
1746   EXPECT_EQ(prompt_factory_->request_count(), 1);
1747   Accept();
1748   EXPECT_TRUE(request1_.granted());
1749 }
1750
1751 // Verifies order of requests with mixed low-high priority requests input,
1752 // added simultaneously, with both chip and quiet chip enabled.
1753 //
1754 // Permissions requested in order:
1755 // 1. MIDI (high)
1756 // 2. Geolocation (low)
1757 // 3. Mic (high)
1758 // 4. Notification (low)
1759 // 5. Multiple Download (high)
1760 //
1761 // Prompt display order:
1762 // 1. Multiple Download
1763 // 2. Mic
1764 // 3. Midi
1765 // 4. Notification
1766 // 5. Geolocation
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();
1779
1780   EXPECT_TRUE(prompt_factory_->is_visible());
1781   EXPECT_EQ(prompt_factory_->request_count(), 1);
1782   Accept();
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();
1789
1790   EXPECT_TRUE(prompt_factory_->is_visible());
1791   EXPECT_EQ(prompt_factory_->request_count(), 1);
1792   Accept();
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();
1798
1799   EXPECT_TRUE(prompt_factory_->is_visible());
1800   EXPECT_EQ(prompt_factory_->request_count(), 1);
1801   Accept();
1802   EXPECT_FALSE(request_notification.granted());
1803   EXPECT_FALSE(request1_.granted());
1804   EXPECT_TRUE(request_midi.granted());
1805   WaitForBubbleToBeShown();
1806
1807   EXPECT_TRUE(prompt_factory_->is_visible());
1808   EXPECT_EQ(prompt_factory_->request_count(), 1);
1809   Accept();
1810   EXPECT_TRUE(request_notification.granted());
1811   EXPECT_FALSE(request1_.granted());
1812   WaitForBubbleToBeShown();
1813
1814   EXPECT_TRUE(prompt_factory_->is_visible());
1815   EXPECT_EQ(prompt_factory_->request_count(), 1);
1816   Accept();
1817   EXPECT_TRUE(request1_.granted());
1818 }
1819
1820 // Verifies order of requests with mixed low-high priority requests input, added
1821 // simultaneously several times, with both chip and quiet chip enabled.
1822 //
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
1828 // 2. Mic
1829 // 3. Notification
1830 // 4. Geolocation
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();
1837
1838   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1839   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
1840   WaitForBubbleToBeShown();
1841
1842   EXPECT_TRUE(prompt_factory_->is_visible());
1843   EXPECT_EQ(prompt_factory_->request_count(), 1);
1844   Accept();
1845   EXPECT_TRUE(request2_.granted());
1846   EXPECT_FALSE(request_mic_.granted());
1847   EXPECT_FALSE(request.granted());
1848   EXPECT_FALSE(request1_.granted());
1849   WaitForBubbleToBeShown();
1850
1851   EXPECT_TRUE(prompt_factory_->is_visible());
1852   EXPECT_EQ(prompt_factory_->request_count(), 1);
1853   Accept();
1854   EXPECT_TRUE(request_mic_.granted());
1855   EXPECT_FALSE(request.granted());
1856   EXPECT_FALSE(request1_.granted());
1857   WaitForBubbleToBeShown();
1858
1859   EXPECT_TRUE(prompt_factory_->is_visible());
1860   EXPECT_EQ(prompt_factory_->request_count(), 1);
1861   Accept();
1862   EXPECT_TRUE(request.granted());
1863   EXPECT_FALSE(request1_.granted());
1864   WaitForBubbleToBeShown();
1865
1866   EXPECT_TRUE(prompt_factory_->is_visible());
1867   EXPECT_EQ(prompt_factory_->request_count(), 1);
1868   Accept();
1869   EXPECT_TRUE(request1_.granted());
1870 }
1871
1872 #endif  // BUILDFLAG(IS_ANDROID)
1873
1874 }  // namespace permissions