Skip to content

Commit

Permalink
Merge pull request #67 from plaidev/flutter-e2e
Browse files Browse the repository at this point in the history
[android] impl test for syncDateFromHttpResponse
  • Loading branch information
RyosukeCla authored Nov 30, 2024
2 parents 3b0e030 + 7d2272c commit 694699c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions android/nativebrik/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ dependencies {
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.appcompat:appcompat:1.6.1")
testImplementation("junit:junit:4.13.2")
testImplementation("org.mockito:mockito-core:5.14.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal const val USER_SEED_MAX = 100000000
internal const val USER_SEED_KEY = "NATIVEBRIK_USER_SEED"

internal var DATETIME_OFFSET: Long = 0

internal fun getCurrentDate(): ZonedDateTime {
val currentMillis = ZonedDateTime.now().toInstant().toEpochMilli()
return ZonedDateTime.ofInstant(
Expand All @@ -39,7 +40,7 @@ internal fun getCurrentDate(): ZonedDateTime {
internal fun syncDateFromHttpResponse(t0: Long, connection: HttpURLConnection) {
val t1 = System.currentTimeMillis()

val serverDateStr = connection.headerFields["Date"]?.firstOrNull() ?: return
val serverDateStr = connection.getHeaderField("Date") ?: return

val serverTime = try {
val formatter = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.nativebrik.sdk.data.user

import org.junit.Assert.assertTrue
import org.junit.Test
import org.mockito.Mockito.mock
import org.mockito.Mockito.`when`
import java.net.HttpURLConnection
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.TimeZone
import kotlin.math.abs

class UtilsUnitTest {
@Test
fun testSyncDateFromHttpResponse_shouldWork() {
com.nativebrik.sdk.data.user.DATETIME_OFFSET = 0
val now = System.currentTimeMillis()
val tomorrow = Date(now + (24 * 60 * 60 * 1000))
val formatter = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US).apply {
timeZone = TimeZone.getTimeZone("GMT")
}
val formattedDate = formatter.format(tomorrow)
val connection = mock(HttpURLConnection::class.java)
`when`(connection.responseCode).thenReturn(400)
`when`(connection.getHeaderField("Date")).thenReturn(formattedDate)

syncDateFromHttpResponse(now, connection)
val offset = com.nativebrik.sdk.data.user.DATETIME_OFFSET
val diff = abs(offset - 24 * 60 * 60 * 1000)

assertTrue("time offset should be around 24 hours", diff < 5000)
}

@Test
fun testGetCurrentDate() {
com.nativebrik.sdk.data.user.DATETIME_OFFSET = 24 * 60 * 60 * 1000
val deviceCurrent = System.currentTimeMillis()
val syncedCurrent = getCurrentDate().toInstant().toEpochMilli()
val diff = (syncedCurrent - deviceCurrent) / 1000

assertTrue("(diff - 24 hours) should be around 2 sec", abs(diff - 24 * 60 * 60) < 2)
}
}

0 comments on commit 694699c

Please sign in to comment.