Skip to content
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

[feature/#313] MainScreenTest 수정 #319

Merged
merged 5 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ plugins {
}

android {
packaging {
resources {
excludes.add("META-INF/**")
}
}
defaultConfig {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
2 changes: 2 additions & 0 deletions core/testing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ dependencies {
api(libs.mockk)
api(libs.turbine)
api(libs.coroutines.test)
implementation(libs.androidx.runner)
implementation(libs.hilt.android.testing)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.droidknights.app.core.testing.runner

import android.app.Application
import android.content.Context
import androidx.test.runner.AndroidJUnitRunner
import dagger.hilt.android.testing.HiltTestApplication

class DroidKnightsTestRunner : AndroidJUnitRunner() {
override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
1 change: 1 addition & 0 deletions core/ui-test-hilt-manifest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
9 changes: 9 additions & 0 deletions core/ui-test-hilt-manifest/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import com.droidknights.app.setNamespace

plugins {
id("droidknights.android.library")
}

android {
setNamespace("core.uitesthiltmanifest")
}
Empty file.
7 changes: 7 additions & 0 deletions core/ui-test-hilt-manifest/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<activity android:name=".HiltComponentActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.droidknights.app.core.uitesthiltmanifest

import androidx.activity.ComponentActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class HiltComponentActivity : ComponentActivity()
9 changes: 9 additions & 0 deletions feature/main/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ plugins {

android {
setNamespace("feature.main")

defaultConfig {
testInstrumentationRunner =
"com.droidknights.app.core.testing.runner.DroidKnightsTestRunner"
}
}

dependencies {
Expand All @@ -14,6 +19,8 @@ dependencies {
implementation(projects.feature.contributor)
implementation(projects.feature.session)
implementation(projects.feature.bookmark)
androidTestImplementation(projects.core.testing)
debugImplementation(projects.core.uiTestHiltManifest)

implementation(projects.widget)
implementation(projects.core.dataApi)
Expand All @@ -24,4 +31,6 @@ dependencies {
implementation(libs.androidx.lifecycle.runtimeCompose)
implementation(libs.androidx.lifecycle.viewModelCompose)
implementation(libs.kotlinx.immutable)
androidTestImplementation(libs.hilt.android.testing)
kspAndroidTest(libs.hilt.android.compiler)
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
package com.droidknights.app.feature.main

import androidx.activity.ComponentActivity
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.performClick
import androidx.navigation.NavDestination.Companion.hasRoute
import androidx.navigation.compose.ComposeNavigator
import androidx.navigation.testing.TestNavHostController
import com.droidknights.app.core.navigation.MainTabRoute
import com.droidknights.app.core.uitesthiltmanifest.HiltComponentActivity
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test

@HiltAndroidTest
class MainScreenTest {

@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
@get:Rule(order = 0)
var hiltRule = HiltAndroidRule(this)

@get:Rule(order = 1)
val composeTestRule = createAndroidComposeRule<HiltComponentActivity>()
private lateinit var navController: TestNavHostController

@Before
fun setup() {
hiltRule.inject()
composeTestRule.setContent {
navController = TestNavHostController(LocalContext.current)
navController.navigatorProvider.addNavigator(ComposeNavigator())
MainScreen(rememberMainNavigator(navController = navController))
MainScreen(
rememberMainNavigator(navController = navController),
onChangeDarkTheme = {}
)
}
}

@Test
fun 시작_화면은_홈이다() {
val actual = navController.currentBackStackEntry?.destination?.route
assertEquals("home", actual)
val actual = navController.currentDestination?.hasRoute<MainTabRoute.Home>()
assertEquals(true, actual)
}

@Test
Expand All @@ -41,7 +53,7 @@ class MainScreenTest {
.performClick()

// then
val actual = navController.currentBackStackEntry?.destination?.route
assertEquals("setting", actual)
val actual = navController.currentDestination?.hasRoute<MainTabRoute.Setting>()
assertEquals(true, actual)
}
}
3 changes: 2 additions & 1 deletion feature/main/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<application>

<activity
android:name=".MainActivity"
android:exported="true"
android:configChanges="uiMode"
android:exported="true"
android:theme="@style/Theme.DroidKnights.TransparentSystemBar">

<intent-filter>
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ androidxDatastore = "1.1.1"
# https://developer.android.com/jetpack/androidx/releases/glance
androidxGlance = "1.0.0"
glanceExperimentalTools = "0.2.2"
androidxTestRunner = "1.5.2"

## Compose
# https://developer.android.com/develop/ui/compose/bom/bom-mapping
Expand Down Expand Up @@ -110,6 +111,7 @@ androidx-compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "u
androidx-compose-ui-testManifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-compose-navigation = { group = "androidx.navigation", name = "navigation-compose", version.ref = "androidxComposeNavigation" }
androidx-compose-navigation-test = { group = "androidx.navigation", name = "navigation-testing", version.ref = "androidxComposeNavigation" }
androidx-runner = { group = "androidx.test", name = "runner", version.ref = "androidxTestRunner" }
compose-compiler-gradle-plugin = { module = "org.jetbrains.kotlin:compose-compiler-gradle-plugin", version.ref = "kotlin" }

hilt-core = { group = "com.google.dagger", name = "hilt-core", version.ref = "hilt" }
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ include(
":core:navigation",
":core:model",
":core:ui",
":core:ui-test-hilt-manifest",
":core:testing",
":core:datastore",
)
Expand Down
Loading