-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
402 additions
and
49 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
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
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
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
7 changes: 7 additions & 0 deletions
7
analytics/src/main/java/com/sofakingforever/analytics/EnabledMap.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,7 @@ | ||
package com.sofakingforever.analytics | ||
|
||
class EnabledMap<Key> : LinkedHashMap<Key, Boolean>() { | ||
|
||
fun isDisabled(something : Key) : Boolean = this[something] == false | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
analytics/src/main/java/com/sofakingforever/analytics/exceptions/EventNotTrackedException.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,10 @@ | ||
package com.sofakingforever.analytics.exceptions | ||
|
||
import com.sofakingforever.analytics.AnalyticsDispatcher | ||
import com.sofakingforever.analytics.events.base.Event | ||
|
||
class EventNotTrackedException(message: String?, cause: Throwable?) : RuntimeException(message, cause) { | ||
|
||
constructor(dispatcher: AnalyticsDispatcher, event: Event, t: Throwable) : this("${dispatcher.dispatcherName} dispatcher couldn't fire \"${event.javaClass.name}\" event", t) | ||
|
||
} |
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
103 changes: 103 additions & 0 deletions
103
analytics/src/test/java/com/sofakingforever/library/AnalyticsUnitTest.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,103 @@ | ||
package com.sofakingforever.library | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import com.sofakingforever.analytics.Analytics | ||
import com.sofakingforever.analytics.AnalyticsSettings | ||
import com.sofakingforever.analytics.exceptions.EventNotTrackedException | ||
import com.sofakingforever.analytics.exceptions.UnsupportedEventException | ||
import com.sofakingforever.library.dispatcher.TestKit | ||
import com.sofakingforever.library.dispatcher.TestableDispatcher | ||
import com.sofakingforever.library.events.InitDispatcherEvent | ||
import com.sofakingforever.library.events.TestContentViewEvent | ||
import com.sofakingforever.library.events.TestCustomEvent | ||
import com.sofakingforever.library.events.UnsupportedEvent | ||
import org.junit.Test | ||
import org.mockito.Mockito | ||
import org.mockito.Mockito.mock | ||
|
||
class AnalyticsUnitTest { | ||
|
||
private val contextMock = mock(Context::class.java) | ||
|
||
private lateinit var analytics: Analytics | ||
|
||
private val dispatcher = TestableDispatcher() | ||
private var raisedException: Exception? = null | ||
|
||
init { | ||
Mockito.`when`(contextMock.applicationContext).thenReturn(contextMock) | ||
} | ||
|
||
@Test | ||
fun testAnalytics() { | ||
|
||
analytics = Analytics(contextMock, dispatcher).apply { | ||
|
||
this.settings.exceptionHandler = object : AnalyticsSettings.ExceptionHandler { | ||
override fun onException(e: Exception) { | ||
raisedException = e | ||
} | ||
|
||
} | ||
} | ||
|
||
// track some events | ||
trackTestEvents() | ||
|
||
// assert events | ||
assertEvents() | ||
|
||
// assert no exceptions were raised | ||
assert(raisedException == null) | ||
|
||
// track an unsupported event | ||
analytics.track(UnsupportedEvent()) | ||
|
||
// assert UnsupportedEventException was raised | ||
assert(raisedException != null) | ||
assert(raisedException is EventNotTrackedException) | ||
assert((raisedException as EventNotTrackedException).cause is UnsupportedEventException) | ||
|
||
} | ||
|
||
|
||
private fun trackTestEvents() { | ||
|
||
analytics.track(TestCustomEvent(1)) | ||
|
||
analytics.setDispatcherEnabled(TestableDispatcher.DispatcherName, false) | ||
|
||
analytics.track(TestCustomEvent(2)) | ||
|
||
analytics.setDispatcherEnabled(TestableDispatcher.DispatcherName, true) | ||
|
||
analytics.track(TestCustomEvent(3)) | ||
|
||
analytics.setKitEnabled(TestKit.instance, false) | ||
|
||
analytics.track(TestCustomEvent(4)) | ||
|
||
analytics.setKitEnabled(TestKit.instance, true) | ||
|
||
analytics.track(TestCustomEvent(5)) | ||
|
||
analytics.track(TestContentViewEvent(1)) | ||
|
||
} | ||
|
||
|
||
private fun assertEvents() { | ||
|
||
|
||
val eventList = dispatcher.eventList | ||
|
||
// expect to find 3 custom events , 1 contentview and 1 init event | ||
assert(eventList.size == 5) | ||
assert(eventList[0] is InitDispatcherEvent) | ||
assert((eventList[1] as TestCustomEvent).number == 1) | ||
assert((eventList[2] as TestCustomEvent).number == 3) | ||
assert((eventList[3] as TestCustomEvent).number == 5) | ||
assert((eventList[4] as TestContentViewEvent).number == 1) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
analytics/src/test/java/com/sofakingforever/library/DispatcherTest.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,41 @@ | ||
package com.sofakingforever.library | ||
|
||
import android.app.Application | ||
import com.sofakingforever.library.dispatcher.TestableDispatcher | ||
import com.sofakingforever.library.events.InitDispatcherEvent | ||
import com.sofakingforever.library.events.TestCustomEvent | ||
import org.junit.Test | ||
|
||
|
||
class DispatcherTest { | ||
|
||
private lateinit var raisedException: Exception | ||
|
||
@Test | ||
fun testDispatcher() { | ||
|
||
val dispatcher = TestableDispatcher() | ||
|
||
dispatcher.initDispatcher(Application()) | ||
|
||
dispatcher.track(TestCustomEvent(1)) | ||
|
||
dispatcher.track(TestCustomEvent(2)) | ||
|
||
dispatcher.track(TestCustomEvent(3)) | ||
|
||
dispatcher.track(TestCustomEvent(4)) | ||
|
||
dispatcher.track(TestCustomEvent(5)) | ||
|
||
val eventList = dispatcher.eventList | ||
|
||
// expect to find 5 events + init event | ||
assert(eventList.size == 6) | ||
assert(eventList[0] is InitDispatcherEvent) | ||
assert((eventList[1] as TestCustomEvent).number == 1) | ||
|
||
// that's enough | ||
|
||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
analytics/src/test/java/com/sofakingforever/library/ExampleUnitTest.java
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
analytics/src/test/java/com/sofakingforever/library/dispatcher/TestKit.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,17 @@ | ||
package com.sofakingforever.library.dispatcher | ||
|
||
import com.sofakingforever.analytics.AnalyticsKit | ||
|
||
class TestKit private constructor() : AnalyticsKit { | ||
override val name: String = "Test Kit" | ||
|
||
private object Holder { | ||
val INSTANCE = TestKit() | ||
} | ||
|
||
companion object { | ||
val instance: TestKit by lazy { Holder.INSTANCE } | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.