Skip to content

Commit

Permalink
Merge pull request #498 from getbouncer/awush/make_activity_registry_…
Browse files Browse the repository at this point in the history
…testable

Make the activity result registry testable
  • Loading branch information
awush-stripe authored Mar 22, 2022
2 parents 2b9ca06 + 10a2e12 commit 454b180
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 86 deletions.
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,
) = this@Companion.createIntent(context, input)

override fun parseResult(
resultCode: Int,
intent: Intent?,
) = this@Companion.parseResult(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,
) = this@Companion.createIntent(context, input)

override fun parseResult(
resultCode: Int,
intent: Intent?,
) = this@Companion.parseResult(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,
) = this@Companion.createIntent(context, input)

override fun parseResult(
resultCode: Int,
intent: Intent?,
) = this@Companion.parseResult(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

0 comments on commit 454b180

Please sign in to comment.