Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Add tests for PhoneFeature #12021

Merged
merged 1 commit into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions app/src/main/java/org/mozilla/fenix/settings/PhoneFeature.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,25 @@ enum class PhoneFeature(val androidPermissionsList: Array<String>) : Parcelable
sitePermissions: SitePermissions? = null,
settings: Settings? = null
): String {
@StringRes val stringRes =
when (isAndroidPermissionGranted(context)) {
false -> R.string.phone_feature_blocked_by_android
else -> when (this) {
AUTOPLAY_AUDIBLE -> {
when (settings?.getAutoplayUserSetting(default = AUTOPLAY_BLOCK_ALL) ?: AUTOPLAY_BLOCK_ALL) {
AUTOPLAY_ALLOW_ALL -> R.string.preference_option_autoplay_allowed2
AUTOPLAY_ALLOW_ON_WIFI -> R.string.preference_option_autoplay_allowed_wifi_only2
AUTOPLAY_BLOCK_AUDIBLE -> R.string.preference_option_autoplay_block_audio2
AUTOPLAY_BLOCK_ALL -> R.string.preference_option_autoplay_blocked3
else -> R.string.preference_option_autoplay_blocked3
}
}
else -> {
when (getStatus(sitePermissions, settings)) {
SitePermissions.Status.BLOCKED -> R.string.preference_option_phone_feature_blocked
SitePermissions.Status.NO_DECISION -> R.string.preference_option_phone_feature_ask_to_allow
SitePermissions.Status.ALLOWED -> R.string.preference_option_phone_feature_allowed
}
@StringRes val stringRes = if (isAndroidPermissionGranted(context)) {
when (this) {
AUTOPLAY_AUDIBLE ->
when (settings?.getAutoplayUserSetting(default = AUTOPLAY_BLOCK_ALL) ?: AUTOPLAY_BLOCK_ALL) {
AUTOPLAY_ALLOW_ALL -> R.string.preference_option_autoplay_allowed2
AUTOPLAY_ALLOW_ON_WIFI -> R.string.preference_option_autoplay_allowed_wifi_only2
AUTOPLAY_BLOCK_AUDIBLE -> R.string.preference_option_autoplay_block_audio2
AUTOPLAY_BLOCK_ALL -> R.string.preference_option_autoplay_blocked3
else -> R.string.preference_option_autoplay_blocked3
}
else -> when (getStatus(sitePermissions, settings)) {
SitePermissions.Status.BLOCKED -> R.string.preference_option_phone_feature_blocked
SitePermissions.Status.NO_DECISION -> R.string.preference_option_phone_feature_ask_to_allow
SitePermissions.Status.ALLOWED -> R.string.preference_option_phone_feature_allowed
}
}
} else {
R.string.phone_feature_blocked_by_android
}
return context.getString(stringRes)
}

Expand Down Expand Up @@ -109,7 +106,7 @@ enum class PhoneFeature(val androidPermissionsList: Array<String>) : Parcelable
fun getAction(settings: Settings): SitePermissionsRules.Action =
settings.getSitePermissionsPhoneFeatureAction(this, getDefault())

fun getDefault(): SitePermissionsRules.Action {
private fun getDefault(): SitePermissionsRules.Action {
return when (this) {
AUTOPLAY_AUDIBLE -> SitePermissionsRules.Action.BLOCKED
AUTOPLAY_INAUDIBLE -> SitePermissionsRules.Action.ALLOWED
Expand Down
98 changes: 98 additions & 0 deletions app/src/test/java/org/mozilla/fenix/settings/PhoneFeatureTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.settings

import android.Manifest
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.MockK
import mozilla.components.feature.sitepermissions.SitePermissions
import mozilla.components.feature.sitepermissions.SitePermissions.Status
import mozilla.components.feature.sitepermissions.SitePermissionsRules.Action
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.R
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.utils.Settings

@RunWith(FenixRobolectricTestRunner::class)
class PhoneFeatureTest {

@MockK private lateinit var sitePermissions: SitePermissions
@MockK private lateinit var settings: Settings

@Before
fun setup() {
MockKAnnotations.init(this)
}

@Test
fun `getStatus throws if both values are null`() {
var exception: IllegalArgumentException? = null
try {
PhoneFeature.AUTOPLAY_AUDIBLE.getStatus()
} catch (e: java.lang.IllegalArgumentException) {
exception = e
}
assertNotNull(exception)
}

@Test
fun `getStatus returns value from site permissions`() {
every { sitePermissions.notification } returns Status.BLOCKED
assertEquals(Status.BLOCKED, PhoneFeature.NOTIFICATION.getStatus(sitePermissions, settings))
}

@Test
fun `getStatus returns value from settings`() {
every {
settings.getSitePermissionsPhoneFeatureAction(PhoneFeature.AUTOPLAY_INAUDIBLE, Action.ALLOWED)
} returns Action.ALLOWED
assertEquals(Status.ALLOWED, PhoneFeature.AUTOPLAY_INAUDIBLE.getStatus(settings = settings))
}

@Test
fun getLabel() {
assertEquals("Camera", PhoneFeature.CAMERA.getLabel(testContext))
assertEquals("Location", PhoneFeature.LOCATION.getLabel(testContext))
assertEquals("Microphone", PhoneFeature.MICROPHONE.getLabel(testContext))
assertEquals("Notification", PhoneFeature.NOTIFICATION.getLabel(testContext))
assertEquals("Autoplay", PhoneFeature.AUTOPLAY_AUDIBLE.getLabel(testContext))
assertEquals("Autoplay", PhoneFeature.AUTOPLAY_INAUDIBLE.getLabel(testContext))
}

@Test
fun getPreferenceId() {
assertEquals(R.string.pref_key_phone_feature_camera, PhoneFeature.CAMERA.getPreferenceId())
assertEquals(R.string.pref_key_phone_feature_location, PhoneFeature.LOCATION.getPreferenceId())
assertEquals(R.string.pref_key_phone_feature_microphone, PhoneFeature.MICROPHONE.getPreferenceId())
assertEquals(R.string.pref_key_phone_feature_notification, PhoneFeature.NOTIFICATION.getPreferenceId())
assertEquals(R.string.pref_key_browser_feature_autoplay_audible, PhoneFeature.AUTOPLAY_AUDIBLE.getPreferenceId())
assertEquals(R.string.pref_key_browser_feature_autoplay_inaudible, PhoneFeature.AUTOPLAY_INAUDIBLE.getPreferenceId())

assertEquals(
"pref_key_browser_feature_autoplay_inaudible",
PhoneFeature.AUTOPLAY_INAUDIBLE.getPreferenceKey(testContext)
)
}

@Test
fun `getAction returns value from settings`() {
every {
settings.getSitePermissionsPhoneFeatureAction(PhoneFeature.AUTOPLAY_AUDIBLE, Action.BLOCKED)
} returns Action.ASK_TO_ALLOW
assertEquals(Action.ASK_TO_ALLOW, PhoneFeature.AUTOPLAY_AUDIBLE.getAction(settings))
}

@Test
fun findFeatureBy() {
assertEquals(PhoneFeature.CAMERA, PhoneFeature.findFeatureBy(arrayOf(Manifest.permission.CAMERA)))
assertEquals(PhoneFeature.MICROPHONE, PhoneFeature.findFeatureBy(arrayOf(Manifest.permission.RECORD_AUDIO)))
}
}