Skip to content

Commit

Permalink
Merge pull request #26 from droibit/feature/mockk
Browse files Browse the repository at this point in the history
Migrate from mockito-kotlin to mockk
  • Loading branch information
droibit authored Dec 21, 2024
2 parents 5d7505e + db59cee commit 5dd308f
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 198 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ androidx-browser = { module = "androidx.browser:browser", version = "1.8.0" }
androidx-test-junit = { module = "androidx.test.ext:junit-ktx", version = "1.1.5" }
junit = { module = "junit:junit", version = "4.13.2" }
robolectric = { module = "org.robolectric:robolectric", version = "4.11" }
mockito-kotlin = { module = "org.mockito.kotlin:mockito-kotlin", version = "4.1.0" }
mockk = { module = "io.mockk:mockk", version = "1.13.3" }
truth = { module = "com.google.truth:truth", version = "1.4.4" }

[plugins]
Expand Down
4 changes: 2 additions & 2 deletions launcher/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ dependencies {
implementation(libs.androidx.browser)

testImplementation(libs.junit)
testImplementation(libs.mockito.kotlin)
testImplementation(libs.truth)
testImplementation(libs.androidx.test.junit)
testImplementation(libs.robolectric)
testImplementation(libs.mockk)
testImplementation(libs.truth)
}

apply(from = "$rootDir/gradle/gradle-mvn-push.gradle.kts")
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@ import androidx.browser.customtabs.CustomTabsIntent
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.droibit.android.customtabs.launcher.CustomTabsPackage.CHROME_PACKAGES
import com.google.common.truth.Truth.assertThat
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.junit4.MockKRule
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.verify
import org.junit.After
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.mockStatic
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.inOrder
import org.robolectric.annotation.Config

@RunWith(AndroidJUnit4::class)
@Config(manifest = Config.NONE)
class CustomTabsIntentHelperTest {
@get:Rule
val mockitoRule: MockitoRule = MockitoJUnit.rule()
val mockkRule = MockKRule(this)

@Mock
@MockK
private lateinit var context: Context

@After
fun tearDown() {
unmockkAll()
}

@Test
fun `CHROME_PACKAGES are listed in priority order`() {
val expectedOrder = listOf(
Expand All @@ -40,199 +45,175 @@ class CustomTabsIntentHelperTest {

@Test
fun `setChromeCustomTabsPackage uses Chrome if found`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
val chromePackage = "com.android.chrome"
mocked.`when`<String?> {
CustomTabsClient.getPackageName(any(), any(), any())
}.thenReturn(chromePackage)

val customTabsIntent = CustomTabsIntent.Builder()
.build()
.setChromeCustomTabsPackage(context)
assertThat(customTabsIntent.intent.`package`).isEqualTo(chromePackage)

mocked.verify {
val packages = CHROME_PACKAGES.toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(true))
}
mockkStatic(CustomTabsClient::class)

val chromePackage = "com.android.chrome"
every { CustomTabsClient.getPackageName(any(), any(), any()) } returns chromePackage

val customTabsIntent = CustomTabsIntent.Builder()
.build()
.setChromeCustomTabsPackage(context)
assertThat(customTabsIntent.intent.`package`).isEqualTo(chromePackage)

verify {
val packages = CHROME_PACKAGES.toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(true))
}
}

@Test
fun `setChromeCustomTabsPackage sets null if Chrome not found`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
mocked.`when`<String?> {
CustomTabsClient.getPackageName(any(), any(), any())
}.thenReturn(null)

val customTabsIntent = CustomTabsIntent.Builder()
.build()
.setChromeCustomTabsPackage(context)
assertThat(customTabsIntent.intent.`package`).isNull()

mocked.verify {
val packages = CHROME_PACKAGES.toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(true))
}
mockkStatic(CustomTabsClient::class)
every { CustomTabsClient.getPackageName(any(), any(), any()) } returns null

val customTabsIntent = CustomTabsIntent.Builder()
.build()
.setChromeCustomTabsPackage(context)
assertThat(customTabsIntent.intent.`package`).isNull()

verify {
val packages = CHROME_PACKAGES.toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(true))
}
}

@Test
fun `setChromeCustomTabsPackage uses non-Chrome Custom Tab if Chrome not found`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
val nonChromePackage = "com.example.customtabs"
mocked.`when`<String?> {
CustomTabsClient.getPackageName(any(), any(), any())
}.thenReturn(nonChromePackage)

val additionalCustomTabs = NonChromeCustomTabs(setOf(nonChromePackage))
val customTabsIntent = CustomTabsIntent.Builder()
.build()
.setChromeCustomTabsPackage(context, additionalCustomTabs)
assertThat(customTabsIntent.intent.`package`).isEqualTo(nonChromePackage)

inOrder(CustomTabsClient::class.java) {
verify(mocked) {
val packages = (CHROME_PACKAGES + nonChromePackage).toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(true))
}
}
mockkStatic(CustomTabsClient::class)

val nonChromePackage = "com.example.customtabs"
every { CustomTabsClient.getPackageName(any(), any(), any()) } returns nonChromePackage

val additionalCustomTabs = NonChromeCustomTabs(setOf(nonChromePackage))
val customTabsIntent = CustomTabsIntent.Builder()
.build()
.setChromeCustomTabsPackage(context, additionalCustomTabs)
assertThat(customTabsIntent.intent.`package`).isEqualTo(nonChromePackage)

verify {
val packages = (CHROME_PACKAGES + nonChromePackage).toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(true))
}
}

@Test
fun `setCustomTabsPackage uses default browser if found`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
val chromePackage = "com.chrome.beta"
mocked.`when`<String?> {
CustomTabsClient.getPackageName(any(), any(), any())
}.thenReturn(chromePackage)

val customTabsIntent = CustomTabsIntent.Builder()
.build()
.setCustomTabsPackage(context)
assertThat(customTabsIntent.intent.`package`).isEqualTo(chromePackage)

mocked.verify {
val packages = CHROME_PACKAGES.toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(false))
}
mockkStatic(CustomTabsClient::class)

val chromePackage = "com.chrome.beta"
every { CustomTabsClient.getPackageName(any(), any(), any()) } returns chromePackage

val customTabsIntent = CustomTabsIntent.Builder()
.build()
.setCustomTabsPackage(context)
assertThat(customTabsIntent.intent.`package`).isEqualTo(chromePackage)

verify {
val packages = CHROME_PACKAGES.toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(false))
}
}

@Test
fun `setCustomTabsPackage sets null if no default browser or Chrome found`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
mocked.`when`<String?> {
CustomTabsClient.getPackageName(any(), any(), any())
}.thenReturn(null)

val customTabsIntent = CustomTabsIntent.Builder()
.build()
.setCustomTabsPackage(context)
assertThat(customTabsIntent.intent.`package`).isNull()

mocked.verify {
val packages = CHROME_PACKAGES.toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(false))
}
mockkStatic(CustomTabsClient::class)
every { CustomTabsClient.getPackageName(any(), any(), any()) } returns null

val customTabsIntent = CustomTabsIntent.Builder()
.build()
.setCustomTabsPackage(context)
assertThat(customTabsIntent.intent.`package`).isNull()

verify {
val packages = CHROME_PACKAGES.toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(false))
}
}

@Test
fun `setCustomTabsPackage uses non-Chrome Custom Tab if none found`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
val nonChromePackage = "com.example.customtabs"
mocked.`when`<String?> {
CustomTabsClient.getPackageName(any(), any(), any())
}.thenReturn(nonChromePackage)

val additionalCustomTabs = NonChromeCustomTabs(setOf(nonChromePackage))
val customTabsIntent = CustomTabsIntent.Builder()
.build()
.setCustomTabsPackage(context, additionalCustomTabs)
assertThat(customTabsIntent.intent.`package`).isEqualTo(nonChromePackage)

mocked.verify {
val packages = (CHROME_PACKAGES + nonChromePackage).toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(false))
}
mockkStatic(CustomTabsClient::class)

val nonChromePackage = "com.example.customtabs"
every { CustomTabsClient.getPackageName(any(), any(), any()) } returns nonChromePackage

val additionalCustomTabs = NonChromeCustomTabs(setOf(nonChromePackage))
val customTabsIntent = CustomTabsIntent.Builder()
.build()
.setCustomTabsPackage(context, additionalCustomTabs)
assertThat(customTabsIntent.intent.`package`).isEqualTo(nonChromePackage)

verify {
val packages = (CHROME_PACKAGES + nonChromePackage).toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(false))
}
}

@Test
fun `getCustomTabsPackage returns default Chrome package when available`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
val chromePackage = "com.android.chrome"
mocked.`when`<String?> {
CustomTabsClient.getPackageName(any(), any(), any())
}.thenReturn(chromePackage)

val packageName = getCustomTabsPackage(context)
assertThat(packageName).isEqualTo(chromePackage)

mocked.verify {
val packages = CHROME_PACKAGES.toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(true))
}
mockkStatic(CustomTabsClient::class)

val chromePackage = "com.android.chrome"
every { CustomTabsClient.getPackageName(any(), any(), any()) } returns chromePackage

val packageName = getCustomTabsPackage(context)
assertThat(packageName).isEqualTo(chromePackage)

verify {
val packages = CHROME_PACKAGES.toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(true))
}
}

@Test
fun `getCustomTabsPackage returns additional Custom Tabs package when Chrome not available`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
val nonChromePackage = "com.example.browser"
mocked.`when`<String?> {
CustomTabsClient.getPackageName(any(), any(), any())
}.thenReturn(nonChromePackage)

val additionalCustomTabs = NonChromeCustomTabs(setOf(nonChromePackage))
val packageName =
getCustomTabsPackage(context, additionalCustomTabs = additionalCustomTabs)
assertThat(packageName).isEqualTo(nonChromePackage)

mocked.verify {
val packages = (CHROME_PACKAGES + nonChromePackage).toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(true))
}
mockkStatic(CustomTabsClient::class)

val nonChromePackage = "com.example.browser"
every { CustomTabsClient.getPackageName(any(), any(), any()) } returns nonChromePackage

val additionalCustomTabs = NonChromeCustomTabs(setOf(nonChromePackage))
val packageName =
getCustomTabsPackage(context, additionalCustomTabs = additionalCustomTabs)
assertThat(packageName).isEqualTo(nonChromePackage)

verify {
val packages = (CHROME_PACKAGES + nonChromePackage).toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(true))
}
}

@Test
fun `getCustomTabsPackage returns null when no packages are available`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
mocked.`when`<String?> {
CustomTabsClient.getPackageName(any(), any(), any())
}.thenReturn(null)

val packageName = getCustomTabsPackage(context)
assertThat(packageName).isNull()

mocked.verify {
val packages = CHROME_PACKAGES.toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(true))
}
mockkStatic(CustomTabsClient::class)
every { CustomTabsClient.getPackageName(any(), any(), any()) } returns null


val packageName = getCustomTabsPackage(context)
assertThat(packageName).isNull()

verify {
val packages = CHROME_PACKAGES.toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(true))
}
}

@Test
fun `getCustomTabsPackage ignores default packages when ignoreDefault is false`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
val nonChromePackage = "com.example.browser"
mocked.`when`<String?> {
CustomTabsClient.getPackageName(any(), any(), any())
}.thenReturn(nonChromePackage)

val packageName = getCustomTabsPackage(
context,
ignoreDefault = false
)
assertThat(packageName).isEqualTo(nonChromePackage)

mocked.verify {
val packages = CHROME_PACKAGES.toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(false))
}
mockkStatic(CustomTabsClient::class)

val nonChromePackage = "com.example.browser"
every { CustomTabsClient.getPackageName(any(), any(), any()) } returns nonChromePackage

val packageName = getCustomTabsPackage(
context,
ignoreDefault = false
)
assertThat(packageName).isEqualTo(nonChromePackage)

verify {
val packages = CHROME_PACKAGES.toList()
CustomTabsClient.getPackageName(any(), eq(packages), eq(false))
}
}
}
Loading

0 comments on commit 5dd308f

Please sign in to comment.