|
| 1 | +package com.company.template.auth |
| 2 | + |
| 3 | +import androidx.compose.ui.test.assertIsDisplayed |
| 4 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 5 | +import androidx.compose.ui.test.onNodeWithTag |
| 6 | +import androidx.compose.ui.test.onNodeWithText |
| 7 | +import androidx.compose.ui.test.performClick |
| 8 | +import androidx.compose.ui.test.performTextInput |
| 9 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 10 | +import com.company.template.ui.theme.TemplateTheme |
| 11 | +import org.junit.Assert.assertTrue |
| 12 | +import org.junit.Rule |
| 13 | +import org.junit.Test |
| 14 | +import org.junit.runner.RunWith |
| 15 | + |
| 16 | +@RunWith(AndroidJUnit4::class) |
| 17 | +class LoginScreenTest { |
| 18 | + |
| 19 | + @get:Rule |
| 20 | + val composeTestRule = createComposeRule() |
| 21 | + |
| 22 | + private var capturedEmail = "" |
| 23 | + private var capturedPassword = "" |
| 24 | + |
| 25 | + private fun setContent( |
| 26 | + loginForm: LoginFormState = LoginFormState(), |
| 27 | + uiState: AuthUiState = AuthUiState.Idle, |
| 28 | + onEmailChange: (String) -> Unit = { capturedEmail = it }, |
| 29 | + onPasswordChange: (String) -> Unit = { capturedPassword = it }, |
| 30 | + onSignIn: () -> Unit = {}, |
| 31 | + onSignInWithGoogle: () -> Unit = {}, |
| 32 | + onNavigateToRegister: () -> Unit = {}, |
| 33 | + ) { |
| 34 | + composeTestRule.setContent { |
| 35 | + TemplateTheme { |
| 36 | + LoginScreen( |
| 37 | + loginForm = loginForm, |
| 38 | + uiState = uiState, |
| 39 | + onEmailChange = onEmailChange, |
| 40 | + onPasswordChange = onPasswordChange, |
| 41 | + onSignIn = onSignIn, |
| 42 | + onSignInWithGoogle = onSignInWithGoogle, |
| 43 | + onNavigateToRegister = onNavigateToRegister, |
| 44 | + ) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun loginScreen_displaysSignInHeading() { |
| 51 | + setContent() |
| 52 | + // The heading "Sign In" is displayed (there may also be a button with the same label) |
| 53 | + composeTestRule.onNodeWithText("Sign In", useUnmergedTree = false).assertIsDisplayed() |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun loginScreen_displaysGoogleSignInButton() { |
| 58 | + setContent() |
| 59 | + composeTestRule.onNodeWithText("Continue with Google").assertIsDisplayed() |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun loginScreen_googleButton_invokesCallback() { |
| 64 | + var called = false |
| 65 | + setContent(onSignInWithGoogle = { called = true }) |
| 66 | + composeTestRule.onNodeWithText("Continue with Google").performClick() |
| 67 | + assertTrue(called) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun loginScreen_displaysErrorMessage() { |
| 72 | + setContent(uiState = AuthUiState.Error("Invalid credentials")) |
| 73 | + composeTestRule.onNodeWithText("Invalid credentials").assertIsDisplayed() |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun loginScreen_displaysRegisterLink() { |
| 78 | + setContent() |
| 79 | + composeTestRule.onNodeWithText("Register").assertIsDisplayed() |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun loginScreen_clickRegister_invokesCallback() { |
| 84 | + var navigateCalled = false |
| 85 | + setContent(onNavigateToRegister = { navigateCalled = true }) |
| 86 | + composeTestRule.onNodeWithText("Register").performClick() |
| 87 | + assertTrue(navigateCalled) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun loginScreen_clickSignIn_invokesCallback() { |
| 92 | + var signInCalled = false |
| 93 | + setContent( |
| 94 | + loginForm = LoginFormState(email = "test@example.com", password = "secret"), |
| 95 | + onSignIn = { signInCalled = true }, |
| 96 | + ) |
| 97 | + // Use the test tag to target the button specifically, not the heading |
| 98 | + composeTestRule.onNodeWithTag(LoginTestTags.SIGN_IN_BUTTON).performClick() |
| 99 | + assertTrue(signInCalled) |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + fun loginScreen_typingEmail_updatesCallback() { |
| 104 | + var updated = "" |
| 105 | + setContent(onEmailChange = { updated = it }) |
| 106 | + composeTestRule.onNodeWithText("Email").performTextInput("test@example.com") |
| 107 | + assertTrue(updated.isNotEmpty()) |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun loginScreen_typingPassword_updatesCallback() { |
| 112 | + var updated = "" |
| 113 | + setContent(onPasswordChange = { updated = it }) |
| 114 | + composeTestRule.onNodeWithText("Password").performTextInput("secret123") |
| 115 | + assertTrue(updated.isNotEmpty()) |
| 116 | + } |
| 117 | +} |
0 commit comments