diff --git a/app/src/androidTest/java/org/tasks/billing/InventoryTest.kt b/app/src/androidTest/java/org/tasks/billing/InventoryTest.kt index e7846a686..b436e821a 100644 --- a/app/src/androidTest/java/org/tasks/billing/InventoryTest.kt +++ b/app/src/androidTest/java/org/tasks/billing/InventoryTest.kt @@ -60,7 +60,15 @@ class InventoryTest : InjectingTestCase() { private fun withPurchases(vararg purchases: String) { preferences.setPurchases(purchases.toHashSet()) - inventory = Inventory(context, preferences, signatureVerifier, localBroadcastManager, caldavDao) + runOnMainSync { + inventory = Inventory( + context, + preferences, + signatureVerifier, + localBroadcastManager, + caldavDao + ) + } } companion object { diff --git a/app/src/androidTest/java/org/tasks/injection/InjectingTestCase.kt b/app/src/androidTest/java/org/tasks/injection/InjectingTestCase.kt index 994ec84c4..3898fde8d 100644 --- a/app/src/androidTest/java/org/tasks/injection/InjectingTestCase.kt +++ b/app/src/androidTest/java/org/tasks/injection/InjectingTestCase.kt @@ -2,6 +2,7 @@ package org.tasks.injection import android.content.Context import androidx.test.core.app.ApplicationProvider.getApplicationContext +import androidx.test.platform.app.InstrumentationRegistry import dagger.hilt.android.testing.HiltAndroidRule import org.junit.Before import org.junit.Rule @@ -15,6 +16,9 @@ abstract class InjectingTestCase { hiltRule.inject() } + protected fun runOnMainSync(runnable: Runnable) = + InstrumentationRegistry.getInstrumentation().runOnMainSync(runnable) + protected val context: Context get() = getApplicationContext() } \ No newline at end of file diff --git a/app/src/main/java/org/tasks/billing/Inventory.kt b/app/src/main/java/org/tasks/billing/Inventory.kt index a6dd387ff..dafc3a366 100644 --- a/app/src/main/java/org/tasks/billing/Inventory.kt +++ b/app/src/main/java/org/tasks/billing/Inventory.kt @@ -3,6 +3,7 @@ package org.tasks.billing import android.content.Context import android.content.Intent import android.net.Uri +import android.os.Handler import androidx.lifecycle.MutableLiveData import dagger.hilt.android.qualifiers.ApplicationContext import org.tasks.BuildConfig @@ -109,6 +110,12 @@ class Inventory @Inject constructor( } init { - verifyAndAdd(preferences.purchases) + val runnable = { verifyAndAdd(preferences.purchases) } + val mainLooper = context.mainLooper + if (mainLooper.isCurrentThread) { + runnable() + } else { + Handler(context.mainLooper).post(runnable) + } } }