|
| 1 | +package com.flashcardsopensourceapp.app.marketing.screenshots |
| 2 | + |
| 3 | +import android.app.LocaleConfig |
| 4 | +import android.app.LocaleManager |
| 5 | +import android.content.Context |
| 6 | +import android.os.LocaleList |
| 7 | +import androidx.test.core.app.ApplicationProvider |
| 8 | +import androidx.test.platform.app.InstrumentationRegistry |
| 9 | +import com.flashcardsopensourceapp.app.AppStateResetRule |
| 10 | +import com.flashcardsopensourceapp.app.FlashcardsApplication |
| 11 | +import kotlinx.coroutines.runBlocking |
| 12 | +import kotlinx.coroutines.withTimeout |
| 13 | + |
| 14 | +private const val localeApplyTimeoutMillis: Long = 5_000L |
| 15 | +private const val marketingScreenshotGuestCleanupTimeoutMillis: Long = 20_000L |
| 16 | + |
| 17 | +class MarketingScreenshotAppStateResetRule : AppStateResetRule() { |
| 18 | + override fun before() { |
| 19 | + runRemoteCleanupThenReset( |
| 20 | + resetAction = { |
| 21 | + applyConfiguredMarketingScreenshotLocaleOverride() |
| 22 | + super.before() |
| 23 | + } |
| 24 | + ) |
| 25 | + } |
| 26 | + |
| 27 | + override fun after() { |
| 28 | + runRemoteCleanupThenReset( |
| 29 | + resetAction = { |
| 30 | + super.after() |
| 31 | + clearConfiguredMarketingScreenshotLocaleOverride() |
| 32 | + } |
| 33 | + ) |
| 34 | + } |
| 35 | + |
| 36 | + private fun runRemoteCleanupThenReset(resetAction: () -> Unit) { |
| 37 | + var primaryFailure: Throwable? = null |
| 38 | + |
| 39 | + try { |
| 40 | + deleteStoredGuestCloudSessionIfPresent() |
| 41 | + } catch (error: Throwable) { |
| 42 | + primaryFailure = error |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + resetAction() |
| 47 | + } catch (error: Throwable) { |
| 48 | + if (primaryFailure != null) { |
| 49 | + primaryFailure.addSuppressed(error) |
| 50 | + } else { |
| 51 | + primaryFailure = error |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if (primaryFailure != null) { |
| 56 | + throw primaryFailure |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private fun deleteStoredGuestCloudSessionIfPresent() { |
| 61 | + val context: Context = ApplicationProvider.getApplicationContext<Context>() |
| 62 | + val application = context as FlashcardsApplication |
| 63 | + |
| 64 | + runBlocking { |
| 65 | + withTimeout(marketingScreenshotGuestCleanupTimeoutMillis) { |
| 66 | + // Marketing screenshot seeding creates a real guest cloud workspace, |
| 67 | + // so the remote session must be deleted before the local reset |
| 68 | + // drops the only stored guest token. |
| 69 | + val appGraph = requireNotNull(application.appGraphOrNull) { |
| 70 | + "App graph is unavailable for marketing screenshot guest cleanup." |
| 71 | + } |
| 72 | + appGraph.deleteStoredGuestCloudSessionIfPresent() |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private fun applyConfiguredMarketingScreenshotLocaleOverride() { |
| 78 | + val localeConfig = configuredMarketingScreenshotLocaleConfigOrNull() ?: return |
| 79 | + val context = ApplicationProvider.getApplicationContext<Context>() |
| 80 | + val localeManager = context.getSystemService(LocaleManager::class.java) |
| 81 | + ?: throw IllegalStateException("LocaleManager was unavailable while applying screenshot locale.") |
| 82 | + val localeList = LocaleList.forLanguageTags(localeConfig.appLocaleTag) |
| 83 | + |
| 84 | + InstrumentationRegistry.getInstrumentation().runOnMainSync { |
| 85 | + localeManager.overrideLocaleConfig = LocaleConfig(localeList) |
| 86 | + localeManager.applicationLocales = localeList |
| 87 | + } |
| 88 | + waitForOverrideLocaleConfigState( |
| 89 | + localeManager = localeManager, |
| 90 | + expectedLanguageTags = localeConfig.appLocaleTag, |
| 91 | + phase = "applying marketing screenshot override locale config '${localeConfig.localePrefix}'" |
| 92 | + ) |
| 93 | + waitForLocaleState( |
| 94 | + localeManager = localeManager, |
| 95 | + expectedLanguageTags = localeConfig.appLocaleTag, |
| 96 | + phase = "applying marketing screenshot locale '${localeConfig.localePrefix}'" |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + private fun clearConfiguredMarketingScreenshotLocaleOverride() { |
| 101 | + val localeConfig = configuredMarketingScreenshotLocaleConfigOrNull() ?: return |
| 102 | + val context = ApplicationProvider.getApplicationContext<Context>() |
| 103 | + val localeManager = context.getSystemService(LocaleManager::class.java) |
| 104 | + ?: throw IllegalStateException("LocaleManager was unavailable while clearing screenshot locale.") |
| 105 | + |
| 106 | + InstrumentationRegistry.getInstrumentation().runOnMainSync { |
| 107 | + localeManager.applicationLocales = LocaleList.getEmptyLocaleList() |
| 108 | + localeManager.overrideLocaleConfig = null |
| 109 | + } |
| 110 | + waitForLocaleState( |
| 111 | + localeManager = localeManager, |
| 112 | + expectedLanguageTags = "", |
| 113 | + phase = "clearing marketing screenshot locale '${localeConfig.localePrefix}'" |
| 114 | + ) |
| 115 | + waitForOverrideLocaleConfigState( |
| 116 | + localeManager = localeManager, |
| 117 | + expectedLanguageTags = null, |
| 118 | + phase = "clearing marketing screenshot override locale config '${localeConfig.localePrefix}'" |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + private fun waitForLocaleState( |
| 123 | + localeManager: LocaleManager, |
| 124 | + expectedLanguageTags: String, |
| 125 | + phase: String |
| 126 | + ) { |
| 127 | + val startTimeMillis = System.currentTimeMillis() |
| 128 | + do { |
| 129 | + InstrumentationRegistry.getInstrumentation().waitForIdleSync() |
| 130 | + if (localeManager.applicationLocales.toLanguageTags() == expectedLanguageTags) { |
| 131 | + return |
| 132 | + } |
| 133 | + } while (System.currentTimeMillis() - startTimeMillis < localeApplyTimeoutMillis) |
| 134 | + |
| 135 | + throw IllegalStateException( |
| 136 | + "Timed out after $localeApplyTimeoutMillis ms while $phase. " + |
| 137 | + "expectedLanguageTags='$expectedLanguageTags' actualLanguageTags='${localeManager.applicationLocales.toLanguageTags()}'." |
| 138 | + ) |
| 139 | + } |
| 140 | + |
| 141 | + private fun waitForOverrideLocaleConfigState( |
| 142 | + localeManager: LocaleManager, |
| 143 | + expectedLanguageTags: String?, |
| 144 | + phase: String |
| 145 | + ) { |
| 146 | + val startTimeMillis = System.currentTimeMillis() |
| 147 | + do { |
| 148 | + InstrumentationRegistry.getInstrumentation().waitForIdleSync() |
| 149 | + val actualLanguageTags = localeManager.overrideLocaleConfig |
| 150 | + ?.supportedLocales |
| 151 | + ?.toLanguageTags() |
| 152 | + if (actualLanguageTags == expectedLanguageTags) { |
| 153 | + return |
| 154 | + } |
| 155 | + } while (System.currentTimeMillis() - startTimeMillis < localeApplyTimeoutMillis) |
| 156 | + |
| 157 | + val actualLanguageTags = localeManager.overrideLocaleConfig |
| 158 | + ?.supportedLocales |
| 159 | + ?.toLanguageTags() |
| 160 | + throw IllegalStateException( |
| 161 | + "Timed out after $localeApplyTimeoutMillis ms while $phase. " + |
| 162 | + "expectedLanguageTags='$expectedLanguageTags' actualLanguageTags='$actualLanguageTags'." |
| 163 | + ) |
| 164 | + } |
| 165 | +} |
0 commit comments