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

Add Navigation Compose with Argument example #102

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ sense.
|[How can I use Jetpack Compose components inside existing screens?](https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example/blob/master/app/src/main/java/com/example/jetpackcompose/interop/ComposeInClassicAndroidActivity.kt) | <img src ="screenshots/interop.png" width=214 height=400>|

### Navigation
|Example|Preview|
|-------|-------|
|[How can I navigate to different screen in Jetpack Compose?](https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example/blob/master/app/src/main/java/com/example/jetpackcompose/navigation/ComposeNavigationActivity.kt) | <img src ="screenshots/compose_navigation_example.gif" width=214 height=400>|
| Example |Preview|
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------|
| [How can I navigate to different screen and send argument in Jetpack Compose?](https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example/blob/master/app/src/main/java/com/example/jetpackcompose/navigation/compose/ComposeNavigationWithArgActivity.kt) | <img src ="screenshots/compose_navigation_w_arg_example.gif" width=214 height=400>|
| [How can I navigate to different screen in Jetpack Compose?](https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example/blob/master/app/src/main/java/com/example/jetpackcompose/navigation/ComposeNavigationActivity.kt) | <img src ="screenshots/compose_navigation_example.gif" width=214 height=400>|

### Testing
|Example|Preview|
Expand Down
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ android {
}
kotlinOptions {
jvmTarget = '11'

freeCompilerArgs += [
"-Xopt-in=androidx.compose.foundation.ExperimentalFoundationApi",
"-Xopt-in=androidx.compose.material.ExperimentalMaterialApi",
]
}
buildFeatures {
compose true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.example.jetpackcompose.navigation.compose

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.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.navigation.compose.ComposeNavigator
import androidx.navigation.testing.TestNavHostController
import com.example.jetpackcompose.navigation.assertCurrentRouteName
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class NavigationComposeWithArgumentNavigationTest {

@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()

private lateinit var navController: TestNavHostController

@Before
fun setupAppNavHost() {
composeTestRule.setContent {
navController = TestNavHostController(LocalContext.current)
navController.navigatorProvider.addNavigator(ComposeNavigator())

ComposeNavigationWithArgApp(navController = navController)
}
}

@Test
fun verifyStartDestination() {
navController
.assertCurrentRouteName("tasks")
}

@Test
fun verifyBackNavigationNotShownOnStartDestination() {
val backText = "Back"

composeTestRule
.onNodeWithContentDescription(backText)
.assertDoesNotExist()
}

@Test
fun clickTask_navigatesToTaskDetailsScreen() {
navController.assertCurrentRouteName("tasks")

composeTestRule
.onNodeWithText("Contribute to Learn-Jetpack-Compose-By-Example")
.performClick()

navController.assertCurrentRouteName("tasks/{taskId}")
}

@Test
fun clickNavigateUpButton_navigatesToTaskListScreen() {
navController.assertCurrentRouteName("tasks")

navigateToTaskDetails()

navController.assertCurrentRouteName("tasks/{taskId}")

val backText = "Back"

composeTestRule
.onNodeWithContentDescription(backText)
.performClick()

navController.assertCurrentRouteName("tasks")
}

private fun navigateToTaskDetails() {
composeTestRule
.onNodeWithText("Contribute to Learn-Jetpack-Compose-By-Example")
.performClick()
}

}
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@
android:exported="true"
android:label="@string/title_compose_navigation_example"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".navigation.compose.ComposeNavigationWithArgActivity"
android:exported="true"
android:label="@string/title_compose_navigation_example"
android:theme="@style/AppTheme.NoActionBar" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.example.jetpackcompose.material.FlowRowActivity
import com.example.jetpackcompose.material.MaterialActivity
import com.example.jetpackcompose.material.ShadowActivity
import com.example.jetpackcompose.navigation.ComposeNavigationActivity
import com.example.jetpackcompose.navigation.compose.ComposeNavigationWithArgActivity
import com.example.jetpackcompose.scrollers.HorizontalScrollableActivity
import com.example.jetpackcompose.scrollers.VerticalScrollableActivity
import com.example.jetpackcompose.stack.StackActivity
Expand Down Expand Up @@ -190,4 +191,8 @@ class MainActivity : AppCompatActivity() {
fun startComposeNavigationExample(view: View) {
startActivity(Intent(this, ComposeNavigationActivity::class.java))
}

fun startDetailViewComposeNavigationExample(view: View) {
startActivity(Intent(this, ComposeNavigationWithArgActivity::class.java))
}
}
Loading