Skip to content

Commit

Permalink
[EXTERNAL] Add awaitCustomerInfo / coroutines tests to `TrustedEnti…
Browse files Browse the repository at this point in the history
…tlementsInformationalModeIntegrationTest` (#1077) via @pablo-guardiola (#1107)

<!-- Thank you for contributing to Purchases! Before pressing the
"Create Pull Request" button, please provide the following: -->

### Checklist
- [ ] If applicable, unit tests
- [ ] If applicable, create follow-up issues for `purchases-ios` and
hybrids

### Motivation
**Why is this change required? What problem does it solve?**

Follow up from

#1071 (comment)

<!-- Please link to issues following this format: Resolves #999999 -->

### Description
**Describe your changes in detail**

- Adds `awaitCustomerInfo` / coroutines tests to
`TrustedEntitlementsInformationalModeIntegrationTest`

**Please describe in detail how you tested your changes**

- Run integration tests locally ✅ 

Contributed by @pablo-guardiola

Co-authored-by: pablo-guardiola <[email protected]>
  • Loading branch information
tonidero and pablo-guardiola authored Jun 29, 2023
1 parent 6423df7 commit 5a5c66d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 56 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ androidx-lifecycle-common = { module = "androidx.lifecycle:lifecycle-common", ve
androidx-lifecycle-livedata = { module = "androidx.lifecycle:lifecycle-livedata-ktx", version.ref = "lifecycle" }
androidx-lifecycle-process = { module = "androidx.lifecycle:lifecycle-process", version.ref = "lifecycle" }
androidx-lifecycle-runtime = { module = "androidx.lifecycle:lifecycle-runtime", version.ref = "lifecycle" }
androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycle" }
androidx-lifecycle-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "lifecycle" }
androidx-multidex = { module = "androidx.multidex:multidex", version.ref = "multidex" }
androidx-navigation-fragment = { module = "androidx.navigation:navigation-fragment-ktx", version.ref = "androidxNavigation" }
Expand Down
1 change: 1 addition & 0 deletions purchases/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ dependencies {
integrationTestImplementation libs.material
integrationTestImplementation libs.androidx.constraintlayout

androidTestIntegrationTestImplementation libs.androidx.lifecycle.runtime.ktx
androidTestIntegrationTestImplementation libs.androidx.test.espresso.core
androidTestIntegrationTestImplementation libs.androidx.test.runner
androidTestIntegrationTestImplementation libs.androidx.test.rules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package com.revenuecat.purchases

import android.content.Context
import android.preference.PreferenceManager
import androidx.lifecycle.lifecycleScope
import androidx.test.ext.junit.rules.activityScenarioRule
import com.revenuecat.purchases.common.BillingAbstract
import com.revenuecat.purchases.models.StoreTransaction
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.assertj.core.api.Assertions
import org.junit.After
import org.junit.BeforeClass
Expand Down Expand Up @@ -166,6 +169,14 @@ open class BasePurchasesIntegrationTest {
}
}

protected fun runTestActivityLifecycleScope(
testBody: suspend CoroutineScope.() -> Unit,
) {
activity.lifecycleScope.launch {
testBody()
}
}

private fun clearAllSharedPreferences(context: Context) {
PreferenceManager.getDefaultSharedPreferences(context).edit().clear().commit()
context.getSharedPreferences(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import com.revenuecat.purchases.ExperimentalPreviewRevenueCatPurchasesAPI
import com.revenuecat.purchases.PurchaseParams
import com.revenuecat.purchases.Purchases
import com.revenuecat.purchases.VerificationResult
import com.revenuecat.purchases.awaitCustomerInfo
import com.revenuecat.purchases.factories.StoreProductFactory
import com.revenuecat.purchases.factories.StoreTransactionFactory
import com.revenuecat.purchases.forceSigningErrors
import com.revenuecat.purchases.getCustomerInfoWith
import com.revenuecat.purchases.helpers.mockQueryProductDetails
import com.revenuecat.purchases.purchaseWith
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -35,20 +35,10 @@ class TrustedEntitlementsInformationalModeIntegrationTest : BasePurchasesIntegra
}

@Test
fun initialCustomerInfoIsVerified() {
var receivedCustomerInfo: CustomerInfo? = null
ensureBlockFinishes { latch ->
Purchases.sharedInstance.getCustomerInfoWith(
onError = { fail("should be success. Error: ${it.message}") },
onSuccess = {
receivedCustomerInfo = it
latch.countDown()
},
)
}
fun initialCustomerInfoIsVerified() = runTestActivityLifecycleScope {
val receivedCustomerInfo = Purchases.sharedInstance.awaitCustomerInfo()

assertThat(receivedCustomerInfo).isNotNull
assertThat(receivedCustomerInfo?.entitlements?.verification).isEqualTo(VerificationResult.VERIFIED)
assertThat(receivedCustomerInfo.entitlements.verification).isEqualTo(VerificationResult.VERIFIED)
}

@Test
Expand Down Expand Up @@ -79,56 +69,22 @@ class TrustedEntitlementsInformationalModeIntegrationTest : BasePurchasesIntegra
}

@Test
fun verificationChangesAfterSuccessIsNotified() {
var receivedCustomerInfo: CustomerInfo? = null
ensureBlockFinishes { latch ->
Purchases.sharedInstance.getCustomerInfoWith(
onError = { fail("should be success. Error: ${it.message}") },
onSuccess = {
receivedCustomerInfo = it
latch.countDown()
},
)
}

assertThat(receivedCustomerInfo).isNotNull
assertThat(receivedCustomerInfo?.entitlements?.verification).isEqualTo(VerificationResult.VERIFIED)

fun verificationChangesAfterSuccessIsNotified() = runTestActivityLifecycleScope {
val receivedCustomerInfo = Purchases.sharedInstance.awaitCustomerInfo()
assertThat(receivedCustomerInfo.entitlements.verification).isEqualTo(VerificationResult.VERIFIED)
Purchases.sharedInstance.forceSigningErrors = true

var receivedCustomerInfo2: CustomerInfo? = null
ensureBlockFinishes { latch ->
Purchases.sharedInstance.getCustomerInfoWith(
fetchPolicy = CacheFetchPolicy.FETCH_CURRENT,
onError = { fail("should be success. Error: ${it.message}") },
onSuccess = {
receivedCustomerInfo2 = it
latch.countDown()
},
)
}
val receivedCustomerInfo2 = Purchases.sharedInstance.awaitCustomerInfo(CacheFetchPolicy.FETCH_CURRENT)

assertThat(receivedCustomerInfo2).isNotNull
assertThat(receivedCustomerInfo2?.entitlements?.verification).isEqualTo(VerificationResult.FAILED)
assertThat(receivedCustomerInfo2.entitlements.verification).isEqualTo(VerificationResult.FAILED)
}

@Test
fun initialCustomerInfoFailsToVerify() {
fun initialCustomerInfoFailsToVerify() = runTestActivityLifecycleScope {
Purchases.sharedInstance.forceSigningErrors = true
val receivedCustomerInfo = Purchases.sharedInstance.awaitCustomerInfo()

var receivedCustomerInfo: CustomerInfo? = null
ensureBlockFinishes { latch ->
Purchases.sharedInstance.getCustomerInfoWith(
onError = { fail("should be success. Error: ${it.message}") },
onSuccess = {
receivedCustomerInfo = it
latch.countDown()
},
)
}

assertThat(receivedCustomerInfo).isNotNull
assertThat(receivedCustomerInfo?.entitlements?.verification).isEqualTo(VerificationResult.FAILED)
assertThat(receivedCustomerInfo.entitlements.verification).isEqualTo(VerificationResult.FAILED)
}

@Test
Expand Down

0 comments on commit 5a5c66d

Please sign in to comment.