permission_request_manager_unittest.cc 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <stddef.h>
  5. #include <memory>
  6. #include <string>
  7. #include "base/bind.h"
  8. #include "base/command_line.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/run_loop.h"
  11. #include "base/test/metrics/histogram_tester.h"
  12. #include "base/test/scoped_feature_list.h"
  13. #include "base/threading/sequenced_task_runner_handle.h"
  14. #include "build/build_config.h"
  15. #include "components/permissions/features.h"
  16. #include "components/permissions/permission_request.h"
  17. #include "components/permissions/permission_request_manager.h"
  18. #include "components/permissions/permission_ui_selector.h"
  19. #include "components/permissions/permission_uma_util.h"
  20. #include "components/permissions/request_type.h"
  21. #include "components/permissions/test/mock_permission_prompt_factory.h"
  22. #include "components/permissions/test/mock_permission_request.h"
  23. #include "components/permissions/test/test_permissions_client.h"
  24. #include "content/public/test/test_renderer_host.h"
  25. #include "testing/gtest/include/gtest/gtest.h"
  26. #include "third_party/abseil-cpp/absl/types/optional.h"
  27. namespace permissions {
  28. namespace {
  29. using QuietUiReason = PermissionUiSelector::QuietUiReason;
  30. }
  31. class PermissionRequestManagerTest
  32. : public content::RenderViewHostTestHarness,
  33. public ::testing::WithParamInterface<bool> {
  34. public:
  35. PermissionRequestManagerTest()
  36. : request1_(RequestType::kGeolocation,
  37. PermissionRequestGestureType::GESTURE),
  38. request2_(RequestType::kMultipleDownloads,
  39. PermissionRequestGestureType::NO_GESTURE),
  40. request_mic_(RequestType::kMicStream,
  41. PermissionRequestGestureType::NO_GESTURE),
  42. request_camera_(RequestType::kCameraStream,
  43. PermissionRequestGestureType::NO_GESTURE),
  44. #if !BUILDFLAG(IS_ANDROID)
  45. request_ptz_(RequestType::kCameraPanTiltZoom,
  46. PermissionRequestGestureType::NO_GESTURE),
  47. #endif
  48. iframe_request_same_domain_(GURL("https://www.google.com/some/url"),
  49. RequestType::kMidiSysex),
  50. iframe_request_other_domain_(GURL("https://www.youtube.com"),
  51. RequestType::kGeolocation),
  52. iframe_request_camera_other_domain_(GURL("https://www.youtube.com"),
  53. RequestType::kStorageAccess),
  54. iframe_request_mic_other_domain_(GURL("https://www.youtube.com"),
  55. RequestType::kMicStream) {
  56. if (GetParam()) {
  57. feature_list_.InitWithFeatures(
  58. {permissions::features::kPermissionChip},
  59. {permissions::features::kPermissionQuietChip});
  60. } else {
  61. feature_list_.InitWithFeatures(
  62. {}, {permissions::features::kPermissionChip,
  63. permissions::features::kPermissionQuietChip});
  64. }
  65. }
  66. void SetUp() override {
  67. content::RenderViewHostTestHarness::SetUp();
  68. SetContents(CreateTestWebContents());
  69. NavigateAndCommit(GURL(permissions::MockPermissionRequest::kDefaultOrigin));
  70. PermissionRequestManager::CreateForWebContents(web_contents());
  71. manager_ = PermissionRequestManager::FromWebContents(web_contents());
  72. manager_->set_enabled_app_level_notification_permission_for_testing(true);
  73. prompt_factory_ = std::make_unique<MockPermissionPromptFactory>(manager_);
  74. }
  75. void TearDown() override {
  76. prompt_factory_ = nullptr;
  77. content::RenderViewHostTestHarness::TearDown();
  78. }
  79. void Accept() {
  80. manager_->Accept();
  81. base::RunLoop().RunUntilIdle();
  82. }
  83. void Deny() {
  84. manager_->Deny();
  85. base::RunLoop().RunUntilIdle();
  86. }
  87. void Closing() {
  88. manager_->Dismiss();
  89. base::RunLoop().RunUntilIdle();
  90. }
  91. void WaitForFrameLoad() {
  92. // PermissionRequestManager ignores all parameters. Yay?
  93. manager_->DOMContentLoaded(nullptr);
  94. base::RunLoop().RunUntilIdle();
  95. }
  96. void WaitForBubbleToBeShown() {
  97. manager_->DocumentOnLoadCompletedInPrimaryMainFrame();
  98. base::RunLoop().RunUntilIdle();
  99. }
  100. void MockTabSwitchAway() {
  101. manager_->OnVisibilityChanged(content::Visibility::HIDDEN);
  102. }
  103. void MockTabSwitchBack() {
  104. manager_->OnVisibilityChanged(content::Visibility::VISIBLE);
  105. }
  106. virtual void NavigationEntryCommitted(
  107. const content::LoadCommittedDetails& details) {
  108. manager_->NavigationEntryCommitted(details);
  109. }
  110. std::unique_ptr<MockPermissionRequest> CreateAndAddRequest(
  111. RequestType type,
  112. bool should_be_seen,
  113. int expected_request_count) {
  114. std::unique_ptr<MockPermissionRequest> request =
  115. std::make_unique<MockPermissionRequest>(
  116. type, PermissionRequestGestureType::GESTURE);
  117. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), request.get());
  118. WaitForBubbleToBeShown();
  119. if (should_be_seen) {
  120. EXPECT_TRUE(prompt_factory_->RequestTypeSeen(type));
  121. } else {
  122. EXPECT_FALSE(prompt_factory_->RequestTypeSeen(type));
  123. }
  124. EXPECT_EQ(prompt_factory_->TotalRequestCount(), expected_request_count);
  125. return request;
  126. }
  127. void WaitAndAcceptPromptForRequest(MockPermissionRequest* request) {
  128. WaitForBubbleToBeShown();
  129. EXPECT_FALSE(request->finished());
  130. EXPECT_TRUE(prompt_factory_->is_visible());
  131. ASSERT_EQ(prompt_factory_->request_count(), 1);
  132. Accept();
  133. EXPECT_TRUE(request->granted());
  134. }
  135. protected:
  136. MockPermissionRequest request1_;
  137. MockPermissionRequest request2_;
  138. MockPermissionRequest request_mic_;
  139. MockPermissionRequest request_camera_;
  140. #if !BUILDFLAG(IS_ANDROID)
  141. MockPermissionRequest request_ptz_;
  142. #endif
  143. MockPermissionRequest iframe_request_same_domain_;
  144. MockPermissionRequest iframe_request_other_domain_;
  145. MockPermissionRequest iframe_request_camera_other_domain_;
  146. MockPermissionRequest iframe_request_mic_other_domain_;
  147. raw_ptr<PermissionRequestManager> manager_;
  148. std::unique_ptr<MockPermissionPromptFactory> prompt_factory_;
  149. TestPermissionsClient client_;
  150. base::test::ScopedFeatureList feature_list_;
  151. };
  152. ////////////////////////////////////////////////////////////////////////////////
  153. // General
  154. ////////////////////////////////////////////////////////////////////////////////
  155. TEST_P(PermissionRequestManagerTest, NoRequests) {
  156. WaitForBubbleToBeShown();
  157. EXPECT_FALSE(prompt_factory_->is_visible());
  158. }
  159. TEST_P(PermissionRequestManagerTest, SingleRequest) {
  160. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  161. WaitForBubbleToBeShown();
  162. EXPECT_TRUE(prompt_factory_->is_visible());
  163. ASSERT_EQ(prompt_factory_->request_count(), 1);
  164. Accept();
  165. EXPECT_TRUE(request1_.granted());
  166. }
  167. TEST_P(PermissionRequestManagerTest, SequentialRequests) {
  168. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  169. WaitForBubbleToBeShown();
  170. EXPECT_TRUE(prompt_factory_->is_visible());
  171. Accept();
  172. EXPECT_TRUE(request1_.granted());
  173. EXPECT_FALSE(prompt_factory_->is_visible());
  174. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
  175. WaitForBubbleToBeShown();
  176. EXPECT_TRUE(prompt_factory_->is_visible());
  177. Accept();
  178. EXPECT_FALSE(prompt_factory_->is_visible());
  179. EXPECT_TRUE(request2_.granted());
  180. }
  181. TEST_P(PermissionRequestManagerTest, ForgetRequestsOnPageNavigation) {
  182. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  183. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
  184. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
  185. &iframe_request_other_domain_);
  186. WaitForBubbleToBeShown();
  187. EXPECT_TRUE(prompt_factory_->is_visible());
  188. ASSERT_EQ(prompt_factory_->request_count(), 1);
  189. NavigateAndCommit(GURL("http://www2.google.com/"));
  190. WaitForBubbleToBeShown();
  191. EXPECT_FALSE(prompt_factory_->is_visible());
  192. EXPECT_TRUE(request1_.finished());
  193. EXPECT_TRUE(request2_.finished());
  194. EXPECT_TRUE(iframe_request_other_domain_.finished());
  195. }
  196. TEST_P(PermissionRequestManagerTest, RequestsDontNeedUserGesture) {
  197. WaitForFrameLoad();
  198. WaitForBubbleToBeShown();
  199. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  200. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
  201. &iframe_request_other_domain_);
  202. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
  203. base::RunLoop().RunUntilIdle();
  204. EXPECT_TRUE(prompt_factory_->is_visible());
  205. }
  206. TEST_P(PermissionRequestManagerTest, RequestsNotSupported) {
  207. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  208. WaitForBubbleToBeShown();
  209. Accept();
  210. EXPECT_TRUE(request1_.granted());
  211. manager_->set_web_contents_supports_permission_requests(false);
  212. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
  213. EXPECT_TRUE(request2_.cancelled());
  214. }
  215. ////////////////////////////////////////////////////////////////////////////////
  216. // Requests grouping
  217. ////////////////////////////////////////////////////////////////////////////////
  218. // Most requests should never be grouped.
  219. TEST_P(PermissionRequestManagerTest, TwoRequestsUngrouped) {
  220. // Grouping for chip feature is tested in ThreeRequestsStackOrderChip.
  221. if (GetParam())
  222. return;
  223. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  224. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
  225. WaitForBubbleToBeShown();
  226. EXPECT_TRUE(prompt_factory_->is_visible());
  227. ASSERT_EQ(prompt_factory_->request_count(), 1);
  228. Accept();
  229. EXPECT_TRUE(request1_.granted());
  230. WaitForBubbleToBeShown();
  231. EXPECT_TRUE(prompt_factory_->is_visible());
  232. ASSERT_EQ(prompt_factory_->request_count(), 1);
  233. Accept();
  234. EXPECT_TRUE(request2_.granted());
  235. ASSERT_EQ(prompt_factory_->show_count(), 2);
  236. }
  237. TEST_P(PermissionRequestManagerTest, ThreeRequestsStackOrderChip) {
  238. if (!GetParam())
  239. return;
  240. // Test new permissions order, requests shouldn't be grouped.
  241. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  242. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
  243. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
  244. WaitForBubbleToBeShown();
  245. EXPECT_TRUE(prompt_factory_->is_visible());
  246. EXPECT_EQ(prompt_factory_->request_count(), 1);
  247. Accept();
  248. EXPECT_TRUE(request_mic_.granted());
  249. EXPECT_FALSE(request2_.granted());
  250. EXPECT_FALSE(request1_.granted());
  251. WaitForBubbleToBeShown();
  252. EXPECT_TRUE(prompt_factory_->is_visible());
  253. EXPECT_EQ(prompt_factory_->request_count(), 1);
  254. Accept();
  255. EXPECT_TRUE(request2_.granted());
  256. EXPECT_FALSE(request1_.granted());
  257. WaitForBubbleToBeShown();
  258. EXPECT_TRUE(prompt_factory_->is_visible());
  259. EXPECT_EQ(prompt_factory_->request_count(), 1);
  260. Accept();
  261. EXPECT_TRUE(request1_.granted());
  262. }
  263. // Test new permissions order by adding requests one at a time.
  264. TEST_P(PermissionRequestManagerTest, ThreeRequestsOneByOneStackOrderChip) {
  265. if (!GetParam())
  266. return;
  267. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  268. WaitForBubbleToBeShown();
  269. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
  270. WaitForBubbleToBeShown();
  271. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
  272. WaitForBubbleToBeShown();
  273. EXPECT_TRUE(prompt_factory_->is_visible());
  274. EXPECT_EQ(prompt_factory_->request_count(), 1);
  275. Accept();
  276. EXPECT_TRUE(request_mic_.granted());
  277. EXPECT_FALSE(request2_.granted());
  278. EXPECT_FALSE(request1_.granted());
  279. WaitForBubbleToBeShown();
  280. EXPECT_TRUE(prompt_factory_->is_visible());
  281. EXPECT_EQ(prompt_factory_->request_count(), 1);
  282. Accept();
  283. EXPECT_TRUE(request2_.granted());
  284. EXPECT_FALSE(request1_.granted());
  285. WaitForBubbleToBeShown();
  286. EXPECT_TRUE(prompt_factory_->is_visible());
  287. EXPECT_EQ(prompt_factory_->request_count(), 1);
  288. Accept();
  289. EXPECT_TRUE(request1_.granted());
  290. }
  291. // Only mic/camera requests from the same origin should be grouped.
  292. TEST_P(PermissionRequestManagerTest, MicCameraGrouped) {
  293. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
  294. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
  295. WaitForBubbleToBeShown();
  296. EXPECT_TRUE(prompt_factory_->is_visible());
  297. ASSERT_EQ(prompt_factory_->request_count(), 2);
  298. Accept();
  299. EXPECT_TRUE(request_mic_.granted());
  300. EXPECT_TRUE(request_camera_.granted());
  301. }
  302. // If mic/camera requests come from different origins, they should not be
  303. // grouped.
  304. TEST_P(PermissionRequestManagerTest, MicCameraDifferentOrigins) {
  305. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
  306. &iframe_request_mic_other_domain_);
  307. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
  308. WaitForBubbleToBeShown();
  309. EXPECT_TRUE(prompt_factory_->is_visible());
  310. ASSERT_EQ(prompt_factory_->request_count(), 1);
  311. }
  312. #if !BUILDFLAG(IS_ANDROID)
  313. // Only camera/ptz requests from the same origin should be grouped.
  314. TEST_P(PermissionRequestManagerTest, CameraPtzGrouped) {
  315. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
  316. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_ptz_);
  317. WaitForBubbleToBeShown();
  318. EXPECT_TRUE(prompt_factory_->is_visible());
  319. ASSERT_EQ(prompt_factory_->request_count(), 2);
  320. Accept();
  321. EXPECT_TRUE(request_camera_.granted());
  322. EXPECT_TRUE(request_ptz_.granted());
  323. }
  324. TEST_P(PermissionRequestManagerTest, CameraPtzDifferentOrigins) {
  325. // If camera/ptz requests come from different origins, they should not be
  326. // grouped.
  327. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
  328. &iframe_request_camera_other_domain_);
  329. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_ptz_);
  330. WaitForBubbleToBeShown();
  331. EXPECT_TRUE(prompt_factory_->is_visible());
  332. ASSERT_EQ(prompt_factory_->request_count(), 1);
  333. }
  334. // Only mic/camera/ptz requests from the same origin should be grouped.
  335. TEST_P(PermissionRequestManagerTest, MicCameraPtzGrouped) {
  336. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
  337. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
  338. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_ptz_);
  339. WaitForBubbleToBeShown();
  340. EXPECT_TRUE(prompt_factory_->is_visible());
  341. ASSERT_EQ(prompt_factory_->request_count(), 3);
  342. Accept();
  343. EXPECT_TRUE(request_mic_.granted());
  344. EXPECT_TRUE(request_camera_.granted());
  345. EXPECT_TRUE(request_ptz_.granted());
  346. }
  347. // If mic/camera/ptz requests come from different origins, they should not be
  348. // grouped.
  349. TEST_P(PermissionRequestManagerTest, MicCameraPtzDifferentOrigins) {
  350. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
  351. &iframe_request_mic_other_domain_);
  352. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
  353. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_ptz_);
  354. WaitForBubbleToBeShown();
  355. // Requests should be split into two groups and each one will contain less
  356. // than 3 requests (1 request + 2 request for current logic and 2 requests + 1
  357. // request for chip).
  358. EXPECT_TRUE(prompt_factory_->is_visible());
  359. ASSERT_LT(prompt_factory_->request_count(), 3);
  360. Accept();
  361. EXPECT_TRUE(prompt_factory_->is_visible());
  362. ASSERT_LT(prompt_factory_->request_count(), 3);
  363. }
  364. #endif // !BUILDFLAG(IS_ANDROID)
  365. // Tests mix of grouped media requests and non-groupable request.
  366. TEST_P(PermissionRequestManagerTest, MixOfMediaAndNotMediaRequests) {
  367. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  368. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
  369. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
  370. WaitForBubbleToBeShown();
  371. // Requests should be split into two groups and each one will contain less
  372. // than 3 requests (1 request + 2 request for current logic and 2 requests + 1
  373. // request for chip).
  374. EXPECT_TRUE(prompt_factory_->is_visible());
  375. ASSERT_LT(prompt_factory_->request_count(), 3);
  376. Accept();
  377. WaitForBubbleToBeShown();
  378. EXPECT_TRUE(prompt_factory_->is_visible());
  379. ASSERT_LT(prompt_factory_->request_count(), 3);
  380. Accept();
  381. }
  382. ////////////////////////////////////////////////////////////////////////////////
  383. // Tab switching
  384. ////////////////////////////////////////////////////////////////////////////////
  385. TEST_P(PermissionRequestManagerTest, TwoRequestsTabSwitch) {
  386. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_mic_);
  387. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
  388. WaitForBubbleToBeShown();
  389. EXPECT_TRUE(prompt_factory_->is_visible());
  390. ASSERT_EQ(prompt_factory_->request_count(), 2);
  391. MockTabSwitchAway();
  392. #if BUILDFLAG(IS_ANDROID)
  393. EXPECT_TRUE(prompt_factory_->is_visible());
  394. #else
  395. EXPECT_FALSE(prompt_factory_->is_visible());
  396. #endif
  397. MockTabSwitchBack();
  398. WaitForBubbleToBeShown();
  399. EXPECT_TRUE(prompt_factory_->is_visible());
  400. ASSERT_EQ(prompt_factory_->request_count(), 2);
  401. Accept();
  402. EXPECT_TRUE(request_mic_.granted());
  403. EXPECT_TRUE(request_camera_.granted());
  404. }
  405. TEST_P(PermissionRequestManagerTest, PermissionRequestWhileTabSwitchedAway) {
  406. MockTabSwitchAway();
  407. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  408. WaitForBubbleToBeShown();
  409. EXPECT_FALSE(prompt_factory_->is_visible());
  410. MockTabSwitchBack();
  411. WaitForBubbleToBeShown();
  412. EXPECT_TRUE(prompt_factory_->is_visible());
  413. }
  414. ////////////////////////////////////////////////////////////////////////////////
  415. // Duplicated requests
  416. ////////////////////////////////////////////////////////////////////////////////
  417. TEST_P(PermissionRequestManagerTest, SameRequestRejected) {
  418. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  419. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  420. EXPECT_FALSE(request1_.finished());
  421. WaitForBubbleToBeShown();
  422. EXPECT_TRUE(prompt_factory_->is_visible());
  423. ASSERT_EQ(prompt_factory_->request_count(), 1);
  424. Accept();
  425. base::RunLoop().RunUntilIdle();
  426. EXPECT_TRUE(request1_.granted());
  427. EXPECT_FALSE(prompt_factory_->is_visible());
  428. }
  429. TEST_P(PermissionRequestManagerTest, DuplicateRequest) {
  430. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  431. WaitForBubbleToBeShown();
  432. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2_);
  433. auto dupe_request = request1_.CreateDuplicateRequest();
  434. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
  435. dupe_request.get());
  436. EXPECT_FALSE(dupe_request->finished());
  437. EXPECT_FALSE(request1_.finished());
  438. auto dupe_request2 = request2_.CreateDuplicateRequest();
  439. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
  440. dupe_request2.get());
  441. EXPECT_FALSE(dupe_request2->finished());
  442. EXPECT_FALSE(request2_.finished());
  443. WaitForBubbleToBeShown();
  444. Accept();
  445. if (GetParam()) {
  446. EXPECT_TRUE(dupe_request2->finished());
  447. EXPECT_TRUE(request2_.finished());
  448. } else {
  449. EXPECT_TRUE(dupe_request->finished());
  450. EXPECT_TRUE(request1_.finished());
  451. }
  452. WaitForBubbleToBeShown();
  453. Accept();
  454. if (GetParam()) {
  455. EXPECT_TRUE(dupe_request->finished());
  456. EXPECT_TRUE(request1_.finished());
  457. } else {
  458. EXPECT_TRUE(dupe_request2->finished());
  459. EXPECT_TRUE(request2_.finished());
  460. }
  461. }
  462. ////////////////////////////////////////////////////////////////////////////////
  463. // Requests from iframes
  464. ////////////////////////////////////////////////////////////////////////////////
  465. TEST_P(PermissionRequestManagerTest, MainFrameNoRequestIFrameRequest) {
  466. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
  467. &iframe_request_same_domain_);
  468. WaitForBubbleToBeShown();
  469. WaitForFrameLoad();
  470. EXPECT_TRUE(prompt_factory_->is_visible());
  471. Closing();
  472. EXPECT_TRUE(iframe_request_same_domain_.finished());
  473. }
  474. TEST_P(PermissionRequestManagerTest, MainFrameAndIFrameRequestSameDomain) {
  475. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  476. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
  477. &iframe_request_same_domain_);
  478. WaitForFrameLoad();
  479. WaitForBubbleToBeShown();
  480. EXPECT_TRUE(prompt_factory_->is_visible());
  481. ASSERT_EQ(1, prompt_factory_->request_count());
  482. Closing();
  483. if (GetParam()) {
  484. EXPECT_TRUE(iframe_request_same_domain_.finished());
  485. EXPECT_FALSE(request1_.finished());
  486. } else {
  487. EXPECT_TRUE(request1_.finished());
  488. EXPECT_FALSE(iframe_request_same_domain_.finished());
  489. }
  490. WaitForBubbleToBeShown();
  491. EXPECT_TRUE(prompt_factory_->is_visible());
  492. ASSERT_EQ(1, prompt_factory_->request_count());
  493. Closing();
  494. EXPECT_FALSE(prompt_factory_->is_visible());
  495. if (GetParam())
  496. EXPECT_TRUE(request1_.finished());
  497. else
  498. EXPECT_TRUE(iframe_request_same_domain_.finished());
  499. }
  500. TEST_P(PermissionRequestManagerTest, MainFrameAndIFrameRequestOtherDomain) {
  501. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  502. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
  503. &iframe_request_other_domain_);
  504. WaitForFrameLoad();
  505. WaitForBubbleToBeShown();
  506. EXPECT_TRUE(prompt_factory_->is_visible());
  507. Closing();
  508. if (GetParam()) {
  509. EXPECT_TRUE(iframe_request_other_domain_.finished());
  510. EXPECT_FALSE(request1_.finished());
  511. } else {
  512. EXPECT_TRUE(request1_.finished());
  513. EXPECT_FALSE(iframe_request_other_domain_.finished());
  514. }
  515. EXPECT_TRUE(prompt_factory_->is_visible());
  516. Closing();
  517. EXPECT_TRUE(iframe_request_other_domain_.finished());
  518. if (GetParam())
  519. EXPECT_TRUE(request1_.finished());
  520. else
  521. EXPECT_TRUE(iframe_request_other_domain_.finished());
  522. }
  523. TEST_P(PermissionRequestManagerTest, IFrameRequestWhenMainRequestVisible) {
  524. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  525. WaitForBubbleToBeShown();
  526. EXPECT_TRUE(prompt_factory_->is_visible());
  527. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
  528. &iframe_request_same_domain_);
  529. WaitForFrameLoad();
  530. ASSERT_EQ(prompt_factory_->request_count(), 1);
  531. Closing();
  532. if (GetParam()) {
  533. EXPECT_TRUE(iframe_request_same_domain_.finished());
  534. EXPECT_FALSE(request1_.finished());
  535. } else {
  536. EXPECT_TRUE(request1_.finished());
  537. EXPECT_FALSE(iframe_request_same_domain_.finished());
  538. }
  539. EXPECT_TRUE(prompt_factory_->is_visible());
  540. ASSERT_EQ(prompt_factory_->request_count(), 1);
  541. Closing();
  542. EXPECT_TRUE(iframe_request_same_domain_.finished());
  543. if (GetParam())
  544. EXPECT_TRUE(request1_.finished());
  545. else
  546. EXPECT_TRUE(iframe_request_same_domain_.finished());
  547. }
  548. TEST_P(PermissionRequestManagerTest,
  549. IFrameRequestOtherDomainWhenMainRequestVisible) {
  550. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  551. WaitForBubbleToBeShown();
  552. EXPECT_TRUE(prompt_factory_->is_visible());
  553. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(),
  554. &iframe_request_other_domain_);
  555. WaitForFrameLoad();
  556. Closing();
  557. if (GetParam()) {
  558. EXPECT_TRUE(iframe_request_other_domain_.finished());
  559. EXPECT_FALSE(request1_.finished());
  560. } else {
  561. EXPECT_TRUE(request1_.finished());
  562. EXPECT_FALSE(iframe_request_other_domain_.finished());
  563. }
  564. EXPECT_TRUE(prompt_factory_->is_visible());
  565. Closing();
  566. if (GetParam())
  567. EXPECT_TRUE(request1_.finished());
  568. else
  569. EXPECT_TRUE(iframe_request_other_domain_.finished());
  570. }
  571. ////////////////////////////////////////////////////////////////////////////////
  572. // UMA logging
  573. ////////////////////////////////////////////////////////////////////////////////
  574. // This code path (calling Accept on a non-merged bubble, with no accepted
  575. // permission) would never be used in actual Chrome, but its still tested for
  576. // completeness.
  577. TEST_P(PermissionRequestManagerTest, UMAForSimpleDeniedBubbleAlternatePath) {
  578. base::HistogramTester histograms;
  579. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  580. WaitForBubbleToBeShown();
  581. // No need to test UMA for showing prompts again, they were tested in
  582. // UMAForSimpleAcceptedBubble.
  583. Deny();
  584. histograms.ExpectUniqueSample(PermissionUmaUtil::kPermissionsPromptDenied,
  585. static_cast<base::HistogramBase::Sample>(
  586. RequestTypeForUma::PERMISSION_GEOLOCATION),
  587. 1);
  588. }
  589. TEST_P(PermissionRequestManagerTest, UMAForTabSwitching) {
  590. base::HistogramTester histograms;
  591. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1_);
  592. WaitForBubbleToBeShown();
  593. histograms.ExpectUniqueSample(PermissionUmaUtil::kPermissionsPromptShown,
  594. static_cast<base::HistogramBase::Sample>(
  595. RequestTypeForUma::PERMISSION_GEOLOCATION),
  596. 1);
  597. MockTabSwitchAway();
  598. MockTabSwitchBack();
  599. histograms.ExpectUniqueSample(PermissionUmaUtil::kPermissionsPromptShown,
  600. static_cast<base::HistogramBase::Sample>(
  601. RequestTypeForUma::PERMISSION_GEOLOCATION),
  602. 1);
  603. }
  604. ////////////////////////////////////////////////////////////////////////////////
  605. // UI selectors
  606. ////////////////////////////////////////////////////////////////////////////////
  607. // Simulate a PermissionUiSelector that simply returns a predefined |ui_to_use|
  608. // every time.
  609. class MockNotificationPermissionUiSelector : public PermissionUiSelector {
  610. public:
  611. explicit MockNotificationPermissionUiSelector(
  612. absl::optional<QuietUiReason> quiet_ui_reason,
  613. absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
  614. prediction_likelihood,
  615. bool async)
  616. : quiet_ui_reason_(quiet_ui_reason),
  617. prediction_likelihood_(prediction_likelihood),
  618. async_(async) {}
  619. void SelectUiToUse(PermissionRequest* request,
  620. DecisionMadeCallback callback) override {
  621. Decision decision(quiet_ui_reason_, Decision::ShowNoWarning());
  622. if (async_) {
  623. base::SequencedTaskRunnerHandle::Get()->PostTask(
  624. FROM_HERE, base::BindOnce(std::move(callback), decision));
  625. } else {
  626. std::move(callback).Run(decision);
  627. }
  628. }
  629. bool IsPermissionRequestSupported(RequestType request_type) override {
  630. return request_type == RequestType::kNotifications ||
  631. request_type == RequestType::kGeolocation;
  632. }
  633. absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
  634. PredictedGrantLikelihoodForUKM() override {
  635. return prediction_likelihood_;
  636. }
  637. static void CreateForManager(
  638. PermissionRequestManager* manager,
  639. absl::optional<QuietUiReason> quiet_ui_reason,
  640. bool async,
  641. absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
  642. prediction_likelihood = absl::nullopt) {
  643. manager->add_permission_ui_selector_for_testing(
  644. std::make_unique<MockNotificationPermissionUiSelector>(
  645. quiet_ui_reason, prediction_likelihood, async));
  646. }
  647. private:
  648. absl::optional<QuietUiReason> quiet_ui_reason_;
  649. absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
  650. prediction_likelihood_;
  651. bool async_;
  652. };
  653. // Same as the MockNotificationPermissionUiSelector but handling only the
  654. // Camera stream request type
  655. class MockCameraStreamPermissionUiSelector
  656. : public MockNotificationPermissionUiSelector {
  657. public:
  658. explicit MockCameraStreamPermissionUiSelector(
  659. absl::optional<QuietUiReason> quiet_ui_reason,
  660. absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
  661. prediction_likelihood,
  662. bool async)
  663. : MockNotificationPermissionUiSelector(quiet_ui_reason,
  664. prediction_likelihood,
  665. async) {}
  666. bool IsPermissionRequestSupported(RequestType request_type) override {
  667. return request_type == RequestType::kCameraStream;
  668. }
  669. static void CreateForManager(
  670. PermissionRequestManager* manager,
  671. absl::optional<QuietUiReason> quiet_ui_reason,
  672. bool async,
  673. absl::optional<PermissionUmaUtil::PredictionGrantLikelihood>
  674. prediction_likelihood = absl::nullopt) {
  675. manager->add_permission_ui_selector_for_testing(
  676. std::make_unique<MockCameraStreamPermissionUiSelector>(
  677. quiet_ui_reason, prediction_likelihood, async));
  678. }
  679. };
  680. TEST_P(PermissionRequestManagerTest,
  681. UiSelectorNotUsedForPermissionsOtherThanNotification) {
  682. manager_->clear_permission_ui_selector_for_testing();
  683. MockNotificationPermissionUiSelector::CreateForManager(
  684. manager_, PermissionUiSelector::QuietUiReason::kEnabledInPrefs,
  685. false /* async */);
  686. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request_camera_);
  687. WaitForBubbleToBeShown();
  688. ASSERT_TRUE(prompt_factory_->is_visible());
  689. ASSERT_TRUE(prompt_factory_->RequestTypeSeen(request_camera_.request_type()));
  690. EXPECT_FALSE(manager_->ShouldCurrentRequestUseQuietUI());
  691. Accept();
  692. EXPECT_TRUE(request_camera_.granted());
  693. }
  694. TEST_P(PermissionRequestManagerTest, UiSelectorUsedForNotifications) {
  695. const struct {
  696. absl::optional<PermissionUiSelector::QuietUiReason> quiet_ui_reason;
  697. bool async;
  698. } kTests[] = {
  699. {QuietUiReason::kEnabledInPrefs, true},
  700. {PermissionUiSelector::Decision::UseNormalUi(), true},
  701. {QuietUiReason::kEnabledInPrefs, false},
  702. {PermissionUiSelector::Decision::UseNormalUi(), false},
  703. };
  704. for (const auto& test : kTests) {
  705. manager_->clear_permission_ui_selector_for_testing();
  706. MockNotificationPermissionUiSelector::CreateForManager(
  707. manager_, test.quiet_ui_reason, test.async);
  708. MockPermissionRequest request(RequestType::kNotifications,
  709. PermissionRequestGestureType::GESTURE);
  710. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
  711. WaitForBubbleToBeShown();
  712. EXPECT_TRUE(prompt_factory_->is_visible());
  713. EXPECT_TRUE(prompt_factory_->RequestTypeSeen(request.request_type()));
  714. EXPECT_EQ(!!test.quiet_ui_reason,
  715. manager_->ShouldCurrentRequestUseQuietUI());
  716. Accept();
  717. EXPECT_TRUE(request.granted());
  718. }
  719. }
  720. TEST_P(PermissionRequestManagerTest,
  721. UiSelectionHappensSeparatelyForEachRequest) {
  722. manager_->clear_permission_ui_selector_for_testing();
  723. MockNotificationPermissionUiSelector::CreateForManager(
  724. manager_, QuietUiReason::kEnabledInPrefs, true);
  725. MockPermissionRequest request1(RequestType::kNotifications,
  726. PermissionRequestGestureType::GESTURE);
  727. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request1);
  728. WaitForBubbleToBeShown();
  729. EXPECT_TRUE(manager_->ShouldCurrentRequestUseQuietUI());
  730. Accept();
  731. MockPermissionRequest request2(RequestType::kNotifications,
  732. PermissionRequestGestureType::GESTURE);
  733. manager_->clear_permission_ui_selector_for_testing();
  734. MockNotificationPermissionUiSelector::CreateForManager(
  735. manager_, PermissionUiSelector::Decision::UseNormalUi(), true);
  736. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2);
  737. WaitForBubbleToBeShown();
  738. EXPECT_FALSE(manager_->ShouldCurrentRequestUseQuietUI());
  739. Accept();
  740. }
  741. TEST_P(PermissionRequestManagerTest, MultipleUiSelectors) {
  742. const struct {
  743. std::vector<absl::optional<QuietUiReason>> quiet_ui_reasons;
  744. std::vector<bool> simulate_delayed_decision;
  745. absl::optional<QuietUiReason> expected_reason;
  746. } kTests[] = {
  747. // Simple sync selectors, first one should take priority.
  748. {{QuietUiReason::kTriggeredByCrowdDeny, QuietUiReason::kEnabledInPrefs},
  749. {false, false},
  750. QuietUiReason::kTriggeredByCrowdDeny},
  751. // First selector is async but should still take priority even if it
  752. // returns later.
  753. {{QuietUiReason::kTriggeredByCrowdDeny, QuietUiReason::kEnabledInPrefs},
  754. {true, false},
  755. QuietUiReason::kTriggeredByCrowdDeny},
  756. // The first selector that has a quiet ui decision should be used.
  757. {{absl::nullopt, absl::nullopt,
  758. QuietUiReason::kTriggeredDueToAbusiveContent,
  759. QuietUiReason::kEnabledInPrefs},
  760. {false, true, true, false},
  761. QuietUiReason::kTriggeredDueToAbusiveContent},
  762. // If all selectors return a normal ui, it should use a normal ui.
  763. {{absl::nullopt, absl::nullopt}, {false, true}, absl::nullopt},
  764. // Use a bunch of selectors both async and sync.
  765. {{absl::nullopt, absl::nullopt, absl::nullopt, absl::nullopt,
  766. absl::nullopt, QuietUiReason::kTriggeredDueToAbusiveRequests,
  767. absl::nullopt, QuietUiReason::kEnabledInPrefs},
  768. {false, true, false, true, true, true, false, false},
  769. QuietUiReason::kTriggeredDueToAbusiveRequests},
  770. // Use a bunch of selectors all sync.
  771. {{absl::nullopt, absl::nullopt, absl::nullopt, absl::nullopt,
  772. absl::nullopt, QuietUiReason::kTriggeredDueToAbusiveRequests,
  773. absl::nullopt, QuietUiReason::kEnabledInPrefs},
  774. {false, false, false, false, false, false, false, false},
  775. QuietUiReason::kTriggeredDueToAbusiveRequests},
  776. // Use a bunch of selectors all async.
  777. {{absl::nullopt, absl::nullopt, absl::nullopt, absl::nullopt,
  778. absl::nullopt, QuietUiReason::kTriggeredDueToAbusiveRequests,
  779. absl::nullopt, QuietUiReason::kEnabledInPrefs},
  780. {true, true, true, true, true, true, true, true},
  781. QuietUiReason::kTriggeredDueToAbusiveRequests},
  782. };
  783. for (const auto& test : kTests) {
  784. manager_->clear_permission_ui_selector_for_testing();
  785. for (size_t i = 0; i < test.quiet_ui_reasons.size(); ++i) {
  786. MockNotificationPermissionUiSelector::CreateForManager(
  787. manager_, test.quiet_ui_reasons[i],
  788. test.simulate_delayed_decision[i]);
  789. }
  790. MockPermissionRequest request(RequestType::kNotifications,
  791. PermissionRequestGestureType::GESTURE);
  792. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
  793. WaitForBubbleToBeShown();
  794. EXPECT_TRUE(prompt_factory_->is_visible());
  795. EXPECT_TRUE(prompt_factory_->RequestTypeSeen(request.request_type()));
  796. if (test.expected_reason.has_value()) {
  797. EXPECT_EQ(test.expected_reason, manager_->ReasonForUsingQuietUi());
  798. } else {
  799. EXPECT_FALSE(manager_->ShouldCurrentRequestUseQuietUI());
  800. }
  801. Accept();
  802. EXPECT_TRUE(request.granted());
  803. }
  804. }
  805. TEST_P(PermissionRequestManagerTest, SelectorsPredictionLikelihood) {
  806. using PredictionLikelihood = PermissionUmaUtil::PredictionGrantLikelihood;
  807. const auto VeryLikely = PredictionLikelihood::
  808. PermissionPrediction_Likelihood_DiscretizedLikelihood_VERY_LIKELY;
  809. const auto Neutral = PredictionLikelihood::
  810. PermissionPrediction_Likelihood_DiscretizedLikelihood_NEUTRAL;
  811. const struct {
  812. std::vector<bool> enable_quiet_uis;
  813. std::vector<absl::optional<PredictionLikelihood>> prediction_likelihoods;
  814. absl::optional<PredictionLikelihood> expected_prediction_likelihood;
  815. } kTests[] = {
  816. // Sanity check: prediction likelihood is populated correctly.
  817. {{true}, {VeryLikely}, VeryLikely},
  818. {{false}, {Neutral}, Neutral},
  819. // Prediction likelihood is populated only if the selector was considered.
  820. {{true, true}, {absl::nullopt, VeryLikely}, absl::nullopt},
  821. {{false, true}, {absl::nullopt, VeryLikely}, VeryLikely},
  822. {{false, false}, {absl::nullopt, VeryLikely}, VeryLikely},
  823. // First considered selector is preserved.
  824. {{true, true}, {Neutral, VeryLikely}, Neutral},
  825. {{false, true}, {Neutral, VeryLikely}, Neutral},
  826. {{false, false}, {Neutral, VeryLikely}, Neutral},
  827. };
  828. for (const auto& test : kTests) {
  829. manager_->clear_permission_ui_selector_for_testing();
  830. for (size_t i = 0; i < test.enable_quiet_uis.size(); ++i) {
  831. MockNotificationPermissionUiSelector::CreateForManager(
  832. manager_,
  833. test.enable_quiet_uis[i]
  834. ? absl::optional<QuietUiReason>(QuietUiReason::kEnabledInPrefs)
  835. : absl::nullopt,
  836. false /* async */, test.prediction_likelihoods[i]);
  837. }
  838. MockPermissionRequest request(RequestType::kNotifications,
  839. PermissionRequestGestureType::GESTURE);
  840. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
  841. WaitForBubbleToBeShown();
  842. EXPECT_TRUE(prompt_factory_->is_visible());
  843. EXPECT_TRUE(prompt_factory_->RequestTypeSeen(request.request_type()));
  844. EXPECT_EQ(test.expected_prediction_likelihood,
  845. manager_->prediction_grant_likelihood_for_testing());
  846. Accept();
  847. EXPECT_TRUE(request.granted());
  848. }
  849. }
  850. TEST_P(PermissionRequestManagerTest, SelectorRequestTypes) {
  851. const struct {
  852. RequestType request_type;
  853. bool should_request_use_quiet_ui;
  854. } kTests[] = {
  855. {RequestType::kNotifications, true},
  856. {RequestType::kGeolocation, true},
  857. {RequestType::kCameraStream, false},
  858. };
  859. manager_->clear_permission_ui_selector_for_testing();
  860. MockNotificationPermissionUiSelector::CreateForManager(
  861. manager_, QuietUiReason::kEnabledInPrefs, true);
  862. for (const auto& test : kTests) {
  863. MockPermissionRequest request(test.request_type,
  864. PermissionRequestGestureType::GESTURE);
  865. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
  866. WaitForBubbleToBeShown();
  867. EXPECT_EQ(test.should_request_use_quiet_ui,
  868. manager_->ShouldCurrentRequestUseQuietUI());
  869. Accept();
  870. }
  871. // Adding a mock PermissionUiSelector that handles Camera stream.
  872. MockCameraStreamPermissionUiSelector::CreateForManager(
  873. manager_, QuietUiReason::kEnabledInPrefs, true);
  874. // Now the RequestType::kCameraStream should show a quiet UI as well
  875. MockPermissionRequest request2(RequestType::kCameraStream,
  876. PermissionRequestGestureType::GESTURE);
  877. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request2);
  878. WaitForBubbleToBeShown();
  879. EXPECT_TRUE(manager_->ShouldCurrentRequestUseQuietUI());
  880. Accept();
  881. }
  882. ////////////////////////////////////////////////////////////////////////////////
  883. // Quiet UI chip. Low priority for Notifications & Geolocation.
  884. ////////////////////////////////////////////////////////////////////////////////
  885. TEST_P(PermissionRequestManagerTest, NotificationsSingleBubbleAndChipRequest) {
  886. MockPermissionRequest request(RequestType::kNotifications,
  887. PermissionRequestGestureType::GESTURE);
  888. manager_->AddRequest(web_contents()->GetPrimaryMainFrame(), &request);
  889. WaitForBubbleToBeShown();
  890. EXPECT_TRUE(prompt_factory_->is_visible());
  891. ASSERT_EQ(prompt_factory_->request_count(), 1);
  892. Accept();
  893. EXPECT_TRUE(request.granted());
  894. EXPECT_EQ(prompt_factory_->show_count(), 1);
  895. }
  896. // Quiet UI feature is disabled. Chip is disabled. No low priority requests, the
  897. // first request is always shown.
  898. //
  899. // Permissions requested in order:
  900. // 1. Notification (non abusive)
  901. // 2. Geolocation
  902. // 3. Camera
  903. //
  904. // Prompt display order:
  905. // 1. Notification request shown
  906. // 2. Geolocation request shown
  907. // 3. Camera request shown
  908. TEST_P(PermissionRequestManagerTest,
  909. NotificationsGeolocationCameraBubbleRequest) {
  910. // permissions::features::kPermissionChip is enabled based on `GetParam()`.
  911. // That test is only for the default bubble.
  912. if (GetParam())
  913. return;
  914. std::unique_ptr<MockPermissionRequest> request_notifications =
  915. CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
  916. 1);
  917. std::unique_ptr<MockPermissionRequest> request_geolocation =
  918. CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/false,
  919. 1);
  920. std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
  921. RequestType::kCameraStream, /*should_be_seen=*/false, 1);
  922. for (auto* kRequest : {request_notifications.get(), request_geolocation.get(),
  923. request_camera.get()}) {
  924. WaitAndAcceptPromptForRequest(kRequest);
  925. }
  926. EXPECT_EQ(prompt_factory_->show_count(), 3);
  927. }
  928. // Quiet UI feature is disabled, no low priority requests, the last request is
  929. // always shown.
  930. //
  931. // Permissions requested in order:
  932. // 1. Notification (non abusive)
  933. // 2. Geolocation
  934. // 3. Camera
  935. //
  936. // Prompt display order:
  937. // 1. Notifications request shown but is preempted
  938. // 2. Geolocation request shown but is preempted
  939. // 3. Camera request shown
  940. // 4. Geolocation request shown again
  941. // 5. Notifications request shown again
  942. TEST_P(PermissionRequestManagerTest,
  943. NotificationsGeolocationCameraChipRequest) {
  944. // permissions::features::kPermissionChip is enabled based on `GetParam()`.
  945. // That test is only for the chip UI.
  946. if (!GetParam())
  947. return;
  948. std::unique_ptr<MockPermissionRequest> request_notifications =
  949. CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
  950. 1);
  951. std::unique_ptr<MockPermissionRequest> request_geolocation =
  952. CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
  953. 2);
  954. std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
  955. RequestType::kCameraStream, /*should_be_seen=*/true, 3);
  956. for (auto* kRequest : {request_camera.get(), request_geolocation.get(),
  957. request_notifications.get()}) {
  958. WaitAndAcceptPromptForRequest(kRequest);
  959. }
  960. EXPECT_EQ(prompt_factory_->show_count(), 5);
  961. }
  962. // Quiet UI feature is disabled, no low priority requests, the last request is
  963. // always shown.
  964. //
  965. // Permissions requested in order:
  966. // 1. Camera
  967. // 2. Notification (non abusive)
  968. // 3. Geolocation
  969. //
  970. // Prompt display order:
  971. // 1. Camera request shown but is preempted
  972. // 2. Notifications request shown but is preempted
  973. // 3. Geolocation request shown
  974. // 4. Notifications request shown again
  975. // 5. Camera request shown again
  976. TEST_P(PermissionRequestManagerTest,
  977. CameraNotificationsGeolocationChipRequest) {
  978. // permissions::features::kPermissionChip is enabled based on `GetParam()`.
  979. // That test is only for the chip.
  980. if (!GetParam())
  981. return;
  982. std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
  983. RequestType::kCameraStream, /*should_be_seen=*/true, 1);
  984. std::unique_ptr<MockPermissionRequest> request_notifications =
  985. CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
  986. 2);
  987. std::unique_ptr<MockPermissionRequest> request_geolocation =
  988. CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
  989. 3);
  990. for (auto* kRequest : {request_geolocation.get(), request_notifications.get(),
  991. request_camera.get()}) {
  992. WaitAndAcceptPromptForRequest(kRequest);
  993. }
  994. EXPECT_EQ(prompt_factory_->show_count(), 5);
  995. }
  996. class PermissionRequestManagerTestQuietChip
  997. : public PermissionRequestManagerTest {
  998. public:
  999. PermissionRequestManagerTestQuietChip() {
  1000. feature_list_.InitWithFeatureState(
  1001. permissions::features::kPermissionQuietChip, true);
  1002. }
  1003. private:
  1004. base::test::ScopedFeatureList feature_list_;
  1005. };
  1006. // Verifies that the quiet UI chip is not ignored if another request came in
  1007. // less than 8.5 seconds after.
  1008. // Permissions requested in order:
  1009. // 1. Notification (abusive)
  1010. // 2. After less than 8.5 seconds Geolocation
  1011. //
  1012. // Prompt display order:
  1013. // 1. Notifications request shown but is preempted because of quiet UI.
  1014. // 2. Geolocation request shown
  1015. // 3. Notifications request shown again
  1016. TEST_P(PermissionRequestManagerTestQuietChip,
  1017. AbusiveNotificationsGeolocationQuietUIChipRequest) {
  1018. MockNotificationPermissionUiSelector::CreateForManager(
  1019. manager_,
  1020. PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
  1021. false /* async */);
  1022. std::unique_ptr<MockPermissionRequest> request_notifications =
  1023. CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
  1024. 1);
  1025. // Less then 8.5 seconds.
  1026. manager_->set_current_request_first_display_time_for_testing(
  1027. base::Time::Now() - base::Milliseconds(5000));
  1028. std::unique_ptr<MockPermissionRequest> request_geolocation =
  1029. CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
  1030. 2);
  1031. WaitAndAcceptPromptForRequest(request_geolocation.get());
  1032. WaitAndAcceptPromptForRequest(request_notifications.get());
  1033. EXPECT_EQ(prompt_factory_->show_count(), 3);
  1034. }
  1035. // Verifies that the quiet UI chip is ignored if another request came in more
  1036. // than 8.5 seconds after.
  1037. //
  1038. // Permissions requested in order:
  1039. // 1. Notification (abusive)
  1040. // 2. After more than 8.5 seconds Geolocation
  1041. //
  1042. // Prompt display order:
  1043. // 1. Notifications request shown but is preempted because of quiet UI.
  1044. // 2. Geolocation request shown
  1045. TEST_P(PermissionRequestManagerTestQuietChip,
  1046. AbusiveNotificationsShownLongEnough) {
  1047. MockNotificationPermissionUiSelector::CreateForManager(
  1048. manager_,
  1049. PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
  1050. false /* async */);
  1051. std::unique_ptr<MockPermissionRequest> request_notifications =
  1052. CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
  1053. 1);
  1054. // More then 8.5 seconds.
  1055. manager_->set_current_request_first_display_time_for_testing(
  1056. base::Time::Now() - base::Milliseconds(9000));
  1057. std::unique_ptr<MockPermissionRequest> request_geolocation =
  1058. CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
  1059. 2);
  1060. // The second permission was requested after 8.5 second window, the quiet UI
  1061. // Notifiations request for an abusive origin is automatically ignored.
  1062. EXPECT_FALSE(request_notifications->granted());
  1063. EXPECT_TRUE(request_notifications->finished());
  1064. WaitAndAcceptPromptForRequest(request_geolocation.get());
  1065. EXPECT_EQ(prompt_factory_->show_count(), 2);
  1066. }
  1067. // Verifies that the quiet UI chip is not ignored if another request came in
  1068. // more than 8.5 seconds after. Verify different requests priority. Camera
  1069. // request is shown despite being requested last.
  1070. //
  1071. // Permissions requested in order:
  1072. // 1. Notification (abusive)
  1073. // 2. After less than 8.5 seconds Geolocation
  1074. // 3. Camera
  1075. //
  1076. // Prompt display order:
  1077. // 1. Notifications request shown but is preempted because of quiet UI.
  1078. // 2. Geolocation request shown but is preempted because of low priority.
  1079. // 3. Camera request shown
  1080. // 4. Geolocation request shown again
  1081. // 5. Notifications quiet UI request shown again
  1082. TEST_P(PermissionRequestManagerTestQuietChip,
  1083. AbusiveNotificationsShownLongEnoughCamera) {
  1084. MockNotificationPermissionUiSelector::CreateForManager(
  1085. manager_,
  1086. PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
  1087. false /* async */);
  1088. std::unique_ptr<MockPermissionRequest> request_notifications =
  1089. CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/true,
  1090. 1);
  1091. // Less then 8.5 seconds.
  1092. manager_->set_current_request_first_display_time_for_testing(
  1093. base::Time::Now() - base::Milliseconds(5000));
  1094. std::unique_ptr<MockPermissionRequest> request_geolocation =
  1095. CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/true,
  1096. 2);
  1097. std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
  1098. RequestType::kCameraStream, /*should_be_seen=*/true, 3);
  1099. // The second permission was requested in 8.5 second window, the quiet UI
  1100. // Notifiations request for an abusive origin is not automatically ignored.
  1101. EXPECT_FALSE(request_notifications->granted());
  1102. EXPECT_FALSE(request_notifications->finished());
  1103. for (auto* kRequest : {request_camera.get(), request_geolocation.get(),
  1104. request_notifications.get()}) {
  1105. WaitAndAcceptPromptForRequest(kRequest);
  1106. }
  1107. EXPECT_EQ(prompt_factory_->show_count(), 5);
  1108. }
  1109. // Verifies that the quiet UI chip is not ignored if another request came in
  1110. // more than 8.5 seconds after. Verify different requests priority. Camera
  1111. // request is not preemted.
  1112. //
  1113. // Permissions requested in order:
  1114. // 1. Camera
  1115. // 2. Notification (abusive)
  1116. // 3. After less than 8.5 seconds Geolocation
  1117. //
  1118. // Prompt display order:
  1119. // 1. Camera request shown
  1120. // 2. Geolocation request shown
  1121. // 3. Camera request shown
  1122. TEST_P(PermissionRequestManagerTestQuietChip,
  1123. CameraAbusiveNotificationsGeolocation) {
  1124. MockNotificationPermissionUiSelector::CreateForManager(
  1125. manager_,
  1126. PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
  1127. false /* async */);
  1128. std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
  1129. RequestType::kCameraStream, /*should_be_seen=*/true, 1);
  1130. // Quiet UI is not shown because Camera has higher priority.
  1131. std::unique_ptr<MockPermissionRequest> request_notifications =
  1132. CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/false,
  1133. 1);
  1134. // Less then 8.5 seconds.
  1135. manager_->set_current_request_first_display_time_for_testing(
  1136. base::Time::Now() - base::Milliseconds(5000));
  1137. // Geolocation is not shown because Camera has higher priority.
  1138. std::unique_ptr<MockPermissionRequest> request_geolocation =
  1139. CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/false,
  1140. 1);
  1141. // The second permission after quiet UI was requested in 8.5 second window,
  1142. // the quiet UI Notifiations request for an abusive origin is not
  1143. // automatically ignored.
  1144. EXPECT_FALSE(request_notifications->granted());
  1145. EXPECT_FALSE(request_notifications->finished());
  1146. for (auto* kRequest : {request_camera.get(), request_geolocation.get(),
  1147. request_notifications.get()}) {
  1148. WaitAndAcceptPromptForRequest(kRequest);
  1149. }
  1150. EXPECT_EQ(prompt_factory_->show_count(), 3);
  1151. }
  1152. // Verifies that the quiet UI chip is not ignored if another request came in
  1153. // more than 8.5 seconds after. Verify different requests priority. Camera
  1154. // request is not preemted.
  1155. //
  1156. // Permissions requested in order:
  1157. // 1. Camera
  1158. // 2. Notification (abusive)
  1159. // 3. After less than 8.5 seconds Geolocation
  1160. // 4. MIDI
  1161. //
  1162. // Prompt display order:
  1163. // 1. Camera request shown
  1164. // 2. MIDI request shown (or MIDI and then Camera, the order depends on
  1165. // `GetParam()`)
  1166. // 3. Geolocation request shown
  1167. // 4. Notifications request shown
  1168. // If Chip is enabled MIDI will replace Camera, hence 5 prompts will be
  1169. // shown. Otherwise 4.
  1170. TEST_P(PermissionRequestManagerTestQuietChip,
  1171. CameraAbusiveNotificationsGeolocationMIDI) {
  1172. MockNotificationPermissionUiSelector::CreateForManager(
  1173. manager_,
  1174. PermissionUiSelector::QuietUiReason::kTriggeredDueToAbusiveRequests,
  1175. false /* async */);
  1176. std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
  1177. RequestType::kCameraStream, /*should_be_seen=*/true, 1);
  1178. // Quiet UI is not shown because Camera has higher priority.
  1179. std::unique_ptr<MockPermissionRequest> request_notifications =
  1180. CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/false,
  1181. 1);
  1182. // Less then 8.5 seconds.
  1183. manager_->set_current_request_first_display_time_for_testing(
  1184. base::Time::Now() - base::Milliseconds(5000));
  1185. // Geolocation is not shown because Camera has higher priority.
  1186. std::unique_ptr<MockPermissionRequest> request_geolocation =
  1187. CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/false,
  1188. 1);
  1189. std::unique_ptr<MockPermissionRequest> request_midi;
  1190. // If Chip is enabled, MIDI should be shown, otherwise MIDI should not be
  1191. // shown.
  1192. if (GetParam()) {
  1193. request_midi = CreateAndAddRequest(RequestType::kMidiSysex,
  1194. /*should_be_seen=*/true, 2);
  1195. } else {
  1196. request_midi = CreateAndAddRequest(RequestType::kMidiSysex,
  1197. /*should_be_seen=*/false, 1);
  1198. }
  1199. // The second permission after quiet UI was requested in 8.5 second window,
  1200. // the quiet UI Notifiations request for an abusive origin is not
  1201. // automatically ignored.
  1202. EXPECT_FALSE(request_notifications->granted());
  1203. EXPECT_FALSE(request_notifications->finished());
  1204. WaitAndAcceptPromptForRequest(GetParam() ? request_midi.get()
  1205. : request_camera.get());
  1206. WaitAndAcceptPromptForRequest(GetParam() ? request_camera.get()
  1207. : request_midi.get());
  1208. WaitAndAcceptPromptForRequest(request_geolocation.get());
  1209. WaitAndAcceptPromptForRequest(request_notifications.get());
  1210. EXPECT_EQ(prompt_factory_->show_count(), GetParam() ? 5 : 4);
  1211. }
  1212. // Verifies that non abusive chip behaves similar to others when Quiet UI Chip
  1213. // is enabled.
  1214. //
  1215. // Permissions requested in order:
  1216. // 1. Camera
  1217. // 2. Notification (non abusive)
  1218. // 3. After less than 8.5 seconds Geolocation
  1219. // 4. MIDI
  1220. //
  1221. // Prompt display order:
  1222. // 1. Camera request shown
  1223. // 2. MIDI request shown (or MIDI and then Camera, the order depends on
  1224. // `GetParam()`)
  1225. // 3. Geolocation request shown
  1226. // 4. Notifications request shown
  1227. // If Chip is enabled MIDI will replace Camera, hence 5 prompts will be
  1228. // shown. Otherwise 4.
  1229. TEST_P(PermissionRequestManagerTestQuietChip,
  1230. CameraNonAbusiveNotificationsGeolocationMIDI) {
  1231. std::unique_ptr<MockPermissionRequest> request_camera = CreateAndAddRequest(
  1232. RequestType::kCameraStream, /*should_be_seen=*/true, 1);
  1233. // Quiet UI is not shown because Camera has higher priority.
  1234. std::unique_ptr<MockPermissionRequest> request_notifications =
  1235. CreateAndAddRequest(RequestType::kNotifications, /*should_be_seen=*/false,
  1236. 1);
  1237. // Less then 8.5 seconds.
  1238. manager_->set_current_request_first_display_time_for_testing(
  1239. base::Time::Now() - base::Milliseconds(5000));
  1240. // Geolocation is not shown because Camera has higher priority.
  1241. std::unique_ptr<MockPermissionRequest> request_geolocation =
  1242. CreateAndAddRequest(RequestType::kGeolocation, /*should_be_seen=*/false,
  1243. 1);
  1244. std::unique_ptr<MockPermissionRequest> request_midi;
  1245. // If Chip is enabled, MIDI should be shown, otherwise MIDI should not be
  1246. // shown.
  1247. if (GetParam()) {
  1248. request_midi = CreateAndAddRequest(RequestType::kMidiSysex,
  1249. /*should_be_seen=*/true, 2);
  1250. } else {
  1251. request_midi = CreateAndAddRequest(RequestType::kMidiSysex,
  1252. /*should_be_seen=*/false, 1);
  1253. }
  1254. // The second permission after quiet UI was requested in 8.5 second window,
  1255. // the quiet UI Notifiations request for an abusive origin is not
  1256. // automatically ignored.
  1257. EXPECT_FALSE(request_notifications->granted());
  1258. EXPECT_FALSE(request_notifications->finished());
  1259. WaitAndAcceptPromptForRequest(GetParam() ? request_midi.get()
  1260. : request_camera.get());
  1261. WaitAndAcceptPromptForRequest(GetParam() ? request_camera.get()
  1262. : request_midi.get());
  1263. WaitAndAcceptPromptForRequest(request_geolocation.get());
  1264. WaitAndAcceptPromptForRequest(request_notifications.get());
  1265. EXPECT_EQ(prompt_factory_->show_count(), GetParam() ? 5 : 4);
  1266. }
  1267. INSTANTIATE_TEST_SUITE_P(All,
  1268. PermissionRequestManagerTest,
  1269. ::testing::Values(false, true));
  1270. INSTANTIATE_TEST_SUITE_P(All,
  1271. PermissionRequestManagerTestQuietChip,
  1272. ::testing::Values(false, true));
  1273. } // namespace permissions