1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.systemui.mediaprojection.devicepolicy 17 18 import android.app.admin.DevicePolicyManager 19 import android.os.UserHandle 20 import android.os.UserManager 21 import androidx.test.filters.SmallTest 22 import com.android.systemui.SysuiTestCase 23 import com.android.systemui.util.mockito.eq 24 import com.android.systemui.util.mockito.mock 25 import com.android.systemui.util.mockito.whenever 26 import com.google.common.truth.Truth.assertWithMessage 27 import org.junit.Before 28 import platform.test.runner.parameterized.ParameterizedAndroidJunit4 29 import platform.test.runner.parameterized.Parameters 30 import platform.test.runner.parameterized.Parameter 31 import org.junit.Test 32 import org.junit.runner.RunWith 33 import org.mockito.ArgumentMatchers.any 34 35 abstract class BaseScreenCaptureDevicePolicyResolverTest(private val precondition: Preconditions) : 36 SysuiTestCase() { 37 38 abstract class Preconditions( 39 val personalScreenCaptureDisabled: Boolean, 40 val workScreenCaptureDisabled: Boolean, 41 val disallowShareIntoManagedProfile: Boolean 42 ) 43 44 protected val devicePolicyManager: DevicePolicyManager = mock() 45 protected val userManager: UserManager = mock() 46 47 protected val personalUserHandle: UserHandle = UserHandle.of(123) 48 protected val workUserHandle: UserHandle = UserHandle.of(456) 49 50 protected val policyResolver = 51 ScreenCaptureDevicePolicyResolver( 52 devicePolicyManager, 53 userManager, 54 personalUserHandle, 55 workUserHandle 56 ) 57 58 @Before setUpnull59 fun setUp() { 60 setUpPolicies() 61 } 62 setUpPoliciesnull63 private fun setUpPolicies() { 64 whenever( 65 devicePolicyManager.getScreenCaptureDisabled( 66 any(), 67 eq(personalUserHandle.identifier) 68 ) 69 ) 70 .thenReturn(precondition.personalScreenCaptureDisabled) 71 72 whenever(devicePolicyManager.getScreenCaptureDisabled(any(), eq(workUserHandle.identifier))) 73 .thenReturn(precondition.workScreenCaptureDisabled) 74 75 whenever( 76 userManager.hasUserRestrictionForUser( 77 eq(UserManager.DISALLOW_SHARE_INTO_MANAGED_PROFILE), 78 eq(workUserHandle) 79 ) 80 ) 81 .thenReturn(precondition.disallowShareIntoManagedProfile) 82 } 83 } 84 85 @RunWith(ParameterizedAndroidJunit4::class) 86 @SmallTest 87 class IsAllowedScreenCaptureDevicePolicyResolverTest( 88 private val test: IsScreenCaptureAllowedTestCase 89 ) : BaseScreenCaptureDevicePolicyResolverTest(test.given) { 90 91 companion object { 92 @Parameters(name = "{0}") 93 @JvmStatic getParamsnull94 fun getParams() = 95 listOf( 96 IsScreenCaptureAllowedTestCase( 97 given = 98 Preconditions( 99 isHostInWorkProfile = false, 100 isTargetInWorkProfile = false, 101 personalScreenCaptureDisabled = false, 102 workScreenCaptureDisabled = false, 103 disallowShareIntoManagedProfile = false, 104 ), 105 expectedScreenCaptureAllowed = true, 106 ), 107 IsScreenCaptureAllowedTestCase( 108 given = 109 Preconditions( 110 isHostInWorkProfile = false, 111 isTargetInWorkProfile = false, 112 personalScreenCaptureDisabled = false, 113 workScreenCaptureDisabled = false, 114 disallowShareIntoManagedProfile = true, 115 ), 116 expectedScreenCaptureAllowed = true, 117 ), 118 IsScreenCaptureAllowedTestCase( 119 given = 120 Preconditions( 121 isHostInWorkProfile = false, 122 isTargetInWorkProfile = false, 123 personalScreenCaptureDisabled = false, 124 workScreenCaptureDisabled = true, 125 disallowShareIntoManagedProfile = false, 126 ), 127 expectedScreenCaptureAllowed = true, 128 ), 129 IsScreenCaptureAllowedTestCase( 130 given = 131 Preconditions( 132 isHostInWorkProfile = false, 133 isTargetInWorkProfile = false, 134 personalScreenCaptureDisabled = false, 135 workScreenCaptureDisabled = true, 136 disallowShareIntoManagedProfile = true, 137 ), 138 expectedScreenCaptureAllowed = true, 139 ), 140 IsScreenCaptureAllowedTestCase( 141 given = 142 Preconditions( 143 isHostInWorkProfile = false, 144 isTargetInWorkProfile = false, 145 personalScreenCaptureDisabled = true, 146 workScreenCaptureDisabled = false, 147 disallowShareIntoManagedProfile = false 148 ), 149 expectedScreenCaptureAllowed = false, 150 ), 151 IsScreenCaptureAllowedTestCase( 152 given = 153 Preconditions( 154 isHostInWorkProfile = false, 155 isTargetInWorkProfile = false, 156 personalScreenCaptureDisabled = true, 157 workScreenCaptureDisabled = false, 158 disallowShareIntoManagedProfile = true 159 ), 160 expectedScreenCaptureAllowed = false, 161 ), 162 IsScreenCaptureAllowedTestCase( 163 given = 164 Preconditions( 165 isHostInWorkProfile = false, 166 isTargetInWorkProfile = false, 167 personalScreenCaptureDisabled = true, 168 workScreenCaptureDisabled = true, 169 disallowShareIntoManagedProfile = false 170 ), 171 expectedScreenCaptureAllowed = false, 172 ), 173 IsScreenCaptureAllowedTestCase( 174 given = 175 Preconditions( 176 isHostInWorkProfile = false, 177 isTargetInWorkProfile = false, 178 personalScreenCaptureDisabled = true, 179 workScreenCaptureDisabled = true, 180 disallowShareIntoManagedProfile = true 181 ), 182 expectedScreenCaptureAllowed = false, 183 ), 184 IsScreenCaptureAllowedTestCase( 185 given = 186 Preconditions( 187 isHostInWorkProfile = false, 188 isTargetInWorkProfile = true, 189 personalScreenCaptureDisabled = false, 190 workScreenCaptureDisabled = false, 191 disallowShareIntoManagedProfile = false 192 ), 193 expectedScreenCaptureAllowed = true, 194 ), 195 IsScreenCaptureAllowedTestCase( 196 given = 197 Preconditions( 198 isHostInWorkProfile = false, 199 isTargetInWorkProfile = true, 200 personalScreenCaptureDisabled = false, 201 workScreenCaptureDisabled = false, 202 disallowShareIntoManagedProfile = true 203 ), 204 expectedScreenCaptureAllowed = true, 205 ), 206 IsScreenCaptureAllowedTestCase( 207 given = 208 Preconditions( 209 isHostInWorkProfile = false, 210 isTargetInWorkProfile = true, 211 personalScreenCaptureDisabled = false, 212 workScreenCaptureDisabled = true, 213 disallowShareIntoManagedProfile = false 214 ), 215 expectedScreenCaptureAllowed = false, 216 ), 217 IsScreenCaptureAllowedTestCase( 218 given = 219 Preconditions( 220 isHostInWorkProfile = false, 221 isTargetInWorkProfile = true, 222 personalScreenCaptureDisabled = false, 223 workScreenCaptureDisabled = true, 224 disallowShareIntoManagedProfile = true 225 ), 226 expectedScreenCaptureAllowed = false, 227 ), 228 IsScreenCaptureAllowedTestCase( 229 given = 230 Preconditions( 231 isHostInWorkProfile = false, 232 isTargetInWorkProfile = true, 233 personalScreenCaptureDisabled = true, 234 workScreenCaptureDisabled = false, 235 disallowShareIntoManagedProfile = false 236 ), 237 expectedScreenCaptureAllowed = false, 238 ), 239 IsScreenCaptureAllowedTestCase( 240 given = 241 Preconditions( 242 isHostInWorkProfile = false, 243 isTargetInWorkProfile = true, 244 personalScreenCaptureDisabled = true, 245 workScreenCaptureDisabled = false, 246 disallowShareIntoManagedProfile = true 247 ), 248 expectedScreenCaptureAllowed = false, 249 ), 250 IsScreenCaptureAllowedTestCase( 251 given = 252 Preconditions( 253 isHostInWorkProfile = false, 254 isTargetInWorkProfile = true, 255 personalScreenCaptureDisabled = true, 256 workScreenCaptureDisabled = true, 257 disallowShareIntoManagedProfile = false 258 ), 259 expectedScreenCaptureAllowed = false, 260 ), 261 IsScreenCaptureAllowedTestCase( 262 given = 263 Preconditions( 264 isHostInWorkProfile = false, 265 isTargetInWorkProfile = true, 266 personalScreenCaptureDisabled = true, 267 workScreenCaptureDisabled = true, 268 disallowShareIntoManagedProfile = true 269 ), 270 expectedScreenCaptureAllowed = false, 271 ), 272 IsScreenCaptureAllowedTestCase( 273 given = 274 Preconditions( 275 isHostInWorkProfile = true, 276 isTargetInWorkProfile = false, 277 personalScreenCaptureDisabled = false, 278 workScreenCaptureDisabled = false, 279 disallowShareIntoManagedProfile = false 280 ), 281 expectedScreenCaptureAllowed = true, 282 ), 283 IsScreenCaptureAllowedTestCase( 284 given = 285 Preconditions( 286 isHostInWorkProfile = true, 287 isTargetInWorkProfile = false, 288 personalScreenCaptureDisabled = false, 289 workScreenCaptureDisabled = false, 290 disallowShareIntoManagedProfile = true 291 ), 292 expectedScreenCaptureAllowed = false, 293 ), 294 IsScreenCaptureAllowedTestCase( 295 given = 296 Preconditions( 297 isHostInWorkProfile = true, 298 isTargetInWorkProfile = false, 299 personalScreenCaptureDisabled = false, 300 workScreenCaptureDisabled = true, 301 disallowShareIntoManagedProfile = false 302 ), 303 expectedScreenCaptureAllowed = false, 304 ), 305 IsScreenCaptureAllowedTestCase( 306 given = 307 Preconditions( 308 isHostInWorkProfile = true, 309 isTargetInWorkProfile = false, 310 personalScreenCaptureDisabled = false, 311 workScreenCaptureDisabled = true, 312 disallowShareIntoManagedProfile = true 313 ), 314 expectedScreenCaptureAllowed = false, 315 ), 316 IsScreenCaptureAllowedTestCase( 317 given = 318 Preconditions( 319 isHostInWorkProfile = true, 320 isTargetInWorkProfile = false, 321 personalScreenCaptureDisabled = true, 322 workScreenCaptureDisabled = false, 323 disallowShareIntoManagedProfile = false 324 ), 325 expectedScreenCaptureAllowed = false, 326 ), 327 IsScreenCaptureAllowedTestCase( 328 given = 329 Preconditions( 330 isHostInWorkProfile = true, 331 isTargetInWorkProfile = false, 332 personalScreenCaptureDisabled = true, 333 workScreenCaptureDisabled = false, 334 disallowShareIntoManagedProfile = true 335 ), 336 expectedScreenCaptureAllowed = false, 337 ), 338 IsScreenCaptureAllowedTestCase( 339 given = 340 Preconditions( 341 isHostInWorkProfile = true, 342 isTargetInWorkProfile = false, 343 personalScreenCaptureDisabled = true, 344 workScreenCaptureDisabled = true, 345 disallowShareIntoManagedProfile = false 346 ), 347 expectedScreenCaptureAllowed = false, 348 ), 349 IsScreenCaptureAllowedTestCase( 350 given = 351 Preconditions( 352 isHostInWorkProfile = true, 353 isTargetInWorkProfile = false, 354 personalScreenCaptureDisabled = true, 355 workScreenCaptureDisabled = true, 356 disallowShareIntoManagedProfile = true 357 ), 358 expectedScreenCaptureAllowed = false, 359 ), 360 IsScreenCaptureAllowedTestCase( 361 given = 362 Preconditions( 363 isHostInWorkProfile = true, 364 isTargetInWorkProfile = true, 365 personalScreenCaptureDisabled = false, 366 workScreenCaptureDisabled = false, 367 disallowShareIntoManagedProfile = false 368 ), 369 expectedScreenCaptureAllowed = true, 370 ), 371 IsScreenCaptureAllowedTestCase( 372 given = 373 Preconditions( 374 isHostInWorkProfile = true, 375 isTargetInWorkProfile = true, 376 personalScreenCaptureDisabled = false, 377 workScreenCaptureDisabled = false, 378 disallowShareIntoManagedProfile = true 379 ), 380 expectedScreenCaptureAllowed = true, 381 ), 382 IsScreenCaptureAllowedTestCase( 383 given = 384 Preconditions( 385 isHostInWorkProfile = true, 386 isTargetInWorkProfile = true, 387 personalScreenCaptureDisabled = false, 388 workScreenCaptureDisabled = true, 389 disallowShareIntoManagedProfile = false 390 ), 391 expectedScreenCaptureAllowed = false, 392 ), 393 IsScreenCaptureAllowedTestCase( 394 given = 395 Preconditions( 396 isHostInWorkProfile = true, 397 isTargetInWorkProfile = true, 398 personalScreenCaptureDisabled = false, 399 workScreenCaptureDisabled = true, 400 disallowShareIntoManagedProfile = true 401 ), 402 expectedScreenCaptureAllowed = false, 403 ), 404 IsScreenCaptureAllowedTestCase( 405 given = 406 Preconditions( 407 isHostInWorkProfile = true, 408 isTargetInWorkProfile = true, 409 personalScreenCaptureDisabled = true, 410 workScreenCaptureDisabled = true, 411 disallowShareIntoManagedProfile = false 412 ), 413 expectedScreenCaptureAllowed = false, 414 ), 415 IsScreenCaptureAllowedTestCase( 416 given = 417 Preconditions( 418 isHostInWorkProfile = true, 419 isTargetInWorkProfile = true, 420 personalScreenCaptureDisabled = true, 421 workScreenCaptureDisabled = true, 422 disallowShareIntoManagedProfile = true 423 ), 424 expectedScreenCaptureAllowed = false, 425 ), 426 ) 427 } 428 429 class Preconditions( 430 personalScreenCaptureDisabled: Boolean, 431 workScreenCaptureDisabled: Boolean, 432 disallowShareIntoManagedProfile: Boolean, 433 val isHostInWorkProfile: Boolean, 434 val isTargetInWorkProfile: Boolean, 435 ) : 436 BaseScreenCaptureDevicePolicyResolverTest.Preconditions( 437 personalScreenCaptureDisabled, 438 workScreenCaptureDisabled, 439 disallowShareIntoManagedProfile 440 ) 441 442 data class IsScreenCaptureAllowedTestCase( 443 val given: Preconditions, 444 val expectedScreenCaptureAllowed: Boolean 445 ) { 446 override fun toString(): String = 447 "isScreenCaptureAllowed: " + 448 "host[${if (given.isHostInWorkProfile) "work" else "personal"} profile], " + 449 "target[${if (given.isTargetInWorkProfile) "work" else "personal"} profile], " + 450 "personal screen capture disabled = ${given.personalScreenCaptureDisabled}, " + 451 "work screen capture disabled = ${given.workScreenCaptureDisabled}, " + 452 "disallow share into managed profile = ${given.disallowShareIntoManagedProfile}, " + 453 "expected screen capture allowed = $expectedScreenCaptureAllowed" 454 } 455 456 @Test testnull457 fun test() { 458 val targetAppUserHandle = 459 if (test.given.isTargetInWorkProfile) workUserHandle else personalUserHandle 460 val hostAppUserHandle = 461 if (test.given.isHostInWorkProfile) workUserHandle else personalUserHandle 462 463 val screenCaptureAllowed = 464 policyResolver.isScreenCaptureAllowed(targetAppUserHandle, hostAppUserHandle) 465 466 assertWithMessage("Screen capture policy resolved incorrectly") 467 .that(screenCaptureAllowed) 468 .isEqualTo(test.expectedScreenCaptureAllowed) 469 } 470 } 471 472 @RunWith(ParameterizedAndroidJunit4::class) 473 @SmallTest 474 class IsCompletelyNotAllowedScreenCaptureDevicePolicyResolverTest( 475 private val test: IsScreenCaptureCompletelyDisabledTestCase 476 ) : BaseScreenCaptureDevicePolicyResolverTest(test.given) { 477 478 companion object { 479 @Parameters(name = "{0}") 480 @JvmStatic getParamsnull481 fun getParams() = 482 listOf( 483 IsScreenCaptureCompletelyDisabledTestCase( 484 given = 485 Preconditions( 486 isHostInWorkProfile = false, 487 personalScreenCaptureDisabled = false, 488 workScreenCaptureDisabled = false, 489 disallowShareIntoManagedProfile = false 490 ), 491 expectedScreenCaptureCompletelyDisabled = false, 492 ), 493 IsScreenCaptureCompletelyDisabledTestCase( 494 given = 495 Preconditions( 496 isHostInWorkProfile = false, 497 personalScreenCaptureDisabled = false, 498 workScreenCaptureDisabled = false, 499 disallowShareIntoManagedProfile = true 500 ), 501 expectedScreenCaptureCompletelyDisabled = false, 502 ), 503 IsScreenCaptureCompletelyDisabledTestCase( 504 given = 505 Preconditions( 506 isHostInWorkProfile = false, 507 personalScreenCaptureDisabled = false, 508 workScreenCaptureDisabled = true, 509 disallowShareIntoManagedProfile = false 510 ), 511 expectedScreenCaptureCompletelyDisabled = false, 512 ), 513 IsScreenCaptureCompletelyDisabledTestCase( 514 given = 515 Preconditions( 516 isHostInWorkProfile = false, 517 personalScreenCaptureDisabled = false, 518 workScreenCaptureDisabled = true, 519 disallowShareIntoManagedProfile = true 520 ), 521 expectedScreenCaptureCompletelyDisabled = false, 522 ), 523 IsScreenCaptureCompletelyDisabledTestCase( 524 given = 525 Preconditions( 526 isHostInWorkProfile = false, 527 personalScreenCaptureDisabled = true, 528 workScreenCaptureDisabled = false, 529 disallowShareIntoManagedProfile = false 530 ), 531 expectedScreenCaptureCompletelyDisabled = true, 532 ), 533 IsScreenCaptureCompletelyDisabledTestCase( 534 given = 535 Preconditions( 536 isHostInWorkProfile = false, 537 personalScreenCaptureDisabled = true, 538 workScreenCaptureDisabled = false, 539 disallowShareIntoManagedProfile = true 540 ), 541 expectedScreenCaptureCompletelyDisabled = true, 542 ), 543 IsScreenCaptureCompletelyDisabledTestCase( 544 given = 545 Preconditions( 546 isHostInWorkProfile = false, 547 personalScreenCaptureDisabled = true, 548 workScreenCaptureDisabled = true, 549 disallowShareIntoManagedProfile = false 550 ), 551 expectedScreenCaptureCompletelyDisabled = true, 552 ), 553 IsScreenCaptureCompletelyDisabledTestCase( 554 given = 555 Preconditions( 556 isHostInWorkProfile = false, 557 personalScreenCaptureDisabled = true, 558 workScreenCaptureDisabled = true, 559 disallowShareIntoManagedProfile = true 560 ), 561 expectedScreenCaptureCompletelyDisabled = true, 562 ), 563 IsScreenCaptureCompletelyDisabledTestCase( 564 given = 565 Preconditions( 566 isHostInWorkProfile = true, 567 personalScreenCaptureDisabled = false, 568 workScreenCaptureDisabled = false, 569 disallowShareIntoManagedProfile = false 570 ), 571 expectedScreenCaptureCompletelyDisabled = false, 572 ), 573 IsScreenCaptureCompletelyDisabledTestCase( 574 given = 575 Preconditions( 576 isHostInWorkProfile = true, 577 personalScreenCaptureDisabled = false, 578 workScreenCaptureDisabled = false, 579 disallowShareIntoManagedProfile = true 580 ), 581 expectedScreenCaptureCompletelyDisabled = false, 582 ), 583 IsScreenCaptureCompletelyDisabledTestCase( 584 given = 585 Preconditions( 586 isHostInWorkProfile = true, 587 personalScreenCaptureDisabled = false, 588 workScreenCaptureDisabled = true, 589 disallowShareIntoManagedProfile = false 590 ), 591 expectedScreenCaptureCompletelyDisabled = true, 592 ), 593 IsScreenCaptureCompletelyDisabledTestCase( 594 given = 595 Preconditions( 596 isHostInWorkProfile = true, 597 personalScreenCaptureDisabled = false, 598 workScreenCaptureDisabled = true, 599 disallowShareIntoManagedProfile = true 600 ), 601 expectedScreenCaptureCompletelyDisabled = true, 602 ), 603 IsScreenCaptureCompletelyDisabledTestCase( 604 given = 605 Preconditions( 606 isHostInWorkProfile = true, 607 personalScreenCaptureDisabled = true, 608 workScreenCaptureDisabled = true, 609 disallowShareIntoManagedProfile = false 610 ), 611 expectedScreenCaptureCompletelyDisabled = true, 612 ), 613 IsScreenCaptureCompletelyDisabledTestCase( 614 given = 615 Preconditions( 616 isHostInWorkProfile = true, 617 personalScreenCaptureDisabled = true, 618 workScreenCaptureDisabled = true, 619 disallowShareIntoManagedProfile = true 620 ), 621 expectedScreenCaptureCompletelyDisabled = true, 622 ) 623 ) 624 } 625 626 class Preconditions( 627 personalScreenCaptureDisabled: Boolean, 628 workScreenCaptureDisabled: Boolean, 629 disallowShareIntoManagedProfile: Boolean, 630 val isHostInWorkProfile: Boolean, 631 ) : 632 BaseScreenCaptureDevicePolicyResolverTest.Preconditions( 633 personalScreenCaptureDisabled, 634 workScreenCaptureDisabled, 635 disallowShareIntoManagedProfile 636 ) 637 638 data class IsScreenCaptureCompletelyDisabledTestCase( 639 val given: Preconditions, 640 val expectedScreenCaptureCompletelyDisabled: Boolean 641 ) { 642 override fun toString(): String = 643 "isScreenCaptureCompletelyDisabled: " + 644 "host[${if (given.isHostInWorkProfile) "work" else "personal"} profile], " + 645 "personal screen capture disabled = ${given.personalScreenCaptureDisabled}, " + 646 "work screen capture disabled = ${given.workScreenCaptureDisabled}, " + 647 "disallow share into managed profile = ${given.disallowShareIntoManagedProfile}, " + 648 "expected screen capture completely disabled = " + 649 "$expectedScreenCaptureCompletelyDisabled" 650 } 651 652 @Test testnull653 fun test() { 654 val hostAppUserHandle = 655 if (test.given.isHostInWorkProfile) workUserHandle else personalUserHandle 656 657 val completelyDisabled = policyResolver.isScreenCaptureCompletelyDisabled(hostAppUserHandle) 658 659 assertWithMessage("Screen capture policy resolved incorrectly") 660 .that(completelyDisabled) 661 .isEqualTo(test.expectedScreenCaptureCompletelyDisabled) 662 } 663 } 664