mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Merge pull request #1111 from sgallese/feature/file-extensions-test
Tests for DateUtils and FileExtensions
This commit is contained in:
@@ -77,11 +77,11 @@ abstract class DateUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getLocalTime(): Long {
|
fun getLocalTime(utcTimeInMillis: Long? = null): Long {
|
||||||
if (fixedLocalTime != null) return fixedLocalTime as Long
|
if (fixedLocalTime != null) return fixedLocalTime as Long
|
||||||
|
|
||||||
val tz = getTimeZone()
|
val tz = getTimeZone()
|
||||||
val now = Date().time
|
val now = utcTimeInMillis ?: Date().time
|
||||||
return now + tz.getOffset(now)
|
return now + tz.getOffset(now)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +100,7 @@ abstract class DateUtils {
|
|||||||
format: Int,
|
format: Int,
|
||||||
firstWeekDay: Int
|
firstWeekDay: Int
|
||||||
): Array<String> {
|
): Array<String> {
|
||||||
val calendar = GregorianCalendar()
|
val calendar = GregorianCalendar(getLocale())
|
||||||
calendar.set(DAY_OF_WEEK, firstWeekDay)
|
calendar.set(DAY_OF_WEEK, firstWeekDay)
|
||||||
|
|
||||||
val daysNullable = ArrayList<String>()
|
val daysNullable = ArrayList<String>()
|
||||||
@@ -149,7 +149,7 @@ abstract class DateUtils {
|
|||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getFirstWeekdayNumberAccordingToLocale(): Int {
|
fun getFirstWeekdayNumberAccordingToLocale(): Int {
|
||||||
return GregorianCalendar().firstDayOfWeek
|
return GregorianCalendar(getLocale()).firstDayOfWeek
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -214,13 +214,7 @@ abstract class DateUtils {
|
|||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getStartOfTodayCalendarWithOffset(): GregorianCalendar = getCalendar(getStartOfTodayWithOffset())
|
fun getStartOfTodayCalendarWithOffset(): GregorianCalendar = getCalendar(getStartOfTodayWithOffset())
|
||||||
|
|
||||||
@JvmStatic
|
private fun getTimeZone(): TimeZone {
|
||||||
fun getTimeZone(): TimeZone {
|
|
||||||
return fixedTimeZone ?: TimeZone.getDefault()
|
|
||||||
}
|
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
fun getTimezone(): TimeZone {
|
|
||||||
return fixedTimeZone ?: TimeZone.getDefault()
|
return fixedTimeZone ?: TimeZone.getDefault()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,8 +230,7 @@ abstract class DateUtils {
|
|||||||
startDayMinuteOffset = minuteOffset
|
startDayMinuteOffset = minuteOffset
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
private fun getLocale(): Locale {
|
||||||
fun getLocale(): Locale {
|
|
||||||
return fixedLocale ?: Locale.getDefault()
|
return fixedLocale ?: Locale.getDefault()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ import org.isoron.uhabits.core.utils.DateUtils.Companion.truncate
|
|||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import java.util.Calendar
|
import java.util.Calendar
|
||||||
|
import java.util.GregorianCalendar
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
import java.util.TimeZone
|
import java.util.TimeZone
|
||||||
|
|
||||||
@@ -58,6 +59,129 @@ class DateUtilsTest : BaseUnitTest() {
|
|||||||
assertThat(formatted, equalTo("Thu\n31"))
|
assertThat(formatted, equalTo("Thu\n31"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetLocalTime() {
|
||||||
|
setFixedLocalTime(null)
|
||||||
|
setFixedTimeZone(TimeZone.getTimeZone("Australia/Sydney"))
|
||||||
|
val utcTestTimeInMillis = unixTime(2015, Calendar.JANUARY, 11)
|
||||||
|
val localTimeInMillis = DateUtils.getLocalTime(utcTestTimeInMillis)
|
||||||
|
val expectedUnixTimeOffsetForSydney = 11 * 60 * 60 * 1000
|
||||||
|
val expectedUnixTimeForSydney = utcTestTimeInMillis + expectedUnixTimeOffsetForSydney
|
||||||
|
assertThat(expectedUnixTimeForSydney, equalTo(localTimeInMillis))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetWeekdaySequence() {
|
||||||
|
val weekdaySequence = DateUtils.getWeekdaySequence(3)
|
||||||
|
assertThat(arrayOf(3, 4, 5, 6, 7, 1, 2), equalTo(weekdaySequence))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetFirstWeekdayNumberAccordingToLocale_germany() {
|
||||||
|
setFixedLocale(Locale.GERMANY)
|
||||||
|
val firstWeekdayNumber = DateUtils.getFirstWeekdayNumberAccordingToLocale()
|
||||||
|
assertThat(2, equalTo(firstWeekdayNumber))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetFirstWeekdayNumberAccordingToLocale_us() {
|
||||||
|
setFixedLocale(Locale.US)
|
||||||
|
val firstWeekdayNumber = DateUtils.getFirstWeekdayNumberAccordingToLocale()
|
||||||
|
assertThat(1, equalTo(firstWeekdayNumber))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetLongWeekdayNames_germany() {
|
||||||
|
setFixedLocale(Locale.GERMANY)
|
||||||
|
val longWeekdayNames = DateUtils.getLongWeekdayNames(Calendar.SATURDAY)
|
||||||
|
assertThat(arrayOf("Samstag", "Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag"), equalTo(longWeekdayNames))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetLongWeekdayNames_us() {
|
||||||
|
setFixedLocale(Locale.US)
|
||||||
|
val longWeekdayNames = DateUtils.getLongWeekdayNames(Calendar.SATURDAY)
|
||||||
|
assertThat(arrayOf("Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"), equalTo(longWeekdayNames))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetShortWeekdayNames_germany() {
|
||||||
|
setFixedLocale(Locale.GERMANY)
|
||||||
|
val longWeekdayNames = DateUtils.getShortWeekdayNames(Calendar.SATURDAY)
|
||||||
|
assertThat(arrayOf("Sa.", "So.", "Mo.", "Di.", "Mi.", "Do.", "Fr."), equalTo(longWeekdayNames))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetShortWeekdayNames_us() {
|
||||||
|
setFixedLocale(Locale.US)
|
||||||
|
val longWeekdayNames = DateUtils.getShortWeekdayNames(Calendar.SATURDAY)
|
||||||
|
assertThat(arrayOf("Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"), equalTo(longWeekdayNames))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetToday() {
|
||||||
|
setFixedLocalTime(FIXED_LOCAL_TIME)
|
||||||
|
val today = DateUtils.getToday()
|
||||||
|
assertThat(Timestamp(FIXED_LOCAL_TIME), equalTo(today))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetStartOfDay() {
|
||||||
|
val expectedStartOfDayUtc = fixedStartOfToday()
|
||||||
|
val laterInTheDayUtc = fixedStartOfTodayWithOffset(20)
|
||||||
|
val startOfDay = DateUtils.getStartOfDay(laterInTheDayUtc)
|
||||||
|
assertThat(expectedStartOfDayUtc, equalTo(startOfDay))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetStartOfToday() {
|
||||||
|
val expectedStartOfDayUtc = fixedStartOfToday()
|
||||||
|
val laterInTheDayUtc = fixedStartOfTodayWithOffset(20)
|
||||||
|
setFixedLocalTime(laterInTheDayUtc)
|
||||||
|
val startOfToday = DateUtils.getStartOfToday()
|
||||||
|
assertThat(expectedStartOfDayUtc, equalTo(startOfToday))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetStartOfTomorrowWithOffset_priorToOffset() {
|
||||||
|
val priorToOffset = HOUR_OFFSET - 1
|
||||||
|
testGetStartOfTomorrowWithOffset(priorToOffset)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetStartOfTomorrowWithOffset_afterOffset() {
|
||||||
|
val afterOffset = HOUR_OFFSET + 1 - HOURS_IN_ONE_DAY
|
||||||
|
testGetStartOfTomorrowWithOffset(afterOffset)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun testGetStartOfTomorrowWithOffset(startOfTodayOffset: Int) {
|
||||||
|
configureOffsetTest(startOfTodayOffset)
|
||||||
|
assertThat(
|
||||||
|
fixedStartOfTodayWithOffset(HOUR_OFFSET),
|
||||||
|
equalTo(DateUtils.getStartOfTomorrowWithOffset())
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetStartOfTodayWithOffset_priorToOffset() {
|
||||||
|
val priorToOffset = HOURS_IN_ONE_DAY + HOUR_OFFSET - 1
|
||||||
|
testGetStartOfTodayWithOffset(priorToOffset)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetStartOfTodayWithOffset_afterOffset() {
|
||||||
|
val afterOffset = HOUR_OFFSET + 1
|
||||||
|
testGetStartOfTodayWithOffset(afterOffset)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun testGetStartOfTodayWithOffset(startOfTodayOffset: Int) {
|
||||||
|
configureOffsetTest(startOfTodayOffset)
|
||||||
|
assertThat(
|
||||||
|
fixedStartOfToday(),
|
||||||
|
equalTo(DateUtils.getStartOfTodayWithOffset())
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testTruncate_dayOfWeek() {
|
fun testTruncate_dayOfWeek() {
|
||||||
val field = DateUtils.TruncateField.WEEK_NUMBER
|
val field = DateUtils.TruncateField.WEEK_NUMBER
|
||||||
@@ -142,22 +266,39 @@ class DateUtilsTest : BaseUnitTest() {
|
|||||||
assertThat(truncate(field, t2, firstWeekday), equalTo(expected))
|
assertThat(truncate(field, t2, firstWeekday), equalTo(expected))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testTruncate_timestamp() {
|
||||||
|
val field = DateUtils.TruncateField.YEAR
|
||||||
|
val nonTruncatedDate = unixTime(2016, Calendar.MAY, 30)
|
||||||
|
val expected = Timestamp(unixTime(2016, Calendar.JANUARY, 1))
|
||||||
|
assertThat(expected, equalTo(truncate(field, Timestamp(nonTruncatedDate), firstWeekday)))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetUpcomingTimeInMillis() {
|
||||||
|
setFixedLocalTime(FIXED_LOCAL_TIME)
|
||||||
|
setFixedTimeZone(TimeZone.getTimeZone("GMT"))
|
||||||
|
val expected = unixTime(2015, Calendar.JANUARY, 25, 10, 1)
|
||||||
|
val upcomingTimeMillis = DateUtils.getUpcomingTimeInMillis(10, 1)
|
||||||
|
assertThat(expected, equalTo(upcomingTimeMillis))
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun testMillisecondsUntilTomorrow() {
|
fun testMillisecondsUntilTomorrow() {
|
||||||
setFixedTimeZone(TimeZone.getTimeZone("GMT"))
|
setFixedTimeZone(TimeZone.getTimeZone("GMT"))
|
||||||
setFixedLocalTime(unixTime(2017, Calendar.JANUARY, 1, 23, 59))
|
setFixedLocalTime(unixTime(2017, Calendar.JANUARY, 1, 23, 59))
|
||||||
assertThat(millisecondsUntilTomorrowWithOffset(), equalTo(DateUtils.MINUTE_LENGTH))
|
assertThat(millisecondsUntilTomorrowWithOffset(), equalTo(DateUtils.MINUTE_LENGTH))
|
||||||
setFixedLocalTime(unixTime(2017, Calendar.JANUARY, 1, 20, 0))
|
setFixedLocalTime(fixedStartOfTodayWithOffset(20))
|
||||||
assertThat(
|
assertThat(
|
||||||
millisecondsUntilTomorrowWithOffset(),
|
millisecondsUntilTomorrowWithOffset(),
|
||||||
equalTo(4 * DateUtils.HOUR_LENGTH)
|
equalTo(4 * DateUtils.HOUR_LENGTH)
|
||||||
)
|
)
|
||||||
setStartDayOffset(3, 30)
|
setStartDayOffset(HOUR_OFFSET, 30)
|
||||||
setFixedLocalTime(unixTime(2017, Calendar.JANUARY, 1, 23, 59))
|
setFixedLocalTime(unixTime(2017, Calendar.JANUARY, 1, 23, 59))
|
||||||
assertThat(
|
assertThat(
|
||||||
millisecondsUntilTomorrowWithOffset(),
|
millisecondsUntilTomorrowWithOffset(),
|
||||||
equalTo(3 * DateUtils.HOUR_LENGTH + 31 * DateUtils.MINUTE_LENGTH)
|
equalTo(HOUR_OFFSET * DateUtils.HOUR_LENGTH + 31 * DateUtils.MINUTE_LENGTH)
|
||||||
)
|
)
|
||||||
setFixedLocalTime(unixTime(2017, Calendar.JANUARY, 2, 1, 0))
|
setFixedLocalTime(unixTime(2017, Calendar.JANUARY, 2, 1, 0))
|
||||||
assertThat(
|
assertThat(
|
||||||
@@ -166,6 +307,52 @@ class DateUtilsTest : BaseUnitTest() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetStartOfTodayCalendar() {
|
||||||
|
setFixedLocalTime(FIXED_LOCAL_TIME)
|
||||||
|
setFixedLocale(Locale.GERMANY)
|
||||||
|
val expectedStartOfDay = unixTime(2015, Calendar.JANUARY, 25, 0, 0)
|
||||||
|
val expectedCalendar = GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.GERMANY)
|
||||||
|
expectedCalendar.timeInMillis = expectedStartOfDay
|
||||||
|
val startOfTodayCalendar = DateUtils.getStartOfTodayCalendar()
|
||||||
|
assertThat(expectedCalendar, equalTo(startOfTodayCalendar))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetStartOfTodayCalendarWithOffset_priorToOffset() {
|
||||||
|
val priorToOffset = HOUR_OFFSET - 1
|
||||||
|
testGetStartOfTodayCalendarWithOffset(priorToOffset)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetStartOfTodayCalendarWithOffset_afterOffset() {
|
||||||
|
val afterOffset = HOUR_OFFSET + 1
|
||||||
|
testGetStartOfTodayCalendarWithOffset(afterOffset)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun testGetStartOfTodayCalendarWithOffset(startOfTodayOffset: Int) {
|
||||||
|
configureOffsetTest(startOfTodayOffset)
|
||||||
|
setFixedLocale(Locale.GERMANY)
|
||||||
|
val expectedCalendar = GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.GERMANY)
|
||||||
|
expectedCalendar.timeInMillis = fixedStartOfToday()
|
||||||
|
assertThat(
|
||||||
|
expectedCalendar,
|
||||||
|
equalTo(DateUtils.getStartOfTodayCalendar())
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun configureOffsetTest(startOfTodayOffset: Int) {
|
||||||
|
setStartDayOffset(HOUR_OFFSET, 0)
|
||||||
|
setFixedTimeZone(TimeZone.getTimeZone("GMT"))
|
||||||
|
setFixedLocalTime(fixedStartOfTodayWithOffset(startOfTodayOffset))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun fixedStartOfToday() = fixedStartOfTodayWithOffset(0)
|
||||||
|
|
||||||
|
private fun fixedStartOfTodayWithOffset(hourOffset: Int): Long {
|
||||||
|
return unixTime(2017, Calendar.JANUARY, 1, hourOffset, 0)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun testGetTodayWithOffset() {
|
fun testGetTodayWithOffset() {
|
||||||
@@ -469,4 +656,9 @@ class DateUtilsTest : BaseUnitTest() {
|
|||||||
unixTime(2018, Calendar.APRIL, 1, 18, 0)
|
unixTime(2018, Calendar.APRIL, 1, 18, 0)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val HOUR_OFFSET = 3
|
||||||
|
const val HOURS_IN_ONE_DAY = 24
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package org.isoron.uhabits.core.utils
|
||||||
|
|
||||||
|
import org.isoron.uhabits.core.BaseUnitTest
|
||||||
|
import org.junit.Test
|
||||||
|
import java.io.File
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
class FileExtensionsTest : BaseUnitTest() {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testIsSQLite3File() {
|
||||||
|
val file = File.createTempFile("asset", "")
|
||||||
|
copyAssetToFile("loop.db", file)
|
||||||
|
val isSqlite3File = file.isSQLite3File()
|
||||||
|
assertTrue(isSqlite3File)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user