Upload upstream chromium 108.0.5359.1
[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/bind.h"
10 #include "base/command_line.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/run_loop.h"
13 #include "base/test/metrics/histogram_tester.h"
14 #include "base/test/scoped_feature_list.h"
15 #include "base/threading/sequenced_task_runner_handle.h"
16 #include "build/build_config.h"
17 #include "components/permissions/features.h"
18 #include "components/permissions/permission_request.h"
19 #include "components/permissions/permission_request_manager.h"
20 #include "components/permissions/permission_ui_selector.h"
21 #include "components/permissions/permission_uma_util.h"
22 #include "components/permissions/request_type.h"
23 #include "components/permissions/test/mock_permission_prompt_factory.h"
24 #include "components/permissions/test/mock_permission_request.h"
25 #include "components/permissions/test/test_permissions_client.h"
26 #include "content/public/test/test_renderer_host.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "third_party/abseil-cpp/absl/types/optional.h"
29
30 namespace permissions {
31
32 namespace {
33 using QuietUiReason = PermissionUiSelector::QuietUiReason;
34 }
35
36 class PermissionRequestManagerTest
37     : public content::RenderViewHostTestHarness,
38       public ::testing::WithParamInterface<bool> {
39  public:
40   PermissionRequestManagerTest()
41       : RenderViewHostTestHarness(
42             base::test::TaskEnvironment::TimeSource::MOCK_TIME),
43         request1_(RequestType::kGeolocation,
44                   PermissionRequestGestureType::GESTURE),
45         request2_(RequestType::kMultipleDownloads,
46                   PermissionRequestGestureType::NO_GESTURE),
47         request_mic_(RequestType::kMicStream,
48                      PermissionRequestGestureType::NO_GESTURE),
49         request_camera_(RequestType::kCameraStream,
50                         PermissionRequestGestureType::NO_GESTURE),
51 #if !BUILDFLAG(IS_ANDROID)
52         request_ptz_(RequestType::kCameraPanTiltZoom,
53                      PermissionRequestGestureType::NO_GESTURE),
54 #endif
55         iframe_request_same_domain_(GURL("https://www.google.com/some/url"),
56                                     RequestType::kMidiSysex),
57         iframe_request_other_domain_(GURL("https://www.youtube.com"),
58                                      RequestType::kGeolocation),
59         iframe_request_camera_other_domain_(GURL("https://www.youtube.com"),
60                                             RequestType::kStorageAccess),
61         iframe_request_mic_other_domain_(GURL("https://www.youtube.com"),
62                                          RequestType::kMicStream) {
63
64     if (GetParam()) {
65       feature_list_.InitWithFeatures(
66           {permissions::features::kPermissionChip},
67           {permissions::features::kPermissionQuietChip});
68     } else {
69       feature_list_.InitWithFeatures(
70           {}, {permissions::features::kPermissionChip,
71                permissions::features::kPermissionQuietChip});
72     }
73   }
74
75   void SetUp() override {
76     content::RenderViewHostTestHarness::SetUp();
77     SetContents(CreateTestWebContents());
78     NavigateAndCommit(GURL(permissions::MockPermissionRequest::kDefaultOrigin));
79
80     PermissionRequestManager::CreateForWebContents(web_contents());
81     manager_ = PermissionRequestManager::FromWebContents(web_contents());
82     manager_->set_enabled_app_level_notification_permission_for_testing(true);
83     prompt_factory_ = std::make_unique<MockPermissionPromptFactory>(manager_);
84   }
85
86   void TearDown() override {
87     prompt_factory_ = nullptr;
88     content::RenderViewHostTestHarness::TearDown();
89   }
90
91   void Accept() {
92     manager_->Accept();
93     task_environment()->RunUntilIdle();
94   }
95
96   void Deny() {
97     manager_->Deny();
98     task_environment()->RunUntilIdle();
99   }
100
101   void Closing() {
102     manager_->Dismiss();
103     task_environment()->RunUntilIdle();
104   }
105
106   void WaitForFrameLoad() {
107     // PermissionRequestManager ignores all parameters. Yay?
108     manager_->DOMContentLoaded(nullptr);
109     task_environment()->RunUntilIdle();
110   }
111
112   void WaitForBubbleToBeShown() {
113     manager_->DocumentOnLoadCompletedInPrimaryMainFrame();
114     task_environment()->RunUntilIdle();
115   }
116
117   void MockTabSwitchAway() {
118     manager_->OnVisibilityChanged(content::Visibility::HIDDEN);
119   }
120
121   void MockTabSwitchBack() {
122     manager_->OnVisibilityChanged(content::Visibility::VISIBLE);
123   }
124
125   virtual void NavigationEntryCommitted(
126       const content::LoadCommittedDetails& details) {
127     manager_->NavigationEntryCommitted(details);
128   }
129
130   std::unique_ptr<MockPermissionRequest> CreateAndAddRequest(
131       RequestType type,
132       bool should_be_seen,
133       int expected_request_count) {
134     std::unique_ptr<MockPermissionRequest> request =
135         std::make_unique<MockPermissionRequest>(
136             type, PermissionRequestGestureType::GESTURE);
137     manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), request.get());
138     WaitForBubbleToBeShown();
139     if (should_be_seen) {
140       EXPECT_TRUE(prompt_factory_->RequestTypeSeen(type));
141     } else {
142       EXPECT_FALSE(prompt_factory_->RequestTypeSeen(type));
143     }
144     EXPECT_EQ(prompt_factory_->TotalRequestCount(), expected_request_count);
145
146     return request;
147   }
148
149   void WaitAndAcceptPromptForRequest(MockPermissionRequest* request) {
150     WaitForBubbleToBeShown();
151
152     EXPECT_FALSE(request->finished());
153     EXPECT_TRUE(prompt_factory_->is_visible());
154     ASSERT_EQ(prompt_factory_->request_count(), 1);
155
156     Accept();
157     EXPECT_TRUE(request->granted());
158   }
159
160  protected:
161   MockPermissionRequest request1_;
162   MockPermissionRequest request2_;
163   MockPermissionRequest request_mic_;
164   MockPermissionRequest request_camera_;
165 #if !BUILDFLAG(IS_ANDROID)
166   MockPermissionRequest request_ptz_;
167 #endif
168   MockPermissionRequest iframe_request_same_domain_;
169   MockPermissionRequest iframe_request_other_domain_;
170   MockPermissionRequest iframe_request_camera_other_domain_;
171   MockPermissionRequest iframe_request_mic_other_domain_;
172   raw_ptr<PermissionRequestManager> manager_;
173   std::unique_ptr<MockPermissionPromptFactory> prompt_factory_;
174   TestPermissionsClient client_;
175   base::test::ScopedFeatureList feature_list_;
176 };
177
178 ////////////////////////////////////////////////////////////////////////////////
179 // General
180 ////////////////////////////////////////////////////////////////////////////////
181
182 TEST_P(PermissionRequestManagerTest, NoRequests) {
183   WaitForBubbleToBeShown();
184   EXPECT_FALSE(prompt_factory_->is_visible());
185 }
186
187 TEST_P(PermissionRequestManagerTest, SingleRequest) {
188   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
189   WaitForBubbleToBeShown();
190
191   EXPECT_TRUE(prompt_factory_->is_visible());
192   ASSERT_EQ(prompt_factory_->request_count(), 1);
193
194   Accept();
195   EXPECT_TRUE(request1_.granted());
196 }
197
198 TEST_P(PermissionRequestManagerTest, SequentialRequests) {
199   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
200   WaitForBubbleToBeShown();
201   EXPECT_TRUE(prompt_factory_->is_visible());
202
203   Accept();
204   EXPECT_TRUE(request1_.granted());
205   EXPECT_FALSE(prompt_factory_->is_visible());
206
207   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
208   WaitForBubbleToBeShown();
209   EXPECT_TRUE(prompt_factory_->is_visible());
210   Accept();
211   EXPECT_FALSE(prompt_factory_->is_visible());
212   EXPECT_TRUE(request2_.granted());
213 }
214
215 TEST_P(PermissionRequestManagerTest, ForgetRequestsOnPageNavigation) {
216   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
217   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
218   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
219                        &iframe_request_other_domain_);
220   WaitForBubbleToBeShown();
221
222   EXPECT_TRUE(prompt_factory_->is_visible());
223   ASSERT_EQ(prompt_factory_->request_count(), 1);
224
225   NavigateAndCommit(GURL("http://www2.google.com/"));
226   WaitForBubbleToBeShown();
227
228   EXPECT_FALSE(prompt_factory_->is_visible());
229   EXPECT_TRUE(request1_.finished());
230   EXPECT_TRUE(request2_.finished());
231   EXPECT_TRUE(iframe_request_other_domain_.finished());
232 }
233
234 TEST_P(PermissionRequestManagerTest, RequestsDontNeedUserGesture) {
235   WaitForFrameLoad();
236   WaitForBubbleToBeShown();
237   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
238   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
239                        &iframe_request_other_domain_);
240   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
241   task_environment()->RunUntilIdle();
242
243   EXPECT_TRUE(prompt_factory_->is_visible());
244 }
245
246 TEST_P(PermissionRequestManagerTest, RequestsNotSupported) {
247   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
248   WaitForBubbleToBeShown();
249   Accept();
250   EXPECT_TRUE(request1_.granted());
251
252   manager_->set_web_contents_supports_permission_requests(false);
253
254   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
255   EXPECT_TRUE(request2_.cancelled());
256 }
257
258 ////////////////////////////////////////////////////////////////////////////////
259 // Requests grouping
260 ////////////////////////////////////////////////////////////////////////////////
261
262 // Most requests should never be grouped.
263 TEST_P(PermissionRequestManagerTest, TwoRequestsUngrouped) {
264   // Grouping for chip feature is tested in ThreeRequestsStackOrderChip.
265   if (GetParam())
266     return;
267
268   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
269   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
270
271   WaitForBubbleToBeShown();
272   EXPECT_TRUE(prompt_factory_->is_visible());
273   ASSERT_EQ(prompt_factory_->request_count(), 1);
274   Accept();
275   EXPECT_TRUE(request1_.granted());
276
277   WaitForBubbleToBeShown();
278   EXPECT_TRUE(prompt_factory_->is_visible());
279   ASSERT_EQ(prompt_factory_->request_count(), 1);
280   Accept();
281   EXPECT_TRUE(request2_.granted());
282
283   ASSERT_EQ(prompt_factory_->show_count(), 2);
284 }
285
286 TEST_P(PermissionRequestManagerTest, ThreeRequestsStackOrderChip) {
287   if (!GetParam())
288     return;
289
290   // Test new permissions order, requests shouldn't be grouped.
291   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
292   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
293   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
294   WaitForBubbleToBeShown();
295
296   EXPECT_TRUE(prompt_factory_->is_visible());
297   EXPECT_EQ(prompt_factory_->request_count(), 1);
298   Accept();
299   EXPECT_TRUE(request_mic_.granted());
300   EXPECT_FALSE(request2_.granted());
301   EXPECT_FALSE(request1_.granted());
302   WaitForBubbleToBeShown();
303
304   EXPECT_TRUE(prompt_factory_->is_visible());
305   EXPECT_EQ(prompt_factory_->request_count(), 1);
306   Accept();
307   EXPECT_TRUE(request2_.granted());
308   EXPECT_FALSE(request1_.granted());
309   WaitForBubbleToBeShown();
310
311   EXPECT_TRUE(prompt_factory_->is_visible());
312   EXPECT_EQ(prompt_factory_->request_count(), 1);
313   Accept();
314   EXPECT_TRUE(request1_.granted());
315 }
316
317 // Test new permissions order by adding requests one at a time.
318 TEST_P(PermissionRequestManagerTest, ThreeRequestsOneByOneStackOrderChip) {
319   if (!GetParam())
320     return;
321
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
352 // Only mic/camera requests from the same origin should be grouped.
353 TEST_P(PermissionRequestManagerTest, MicCameraGrouped) {
354   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
355   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
356   WaitForBubbleToBeShown();
357
358   EXPECT_TRUE(prompt_factory_->is_visible());
359   ASSERT_EQ(prompt_factory_->request_count(), 2);
360
361   Accept();
362   EXPECT_TRUE(request_mic_.granted());
363   EXPECT_TRUE(request_camera_.granted());
364 }
365
366 // If mic/camera requests come from different origins, they should not be
367 // grouped.
368 TEST_P(PermissionRequestManagerTest, MicCameraDifferentOrigins) {
369   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
370                        &iframe_request_mic_other_domain_);
371   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
372   WaitForBubbleToBeShown();
373
374   EXPECT_TRUE(prompt_factory_->is_visible());
375   ASSERT_EQ(prompt_factory_->request_count(), 1);
376 }
377
378 #if !BUILDFLAG(IS_ANDROID)
379 // Only camera/ptz requests from the same origin should be grouped.
380 TEST_P(PermissionRequestManagerTest, CameraPtzGrouped) {
381   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
382   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_ptz_);
383   WaitForBubbleToBeShown();
384
385   EXPECT_TRUE(prompt_factory_->is_visible());
386   ASSERT_EQ(prompt_factory_->request_count(), 2);
387
388   Accept();
389   EXPECT_TRUE(request_camera_.granted());
390   EXPECT_TRUE(request_ptz_.granted());
391 }
392
393 TEST_P(PermissionRequestManagerTest, CameraPtzDifferentOrigins) {
394   // If camera/ptz requests come from different origins, they should not be
395   // grouped.
396   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
397                        &iframe_request_camera_other_domain_);
398   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_ptz_);
399   WaitForBubbleToBeShown();
400
401   EXPECT_TRUE(prompt_factory_->is_visible());
402   ASSERT_EQ(prompt_factory_->request_count(), 1);
403 }
404
405 // Only mic/camera/ptz requests from the same origin should be grouped.
406 TEST_P(PermissionRequestManagerTest, MicCameraPtzGrouped) {
407   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
408   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
409   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_ptz_);
410   WaitForBubbleToBeShown();
411
412   EXPECT_TRUE(prompt_factory_->is_visible());
413   ASSERT_EQ(prompt_factory_->request_count(), 3);
414
415   Accept();
416   EXPECT_TRUE(request_mic_.granted());
417   EXPECT_TRUE(request_camera_.granted());
418   EXPECT_TRUE(request_ptz_.granted());
419 }
420
421 // If mic/camera/ptz requests come from different origins, they should not be
422 // grouped.
423 TEST_P(PermissionRequestManagerTest, MicCameraPtzDifferentOrigins) {
424   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
425                        &iframe_request_mic_other_domain_);
426   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
427   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_ptz_);
428   WaitForBubbleToBeShown();
429
430   // Requests should be split into two groups and each one will contain less
431   // than 3 requests (1 request + 2 request for current logic and 2 requests + 1
432   // request for chip).
433   EXPECT_TRUE(prompt_factory_->is_visible());
434   ASSERT_LT(prompt_factory_->request_count(), 3);
435   Accept();
436
437   EXPECT_TRUE(prompt_factory_->is_visible());
438   ASSERT_LT(prompt_factory_->request_count(), 3);
439 }
440 #endif  // !BUILDFLAG(IS_ANDROID)
441
442 // Tests mix of grouped media requests and non-groupable request.
443 TEST_P(PermissionRequestManagerTest, MixOfMediaAndNotMediaRequests) {
444   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
445   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
446   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
447   WaitForBubbleToBeShown();
448
449   // Requests should be split into two groups and each one will contain less
450   // than 3 requests (1 request + 2 request for current logic and 2 requests + 1
451   // request for chip).
452   EXPECT_TRUE(prompt_factory_->is_visible());
453   ASSERT_LT(prompt_factory_->request_count(), 3);
454   Accept();
455   WaitForBubbleToBeShown();
456
457   EXPECT_TRUE(prompt_factory_->is_visible());
458   ASSERT_LT(prompt_factory_->request_count(), 3);
459   Accept();
460 }
461
462 ////////////////////////////////////////////////////////////////////////////////
463 // Tab switching
464 ////////////////////////////////////////////////////////////////////////////////
465
466 TEST_P(PermissionRequestManagerTest, TwoRequestsTabSwitch) {
467   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
468   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
469   WaitForBubbleToBeShown();
470
471   EXPECT_TRUE(prompt_factory_->is_visible());
472   ASSERT_EQ(prompt_factory_->request_count(), 2);
473
474   MockTabSwitchAway();
475 #if BUILDFLAG(IS_ANDROID)
476   EXPECT_TRUE(prompt_factory_->is_visible());
477 #else
478   EXPECT_FALSE(prompt_factory_->is_visible());
479 #endif
480
481   MockTabSwitchBack();
482   WaitForBubbleToBeShown();
483   EXPECT_TRUE(prompt_factory_->is_visible());
484   ASSERT_EQ(prompt_factory_->request_count(), 2);
485
486   Accept();
487   EXPECT_TRUE(request_mic_.granted());
488   EXPECT_TRUE(request_camera_.granted());
489 }
490
491 TEST_P(PermissionRequestManagerTest, PermissionRequestWhileTabSwitchedAway) {
492   MockTabSwitchAway();
493   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
494   WaitForBubbleToBeShown();
495   EXPECT_FALSE(prompt_factory_->is_visible());
496
497   MockTabSwitchBack();
498   WaitForBubbleToBeShown();
499   EXPECT_TRUE(prompt_factory_->is_visible());
500 }
501
502 ////////////////////////////////////////////////////////////////////////////////
503 // Duplicated requests
504 ////////////////////////////////////////////////////////////////////////////////
505
506 TEST_P(PermissionRequestManagerTest, SameRequestRejected) {
507   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
508   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
509   EXPECT_FALSE(request1_.finished());
510
511   WaitForBubbleToBeShown();
512   EXPECT_TRUE(prompt_factory_->is_visible());
513   ASSERT_EQ(prompt_factory_->request_count(), 1);
514   Accept();
515   task_environment()->RunUntilIdle();
516   EXPECT_TRUE(request1_.granted());
517   EXPECT_FALSE(prompt_factory_->is_visible());
518 }
519
520 TEST_P(PermissionRequestManagerTest, DuplicateRequest) {
521   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
522   WaitForBubbleToBeShown();
523   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
524
525   auto dupe_request = request1_.CreateDuplicateRequest();
526   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
527                        dupe_request.get());
528   EXPECT_FALSE(dupe_request->finished());
529   EXPECT_FALSE(request1_.finished());
530
531   auto dupe_request2 = request2_.CreateDuplicateRequest();
532   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
533                        dupe_request2.get());
534   EXPECT_FALSE(dupe_request2->finished());
535   EXPECT_FALSE(request2_.finished());
536
537   WaitForBubbleToBeShown();
538   Accept();
539   if (GetParam()) {
540     EXPECT_TRUE(dupe_request2->finished());
541     EXPECT_TRUE(request2_.finished());
542   } else {
543     EXPECT_TRUE(dupe_request->finished());
544     EXPECT_TRUE(request1_.finished());
545   }
546
547   WaitForBubbleToBeShown();
548   Accept();
549   if (GetParam()) {
550     EXPECT_TRUE(dupe_request->finished());
551     EXPECT_TRUE(request1_.finished());
552   } else {
553     EXPECT_TRUE(dupe_request2->finished());
554     EXPECT_TRUE(request2_.finished());
555   }
556 }
557
558 ////////////////////////////////////////////////////////////////////////////////
559 // Requests from iframes
560 ////////////////////////////////////////////////////////////////////////////////
561
562 TEST_P(PermissionRequestManagerTest, MainFrameNoRequestIFrameRequest) {
563   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
564                        &iframe_request_same_domain_);
565   WaitForBubbleToBeShown();
566   WaitForFrameLoad();
567
568   EXPECT_TRUE(prompt_factory_->is_visible());
569   Closing();
570   EXPECT_TRUE(iframe_request_same_domain_.finished());
571 }
572
573 TEST_P(PermissionRequestManagerTest, MainFrameAndIFrameRequestSameDomain) {
574   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
575   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
576                        &iframe_request_same_domain_);
577   WaitForFrameLoad();
578   WaitForBubbleToBeShown();
579
580   EXPECT_TRUE(prompt_factory_->is_visible());
581   ASSERT_EQ(1, prompt_factory_->request_count());
582   Closing();
583   if (GetParam()) {
584     EXPECT_TRUE(iframe_request_same_domain_.finished());
585     EXPECT_FALSE(request1_.finished());
586   } else {
587     EXPECT_TRUE(request1_.finished());
588     EXPECT_FALSE(iframe_request_same_domain_.finished());
589   }
590
591   WaitForBubbleToBeShown();
592   EXPECT_TRUE(prompt_factory_->is_visible());
593   ASSERT_EQ(1, prompt_factory_->request_count());
594
595   Closing();
596   EXPECT_FALSE(prompt_factory_->is_visible());
597   if (GetParam())
598     EXPECT_TRUE(request1_.finished());
599   else
600     EXPECT_TRUE(iframe_request_same_domain_.finished());
601 }
602
603 TEST_P(PermissionRequestManagerTest, MainFrameAndIFrameRequestOtherDomain) {
604   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
605   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
606                        &iframe_request_other_domain_);
607   WaitForFrameLoad();
608   WaitForBubbleToBeShown();
609
610   EXPECT_TRUE(prompt_factory_->is_visible());
611   Closing();
612   if (GetParam()) {
613     EXPECT_TRUE(iframe_request_other_domain_.finished());
614     EXPECT_FALSE(request1_.finished());
615   } else {
616     EXPECT_TRUE(request1_.finished());
617     EXPECT_FALSE(iframe_request_other_domain_.finished());
618   }
619
620   EXPECT_TRUE(prompt_factory_->is_visible());
621   Closing();
622   EXPECT_TRUE(iframe_request_other_domain_.finished());
623   if (GetParam())
624     EXPECT_TRUE(request1_.finished());
625   else
626     EXPECT_TRUE(iframe_request_other_domain_.finished());
627 }
628
629 TEST_P(PermissionRequestManagerTest, IFrameRequestWhenMainRequestVisible) {
630   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
631   WaitForBubbleToBeShown();
632   EXPECT_TRUE(prompt_factory_->is_visible());
633
634   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
635                        &iframe_request_same_domain_);
636   WaitForFrameLoad();
637   ASSERT_EQ(prompt_factory_->request_count(), 1);
638   Closing();
639   if (GetParam()) {
640     EXPECT_TRUE(iframe_request_same_domain_.finished());
641     EXPECT_FALSE(request1_.finished());
642   } else {
643     EXPECT_TRUE(request1_.finished());
644     EXPECT_FALSE(iframe_request_same_domain_.finished());
645   }
646
647   EXPECT_TRUE(prompt_factory_->is_visible());
648   ASSERT_EQ(prompt_factory_->request_count(), 1);
649   Closing();
650   EXPECT_TRUE(iframe_request_same_domain_.finished());
651   if (GetParam())
652     EXPECT_TRUE(request1_.finished());
653   else
654     EXPECT_TRUE(iframe_request_same_domain_.finished());
655 }
656
657 TEST_P(PermissionRequestManagerTest,
658        IFrameRequestOtherDomainWhenMainRequestVisible) {
659   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
660   WaitForBubbleToBeShown();
661   EXPECT_TRUE(prompt_factory_->is_visible());
662
663   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
664                        &iframe_request_other_domain_);
665   WaitForFrameLoad();
666   Closing();
667   if (GetParam()) {
668     EXPECT_TRUE(iframe_request_other_domain_.finished());
669     EXPECT_FALSE(request1_.finished());
670   } else {
671     EXPECT_TRUE(request1_.finished());
672     EXPECT_FALSE(iframe_request_other_domain_.finished());
673   }
674
675   EXPECT_TRUE(prompt_factory_->is_visible());
676   Closing();
677   if (GetParam())
678     EXPECT_TRUE(request1_.finished());
679   else
680     EXPECT_TRUE(iframe_request_other_domain_.finished());
681 }
682
683 ////////////////////////////////////////////////////////////////////////////////
684 // UMA logging
685 ////////////////////////////////////////////////////////////////////////////////
686
687 // This code path (calling Accept on a non-merged bubble, with no accepted
688 // permission) would never be used in actual Chrome, but its still tested for
689 // completeness.
690 TEST_P(PermissionRequestManagerTest, UMAForSimpleDeniedBubbleAlternatePath) {
691   base::HistogramTester histograms;
692
693   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
694   WaitForBubbleToBeShown();
695   // No need to test UMA for showing prompts again, they were tested in
696   // UMAForSimpleAcceptedBubble.
697
698   Deny();
699   histograms.ExpectUniqueSample(PermissionUmaUtil::kPermissionsPromptDenied,
700                                 static_cast<base::HistogramBase::Sample>(
701                                     RequestTypeForUma::PERMISSION_GEOLOCATION),
702                                 1);
703 }
704
705 TEST_P(PermissionRequestManagerTest, UMAForTabSwitching) {
706   base::HistogramTester histograms;
707
708   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
709   WaitForBubbleToBeShown();
710   histograms.ExpectUniqueSample(PermissionUmaUtil::kPermissionsPromptShown,
711                                 static_cast<base::HistogramBase::Sample>(
712                                     RequestTypeForUma::PERMISSION_GEOLOCATION),
713                                 1);
714
715   MockTabSwitchAway();
716   MockTabSwitchBack();
717   histograms.ExpectUniqueSample(PermissionUmaUtil::kPermissionsPromptShown,
718                                 static_cast<base::HistogramBase::Sample>(
719                                     RequestTypeForUma::PERMISSION_GEOLOCATION),
720                                 1);
721 }
722
723 ////////////////////////////////////////////////////////////////////////////////
724 // UI selectors
725 ////////////////////////////////////////////////////////////////////////////////
726
727 // Simulate a PermissionUiSelector that simply returns a predefined |ui_to_use|
728 // every time.
729 class MockNotificationPermissionUiSelector : public PermissionUiSelector {
730  public:
731   explicit MockNotificationPermissionUiSelector(
732       absl::optional<QuietUiReason> quiet_ui_reason,
733       absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
734           prediction_likelihood,
735       absl::optional<base::TimeDelta> async_delay)
736       : quiet_ui_reason_(quiet_ui_reason),
737         prediction_likelihood_(prediction_likelihood),
738         async_delay_(async_delay) {}
739
740   void SelectUiToUse(PermissionRequest* request,
741                      DecisionMadeCallback callback) override {
742     Decision decision(quiet_ui_reason_, Decision::ShowNoWarning());
743     if (async_delay_) {
744       base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
745           FROM_HERE, base::BindOnce(std::move(callback), decision),
746           async_delay_.value());
747     } else {
748       std::move(callback).Run(decision);
749     }
750   }
751
752   bool IsPermissionRequestSupported(RequestType request_type) override {
753     return request_type == RequestType::kNotifications ||
754            request_type == RequestType::kGeolocation;
755   }
756
757   absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
758   PredictedGrantLikelihoodForUKM() override {
759     return prediction_likelihood_;
760   }
761
762   static void CreateForManager(
763       PermissionRequestManager* manager,
764       absl::optional<QuietUiReason> quiet_ui_reason,
765       absl::optional<base::TimeDelta> async_delay,
766       absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
767           prediction_likelihood = absl::nullopt) {
768     manager->add_permission_ui_selector_for_testing(
769         std::make_unique<MockNotificationPermissionUiSelector>(
770             quiet_ui_reason, prediction_likelihood, async_delay));
771   }
772
773  private:
774   absl::optional<QuietUiReason> quiet_ui_reason_;
775   absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
776       prediction_likelihood_;
777   absl::optional<base::TimeDelta> async_delay_;
778 };
779
780 // Same as the MockNotificationPermissionUiSelector but handling only the
781 // Camera stream request type
782 class MockCameraStreamPermissionUiSelector
783     : public MockNotificationPermissionUiSelector {
784  public:
785   explicit MockCameraStreamPermissionUiSelector(
786       absl::optional<QuietUiReason> quiet_ui_reason,
787       absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
788           prediction_likelihood,
789       absl::optional<base::TimeDelta> async_delay)
790       : MockNotificationPermissionUiSelector(quiet_ui_reason,
791                                              prediction_likelihood,
792                                              async_delay) {}
793
794   bool IsPermissionRequestSupported(RequestType request_type) override {
795     return request_type == RequestType::kCameraStream;
796   }
797
798   static void CreateForManager(
799       PermissionRequestManager* manager,
800       absl::optional<QuietUiReason> quiet_ui_reason,
801       absl::optional<base::TimeDelta> async_delay,
802       absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
803           prediction_likelihood = absl::nullopt) {
804     manager->add_permission_ui_selector_for_testing(
805         std::make_unique<MockCameraStreamPermissionUiSelector>(
806             quiet_ui_reason, prediction_likelihood, async_delay));
807   }
808 };
809
810 TEST_P(PermissionRequestManagerTest,
811        UiSelectorNotUsedForPermissionsOtherThanNotification) {
812   manager_->clear_permission_ui_selector_for_testing();
813   MockNotificationPermissionUiSelector::CreateForManager(
814       manager_, PermissionUiSelector::QuietUiReason::kEnabledInPrefs,
815       absl::nullopt /* async_delay */);
816
817   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
818   WaitForBubbleToBeShown();
819
820   ASSERT_TRUE(prompt_factory_->is_visible());
821   ASSERT_TRUE(prompt_factory_->RequestTypeSeen(request_camera_.request_type()));
822   EXPECT_FALSE(manager_->ShouldCurrentRequestUseQuietUI());
823   Accept();
824
825   EXPECT_TRUE(request_camera_.granted());
826 }
827
828 TEST_P(PermissionRequestManagerTest, UiSelectorUsedForNotifications) {
829   const struct {
830     absl::optional<PermissionUiSelector::QuietUiReason> quiet_ui_reason;
831     absl::optional<base::TimeDelta> async_delay;
832   } kTests[] = {
833       {QuietUiReason::kEnabledInPrefs, absl::make_optional<base::TimeDelta>()},
834       {PermissionUiSelector::Decision::UseNormalUi(),
835        absl::make_optional<base::TimeDelta>()},
836       {QuietUiReason::kEnabledInPrefs, absl::nullopt},
837       {PermissionUiSelector::Decision::UseNormalUi(), absl::nullopt},
838   };
839
840   for (const auto& test : kTests) {
841     manager_->clear_permission_ui_selector_for_testing();
842     MockNotificationPermissionUiSelector::CreateForManager(
843         manager_, test.quiet_ui_reason, test.async_delay);
844
845     MockPermissionRequest request(RequestType::kNotifications,
846                                   PermissionRequestGestureType::GESTURE);
847
848     manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
849     WaitForBubbleToBeShown();
850
851     EXPECT_TRUE(prompt_factory_->is_visible());
852     EXPECT_TRUE(prompt_factory_->RequestTypeSeen(request.request_type()));
853     EXPECT_EQ(!!test.quiet_ui_reason,
854               manager_->ShouldCurrentRequestUseQuietUI());
855     Accept();
856
857     EXPECT_TRUE(request.granted());
858   }
859 }
860
861 TEST_P(PermissionRequestManagerTest,
862        UiSelectionHappensSeparatelyForEachRequest) {
863   manager_->clear_permission_ui_selector_for_testing();
864   MockNotificationPermissionUiSelector::CreateForManager(
865       manager_, QuietUiReason::kEnabledInPrefs,
866       absl::make_optional<base::TimeDelta>());
867   MockPermissionRequest request1(RequestType::kNotifications,
868                                  PermissionRequestGestureType::GESTURE);
869   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1);
870   WaitForBubbleToBeShown();
871   EXPECT_TRUE(manager_->ShouldCurrentRequestUseQuietUI());
872   Accept();
873
874   MockPermissionRequest request2(RequestType::kNotifications,
875                                  PermissionRequestGestureType::GESTURE);
876   manager_->clear_permission_ui_selector_for_testing();
877   MockNotificationPermissionUiSelector::CreateForManager(
878       manager_, PermissionUiSelector::Decision::UseNormalUi(),
879       absl::make_optional<base::TimeDelta>());
880   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2);
881   WaitForBubbleToBeShown();
882   EXPECT_FALSE(manager_->ShouldCurrentRequestUseQuietUI());
883   Accept();
884 }
885
886 TEST_P(PermissionRequestManagerTest, MultipleUiSelectors) {
887
888   const struct {
889     std::vector<absl::optional<QuietUiReason>> quiet_ui_reasons;
890     std::vector<bool> simulate_delayed_decision;
891     absl::optional<QuietUiReason> expected_reason;
892   } kTests[] = {
893       // Simple sync selectors, first one should take priority.
894       {{QuietUiReason::kTriggeredByCrowdDeny, QuietUiReason::kEnabledInPrefs},
895        {false, false},
896        QuietUiReason::kTriggeredByCrowdDeny},
897       // First selector is async but should still take priority even if it
898       // returns later.
899       {{QuietUiReason::kTriggeredByCrowdDeny, QuietUiReason::kEnabledInPrefs},
900        {true, false},
901        QuietUiReason::kTriggeredByCrowdDeny},
902       // The first selector that has a quiet ui decision should be used.
903       {{absl::nullopt, absl::nullopt,
904         QuietUiReason::kTriggeredDueToAbusiveContent,
905         QuietUiReason::kEnabledInPrefs},
906        {false, true, true, false},
907        QuietUiReason::kTriggeredDueToAbusiveContent},
908       // If all selectors return a normal ui, it should use a normal ui.
909       {{absl::nullopt, absl::nullopt}, {false, true}, absl::nullopt},
910
911       // Use a bunch of selectors both async and sync.
912       {{absl::nullopt, absl::nullopt, absl::nullopt, absl::nullopt,
913         absl::nullopt, QuietUiReason::kTriggeredDueToAbusiveRequests,
914         absl::nullopt, QuietUiReason::kEnabledInPrefs},
915        {false, true, false, true, true, true, false, false},
916        QuietUiReason::kTriggeredDueToAbusiveRequests},
917       // Use a bunch of selectors all sync.
918       {{absl::nullopt, absl::nullopt, absl::nullopt, absl::nullopt,
919         absl::nullopt, QuietUiReason::kTriggeredDueToAbusiveRequests,
920         absl::nullopt, QuietUiReason::kEnabledInPrefs},
921        {false, false, false, false, false, false, false, false},
922        QuietUiReason::kTriggeredDueToAbusiveRequests},
923       // Use a bunch of selectors all async.
924       {{absl::nullopt, absl::nullopt, absl::nullopt, absl::nullopt,
925         absl::nullopt, QuietUiReason::kTriggeredDueToAbusiveRequests,
926         absl::nullopt, QuietUiReason::kEnabledInPrefs},
927        {true, true, true, true, true, true, true, true},
928        QuietUiReason::kTriggeredDueToAbusiveRequests},
929   };
930
931   for (const auto& test : kTests) {
932     manager_->clear_permission_ui_selector_for_testing();
933     for (size_t i = 0; i < test.quiet_ui_reasons.size(); ++i) {
934       MockNotificationPermissionUiSelector::CreateForManager(
935           manager_, test.quiet_ui_reasons[i],
936           test.simulate_delayed_decision[i]
937               ? absl::make_optional<base::TimeDelta>()
938               : absl::nullopt);
939     }
940
941     MockPermissionRequest request(RequestType::kNotifications,
942                                   PermissionRequestGestureType::GESTURE);
943
944     manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
945     WaitForBubbleToBeShown();
946
947     EXPECT_TRUE(prompt_factory_->is_visible());
948     EXPECT_TRUE(prompt_factory_->RequestTypeSeen(request.request_type()));
949     if (test.expected_reason.has_value()) {
950       EXPECT_EQ(test.expected_reason, manager_->ReasonForUsingQuietUi());
951     } else {
952       EXPECT_FALSE(manager_->ShouldCurrentRequestUseQuietUI());
953     }
954
955     Accept();
956     EXPECT_TRUE(request.granted());
957   }
958 }
959
960 TEST_P(PermissionRequestManagerTest, SelectorsPredictionLikelihood) {
961   using PredictionLikelihood = PermissionUmaUtil::PredictionGrantLikelihood;
962   const auto VeryLikely = PredictionLikelihood::
963       PermissionPrediction_Likelihood_DiscretizedLikelihood_VERY_LIKELY;
964   const auto Neutral = PredictionLikelihood::
965       PermissionPrediction_Likelihood_DiscretizedLikelihood_NEUTRAL;
966
967   const struct {
968     std::vector<bool> enable_quiet_uis;
969     std::vector<absl::optional<PredictionLikelihood>> prediction_likelihoods;
970     absl::optional<PredictionLikelihood> expected_prediction_likelihood;
971   } kTests[] = {
972       // Sanity check: prediction likelihood is populated correctly.
973       {{true}, {VeryLikely}, VeryLikely},
974       {{false}, {Neutral}, Neutral},
975
976       // Prediction likelihood is populated only if the selector was considered.
977       {{true, true}, {absl::nullopt, VeryLikely}, absl::nullopt},
978       {{false, true}, {absl::nullopt, VeryLikely}, VeryLikely},
979       {{false, false}, {absl::nullopt, VeryLikely}, VeryLikely},
980
981       // First considered selector is preserved.
982       {{true, true}, {Neutral, VeryLikely}, Neutral},
983       {{false, true}, {Neutral, VeryLikely}, Neutral},
984       {{false, false}, {Neutral, VeryLikely}, Neutral},
985   };
986
987   for (const auto& test : kTests) {
988     manager_->clear_permission_ui_selector_for_testing();
989     for (size_t i = 0; i < test.enable_quiet_uis.size(); ++i) {
990       MockNotificationPermissionUiSelector::CreateForManager(
991           manager_,
992           test.enable_quiet_uis[i]
993               ? absl::optional<QuietUiReason>(QuietUiReason::kEnabledInPrefs)
994               : absl::nullopt,
995           absl::nullopt /* async_delay */, test.prediction_likelihoods[i]);
996     }
997
998     MockPermissionRequest request(RequestType::kNotifications,
999                                   PermissionRequestGestureType::GESTURE);
1000
1001     manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1002     WaitForBubbleToBeShown();
1003
1004     EXPECT_TRUE(prompt_factory_->is_visible());
1005     EXPECT_TRUE(prompt_factory_->RequestTypeSeen(request.request_type()));
1006     EXPECT_EQ(test.expected_prediction_likelihood,
1007               manager_->prediction_grant_likelihood_for_testing());
1008
1009     Accept();
1010     EXPECT_TRUE(request.granted());
1011   }
1012 }
1013
1014 TEST_P(PermissionRequestManagerTest, SelectorRequestTypes) {
1015   const struct {
1016     RequestType request_type;
1017     bool should_request_use_quiet_ui;
1018   } kTests[] = {
1019       {RequestType::kNotifications, true},
1020       {RequestType::kGeolocation, true},
1021       {RequestType::kCameraStream, false},
1022   };
1023   manager_->clear_permission_ui_selector_for_testing();
1024   MockNotificationPermissionUiSelector::CreateForManager(
1025       manager_, QuietUiReason::kEnabledInPrefs,
1026       absl::make_optional<base::TimeDelta>());
1027   for (const auto& test : kTests) {
1028     MockPermissionRequest request(test.request_type,
1029                                   PermissionRequestGestureType::GESTURE);
1030     manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1031     WaitForBubbleToBeShown();
1032     EXPECT_EQ(test.should_request_use_quiet_ui,
1033               manager_->ShouldCurrentRequestUseQuietUI());
1034     Accept();
1035   }
1036   // Adding a mock PermissionUiSelector that handles Camera stream.
1037   MockCameraStreamPermissionUiSelector::CreateForManager(
1038       manager_, QuietUiReason::kEnabledInPrefs,
1039       absl::make_optional<base::TimeDelta>());
1040   // Now the RequestType::kCameraStream should show a quiet UI as well
1041   MockPermissionRequest request2(RequestType::kCameraStream,
1042                                  PermissionRequestGestureType::GESTURE);
1043   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2);
1044   WaitForBubbleToBeShown();
1045   EXPECT_TRUE(manager_->ShouldCurrentRequestUseQuietUI());
1046   Accept();
1047 }
1048
1049 ////////////////////////////////////////////////////////////////////////////////
1050 // Quiet UI chip. Low priority for Notifications & Geolocation.
1051 ////////////////////////////////////////////////////////////////////////////////
1052
1053 TEST_P(PermissionRequestManagerTest, NotificationsSingleBubbleAndChipRequest) {
1054   MockPermissionRequest request(RequestType::kNotifications,
1055                                 PermissionRequestGestureType::GESTURE);
1056
1057   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1058   WaitForBubbleToBeShown();
1059
1060   EXPECT_TRUE(prompt_factory_->is_visible());
1061   ASSERT_EQ(prompt_factory_->request_count(), 1);
1062
1063   Accept();
1064   EXPECT_TRUE(request.granted());
1065   EXPECT_EQ(prompt_factory_->show_count(), 1);
1066 }
1067
1068 // Quiet UI feature is disabled. Chip is disabled. No low priority requests, the
1069 // first request is always shown.
1070 //
1071 // Permissions requested in order:
1072 // 1. Notification (non abusive)
1073 // 2. Geolocation
1074 // 3. Camera
1075 //
1076 // Prompt display order:
1077 // 1. Notification request shown
1078 // 2. Geolocation request shown
1079 // 3. Camera request shown
1080 TEST_P(PermissionRequestManagerTest,
1081        NotificationsGeolocationCameraBubbleRequest) {
1082   // permissions::features::kPermissionChip is enabled based on `GetParam()`.
1083   // That test is only for the default bubble.
1084   if (GetParam())
1085     return;
1086
1087   std::unique_ptr<MockPermissionRequest> request_notifications =
1088       CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
1089                           1);
1090   std::unique_ptr<MockPermissionRequest> request_geolocation =
1091       CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/false,
1092                           1);
1093   std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1094       RequestType::kCameraStream, /*should_be_seen=*/false, 1);
1095
1096   for (auto* kRequest : {request_notifications.get(), request_geolocation.get(),
1097                          request_camera.get()}) {
1098     WaitAndAcceptPromptForRequest(kRequest);
1099   }
1100
1101   EXPECT_EQ(prompt_factory_->show_count(), 3);
1102 }
1103
1104 // Quiet UI feature is disabled, no low priority requests, the last request is
1105 // always shown.
1106 //
1107 // Permissions requested in order:
1108 // 1. Notification (non abusive)
1109 // 2. Geolocation
1110 // 3. Camera
1111 //
1112 // Prompt display order:
1113 // 1. Notifications request shown but is preempted
1114 // 2. Geolocation request shown but is preempted
1115 // 3. Camera request shown
1116 // 4. Geolocation request shown again
1117 // 5. Notifications request shown again
1118 TEST_P(PermissionRequestManagerTest,
1119        NotificationsGeolocationCameraChipRequest) {
1120   // permissions::features::kPermissionChip is enabled based on `GetParam()`.
1121   // That test is only for the chip UI.
1122   if (!GetParam())
1123     return;
1124
1125   std::unique_ptr<MockPermissionRequest> request_notifications =
1126       CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
1127                           1);
1128   std::unique_ptr<MockPermissionRequest> request_geolocation =
1129       CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
1130                           2);
1131   std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1132       RequestType::kCameraStream, /*should_be_seen=*/true, 3);
1133
1134   for (auto* kRequest : {request_camera.get(), request_geolocation.get(),
1135                          request_notifications.get()}) {
1136     WaitAndAcceptPromptForRequest(kRequest);
1137   }
1138
1139   EXPECT_EQ(prompt_factory_->show_count(), 5);
1140 }
1141
1142 // Quiet UI feature is disabled, no low priority requests, the last request is
1143 // always shown.
1144 //
1145 // Permissions requested in order:
1146 // 1. Camera
1147 // 2. Notification (non abusive)
1148 // 3. Geolocation
1149 //
1150 // Prompt display order:
1151 // 1. Camera request shown but is preempted
1152 // 2. Notifications request shown but is preempted
1153 // 3. Geolocation request shown
1154 // 4. Notifications request shown again
1155 // 5. Camera request shown again
1156 TEST_P(PermissionRequestManagerTest,
1157        CameraNotificationsGeolocationChipRequest) {
1158   // permissions::features::kPermissionChip is enabled based on `GetParam()`.
1159   // That test is only for the chip.
1160   if (!GetParam())
1161     return;
1162
1163   std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1164       RequestType::kCameraStream, /*should_be_seen=*/true, 1);
1165   std::unique_ptr<MockPermissionRequest> request_notifications =
1166       CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
1167                           2);
1168   std::unique_ptr<MockPermissionRequest> request_geolocation =
1169       CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
1170                           3);
1171
1172   for (auto* kRequest : {request_geolocation.get(), request_notifications.get(),
1173                          request_camera.get()}) {
1174     WaitAndAcceptPromptForRequest(kRequest);
1175   }
1176
1177   EXPECT_EQ(prompt_factory_->show_count(), 5);
1178 }
1179
1180 // Verifies order of simultaneous requests, with quiet chip enabled.
1181 // Simultaneous new requests are coming while we are waiting for UI selector
1182 // decisions.
1183 //
1184 // Permissions requested in order:
1185 // 1. Geolocation, UI selector takes 2 seconds to decide.
1186 // 2. Notification then mic. Notification will preempt geolocation
1187 //
1188 // Prompt display order:
1189 // 1. Mic
1190 // 2. Notification
1191 // 3. Geolocation
1192 TEST_P(PermissionRequestManagerTest, NewHighPriorityRequestDuringUIDecision) {
1193   if (!GetParam())
1194     return;
1195
1196   manager_->clear_permission_ui_selector_for_testing();
1197   MockNotificationPermissionUiSelector::CreateForManager(
1198       manager_, QuietUiReason::kTriggeredDueToAbusiveRequests,
1199       absl::make_optional<base::TimeDelta>(base::Seconds(2)));
1200   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1201
1202   task_environment()->FastForwardBy(base::Seconds(1));
1203
1204   MockPermissionRequest request(RequestType::kNotifications,
1205                                 PermissionRequestGestureType::GESTURE);
1206   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1207   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1208   WaitForBubbleToBeShown();
1209   manager_->clear_permission_ui_selector_for_testing();
1210
1211   EXPECT_TRUE(prompt_factory_->is_visible());
1212   EXPECT_EQ(prompt_factory_->request_count(), 1);
1213   Accept();
1214   EXPECT_TRUE(request_mic_.granted());
1215   EXPECT_FALSE(request.granted());
1216   EXPECT_FALSE(request1_.granted());
1217   WaitForBubbleToBeShown();
1218
1219   EXPECT_TRUE(prompt_factory_->is_visible());
1220   EXPECT_EQ(prompt_factory_->request_count(), 1);
1221   Accept();
1222   EXPECT_TRUE(request.granted());
1223   EXPECT_FALSE(request1_.granted());
1224   WaitForBubbleToBeShown();
1225
1226   EXPECT_TRUE(prompt_factory_->is_visible());
1227   EXPECT_EQ(prompt_factory_->request_count(), 1);
1228   Accept();
1229   EXPECT_TRUE(request1_.granted());
1230 }
1231
1232 class PermissionRequestManagerTestQuietChip
1233     : public PermissionRequestManagerTest {
1234  public:
1235   PermissionRequestManagerTestQuietChip() {
1236     feature_list_.InitWithFeatureState(
1237         permissions::features::kPermissionQuietChip, true);
1238   }
1239
1240  private:
1241   base::test::ScopedFeatureList feature_list_;
1242 };
1243
1244 // Verifies that the quiet UI chip is not ignored if another request came in
1245 // less than 8.5 seconds after.
1246 // Permissions requested in order:
1247 // 1. Notification (abusive)
1248 // 2. After less than 8.5 seconds Geolocation
1249 //
1250 // Prompt display order:
1251 // 1. Notifications request shown but is preempted because of quiet UI.
1252 // 2. Geolocation request shown
1253 // 3. Notifications request shown again
1254 TEST_P(PermissionRequestManagerTestQuietChip,
1255        AbusiveNotificationsGeolocationQuietUIChipRequest) {
1256   MockNotificationPermissionUiSelector::CreateForManager(
1257       manager_,
1258       PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1259       absl::nullopt /* async_delay */);
1260
1261   std::unique_ptr<MockPermissionRequest> request_notifications =
1262       CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
1263                           1);
1264
1265   // Less then 8.5 seconds.
1266   manager_->set_current_request_first_display_time_for_testing(
1267       base::Time::Now() - base::Milliseconds(5000));
1268
1269   std::unique_ptr<MockPermissionRequest> request_geolocation =
1270       CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
1271                           2);
1272
1273   WaitAndAcceptPromptForRequest(request_geolocation.get());
1274   WaitAndAcceptPromptForRequest(request_notifications.get());
1275
1276   EXPECT_EQ(prompt_factory_->show_count(), 3);
1277 }
1278
1279 // Verifies that the quiet UI chip is ignored if another request came in more
1280 // than 8.5 seconds after.
1281 //
1282 // Permissions requested in order:
1283 // 1. Notification (abusive)
1284 // 2. After more than 8.5 seconds Geolocation
1285 //
1286 // Prompt display order:
1287 // 1. Notifications request shown but is preempted because of quiet UI.
1288 // 2. Geolocation request shown
1289 TEST_P(PermissionRequestManagerTestQuietChip,
1290        AbusiveNotificationsShownLongEnough) {
1291   MockNotificationPermissionUiSelector::CreateForManager(
1292       manager_,
1293       PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1294       absl::nullopt /* async_delay */);
1295
1296   std::unique_ptr<MockPermissionRequest> request_notifications =
1297       CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
1298                           1);
1299
1300   // More then 8.5 seconds.
1301   manager_->set_current_request_first_display_time_for_testing(
1302       base::Time::Now() - base::Milliseconds(9000));
1303
1304   std::unique_ptr<MockPermissionRequest> request_geolocation =
1305       CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
1306                           2);
1307
1308   // The second permission was requested after 8.5 second window, the quiet UI
1309   // Notifiations request for an abusive origin is automatically ignored.
1310   EXPECT_FALSE(request_notifications->granted());
1311   EXPECT_TRUE(request_notifications->finished());
1312
1313   WaitAndAcceptPromptForRequest(request_geolocation.get());
1314
1315   EXPECT_EQ(prompt_factory_->show_count(), 2);
1316 }
1317
1318 // Verifies that the quiet UI chip is not ignored if another request came in
1319 // more than 8.5 seconds after. Verify different requests priority. Camera
1320 // request is shown despite being requested last.
1321 //
1322 // Permissions requested in order:
1323 // 1. Notification (abusive)
1324 // 2. After less than 8.5 seconds Geolocation
1325 // 3. Camera
1326 //
1327 // Prompt display order:
1328 // 1. Notifications request shown but is preempted because of quiet UI.
1329 // 2. Geolocation request shown but is preempted because of low priority.
1330 // 3. Camera request shown
1331 // 4. Geolocation request shown again
1332 // 5. Notifications quiet UI request shown again
1333 TEST_P(PermissionRequestManagerTestQuietChip,
1334        AbusiveNotificationsShownLongEnoughCamera) {
1335   MockNotificationPermissionUiSelector::CreateForManager(
1336       manager_,
1337       PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1338       absl::nullopt /* async_delay */);
1339
1340   std::unique_ptr<MockPermissionRequest> request_notifications =
1341       CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
1342                           1);
1343   // Less then 8.5 seconds.
1344   manager_->set_current_request_first_display_time_for_testing(
1345       base::Time::Now() - base::Milliseconds(5000));
1346
1347   std::unique_ptr<MockPermissionRequest> request_geolocation =
1348       CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
1349                           2);
1350   std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1351       RequestType::kCameraStream, /*should_be_seen=*/true, 3);
1352
1353   // The second permission was requested in 8.5 second window, the quiet UI
1354   // Notifiations request for an abusive origin is not automatically ignored.
1355   EXPECT_FALSE(request_notifications->granted());
1356   EXPECT_FALSE(request_notifications->finished());
1357
1358   for (auto* kRequest : {request_camera.get(), request_geolocation.get(),
1359                          request_notifications.get()}) {
1360     WaitAndAcceptPromptForRequest(kRequest);
1361   }
1362
1363   EXPECT_EQ(prompt_factory_->show_count(), 5);
1364 }
1365
1366 // Verifies that the quiet UI chip is not ignored if another request came in
1367 // more than 8.5 seconds after. Verify different requests priority. Camera
1368 // request is not preemted.
1369 //
1370 // Permissions requested in order:
1371 // 1. Camera
1372 // 2. Notification (abusive)
1373 // 3. After less than 8.5 seconds Geolocation
1374 //
1375 // Prompt display order:
1376 // 1. Camera request shown
1377 // 2. Geolocation request shown
1378 // 3. Camera request shown
1379 TEST_P(PermissionRequestManagerTestQuietChip,
1380        CameraAbusiveNotificationsGeolocation) {
1381   MockNotificationPermissionUiSelector::CreateForManager(
1382       manager_,
1383       PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1384       absl::nullopt /* async_delay */);
1385
1386   std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1387       RequestType::kCameraStream, /*should_be_seen=*/true, 1);
1388
1389   // Quiet UI is not shown because Camera has higher priority.
1390   std::unique_ptr<MockPermissionRequest> request_notifications =
1391       CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/false,
1392                           1);
1393   // Less then 8.5 seconds.
1394   manager_->set_current_request_first_display_time_for_testing(
1395       base::Time::Now() - base::Milliseconds(5000));
1396
1397   // Geolocation is not shown because Camera has higher priority.
1398   std::unique_ptr<MockPermissionRequest> request_geolocation =
1399       CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/false,
1400                           1);
1401
1402   // The second permission after quiet UI was requested in 8.5 second window,
1403   // the quiet UI Notifiations request for an abusive origin is not
1404   // automatically ignored.
1405   EXPECT_FALSE(request_notifications->granted());
1406   EXPECT_FALSE(request_notifications->finished());
1407
1408   for (auto* kRequest : {request_camera.get(), request_geolocation.get(),
1409                          request_notifications.get()}) {
1410     WaitAndAcceptPromptForRequest(kRequest);
1411   }
1412
1413   EXPECT_EQ(prompt_factory_->show_count(), 3);
1414 }
1415
1416 // Verifies that the quiet UI chip is not ignored if another request came in
1417 // more than 8.5 seconds after. Verify different requests priority. Camera
1418 // request is not preemted.
1419 //
1420 // Permissions requested in order:
1421 // 1. Camera
1422 // 2. Notification (abusive)
1423 // 3. After less than 8.5 seconds Geolocation
1424 // 4. MIDI
1425 //
1426 // Prompt display order:
1427 // 1. Camera request shown
1428 // 2. MIDI request shown (or MIDI and then Camera, the order depends on
1429 // `GetParam()`)
1430 // 3. Geolocation request shown
1431 // 4. Notifications request shown
1432 // If Chip is enabled MIDI will replace Camera, hence 5 prompts will be
1433 // shown. Otherwise 4.
1434 TEST_P(PermissionRequestManagerTestQuietChip,
1435        CameraAbusiveNotificationsGeolocationMIDI) {
1436   MockNotificationPermissionUiSelector::CreateForManager(
1437       manager_,
1438       PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
1439       absl::nullopt /* async_delay */);
1440
1441   std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1442       RequestType::kCameraStream, /*should_be_seen=*/true, 1);
1443
1444   // Quiet UI is not shown because Camera has higher priority.
1445   std::unique_ptr<MockPermissionRequest> request_notifications =
1446       CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/false,
1447                           1);
1448   // Less then 8.5 seconds.
1449   manager_->set_current_request_first_display_time_for_testing(
1450       base::Time::Now() - base::Milliseconds(5000));
1451
1452   // Geolocation is not shown because Camera has higher priority.
1453   std::unique_ptr<MockPermissionRequest> request_geolocation =
1454       CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/false,
1455                           1);
1456
1457   std::unique_ptr<MockPermissionRequest> request_midi;
1458
1459   // If Chip is enabled, MIDI should be shown, otherwise MIDI should not be
1460   // shown.
1461   if (GetParam()) {
1462     request_midi = CreateAndAddRequest(RequestType::kMidiSysex,
1463                                        /*should_be_seen=*/true, 2);
1464   } else {
1465     request_midi = CreateAndAddRequest(RequestType::kMidiSysex,
1466                                        /*should_be_seen=*/false, 1);
1467   }
1468
1469   // The second permission after quiet UI was requested in 8.5 second window,
1470   // the quiet UI Notifiations request for an abusive origin is not
1471   // automatically ignored.
1472   EXPECT_FALSE(request_notifications->granted());
1473   EXPECT_FALSE(request_notifications->finished());
1474
1475   WaitAndAcceptPromptForRequest(GetParam() ? request_midi.get()
1476                                            : request_camera.get());
1477   WaitAndAcceptPromptForRequest(GetParam() ? request_camera.get()
1478                                            : request_midi.get());
1479   WaitAndAcceptPromptForRequest(request_geolocation.get());
1480   WaitAndAcceptPromptForRequest(request_notifications.get());
1481
1482   EXPECT_EQ(prompt_factory_->show_count(), GetParam() ? 5 : 4);
1483 }
1484
1485 // Verifies that non abusive chip behaves similar to others when Quiet UI Chip
1486 // is enabled.
1487 //
1488 // Permissions requested in order:
1489 // 1. Camera
1490 // 2. Notification (non abusive)
1491 // 3. After less than 8.5 seconds Geolocation
1492 // 4. MIDI
1493 //
1494 // Prompt display order:
1495 // 1. Camera request shown
1496 // 2. MIDI request shown (or MIDI and then Camera, the order depends on
1497 // `GetParam()`)
1498 // 3. Geolocation request shown
1499 // 4. Notifications request shown
1500 // If Chip is enabled MIDI will replace Camera, hence 5 prompts will be
1501 // shown. Otherwise 4.
1502 TEST_P(PermissionRequestManagerTestQuietChip,
1503        CameraNonAbusiveNotificationsGeolocationMIDI) {
1504   std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
1505       RequestType::kCameraStream, /*should_be_seen=*/true, 1);
1506
1507   // Quiet UI is not shown because Camera has higher priority.
1508   std::unique_ptr<MockPermissionRequest> request_notifications =
1509       CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/false,
1510                           1);
1511   // Less then 8.5 seconds.
1512   manager_->set_current_request_first_display_time_for_testing(
1513       base::Time::Now() - base::Milliseconds(5000));
1514
1515   // Geolocation is not shown because Camera has higher priority.
1516   std::unique_ptr<MockPermissionRequest> request_geolocation =
1517       CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/false,
1518                           1);
1519
1520   std::unique_ptr<MockPermissionRequest> request_midi;
1521
1522   // If Chip is enabled, MIDI should be shown, otherwise MIDI should not be
1523   // shown.
1524   if (GetParam()) {
1525     request_midi = CreateAndAddRequest(RequestType::kMidiSysex,
1526                                        /*should_be_seen=*/true, 2);
1527   } else {
1528     request_midi = CreateAndAddRequest(RequestType::kMidiSysex,
1529                                        /*should_be_seen=*/false, 1);
1530   }
1531
1532   // The second permission after quiet UI was requested in 8.5 second window,
1533   // the quiet UI Notifiations request for an abusive origin is not
1534   // automatically ignored.
1535   EXPECT_FALSE(request_notifications->granted());
1536   EXPECT_FALSE(request_notifications->finished());
1537
1538   WaitAndAcceptPromptForRequest(GetParam() ? request_midi.get()
1539                                            : request_camera.get());
1540   WaitAndAcceptPromptForRequest(GetParam() ? request_camera.get()
1541                                            : request_midi.get());
1542   WaitAndAcceptPromptForRequest(request_geolocation.get());
1543   WaitAndAcceptPromptForRequest(request_notifications.get());
1544
1545   EXPECT_EQ(prompt_factory_->show_count(), GetParam() ? 5 : 4);
1546 }
1547
1548 INSTANTIATE_TEST_SUITE_P(All,
1549                          PermissionRequestManagerTest,
1550                          ::testing::Values(false, true));
1551 INSTANTIATE_TEST_SUITE_P(All,
1552                          PermissionRequestManagerTestQuietChip,
1553                          ::testing::Values(false, true));
1554
1555 // Verifies order of requests with mixed low-high priority requests input, with
1556 // both chip and quiet chip enabled. New permissions are added and accepted one
1557 // by one.
1558 //
1559 // Permissions requested in order:
1560 // 1. Multiple Download (high)
1561 // 2. Geolocation (low)
1562 // 3. Mic (high)
1563 //
1564 // Prompt display order:
1565 // 1. Mic
1566 // 2. Multiple Download
1567 // 3. Geolocation
1568 TEST_P(PermissionRequestManagerTestQuietChip, Mixed1Low2HighPriorityRequests) {
1569   if (!GetParam())
1570     return;
1571
1572   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
1573   WaitForBubbleToBeShown();
1574
1575   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1576   WaitForBubbleToBeShown();
1577
1578   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1579   WaitForBubbleToBeShown();
1580
1581   EXPECT_TRUE(prompt_factory_->is_visible());
1582   EXPECT_EQ(prompt_factory_->request_count(), 1);
1583   Accept();
1584   EXPECT_TRUE(request_mic_.granted());
1585   EXPECT_FALSE(request2_.granted());
1586   EXPECT_FALSE(request1_.granted());
1587   WaitForBubbleToBeShown();
1588
1589   EXPECT_TRUE(prompt_factory_->is_visible());
1590   EXPECT_EQ(prompt_factory_->request_count(), 1);
1591   Accept();
1592   EXPECT_TRUE(request2_.granted());
1593   EXPECT_FALSE(request1_.granted());
1594   WaitForBubbleToBeShown();
1595
1596   EXPECT_TRUE(prompt_factory_->is_visible());
1597   EXPECT_EQ(prompt_factory_->request_count(), 1);
1598   Accept();
1599   EXPECT_TRUE(request1_.granted());
1600 }
1601
1602 // Verifies order of requests with mixed low-high priority requests input, with
1603 // both chip and quiet chip enabled. New permissions are added and accepted one
1604 // by one.
1605 //
1606 // Permissions requested in order:
1607 // 1. Geolocation (low)
1608 // 2. Mic (high)
1609 // 3. Notification (low)
1610 //
1611 // Prompt display order:
1612 // 1. Mic
1613 // 2. Notification
1614 // 3. Geolocation
1615 TEST_P(PermissionRequestManagerTestQuietChip, Mixed2Low1HighRequests) {
1616   if (!GetParam())
1617     return;
1618
1619   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1620   WaitForBubbleToBeShown();
1621
1622   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1623   WaitForBubbleToBeShown();
1624
1625   MockPermissionRequest request(RequestType::kNotifications,
1626                                 PermissionRequestGestureType::GESTURE);
1627   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1628   WaitForBubbleToBeShown();
1629
1630   EXPECT_TRUE(prompt_factory_->is_visible());
1631   EXPECT_EQ(prompt_factory_->request_count(), 1);
1632   Accept();
1633   EXPECT_TRUE(request_mic_.granted());
1634   EXPECT_FALSE(request.granted());
1635   EXPECT_FALSE(request1_.granted());
1636   WaitForBubbleToBeShown();
1637
1638   EXPECT_TRUE(prompt_factory_->is_visible());
1639   EXPECT_EQ(prompt_factory_->request_count(), 1);
1640   Accept();
1641   EXPECT_TRUE(request.granted());
1642   EXPECT_FALSE(request1_.granted());
1643   WaitForBubbleToBeShown();
1644
1645   EXPECT_TRUE(prompt_factory_->is_visible());
1646   EXPECT_EQ(prompt_factory_->request_count(), 1);
1647   Accept();
1648   EXPECT_TRUE(request1_.granted());
1649 }
1650
1651 // Verifies order of requests with mixed low-high priority requests input, added
1652 // simultaneously, with both chip and quiet chip enabled.
1653 //
1654 // Permissions requested in order:
1655 // 1. Geolocation (low)
1656 // 2. Mic (high)
1657 // 3. Notification (low)
1658 //
1659 // Prompt display order:
1660 // 1. Mic
1661 // 2. Notification
1662 // 3. Geolocation
1663 TEST_P(PermissionRequestManagerTestQuietChip,
1664        MultipleSimultaneous2Low1HighRequests) {
1665   if (!GetParam())
1666     return;
1667
1668   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1669   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1670   MockPermissionRequest request(RequestType::kNotifications,
1671                                 PermissionRequestGestureType::GESTURE);
1672   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1673   WaitForBubbleToBeShown();
1674
1675   EXPECT_TRUE(prompt_factory_->is_visible());
1676   EXPECT_EQ(prompt_factory_->request_count(), 1);
1677   Accept();
1678   EXPECT_TRUE(request_mic_.granted());
1679   EXPECT_FALSE(request.granted());
1680   EXPECT_FALSE(request1_.granted());
1681   WaitForBubbleToBeShown();
1682
1683   EXPECT_TRUE(prompt_factory_->is_visible());
1684   EXPECT_EQ(prompt_factory_->request_count(), 1);
1685   Accept();
1686   EXPECT_TRUE(request.granted());
1687   EXPECT_FALSE(request1_.granted());
1688   WaitForBubbleToBeShown();
1689
1690   EXPECT_TRUE(prompt_factory_->is_visible());
1691   EXPECT_EQ(prompt_factory_->request_count(), 1);
1692   Accept();
1693   EXPECT_TRUE(request1_.granted());
1694 }
1695
1696 // Verifies order of requests with mixed low-high priority requests input,
1697 // added simultaneously, with both chip and quiet chip enabled.
1698 //
1699 // Permissions requested in order:
1700 // 1. MIDI (high)
1701 // 2. Geolocation (low)
1702 // 3. Mic (high)
1703 // 4. Notification (low)
1704 // 5. Multiple Download (high)
1705 //
1706 // Prompt display order:
1707 // 1. Multiple Download
1708 // 2. Mic
1709 // 3. Midi
1710 // 4. Notification
1711 // 5. Geolocation
1712 TEST_P(PermissionRequestManagerTestQuietChip,
1713        MultipleSimultaneous2Low3HighRequests) {
1714   if (!GetParam())
1715     return;
1716   MockPermissionRequest request_midi(RequestType::kMidiSysex,
1717                                      PermissionRequestGestureType::GESTURE);
1718   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_midi);
1719   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1720   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1721   MockPermissionRequest request_notification(
1722       RequestType::kNotifications, PermissionRequestGestureType::GESTURE);
1723   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
1724                        &request_notification);
1725   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
1726   WaitForBubbleToBeShown();
1727
1728   EXPECT_TRUE(prompt_factory_->is_visible());
1729   EXPECT_EQ(prompt_factory_->request_count(), 1);
1730   Accept();
1731   EXPECT_FALSE(request_mic_.granted());
1732   EXPECT_TRUE(request2_.granted());
1733   EXPECT_FALSE(request_notification.granted());
1734   EXPECT_FALSE(request1_.granted());
1735   EXPECT_FALSE(request_midi.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_mic_.granted());
1742   EXPECT_FALSE(request_notification.granted());
1743   EXPECT_FALSE(request1_.granted());
1744   EXPECT_FALSE(request_midi.granted());
1745   WaitForBubbleToBeShown();
1746
1747   EXPECT_TRUE(prompt_factory_->is_visible());
1748   EXPECT_EQ(prompt_factory_->request_count(), 1);
1749   Accept();
1750   EXPECT_FALSE(request_notification.granted());
1751   EXPECT_FALSE(request1_.granted());
1752   EXPECT_TRUE(request_midi.granted());
1753   WaitForBubbleToBeShown();
1754
1755   EXPECT_TRUE(prompt_factory_->is_visible());
1756   EXPECT_EQ(prompt_factory_->request_count(), 1);
1757   Accept();
1758   EXPECT_TRUE(request_notification.granted());
1759   EXPECT_FALSE(request1_.granted());
1760   WaitForBubbleToBeShown();
1761
1762   EXPECT_TRUE(prompt_factory_->is_visible());
1763   EXPECT_EQ(prompt_factory_->request_count(), 1);
1764   Accept();
1765   EXPECT_TRUE(request1_.granted());
1766 }
1767
1768 // Verifies order of requests with mixed low-high priority requests input, added
1769 // simultaneously several times, with both chip and quiet chip enabled.
1770 //
1771 // Permissions requested in order:
1772 // 1. Geolocation(low) then Notification(low)
1773 // 2. Mic (high) then multiple downloads (high)
1774 // Prompt display order:
1775 // 1. Multiple Download
1776 // 2. Mic
1777 // 3. Notification
1778 // 4. Geolocation
1779 TEST_P(PermissionRequestManagerTestQuietChip,
1780        MultipleSimultaneous2Low2HighRequests) {
1781   if (!GetParam())
1782     return;
1783
1784   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1785   MockPermissionRequest request(RequestType::kNotifications,
1786                                 PermissionRequestGestureType::GESTURE);
1787   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1788   WaitForBubbleToBeShown();
1789
1790   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1791   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
1792   WaitForBubbleToBeShown();
1793
1794   EXPECT_TRUE(prompt_factory_->is_visible());
1795   EXPECT_EQ(prompt_factory_->request_count(), 1);
1796   Accept();
1797   EXPECT_TRUE(request2_.granted());
1798   EXPECT_FALSE(request_mic_.granted());
1799   EXPECT_FALSE(request.granted());
1800   EXPECT_FALSE(request1_.granted());
1801   WaitForBubbleToBeShown();
1802
1803   EXPECT_TRUE(prompt_factory_->is_visible());
1804   EXPECT_EQ(prompt_factory_->request_count(), 1);
1805   Accept();
1806   EXPECT_TRUE(request_mic_.granted());
1807   EXPECT_FALSE(request.granted());
1808   EXPECT_FALSE(request1_.granted());
1809   WaitForBubbleToBeShown();
1810
1811   EXPECT_TRUE(prompt_factory_->is_visible());
1812   EXPECT_EQ(prompt_factory_->request_count(), 1);
1813   Accept();
1814   EXPECT_TRUE(request.granted());
1815   EXPECT_FALSE(request1_.granted());
1816   WaitForBubbleToBeShown();
1817
1818   EXPECT_TRUE(prompt_factory_->is_visible());
1819   EXPECT_EQ(prompt_factory_->request_count(), 1);
1820   Accept();
1821   EXPECT_TRUE(request1_.granted());
1822 }
1823
1824 // Verifies order of requests with mixed low-high priority requests input, with
1825 // both chip and quiet chip enabled. Simultaneous new requests are coming while
1826 // we are waiting for UI selector decisions.
1827 //
1828 // Permissions requested in order:
1829 // 1. Geolocation (low), UI selector takes 2 seconds to decide.
1830 // 2. Notification(low) then mic (high)
1831 //
1832 // Prompt display order:
1833 // 1. Mic
1834 // 2. Geolocation will get delayed 2 seconds, then preempted to front of queue
1835 // 3. Notification
1836 TEST_P(PermissionRequestManagerTestQuietChip,
1837        NewHighPriorityRequestDuringUIDecision) {
1838   if (!GetParam())
1839     return;
1840
1841   manager_->clear_permission_ui_selector_for_testing();
1842   MockNotificationPermissionUiSelector::CreateForManager(
1843       manager_, QuietUiReason::kTriggeredDueToAbusiveRequests,
1844       absl::make_optional<base::TimeDelta>(base::Seconds(2)));
1845   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
1846
1847   task_environment()->FastForwardBy(base::Seconds(1));
1848
1849   MockPermissionRequest request(RequestType::kNotifications,
1850                                 PermissionRequestGestureType::GESTURE);
1851   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
1852   manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
1853   WaitForBubbleToBeShown();
1854   manager_->clear_permission_ui_selector_for_testing();
1855
1856   EXPECT_TRUE(prompt_factory_->is_visible());
1857   EXPECT_EQ(prompt_factory_->request_count(), 1);
1858   Accept();
1859   EXPECT_TRUE(request_mic_.granted());
1860   EXPECT_FALSE(request.granted());
1861   EXPECT_FALSE(request1_.granted());
1862   WaitForBubbleToBeShown();
1863
1864   EXPECT_TRUE(prompt_factory_->is_visible());
1865   EXPECT_EQ(prompt_factory_->request_count(), 1);
1866   Accept();
1867   EXPECT_TRUE(request1_.granted());
1868   EXPECT_FALSE(request.granted());
1869   WaitForBubbleToBeShown();
1870
1871   EXPECT_TRUE(prompt_factory_->is_visible());
1872   EXPECT_EQ(prompt_factory_->request_count(), 1);
1873   Accept();
1874   EXPECT_TRUE(request.granted());
1875 }
1876
1877 }  // namespace permissions