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

Instrumentation tests #41

Merged
merged 2 commits into from
Jun 19, 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ replay_pid*

# IDEA/Android Studio project files
*.iml
.idea/*
.idea/
**/.idea/
!.idea/copyright
# Keep the code styles.
!/.idea/codeStyles
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package edu.stanford.bdh.engagehf

import android.Manifest
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.test.rule.GrantPermissionRule
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import edu.stanford.bdh.engagehf.simulator.NavigatorSimulator
import edu.stanford.spezi.core.navigation.Navigator
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import javax.inject.Inject

@HiltAndroidTest
class AppNavigationTest {

@get:Rule(order = 1)
val hiltRule = HiltAndroidRule(this)

@get:Rule(order = 2)
val composeTestRule = createAndroidComposeRule<MainActivity>()

@get:Rule
val runtimePermissionRule: GrantPermissionRule = GrantPermissionRule.grant(
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN,
)

@Inject
lateinit var navigator: Navigator

@Before
fun init() {
hiltRule.inject()
}

@Test
fun `test start destination`() {
mainActivity {
assertOnboardingIsDisplayed()
}
}

@Test
fun `test navigation to onboarding screen`() {
mainActivity {
navigateToOnboardingScreen()
assertOnboardingIsDisplayed()
}
}

@Test
fun `test navigation to bluetooth screen`() {
mainActivity {
navigateToBluetoothScreen()
assertBluetoothScreenIsDisplayed()
}
}

@Test
fun `test navigation to login screen`() {
mainActivity {
navigateToLoginScreen()
assertLoginScreenIsDisplayed()
}
}

@Test
fun `test navigation to register screen`() {
mainActivity {
navigateToRegisterScreen()
assertRegisterScreenIsDisplayed()
}
}

@Test
fun `test navigation to invitation code screen`() {
mainActivity {
navigateToInvitationCodeScreen()
assertInvitationCodeScreenIsDisplayed()
}
}

@Test
fun `test navigation to sequential onboarding screen`() {
mainActivity {
navigateToSequentialOnboardingScreen()
assertSequentialOnboardingScreenIsDisplayed()
}
}

@Test
fun `test navigation to consent screen`() {
mainActivity {
navigateToConsentScreen()
assertConsentScreenIsDisplayed()
}
}

private fun mainActivity(scope: NavigatorSimulator.() -> Unit) {
NavigatorSimulator(composeTestRule, navigator).apply(scope)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package edu.stanford.bdh.engagehf

import androidx.compose.ui.test.junit4.createAndroidComposeRule
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import edu.stanford.bdh.engagehf.simulator.OnboardingFlowSimulator
import edu.stanford.spezi.module.onboarding.invitation.InvitationCodeRepository
import edu.stanford.spezi.module.onboarding.onboarding.OnboardingRepository
import edu.stanford.spezi.module.onboarding.sequential.SequentialOnboardingRepository
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import javax.inject.Inject

@HiltAndroidTest
class OnboardingFlowTest {

@get:Rule(order = 1)
val hiltRule = HiltAndroidRule(this)

@get:Rule(order = 2)
val composeTestRule = createAndroidComposeRule<MainActivity>()

@Inject
lateinit var onboardingRepository: OnboardingRepository

@Inject
lateinit var sequentialOnboardingRepository: SequentialOnboardingRepository

@Inject
lateinit var invitationCodeRepository: InvitationCodeRepository

@Before
fun init() {
hiltRule.inject()
}

@Test
fun `it should show expected state of initial onboarding screen`() = runTest {
val onboardingData = onboardingRepository.getOnboardingData().getOrThrow()
onboardingFlow {
onboardingScreen {
assertDisplayed()
assertTitle(text = onboardingData.title)
assertSubtitle(text = onboardingData.subTitle)
assertContinueButtonTitle(text = onboardingData.continueButtonText)

onAreasList {
assertDisplayed()
onboardingData.areas.forEach {
assertAreaTitle(title = it.title)
}
}
}
}
}

@Test
fun `it should navigate and display sequential onboarding correctly`() = runTest {
val stepTitle = sequentialOnboardingRepository.getSequentialOnboardingData().steps.first().title
onboardingFlow {
onboardingScreen {
clickContinueButton()
}
sequentialOnboarding {
assertIsDisplayed()
assertPagerIsDisplayed()
assertPageIndicatorIsDisplayed()
assertPageTitle(text = stepTitle)
}
}
}

@Test
fun `it should display and navigate invitation screen correctly`() = runTest {
val steps = sequentialOnboardingRepository.getSequentialOnboardingData().steps
onboardingFlow {
onboardingScreen {
clickContinueButton()
}
sequentialOnboarding {
steps.forEach {
assertPageTitle(text = it.title)
clickForward()
}
}

invitationCodeScreen {
val screenData = invitationCodeRepository.getScreenData()
assertTitle(text = screenData.title)
assertDescription(text = screenData.description)
assertIsDisplayed()
assertMainButtonDisplayed()
assertSecondaryButtonDisplayed()
}
}
}

private fun onboardingFlow(scope: OnboardingFlowSimulator.() -> Unit) {
OnboardingFlowSimulator(composeTestRule).apply(scope)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package edu.stanford.bdh.engagehf.simulator

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.junit4.ComposeTestRule
import edu.stanford.spezi.core.testing.onNodeWithIdentifier
import edu.stanford.spezi.core.testing.waitNode
import edu.stanford.spezi.module.onboarding.invitation.InvitationCodeScreenTestIdentifier

class InvitationCodeScreenSimulator(
private val composeTestRule: ComposeTestRule,
) {
private val root = composeTestRule.onNodeWithIdentifier(InvitationCodeScreenTestIdentifier.ROOT)
private val title =
composeTestRule.onNodeWithIdentifier(InvitationCodeScreenTestIdentifier.TITLE)
private val description = composeTestRule.onNodeWithIdentifier(
InvitationCodeScreenTestIdentifier.DESCRIPTION
)
private val mainButton =
composeTestRule.onNodeWithIdentifier(InvitationCodeScreenTestIdentifier.MAIN_ACTION_BUTTON)
private val secondaryButton =
composeTestRule.onNodeWithIdentifier(InvitationCodeScreenTestIdentifier.SECONDARY_ACTION_BUTTON)

fun assertIsDisplayed() {
composeTestRule.waitNode(InvitationCodeScreenTestIdentifier.ROOT)
root.assertIsDisplayed()
}

fun assertTitle(text: String) {
title
.assertIsDisplayed()
.assertTextEquals(text)
}

fun assertDescription(text: String) {
description
.assertIsDisplayed()
.assertTextEquals(text)
}

fun assertMainButtonDisplayed() {
mainButton.assertIsDisplayed()
}

fun assertSecondaryButtonDisplayed() {
secondaryButton.assertIsDisplayed()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package edu.stanford.bdh.engagehf.simulator

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.ComposeTestRule
import edu.stanford.bdh.engagehf.bluetooth.screen.BluetoothScreenTestIdentifier
import edu.stanford.bdh.engagehf.navigation.AppNavigationEvent
import edu.stanford.spezi.core.navigation.Navigator
import edu.stanford.spezi.core.testing.onAllNodes
import edu.stanford.spezi.core.testing.onNodeWithIdentifier
import edu.stanford.spezi.core.utils.TestIdentifier
import edu.stanford.spezi.module.account.AccountNavigationEvent
import edu.stanford.spezi.module.account.login.LoginScreenTestIdentifier
import edu.stanford.spezi.module.account.register.RegisterScreenTestIdentifier
import edu.stanford.spezi.module.onboarding.OnboardingNavigationEvent
import edu.stanford.spezi.module.onboarding.consent.ConsentScreenTestIdentifier
import edu.stanford.spezi.module.onboarding.invitation.InvitationCodeScreenTestIdentifier
import edu.stanford.spezi.module.onboarding.onboarding.OnboardingScreenTestIdentifier
import edu.stanford.spezi.module.onboarding.sequential.SequentialOnboardingScreenTestIdentifier

class NavigatorSimulator(
private val composeTestRule: ComposeTestRule,
private val navigator: Navigator,
) {
private val onboarding =
composeTestRule.onNodeWithIdentifier(OnboardingScreenTestIdentifier.ROOT)
private val register = composeTestRule.onNodeWithIdentifier(RegisterScreenTestIdentifier.ROOT)
private val login = composeTestRule.onNodeWithIdentifier(LoginScreenTestIdentifier.ROOT)
private val invitation =
composeTestRule.onNodeWithIdentifier(InvitationCodeScreenTestIdentifier.ROOT)
private val sequential =
composeTestRule.onNodeWithIdentifier(SequentialOnboardingScreenTestIdentifier.ROOT)
private val bluetooth = composeTestRule.onNodeWithIdentifier(BluetoothScreenTestIdentifier.ROOT)
private val consent = composeTestRule.onNodeWithIdentifier(ConsentScreenTestIdentifier.ROOT)

fun assertOnboardingIsDisplayed() {
waitNode(OnboardingScreenTestIdentifier.ROOT)
onboarding.assertIsDisplayed()
}

fun assertBluetoothScreenIsDisplayed() {
waitNode(BluetoothScreenTestIdentifier.ROOT)
bluetooth.assertIsDisplayed()
}

fun assertLoginScreenIsDisplayed() {
waitNode(LoginScreenTestIdentifier.ROOT)
login.assertIsDisplayed()
}

fun assertRegisterScreenIsDisplayed() {
waitNode(RegisterScreenTestIdentifier.ROOT)
register.assertIsDisplayed()
}

fun assertInvitationCodeScreenIsDisplayed() {
waitNode(InvitationCodeScreenTestIdentifier.ROOT)
invitation.assertIsDisplayed()
}

fun assertSequentialOnboardingScreenIsDisplayed() {
waitNode(SequentialOnboardingScreenTestIdentifier.ROOT)
sequential.assertIsDisplayed()
}

fun assertConsentScreenIsDisplayed() {
waitNode(ConsentScreenTestIdentifier.ROOT)
consent.assertIsDisplayed()
}

fun navigateToBluetoothScreen() {
navigator.navigateTo(AppNavigationEvent.BluetoothScreen)
}

fun navigateToOnboardingScreen() {
navigator.navigateTo(OnboardingNavigationEvent.OnboardingScreen)
}

fun navigateToLoginScreen() {
navigator.navigateTo(AccountNavigationEvent.LoginScreen(false))
}

fun navigateToRegisterScreen() {
navigator.navigateTo(AccountNavigationEvent.RegisterScreen())
}

fun navigateToInvitationCodeScreen() {
navigator.navigateTo(OnboardingNavigationEvent.InvitationCodeScreen)
}

fun navigateToSequentialOnboardingScreen() {
navigator.navigateTo(OnboardingNavigationEvent.SequentialOnboardingScreen)
}

fun navigateToConsentScreen() {
navigator.navigateTo(OnboardingNavigationEvent.ConsentScreen)
}

private fun waitNode(testIdentifier: TestIdentifier) {
composeTestRule.waitUntil {
composeTestRule.onAllNodes(testIdentifier).fetchSemanticsNodes().isNotEmpty()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edu.stanford.bdh.engagehf.simulator

import androidx.compose.ui.test.junit4.ComposeTestRule

class OnboardingFlowSimulator(
composeTestRule: ComposeTestRule,
) {
private val onboardingScreenSimulator = OnboardingScreenSimulator(composeTestRule)
private val sequentialOnboardingScreenSimulator = SequentialOnboardingScreenSimulator(composeTestRule)
private val invitationCodeScreenSimulator = InvitationCodeScreenSimulator(composeTestRule)

fun onboardingScreen(scope: OnboardingScreenSimulator.() -> Unit) {
onboardingScreenSimulator.apply(scope)
}

fun sequentialOnboarding(scope: SequentialOnboardingScreenSimulator.() -> Unit) {
sequentialOnboardingScreenSimulator.apply(scope)
}

fun invitationCodeScreen(scope: InvitationCodeScreenSimulator.() -> Unit) {
invitationCodeScreenSimulator.apply(scope)
}
}
Loading
Loading