2021-01-23 08:02:11 +00:00
|
|
|
package org.schabi.newpipe
|
|
|
|
|
|
|
|
import org.junit.Assert.assertFalse
|
|
|
|
import org.junit.Assert.assertTrue
|
|
|
|
import org.junit.Test
|
2022-03-03 18:21:50 +00:00
|
|
|
import org.schabi.newpipe.util.ReleaseVersionUtil.coerceUpdateCheckExpiry
|
|
|
|
import org.schabi.newpipe.util.ReleaseVersionUtil.isLastUpdateCheckExpired
|
2021-01-23 08:02:11 +00:00
|
|
|
import java.time.Instant
|
|
|
|
import java.time.ZoneId
|
|
|
|
import java.time.format.DateTimeFormatter
|
|
|
|
import kotlin.math.abs
|
|
|
|
|
|
|
|
class NewVersionManagerTest {
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `Expiry is reached`() {
|
|
|
|
val oneHourEarlier = Instant.now().atZone(ZoneId.of("GMT")).minusHours(1)
|
|
|
|
|
2022-03-03 18:21:50 +00:00
|
|
|
val expired = isLastUpdateCheckExpired(oneHourEarlier.toEpochSecond())
|
2021-01-23 08:02:11 +00:00
|
|
|
|
|
|
|
assertTrue(expired)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `Expiry is not reached`() {
|
|
|
|
val oneHourLater = Instant.now().atZone(ZoneId.of("GMT")).plusHours(1)
|
|
|
|
|
2022-03-03 18:21:50 +00:00
|
|
|
val expired = isLastUpdateCheckExpired(oneHourLater.toEpochSecond())
|
2021-01-23 08:02:11 +00:00
|
|
|
|
|
|
|
assertFalse(expired)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Equal within a range of 5 seconds
|
|
|
|
*/
|
|
|
|
private fun assertNearlyEqual(a: Long, b: Long) {
|
|
|
|
assertTrue(abs(a - b) < 5)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `Expiry must be returned as is because it is inside the acceptable range of 6-72 hours`() {
|
|
|
|
val sixHoursLater = Instant.now().atZone(ZoneId.of("GMT")).plusHours(6)
|
|
|
|
|
2022-03-03 18:21:50 +00:00
|
|
|
val coerced = coerceUpdateCheckExpiry(DateTimeFormatter.RFC_1123_DATE_TIME.format(sixHoursLater))
|
2021-01-23 08:02:11 +00:00
|
|
|
|
|
|
|
assertNearlyEqual(sixHoursLater.toEpochSecond(), coerced)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `Expiry must be increased to 6 hours if below`() {
|
|
|
|
val tooLow = Instant.now().atZone(ZoneId.of("GMT")).plusHours(5)
|
|
|
|
|
2022-03-03 18:21:50 +00:00
|
|
|
val coerced = coerceUpdateCheckExpiry(DateTimeFormatter.RFC_1123_DATE_TIME.format(tooLow))
|
2021-01-23 08:02:11 +00:00
|
|
|
|
|
|
|
assertNearlyEqual(tooLow.plusHours(1).toEpochSecond(), coerced)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `Expiry must be decreased to 72 hours if above`() {
|
|
|
|
val tooHigh = Instant.now().atZone(ZoneId.of("GMT")).plusHours(73)
|
|
|
|
|
2022-03-03 18:21:50 +00:00
|
|
|
val coerced = coerceUpdateCheckExpiry(DateTimeFormatter.RFC_1123_DATE_TIME.format(tooHigh))
|
2021-01-23 08:02:11 +00:00
|
|
|
|
|
|
|
assertNearlyEqual(tooHigh.minusHours(1).toEpochSecond(), coerced)
|
|
|
|
}
|
|
|
|
}
|