From 5a5c66de9de29740a66bfa3c1bf41ddc287927af Mon Sep 17 00:00:00 2001 From: Toni Rico Date: Wed, 28 Jun 2023 23:00:10 -0700 Subject: [PATCH] [EXTERNAL] Add `awaitCustomerInfo` / coroutines tests to `TrustedEntitlementsInformationalModeIntegrationTest` (#1077) via @pablo-guardiola (#1107) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### 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 https://github.com/RevenueCat/purchases-android/pull/1071#discussion_r1232485138 ### 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 <131195486+pablo-guardiola@users.noreply.github.com> --- gradle/libs.versions.toml | 1 + purchases/build.gradle | 1 + .../purchases/BasePurchasesIntegrationTest.kt | 11 +++ ...lementsInformationalModeIntegrationTest.kt | 68 ++++--------------- 4 files changed, 25 insertions(+), 56 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 41b8c8667c..2d3b2ef1bb 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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" } diff --git a/purchases/build.gradle b/purchases/build.gradle index 4dbf60f42b..aea492303d 100644 --- a/purchases/build.gradle +++ b/purchases/build.gradle @@ -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 diff --git a/purchases/src/androidTestIntegrationTest/kotlin/com/revenuecat/purchases/BasePurchasesIntegrationTest.kt b/purchases/src/androidTestIntegrationTest/kotlin/com/revenuecat/purchases/BasePurchasesIntegrationTest.kt index cda3e5d7d8..cb3b8dd570 100644 --- a/purchases/src/androidTestIntegrationTest/kotlin/com/revenuecat/purchases/BasePurchasesIntegrationTest.kt +++ b/purchases/src/androidTestIntegrationTest/kotlin/com/revenuecat/purchases/BasePurchasesIntegrationTest.kt @@ -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 @@ -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( diff --git a/purchases/src/androidTestIntegrationTest/kotlin/com/revenuecat/purchases/trustedentitlements/TrustedEntitlementsInformationalModeIntegrationTest.kt b/purchases/src/androidTestIntegrationTest/kotlin/com/revenuecat/purchases/trustedentitlements/TrustedEntitlementsInformationalModeIntegrationTest.kt index aada6bbc85..99a43de86d 100644 --- a/purchases/src/androidTestIntegrationTest/kotlin/com/revenuecat/purchases/trustedentitlements/TrustedEntitlementsInformationalModeIntegrationTest.kt +++ b/purchases/src/androidTestIntegrationTest/kotlin/com/revenuecat/purchases/trustedentitlements/TrustedEntitlementsInformationalModeIntegrationTest.kt @@ -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 @@ -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 @@ -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