test: add RssHelperTest.kt

This commit is contained in:
Ash 2024-04-03 17:16:03 +08:00
parent c7b7283b8b
commit 71b470a139
No known key found for this signature in database
2 changed files with 83 additions and 5 deletions

View File

@ -81,11 +81,11 @@ android {
freeCompilerArgs = freeCompilerArgs + "-opt-in=kotlin.RequiresOptIn"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "1.8"
jvmTarget = JavaVersion.VERSION_11
}
buildFeatures {
compose true
@ -214,7 +214,11 @@ dependencies {
implementation "androidx.activity:activity-compose:1.8.2"
implementation 'androidx.appcompat:appcompat:1.7.0-alpha03'
testImplementation "junit:junit:4.13.2"
androidTestImplementation "androidx.test.ext:junit:1.1.3"
androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
androidTestImplementation "androidx.test.ext:junit:1.1.5"
androidTestImplementation "androidx.test.espresso:espresso-core:3.5.1"
// mockito
testImplementation "org.mockito:mockito-core:5.11.0"
testImplementation 'org.mockito:mockito-junit-jupiter:5.11.0'
testImplementation "org.mockito.kotlin:mockito-kotlin:5.2.1"
}

View File

@ -0,0 +1,74 @@
package me.ash.reader.infrastructure.rss
import android.content.Context
import kotlinx.coroutines.CoroutineDispatcher
import okhttp3.OkHttpClient
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import org.mockito.kotlin.mock
const val enclosureUrlString1: String = "https://example.com/enclosure.jpg"
const val enclosureUrlString2: String = "https://github.blog/wp-content/uploads/2024/03/github_copilot_header.png"
const val imageUrlString: String = "https://example.com/image.jpg"
const val enclosureHtmlCase1: String = """
<enclosure url="$enclosureUrlString1" type="image/jpeg"/>
<img src="$imageUrlString"/>
"""
const val enclosureHtmlCase2: String = """
<img src="$imageUrlString"/>
<enclosure url="$enclosureUrlString1" type="image/jpeg"/>
<img src="$imageUrlString"/>
"""
const val enclosureHtmlCase3: String = """
<img src="$imageUrlString"/>
<enclosure url="$enclosureUrlString2" type="image/png"/>
<img src="$imageUrlString"/>
"""
const val imageHtmlCase1: String = """
<img src="$enclosureUrlString1"/>
<img src="$imageUrlString"/>
"""
const val imageHtmlCase2: String = """
<img src="$imageUrlString"/>
<img src="$enclosureUrlString1"/>
<img src="$enclosureUrlString1"/>
"""
@RunWith(MockitoJUnitRunner::class)
class RssHelperTest {
@Mock
private lateinit var mockContext: Context
@Mock
private lateinit var mockIODispatcher: CoroutineDispatcher
@Mock
private lateinit var mockOkHttpClient: OkHttpClient
private lateinit var rssHelper: RssHelper
@Before
fun setUp() {
mockContext = mock<Context> { }
mockIODispatcher = mock<CoroutineDispatcher> {}
mockOkHttpClient = mock<OkHttpClient> {}
rssHelper = RssHelper(mockContext, mockIODispatcher, mockOkHttpClient)
}
@Test
fun testFindThumbnail() {
Assert.assertNull(rssHelper.findThumbnail(""))
Assert.assertNull(rssHelper.findThumbnail(" "))
Assert.assertNull(rssHelper.findThumbnail(null))
Assert.assertEquals(enclosureUrlString1, rssHelper.findThumbnail(enclosureHtmlCase1))
Assert.assertEquals(enclosureUrlString1, rssHelper.findThumbnail(enclosureHtmlCase2))
Assert.assertEquals(enclosureUrlString2, rssHelper.findThumbnail(enclosureHtmlCase3))
Assert.assertEquals(enclosureUrlString1, rssHelper.findThumbnail(imageHtmlCase1))
Assert.assertEquals(imageUrlString, rssHelper.findThumbnail(imageHtmlCase2))
}
}