Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercy811 committed Dec 28, 2023
1 parent c677976 commit e4440b9
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
2 changes: 1 addition & 1 deletion android/src/main/java/com/amplitude/android/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ open class Amplitude(
add(AndroidLifecyclePlugin())
add(AnalyticsConnectorIdentityPlugin())
add(AnalyticsConnectorPlugin())
add(AmplitudeDestination(AndroidNetworkConnectivityChecker(this.configuration.context)))
add(AmplitudeDestination(AndroidNetworkConnectivityChecker(this.configuration.context, this.logger)))

(timeline as Timeline).start()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import com.amplitude.common.Logger
import com.amplitude.core.platform.NetworkConnectivityChecker

class AndroidNetworkConnectivityChecker(private val context: Context) : NetworkConnectivityChecker {

class AndroidNetworkConnectivityChecker(private val context: Context, private val logger: Logger) : NetworkConnectivityChecker {
companion object {
private const val ACCESS_NETWORK_STATE = "android.permission.ACCESS_NETWORK_STATE"
}
Expand All @@ -19,22 +19,28 @@ class AndroidNetworkConnectivityChecker(private val context: Context) : NetworkC
// Events will be treated like online
// regardless network connectivity
if (!hasPermission(context, ACCESS_NETWORK_STATE)) {
logger.warn("No ACCESS_NETWORK_STATE permission, offline mode is not supported.")
return true
}

val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val network = cm.activeNetwork ?: return false
val capabilities = cm.getNetworkCapabilities(network) ?: return false
return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)

return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
} else {
@SuppressLint("MissingPermission")
val networkInfo = cm.activeNetworkInfo
return networkInfo != null && networkInfo.isConnectedOrConnecting
}
}

private fun hasPermission(context: Context, permission: String): Boolean {
private fun hasPermission(
context: Context,
permission: String,
): Boolean {
return context.checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.amplitude.core.platform

import com.amplitude.core.Amplitude
import com.amplitude.core.Configuration
import com.amplitude.core.events.BaseEvent
import io.mockk.coEvery
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

@ExperimentalCoroutinesApi
class EventPipelineTest {
private lateinit var amplitude: Amplitude
private lateinit var networkConnectivityChecker: NetworkConnectivityChecker
private val config = Configuration(apiKey = "API_KEY", flushIntervalMillis = 5)

@BeforeEach
fun setup() {
amplitude = Amplitude(config)
networkConnectivityChecker = mockk<NetworkConnectivityChecker>(relaxed = true)
}

@Test
fun `should not flush when put and offline`() =
runBlocking {
coEvery { networkConnectivityChecker.isConnected() } returns false
val eventPipeline = spyk(EventPipeline(amplitude, networkConnectivityChecker))
val event = BaseEvent().apply { eventType = "test_event" }

eventPipeline.start()
eventPipeline.put(event)
delay(6)

verify(exactly = 0) { eventPipeline.flush() }
}

@Test
fun `should flush when put and online`() =
runBlocking {
coEvery { networkConnectivityChecker.isConnected() } returns true
val eventPipeline = spyk(EventPipeline(amplitude, networkConnectivityChecker))
val event = BaseEvent().apply { eventType = "test_event" }

eventPipeline.start()
eventPipeline.put(event)
delay(6)

verify(exactly = 1) { eventPipeline.flush() }
}

@Test
fun `should flush when back to online and an event is tracked`() =
runBlocking {
coEvery { networkConnectivityChecker.isConnected() } returns false
val eventPipeline = spyk(EventPipeline(amplitude, networkConnectivityChecker))
val event1 = BaseEvent().apply { eventType = "test_event1" }
val event2 = BaseEvent().apply { eventType = "test_event2" }

eventPipeline.start()
eventPipeline.put(event1)
delay(6)

coEvery { networkConnectivityChecker.isConnected() } returns true
eventPipeline.put(event2)
delay(6)

verify(exactly = 1) { eventPipeline.flush() }
}
}

0 comments on commit e4440b9

Please sign in to comment.