Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the activity result registry testable #498

Merged
merged 4 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Intent
import android.os.Parcelable
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.ActivityResultRegistry
import androidx.activity.result.contract.ActivityResultContract
import androidx.fragment.app.Fragment
import com.getbouncer.cardscan.ui.exception.UnknownScanException
Expand Down Expand Up @@ -52,47 +53,40 @@ class CardScanSheet private constructor(private val apiKey: String) {
* is created (in the onCreate method).
*/
@JvmStatic
fun create(from: ComponentActivity, apiKey: String) =
CardScanSheet(apiKey).apply {
launcher = from.registerForActivityResult(
object : ActivityResultContract<
CardScanSheetParams,
CardScanSheetResult
>() {
override fun createIntent(
context: Context,
input: CardScanSheetParams,
) = [email protected](context, input)

override fun parseResult(
resultCode: Int,
intent: Intent?,
) = [email protected](requireNotNull(intent))
},
::onResult,
)
}
@JvmOverloads
fun create(
from: ComponentActivity,
apiKey: String,
registry: ActivityResultRegistry = from.activityResultRegistry,
) = CardScanSheet(apiKey).apply {
launcher = from.registerForActivityResult(activityResultContract, registry, ::onResult)
}

@JvmStatic
fun create(from: Fragment, apiKey: String) =
CardScanSheet(apiKey).apply {
launcher = from.registerForActivityResult(
object : ActivityResultContract<
CardScanSheetParams,
CardScanSheetResult,
>() {
override fun createIntent(
context: Context,
input: CardScanSheetParams,
) = [email protected](context, input)

override fun parseResult(
resultCode: Int,
intent: Intent?,
) = [email protected](requireNotNull(intent))
},
::onResult,
)
@JvmOverloads
fun create(
from: Fragment,
apiKey: String,
registry: ActivityResultRegistry? = null,
) = CardScanSheet(apiKey).apply {
launcher = if (registry != null) {
from.registerForActivityResult(activityResultContract, registry, ::onResult)
} else {
from.registerForActivityResult(activityResultContract, ::onResult)
}
}

private val activityResultContract =
object : ActivityResultContract<CardScanSheetParams, CardScanSheetResult>() {
override fun createIntent(
context: Context,
input: CardScanSheetParams,
) = [email protected](context, input)

override fun parseResult(
resultCode: Int,
intent: Intent?,
) = [email protected](requireNotNull(intent))
}

private fun createIntent(context: Context, input: CardScanSheetParams) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,30 +136,6 @@ class FetcherTest {
assertNull((fetchedModel as FetchedFile).file)
}

@Test
@LargeTest
fun fetchModelFromWebSignedUrl_getSignedUrlFail() = runBlocking {
Config.apiKey = "__INTEGRATION_TEST_INVALID_KEY__"

class FetcherImpl : SignedUrlModelWebFetcher(testContext) {
override val modelClass = "four_recognize"
override val modelFrameworkVersion = 1
override val modelVersion = "0.0.1.16"
override val modelFileName = "fourrecognize.tflite"
override val hash = "55eea0d57239a7e92904fb15209963f7236bd06919275bdeb0a765a94b559c97"
override val hashAlgorithm = "SHA-256"
}

// force downloading the model for this test
val fetcher = FetcherImpl()
fetcher.clearCache()

val fetchedModel = fetcher.fetchData(forImmediateUse = false, isOptional = false)
assertTrue { fetchedModel is FetchedFile }

assertNull((fetchedModel as FetchedFile).file)
}

@Test
@LargeTest
fun fetchUpgradableModelFromWeb_success() = runBlocking {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlin.test.fail

class BouncerApiTest {
Expand Down Expand Up @@ -90,26 +88,6 @@ class BouncerApiTest {
}
}

/**
* TODO: this method should use runBlockingTest instead of runBlocking. However, an issue with
* runBlockingTest currently fails when functions under test use withContext(Dispatchers.IO) or
* withContext(Dispatchers.Default).
*
* See https://github.com/Kotlin/kotlinx.coroutines/issues/1204 for details.
*/
@Test
@LargeTest
fun validateApiKey() = runBlocking {
when (val result = validateApiKey(appContext)) {
is NetworkResult.Success -> {
assertEquals(200, result.responseCode)
assertTrue(result.body.isApiKeyValid)
assertNull(result.body.keyInvalidReason)
}
else -> fail("network result was not success: $result")
}
}

/**
* TODO: this method should use runBlockingTest instead of runBlocking. However, an issue with
* runBlockingTest currently fails when functions under test use withContext(Dispatchers.IO) or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class PaymentCardTest {
fun isValidExpiry() {
val expDay = "01"
val expMonth = "02"
val expYear = "2022"
val expYear = "2032"

assertTrue { isValidExpiry(expDay, expMonth, expYear) }
assertTrue { isValidExpiry(null, expMonth, expYear) }
Expand Down