Ryan Harg
3 years ago
21 changed files with 465 additions and 131 deletions
@ -0,0 +1,75 @@ |
|||||||
|
package audio.funkwhale.ffa.playback |
||||||
|
|
||||||
|
import android.content.Context |
||||||
|
import android.net.Uri |
||||||
|
import audio.funkwhale.ffa.utils.OAuth |
||||||
|
import audio.funkwhale.util.MockKJUnitRunner |
||||||
|
import com.google.android.exoplayer2.upstream.DataSpec |
||||||
|
import com.google.android.exoplayer2.upstream.HttpDataSource |
||||||
|
import com.google.android.exoplayer2.upstream.TransferListener |
||||||
|
import io.mockk.every |
||||||
|
import io.mockk.impl.annotations.InjectMockKs |
||||||
|
import io.mockk.impl.annotations.MockK |
||||||
|
import io.mockk.mockk |
||||||
|
import io.mockk.verify |
||||||
|
import org.junit.Test |
||||||
|
import org.junit.runner.RunWith |
||||||
|
import strikt.api.expectThat |
||||||
|
import strikt.assertions.isEqualTo |
||||||
|
|
||||||
|
@RunWith(MockKJUnitRunner::class) |
||||||
|
class OAuthDatasourceTest { |
||||||
|
|
||||||
|
@InjectMockKs |
||||||
|
private lateinit var datasource: OAuthDatasource |
||||||
|
|
||||||
|
@MockK |
||||||
|
private lateinit var context: Context |
||||||
|
|
||||||
|
@MockK |
||||||
|
private lateinit var http: HttpDataSource |
||||||
|
|
||||||
|
@MockK |
||||||
|
private lateinit var oAuth: OAuth |
||||||
|
|
||||||
|
private var dataSpec: DataSpec = DataSpec(Uri.EMPTY) |
||||||
|
|
||||||
|
@Test |
||||||
|
fun `open() should set accessToken and delegate to http dataSource`() { |
||||||
|
every { http.open(any()) } returns 0 |
||||||
|
every { oAuth.tryRefreshAccessToken(any(), any()) } returns true |
||||||
|
every { oAuth.state().accessToken } returns "accessToken" |
||||||
|
|
||||||
|
datasource.open(dataSpec) |
||||||
|
verify { http.open(dataSpec) } |
||||||
|
verify { http.setRequestProperty("Authorization", "Bearer accessToken") } |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun `close() should delegate to http dataSource`() { |
||||||
|
datasource.close() |
||||||
|
verify { http.close() } |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun `addTransferListener() should delegate to http dataSource`() { |
||||||
|
val transferListener = mockk<TransferListener>() |
||||||
|
datasource.addTransferListener(transferListener) |
||||||
|
verify { http.addTransferListener(transferListener) } |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun `read() should delegate to http dataSource`() { |
||||||
|
every { http.read(any(), any(), any()) } returns 0 |
||||||
|
datasource.read("123".encodeToByteArray(), 1, 2) |
||||||
|
verify { http.read("123".encodeToByteArray(), 1, 2) } |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun `getUri() should delegate to http dataSource`() { |
||||||
|
every { http.uri } returns Uri.EMPTY |
||||||
|
val result = datasource.uri |
||||||
|
verify { http.uri } |
||||||
|
expectThat(result).isEqualTo(Uri.EMPTY) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,58 @@ |
|||||||
|
package audio.funkwhale.util |
||||||
|
|
||||||
|
import io.mockk.MockKAnnotations |
||||||
|
import io.mockk.clearAllMocks |
||||||
|
import org.junit.Test |
||||||
|
import org.junit.runner.Description |
||||||
|
import org.junit.runner.Runner |
||||||
|
import org.junit.runner.notification.Failure |
||||||
|
import org.junit.runner.notification.RunNotifier |
||||||
|
import java.lang.reflect.Method |
||||||
|
|
||||||
|
class MockKJUnitRunner(private val testClass: Class<*>) : Runner() { |
||||||
|
|
||||||
|
private val methodDescriptions: MutableMap<Method, Description> = mutableMapOf() |
||||||
|
|
||||||
|
init { |
||||||
|
// Build method/descriptions map |
||||||
|
testClass.methods |
||||||
|
.map { method -> |
||||||
|
val annotation: Annotation? = method.getAnnotation(Test::class.java) |
||||||
|
method to annotation |
||||||
|
} |
||||||
|
.filter { (_, annotation) -> |
||||||
|
annotation != null |
||||||
|
} |
||||||
|
.map { (method, annotation) -> |
||||||
|
val desc = Description.createTestDescription(testClass, method.name, annotation) |
||||||
|
method to desc |
||||||
|
} |
||||||
|
.forEach { (method, desc) -> methodDescriptions[method] = desc } |
||||||
|
} |
||||||
|
|
||||||
|
override fun getDescription(): Description { |
||||||
|
val description = Description.createSuiteDescription( |
||||||
|
testClass.name, *testClass.annotations |
||||||
|
) |
||||||
|
methodDescriptions.values.forEach { description.addChild(it) } |
||||||
|
return description |
||||||
|
} |
||||||
|
|
||||||
|
override fun run(notifier: RunNotifier?) { |
||||||
|
val testObject = testClass.newInstance() |
||||||
|
MockKAnnotations.init(testObject, relaxUnitFun = true) |
||||||
|
|
||||||
|
methodDescriptions |
||||||
|
.onEach { (_, _) -> clearAllMocks() } |
||||||
|
.onEach { (_, desc) -> notifier!!.fireTestStarted(desc) } |
||||||
|
.forEach { (method, desc) -> |
||||||
|
try { |
||||||
|
method.invoke(testObject) |
||||||
|
} catch (e: Throwable) { |
||||||
|
notifier!!.fireTestFailure(Failure(desc, e.cause)) |
||||||
|
} finally { |
||||||
|
notifier!!.fireTestFinished(desc) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue