-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix log session event when optOut is true (#117)
- Loading branch information
1 parent
096d10f
commit 155b3f5
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
android/src/test/java/com/amplitude/android/AmplitudeRobolectricTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.amplitude.android | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import com.amplitude.core.events.BaseEvent | ||
import com.amplitude.core.utilities.ConsoleLoggerProvider | ||
import io.mockk.mockk | ||
import io.mockk.spyk | ||
import io.mockk.verify | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class AmplitudeRobolectricTests { | ||
private lateinit var amplitude: Amplitude | ||
private var context: Context? = null | ||
|
||
@ExperimentalCoroutinesApi | ||
@Before | ||
fun setup() { | ||
context = mockk<Application>(relaxed = true) | ||
|
||
amplitude = Amplitude(createConfiguration()) | ||
} | ||
|
||
@Test | ||
fun `test optOut is true no event should be send`() { | ||
val mockedPluginTest = spyk(StubPlugin()) | ||
val amplitudeInstance = Amplitude(createConfiguration(100, true)) | ||
amplitudeInstance.add(mockedPluginTest) | ||
|
||
val event1 = BaseEvent() | ||
event1.eventType = "test event 1" | ||
event1.timestamp = 1000 | ||
amplitudeInstance.track(event1) | ||
|
||
amplitudeInstance.onEnterForeground(1500) | ||
|
||
val event2 = BaseEvent() | ||
event2.eventType = "test event 2" | ||
event2.timestamp = 1700 | ||
amplitudeInstance.track(event2) | ||
|
||
amplitudeInstance.onExitForeground() | ||
|
||
verify(exactly = 0) { mockedPluginTest.track(any()) } | ||
} | ||
|
||
private fun createConfiguration( | ||
minTimeBetweenSessionsMillis: Long? = null, | ||
optOut: Boolean? = null, | ||
): Configuration { | ||
|
||
return Configuration( | ||
apiKey = "api-key", | ||
context = context!!, | ||
instanceName = "testInstance", | ||
loggerProvider = ConsoleLoggerProvider(), | ||
optOut = optOut ?: false, | ||
minTimeBetweenSessionsMillis = minTimeBetweenSessionsMillis | ||
?: Configuration.MIN_TIME_BETWEEN_SESSIONS_MILLIS, | ||
) | ||
} | ||
} |