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

Fix null intent crash #430

Merged
merged 6 commits into from
Jan 11, 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 @@ -222,6 +222,9 @@ class BillingWrapper(
activity: Activity,
params: BillingFlowParams
) {
if (activity.intent == null) {
log(LogIntent.WARNING, BillingStrings.NULL_ACTIVITY_INTENT)
}
withConnectedClient {
launchBillingFlow(activity, params)
.takeIf { billingResult -> billingResult.responseCode != BillingClient.BillingResponseCode.OK }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.revenuecat.purchases.google

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Handler
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.billingclient.api.AcknowledgePurchaseParams
Expand Down Expand Up @@ -80,6 +81,7 @@ class BillingWrapperTest {

private val billingClientOKResult = BillingClient.BillingResponseCode.OK.buildResult()
private val appUserId = "jerry"
private var mockActivity = mockk<Activity>()

@Before
fun setup() {
Expand Down Expand Up @@ -145,6 +147,10 @@ class BillingWrapperTest {
onConnectedCalled = true
}
}

every {
mockActivity.intent
} returns Intent()
}

@Test
Expand Down Expand Up @@ -252,11 +258,9 @@ class BillingWrapperTest {

val skuDetails = stubSkuDetails(productId = "product_a")

val activity: Activity = mockk()

billingClientStateListener!!.onBillingSetupFinished(BillingClient.BillingResponseCode.OK.buildResult())
wrapper.makePurchaseAsync(
activity,
mockActivity,
appUserId,
skuDetails.toStoreProduct(),
mockReplaceSkuInfo(),
Expand All @@ -265,7 +269,7 @@ class BillingWrapperTest {

verify {
mockClient.launchBillingFlow(
eq(activity),
eq(mockActivity),
any()
)
}
Expand Down Expand Up @@ -305,12 +309,11 @@ class BillingWrapperTest {
@BillingClient.SkuType val skuType = BillingClient.SkuType.SUBS

val upgradeInfo = mockReplaceSkuInfo()
val activity: Activity = mockk()
val skuDetails = stubSkuDetails(productId = sku, type = skuType)

val slot = slot<BillingFlowParams>()
every {
mockClient.launchBillingFlow(eq(activity), capture(slot))
mockClient.launchBillingFlow(eq(mockActivity), capture(slot))
} answers {
val capturedSkuDetails = skuDetailsSlot.captured

Expand All @@ -324,7 +327,7 @@ class BillingWrapperTest {

billingClientStateListener!!.onBillingSetupFinished(BillingClient.BillingResponseCode.OK.buildResult())
wrapper.makePurchaseAsync(
activity,
mockActivity,
appUserId,
skuDetails.toStoreProduct(),
upgradeInfo,
Expand All @@ -337,7 +340,7 @@ class BillingWrapperTest {
val mockBuilder = setUpForObfuscatedAccountIDTests()

wrapper.makePurchaseAsync(
mockk(),
mockActivity,
appUserId,
stubSkuDetails(productId = "product_a").toStoreProduct(),
null,
Expand All @@ -357,7 +360,7 @@ class BillingWrapperTest {
val mockBuilder = setUpForObfuscatedAccountIDTests()

wrapper.makePurchaseAsync(
mockk(),
mockActivity,
appUserId,
stubSkuDetails(productId = "product_a").toStoreProduct(),
mockReplaceSkuInfo(),
Expand All @@ -379,27 +382,26 @@ class BillingWrapperTest {

every { mockClient.isReady } returns false

val activity: Activity = mockk()
val skuDetails = stubSkuDetails(productId = "product_a")

wrapper.makePurchaseAsync(
activity,
mockActivity,
appUserId,
skuDetails.toStoreProduct(),
mockReplaceSkuInfo(),
null
)

verify(exactly = 0) {
mockClient.launchBillingFlow(eq(activity), any())
mockClient.launchBillingFlow(eq(mockActivity), any())
}

every { mockClient.isReady } returns true

billingClientStateListener!!.onBillingSetupFinished(BillingClient.BillingResponseCode.OK.buildResult())

verify(exactly = 1) {
mockClient.launchBillingFlow(eq(activity), any())
mockClient.launchBillingFlow(eq(mockActivity), any())
}
}

Expand All @@ -413,10 +415,8 @@ class BillingWrapperTest {

val skuDetails = stubSkuDetails(productId = "product_a")

val activity: Activity = mockk()

wrapper.makePurchaseAsync(
activity,
mockActivity,
appUserId,
skuDetails.toStoreProduct(),
mockReplaceSkuInfo(),
Expand Down Expand Up @@ -809,11 +809,9 @@ class BillingWrapperTest {

val skuDetails = stubSkuDetails(productId = "product_a")

val activity: Activity = mockk()

billingClientStateListener!!.onBillingSetupFinished(BillingClient.BillingResponseCode.OK.buildResult())
wrapper.makePurchaseAsync(
activity,
mockActivity,
appUserId,
skuDetails.toStoreProduct(),
mockReplaceSkuInfo(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ object BillingStrings {
"PurchaseHistoryRecord, but only one will be used."
const val BILLING_PURCHASE_MORE_THAN_ONE_SKU = "There's more than one sku in the PurchaseHistoryRecord, " +
"but only one will be used."
const val NULL_ACTIVITY_INTENT = "Activity passed into launchBillingFlow has a null intent, which may cause " +
"a crash. See https://github.com/RevenueCat/purchases-android/issues/381 for more information."
}