From 7e8002c442d551c070099056c3de4681e92ea048 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Fri, 21 Jun 2024 16:49:28 +0600 Subject: [PATCH] test: added unit test for ValidateAgeLimitUseCase --- .../apps/domain/ValidateAppAgeLimitUseCase.kt | 23 ++- .../domain/ValidateAppAgeLimitUseCaseTest.kt | 174 ++++++++++++++++++ 2 files changed, 194 insertions(+), 3 deletions(-) create mode 100644 app/src/test/java/foundation/e/apps/domain/ValidateAppAgeLimitUseCaseTest.kt diff --git a/app/src/main/java/foundation/e/apps/domain/ValidateAppAgeLimitUseCase.kt b/app/src/main/java/foundation/e/apps/domain/ValidateAppAgeLimitUseCase.kt index f94648b50..e3525c387 100644 --- a/app/src/main/java/foundation/e/apps/domain/ValidateAppAgeLimitUseCase.kt +++ b/app/src/main/java/foundation/e/apps/domain/ValidateAppAgeLimitUseCase.kt @@ -25,6 +25,7 @@ import foundation.e.apps.data.blockedApps.ContentRatingGroup import foundation.e.apps.data.blockedApps.ContentRatingsRepository import foundation.e.apps.data.blockedApps.ParentalControlRepository import foundation.e.apps.data.enums.Origin +import foundation.e.apps.data.enums.ResultStatus import foundation.e.apps.data.enums.Type import foundation.e.apps.data.install.models.AppInstall import timber.log.Timber @@ -46,7 +47,7 @@ class ValidateAppAgeLimitUseCase @Inject constructor( return when { isParentalControlDisabled(ageGroup) -> ResultSupreme.Success(data = true) isKnownNsfwApp(app) -> ResultSupreme.Success(data = false) - isCleanApkApp(app) -> ResultSupreme.Success(!isNsfwAppByCleanApkApi(app)) + isCleanApkApp(app) -> checkIsNsfwApp(app) isWhiteListedCleanApkApp(app) -> ResultSupreme.Success(data = true) // Check for GPlay apps now hasNoContentRatingOnGPlay(app) -> ResultSupreme.Error() @@ -54,6 +55,15 @@ class ValidateAppAgeLimitUseCase @Inject constructor( } } + private suspend fun checkIsNsfwApp(app: AppInstall): ResultSupreme { + val isNsfwResult = isNsfwAppByCleanApkApi(app) + if (isNsfwResult.second != ResultStatus.OK) { + return ResultSupreme.Error() + } + + return ResultSupreme.Success(!isNsfwResult.first) + } + private fun isCleanApkApp(app: AppInstall): Boolean { return app.id.isNotBlank() && app.origin == Origin.CLEANAPK @@ -64,12 +74,19 @@ class ValidateAppAgeLimitUseCase @Inject constructor( return app.origin == Origin.CLEANAPK } - private suspend fun isNsfwAppByCleanApkApi(app: AppInstall): Boolean { - return appsApi.getCleanapkAppDetails(app.packageName).first.let { + private suspend fun isNsfwAppByCleanApkApi(app: AppInstall): Pair { + val cleanApkAppDetails = appsApi.getCleanapkAppDetails(app.packageName) + if (cleanApkAppDetails.second != ResultStatus.OK) { + return Pair(false, cleanApkAppDetails.second) + } + + val isNsfwApp = cleanApkAppDetails.first.let { it.antiFeatures.any { antiFeature -> antiFeature.containsKey(KEY_ANTI_FEATURES_NSFW) } } + + return Pair(isNsfwApp, cleanApkAppDetails.second) } private fun isKnownNsfwApp(app: AppInstall): Boolean { diff --git a/app/src/test/java/foundation/e/apps/domain/ValidateAppAgeLimitUseCaseTest.kt b/app/src/test/java/foundation/e/apps/domain/ValidateAppAgeLimitUseCaseTest.kt new file mode 100644 index 000000000..226deb4b4 --- /dev/null +++ b/app/src/test/java/foundation/e/apps/domain/ValidateAppAgeLimitUseCaseTest.kt @@ -0,0 +1,174 @@ +package foundation.e.apps.domain + +import com.aurora.gplayapi.data.models.ContentRating +import foundation.e.apps.data.ResultSupreme +import foundation.e.apps.data.application.apps.AppsApi +import foundation.e.apps.data.application.data.Application +import foundation.e.apps.data.blockedApps.Age +import foundation.e.apps.data.blockedApps.ContentRatingGroup +import foundation.e.apps.data.blockedApps.ContentRatingsRepository +import foundation.e.apps.data.blockedApps.ParentalControlRepository +import foundation.e.apps.data.enums.Origin +import foundation.e.apps.data.enums.ResultStatus +import foundation.e.apps.data.enums.Type +import foundation.e.apps.data.install.models.AppInstall +import kotlinx.coroutines.test.runTest +import org.junit.Assert.assertEquals +import org.junit.Before +import org.mockito.Mock +import org.mockito.Mockito.`when` +import org.mockito.MockitoAnnotations +import kotlin.test.Test + + +class ValidateAppAgeLimitUseCaseTest { + + @Mock + private lateinit var contentRatingRepository: ContentRatingsRepository + + @Mock + private lateinit var parentalControlRepository: ParentalControlRepository + + @Mock + private lateinit var appsApi: AppsApi + + private lateinit var validateAppAgeLimitUseCase: ValidateAppAgeLimitUseCase + + @Before + fun setup() { + MockitoAnnotations.openMocks(this) + validateAppAgeLimitUseCase = ValidateAppAgeLimitUseCase( + contentRatingRepository, + parentalControlRepository, + appsApi + ) + } + + @Test + fun parentalControlDisabled_returnsSuccessTrue() = runTest { + `when`(parentalControlRepository.getSelectedAgeGroup()).thenReturn(Age.PARENTAL_CONTROL_DISABLED) + + val result = validateAppAgeLimitUseCase(AppInstall()) // Using a dummy AppInstall object + + assertEquals(true, result.data) + } + + @Test + fun knownNsfwApp_returnsSuccessFalse() = runTest { + val app = AppInstall(packageName = "com.example.nsfwapp") + `when`(parentalControlRepository.getSelectedAgeGroup()).thenReturn(Age.ELEVEN) + `when`(contentRatingRepository.fDroidNSFWApps).thenReturn(listOf("com.example.nsfwapp")) + + val result = validateAppAgeLimitUseCase(app) + + assertEquals(false, result.data) + } + + @Test + fun cleanApkApp_isNsfw_returnsSuccessFalse() = runTest { + val app = AppInstall( + id = "123", + packageName = "com.example.cleanapp", + origin = Origin.CLEANAPK, + type = Type.NATIVE + ) + `when`(parentalControlRepository.getSelectedAgeGroup()).thenReturn(Age.ELEVEN) + `when`(appsApi.getCleanapkAppDetails(app.packageName)).thenReturn( + Pair( + Application( + "com.example.cleanapp", + antiFeatures = listOf(mapOf(Pair("NSFW", "message"))) + ), ResultStatus.OK + ) + ) + + val result = validateAppAgeLimitUseCase(app) + + assertEquals(false, result.data) + } + + @Test + fun cleanApkApp_isNotNsfw_returnsSuccessTrue() = runTest { + val app = AppInstall( + id = "123", + packageName = "com.example.cleanapp", + origin = Origin.CLEANAPK, + type = Type.NATIVE + ) + `when`(parentalControlRepository.getSelectedAgeGroup()).thenReturn(Age.ELEVEN) + `when`(appsApi.getCleanapkAppDetails(app.packageName)).thenReturn( + Pair( + Application( + "com.example.cleanapp", + ), ResultStatus.OK + ) + ) + + val result = validateAppAgeLimitUseCase(app) + + assertEquals(true, result.data) + } + + @Test + fun cleanApkApp_getCleanApkDetails_error_returnsError() = runTest { + val app = AppInstall( + id = "123", + packageName = "com.example.cleanapp", + origin = Origin.CLEANAPK, + type = Type.NATIVE + ) + `when`(parentalControlRepository.getSelectedAgeGroup()).thenReturn(Age.ELEVEN) + `when`(appsApi.getCleanapkAppDetails(app.packageName)).thenReturn( + Pair( + Application(), ResultStatus.UNKNOWN + ) + ) + + val result = validateAppAgeLimitUseCase(app) + + assertEquals(true, result is ResultSupreme.Error) + } + + @Test + fun noContentRatingOnGPlay_returnsError() = runTest { + val app = AppInstall(packageName = "com.example.gplayapp", origin = Origin.GPLAY) + `when`(parentalControlRepository.getSelectedAgeGroup()).thenReturn(Age.ELEVEN) + `when`(contentRatingRepository.getEnglishContentRating(app.packageName)).thenReturn(null) + + val result = validateAppAgeLimitUseCase(app) + + assertEquals(true, result is ResultSupreme.Error) + } + + @Test + fun validAgeLimit_returnsSuccessTrue() = runTest { + val app = AppInstall(packageName = "com.example.gplayapp", origin = Origin.GPLAY) + app.contentRating = ContentRating("eleven", "11+") + `when`(parentalControlRepository.getSelectedAgeGroup()).thenReturn(Age.ELEVEN) + `when`(contentRatingRepository.getEnglishContentRating(app.packageName)) + .thenReturn(ContentRating("ELEVEN", "11+")) + `when`(contentRatingRepository.contentRatingGroups).thenReturn( + listOf(ContentRatingGroup("ELEVEN", "11+", listOf("ELEVEN")))) + + val result = validateAppAgeLimitUseCase(app) + assertEquals(true, result.data) + } + + @Test + fun invalidAgeLimit_returnsSuccessFalse() = runTest { + val app = AppInstall(packageName = "com.example.gplayapp", origin = Origin.GPLAY) + app.contentRating = ContentRating("eighteen", "18+") + `when`(parentalControlRepository.getSelectedAgeGroup()).thenReturn(Age.ELEVEN) + `when`(contentRatingRepository.getEnglishContentRating(app.packageName)) + .thenReturn(ContentRating("EIGHTEEN", "18+")) + `when`(contentRatingRepository.contentRatingGroups).thenReturn( + listOf( + ContentRatingGroup("ELEVEN", "11+", listOf("ELEVEN")), + ContentRatingGroup("EIGHTEEN", "18+", listOf("EIGHTEEN")) + ) + ) + + val result = validateAppAgeLimitUseCase(app) + assertEquals(false, result.data) + } +} \ No newline at end of file -- GitLab