From e69a5cee95da751a09fb3414d7eb7f92e947947d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Forlini?= Date: Tue, 3 Oct 2023 17:33:31 +0200 Subject: [PATCH] Move WebView mocking into a separate class in test utils Minor changes needed to avoid duplication MAILANDR-1015 --- .../data/system/DeviceCapabilitiesImplTest.kt | 22 ++--------- .../test/utils/mocks/WebViewProviderMocks.kt | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 test/utils/src/main/kotlin/ch/protonmail/android/test/utils/mocks/WebViewProviderMocks.kt diff --git a/mail-common/data/src/test/kotlin/ch/protonmail/android/mailcommon/data/system/DeviceCapabilitiesImplTest.kt b/mail-common/data/src/test/kotlin/ch/protonmail/android/mailcommon/data/system/DeviceCapabilitiesImplTest.kt index f5f6a87243..ca0b1dfaee 100644 --- a/mail-common/data/src/test/kotlin/ch/protonmail/android/mailcommon/data/system/DeviceCapabilitiesImplTest.kt +++ b/mail-common/data/src/test/kotlin/ch/protonmail/android/mailcommon/data/system/DeviceCapabilitiesImplTest.kt @@ -18,10 +18,8 @@ package ch.protonmail.android.mailcommon.data.system -import android.content.pm.ApplicationInfo -import android.content.pm.PackageInfo import android.webkit.WebView -import io.mockk.every +import ch.protonmail.android.test.utils.mocks.WebViewProviderMocks.mockExpectedWebViewAvailableOnDevice import io.mockk.mockkStatic import io.mockk.unmockkAll import org.junit.After @@ -44,7 +42,7 @@ class DeviceCapabilitiesImplTest { @Test fun `Should return web view not available when provider is not present`() { // Given - expectedWebViewAvailableOnDevice(isPackagePresent = false) + mockExpectedWebViewAvailableOnDevice(isPackagePresent = false) // When val capabilities = DeviceCapabilitiesImpl().getCapabilities() @@ -56,7 +54,7 @@ class DeviceCapabilitiesImplTest { @Test fun `Should return web view not available when provider is present but not enabled`() { // Given - expectedWebViewAvailableOnDevice(isPackagePresent = true, isPackageEnabled = false) + mockExpectedWebViewAvailableOnDevice(isPackagePresent = true, isPackageEnabled = false) // When val capabilities = DeviceCapabilitiesImpl().getCapabilities() @@ -68,7 +66,7 @@ class DeviceCapabilitiesImplTest { @Test fun `Should return web view available when provider is present and enabled`() { // Given - expectedWebViewAvailableOnDevice(isPackagePresent = true, isPackageEnabled = true) + mockExpectedWebViewAvailableOnDevice(isPackagePresent = true, isPackageEnabled = true) // When val capabilities = DeviceCapabilitiesImpl().getCapabilities() @@ -76,16 +74,4 @@ class DeviceCapabilitiesImplTest { // Then assertEquals(true, capabilities.hasWebView) } - - private fun expectedWebViewAvailableOnDevice( - isPackagePresent: Boolean, - isPackageEnabled: Boolean = false - ) { - val packageInfo = PackageInfo().apply { - applicationInfo = ApplicationInfo() - applicationInfo.enabled = isPackageEnabled - } - - every { WebView.getCurrentWebViewPackage() } returns if (isPackagePresent) packageInfo else null - } } diff --git a/test/utils/src/main/kotlin/ch/protonmail/android/test/utils/mocks/WebViewProviderMocks.kt b/test/utils/src/main/kotlin/ch/protonmail/android/test/utils/mocks/WebViewProviderMocks.kt new file mode 100644 index 0000000000..9e1e88b229 --- /dev/null +++ b/test/utils/src/main/kotlin/ch/protonmail/android/test/utils/mocks/WebViewProviderMocks.kt @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 Proton Technologies AG + * This file is part of Proton Technologies AG and Proton Mail. + * + * Proton Mail is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Proton Mail is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Proton Mail. If not, see . + */ + +package ch.protonmail.android.test.utils.mocks + +import android.content.pm.ApplicationInfo +import android.content.pm.PackageInfo +import android.webkit.WebView +import io.mockk.every + +object WebViewProviderMocks { + + fun mockExpectedWebViewAvailableOnDevice( + isPackagePresent: Boolean, + isPackageEnabled: Boolean = false + ) { + val packageInfo = PackageInfo().apply { + applicationInfo = ApplicationInfo() + applicationInfo.enabled = isPackageEnabled + } + + every { WebView.getCurrentWebViewPackage() } returns if (isPackagePresent) packageInfo else null + } +} \ No newline at end of file