Skip to content

Commit 8e23ea1

Browse files
committed
Merge branch 'issue/migrate_events_builder_to_kotlin' into issue/parsely_tracker_kotlin_migration
2 parents 2e77807 + 2c64338 commit 8e23ea1

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

parsely/src/main/java/com/parsely/parselyandroid/EventsBuilder.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import java.util.TimeZone
66

77
internal class EventsBuilder(
88
private val deviceInfoRepository: DeviceInfoRepository,
9-
private val siteId: String
9+
private val siteId: String,
10+
private val clock: Clock,
1011
) {
1112
/**
1213
* Create an event Map
@@ -26,7 +27,6 @@ internal class EventsBuilder(
2627
uuid: String
2728
): Map<String, Any> {
2829
log("buildEvent called for %s/%s", action, url)
29-
val now = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
3030

3131
// Main event info
3232
val event: MutableMap<String, Any> = HashMap()
@@ -41,7 +41,7 @@ internal class EventsBuilder(
4141
data.putAll(extraData)
4242
}
4343
val deviceInfo = deviceInfoRepository.collectDeviceInfo()
44-
data["ts"] = now.timeInMillis
44+
data["ts"] = clock.now.inWholeMilliseconds
4545
data.putAll(deviceInfo)
4646
event["data"] = data
4747
metadata?.let {

parsely/src/main/java/com/parsely/parselyandroid/ParselyTracker.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ public open class ParselyTracker protected constructor(siteId: String, flushInte
4747
*/
4848
init {
4949
val context = c.applicationContext
50+
clock = Clock()
5051
eventsBuilder = EventsBuilder(
5152
AndroidDeviceInfoRepository(
5253
AdvertisementIdProvider(context, sdkScope),
5354
AndroidIdProvider(context)
54-
), siteId
55+
), siteId, clock
5556
)
5657
val localStorageRepository = LocalStorageRepository(context)
5758
flushManager = ParselyFlushManager(
@@ -73,7 +74,6 @@ public open class ParselyTracker protected constructor(siteId: String, flushInte
7374
sdkScope,
7475
AndroidConnectivityStatusProvider(context)
7576
)
76-
clock = Clock()
7777
intervalCalculator = HeartbeatIntervalCalculator(clock)
7878

7979
isDebug = false

parsely/src/test/java/com/parsely/parselyandroid/EventsBuilderTest.kt

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
package com.parsely.parselyandroid
22

3+
import kotlin.time.Duration
4+
import kotlin.time.Duration.Companion.hours
35
import org.assertj.core.api.Assertions.assertThat
46
import org.assertj.core.api.MapAssert
57
import org.junit.Before
68
import org.junit.Test
79

810
internal class EventsBuilderTest {
911
private lateinit var sut: EventsBuilder
12+
private val clock = FakeClock()
1013

1114
@Before
1215
fun setUp() {
1316
sut = EventsBuilder(
1417
FakeDeviceInfoRepository(),
1518
TEST_SITE_ID,
19+
clock
1620
)
1721
}
1822

@@ -187,20 +191,22 @@ internal class EventsBuilderTest {
187191
assertThat(it)
188192
.hasSize(2)
189193
.containsAllEntriesOf(FAKE_DEVICE_INFO)
190-
.hasEntrySatisfying("ts") { timestamp ->
191-
assertThat(timestamp as Long).isBetween(1111111111111, 9999999999999)
192-
}
193194
}
194195

195196
companion object {
196197
const val TEST_SITE_ID = "Example"
197198
const val TEST_URL = "http://example.com/some-old/article.html"
198199
const val TEST_UUID = "123e4567-e89b-12d3-a456-426614174000"
199200

200-
val FAKE_DEVICE_INFO = mapOf("device" to "info")
201+
val FAKE_NOW = 15.hours
202+
val FAKE_DEVICE_INFO = mapOf("device" to "info", "ts" to FAKE_NOW.inWholeMilliseconds.toString())
201203
}
202204

203205
class FakeDeviceInfoRepository: DeviceInfoRepository {
204206
override fun collectDeviceInfo(): Map<String, String> = FAKE_DEVICE_INFO
205207
}
208+
209+
class FakeClock() : Clock() {
210+
override val now: Duration = FAKE_NOW
211+
}
206212
}

0 commit comments

Comments
 (0)