Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telemetry events: migrating to NavNative #6852

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions instrumentation-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dependencies {
implementation project(':libnavigation-android')
implementation project(':libnavui-dropin')
implementation project(":libnavui-util")
implementation project(':libnavigator')

// test
androidTestImplementation project(':libtesting-ui')
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.mapbox.navigation.instrumentation_tests.utils.events

import com.mapbox.bindgen.Value
import com.mapbox.common.EventsServerOptions
import com.mapbox.common.EventsService
import com.mapbox.common.EventsServiceError
import com.mapbox.common.EventsServiceObserver
import com.mapbox.navigation.utils.internal.logE
import com.mapbox.navigator.Navigator
import org.junit.rules.TestWatcher
import org.junit.runner.Description

class EventsAccumulatorRule(
mapboxToken: String,
) : TestWatcher() {

private val eventService = EventsService.getOrCreate(
EventsServerOptions(
mapboxToken, Navigator.getUserAgentFragment(), null
)
)

private val eventsServiceObserver = object : EventsServiceObserver {
override fun didEncounterError(error: EventsServiceError, events: Value) {
logE(LOG_CATEGORY) {
"Occurred error [$error] when send events: $events"
}
}

override fun didSendEvents(events: Value) {
_events.addAll(events.contents as List<Value>)
}
}

private val _events = mutableListOf<Value>()
val events: List<Value> get() = _events

private companion object {
private const val LOG_CATEGORY = "EventsAccumulatorRule"
}

override fun starting(description: Description?) {
_events.clear()
eventService.registerObserver(eventsServiceObserver)
}

override fun finished(description: Description?) {
eventService.unregisterObserver(eventsServiceObserver)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.mapbox.navigation.instrumentation_tests.utils.events.domain

internal abstract class EventBase(
driverMode: DriverMode,
) {
abstract val event: String
val eventVersion: Double = 2.4
val driverMode: String = when (driverMode) {
DriverMode.FreeDrive -> "freeDrive"
DriverMode.ActiveGuidance -> "trip"
}

enum class DriverMode {
FreeDrive,
ActiveGuidance,
}
}

internal abstract class EventBaseActiveGuidance(

) : EventBase(DriverMode.ActiveGuidance) {

}

internal class EventFeedback(
driverMode: DriverMode,
val feedbackType: String,
val description: String,
val feedbackSubType: Array<String>,
): EventBase(driverMode) {
override val event: String = "navigation.feedback"
}

internal class EventFreeDrive(
eventType: Type,
) : EventBase(DriverMode.FreeDrive) {
override val event: String = "navigation.freeDrive"
val eventType: String = when (eventType) {
Type.Start -> "start"
Type.Stop -> "stop"
}

enum class Type {
Start,
Stop,
}
}

internal class EventNavigationStateChanged(
state: State,
) : EventBase(DriverMode.ActiveGuidance) {
override val event: String = "navigation.navigationStateChanged"
val state: String = when (state) {
State.NavStarted -> "navigation_started"
State.NavEnded -> "navigation_ended"
}

enum class State {
NavStarted,
NavEnded,
}
}

internal class EventDepart : EventBaseActiveGuidance() {
override val event: String = "navigation.depart"
}

internal class EventArrive : EventBaseActiveGuidance() {
override val event: String = "navigation.arrive"
}

internal class EventCancel : EventBaseActiveGuidance() {
override val event: String = "navigation.cancel"
}

internal class EventAlternativeRoute: EventBaseActiveGuidance() {
override val event: String = "navigation.alternativeRoute"
}

internal class EventReroute: EventBaseActiveGuidance() {
override val event: String = "navigation.reroute"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.mapbox.navigation.instrumentation_tests.utils.events.verify

import com.google.gson.Gson
import com.mapbox.bindgen.Value
import com.mapbox.navigation.instrumentation_tests.utils.events.domain.EventBase
import org.json.JSONObject

internal fun EventBase.checkIfSubOf(value: Value): List<String> {
val valueJson = JSONObject(value.toJson())
val eventJson = JSONObject(Gson().toJson(this))

val nonValidValues = mutableListOf<String>()
val subKeys = eventJson.keys()
subKeys.forEach { eventJsonKey ->
val eventData = eventJson.get(eventJsonKey)
val valueData = valueJson.get(eventJsonKey)
if (eventData != valueData) {
nonValidValues.add(
"Event [$event], key [$eventJsonKey] does not match data in `event` [$eventData] and `value` [$valueData]"
)
}
}
return nonValidValues
}

internal fun Collection<EventBase>.verifyEvents(events: List<Value>): List<String> {
if (this.size != events.size) {
return listOf(
"""---
Events lists must have same sizes: expected List<EventBase> is ${this.size} actual List<Value> is ${events.size}
expected: ${this.joinToString { it.event }}
actual: ${events.joinToString { (it.contents as Map<String, Value>)["event"]?.contents as String }}
---"""
)
}

return this.foldIndexed(mutableListOf<String>()) { index, accumulateList, eventBase ->
accumulateList.apply {
addAll(eventBase.checkIfSubOf(events[index]))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mapbox.navigation.instrumentation_tests.utils.http

import com.mapbox.navigation.testing.ui.http.MockRequestHandler
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.RecordedRequest

class EventsRequestHandle : MockRequestHandler {
override fun handle(request: RecordedRequest): MockResponse? {
if (request.path!!.startsWith("/events/v2")) {
return MockResponse().setResponseCode(204)
}
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class MockLocationReplayerRule(mockLocationUpdatesRule: MockLocationUpdatesRule)
clearEvents()
}
}

fun playbackSpeed(scale: Double) {
mapboxReplayer?.playbackSpeed(scale)
}
}

fun Location.setUpLocation(event: ReplayEventUpdateLocation) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mapbox.navigation.instrumentation_tests.utils.location

import com.mapbox.geojson.Point
import com.mapbox.turf.TurfConstants
import com.mapbox.turf.TurfMeasurement

internal operator fun Int.compareTo(pointsDiff: Pair<Point, Point>): Int {
val (p1, p2) = pointsDiff
return this - TurfMeasurement.distance(p1, p2, TurfConstants.UNIT_METERS).toInt()
}
Loading