From 662c5e2cfb54d357a1280aa59e7d8be11a442ef2 Mon Sep 17 00:00:00 2001 From: Adam Wushensky Date: Tue, 22 Mar 2022 14:44:50 -0700 Subject: [PATCH 1/4] Make the activity result registry testable --- .../getbouncer/cardscan/ui/CardScanSheet.kt | 70 ++++++++----------- 1 file changed, 31 insertions(+), 39 deletions(-) diff --git a/cardscan-ui/src/main/java/com/getbouncer/cardscan/ui/CardScanSheet.kt b/cardscan-ui/src/main/java/com/getbouncer/cardscan/ui/CardScanSheet.kt index 983eb9e4..c7d0c8b3 100644 --- a/cardscan-ui/src/main/java/com/getbouncer/cardscan/ui/CardScanSheet.kt +++ b/cardscan-ui/src/main/java/com/getbouncer/cardscan/ui/CardScanSheet.kt @@ -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 @@ -52,47 +53,38 @@ 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, - ) - } + 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, - ) + 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() { + 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) = From 15c0499f16a348742dae5605f6d371a6230e9082 Mon Sep 17 00:00:00 2001 From: Adam Wushensky Date: Tue, 22 Mar 2022 14:51:00 -0700 Subject: [PATCH 2/4] Add JvmOverloads --- .../src/main/java/com/getbouncer/cardscan/ui/CardScanSheet.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cardscan-ui/src/main/java/com/getbouncer/cardscan/ui/CardScanSheet.kt b/cardscan-ui/src/main/java/com/getbouncer/cardscan/ui/CardScanSheet.kt index c7d0c8b3..7c3cbb87 100644 --- a/cardscan-ui/src/main/java/com/getbouncer/cardscan/ui/CardScanSheet.kt +++ b/cardscan-ui/src/main/java/com/getbouncer/cardscan/ui/CardScanSheet.kt @@ -53,6 +53,7 @@ class CardScanSheet private constructor(private val apiKey: String) { * is created (in the onCreate method). */ @JvmStatic + @JvmOverloads fun create( from: ComponentActivity, apiKey: String, @@ -62,6 +63,7 @@ class CardScanSheet private constructor(private val apiKey: String) { } @JvmStatic + @JvmOverloads fun create( from: Fragment, apiKey: String, From 5655711f2424a3081735261bc71db60ebfd08c38 Mon Sep 17 00:00:00 2001 From: Adam Wushensky Date: Tue, 22 Mar 2022 15:17:27 -0700 Subject: [PATCH 3/4] Remove old, broken tests --- .../getbouncer/scan/framework/FetcherTest.kt | 24 ------------------- .../scan/framework/api/BouncerApiTest.kt | 20 ---------------- .../scan/payment/card/PaymentCardTest.kt | 2 +- 3 files changed, 1 insertion(+), 45 deletions(-) diff --git a/scan-framework/src/androidTest/java/com/getbouncer/scan/framework/FetcherTest.kt b/scan-framework/src/androidTest/java/com/getbouncer/scan/framework/FetcherTest.kt index b53a8d65..cd168021 100644 --- a/scan-framework/src/androidTest/java/com/getbouncer/scan/framework/FetcherTest.kt +++ b/scan-framework/src/androidTest/java/com/getbouncer/scan/framework/FetcherTest.kt @@ -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 { diff --git a/scan-framework/src/androidTest/java/com/getbouncer/scan/framework/api/BouncerApiTest.kt b/scan-framework/src/androidTest/java/com/getbouncer/scan/framework/api/BouncerApiTest.kt index 256966cf..398c3139 100644 --- a/scan-framework/src/androidTest/java/com/getbouncer/scan/framework/api/BouncerApiTest.kt +++ b/scan-framework/src/androidTest/java/com/getbouncer/scan/framework/api/BouncerApiTest.kt @@ -90,26 +90,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 diff --git a/scan-payment/src/test/java/com/getbouncer/scan/payment/card/PaymentCardTest.kt b/scan-payment/src/test/java/com/getbouncer/scan/payment/card/PaymentCardTest.kt index a5b8ac20..6d9ce982 100644 --- a/scan-payment/src/test/java/com/getbouncer/scan/payment/card/PaymentCardTest.kt +++ b/scan-payment/src/test/java/com/getbouncer/scan/payment/card/PaymentCardTest.kt @@ -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) } From 10a2e12c6367fdd69a677db0567de6a005fed0c0 Mon Sep 17 00:00:00 2001 From: Adam Wushensky Date: Tue, 22 Mar 2022 15:44:52 -0700 Subject: [PATCH 4/4] Fix lint --- .../java/com/getbouncer/scan/framework/api/BouncerApiTest.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/scan-framework/src/androidTest/java/com/getbouncer/scan/framework/api/BouncerApiTest.kt b/scan-framework/src/androidTest/java/com/getbouncer/scan/framework/api/BouncerApiTest.kt index 398c3139..cb4f23d3 100644 --- a/scan-framework/src/androidTest/java/com/getbouncer/scan/framework/api/BouncerApiTest.kt +++ b/scan-framework/src/androidTest/java/com/getbouncer/scan/framework/api/BouncerApiTest.kt @@ -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 {