-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into contact-information-feature
# Conflicts: # CHANGELOG.md
- Loading branch information
Showing
1,221 changed files
with
6,870 additions
and
1,795 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
BraintreeCore/src/main/java/com/braintreepayments/api/core/GetReturnLinkUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.braintreepayments.api.core | ||
|
||
import android.content.Intent | ||
import android.content.pm.PackageManager | ||
import android.net.Uri | ||
import androidx.annotation.RestrictTo | ||
import com.braintreepayments.api.core.GetReturnLinkUseCase.ReturnLinkResult | ||
|
||
/** | ||
* Use case that returns a return link that should be used for navigating from App Switch / CCT back into the merchant | ||
* app. It handles both App Links and Deep Links. | ||
* | ||
* If a user unchecks the "Open supported links" checkbox in the Android OS settings for the merchant's app. If this | ||
* setting is unchecked, this use case will return [ReturnLinkResult.DeepLink], otherwise [ReturnLinkResult.AppLink] | ||
* will be returned. | ||
*/ | ||
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) | ||
class GetReturnLinkUseCase(private val merchantRepository: MerchantRepository) { | ||
|
||
sealed class ReturnLinkResult { | ||
data class AppLink(val appLinkReturnUri: Uri) : ReturnLinkResult() | ||
|
||
data class DeepLink(val deepLinkFallbackUrlScheme: String) : ReturnLinkResult() | ||
|
||
data class Failure(val exception: Exception) : ReturnLinkResult() | ||
} | ||
|
||
operator fun invoke(): ReturnLinkResult { | ||
val context = merchantRepository.applicationContext | ||
val intent = Intent(Intent.ACTION_VIEW, merchantRepository.appLinkReturnUri).apply { | ||
addCategory(Intent.CATEGORY_BROWSABLE) | ||
} | ||
val resolvedActivity = context.packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) | ||
return if (resolvedActivity?.activityInfo?.packageName == context.packageName) { | ||
merchantRepository.appLinkReturnUri?.let { | ||
ReturnLinkResult.AppLink(it) | ||
} ?: run { | ||
ReturnLinkResult.Failure(BraintreeException("App Link Return Uri is null")) | ||
} | ||
} else { | ||
merchantRepository.deepLinkFallbackUrlScheme?.let { | ||
ReturnLinkResult.DeepLink(it) | ||
} ?: run { | ||
ReturnLinkResult.Failure(BraintreeException("Deep Link fallback return url is null")) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
BraintreeCore/src/test/java/com/braintreepayments/api/core/GetReturnLinkUseCaseUnitTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.braintreepayments.api.core | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.pm.ActivityInfo | ||
import android.content.pm.ResolveInfo | ||
import android.net.Uri | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import org.junit.Before | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class GetReturnLinkUseCaseUnitTest { | ||
|
||
private val merchantRepository: MerchantRepository = mockk(relaxed = true) | ||
private val context: Context = mockk(relaxed = true) | ||
private val resolveInfo = ResolveInfo() | ||
private val activityInfo = ActivityInfo() | ||
private val contextPackageName = "context.package.name" | ||
private val appLinkReturnUri = Uri.parse("https://example.com") | ||
private val deepLinkFallbackUrlScheme = "com.braintreepayments.demo" | ||
|
||
lateinit var subject: GetReturnLinkUseCase | ||
|
||
@Before | ||
fun setUp() { | ||
every { merchantRepository.applicationContext } returns context | ||
every { merchantRepository.appLinkReturnUri } returns appLinkReturnUri | ||
every { merchantRepository.deepLinkFallbackUrlScheme } returns deepLinkFallbackUrlScheme | ||
every { context.packageName } returns contextPackageName | ||
resolveInfo.activityInfo = activityInfo | ||
every { context.packageManager.resolveActivity(any<Intent>(), any<Int>()) } returns resolveInfo | ||
|
||
subject = GetReturnLinkUseCase(merchantRepository) | ||
} | ||
|
||
@Test | ||
fun `when invoke is called and app link is available, APP_LINK is returned`() { | ||
activityInfo.packageName = "context.package.name" | ||
|
||
val result = subject() | ||
|
||
assertEquals(GetReturnLinkUseCase.ReturnLinkResult.AppLink(appLinkReturnUri), result) | ||
} | ||
|
||
@Test | ||
fun `when invoke is called and app link is not available, DEEP_LINK is returned`() { | ||
activityInfo.packageName = "different.package.name" | ||
|
||
val result = subject() | ||
|
||
assertEquals(GetReturnLinkUseCase.ReturnLinkResult.DeepLink(deepLinkFallbackUrlScheme), result) | ||
} | ||
|
||
@Test | ||
fun `when invoke is called and deep link is available but null, Failure is returned`() { | ||
activityInfo.packageName = "different.package.name" | ||
every { merchantRepository.deepLinkFallbackUrlScheme } returns null | ||
|
||
val result = subject() | ||
|
||
assertTrue { result is GetReturnLinkUseCase.ReturnLinkResult.Failure } | ||
assertEquals( | ||
"Deep Link fallback return url is null", | ||
(result as GetReturnLinkUseCase.ReturnLinkResult.Failure).exception.message | ||
) | ||
} | ||
|
||
@Test | ||
fun `when invoke is called and app link is available but null, Failure is returned`() { | ||
activityInfo.packageName = "context.package.name" | ||
every { merchantRepository.appLinkReturnUri } returns null | ||
|
||
val result = subject() | ||
|
||
assertTrue { result is GetReturnLinkUseCase.ReturnLinkResult.Failure } | ||
assertEquals( | ||
"App Link Return Uri is null", | ||
(result as GetReturnLinkUseCase.ReturnLinkResult.Failure).exception.message | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.