From 7f4bc66ba248ad6b738fb053bc3fed3cacdaf770 Mon Sep 17 00:00:00 2001 From: Igor Escodro Date: Wed, 21 Sep 2022 15:59:36 -0400 Subject: [PATCH] =?UTF-8?q?Revert=20"=E2=9C=85=20Allow=20some=20test=20cla?= =?UTF-8?q?ss=20to=20be=20flaky=20(#318)"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 4b9454fd --- .../com/escodro/alkaa/CategoryFlowTest.kt | 14 +++- .../java/com/escodro/alkaa/HomeScreenTest.kt | 14 +++- .../com/escodro/alkaa/NotificationFlowTest.kt | 14 +++- .../com/escodro/alkaa/PreferenceFlowTest.kt | 14 +++- .../java/com/escodro/alkaa/SearchFlowTest.kt | 14 +++- .../java/com/escodro/alkaa/TaskFlowTest.kt | 14 +++- .../com/escodro/alkaa/TaskListFlowTest.kt | 14 +++- .../escodro/task/AlarmPermissionFlowTest.kt | 51 ++++++++++--- .../com/escodro/task/AlarmSelectionTest.kt | 73 +++++++++++++------ libraries/test/build.gradle.kts | 4 +- .../main/java/com/escodro/test/FlakyTest.kt | 44 ----------- 11 files changed, 168 insertions(+), 102 deletions(-) delete mode 100644 libraries/test/src/main/java/com/escodro/test/FlakyTest.kt diff --git a/app/src/androidTest/java/com/escodro/alkaa/CategoryFlowTest.kt b/app/src/androidTest/java/com/escodro/alkaa/CategoryFlowTest.kt index 227d857e6..e75e71a4a 100644 --- a/app/src/androidTest/java/com/escodro/alkaa/CategoryFlowTest.kt +++ b/app/src/androidTest/java/com/escodro/alkaa/CategoryFlowTest.kt @@ -5,6 +5,7 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.test.SemanticsMatcher import androidx.compose.ui.test.hasSetTextAction import androidx.compose.ui.test.junit4.ComposeTestRule +import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick @@ -15,18 +16,25 @@ import com.escodro.alkaa.navigation.NavGraph import com.escodro.category.presentation.semantics.ColorKey import com.escodro.designsystem.AlkaaTheme import com.escodro.local.provider.DaoProvider -import com.escodro.test.FlakyTest +import com.escodro.test.DisableAnimationsRule import kotlinx.coroutines.runBlocking import org.junit.Before +import org.junit.Rule import org.junit.Test import org.koin.test.KoinTest import org.koin.test.inject import com.escodro.category.R as CategoryR -internal class CategoryFlowTest : FlakyTest(), KoinTest { +internal class CategoryFlowTest : KoinTest { private val daoProvider: DaoProvider by inject() + @get:Rule + val composeTestRule = createComposeRule() + + @get:Rule + val disableAnimationsRule = DisableAnimationsRule() + private val context = InstrumentationRegistry.getInstrumentation().targetContext @Before @@ -36,7 +44,7 @@ internal class CategoryFlowTest : FlakyTest(), KoinTest { daoProvider.getCategoryDao().cleanTable() } - setContent { + composeTestRule.setContent { AlkaaTheme { NavGraph() } diff --git a/app/src/androidTest/java/com/escodro/alkaa/HomeScreenTest.kt b/app/src/androidTest/java/com/escodro/alkaa/HomeScreenTest.kt index 723120c1b..29669c38d 100644 --- a/app/src/androidTest/java/com/escodro/alkaa/HomeScreenTest.kt +++ b/app/src/androidTest/java/com/escodro/alkaa/HomeScreenTest.kt @@ -1,5 +1,6 @@ package com.escodro.alkaa +import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick @@ -7,17 +8,24 @@ import androidx.test.platform.app.InstrumentationRegistry import com.escodro.alkaa.model.HomeSection import com.escodro.alkaa.navigation.NavGraph import com.escodro.designsystem.AlkaaTheme -import com.escodro.test.FlakyTest +import com.escodro.test.DisableAnimationsRule import org.junit.Before +import org.junit.Rule import org.junit.Test -internal class HomeScreenTest : FlakyTest() { +internal class HomeScreenTest { + + @get:Rule + val composeTestRule = createComposeRule() + + @get:Rule + val disableAnimationsRule = DisableAnimationsRule() private val context = InstrumentationRegistry.getInstrumentation().targetContext @Before fun setup() { - setContent { + composeTestRule.setContent { AlkaaTheme { NavGraph() } diff --git a/app/src/androidTest/java/com/escodro/alkaa/NotificationFlowTest.kt b/app/src/androidTest/java/com/escodro/alkaa/NotificationFlowTest.kt index 42fe11c62..ef715631d 100644 --- a/app/src/androidTest/java/com/escodro/alkaa/NotificationFlowTest.kt +++ b/app/src/androidTest/java/com/escodro/alkaa/NotificationFlowTest.kt @@ -3,6 +3,7 @@ package com.escodro.alkaa import android.app.Notification import androidx.annotation.StringRes import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.test.platform.app.InstrumentationRegistry @@ -14,10 +15,11 @@ import com.escodro.local.model.AlarmInterval import com.escodro.local.model.Task import com.escodro.local.provider.DaoProvider import com.escodro.task.R -import com.escodro.test.FlakyTest +import com.escodro.test.DisableAnimationsRule import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test import org.koin.test.KoinTest import org.koin.test.inject @@ -26,12 +28,18 @@ import kotlin.test.assertEquals import kotlin.test.assertTrue import com.escodro.alarm.R as AlarmR -internal class NotificationFlowTest : FlakyTest(), KoinTest { +internal class NotificationFlowTest : KoinTest { private val daoProvider: DaoProvider by inject() private val scheduleAlarm: ScheduleAlarm by inject() + @get:Rule + val composeTestRule = createComposeRule() + + @get:Rule + val disableAnimationsRule = DisableAnimationsRule() + private val context = InstrumentationRegistry.getInstrumentation().targetContext @Before @@ -40,7 +48,7 @@ internal class NotificationFlowTest : FlakyTest(), KoinTest { runBlocking { daoProvider.getTaskDao().cleanTable() } - setContent { + composeTestRule.setContent { AlkaaTheme { NavGraph() } diff --git a/app/src/androidTest/java/com/escodro/alkaa/PreferenceFlowTest.kt b/app/src/androidTest/java/com/escodro/alkaa/PreferenceFlowTest.kt index aee74fc54..5eaa4397d 100644 --- a/app/src/androidTest/java/com/escodro/alkaa/PreferenceFlowTest.kt +++ b/app/src/androidTest/java/com/escodro/alkaa/PreferenceFlowTest.kt @@ -1,25 +1,33 @@ package com.escodro.alkaa import androidx.annotation.StringRes +import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.test.platform.app.InstrumentationRegistry import com.escodro.alkaa.navigation.NavGraph import com.escodro.core.extension.getVersionName -import com.escodro.test.FlakyTest +import com.escodro.test.DisableAnimationsRule import org.junit.Before +import org.junit.Rule import org.junit.Test import com.escodro.core.R as CoreR import com.escodro.preference.R as PrefR -internal class PreferenceFlowTest : FlakyTest() { +internal class PreferenceFlowTest { + + @get:Rule + val composeTestRule = createComposeRule() + + @get:Rule + val disableAnimationsRule = DisableAnimationsRule() private val context = InstrumentationRegistry.getInstrumentation().targetContext @Before fun setup() { - setContent { + composeTestRule.setContent { NavGraph() } navigateToPreferences() diff --git a/app/src/androidTest/java/com/escodro/alkaa/SearchFlowTest.kt b/app/src/androidTest/java/com/escodro/alkaa/SearchFlowTest.kt index 9e2db58cb..4c0218eb3 100644 --- a/app/src/androidTest/java/com/escodro/alkaa/SearchFlowTest.kt +++ b/app/src/androidTest/java/com/escodro/alkaa/SearchFlowTest.kt @@ -2,6 +2,7 @@ package com.escodro.alkaa import androidx.annotation.StringRes import androidx.compose.ui.test.hasSetTextAction +import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText @@ -12,18 +13,25 @@ import com.escodro.alkaa.fake.FAKE_TASKS import com.escodro.alkaa.navigation.NavGraph import com.escodro.designsystem.AlkaaTheme import com.escodro.local.provider.DaoProvider -import com.escodro.test.FlakyTest +import com.escodro.test.DisableAnimationsRule import kotlinx.coroutines.runBlocking import org.junit.Before +import org.junit.Rule import org.junit.Test import org.koin.test.KoinTest import org.koin.test.inject import com.escodro.search.R as SearchR -internal class SearchFlowTest : FlakyTest(), KoinTest { +internal class SearchFlowTest : KoinTest { private val daoProvider: DaoProvider by inject() + @get:Rule + val composeTestRule = createComposeRule() + + @get:Rule + val disableAnimationsRule = DisableAnimationsRule() + private val context = InstrumentationRegistry.getInstrumentation().targetContext @Before @@ -36,7 +44,7 @@ internal class SearchFlowTest : FlakyTest(), KoinTest { FAKE_TASKS.forEach { task -> daoProvider.getTaskDao().insertTask(task) } } - setContent { + composeTestRule.setContent { AlkaaTheme { NavGraph() } diff --git a/app/src/androidTest/java/com/escodro/alkaa/TaskFlowTest.kt b/app/src/androidTest/java/com/escodro/alkaa/TaskFlowTest.kt index 446b5a26d..5dae8e64c 100644 --- a/app/src/androidTest/java/com/escodro/alkaa/TaskFlowTest.kt +++ b/app/src/androidTest/java/com/escodro/alkaa/TaskFlowTest.kt @@ -3,6 +3,7 @@ package com.escodro.alkaa import androidx.annotation.StringRes import androidx.compose.ui.test.assertIsSelected import androidx.compose.ui.test.hasSetTextAction +import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick @@ -15,11 +16,12 @@ import com.escodro.core.coroutines.CoroutineDebouncer import com.escodro.designsystem.AlkaaTheme import com.escodro.local.model.Category import com.escodro.local.provider.DaoProvider +import com.escodro.test.DisableAnimationsRule import com.escodro.test.Events -import com.escodro.test.FlakyTest import com.escodro.test.onChip import kotlinx.coroutines.runBlocking import org.junit.Before +import org.junit.Rule import org.junit.Test import org.koin.test.KoinTest import org.koin.test.inject @@ -27,10 +29,16 @@ import org.koin.test.mock.declare import java.util.Calendar import com.escodro.task.R as TaskR -internal class TaskFlowTest : FlakyTest(), KoinTest { +internal class TaskFlowTest : KoinTest { private val daoProvider: DaoProvider by inject() + @get:Rule + val composeTestRule = createComposeRule() + + @get:Rule + val disableAnimationsRule = DisableAnimationsRule() + private val context = InstrumentationRegistry.getInstrumentation().targetContext @Before @@ -50,7 +58,7 @@ internal class TaskFlowTest : FlakyTest(), KoinTest { // Replace Debouncer with a Immediate Executor declare { CoroutinesDebouncerFake() } - setContent { + composeTestRule.setContent { AlkaaTheme { NavGraph() } diff --git a/app/src/androidTest/java/com/escodro/alkaa/TaskListFlowTest.kt b/app/src/androidTest/java/com/escodro/alkaa/TaskListFlowTest.kt index cf6864cbf..ea68d8d9b 100644 --- a/app/src/androidTest/java/com/escodro/alkaa/TaskListFlowTest.kt +++ b/app/src/androidTest/java/com/escodro/alkaa/TaskListFlowTest.kt @@ -4,6 +4,7 @@ import androidx.annotation.StringRes import androidx.compose.ui.test.SemanticsMatcher import androidx.compose.ui.test.hasSetTextAction import androidx.compose.ui.test.junit4.ComposeTestRule +import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText @@ -15,18 +16,25 @@ import com.escodro.designsystem.AlkaaTheme import com.escodro.local.model.Category import com.escodro.local.provider.DaoProvider import com.escodro.task.presentation.list.CheckboxNameKey -import com.escodro.test.FlakyTest +import com.escodro.test.DisableAnimationsRule import kotlinx.coroutines.runBlocking import org.junit.Before +import org.junit.Rule import org.junit.Test import org.koin.test.KoinTest import org.koin.test.inject import com.escodro.task.R as TaskR -internal class TaskListFlowTest : FlakyTest(), KoinTest { +internal class TaskListFlowTest : KoinTest { private val daoProvider: DaoProvider by inject() + @get:Rule + val composeTestRule = createComposeRule() + + @get:Rule + val disableAnimationsRule = DisableAnimationsRule() + private val context = InstrumentationRegistry.getInstrumentation().targetContext @Before @@ -42,7 +50,7 @@ internal class TaskListFlowTest : FlakyTest(), KoinTest { getCategoryDao().insertCategory(Category(name = "Work", color = "#519872")) } } - setContent { + composeTestRule.setContent { AlkaaTheme { NavGraph() } diff --git a/features/task/src/androidTest/java/com/escodro/task/AlarmPermissionFlowTest.kt b/features/task/src/androidTest/java/com/escodro/task/AlarmPermissionFlowTest.kt index bbdb8c694..ca70e2ee1 100644 --- a/features/task/src/androidTest/java/com/escodro/task/AlarmPermissionFlowTest.kt +++ b/features/task/src/androidTest/java/com/escodro/task/AlarmPermissionFlowTest.kt @@ -1,25 +1,50 @@ package com.escodro.task +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.test.junit4.createEmptyComposeRule import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick +import androidx.test.core.app.ActivityScenario import androidx.test.platform.app.InstrumentationRegistry import com.escodro.designsystem.AlkaaTheme import com.escodro.task.fake.PermissionStateFake import com.escodro.task.presentation.detail.alarm.AlarmSelectionContent import com.escodro.task.presentation.detail.alarm.AlarmSelectionState -import com.escodro.test.FlakyTest +import com.escodro.test.DisableAnimationsRule import com.google.accompanist.permissions.ExperimentalPermissionsApi import com.google.accompanist.permissions.PermissionState import com.google.accompanist.permissions.PermissionStatus +import org.junit.After import org.junit.Assert +import org.junit.Before +import org.junit.Rule import org.junit.Test @OptIn(ExperimentalPermissionsApi::class) -internal class AlarmPermissionFlowTest : FlakyTest() { +internal class AlarmPermissionFlowTest { + + @get:Rule + val composeTestRule = createEmptyComposeRule() + + @get:Rule + val disableAnimationsRule = DisableAnimationsRule() private val context = InstrumentationRegistry.getInstrumentation().targetContext + private lateinit var scenario: ActivityScenario + + @Before + fun setup() { + scenario = ActivityScenario.launch(ComponentActivity::class.java) + } + + @After + fun tearDown() { + scenario.close() + } + @Test fun test_notificationPermissionGrantedDoesNotShowDialog() { val state = AlarmSelectionState(calendar = null, alarmInterval = null) @@ -123,16 +148,18 @@ internal class AlarmPermissionFlowTest : FlakyTest() { permissionState: PermissionState, hasAlarmPermission: Boolean ) { - setContent { - AlkaaTheme { - AlarmSelectionContent( - context = LocalContext.current, - state = state, - permissionState = permissionState, - hasAlarmPermission = { hasAlarmPermission }, - onAlarmUpdate = {}, - onIntervalSelect = {} - ) + scenario.onActivity { activity -> + activity.setContent { + AlkaaTheme { + AlarmSelectionContent( + context = LocalContext.current, + state = state, + permissionState = permissionState, + hasAlarmPermission = { hasAlarmPermission }, + onAlarmUpdate = {}, + onIntervalSelect = {} + ) + } } } } diff --git a/features/task/src/androidTest/java/com/escodro/task/AlarmSelectionTest.kt b/features/task/src/androidTest/java/com/escodro/task/AlarmSelectionTest.kt index 9060938d4..36b5135d4 100644 --- a/features/task/src/androidTest/java/com/escodro/task/AlarmSelectionTest.kt +++ b/features/task/src/androidTest/java/com/escodro/task/AlarmSelectionTest.kt @@ -1,23 +1,48 @@ package com.escodro.task +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.junit4.createEmptyComposeRule import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick +import androidx.test.core.app.ActivityScenario import androidx.test.platform.app.InstrumentationRegistry import com.escodro.designsystem.AlkaaTheme import com.escodro.task.model.AlarmInterval import com.escodro.task.presentation.detail.alarm.AlarmSelection +import com.escodro.test.DisableAnimationsRule import com.escodro.test.Events -import com.escodro.test.FlakyTest +import org.junit.After +import org.junit.Before +import org.junit.Rule import org.junit.Test import java.util.Calendar -internal class AlarmSelectionTest : FlakyTest() { +internal class AlarmSelectionTest { + + @get:Rule + val composeTestRule = createEmptyComposeRule() + + @get:Rule + val disableAnimationsRule = DisableAnimationsRule() private val context = InstrumentationRegistry.getInstrumentation().targetContext + private lateinit var scenario: ActivityScenario + + @Before + fun setup() { + scenario = ActivityScenario.launch(ComponentActivity::class.java) + } + + @After + fun tearDown() { + scenario.close() + } + @Test fun test_addAlarm() { // Load the alarm section component @@ -144,16 +169,18 @@ internal class AlarmSelectionTest : FlakyTest() { hasExactAlarmPermission: Boolean = true, shouldAskForNotificationPermission: Boolean = false ) { - setContent { - AlkaaTheme { - AlarmSelection( - calendar = null, - interval = AlarmInterval.NEVER, - onAlarmUpdate = {}, - onIntervalSelect = {}, - hasAlarmPermission = { hasExactAlarmPermission }, - shouldCheckNotificationPermission = shouldAskForNotificationPermission - ) + scenario.onActivity { activity -> + activity.setContent { + AlkaaTheme { + AlarmSelection( + calendar = null, + interval = AlarmInterval.NEVER, + onAlarmUpdate = {}, + onIntervalSelect = {}, + hasAlarmPermission = { hasExactAlarmPermission }, + shouldCheckNotificationPermission = shouldAskForNotificationPermission + ) + } } } } @@ -162,16 +189,18 @@ internal class AlarmSelectionTest : FlakyTest() { calendar: Calendar, alarmInterval: AlarmInterval ) { - setContent { - AlkaaTheme { - AlarmSelection( - calendar = calendar, - interval = alarmInterval, - onAlarmUpdate = {}, - onIntervalSelect = {}, - hasAlarmPermission = { true }, - shouldCheckNotificationPermission = false - ) + scenario.onActivity { activity -> + activity.setContent { + AlkaaTheme { + AlarmSelection( + calendar = calendar, + interval = alarmInterval, + onAlarmUpdate = {}, + onIntervalSelect = {}, + hasAlarmPermission = { true }, + shouldCheckNotificationPermission = false + ) + } } } } diff --git a/libraries/test/build.gradle.kts b/libraries/test/build.gradle.kts index abe27b8b1..dd3c0efd0 100644 --- a/libraries/test/build.gradle.kts +++ b/libraries/test/build.gradle.kts @@ -1,6 +1,5 @@ plugins { id("com.escodro.android-library") - id("com.escodro.android-compose") } android { @@ -23,8 +22,7 @@ dependencies { api(libs.coroutines.test) - api(libs.compose.activity) - implementation(libs.bundles.composetest) { + implementation(libs.compose.uitest) { exclude(group = "androidx.core", module = "core-ktx") exclude(group = "androidx.fragment", module = "fragment") exclude(group = "androidx.customview", module = "customview") diff --git a/libraries/test/src/main/java/com/escodro/test/FlakyTest.kt b/libraries/test/src/main/java/com/escodro/test/FlakyTest.kt deleted file mode 100644 index 1720a7449..000000000 --- a/libraries/test/src/main/java/com/escodro/test/FlakyTest.kt +++ /dev/null @@ -1,44 +0,0 @@ -package com.escodro.test - -import androidx.activity.ComponentActivity -import androidx.activity.compose.setContent -import androidx.compose.runtime.Composable -import androidx.compose.ui.test.junit4.createEmptyComposeRule -import androidx.test.ext.junit.rules.ActivityScenarioRule -import com.adevinta.android.barista.rule.flaky.FlakyTestRule -import org.junit.Rule - -/** - * Allows flaky tests to re-execute if not passing. - * - * Unfortunately this is only happening in the CI, while using Gradle Managed Devices and I'm not - * able to reproduce it locally. So, the silver tape solution here is basically try to execute it - * again and hope that the Activity is on the right state. - */ -open class FlakyTest { - - @get:Rule(order = 0) - val scenarioRule = ActivityScenarioRule(ComponentActivity::class.java) - - @get:Rule(order = 1) - val composeTestRule = createEmptyComposeRule() - - @get:Rule(order = 2) - val flakyRule = FlakyTestRule().apply { allowFlakyAttemptsByDefault(defaultAttempts = 10) } - - @get:Rule(order = 3) - val disableAnimationsRule = DisableAnimationsRule() - - /** - * Loads the Compose content in the UI for testing. - * - * @param content the content to be loaded - */ - fun setContent(content: @Composable () -> Unit) { - scenarioRule.scenario.onActivity { activity -> - activity.setContent { - content() - } - } - } -}