Dodaj timeout i ręczny tryb offline przy uruchamianiu
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package pl.firmatpp.kierowca.data.sync
|
||||
|
||||
import java.io.IOException
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class StartupOfflineFallbackLoaderTest {
|
||||
@Test
|
||||
fun returnsServerDataBeforeTimeout() = runTest {
|
||||
val loader = StartupOfflineFallbackLoader(timeoutMillis = 1_000L)
|
||||
|
||||
val result = loader.load(
|
||||
onlineLoad = { "server" },
|
||||
offlineLoad = { "cache" },
|
||||
)
|
||||
|
||||
assertEquals("server", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun returnsCachedDataWhenServerExceedsTimeout() = runTest {
|
||||
val loader = StartupOfflineFallbackLoader(timeoutMillis = 1_000L)
|
||||
|
||||
val result = loader.load(
|
||||
onlineLoad = {
|
||||
delay(2_000L)
|
||||
"server"
|
||||
},
|
||||
offlineLoad = { "cache" },
|
||||
)
|
||||
|
||||
assertEquals("cache", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reportsMissingOfflineDataAfterTimeout() = runTest {
|
||||
val loader = StartupOfflineFallbackLoader(timeoutMillis = 1_000L)
|
||||
|
||||
val error = runCatching {
|
||||
loader.load(
|
||||
onlineLoad = {
|
||||
delay(2_000L)
|
||||
"server"
|
||||
},
|
||||
offlineLoad = { null },
|
||||
)
|
||||
}.exceptionOrNull()
|
||||
|
||||
assertTrue(error is IOException)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun doesNotHideAuthenticationFailures() = runTest {
|
||||
val loader = StartupOfflineFallbackLoader(timeoutMillis = 1_000L)
|
||||
val authenticationFailure = IllegalStateException("401")
|
||||
|
||||
val error = runCatching {
|
||||
loader.load(
|
||||
onlineLoad = { throw authenticationFailure },
|
||||
offlineLoad = { "cache" },
|
||||
)
|
||||
}.exceptionOrNull()
|
||||
|
||||
assertTrue(error is IllegalStateException)
|
||||
assertEquals(authenticationFailure.message, error?.message)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package pl.firmatpp.kierowca.domain
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class StartupSessionPolicyTest {
|
||||
@Test
|
||||
fun rotatesStartupMessagesAsWaitingTimeIncreases() {
|
||||
assertEquals("Sprawdzamy zapisaną sesję", StartupSessionPolicy.message(0).title)
|
||||
assertEquals("Potwierdzamy logowanie", StartupSessionPolicy.message(3).title)
|
||||
assertEquals("Pobieramy aktualne kursy", StartupSessionPolicy.message(6).title)
|
||||
assertEquals("To trwa dłużej niż zwykle", StartupSessionPolicy.message(9).title)
|
||||
assertEquals("Przygotowujemy tryb offline", StartupSessionPolicy.message(12).title)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun countsDownToOfflineFallbackWithoutGoingBelowZero() {
|
||||
assertEquals(15, StartupSessionPolicy.remainingSeconds(0))
|
||||
assertEquals(7, StartupSessionPolicy.remainingSeconds(8))
|
||||
assertEquals(0, StartupSessionPolicy.remainingSeconds(15))
|
||||
assertEquals(0, StartupSessionPolicy.remainingSeconds(30))
|
||||
assertEquals("Tryb offline najpóźniej za 5 s", StartupSessionPolicy.countdownLabel(10))
|
||||
assertEquals("Otwieramy zapisane dane offline", StartupSessionPolicy.countdownLabel(15))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun showsManualOfflineButtonAfterFourSecondsOnlyWhenCacheExists() {
|
||||
assertEquals(false, StartupSessionPolicy.shouldShowOfflineButton(3, offlineDataAvailable = true))
|
||||
assertEquals(true, StartupSessionPolicy.shouldShowOfflineButton(4, offlineDataAvailable = true))
|
||||
assertEquals(false, StartupSessionPolicy.shouldShowOfflineButton(10, offlineDataAvailable = false))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user