Create use case to decide to show alert.
This commit is contained in:
parent
821a561235
commit
8835e4d25e
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright (c) 2022 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 im.vector.app.features.home
|
||||
|
||||
import im.vector.app.config.Config
|
||||
import im.vector.app.core.time.Clock
|
||||
import im.vector.app.features.VectorFeatures
|
||||
import im.vector.app.features.settings.VectorPreferences
|
||||
import javax.inject.Inject
|
||||
|
||||
class ShouldShowUnverifiedSessionsAlertUseCase @Inject constructor(
|
||||
private val vectorFeatures: VectorFeatures,
|
||||
private val vectorPreferences: VectorPreferences,
|
||||
private val clock: Clock,
|
||||
) {
|
||||
|
||||
fun execute(): Boolean {
|
||||
val isUnverifiedSessionsAlertEnabled = vectorFeatures.isUnverifiedSessionsAlertEnabled()
|
||||
val unverifiedSessionsAlertLastShownMillis = vectorPreferences.getUnverifiedSessionsAlertLastShownMillis()
|
||||
return isUnverifiedSessionsAlertEnabled &&
|
||||
clock.epochMillis() - unverifiedSessionsAlertLastShownMillis >= Config.SHOW_UNVERIFIED_SESSIONS_ALERT_AFTER_MILLIS
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright (c) 2022 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 im.vector.app.features.home
|
||||
|
||||
import im.vector.app.config.Config
|
||||
import im.vector.app.test.fakes.FakeClock
|
||||
import im.vector.app.test.fakes.FakeVectorFeatures
|
||||
import im.vector.app.test.fakes.FakeVectorPreferences
|
||||
import org.amshove.kluent.shouldBe
|
||||
import org.junit.Test
|
||||
|
||||
private val AN_EPOCH = Config.SHOW_UNVERIFIED_SESSIONS_ALERT_AFTER_MILLIS
|
||||
|
||||
class ShouldShowUnverifiedSessionsAlertUseCaseTest {
|
||||
|
||||
private val fakeVectorFeatures = FakeVectorFeatures()
|
||||
private val fakeVectorPreferences = FakeVectorPreferences()
|
||||
private val fakeClock = FakeClock()
|
||||
|
||||
private val shouldShowUnverifiedSessionsAlertUseCase = ShouldShowUnverifiedSessionsAlertUseCase(
|
||||
vectorFeatures = fakeVectorFeatures,
|
||||
vectorPreferences = fakeVectorPreferences.instance,
|
||||
clock = fakeClock,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `given the feature is disabled then the use case returns false`() {
|
||||
fakeVectorFeatures.givenUnverifiedSessionsAlertEnabled(false)
|
||||
fakeVectorPreferences.givenUnverifiedSessionsAlertLastShownMillis(0L)
|
||||
|
||||
shouldShowUnverifiedSessionsAlertUseCase.execute() shouldBe false
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given the feature in enabled and there is not a saved preference then the use case returns true`() {
|
||||
fakeVectorFeatures.givenUnverifiedSessionsAlertEnabled(true)
|
||||
fakeVectorPreferences.givenUnverifiedSessionsAlertLastShownMillis(0L)
|
||||
fakeClock.givenEpoch(AN_EPOCH + 1)
|
||||
|
||||
shouldShowUnverifiedSessionsAlertUseCase.execute() shouldBe true
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given the feature in enabled and last shown is a long time ago then the use case returns true`() {
|
||||
fakeVectorFeatures.givenUnverifiedSessionsAlertEnabled(true)
|
||||
fakeVectorPreferences.givenUnverifiedSessionsAlertLastShownMillis(AN_EPOCH)
|
||||
fakeClock.givenEpoch(AN_EPOCH * 2 + 1)
|
||||
|
||||
shouldShowUnverifiedSessionsAlertUseCase.execute() shouldBe true
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given the feature in enabled and last shown is not a long time ago then the use case returns false`() {
|
||||
fakeVectorFeatures.givenUnverifiedSessionsAlertEnabled(true)
|
||||
fakeVectorPreferences.givenUnverifiedSessionsAlertLastShownMillis(AN_EPOCH)
|
||||
fakeClock.givenEpoch(AN_EPOCH + 1)
|
||||
|
||||
shouldShowUnverifiedSessionsAlertUseCase.execute() shouldBe false
|
||||
}
|
||||
}
|
|
@ -50,4 +50,8 @@ class FakeVectorFeatures : VectorFeatures by spyk<DefaultVectorFeatures>() {
|
|||
fun givenVoiceBroadcast(isEnabled: Boolean) {
|
||||
every { isVoiceBroadcastEnabled() } returns isEnabled
|
||||
}
|
||||
|
||||
fun givenUnverifiedSessionsAlertEnabled(isEnabled: Boolean) {
|
||||
every { isUnverifiedSessionsAlertEnabled() } returns isEnabled
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,4 +56,8 @@ class FakeVectorPreferences {
|
|||
fun givenSessionManagerShowIpAddress(showIpAddress: Boolean) {
|
||||
every { instance.showIpAddressInSessionManagerScreens() } returns showIpAddress
|
||||
}
|
||||
|
||||
fun givenUnverifiedSessionsAlertLastShownMillis(lastShownMillis: Long) {
|
||||
every { instance.getUnverifiedSessionsAlertLastShownMillis() } returns lastShownMillis
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue