-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Hilt-SavedState UI Test for AndroidTarget
- Loading branch information
1 parent
29d8140
commit 6175c09
Showing
8 changed files
with
151 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
93 changes: 93 additions & 0 deletions
93
androidApp/src/androidTest/kotlin/com/easternkite/eungabi/android/EunGabiHiltTest.kt
This file contains 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,93 @@ | ||
package com.easternkite.eungabi.android | ||
|
||
import androidx.activity.compose.setContent | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.testTag | ||
import androidx.compose.ui.test.assertTextEquals | ||
import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import androidx.compose.ui.test.performClick | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.easternkite.eungabi.navigation.EunGabiNavHost | ||
import com.easternkite.eungabi.navigation.rememberEunGabiController | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import javax.inject.Inject | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
@HiltAndroidTest | ||
class EunGabiHiltTest { | ||
@get:Rule | ||
var hiltRule = HiltAndroidRule(this) | ||
|
||
@get:Rule | ||
var composeTestRule = createAndroidComposeRule<MainActivity>() | ||
|
||
@Before | ||
fun setUp() { | ||
hiltRule.inject() | ||
} | ||
|
||
@Test | ||
fun hiltNavigationTest() { | ||
composeTestRule.activity.setContent { | ||
HiltNavHost() | ||
} | ||
|
||
composeTestRule.apply { | ||
waitForIdle() | ||
onNodeWithTag("MainButton").performClick() | ||
waitForIdle() | ||
onNodeWithTag("state").assertTextEquals("SecondState") | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun HiltNavHost(modifier: Modifier = Modifier) { | ||
val controller = rememberEunGabiController() | ||
EunGabiNavHost( | ||
modifier = modifier, | ||
controller = controller, | ||
startDestination = "Main" | ||
) { | ||
composable("Main") { | ||
Button( | ||
onClick = { controller.navigate("Second?state=SecondState") }, | ||
modifier = Modifier.testTag("MainButton") | ||
) { | ||
Text("Main") | ||
} | ||
} | ||
composable("Second") { | ||
HiltView() | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun HiltView(viewModel: HiltViewModel = hiltViewModel()) { | ||
val state by viewModel.state.collectAsState() | ||
Text(text = state, modifier = Modifier.testTag("state")) | ||
} | ||
|
||
@dagger.hilt.android.lifecycle.HiltViewModel | ||
class HiltViewModel | ||
@Inject | ||
constructor( | ||
savedStateHandle: SavedStateHandle | ||
) : ViewModel() { | ||
val state = savedStateHandle.getStateFlow("state", "state") | ||
} |
14 changes: 14 additions & 0 deletions
14
androidApp/src/androidTest/kotlin/com/easternkite/eungabi/android/HiltTestRunner.kt
This file contains 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,14 @@ | ||
package com.easternkite.eungabi.android | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import androidx.test.runner.AndroidJUnitRunner | ||
import dagger.hilt.android.testing.HiltTestApplication | ||
|
||
class HiltTestRunner : AndroidJUnitRunner() { | ||
override fun newApplication( | ||
cl: ClassLoader?, | ||
className: String?, | ||
context: Context? | ||
): Application = super.newApplication(cl, HiltTestApplication::class.java.name, context) | ||
} |
This file contains 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
7 changes: 7 additions & 0 deletions
7
androidApp/src/main/java/com/easternkite/eungabi/android/Application.kt
This file contains 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,7 @@ | ||
package com.easternkite.eungabi.android | ||
|
||
import android.app.Application | ||
import dagger.hilt.android.HiltAndroidApp | ||
|
||
@HiltAndroidApp | ||
class Application : Application() |
This file contains 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
This file contains 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
This file contains 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