mirror of
https://github.com/element-hq/element-android
synced 2024-11-28 13:38:49 +03:00
Add unit tests for filter/map logic
This commit is contained in:
parent
9e084ec372
commit
92a140b504
2 changed files with 95 additions and 6 deletions
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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<String>, androidPermissionResult: Map<String, Boolean>): List<String> {
|
||||
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()
|
||||
|
|
Loading…
Reference in a new issue