Skip to content

Commit e67cc2b

Browse files
author
Maxime NATUREL
committed
Adding unit tests on GetNotificationsStatusUseCase
1 parent 62912f8 commit e67cc2b

File tree

2 files changed

+136
-3
lines changed

2 files changed

+136
-3
lines changed

vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/GetNotificationsStatusUseCase.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package im.vector.app.features.settings.devices.v2.notification
1818

1919
import im.vector.app.core.di.ActiveSessionHolder
2020
import kotlinx.coroutines.flow.Flow
21-
import kotlinx.coroutines.flow.emptyFlow
2221
import kotlinx.coroutines.flow.flowOf
2322
import kotlinx.coroutines.flow.map
2423
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
@@ -34,11 +33,10 @@ class GetNotificationsStatusUseCase @Inject constructor(
3433
private val checkIfCanTogglePushNotificationsViaAccountDataUseCase: CheckIfCanTogglePushNotificationsViaAccountDataUseCase,
3534
) {
3635

37-
// TODO add unit tests
3836
fun execute(deviceId: String): Flow<NotificationsStatus> {
3937
val session = activeSessionHolder.getSafeActiveSession()
4038
return when {
41-
session == null -> emptyFlow()
39+
session == null -> flowOf(NotificationsStatus.NOT_SUPPORTED)
4240
checkIfCanTogglePushNotificationsViaPusherUseCase.execute() -> {
4341
session.flow()
4442
.livePushers()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright (c) 2022 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package im.vector.app.features.settings.devices.v2.notification
18+
19+
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
20+
import im.vector.app.test.fakes.FakeActiveSessionHolder
21+
import im.vector.app.test.fixtures.PusherFixture
22+
import im.vector.app.test.testDispatcher
23+
import io.mockk.every
24+
import io.mockk.mockk
25+
import kotlinx.coroutines.Dispatchers
26+
import kotlinx.coroutines.flow.firstOrNull
27+
import kotlinx.coroutines.test.resetMain
28+
import kotlinx.coroutines.test.runTest
29+
import kotlinx.coroutines.test.setMain
30+
import org.amshove.kluent.shouldBeEqualTo
31+
import org.junit.After
32+
import org.junit.Before
33+
import org.junit.Rule
34+
import org.junit.Test
35+
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
36+
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
37+
import org.matrix.android.sdk.api.session.events.model.toContent
38+
39+
private const val A_DEVICE_ID = "device-id"
40+
41+
class GetNotificationsStatusUseCaseTest {
42+
43+
@get:Rule
44+
val instantTaskExecutorRule = InstantTaskExecutorRule()
45+
46+
private val fakeActiveSessionHolder = FakeActiveSessionHolder()
47+
private val fakeCheckIfCanTogglePushNotificationsViaPusherUseCase =
48+
mockk<CheckIfCanTogglePushNotificationsViaPusherUseCase>()
49+
private val fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase =
50+
mockk<CheckIfCanTogglePushNotificationsViaAccountDataUseCase>()
51+
52+
private val getNotificationsStatusUseCase =
53+
GetNotificationsStatusUseCase(
54+
activeSessionHolder = fakeActiveSessionHolder.instance,
55+
checkIfCanTogglePushNotificationsViaPusherUseCase = fakeCheckIfCanTogglePushNotificationsViaPusherUseCase,
56+
checkIfCanTogglePushNotificationsViaAccountDataUseCase = fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase,
57+
)
58+
59+
@Before
60+
fun setup() {
61+
Dispatchers.setMain(testDispatcher)
62+
}
63+
64+
@After
65+
fun tearDown() {
66+
Dispatchers.resetMain()
67+
}
68+
69+
@Test
70+
fun `given NO current session when execute then resulting flow contains NOT_SUPPORTED value`() = runTest {
71+
// Given
72+
fakeActiveSessionHolder.givenGetSafeActiveSessionReturns(null)
73+
74+
// When
75+
val result = getNotificationsStatusUseCase.execute(A_DEVICE_ID)
76+
77+
// Then
78+
result.firstOrNull() shouldBeEqualTo NotificationsStatus.NOT_SUPPORTED
79+
}
80+
81+
@Test
82+
fun `given current session and toggle is not supported when execute then resulting flow contains NOT_SUPPORTED value`() = runTest {
83+
// Given
84+
every { fakeCheckIfCanTogglePushNotificationsViaPusherUseCase.execute() } returns false
85+
every { fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase.execute(A_DEVICE_ID) } returns false
86+
87+
// When
88+
val result = getNotificationsStatusUseCase.execute(A_DEVICE_ID)
89+
90+
// Then
91+
result.firstOrNull() shouldBeEqualTo NotificationsStatus.NOT_SUPPORTED
92+
}
93+
94+
@Test
95+
fun `given current session and toggle via pusher is supported when execute then resulting flow contains status based on pusher value`() = runTest {
96+
// Given
97+
val pushers = listOf(
98+
PusherFixture.aPusher(
99+
deviceId = A_DEVICE_ID,
100+
enabled = true,
101+
)
102+
)
103+
fakeActiveSessionHolder.fakeSession.pushersService().givenPushersLive(pushers)
104+
every { fakeCheckIfCanTogglePushNotificationsViaPusherUseCase.execute() } returns true
105+
every { fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase.execute(A_DEVICE_ID) } returns false
106+
107+
// When
108+
val result = getNotificationsStatusUseCase.execute(A_DEVICE_ID)
109+
110+
// Then
111+
result.firstOrNull() shouldBeEqualTo NotificationsStatus.ENABLED
112+
}
113+
114+
@Test
115+
fun `given current session and toggle via account data is supported when execute then resulting flow contains status based on settings value`() = runTest {
116+
// Given
117+
fakeActiveSessionHolder
118+
.fakeSession
119+
.accountDataService()
120+
.givenGetUserAccountDataEventReturns(
121+
type = UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + A_DEVICE_ID,
122+
content = LocalNotificationSettingsContent(
123+
isSilenced = false
124+
).toContent(),
125+
)
126+
every { fakeCheckIfCanTogglePushNotificationsViaPusherUseCase.execute() } returns false
127+
every { fakeCheckIfCanTogglePushNotificationsViaAccountDataUseCase.execute(A_DEVICE_ID) } returns true
128+
129+
// When
130+
val result = getNotificationsStatusUseCase.execute(A_DEVICE_ID)
131+
132+
// Then
133+
result.firstOrNull() shouldBeEqualTo NotificationsStatus.ENABLED
134+
}
135+
}

0 commit comments

Comments
 (0)