Skip to content

Commit

Permalink
Kotlin Unittest helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesa2 committed Jul 12, 2024
1 parent 0e45bba commit d8325ba
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 335 deletions.
24 changes: 0 additions & 24 deletions tracker/src/test/java/testhelpers/DefaultTestCase.java

This file was deleted.

22 changes: 22 additions & 0 deletions tracker/src/test/java/testhelpers/DefaultTestCase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package testhelpers

import androidx.test.core.app.ApplicationProvider
import org.junit.runner.RunWith
import org.matomo.sdk.Matomo
import org.matomo.sdk.Matomo.Companion.getInstance
import org.matomo.sdk.Tracker
import org.robolectric.annotation.Config

@Config(sdk = [19], manifest = Config.NONE)
@RunWith(FullEnvTestRunner::class)
abstract class DefaultTestCase : BaseTest() {
fun createTracker(): Tracker {
val app = ApplicationProvider.getApplicationContext<MatomoTestApplication>()
val tracker = app.onCreateTrackerConfig().build(getInstance(ApplicationProvider.getApplicationContext()))
tracker.preferences.edit().clear().apply()
return tracker
}

val matomo: Matomo?
get() = getInstance(ApplicationProvider.getApplicationContext())
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
* @link https://github.com/matomo-org/matomo-android-sdk
* @license https://github.com/matomo-org/matomo-sdk-android/blob/master/LICENSE BSD-3 Clause
*/
package testhelpers

package testhelpers;

import org.robolectric.DefaultTestLifecycle;
import org.robolectric.DefaultTestLifecycle

/**
* Tries to emulate a full app environment to satisfy more in-depth tests
*/
public class FullEnvTestLifeCycle extends DefaultTestLifecycle {
}
class FullEnvTestLifeCycle : DefaultTestLifecycle()
35 changes: 0 additions & 35 deletions tracker/src/test/java/testhelpers/FullEnvTestRunner.java

This file was deleted.

25 changes: 25 additions & 0 deletions tracker/src/test/java/testhelpers/FullEnvTestRunner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Android SDK for Matomo
*
* @link https://github.com/matomo-org/matomo-android-sdk
* @license https://github.com/matomo-org/matomo-sdk-android/blob/master/LICENSE BSD-3 Clause
*/
package testhelpers

import org.robolectric.RobolectricTestRunner
import org.robolectric.TestLifecycle
import org.robolectric.util.inject.Injector

/**
* Tries to emulate a full app environment to satisfy more in-depth tests
*/
@Suppress("unused")
open class FullEnvTestRunner : RobolectricTestRunner {
constructor(testClass: Class<*>?) : super(testClass)

protected constructor(testClass: Class<*>?, injector: Injector?) : super(testClass, injector)

override fun getTestLifecycleClass(): Class<out TestLifecycle<*>?> {
return FullEnvTestLifeCycle::class.java
}
}
38 changes: 0 additions & 38 deletions tracker/src/test/java/testhelpers/JUnitTree.java

This file was deleted.

26 changes: 26 additions & 0 deletions tracker/src/test/java/testhelpers/JUnitTree.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package testhelpers

import android.util.Log
import timber.log.Timber

class JUnitTree : Timber.DebugTree() {
private val minLogLevel = Log.VERBOSE

override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
if (priority < minLogLevel) return
println(System.currentTimeMillis().toString() + " " + priorityToString(priority) + "/" + tag + ": " + message)
}

companion object {
private fun priorityToString(priority: Int): String {
return when (priority) {
Log.ERROR -> "E"
Log.WARN -> "W"
Log.INFO -> "I"
Log.DEBUG -> "D"
Log.VERBOSE -> "V"
else -> priority.toString()
}
}
}
}
43 changes: 0 additions & 43 deletions tracker/src/test/java/testhelpers/MatomoTestApplication.java

This file was deleted.

33 changes: 33 additions & 0 deletions tracker/src/test/java/testhelpers/MatomoTestApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package testhelpers

import org.matomo.sdk.TrackerBuilder
import org.matomo.sdk.extra.MatomoApplication
import org.robolectric.TestLifecycleApplication
import org.robolectric.shadows.ShadowLog
import java.lang.reflect.Method


class MatomoTestApplication : MatomoApplication(), TestLifecycleApplication {
override fun onCreate() {
ShadowLog.stream = System.out
super.onCreate()
}

override fun beforeTest(method: Method) {
}

override fun prepareTest(test: Any) {
}

override fun afterTest(method: Method) {
}

override fun getPackageName(): String {
return "11"
}


override fun onCreateTrackerConfig(): TrackerBuilder {
return TrackerBuilder.createDefault("http://example.com", 1)
}
}
19 changes: 0 additions & 19 deletions tracker/src/test/java/testhelpers/QueryHashMap.java

This file was deleted.

10 changes: 10 additions & 0 deletions tracker/src/test/java/testhelpers/QueryHashMap.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package testhelpers

import org.matomo.sdk.QueryParams
import org.matomo.sdk.TrackMe

class QueryHashMap(trackMe: TrackMe) : HashMap<String?, String?>(trackMe.toMap()) {
fun get(key: QueryParams): String? {
return get(key.toString())
}
}
17 changes: 0 additions & 17 deletions tracker/src/test/java/testhelpers/TestActivity.java

This file was deleted.

17 changes: 17 additions & 0 deletions tracker/src/test/java/testhelpers/TestActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package testhelpers

import android.app.Activity
import android.os.Bundle

class TestActivity : Activity() {
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = testTitle
}

companion object {
@JvmStatic
val testTitle: String
get() = "Test Activity"
}
}
15 changes: 0 additions & 15 deletions tracker/src/test/java/testhelpers/TestHelper.java

This file was deleted.

14 changes: 14 additions & 0 deletions tracker/src/test/java/testhelpers/TestHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package testhelpers

import timber.log.Timber

object TestHelper {
@JvmStatic
fun sleep(millis: Long) {
try {
Thread.sleep(millis)
} catch (e: InterruptedException) {
Timber.e(e)
}
}
}
Loading

0 comments on commit d8325ba

Please sign in to comment.