-
Notifications
You must be signed in to change notification settings - Fork 3
test: add calculator widget device tests #944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ovitrif
wants to merge
3
commits into
master
Choose a base branch
from
test/calculator-widget-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+351
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
app/src/androidTest/java/to/bitkit/test/annotations/DeviceUiIntegration.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package to.bitkit.test.annotations | ||
|
|
||
| @Retention(AnnotationRetention.RUNTIME) | ||
| @Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) | ||
| annotation class DeviceUiIntegration |
346 changes: 346 additions & 0 deletions
346
...androidTest/java/to/bitkit/ui/screens/widgets/calculator/CalculatorCardIntegrationTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,346 @@ | ||
| package to.bitkit.ui.screens.widgets.calculator | ||
|
|
||
| import androidx.compose.foundation.layout.fillMaxWidth | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.test.assertTextContains | ||
| import androidx.compose.ui.test.assertIsDisplayed | ||
| import androidx.compose.ui.test.hasSetTextAction | ||
| import androidx.compose.ui.test.hasText | ||
| import androidx.compose.ui.test.junit4.createComposeRule | ||
| import androidx.compose.ui.test.onRoot | ||
| import androidx.compose.ui.test.performTextClearance | ||
| import androidx.compose.ui.test.performTextInput | ||
| import androidx.compose.ui.test.printToString | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import androidx.lifecycle.ViewModelStore | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.android.testing.HiltAndroidRule | ||
| import dagger.hilt.android.testing.HiltAndroidTest | ||
| import dagger.hilt.android.testing.UninstallModules | ||
| import dagger.hilt.components.SingletonComponent | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.junit.After | ||
| import org.junit.Before | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import to.bitkit.data.AppCacheData | ||
| import to.bitkit.data.CacheStore | ||
| import to.bitkit.data.SettingsData | ||
| import to.bitkit.data.SettingsStore | ||
| import to.bitkit.data.WidgetsData | ||
| import to.bitkit.data.WidgetsStore | ||
| import to.bitkit.di.RepoModule | ||
| import to.bitkit.models.BitcoinDisplayUnit | ||
| import to.bitkit.models.FxRate | ||
| import to.bitkit.models.USD | ||
| import to.bitkit.models.WidgetType | ||
| import to.bitkit.models.WidgetWithPosition | ||
| import to.bitkit.models.WidgetsBackupV1 | ||
| import to.bitkit.models.widget.CalculatorValues | ||
| import to.bitkit.repositories.AmountInputHandler | ||
| import to.bitkit.repositories.CurrencyRepo | ||
| import to.bitkit.repositories.WidgetsRepo | ||
| import to.bitkit.test.annotations.DeviceIntegration | ||
| import to.bitkit.test.annotations.DeviceUiIntegration | ||
| import to.bitkit.ui.screens.widgets.calculator.components.CalculatorCard | ||
| import to.bitkit.ui.theme.AppThemeSurface | ||
| import to.bitkit.viewmodels.CurrencyViewModel | ||
| import java.util.Locale | ||
| import javax.inject.Inject | ||
| import javax.inject.Named | ||
| import kotlin.test.assertEquals | ||
|
|
||
| @HiltAndroidTest | ||
| @UninstallModules(RepoModule::class) | ||
| @RunWith(AndroidJUnit4::class) | ||
| @DeviceIntegration | ||
| @DeviceUiIntegration | ||
| class CalculatorCardIntegrationTest { | ||
|
|
||
| @get:Rule | ||
| val hiltRule = HiltAndroidRule(this) | ||
|
|
||
| @get:Rule | ||
| val composeTestRule = createComposeRule() | ||
|
|
||
| @Inject | ||
| lateinit var widgetsRepo: WidgetsRepo | ||
|
|
||
| @Inject | ||
| lateinit var currencyRepo: CurrencyRepo | ||
|
|
||
| @Inject | ||
| lateinit var widgetsStore: WidgetsStore | ||
|
|
||
| @Inject | ||
| lateinit var settingsStore: SettingsStore | ||
|
|
||
| @Inject | ||
| lateinit var cacheStore: CacheStore | ||
|
|
||
| private lateinit var viewModelStore: ViewModelStore | ||
| private lateinit var calculatorViewModel: CalculatorViewModel | ||
| private lateinit var currencyViewModel: CurrencyViewModel | ||
| private lateinit var previousWidgetsData: WidgetsData | ||
| private lateinit var previousSettingsData: SettingsData | ||
| private lateinit var previousCacheData: AppCacheData | ||
| private lateinit var previousLocale: Locale | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| previousLocale = Locale.getDefault() | ||
| Locale.setDefault(Locale.US) | ||
| hiltRule.inject() | ||
|
|
||
| runBlocking { | ||
| previousWidgetsData = widgetsStore.data.first() | ||
| previousSettingsData = settingsStore.data.first() | ||
| previousCacheData = cacheStore.data.first() | ||
|
|
||
| settingsStore.update { | ||
| it.copy( | ||
| selectedCurrency = USD, | ||
| displayUnit = BitcoinDisplayUnit.MODERN, | ||
| showWidgetTitles = true, | ||
| ) | ||
| } | ||
| cacheStore.update { it.copy(cachedRates = listOf(testUsdRate)) } | ||
| widgetsStore.restoreFromBackup( | ||
| WidgetsBackupV1( | ||
| createdAt = TEST_CREATED_AT, | ||
| widgets = WidgetsData( | ||
| widgets = listOf(WidgetWithPosition(type = WidgetType.CALCULATOR, position = 0)), | ||
| calculatorValues = emptyCalculatorValues, | ||
| ), | ||
| ) | ||
| ).getOrThrow() | ||
|
|
||
| currencyRepo.currencyState.first { | ||
| it.selectedCurrency == USD && | ||
| it.displayUnit == BitcoinDisplayUnit.MODERN && | ||
| it.rates.any { rate -> rate.quote == USD && rate.lastPrice == TEST_USD_RATE } | ||
| } | ||
| widgetsRepo.widgetsDataFlow.first { | ||
| it.widgets == listOf(WidgetWithPosition(type = WidgetType.CALCULATOR, position = 0)) && | ||
| it.calculatorValues == emptyCalculatorValues | ||
| } | ||
| } | ||
|
|
||
| calculatorViewModel = createCalculatorViewModel() | ||
| currencyViewModel = createCurrencyViewModel() | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| if (::viewModelStore.isInitialized) { | ||
| viewModelStore.clear() | ||
| } | ||
| runBlocking { | ||
| widgetsStore.restoreFromBackup( | ||
| WidgetsBackupV1( | ||
| createdAt = TEST_CREATED_AT, | ||
| widgets = previousWidgetsData, | ||
| ) | ||
| ).getOrThrow() | ||
| settingsStore.update { previousSettingsData } | ||
| cacheStore.update { previousCacheData } | ||
| } | ||
| Locale.setDefault(previousLocale) | ||
| } | ||
|
|
||
| @Test | ||
| fun btcInputUpdatesFiatValueAndPersistsWidgetState() { | ||
| setCalculatorCard() | ||
|
|
||
| replaceInput(BTC_INPUT_INDEX, "12340") | ||
|
|
||
| waitForValues( | ||
| btcValue = "12340", | ||
| fiatValue = "12.34", | ||
| ) | ||
|
|
||
| assertInputText(BTC_INPUT_INDEX, "12 340") | ||
| assertInputText(FIAT_INPUT_INDEX, "12.34") | ||
| assertPersistedValues( | ||
| btcValue = "12340", | ||
| fiatValue = "12.34", | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun fiatInputUpdatesBtcValueAndPersistsWidgetState() { | ||
| setCalculatorCard() | ||
|
|
||
| replaceInput(FIAT_INPUT_INDEX, "10.00") | ||
|
|
||
| waitForValues( | ||
| btcValue = "10000", | ||
| fiatValue = "10.00", | ||
| ) | ||
|
|
||
| assertInputText(BTC_INPUT_INDEX, "10 000") | ||
| assertInputText(FIAT_INPUT_INDEX, "10.00") | ||
| assertPersistedValues( | ||
| btcValue = "10000", | ||
| fiatValue = "10.00", | ||
| ) | ||
| } | ||
|
|
||
| private fun createCalculatorViewModel(): CalculatorViewModel { | ||
| viewModelStore = ViewModelStore() | ||
| return ViewModelProvider( | ||
| viewModelStore, | ||
| object : ViewModelProvider.Factory { | ||
| @Suppress("UNCHECKED_CAST") | ||
| override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
| return CalculatorViewModel( | ||
| widgetsRepo = widgetsRepo, | ||
| ) as T | ||
| } | ||
| }, | ||
| )[CalculatorViewModel::class.java] | ||
| } | ||
|
|
||
| private fun createCurrencyViewModel(): CurrencyViewModel = ViewModelProvider( | ||
| viewModelStore, | ||
| object : ViewModelProvider.Factory { | ||
| @Suppress("UNCHECKED_CAST") | ||
| override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
| return CurrencyViewModel( | ||
| currencyRepo = currencyRepo, | ||
| ) as T | ||
| } | ||
| }, | ||
| )[CurrencyViewModel::class.java] | ||
|
|
||
| private fun setCalculatorCard() { | ||
| composeTestRule.setContent { | ||
| AppThemeSurface { | ||
| CalculatorCard( | ||
| currencyViewModel = currencyViewModel, | ||
| calculatorViewModel = calculatorViewModel, | ||
| showWidgetTitle = true, | ||
| modifier = Modifier.fillMaxWidth() | ||
|
ovitrif marked this conversation as resolved.
|
||
| ) | ||
| } | ||
| } | ||
| composeTestRule.waitForIdle() | ||
| composeTestRule.waitUntil(timeoutMillis = TIMEOUT_MS) { | ||
| composeTestRule.onAllNodes(hasSetTextAction()).fetchSemanticsNodes().size == INPUT_COUNT | ||
| } | ||
| } | ||
|
|
||
| private fun inputAt(index: Int) = composeTestRule.onAllNodes(hasSetTextAction())[index] | ||
|
|
||
| private fun replaceInput( | ||
| index: Int, | ||
| text: String, | ||
| ) { | ||
| inputAt(index).performTextClearance() | ||
| inputAt(index).performTextInput(text) | ||
| } | ||
|
|
||
| private fun waitForValues( | ||
| btcValue: String, | ||
| fiatValue: String, | ||
| ) { | ||
| runCatching { | ||
| composeTestRule.waitUntil(timeoutMillis = TIMEOUT_MS) { | ||
| calculatorViewModel.calculatorValues.value.btcValue == btcValue && | ||
| calculatorViewModel.calculatorValues.value.fiatValue == fiatValue | ||
| } | ||
| }.onFailure { | ||
| throw AssertionError( | ||
| buildString { | ||
| append("Expected calculatorValues btcValue='$btcValue', fiatValue='$fiatValue', ") | ||
| append("but was '${calculatorViewModel.calculatorValues.value}'. Persisted values were ") | ||
| append("'${widgetsRepo.widgetsDataFlow.value.calculatorValues}'. Semantics tree:\n") | ||
| append(composeTestRule.onRoot(useUnmergedTree = true).printToString()) | ||
| }, | ||
| it, | ||
| ) | ||
| } | ||
|
|
||
| val expectedValues = CalculatorValues( | ||
| btcValue = btcValue, | ||
| fiatValue = fiatValue, | ||
| ) | ||
| runCatching { | ||
| composeTestRule.waitUntil(timeoutMillis = TIMEOUT_MS) { | ||
| widgetsRepo.widgetsDataFlow.value.calculatorValues == expectedValues | ||
| } | ||
| }.onFailure { | ||
| throw AssertionError( | ||
| "Expected persisted values '$expectedValues', but was " + | ||
| "'${widgetsRepo.widgetsDataFlow.value.calculatorValues}'", | ||
| it, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private fun assertInputText( | ||
| inputIndex: Int, | ||
| text: String, | ||
| ) { | ||
| inputAt(inputIndex).assertTextContains(text, substring = true) | ||
| composeTestRule.onNode(hasText(text, substring = true), useUnmergedTree = true) | ||
| .assertIsDisplayed() | ||
| } | ||
|
|
||
| private fun assertPersistedValues( | ||
| btcValue: String, | ||
| fiatValue: String, | ||
| ) { | ||
| assertEquals( | ||
| CalculatorValues( | ||
| btcValue = btcValue, | ||
| fiatValue = fiatValue, | ||
| ), | ||
| widgetsRepo.widgetsDataFlow.value.calculatorValues, | ||
| ) | ||
| } | ||
|
|
||
| companion object { | ||
| private const val BTC_INPUT_INDEX = 0 | ||
| private const val FIAT_INPUT_INDEX = 1 | ||
| private const val INPUT_COUNT = 2 | ||
| private const val TIMEOUT_MS = 5_000L | ||
| private const val TEST_CREATED_AT = 0L | ||
| private const val TEST_USD_RATE = "100000" | ||
|
|
||
| private val emptyCalculatorValues = CalculatorValues( | ||
| btcValue = "", | ||
| fiatValue = "", | ||
| ) | ||
|
|
||
| private val testUsdRate = FxRate( | ||
| symbol = "BTCUSD", | ||
| lastPrice = TEST_USD_RATE, | ||
| base = "BTC", | ||
| baseName = "Bitcoin", | ||
| quote = USD, | ||
| quoteName = "US Dollar", | ||
| currencySymbol = "$", | ||
| currencyFlag = "US", | ||
| lastUpdatedAt = TEST_CREATED_AT, | ||
| ) | ||
| } | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| object TestRepoModule { | ||
|
|
||
| @Provides | ||
| fun bindAmountInputHandler(currencyRepo: CurrencyRepo): AmountInputHandler = currencyRepo | ||
|
|
||
| @Provides | ||
| @Named("enablePolling") | ||
| fun provideEnablePolling(): Boolean = false | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.