|
1 | 1 | package com.segment.analytics.kotlin.destinations.flurry |
2 | 2 |
|
3 | | -import android.os.Bundle |
4 | | -import io.mockk.every |
5 | | -import io.mockk.mockk |
| 3 | +import android.app.Activity |
| 4 | +import android.app.Application |
| 5 | +import android.content.Context |
| 6 | +import android.util.Log |
| 7 | +import com.flurry.android.Constants |
| 8 | +import com.flurry.android.FlurryAgent |
| 9 | +import com.segment.analytics.kotlin.core.* |
| 10 | +import com.segment.analytics.kotlin.core.platform.Plugin |
| 11 | +import com.segment.analytics.kotlin.core.utilities.LenientJson |
| 12 | +import io.mockk.* |
| 13 | +import io.mockk.impl.annotations.MockK |
| 14 | +import kotlinx.serialization.decodeFromString |
| 15 | +import kotlinx.serialization.json.buildJsonObject |
| 16 | +import kotlinx.serialization.json.put |
| 17 | +import org.junit.Assert.assertEquals |
| 18 | +import org.junit.Before |
6 | 19 | import org.junit.Test |
| 20 | +import org.junit.jupiter.api.Assertions |
7 | 21 | import org.junit.runner.RunWith |
8 | 22 | import org.robolectric.RobolectricTestRunner |
9 | 23 | import org.robolectric.annotation.Config |
10 | 24 |
|
11 | 25 | @RunWith(RobolectricTestRunner::class) |
12 | 26 | @Config(manifest = Config.NONE) |
13 | 27 | class FlurryDestinationTests { |
| 28 | + @MockK(relaxUnitFun = true) |
| 29 | + lateinit var mockApplication: Application |
| 30 | + |
| 31 | + @MockK(relaxUnitFun = true) |
| 32 | + lateinit var mockedContext: Context |
| 33 | + |
| 34 | + @MockK(relaxUnitFun = true) |
| 35 | + lateinit var mockedAnalytics: Analytics |
| 36 | + |
| 37 | + @MockK(relaxUnitFun = true) |
| 38 | + lateinit var mockedFlurryAgentBuilder: FlurryAgent.Builder |
| 39 | + lateinit var mockedFlurryDestination: FlurryDestination |
| 40 | + |
| 41 | + init { |
| 42 | + MockKAnnotations.init(this) |
| 43 | + } |
| 44 | + |
| 45 | + @Before |
| 46 | + fun setUp() { |
| 47 | + mockkStatic(FlurryAgent::class) |
| 48 | + mockedFlurryDestination = FlurryDestination() |
| 49 | + mockedFlurryDestination.flurryAgentBuilder = mockedFlurryAgentBuilder |
| 50 | + mockedFlurryDestination.analytics = mockedAnalytics |
| 51 | + every { mockedAnalytics.configuration.application } returns mockApplication |
| 52 | + every { mockApplication.applicationContext } returns mockedContext |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun `settings are updated correctly`() { |
| 57 | + // An Flurry example settings |
| 58 | + val sampleFlurrySettings: Settings = LenientJson.decodeFromString( |
| 59 | + """ |
| 60 | + { |
| 61 | + "integrations": { |
| 62 | + "Flurry": { |
| 63 | + "apiKey": "APIKEY1234567890", |
| 64 | + "captureUncaughtExceptions": false, |
| 65 | + "reportLocation": true, |
| 66 | + "screenTracksEvents": true, |
| 67 | + "sessionContinueSeconds": 10, |
| 68 | + "useHttps": true |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + """.trimIndent() |
| 73 | + ) |
| 74 | + every { mockedFlurryAgentBuilder.withContinueSessionMillis(10000) } returns mockedFlurryAgentBuilder |
| 75 | + every { mockedFlurryAgentBuilder.withCaptureUncaughtExceptions(false) } returns mockedFlurryAgentBuilder |
| 76 | + every { mockedFlurryAgentBuilder.withLogEnabled(true) } returns mockedFlurryAgentBuilder |
| 77 | + every { mockedFlurryAgentBuilder.withLogLevel(Log.VERBOSE) } returns mockedFlurryAgentBuilder |
| 78 | + every { mockedFlurryAgentBuilder.withListener(any()) } returns mockedFlurryAgentBuilder |
| 79 | + |
| 80 | + val flurrySettings: Settings = sampleFlurrySettings |
| 81 | + mockedFlurryDestination.update(flurrySettings, Plugin.UpdateType.Initial) |
| 82 | + |
| 83 | +// assertions Flurry config |
| 84 | + Assertions.assertNotNull(mockedFlurryDestination.flurrySettings) |
| 85 | + with(mockedFlurryDestination.flurrySettings!!) { |
| 86 | + assertEquals(apiKey, "APIKEY1234567890") |
| 87 | + assertEquals(captureUncaughtExceptions, false) |
| 88 | + assertEquals(reportLocation, true) |
| 89 | + assertEquals(screenTracksEvents, true) |
| 90 | + assertEquals(sessionContinueSeconds, 10) |
| 91 | + assertEquals(useHttps, true) |
| 92 | + } |
| 93 | + |
| 94 | + verify { mockedFlurryAgentBuilder.withContinueSessionMillis(10000) } |
| 95 | + verify { mockedFlurryAgentBuilder.withCaptureUncaughtExceptions(false) } |
| 96 | + verify { mockedFlurryAgentBuilder.withLogEnabled(true) } |
| 97 | + verify { mockedFlurryAgentBuilder.withLogLevel(Log.VERBOSE) } |
| 98 | + verify { mockedFlurryAgentBuilder.withListener(any()) } |
| 99 | + verify { FlurryAgent.setReportLocation(true) } |
| 100 | + verify { FlurryAgent.onStartSession(mockApplication) } |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun `activity start handled correctly`() { |
| 105 | + val activity: Activity = mockkClass(Activity::class) |
| 106 | + mockedFlurryDestination.onActivityStarted(activity) |
| 107 | + verify { FlurryAgent.onStartSession(activity) } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun `activity start stopped correctly`() { |
| 112 | + val activity: Activity = mockkClass(Activity::class) |
| 113 | + mockedFlurryDestination.onActivityStopped(activity) |
| 114 | + verify { FlurryAgent.onEndSession(activity) } |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + fun `identify handled correctly`() { |
| 119 | + val identifyEvent = IdentifyEvent( |
| 120 | + userId = "User-Id-123", |
| 121 | + traits = emptyJsonObject |
| 122 | + ) |
| 123 | + mockedFlurryDestination.identify(identifyEvent) |
| 124 | + verify { FlurryAgent.setUserId("User-Id-123") } |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + fun `identify with traits handled correctly`() { |
| 129 | + val identifyEvent = IdentifyEvent( |
| 130 | + userId = "User-Id-123", |
| 131 | + traits = buildJsonObject { |
| 132 | + put("age", 10) |
| 133 | + put("gender", "f") |
| 134 | + } |
| 135 | + ) |
| 136 | + mockedFlurryDestination.identify(identifyEvent) |
| 137 | + verify { FlurryAgent.setUserId("User-Id-123") } |
| 138 | + verify { FlurryAgent.setAge(10) } |
| 139 | + verify { FlurryAgent.setGender(Constants.FEMALE) } |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + fun `screen handled correctly`() { |
| 144 | + val sampleEvent = ScreenEvent( |
| 145 | + name = "Screen 1", |
| 146 | + category = "Category 1", |
| 147 | + properties = emptyJsonObject |
| 148 | + ).apply { |
| 149 | + context = emptyJsonObject |
| 150 | + } |
| 151 | + mockedFlurryDestination.screen(sampleEvent) |
| 152 | + verify { FlurryAgent.logEvent("Screen 1", mapOf()) } |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + fun `screen with properties handled correctly`() { |
| 157 | + val sampleEvent = ScreenEvent( |
| 158 | + name = "Screen 2", |
| 159 | + category = "Category 2", |
| 160 | + properties = buildJsonObject { |
| 161 | + put("test1", "screen test1 Value") |
| 162 | + put("test2", "screen test2 Value") |
| 163 | + } |
| 164 | + ).apply { |
| 165 | + context = emptyJsonObject |
| 166 | + } |
| 167 | + mockedFlurryDestination.screen(sampleEvent) |
| 168 | + val expectedProperties: MutableMap<String, String> = HashMap() |
| 169 | + expectedProperties["test1"] = "screen test1 Value" |
| 170 | + expectedProperties["test2"] = "screen test2 Value" |
| 171 | + verify { FlurryAgent.logEvent("Screen 2", expectedProperties) } |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + fun `track handled correctly`() { |
| 176 | + val sampleEvent = TrackEvent( |
| 177 | + event = "Track 1", |
| 178 | + properties = emptyJsonObject |
| 179 | + ).apply { |
| 180 | + context = emptyJsonObject |
| 181 | + } |
| 182 | + mockedFlurryDestination.track(sampleEvent) |
| 183 | + verify { FlurryAgent.logEvent("Track 1", mapOf()) } |
| 184 | + } |
14 | 185 |
|
15 | 186 | @Test |
16 | | - fun `sample mock test`() { |
17 | | - // https://mockk.io/#dsl-examples |
18 | | - val bundle = mockk<Bundle>() |
19 | | - every { bundle.get("key") }.returns("value") |
| 187 | + fun `track with properties handled correctly`() { |
| 188 | + val sampleEvent = TrackEvent( |
| 189 | + event = "Track 1", |
| 190 | + properties = buildJsonObject { |
| 191 | + put("test1", "track test1 Value") |
| 192 | + put("test2", "track test2 Value") |
| 193 | + } |
| 194 | + ).apply { |
| 195 | + context = emptyJsonObject |
| 196 | + } |
| 197 | + mockedFlurryDestination.track(sampleEvent) |
| 198 | + val expectedProperties: MutableMap<String, String> = HashMap() |
| 199 | + expectedProperties["test1"] = "track test1 Value" |
| 200 | + expectedProperties["test2"] = "track test2 Value" |
| 201 | + verify { FlurryAgent.logEvent("Track 1", expectedProperties) } |
20 | 202 | } |
21 | 203 | } |
0 commit comments