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 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.google.android.ground.ui.compose.HyperlinkText
import com.google.android.ground.ui.theme.AppTheme
import timber.log.Timber

@Composable
Expand Down Expand Up @@ -106,17 +104,3 @@ private fun SignupLink(signupLink: String) {
),
)
}

@Preview
@Composable
fun PermissionDeniedDialogWithSignupLinkPreview() {
AppTheme {
PermissionDeniedDialog(signupLink = "www.google.com", onSignOut = {}, onCloseApp = {})
}
}

@Preview
@Composable
fun PermissionDeniedDialogWithoutSignupLinkPreview() {
AppTheme { PermissionDeniedDialog(signupLink = "", onSignOut = {}, onCloseApp = {}) }
}
anandwana001 marked this conversation as resolved.
Show resolved Hide resolved
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,153 @@
/*
* 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() {

/**
* composeTestRule has to be created in the specific test file in order to access the required
* activity. [composeTestRule.activity]
*/
@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
}

// Check if the title is displayed correctly
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.permission_denied))
.assertIsDisplayed()
}

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

// Check if the sign-out button is displayed
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.sign_out))
.assertIsDisplayed()
}

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

// Check if the close app button is displayed
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 = {},
)
}

// Click the sign-out button
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.sign_out))
.performClick()

// Check if onSignOut was called
assert(signOutCalled)
}

@Test
fun permissionDeniedDialog_ClickCloseAppButton_CallsOnCloseApp() {
var closeAppCalled = false

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

// Click the close app button
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.close_app))
.performClick()

// Check if onCloseApp was called
assert(closeAppCalled)
}

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

// Check if the signup link is displayed
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.signup_request_access))
.assertIsDisplayed()
}

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

// Check if the signup link is not displayed
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()
}
}