Skip to content

Commit 8c3af70

Browse files
authored
Unified and simplified UI tests. (#390)
1 parent d3854e3 commit 8c3af70

File tree

7 files changed

+50
-110
lines changed

7 files changed

+50
-110
lines changed

.idea/kotlinc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.mitteloupe.whoami.launcher
2+
3+
import androidx.activity.ComponentActivity
4+
import androidx.activity.compose.LocalActivity
5+
import androidx.appcompat.app.AppCompatActivity
6+
import com.mitteloupe.whoami.di.testAppDependenciesEntryPoint
7+
import com.mitteloupe.whoami.test.test.BaseTest.AppLauncher
8+
import com.mitteloupe.whoami.test.test.TypedAndroidComposeTestRule
9+
import com.mitteloupe.whoami.ui.main.AppNavHost
10+
import com.mitteloupe.whoami.ui.theme.WhoAmITheme
11+
12+
data class FromScreen<ACTIVITY : ComponentActivity>(
13+
private val composeContentTestRule: TypedAndroidComposeTestRule<ACTIVITY>,
14+
private val startDestination: Any
15+
) : AppLauncher() {
16+
private val composableAppLauncher = FromComposable(composeContentTestRule) {
17+
WhoAmITheme {
18+
val activity = LocalActivity.current as AppCompatActivity
19+
with(testAppDependenciesEntryPoint(activity).appNavHostDependencies) {
20+
AppNavHost(
21+
supportFragmentManager = activity.supportFragmentManager,
22+
startDestination = startDestination
23+
)
24+
}
25+
}
26+
}
27+
28+
override fun launch() = composableAppLauncher.launch()
29+
}

app/src/androidTest/java/com/mitteloupe/whoami/launcher/HistoryActivityLauncher.kt

Lines changed: 0 additions & 30 deletions
This file was deleted.

app/src/androidTest/java/com/mitteloupe/whoami/test/HistoryHighlightedIpAddressTest.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package com.mitteloupe.whoami.test
22

33
import androidx.compose.ui.test.ExperimentalTestApi
44
import androidx.compose.ui.test.junit4.createAndroidComposeRule
5-
import com.mitteloupe.whoami.analytics.Analytics
6-
import com.mitteloupe.whoami.coroutine.CoroutineContextProvider
7-
import com.mitteloupe.whoami.launcher.historyScreenLauncher
5+
import com.mitteloupe.whoami.launcher.FromScreen
86
import com.mitteloupe.whoami.localstore.KEY_VALUE_SAVED_HISTORY
97
import com.mitteloupe.whoami.screen.HistoryScreen
108
import com.mitteloupe.whoami.test.annotation.LocalStore
119
import com.mitteloupe.whoami.test.test.BaseTest
1210
import com.mitteloupe.whoami.test.test.retry
1311
import com.mitteloupe.whoami.ui.main.MainActivity
12+
import com.mitteloupe.whoami.ui.main.route.History
1413
import dagger.hilt.android.testing.HiltAndroidTest
1514
import javax.inject.Inject
1615
import org.junit.Test
@@ -23,18 +22,12 @@ class HistoryHighlightedIpAddressTest : BaseTest() {
2322
override val composeTestRule = createAndroidComposeRule<MainActivity>()
2423

2524
override val startActivityLauncher: AppLauncher by lazy {
26-
historyScreenLauncher(highlightedIpAddress = HIGHLIGHTED_IP_ADDRESS)
25+
FromScreen(composeTestRule, History(highlightedIpAddress = HIGHLIGHTED_IP_ADDRESS))
2726
}
2827

2928
@Inject
3029
lateinit var historyScreen: HistoryScreen
3130

32-
@Inject
33-
lateinit var coroutineContextProvider: CoroutineContextProvider
34-
35-
@Inject
36-
lateinit var analytics: Analytics
37-
3831
@Test
3932
@LocalStore(
4033
localStoreDataIds = [KEY_VALUE_SAVED_HISTORY]

app/src/androidTest/java/com/mitteloupe/whoami/test/HistoryTest.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package com.mitteloupe.whoami.test
22

33
import androidx.compose.ui.test.ExperimentalTestApi
44
import androidx.compose.ui.test.junit4.createAndroidComposeRule
5-
import com.mitteloupe.whoami.analytics.Analytics
6-
import com.mitteloupe.whoami.coroutine.CoroutineContextProvider
7-
import com.mitteloupe.whoami.launcher.historyScreenLauncher
5+
import com.mitteloupe.whoami.launcher.FromScreen
86
import com.mitteloupe.whoami.localstore.KEY_VALUE_NO_HISTORY
97
import com.mitteloupe.whoami.localstore.KEY_VALUE_SAVED_HISTORY
108
import com.mitteloupe.whoami.screen.HistoryScreen
@@ -13,6 +11,7 @@ import com.mitteloupe.whoami.test.test.BaseTest
1311
import com.mitteloupe.whoami.test.test.doesNot
1412
import com.mitteloupe.whoami.test.test.retry
1513
import com.mitteloupe.whoami.ui.main.MainActivity
14+
import com.mitteloupe.whoami.ui.main.route.History
1615
import dagger.hilt.android.testing.HiltAndroidTest
1716
import javax.inject.Inject
1817
import org.junit.Test
@@ -23,18 +22,12 @@ class HistoryTest : BaseTest() {
2322
override val composeTestRule = createAndroidComposeRule<MainActivity>()
2423

2524
override val startActivityLauncher: AppLauncher by lazy {
26-
historyScreenLauncher(highlightedIpAddress = null)
25+
FromScreen(composeTestRule, History(highlightedIpAddress = null))
2726
}
2827

2928
@Inject
3029
lateinit var historyScreen: HistoryScreen
3130

32-
@Inject
33-
lateinit var coroutineContextProvider: CoroutineContextProvider
34-
35-
@Inject
36-
lateinit var analytics: Analytics
37-
3831
@Test
3932
@LocalStore(
4033
localStoreDataIds = [KEY_VALUE_SAVED_HISTORY]
Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,28 @@
11
package com.mitteloupe.whoami.test
22

3-
import androidx.activity.ComponentActivity
4-
import androidx.compose.ui.platform.LocalContext
53
import androidx.compose.ui.test.ExperimentalTestApi
6-
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
74
import androidx.compose.ui.test.junit4.createAndroidComposeRule
85
import androidx.test.espresso.intent.Intents
6+
import com.mitteloupe.whoami.launcher.FromScreen
97
import com.mitteloupe.whoami.screen.HomeScreen
108
import com.mitteloupe.whoami.screen.OpenSourceNoticesScreen
119
import com.mitteloupe.whoami.server.REQUEST_RESPONSE_GET_IP
1210
import com.mitteloupe.whoami.server.REQUEST_RESPONSE_GET_IP_DETAILS
1311
import com.mitteloupe.whoami.test.annotation.ServerRequestResponse
1412
import com.mitteloupe.whoami.test.test.BaseTest
15-
import com.mitteloupe.whoami.test.test.BaseTest.AppLauncher.FromComposable
16-
import com.mitteloupe.whoami.ui.main.AppNavHost
1713
import com.mitteloupe.whoami.ui.main.MainActivity
18-
import com.mitteloupe.whoami.ui.main.di.AppNavHostDependencies
19-
import com.mitteloupe.whoami.ui.theme.WhoAmITheme
20-
import dagger.hilt.EntryPoint
21-
import dagger.hilt.EntryPoints
22-
import dagger.hilt.InstallIn
23-
import dagger.hilt.android.components.ActivityComponent
14+
import com.mitteloupe.whoami.ui.main.route.Home
2415
import dagger.hilt.android.testing.HiltAndroidTest
2516
import javax.inject.Inject
26-
import org.junit.After
27-
import org.junit.Before
2817
import org.junit.Test
29-
import org.junit.rules.TestRule
3018

3119
@HiltAndroidTest
3220
@ExperimentalTestApi
3321
class HomeTest : BaseTest() {
34-
@EntryPoint
35-
@InstallIn(ActivityComponent::class)
36-
interface ActivityEntryPoint {
37-
fun appNavHostDependencies(): AppNavHostDependencies
38-
}
39-
4022
override val composeTestRule = createAndroidComposeRule<MainActivity>()
4123

4224
override val startActivityLauncher: AppLauncher by lazy {
43-
@Suppress("UNCHECKED_CAST")
44-
val composeContentTestRule =
45-
composeTestRule as AndroidComposeTestRule<TestRule, ComponentActivity>
46-
FromComposable(composeContentTestRule) {
47-
WhoAmITheme {
48-
val activity = LocalContext.current as MainActivity
49-
EntryPoints.get(activity, ActivityEntryPoint::class.java).appNavHostDependencies()
50-
.AppNavHost(
51-
activity.supportFragmentManager
52-
)
53-
}
54-
}
25+
FromScreen(composeTestRule, Home)
5526
}
5627

5728
@Inject
@@ -60,24 +31,8 @@ class HomeTest : BaseTest() {
6031
@Inject
6132
lateinit var openSourceNoticesScreen: OpenSourceNoticesScreen
6233

63-
@Before
64-
override fun setUp() {
65-
super.setUp()
66-
Intents.init()
67-
}
68-
69-
@After
70-
fun tearDown() {
71-
Intents.release()
72-
}
73-
7434
@Test
75-
@ServerRequestResponse(
76-
[
77-
REQUEST_RESPONSE_GET_IP,
78-
REQUEST_RESPONSE_GET_IP_DETAILS
79-
]
80-
)
35+
@ServerRequestResponse([REQUEST_RESPONSE_GET_IP, REQUEST_RESPONSE_GET_IP_DETAILS])
8136
fun givenConnectedWhenStartingAppThenIpAddressPresented() {
8237
with(composeTestRule) {
8338
with(homeScreen) {
@@ -88,12 +43,7 @@ class HomeTest : BaseTest() {
8843
}
8944

9045
@Test
91-
@ServerRequestResponse(
92-
[
93-
REQUEST_RESPONSE_GET_IP,
94-
REQUEST_RESPONSE_GET_IP_DETAILS
95-
]
96-
)
46+
@ServerRequestResponse([REQUEST_RESPONSE_GET_IP, REQUEST_RESPONSE_GET_IP_DETAILS])
9747
fun givenConnectedWhenStartingAppThenIpAddressDetailsPresented() {
9848
with(composeTestRule) {
9949
with(homeScreen) {
@@ -110,6 +60,7 @@ class HomeTest : BaseTest() {
11060

11161
@Test
11262
fun whenTappingOnOpenSourceNoticesLaunchesActivity() {
63+
Intents.init()
11364
with(composeTestRule) {
11465
with(homeScreen) {
11566
tapOpenSourceNotices()
@@ -119,5 +70,6 @@ class HomeTest : BaseTest() {
11970
seesScreen()
12071
}
12172
}
73+
Intents.release()
12274
}
12375
}

architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/test/BaseTest.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import androidx.compose.ui.test.junit4.ComposeContentTestRule
1515
import androidx.test.core.app.ActivityScenario
1616
import androidx.test.espresso.IdlingRegistry
1717
import androidx.test.espresso.IdlingResource as EspressoIdlingResource
18+
import androidx.test.ext.junit.rules.ActivityScenarioRule
1819
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
1920
import androidx.test.uiautomator.UiDevice
2021
import com.mitteloupe.whoami.test.idlingresource.findAndCloseAppNotRespondingDialog
@@ -37,7 +38,9 @@ import org.junit.Before
3738
import org.junit.BeforeClass
3839
import org.junit.Rule
3940
import org.junit.rules.RuleChain
40-
import org.junit.rules.TestRule
41+
42+
typealias TypedAndroidComposeTestRule<ACTIVITY> =
43+
AndroidComposeTestRule<ActivityScenarioRule<ACTIVITY>, ACTIVITY>
4144

4245
abstract class BaseTest {
4346
private val hiltAndroidRule by lazy { HiltAndroidRule(this) }
@@ -113,7 +116,7 @@ abstract class BaseTest {
113116
composeIdlingResources.forEach(composeTestRule::registerIdlingResource)
114117
}
115118

116-
sealed class AppLauncher {
119+
abstract class AppLauncher {
117120
abstract fun launch()
118121

119122
data class FromIntent(private val intent: Intent) : AppLauncher() {
@@ -130,8 +133,8 @@ abstract class BaseTest {
130133
}
131134
}
132135

133-
data class FromComposable(
134-
private val composeContentTestRule: AndroidComposeTestRule<TestRule, ComponentActivity>,
136+
data class FromComposable<ACTIVITY : ComponentActivity>(
137+
private val composeContentTestRule: TypedAndroidComposeTestRule<ACTIVITY>,
135138
private val composable: @Composable () -> Unit
136139
) : AppLauncher() {
137140
override fun launch() {

0 commit comments

Comments
 (0)