Skip to content
Open
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
3 changes: 3 additions & 0 deletions noty-android/app/composeapp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ dependencies {
// WorkManager for testing
androidTestImplementation(libs.androidx.work.testing)

// Navigation for testing
androidTestImplementation(libs.androidx.navigation.testing)

// Leak Canary
// Uncomment this when have to check for leaks
// debugImplementation(libs.leakcanary)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2020 Shreyas Patil
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.shreyaspatil.noty.composeapp.navigation

import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.navigation.NavHostController
import androidx.navigation.compose.ComposeNavigator
import androidx.navigation.testing.TestNavHostController
import androidx.test.ext.junit.rules.ActivityScenarioRule
import dagger.hilt.android.testing.HiltAndroidTest
import dev.shreyaspatil.noty.composeapp.NotyScreenTest
import dev.shreyaspatil.noty.composeapp.ui.MainActivity
import dev.shreyaspatil.noty.composeapp.ui.Screen
import dev.shreyaspatil.noty.composeapp.ui.theme.LocalUiInDarkMode
import dev.shreyaspatil.noty.core.session.SessionManager
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import javax.inject.Inject

@HiltAndroidTest
class NavigationTest : NotyScreenTest() {

@Inject
lateinit var sessionManager: SessionManager

@Before
fun setUp() {
inject()
// Mock fake authentication
sessionManager.saveToken("Bearer ABCD")
}

@Test
fun startDestination_isLoginScreen_whenLoggedOut() = runTest {
val isLoggedIn = false
val expectedRoute = Screen.Login.route

testStartDestination(isLoggedIn, expectedRoute)
}

@Test
fun startDestination_isNotesScreen_whenLoggedIn() = runTest {
val isLoggedIn = true
val expectedRoute = Screen.Notes.route

testStartDestination(isLoggedIn, expectedRoute)
}

private fun AndroidComposeTestRule<ActivityScenarioRule<MainActivity>, MainActivity>.testStartDestination(
isLoggedIn: Boolean,
expectedRoute: String
) {
lateinit var navController: NavHostController

setNotyContent {
CompositionLocalProvider(LocalUiInDarkMode provides true) {
navController = TestNavHostController(LocalContext.current)
navController.navigatorProvider.addNavigator(ComposeNavigator())

NotyNavigation(
isLoggedIn = isLoggedIn,
navController = navController
)
}
}

waitForIdle()
assertEquals(expectedRoute, navController.currentDestination?.route)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
Expand All @@ -43,8 +44,10 @@ import dev.shreyaspatil.noty.view.viewmodel.NoteDetailViewModel
const val NOTY_NAV_HOST_ROUTE = "noty-main-route"

@Composable
fun NotyNavigation() {
val navController = rememberNavController()
fun NotyNavigation(
isLoggedIn: Boolean,
navController: NavHostController = rememberNavController(),
) {

NavHost(
navController,
Expand All @@ -54,7 +57,7 @@ fun NotyNavigation() {
.statusBarsPadding()
.navigationBarsPadding()
.displayCutoutPadding(),
startDestination = Screen.Notes.route,
startDestination = if (isLoggedIn) Screen.Notes.route else Screen.Login.route,
route = NOTY_NAV_HOST_ROUTE,
enterTransition = { slideIntoContainer(AnimatedContentTransitionScope.SlideDirection.Start, tween(700)) },
exitTransition = { slideOutOfContainer(AnimatedContentTransitionScope.SlideDirection.Start, tween(700)) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.core.view.WindowInsetsControllerCompat
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
Expand All @@ -36,7 +37,9 @@ import dev.shreyaspatil.noty.composeapp.R
import dev.shreyaspatil.noty.composeapp.navigation.NotyNavigation
import dev.shreyaspatil.noty.composeapp.ui.theme.LocalUiInDarkMode
import dev.shreyaspatil.noty.composeapp.ui.theme.NotyTheme
import dev.shreyaspatil.noty.composeapp.utils.collectState
import dev.shreyaspatil.noty.core.preference.PreferenceManager
import dev.shreyaspatil.noty.view.viewmodel.HomeViewModel
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import javax.inject.Inject
Expand All @@ -59,10 +62,14 @@ class MainActivity : ComponentActivity() {
@Composable
private fun NotyMain() {
val darkMode by rememberUiMode()

val homeViewModel: HomeViewModel = hiltViewModel<HomeViewModel>()
val state by homeViewModel.collectState()

CompositionLocalProvider(LocalUiInDarkMode provides darkMode) {
NotyTheme(darkTheme = LocalUiInDarkMode.current) {
Surface {
NotyNavigation()
NotyNavigation(state.isLoggedIn == true)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions noty-android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ junit5-api = { group = "org.junit.jupiter", name = "junit-jupiter-api", version.
junit5-engine = { group = "org.junit.jupiter", name = "junit-jupiter-engine", version.ref = "junit5" }
junit5-params = { group = "org.junit.jupiter", name = "junit-jupiter-params", version.ref = "junit5" }
junit5-vintage = { group = "org.junit.vintage", name = "junit-vintage-engine", version.ref = "junit5" }
androidx-navigation-testing = { group = "androidx.navigation", name = "navigation-testing", version.ref = "composeNav"}
androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidJUnit" }
androidx-test-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espresso" }
androidx-test-core = { group = "androidx.test", name = "core", version.ref = "androidTestCore" }
Expand Down