Skip to content

Commit

Permalink
Fix null intent crash (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
beylmk authored Jan 11, 2022
1 parent f2464fe commit ac305e7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
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."
}

0 comments on commit ac305e7

Please sign in to comment.