From 92a140b5041f9fc4423ccc9aa9348de41ab8a1c8 Mon Sep 17 00:00:00 2001 From: Johannes Marbach Date: Wed, 25 May 2022 20:50:58 +0200 Subject: [PATCH] Add unit tests for filter/map logic --- .../widgets/WebviewPermissionUtilsTest.kt | 83 +++++++++++++++++++ .../widgets/webview/WebviewPermissionUtils.kt | 18 ++-- 2 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 vector/src/androidTest/java/im/vector/app/features/widgets/WebviewPermissionUtilsTest.kt diff --git a/vector/src/androidTest/java/im/vector/app/features/widgets/WebviewPermissionUtilsTest.kt b/vector/src/androidTest/java/im/vector/app/features/widgets/WebviewPermissionUtilsTest.kt new file mode 100644 index 0000000000..6ae17dbda4 --- /dev/null +++ b/vector/src/androidTest/java/im/vector/app/features/widgets/WebviewPermissionUtilsTest.kt @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.widgets + +import android.Manifest +import android.webkit.PermissionRequest +import im.vector.app.InstrumentedTest +import im.vector.app.features.widgets.webview.WebviewPermissionUtils +import org.amshove.kluent.shouldBeEqualTo +import org.junit.FixMethodOrder +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.runners.MethodSorters + +@RunWith(JUnit4::class) +@FixMethodOrder(MethodSorters.JVM) +class WebviewPermissionUtilsTest : InstrumentedTest { + + private val utils = WebviewPermissionUtils() + + @Test + fun filterPermissionsToBeGranted_selectedAndGrantedNothing() { + val permissions = utils.filterPermissionsToBeGranted( + selectedWebPermissions = listOf(), + androidPermissionResult = mapOf()) + permissions shouldBeEqualTo listOf() + } + + @Test + fun filterPermissionsToBeGranted_selectedNothingGrantedCamera() { + val permissions = utils.filterPermissionsToBeGranted( + selectedWebPermissions = listOf(), + androidPermissionResult = mapOf(Manifest.permission.CAMERA to true)) + permissions shouldBeEqualTo listOf() + } + + @Test + fun filterPermissionsToBeGranted_selectedAndPreviouslyGrantedCamera() { + val permissions = utils.filterPermissionsToBeGranted( + selectedWebPermissions = listOf(PermissionRequest.RESOURCE_VIDEO_CAPTURE), + androidPermissionResult = mapOf()) + permissions shouldBeEqualTo listOf(PermissionRequest.RESOURCE_VIDEO_CAPTURE) + } + + @Test + fun filterPermissionsToBeGranted_selectedAndGrantedCamera() { + val permissions = utils.filterPermissionsToBeGranted( + selectedWebPermissions = listOf(PermissionRequest.RESOURCE_VIDEO_CAPTURE), + androidPermissionResult = mapOf(Manifest.permission.CAMERA to true)) + permissions shouldBeEqualTo listOf(PermissionRequest.RESOURCE_VIDEO_CAPTURE) + } + + @Test + fun filterPermissionsToBeGranted_selectedAndDeniedCamera() { + val permissions = utils.filterPermissionsToBeGranted( + selectedWebPermissions = listOf(PermissionRequest.RESOURCE_VIDEO_CAPTURE), + androidPermissionResult = mapOf(Manifest.permission.CAMERA to false)) + permissions shouldBeEqualTo listOf() + } + + @Test + fun filterPermissionsToBeGranted_selectedProtectedMediaGrantedNothing() { + val permissions = utils.filterPermissionsToBeGranted( + selectedWebPermissions = listOf(PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID), + androidPermissionResult = mapOf(Manifest.permission.CAMERA to false)) + permissions shouldBeEqualTo listOf(PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID) + } +} diff --git a/vector/src/main/java/im/vector/app/features/widgets/webview/WebviewPermissionUtils.kt b/vector/src/main/java/im/vector/app/features/widgets/webview/WebviewPermissionUtils.kt index 2bf12dff43..92b6ad213c 100644 --- a/vector/src/main/java/im/vector/app/features/widgets/webview/WebviewPermissionUtils.kt +++ b/vector/src/main/java/im/vector/app/features/widgets/webview/WebviewPermissionUtils.kt @@ -21,6 +21,7 @@ import android.content.Context import android.webkit.PermissionRequest import androidx.activity.result.ActivityResultLauncher import androidx.annotation.StringRes +import androidx.annotation.VisibleForTesting import androidx.fragment.app.FragmentActivity import com.google.android.material.dialog.MaterialAlertDialogBuilder import im.vector.app.R @@ -78,12 +79,7 @@ class WebviewPermissionUtils @Inject constructor() { if (permissionRequest == null) { throw NullPointerException("permissionRequest was null! Make sure to call promptForPermissions first.") } - val grantedPermissions = selectedPermissions.filter { webPermission -> - val androidPermission = webPermissionToAndroidPermission(webPermission) - ?: return@filter true // No corresponding Android permission exists - return@filter result[androidPermission] - ?: return@filter true // Android permission already granted before - } + val grantedPermissions = filterPermissionsToBeGranted(selectedPermissions, result) if (grantedPermissions.isNotEmpty()) { permissionRequest?.grant(grantedPermissions.toTypedArray()) } else { @@ -92,6 +88,16 @@ class WebviewPermissionUtils @Inject constructor() { reset() } + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + fun filterPermissionsToBeGranted(selectedWebPermissions: List, androidPermissionResult: Map): List { + return selectedWebPermissions.filter { webPermission -> + val androidPermission = webPermissionToAndroidPermission(webPermission) + ?: return@filter true // No corresponding Android permission exists + return@filter androidPermissionResult[androidPermission] + ?: return@filter true // Android permission already granted before + } + } + private fun reset() { permissionRequest = null selectedPermissions = listOf()