diff --git a/features/location/impl/build.gradle.kts b/features/location/impl/build.gradle.kts index dd265ee7f0..515843c7bb 100644 --- a/features/location/impl/build.gradle.kts +++ b/features/location/impl/build.gradle.kts @@ -22,6 +22,11 @@ plugins { android { namespace = "io.element.android.features.location.impl" + testOptions { + unitTests { + isIncludeAndroidResources = true + } + } } anvil { @@ -51,11 +56,14 @@ dependencies { testImplementation(libs.test.junit) testImplementation(libs.coroutines.test) testImplementation(libs.molecule.runtime) + testImplementation(libs.test.robolectric) testImplementation(libs.test.truth) testImplementation(libs.test.turbine) - testImplementation(libs.test.truth) testImplementation(projects.libraries.matrix.test) + testImplementation(projects.libraries.testtags) testImplementation(projects.services.analytics.test) testImplementation(projects.features.messages.test) testImplementation(projects.tests.testutils) + testImplementation(libs.androidx.compose.ui.test.junit) + testReleaseImplementation(libs.androidx.compose.ui.test.manifest) } diff --git a/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationView.kt b/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationView.kt index d603bd19ae..2f352d50d2 100644 --- a/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationView.kt +++ b/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationView.kt @@ -120,10 +120,14 @@ fun ShowLocationView( ) }, navigationIcon = { - BackButton(onClick = onBackPressed) + BackButton( + onClick = onBackPressed, + ) }, actions = { - IconButton(onClick = { state.eventSink(ShowLocationEvents.Share) }) { + IconButton( + onClick = { state.eventSink(ShowLocationEvents.Share) } + ) { Icon( imageVector = CompoundIcons.ShareAndroid(), contentDescription = stringResource(CommonStrings.action_share), diff --git a/features/location/impl/src/test/kotlin/io/element/android/features/location/impl/show/ShowLocationViewTest.kt b/features/location/impl/src/test/kotlin/io/element/android/features/location/impl/show/ShowLocationViewTest.kt new file mode 100644 index 0000000000..a66197e733 --- /dev/null +++ b/features/location/impl/src/test/kotlin/io/element/android/features/location/impl/show/ShowLocationViewTest.kt @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2024 New Vector Ltd + * + * 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 io.element.android.features.location.impl.show + +import androidx.activity.ComponentActivity +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.ui.platform.LocalInspectionMode +import androidx.compose.ui.test.junit4.AndroidComposeTestRule +import androidx.compose.ui.test.junit4.createAndroidComposeRule +import androidx.compose.ui.test.onNodeWithContentDescription +import androidx.compose.ui.test.onNodeWithTag +import androidx.compose.ui.test.performClick +import androidx.test.ext.junit.runners.AndroidJUnit4 +import io.element.android.libraries.testtags.TestTags +import io.element.android.libraries.ui.strings.CommonStrings +import io.element.android.tests.testutils.EnsureNeverCalled +import io.element.android.tests.testutils.EventsRecorder +import io.element.android.tests.testutils.clickOn +import io.element.android.tests.testutils.ensureCalledOnce +import io.element.android.tests.testutils.pressBack +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TestRule +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class ShowLocationViewTest { + @get:Rule val rule = createAndroidComposeRule() + + @Test + fun `test back action`() { + val eventsRecorder = EventsRecorder(expectEvents = false) + ensureCalledOnce { callback -> + rule.setShowLocationView( + state = aShowLocationState( + eventSink = eventsRecorder + ), + onBackPressed = callback, + ) + rule.pressBack() + } + } + + @Test + fun `test share action`() { + val eventsRecorder = EventsRecorder() + rule.setShowLocationView( + aShowLocationState( + eventSink = eventsRecorder + ), + onBackPressed = EnsureNeverCalled(), + ) + val shareContentDescription = rule.activity.getString(CommonStrings.action_share) + rule.onNodeWithContentDescription(shareContentDescription).performClick() + eventsRecorder.assertSingle(ShowLocationEvents.Share) + } + + @Test + fun `test fab click`() { + val eventsRecorder = EventsRecorder() + rule.setShowLocationView( + aShowLocationState( + eventSink = eventsRecorder + ), + onBackPressed = EnsureNeverCalled(), + ) + val shareContentDescription = rule.activity.getString(CommonStrings.action_share) + rule.onNodeWithTag(TestTags.floatingActionButton.value).performClick() + eventsRecorder.assertSingle(ShowLocationEvents.TrackMyLocation(true)) + } + + @Test + fun `when permission denied is displayed user can open the settings`() { + val eventsRecorder = EventsRecorder() + rule.setShowLocationView( + aShowLocationState( + permissionDialog = ShowLocationState.Dialog.PermissionDenied, + eventSink = eventsRecorder + ), + onBackPressed = EnsureNeverCalled(), + ) + rule.clickOn(CommonStrings.action_continue) + eventsRecorder.assertSingle(ShowLocationEvents.OpenAppSettings) + } + + @Test + fun `when permission denied is displayed user can close the dialog`() { + val eventsRecorder = EventsRecorder() + rule.setShowLocationView( + aShowLocationState( + permissionDialog = ShowLocationState.Dialog.PermissionDenied, + eventSink = eventsRecorder + ), + onBackPressed = EnsureNeverCalled(), + ) + rule.clickOn(CommonStrings.action_cancel) + eventsRecorder.assertSingle(ShowLocationEvents.DismissDialog) + } + + @Test + fun `when permission rationale is displayed user can request permissions`() { + val eventsRecorder = EventsRecorder() + rule.setShowLocationView( + aShowLocationState( + permissionDialog = ShowLocationState.Dialog.PermissionRationale, + eventSink = eventsRecorder + ), + onBackPressed = EnsureNeverCalled(), + ) + rule.clickOn(CommonStrings.action_continue) + eventsRecorder.assertSingle(ShowLocationEvents.RequestPermissions) + } + + @Test + fun `when permission rationale is displayed user can close the dialog`() { + val eventsRecorder = EventsRecorder() + rule.setShowLocationView( + aShowLocationState( + permissionDialog = ShowLocationState.Dialog.PermissionRationale, + eventSink = eventsRecorder + ), + onBackPressed = EnsureNeverCalled(), + ) + rule.clickOn(CommonStrings.action_cancel) + eventsRecorder.assertSingle(ShowLocationEvents.DismissDialog) + } +} + +private fun AndroidComposeTestRule.setShowLocationView( + state: ShowLocationState, + onBackPressed: () -> Unit = EnsureNeverCalled(), +) { + setContent { + // Simulate a LocalInspectionMode for MapboxMap + CompositionLocalProvider(LocalInspectionMode provides true) { + ShowLocationView( + state = state, + onBackPressed = onBackPressed, + ) + } + } +} diff --git a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/theme/components/FloatingActionButton.kt b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/theme/components/FloatingActionButton.kt index c7d9f2344f..1d50e77ce4 100644 --- a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/theme/components/FloatingActionButton.kt +++ b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/theme/components/FloatingActionButton.kt @@ -33,6 +33,8 @@ import androidx.compose.ui.unit.dp import io.element.android.compound.tokens.generated.CompoundIcons import io.element.android.libraries.designsystem.preview.ElementThemedPreview import io.element.android.libraries.designsystem.preview.PreviewGroup +import io.element.android.libraries.testtags.TestTags +import io.element.android.libraries.testtags.testTag @Composable fun FloatingActionButton( @@ -48,7 +50,7 @@ fun FloatingActionButton( ) { androidx.compose.material3.FloatingActionButton( onClick = onClick, - modifier = modifier, + modifier = modifier.testTag(TestTags.floatingActionButton), shape = shape, containerColor = containerColor, contentColor = contentColor, diff --git a/libraries/testtags/src/main/kotlin/io/element/android/libraries/testtags/TestTags.kt b/libraries/testtags/src/main/kotlin/io/element/android/libraries/testtags/TestTags.kt index c2acd98bfe..7992ddee17 100644 --- a/libraries/testtags/src/main/kotlin/io/element/android/libraries/testtags/TestTags.kt +++ b/libraries/testtags/src/main/kotlin/io/element/android/libraries/testtags/TestTags.kt @@ -75,6 +75,11 @@ object TestTags { val dialogNegative = TestTag("dialog-negative") val dialogNeutral = TestTag("dialog-neutral") + /** + * Floating Action Button. + */ + val floatingActionButton = TestTag("floating-action-button") + /** * Timeline item. */