Troubleshoot notifications screen

This commit is contained in:
Benoit Marty 2024-03-26 11:36:31 +01:00
parent 6c9ea2b920
commit 2bfe125a77
80 changed files with 3086 additions and 99 deletions

View file

@ -23,6 +23,11 @@ plugins {
android {
namespace = "io.element.android.features.preferences.impl"
testOptions {
unitTests {
isIncludeAndroidResources = true
}
}
}
anvil {
@ -50,6 +55,7 @@ dependencies {
implementation(projects.libraries.mediapickers.api)
implementation(projects.libraries.mediaupload.api)
implementation(projects.libraries.permissions.api)
implementation(projects.libraries.push.api)
implementation(projects.features.rageshake.api)
implementation(projects.features.lockscreen.api)
implementation(projects.features.analytics.api)
@ -71,12 +77,14 @@ dependencies {
testImplementation(libs.test.truth)
testImplementation(libs.test.turbine)
testImplementation(libs.test.mockk)
testImplementation(libs.test.robolectric)
testImplementation(projects.libraries.matrix.test)
testImplementation(projects.libraries.featureflag.test)
testImplementation(projects.libraries.mediapickers.test)
testImplementation(projects.libraries.mediaupload.test)
testImplementation(projects.libraries.permissions.test)
testImplementation(projects.libraries.preferences.test)
testImplementation(projects.libraries.push.test)
testImplementation(projects.libraries.pushstore.test)
testImplementation(projects.features.rageshake.test)
testImplementation(projects.features.rageshake.impl)
@ -86,4 +94,6 @@ dependencies {
testImplementation(projects.services.toolbox.test)
testImplementation(projects.features.analytics.impl)
testImplementation(projects.tests.testutils)
testImplementation(libs.androidx.compose.ui.test.junit)
testReleaseImplementation(libs.androidx.compose.ui.test.manifest)
}

View file

@ -39,6 +39,7 @@ import io.element.android.features.preferences.impl.developer.DeveloperSettingsN
import io.element.android.features.preferences.impl.developer.tracing.ConfigureTracingNode
import io.element.android.features.preferences.impl.notifications.NotificationSettingsNode
import io.element.android.features.preferences.impl.notifications.edit.EditDefaultNotificationSettingNode
import io.element.android.features.preferences.impl.notifications.troubleshoot.TroubleshootNotificationsNode
import io.element.android.features.preferences.impl.root.PreferencesRootNode
import io.element.android.features.preferences.impl.user.editprofile.EditUserProfileNode
import io.element.android.libraries.architecture.BackstackView
@ -85,6 +86,9 @@ class PreferencesFlowNode @AssistedInject constructor(
@Parcelize
data object NotificationSettings : NavTarget
@Parcelize
data object TroubleshootNotifications : NavTarget
@Parcelize
data object LockScreenSettings : NavTarget
@ -177,9 +181,16 @@ class PreferencesFlowNode @AssistedInject constructor(
override fun editDefaultNotificationMode(isOneToOne: Boolean) {
backstack.push(NavTarget.EditDefaultNotificationSetting(isOneToOne))
}
override fun onTroubleshootNotificationsClicked() {
backstack.push(NavTarget.TroubleshootNotifications)
}
}
createNode<NotificationSettingsNode>(buildContext, listOf(notificationSettingsCallback))
}
NavTarget.TroubleshootNotifications -> {
createNode<TroubleshootNotificationsNode>(buildContext)
}
is NavTarget.EditDefaultNotificationSetting -> {
val callback = object : EditDefaultNotificationSettingNode.Callback {
override fun openRoomNotificationSettings(roomId: RoomId) {

View file

@ -35,6 +35,7 @@ class NotificationSettingsNode @AssistedInject constructor(
) : Node(buildContext, plugins = plugins) {
interface Callback : Plugin {
fun editDefaultNotificationMode(isOneToOne: Boolean)
fun onTroubleshootNotificationsClicked()
}
private val callbacks = plugins<Callback>()
@ -43,6 +44,10 @@ class NotificationSettingsNode @AssistedInject constructor(
callbacks.forEach { it.editDefaultNotificationMode(isOneToOne) }
}
private fun onTroubleshootNotificationsClicked() {
callbacks.forEach { it.onTroubleshootNotificationsClicked() }
}
@Composable
override fun View(modifier: Modifier) {
val state = presenter.present()
@ -50,6 +55,7 @@ class NotificationSettingsNode @AssistedInject constructor(
state = state,
onOpenEditDefault = { openEditDefault(isOneToOne = it) },
onBackPressed = ::navigateUp,
onTroubleshootNotificationsClicked = ::onTroubleshootNotificationsClicked,
modifier = modifier,
)
}

View file

@ -46,6 +46,7 @@ import io.element.android.libraries.ui.strings.CommonStrings
fun NotificationSettingsView(
state: NotificationSettingsState,
onOpenEditDefault: (isOneToOne: Boolean) -> Unit,
onTroubleshootNotificationsClicked: () -> Unit,
onBackPressed: () -> Unit,
modifier: Modifier = Modifier,
) {
@ -77,6 +78,7 @@ fun NotificationSettingsView(
// TODO We are removing the call notification toggle until support for call notifications has been added
// onCallsNotificationsChanged = { state.eventSink(NotificationSettingsEvents.SetCallNotificationsEnabled(it)) },
onInviteForMeNotificationsChanged = { state.eventSink(NotificationSettingsEvents.SetInviteForMeNotificationsEnabled(it)) },
onTroubleshootNotificationsClicked = onTroubleshootNotificationsClicked,
)
}
AsyncActionView(
@ -99,6 +101,7 @@ private fun NotificationSettingsContentView(
// TODO We are removing the call notification toggle until support for call notifications has been added
// onCallsNotificationsChanged: (Boolean) -> Unit,
onInviteForMeNotificationsChanged: (Boolean) -> Unit,
onTroubleshootNotificationsClicked: () -> Unit,
) {
val context = LocalContext.current
if (systemSettings.appNotificationsEnabled && !systemSettings.systemNotificationsEnabled) {
@ -163,6 +166,13 @@ private fun NotificationSettingsContentView(
onCheckedChange = onInviteForMeNotificationsChanged
)
}
PreferenceCategory(title = "Troubleshoot") {
PreferenceText(
modifier = Modifier,
title = "Troubleshoot notifications",
onClick = onTroubleshootNotificationsClicked
)
}
}
}
@ -204,6 +214,7 @@ internal fun NotificationSettingsViewPreview(@PreviewParameter(NotificationSetti
state = state,
onBackPressed = {},
onOpenEditDefault = {},
onTroubleshootNotificationsClicked = {},
)
}

View file

@ -0,0 +1,23 @@
/*
* 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.preferences.impl.notifications.troubleshoot
sealed interface TroubleshootNotificationsEvents {
data object StartTests : TroubleshootNotificationsEvents
data object RetryFailedTests : TroubleshootNotificationsEvents
data class QuickFix(val testIndex: Int) : TroubleshootNotificationsEvents
}

View file

@ -0,0 +1,44 @@
/*
* 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.preferences.impl.notifications.troubleshoot
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.bumble.appyx.core.modality.BuildContext
import com.bumble.appyx.core.node.Node
import com.bumble.appyx.core.plugin.Plugin
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import io.element.android.anvilannotations.ContributesNode
import io.element.android.libraries.di.SessionScope
@ContributesNode(SessionScope::class)
class TroubleshootNotificationsNode @AssistedInject constructor(
@Assisted buildContext: BuildContext,
@Assisted plugins: List<Plugin>,
private val presenter: TroubleshootNotificationsPresenter,
) : Node(buildContext, plugins = plugins) {
@Composable
override fun View(modifier: Modifier) {
val state = presenter.present()
TroubleshootNotificationsView(
state = state,
onBackPressed = ::navigateUp,
modifier = modifier,
)
}
}

View file

@ -0,0 +1,58 @@
/*
* 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.preferences.impl.notifications.troubleshoot
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import io.element.android.libraries.architecture.Presenter
import kotlinx.coroutines.launch
import javax.inject.Inject
class TroubleshootNotificationsPresenter @Inject constructor(
private val troubleshootTestSuite: TroubleshootTestSuite,
) : Presenter<TroubleshootNotificationsState> {
@Composable
override fun present(): TroubleshootNotificationsState {
val coroutineScope = rememberCoroutineScope()
LaunchedEffect(Unit) {
troubleshootTestSuite.start(this)
}
val testSuiteState by troubleshootTestSuite.state.collectAsState()
fun handleEvents(event: TroubleshootNotificationsEvents) {
when (event) {
TroubleshootNotificationsEvents.StartTests -> coroutineScope.launch {
troubleshootTestSuite.runTestSuite(this)
}
is TroubleshootNotificationsEvents.QuickFix -> coroutineScope.launch {
troubleshootTestSuite.quickFix(event.testIndex, this)
}
TroubleshootNotificationsEvents.RetryFailedTests -> coroutineScope.launch {
troubleshootTestSuite.retryFailedTest(this)
}
}
}
return TroubleshootNotificationsState(
testSuiteState = testSuiteState,
eventSink = ::handleEvents
)
}
}

View file

@ -0,0 +1,24 @@
/*
* 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.preferences.impl.notifications.troubleshoot
data class TroubleshootNotificationsState(
val testSuiteState: TroubleshootTestSuiteState,
val eventSink: (TroubleshootNotificationsEvents) -> Unit,
) {
val hasFailedTests: Boolean = testSuiteState.mainState.isFailure()
}

View file

@ -0,0 +1,119 @@
/*
* 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.preferences.impl.notifications.troubleshoot
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestState
import kotlinx.collections.immutable.toImmutableList
open class TroubleshootNotificationsStateProvider : PreviewParameterProvider<TroubleshootNotificationsState> {
override val values: Sequence<TroubleshootNotificationsState>
get() = sequenceOf(
aTroubleshootNotificationsState(
listOf(
aTroubleshootTestStateIdle(),
aTroubleshootTestStateIdle(),
aTroubleshootTestStateIdle(visible = false),
)
),
aTroubleshootNotificationsState(
listOf(
aTroubleshootTestStateInProgress(),
aTroubleshootTestStateIdle(),
aTroubleshootTestStateIdle(),
)
),
aTroubleshootNotificationsState(
listOf(
aTroubleshootTestStateSuccess(),
aTroubleshootTestStateInProgress(),
aTroubleshootTestStateIdle(),
)
),
aTroubleshootNotificationsState(
listOf(
aTroubleshootTestStateSuccess(),
aTroubleshootTestStateWaitingForUser(),
aTroubleshootTestStateIdle(),
)
),
aTroubleshootNotificationsState(
listOf(
aTroubleshootTestStateSuccess(),
aTroubleshootTestStateFailure(hasQuickFix = true),
aTroubleshootTestStateInProgress(),
)
),
aTroubleshootNotificationsState(
listOf(
aTroubleshootTestStateSuccess(),
aTroubleshootTestStateFailure(hasQuickFix = true),
aTroubleshootTestStateFailure(hasQuickFix = false),
)
),
aTroubleshootNotificationsState(
listOf(
aTroubleshootTestStateSuccess(),
aTroubleshootTestStateSuccess(),
aTroubleshootTestStateSuccess(),
)
),
aTroubleshootNotificationsState(
listOf(
aTroubleshootTestStateWaitingForUser(),
)
),
)
}
fun aTroubleshootNotificationsState(
tests: List<NotificationTroubleshootTestState> = emptyList(),
eventSink: (TroubleshootNotificationsEvents) -> Unit = {},
) = TroubleshootNotificationsState(
eventSink = eventSink,
testSuiteState = TroubleshootTestSuiteState(
mainState = tests.computeMainState(),
tests = tests.toImmutableList(),
),
)
fun aTroubleshootTestState(
status: NotificationTroubleshootTestState.Status,
name: String = "Test",
description: String = "Description",
): NotificationTroubleshootTestState {
return NotificationTroubleshootTestState(
name = name,
description = description,
status = status,
)
}
fun aTroubleshootTestStateIdle(visible: Boolean = true) =
aTroubleshootTestState(status = NotificationTroubleshootTestState.Status.Idle(visible = visible))
fun aTroubleshootTestStateInProgress() =
aTroubleshootTestState(status = NotificationTroubleshootTestState.Status.InProgress)
fun aTroubleshootTestStateWaitingForUser() =
aTroubleshootTestState(status = NotificationTroubleshootTestState.Status.WaitingForUser)
fun aTroubleshootTestStateSuccess() =
aTroubleshootTestState(status = NotificationTroubleshootTestState.Status.Success)
fun aTroubleshootTestStateFailure(hasQuickFix: Boolean) =
aTroubleshootTestState(status = NotificationTroubleshootTestState.Status.Failure(hasQuickFix = hasQuickFix))

View file

@ -0,0 +1,220 @@
/*
* 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.preferences.impl.notifications.troubleshoot
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.progressSemantics
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import io.element.android.compound.theme.ElementTheme
import io.element.android.compound.tokens.generated.CompoundIcons
import io.element.android.libraries.architecture.AsyncAction
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestState
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestState.Status
import io.element.android.libraries.designsystem.components.list.ListItemContent
import io.element.android.libraries.designsystem.components.preferences.PreferencePage
import io.element.android.libraries.designsystem.preview.ElementPreview
import io.element.android.libraries.designsystem.preview.PreviewsDayNight
import io.element.android.libraries.designsystem.theme.components.Button
import io.element.android.libraries.designsystem.theme.components.CircularProgressIndicator
import io.element.android.libraries.designsystem.theme.components.Icon
import io.element.android.libraries.designsystem.theme.components.ListItem
import io.element.android.libraries.designsystem.theme.components.Text
import io.element.android.libraries.designsystem.utils.OnLifecycleEvent
/**
* A view that allows a user edit their global notification settings.
*/
@Composable
fun TroubleshootNotificationsView(
state: TroubleshootNotificationsState,
onBackPressed: () -> Unit,
modifier: Modifier = Modifier,
) {
OnLifecycleEvent { _, event ->
when (event) {
Lifecycle.Event.ON_RESUME -> {
if (state.hasFailedTests) {
state.eventSink(TroubleshootNotificationsEvents.RetryFailedTests)
}
}
else -> Unit
}
}
PreferencePage(
modifier = modifier,
onBackPressed = onBackPressed,
title = "Troubleshoot notifications",
) {
TroubleshootNotificationsContent(state)
}
}
@Composable
private fun TroubleshootTestView(
testState: NotificationTroubleshootTestState,
onQuickFixClicked: () -> Unit,
) {
if ((testState.status as? Status.Idle)?.visible == false) return
ListItem(
headlineContent = { Text(text = testState.name) },
supportingContent = { Text(text = testState.description) },
trailingContent = when (testState.status) {
is Status.Idle -> null
Status.InProgress -> ListItemContent.Custom {
CircularProgressIndicator(
modifier = Modifier
.progressSemantics()
.size(20.dp),
strokeWidth = 2.dp
)
}
Status.WaitingForUser -> ListItemContent.Custom {
Icon(
contentDescription = null,
modifier = Modifier.size(24.dp),
imageVector = CompoundIcons.Info(),
tint = ElementTheme.colors.iconAccentTertiary
)
}
Status.Success -> ListItemContent.Custom {
Icon(
contentDescription = null,
modifier = Modifier.size(24.dp),
imageVector = CompoundIcons.Check(),
tint = ElementTheme.colors.iconAccentTertiary
)
}
is Status.Failure -> ListItemContent.Custom {
Icon(
contentDescription = null,
modifier = Modifier.size(24.dp),
imageVector = CompoundIcons.Error(),
tint = ElementTheme.colors.textCriticalPrimary
)
}
}
)
if ((testState.status as? Status.Failure)?.hasQuickFix == true) {
ListItem(
headlineContent = {
},
trailingContent = ListItemContent.Custom {
Button(
text = "Attempt to fix",
onClick = onQuickFixClicked
)
}
)
}
}
@Composable
private fun TroubleshootNotificationsContent(state: TroubleshootNotificationsState) {
when (state.testSuiteState.mainState) {
AsyncAction.Loading,
AsyncAction.Confirming,
is AsyncAction.Success,
is AsyncAction.Failure -> {
TestSuiteView(
testSuiteState = state.testSuiteState,
onQuickFixClicked = {
state.eventSink(TroubleshootNotificationsEvents.QuickFix(it))
}
)
}
AsyncAction.Uninitialized -> Unit
}
when (state.testSuiteState.mainState) {
AsyncAction.Uninitialized -> {
ListItem(headlineContent = {
Text(
text = "Run the tests to detect any issue in your configuration " +
"that may make notifications not behave as expected."
)
})
RunTestButton(state = state)
}
AsyncAction.Loading -> Unit
is AsyncAction.Failure -> {
ListItem(headlineContent = {
Text(text = "Some tests failed, please check the details.")
})
RunTestButton(state = state)
}
AsyncAction.Confirming -> {
ListItem(headlineContent = {
Text(
text = "Some tests require your attention. Please check the details."
)
})
}
is AsyncAction.Success -> {
ListItem(headlineContent = {
Text(
text = "All tests passed successfully."
)
})
}
}
}
@Composable
private fun RunTestButton(state: TroubleshootNotificationsState) {
ListItem(
headlineContent = {
Button(
text = if (state.testSuiteState.mainState is AsyncAction.Failure) "Run tests again" else "Run tests",
onClick = {
state.eventSink(TroubleshootNotificationsEvents.StartTests)
},
modifier = Modifier.fillMaxWidth(),
)
}
)
}
@Composable
private fun TestSuiteView(
testSuiteState: TroubleshootTestSuiteState,
onQuickFixClicked: (Int) -> Unit,
) {
testSuiteState.tests.forEachIndexed { index, testState ->
TroubleshootTestView(
testState = testState,
onQuickFixClicked = {
onQuickFixClicked(index)
},
)
}
}
@PreviewsDayNight
@Composable
internal fun TroubleshootNotificationsViewPreview(
@PreviewParameter(TroubleshootNotificationsStateProvider::class) state: TroubleshootNotificationsState,
) = ElementPreview {
TroubleshootNotificationsView(
state = state,
onBackPressed = {},
)
}

View file

@ -0,0 +1,109 @@
/*
* 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.preferences.impl.notifications.troubleshoot
import io.element.android.libraries.architecture.AsyncAction
import io.element.android.libraries.core.notifications.NotificationTroubleshootTest
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestState
import io.element.android.libraries.core.notifications.TestFilterData
import io.element.android.libraries.push.api.GetCurrentPushProvider
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import javax.inject.Inject
class TroubleshootTestSuite @Inject constructor(
private val notificationTroubleshootTests: Set<@JvmSuppressWildcards NotificationTroubleshootTest>,
private val getCurrentPushProvider: GetCurrentPushProvider,
) {
lateinit var tests: List<NotificationTroubleshootTest>
private val _state: MutableStateFlow<TroubleshootTestSuiteState> = MutableStateFlow(
TroubleshootTestSuiteState(
mainState = AsyncAction.Uninitialized,
tests = emptyList<NotificationTroubleshootTestState>().toImmutableList()
)
)
val state: StateFlow<TroubleshootTestSuiteState> = _state
suspend fun start(coroutineScope: CoroutineScope) {
val testFilterData = TestFilterData(
currentPushProviderName = getCurrentPushProvider.getCurrentPushProvider()
)
tests = notificationTroubleshootTests
.filter { it.isRelevant(testFilterData) }
.sortedBy { it.order }
tests.forEach {
// Observe the state of the tests
it.state.onEach {
emitState()
}.launchIn(coroutineScope)
}
}
suspend fun runTestSuite(coroutineScope: CoroutineScope) {
tests.forEach {
it.reset()
}
tests.forEach {
it.run(coroutineScope)
}
}
suspend fun retryFailedTest(coroutineScope: CoroutineScope) {
tests
.filter { it.state.value.status is NotificationTroubleshootTestState.Status.Failure }
.forEach {
it.run(coroutineScope)
}
}
private fun emitState() {
val states = tests.map { it.state.value }
_state.tryEmit(
TroubleshootTestSuiteState(
mainState = states.computeMainState(),
tests = states.toImmutableList()
)
)
}
suspend fun quickFix(testIndex: Int, coroutineScope: CoroutineScope) {
tests[testIndex].quickFix(coroutineScope)
}
}
fun List<NotificationTroubleshootTestState>.computeMainState(): AsyncAction<Unit> {
val isIdle = all { it.status is NotificationTroubleshootTestState.Status.Idle }
val isRunning = any { it.status is NotificationTroubleshootTestState.Status.InProgress }
return when {
isIdle -> AsyncAction.Uninitialized
isRunning -> AsyncAction.Loading
else -> {
if (any { it.status is NotificationTroubleshootTestState.Status.WaitingForUser }) {
AsyncAction.Confirming
} else if (any { it.status is NotificationTroubleshootTestState.Status.Failure }) {
AsyncAction.Failure(Exception("Some tests failed"))
} else {
AsyncAction.Success(Unit)
}
}
}
}

View file

@ -0,0 +1,26 @@
/*
* 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.preferences.impl.notifications.troubleshoot
import io.element.android.libraries.architecture.AsyncAction
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestState
import kotlinx.collections.immutable.ImmutableList
data class TroubleshootTestSuiteState(
val mainState: AsyncAction<Unit>,
val tests: ImmutableList<NotificationTroubleshootTestState>,
)

View file

@ -0,0 +1,82 @@
/*
* 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.preferences.impl.notifications.troubleshoot
import io.element.android.libraries.core.notifications.NotificationTroubleshootTest
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
class FakeNotificationTroubleshootTest(
override val order: Int = 0,
private val defaultName: String = "test name",
private val defaultDescription: String = "test description",
private val firstStatus: NotificationTroubleshootTestState.Status = NotificationTroubleshootTestState.Status.Idle(visible = true),
private val runAction: () -> NotificationTroubleshootTestState? = { null },
private val resetAction: () -> NotificationTroubleshootTestState? = { null },
private val quickFixAction: () -> NotificationTroubleshootTestState? = { null },
) : NotificationTroubleshootTest {
private val _state = MutableStateFlow(
NotificationTroubleshootTestState(
name = defaultName,
description = defaultDescription,
status = firstStatus
)
)
override val state: StateFlow<NotificationTroubleshootTestState> = _state.asStateFlow()
override suspend fun run(coroutineScope: CoroutineScope) {
updateState(NotificationTroubleshootTestState.Status.InProgress)
runAction()?.let {
_state.tryEmit(it)
}
}
override fun reset() {
updateState(
name = defaultName,
description = defaultDescription,
status = firstStatus,
)
resetAction()?.let {
_state.tryEmit(it)
}
}
override suspend fun quickFix(coroutineScope: CoroutineScope) {
updateState(NotificationTroubleshootTestState.Status.InProgress)
quickFixAction()?.let {
_state.tryEmit(it)
}
}
fun updateState(
status: NotificationTroubleshootTestState.Status,
name: String = defaultName,
description: String = defaultDescription,
) {
_state.tryEmit(
NotificationTroubleshootTestState(
name = name,
description = description,
status = status,
)
)
}
}

View file

@ -0,0 +1,126 @@
/*
* Copyright (c) 2023 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.preferences.impl.notifications.troubleshoot
import app.cash.molecule.RecompositionMode
import app.cash.molecule.moleculeFlow
import app.cash.turbine.test
import com.google.common.truth.Truth.assertThat
import io.element.android.libraries.architecture.AsyncAction
import io.element.android.libraries.core.notifications.NotificationTroubleshootTest
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestState
import io.element.android.libraries.push.test.FakeGetCurrentPushProvider
import kotlinx.coroutines.test.runTest
import org.junit.Test
class TroubleshootNotificationsPresenterTests {
@Test
fun `present - initial state`() = runTest {
val presenter = createTroubleshootNotificationsPresenter()
moleculeFlow(RecompositionMode.Immediate) {
presenter.present()
}.test {
val initialState = awaitItem()
assertThat(initialState.testSuiteState.tests).isEmpty()
assertThat(initialState.testSuiteState.mainState).isEqualTo(AsyncAction.Uninitialized)
}
}
@Test
fun `present - start test`() = runTest {
val troubleshootTestSuite = createTroubleshootTestSuite(
tests = setOf(FakeNotificationTroubleshootTest())
)
val presenter = createTroubleshootNotificationsPresenter(
troubleshootTestSuite = troubleshootTestSuite,
)
moleculeFlow(RecompositionMode.Immediate) {
presenter.present()
}.test {
val initialState = awaitItem()
initialState.eventSink(TroubleshootNotificationsEvents.StartTests)
skipItems(1)
val stateAfterStart = awaitItem()
assertThat(stateAfterStart.testSuiteState.mainState).isEqualTo(AsyncAction.Loading)
}
}
@Test
fun `present - start failed test`() = runTest {
val troubleshootTestSuite = createTroubleshootTestSuite(
tests = setOf(
FakeNotificationTroubleshootTest(
firstStatus = NotificationTroubleshootTestState.Status.Failure(hasQuickFix = false)
)
)
)
val presenter = createTroubleshootNotificationsPresenter(
troubleshootTestSuite = troubleshootTestSuite,
)
moleculeFlow(RecompositionMode.Immediate) {
presenter.present()
}.test {
val initialState = awaitItem()
initialState.eventSink(TroubleshootNotificationsEvents.RetryFailedTests)
skipItems(1)
val stateAfterStart = awaitItem()
assertThat(stateAfterStart.testSuiteState.mainState).isEqualTo(AsyncAction.Loading)
}
}
@Test
fun `present - quick fix test`() = runTest {
val troubleshootTestSuite = createTroubleshootTestSuite(
tests = setOf(
FakeNotificationTroubleshootTest(
firstStatus = NotificationTroubleshootTestState.Status.Failure(hasQuickFix = false)
)
)
)
val presenter = createTroubleshootNotificationsPresenter(
troubleshootTestSuite = troubleshootTestSuite,
)
moleculeFlow(RecompositionMode.Immediate) {
presenter.present()
}.test {
skipItems(1)
val initialState = awaitItem()
assertThat(initialState.testSuiteState.mainState).isInstanceOf(AsyncAction.Failure::class.java)
initialState.eventSink(TroubleshootNotificationsEvents.QuickFix(0))
val stateAfterStart = awaitItem()
assertThat(stateAfterStart.testSuiteState.mainState).isEqualTo(AsyncAction.Loading)
}
}
private fun createTroubleshootTestSuite(
tests: Set<NotificationTroubleshootTest> = emptySet(),
currentPushProvider: String? = null,
): TroubleshootTestSuite {
return TroubleshootTestSuite(
notificationTroubleshootTests = tests,
getCurrentPushProvider = FakeGetCurrentPushProvider(currentPushProvider),
)
}
private fun createTroubleshootNotificationsPresenter(
troubleshootTestSuite: TroubleshootTestSuite = createTroubleshootTestSuite(),
): TroubleshootNotificationsPresenter {
return TroubleshootNotificationsPresenter(
troubleshootTestSuite = troubleshootTestSuite,
)
}
}

View file

@ -0,0 +1,115 @@
/*
* 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.preferences.impl.notifications.troubleshoot
import androidx.activity.ComponentActivity
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.element.android.tests.testutils.EnsureNeverCalled
import io.element.android.tests.testutils.EventsRecorder
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
import org.robolectric.annotation.Config
@RunWith(AndroidJUnit4::class)
class TroubleshootNotificationsViewTest {
@get:Rule
val rule = createAndroidComposeRule<ComponentActivity>()
@Test
fun `press menu back invokes the expected callback`() {
val eventsRecorder = EventsRecorder<TroubleshootNotificationsEvents>(expectEvents = false)
ensureCalledOnce {
rule.setTroubleshootNotificationsView(
state = aTroubleshootNotificationsState(
eventSink = eventsRecorder
),
onBackPressed = it,
)
rule.pressBack()
}
}
@Test
fun `clicking on run test emits the expected Event`() {
val eventsRecorder = EventsRecorder<TroubleshootNotificationsEvents>()
rule.setTroubleshootNotificationsView(
aTroubleshootNotificationsState(
eventSink = eventsRecorder
),
)
rule.onNodeWithText("Run tests").performClick()
eventsRecorder.assertSingle(TroubleshootNotificationsEvents.StartTests)
}
@Config(qualifiers = "h1024dp")
@Test
fun `clicking on run test again emits the expected Event`() {
val eventsRecorder = EventsRecorder<TroubleshootNotificationsEvents>()
rule.setTroubleshootNotificationsView(
aTroubleshootNotificationsState(
tests = listOf(aTroubleshootTestStateFailure(hasQuickFix = false)),
eventSink = eventsRecorder
),
)
rule.onNodeWithText("Run tests again").performClick()
eventsRecorder.assertList(
listOf(
TroubleshootNotificationsEvents.RetryFailedTests,
TroubleshootNotificationsEvents.StartTests,
)
)
}
@Config(qualifiers = "h1024dp")
@Test
fun `clicking on quick fix emits the expected Event`() {
val eventsRecorder = EventsRecorder<TroubleshootNotificationsEvents>()
rule.setTroubleshootNotificationsView(
aTroubleshootNotificationsState(
tests = listOf(aTroubleshootTestStateFailure(hasQuickFix = true)),
eventSink = eventsRecorder
),
)
rule.onNodeWithText("Attempt to fix").performClick()
eventsRecorder.assertList(
listOf(
TroubleshootNotificationsEvents.RetryFailedTests,
TroubleshootNotificationsEvents.QuickFix(0),
)
)
}
}
private fun <R : TestRule> AndroidComposeTestRule<R, ComponentActivity>.setTroubleshootNotificationsView(
state: TroubleshootNotificationsState,
onBackPressed: () -> Unit = EnsureNeverCalled(),
) {
setContent {
TroubleshootNotificationsView(
state = state,
onBackPressed = onBackPressed,
)
}
}