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

PermissionDeniedDialogTest and SignInError Test Coverage #2806

Merged
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
19 changes: 19 additions & 0 deletions ground/src/test/java/com/google/android/ground/MainActivityTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package com.google.android.ground

import com.google.android.ground.system.auth.SignInState
import com.google.common.truth.Truth.assertThat
import com.google.firebase.firestore.FirebaseFirestoreException
import com.sharedtest.system.auth.FakeAuthenticationManager
import dagger.hilt.android.testing.HiltAndroidTest
import javax.inject.Inject
Expand Down Expand Up @@ -67,4 +68,22 @@ class MainActivityTest : BaseHiltTest() {
assertThat(ShadowProgressDialog.getLatestDialog().isShowing).isFalse()
}
}

@Test
fun signInErrorDialog_isDisplayed() = runWithTestDispatcher {
Robolectric.buildActivity(MainActivity::class.java).use { controller ->
controller.setup() // Moves Activity to RESUMED state
activity = controller.get()

fakeAuthenticationManager.setState(
SignInState.Error(
error =
FirebaseFirestoreException("error", FirebaseFirestoreException.Code.PERMISSION_DENIED)
)
)
advanceUntilIdle()

assertThat(ShadowProgressDialog.getLatestDialog().isShowing).isTrue()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright 2024 Google LLC
*
* 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
*
* https://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 com.google.android.ground

import androidx.activity.ComponentActivity
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import dagger.hilt.android.testing.HiltAndroidTest
import kotlin.test.Test
import org.junit.Rule
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@HiltAndroidTest
@RunWith(RobolectricTestRunner::class)
class PermissionDeniedDialogTest : BaseHiltTest() {

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

@Test
fun permissionDeniedDialog_DisplaysCorrectTitle() {
composeTestRule.setContent {
PermissionDeniedDialog(signupLink = "http://example.com", onSignOut = {}, onCloseApp = {})
anandwana001 marked this conversation as resolved.
Show resolved Hide resolved
}

composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.permission_denied))
.assertIsDisplayed()
}

@Test
fun permissionDeniedDialog_DisplaysSignOutButton() {
composeTestRule.setContent {
PermissionDeniedDialog(signupLink = "http://example.com", onSignOut = {}, onCloseApp = {})
}

composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.sign_out))
.assertIsDisplayed()
}

@Test
fun permissionDeniedDialog_DisplaysCloseAppButton() {
composeTestRule.setContent {
PermissionDeniedDialog(signupLink = "http://example.com", onSignOut = {}, onCloseApp = {})
}

composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.close_app))
.assertIsDisplayed()
}

@Test
fun permissionDeniedDialog_ClickSignOutButton_CallsOnSignOut() {
var signOutCalled = false

composeTestRule.setContent {
PermissionDeniedDialog(
signupLink = "http://example.com",
onSignOut = { signOutCalled = true },
onCloseApp = {},
)
}

composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.sign_out))
.performClick()

assert(signOutCalled)
}

@Test
fun permissionDeniedDialog_ClickCloseAppButton_CallsOnCloseApp() {
var closeAppCalled = false

composeTestRule.setContent {
PermissionDeniedDialog(
signupLink = "http://example.com",
onSignOut = {},
onCloseApp = { closeAppCalled = true },
)
}

composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.close_app))
.performClick()

assert(closeAppCalled)
}

@Test
fun permissionDeniedDialog_DisplaysSignupLink_WhenLinkIsProvided() {
composeTestRule.setContent {
PermissionDeniedDialog(signupLink = "http://example.com", onSignOut = {}, onCloseApp = {})
}

composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.signup_request_access))
.assertIsDisplayed()
}

@Test
fun permissionDeniedDialog_DoesNotDisplaySignupLink_WhenLinkIsEmpty() {
composeTestRule.setContent {
PermissionDeniedDialog(signupLink = "", onSignOut = {}, onCloseApp = {})
}

composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.signup_request_access))
.assertDoesNotExist()
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.admin_request_access))
.assertIsDisplayed()
}

@Test
fun signOutWarning_isDisplayed() {
composeTestRule.setContent {
PermissionDeniedDialog(signupLink = "", onSignOut = {}, onCloseApp = {})
}
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.signout_warning))
.assertIsDisplayed()
}
}