Repository: android/nowinandroid
File: app/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/ui/interests2pane/InterestsListDetailScreenTest.kt (Kotlin)

Code before:
    // Overrides for device sizes.
    private enum class TestDeviceConfig(widthDp: Float, heightDp: Float) {
        Compact(412f, 915f),
        Expanded(1200f, 840f);

        val sizeOverride = DeviceConfigurationOverride.ForcedSize(DpSize(widthDp.dp, heightDp.dp))
        val adaptiveInfo = WindowAdaptiveInfo(
            windowSizeClass = WindowSizeClass.compute(widthDp, heightDp),
            windowPosture = Posture()
        )
    }

    @Before
    fun setup() {
        hiltRule.inject()
        composeTestRule.activityRule
    }

    @Test
    fun expandedWidth_initialState_showsTwoPanesWithPlaceholder() {
        composeTestRule.apply {
            setContent {
                with(TestDeviceConfig.Expanded) {
                    DeviceConfigurationOverride(override = sizeOverride) {
                        NiaTheme {
                            InterestsListDetailScreen(windowAdaptiveInfo = adaptiveInfo)
                        }
                    }
                }
            }

            onNodeWithTag(listPaneTag).assertIsDisplayed()
            onNodeWithText(placeholderText).assertIsDisplayed()
        }
    }

    @Test
    fun notExpandedWidth_initialState_showsListPane() {
        composeTestRule.apply {
            setContent {
                with(TestDeviceConfig.Compact) {
                    DeviceConfigurationOverride(override = sizeOverride) {
                        NiaTheme {
                            InterestsListDetailScreen(windowAdaptiveInfo = adaptiveInfo)
                        }
                    }
                }
            }

            onNodeWithTag(listPaneTag).assertIsDisplayed()
            onNodeWithText(placeholderText).assertIsNotDisplayed()

Review comment (refactor):
I feel like these patterns of `composeTestRule.apply` followed by `setContent` could be replaced with a more simple `composeTestRule.setContent` and also to reduce indentation.


Or even simpler, if all these tests are actually doing more or less the same thing, to extract the setup phase into a private function:

```kotlin
private fun setup(
    config: TestDeviceConfig,
    then: AndroidComposeTestRule<*, *>.() -> Unit,
) = with(composeTestRule) {
    setContent {
        DeviceConfigurationOverride(override = config.sizeOverride) {
            NiaTheme {
                InterestsListDetailScreen(windowAdaptiveInfo = config.adaptiveInfo)
            }
        }
    }
    then()
}
```

to be called like this in the tests themselves:

```kotlin
setup(TestDeviceConfig.Compact) {
    onNodeWithTag(...).assertIsDisplayed()
    ...
}
```

Code after:
    // Overrides for device sizes.
    private enum class TestDeviceConfig(widthDp: Float, heightDp: Float) {
        Compact(412f, 915f),
        Expanded(1200f, 840f),
        ;

        val sizeOverride = DeviceConfigurationOverride.ForcedSize(DpSize(widthDp.dp, heightDp.dp))
        val adaptiveInfo = WindowAdaptiveInfo(
            windowSizeClass = WindowSizeClass.compute(widthDp, heightDp),
            windowPosture = Posture(),
        )
    }

    @Before
    fun setup() {
        hiltRule.inject()
    }

    /** Convenience function for getting all topics during tests, */
    private fun getTopics(): List<Topic> = runBlocking {
        topicsRepository.getTopics().first()
    }

    @Test
    fun expandedWidth_initialState_showsTwoPanesWithPlaceholder() {
        composeTestRule.apply {
            setContent {
                with(TestDeviceConfig.Expanded) {
                    DeviceConfigurationOverride(override = sizeOverride) {
                        NiaTheme {
                            InterestsListDetailScreen(windowAdaptiveInfo = adaptiveInfo)
                        }
                    }
                }
            }

            onNodeWithTag(listPaneTag).assertIsDisplayed()
            onNodeWithText(placeholderText).assertIsDisplayed()
        }
    }

    @Test
    fun compactWidth_initialState_showsListPane() {
        composeTestRule.apply {
            setContent {
                with(TestDeviceConfig.Compact) {
                    DeviceConfigurationOverride(override = sizeOverride) {
                        NiaTheme {
                            InterestsListDetailScreen(windowAdaptiveInfo = adaptiveInfo)
                        }
                    }