-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1692 from OneSignal/user-model/notification-unit-…
…tests [User Model] Notifications Unit Tests
- Loading branch information
Showing
114 changed files
with
1,857 additions
and
21,613 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
16 changes: 0 additions & 16 deletions
16
...lSDK/onesignal/notifications/src/test/java/com/onesignal/notifications/ExampleUnitTest.kt
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
...ations/src/test/java/com/onesignal/notifications/extensions/ContainedRobolectricRunner.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 @@ | ||
/** | ||
* Code taken from https://github.com/kotest/kotest-extensions-robolectric with no changes. | ||
* | ||
* LICENSE: https://github.com/kotest/kotest-extensions-robolectric/blob/master/LICENSE | ||
*/ | ||
package com.onesignal.notifications.extensions | ||
|
||
import org.junit.runners.model.FrameworkMethod | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.annotation.Config | ||
import org.robolectric.internal.bytecode.InstrumentationConfiguration | ||
import org.robolectric.pluginapi.config.ConfigurationStrategy | ||
import org.robolectric.plugins.ConfigConfigurer | ||
import java.lang.reflect.Method | ||
|
||
internal class ContainedRobolectricRunner( | ||
private val config: Config? | ||
) : RobolectricTestRunner(PlaceholderTest::class.java, injector) { | ||
private val placeHolderMethod: FrameworkMethod = children[0] | ||
val sdkEnvironment = getSandbox(placeHolderMethod).also { | ||
configureSandbox(it, placeHolderMethod) | ||
} | ||
private val bootStrapMethod = sdkEnvironment.bootstrappedClass<Any>(testClass.javaClass) | ||
.getMethod(PlaceholderTest::bootStrapMethod.name) | ||
|
||
fun containedBefore() { | ||
Thread.currentThread().contextClassLoader = sdkEnvironment.robolectricClassLoader | ||
super.beforeTest(sdkEnvironment, placeHolderMethod, bootStrapMethod) | ||
} | ||
|
||
fun containedAfter() { | ||
super.afterTest(placeHolderMethod, bootStrapMethod) | ||
super.finallyAfterTest(placeHolderMethod) | ||
Thread.currentThread().contextClassLoader = ContainedRobolectricRunner::class.java.classLoader | ||
} | ||
|
||
override fun createClassLoaderConfig(method: FrameworkMethod?): InstrumentationConfiguration { | ||
return InstrumentationConfiguration.Builder(super.createClassLoaderConfig(method)) | ||
.doNotAcquirePackage("io.kotest") | ||
.build() | ||
} | ||
|
||
override fun getConfig(method: Method?): Config { | ||
val defaultConfiguration = injector.getInstance(ConfigurationStrategy::class.java) | ||
.getConfig(testClass.javaClass, method) | ||
|
||
if (config != null) { | ||
val configConfigurer = injector.getInstance(ConfigConfigurer::class.java) | ||
return configConfigurer.merge(defaultConfiguration[Config::class.java], config) | ||
} | ||
|
||
return super.getConfig(method) | ||
} | ||
|
||
class PlaceholderTest { | ||
@org.junit.Test | ||
fun testPlaceholder() { | ||
} | ||
|
||
fun bootStrapMethod() { | ||
} | ||
} | ||
|
||
companion object { | ||
private val injector = defaultInjector().build() | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...otifications/src/test/java/com/onesignal/notifications/extensions/RobolectricExtension.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,96 @@ | ||
/** | ||
* Code taken from https://github.com/kotest/kotest-extensions-robolectric with a | ||
* fix in the intercept method. | ||
* | ||
* LICENSE: https://github.com/kotest/kotest-extensions-robolectric/blob/master/LICENSE | ||
*/ | ||
package com.onesignal.notifications.extensions | ||
|
||
import android.app.Application | ||
import io.kotest.core.extensions.ConstructorExtension | ||
import io.kotest.core.extensions.TestCaseExtension | ||
import io.kotest.core.spec.AutoScan | ||
import io.kotest.core.spec.Spec | ||
import io.kotest.core.test.TestCase | ||
import io.kotest.core.test.TestResult | ||
import org.robolectric.annotation.Config | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.full.findAnnotation | ||
|
||
/** | ||
* We override TestCaseExtension to configure the Robolectric environment because TestCase intercept | ||
* occurs on the same thread the test is run. This is unfortunate because it is run for every test, | ||
* rather than every spec. But the SpecExtension intercept is run on a different thread. | ||
*/ | ||
@AutoScan | ||
internal class RobolectricExtension : ConstructorExtension, TestCaseExtension { | ||
private fun Class<*>.getParentClass(): List<Class<*>> { | ||
if (superclass == null) return listOf() | ||
return listOf(superclass) + superclass.getParentClass() | ||
} | ||
|
||
private fun KClass<*>.getConfig(): Config { | ||
val configAnnotations = listOf(this.java).plus(this.java.getParentClass()) | ||
.mapNotNull { it.kotlin.findAnnotation<Config>() } | ||
.asSequence() | ||
|
||
val configAnnotation = configAnnotations.firstOrNull() | ||
|
||
if (configAnnotation != null) { | ||
return Config.Builder(configAnnotation).build() | ||
} | ||
|
||
val robolectricTestAnnotations = listOf(this.java).plus(this.java.getParentClass()) | ||
.mapNotNull { it.kotlin.findAnnotation<RobolectricTest>() } | ||
.asSequence() | ||
|
||
val application: KClass<out Application>? = robolectricTestAnnotations | ||
.firstOrNull { it.application != KotestDefaultApplication::class }?.application | ||
val sdk: Int? = robolectricTestAnnotations.firstOrNull { it.sdk != -1 }?.takeUnless { it.sdk == -1 }?.sdk | ||
|
||
return Config.Builder() | ||
.also { builder -> | ||
if (application != null) { | ||
builder.setApplication(application.java) | ||
} | ||
|
||
if (sdk != null) { | ||
builder.setSdk(sdk) | ||
} | ||
}.build() | ||
} | ||
|
||
override fun <T : Spec> instantiate(clazz: KClass<T>): Spec? { | ||
clazz.findAnnotation<RobolectricTest>() ?: return null | ||
|
||
return ContainedRobolectricRunner(clazz.getConfig()) | ||
.sdkEnvironment.bootstrappedClass<Spec>(clazz.java).newInstance() | ||
} | ||
|
||
override suspend fun intercept( | ||
testCase: TestCase, | ||
execute: suspend (TestCase) -> TestResult | ||
): TestResult { | ||
// FIXED: Updated code based on https://github.com/kotest/kotest/issues/2717 | ||
val hasRobolectricAnnotation = testCase.spec::class.annotations.any { annotation -> | ||
annotation.annotationClass.qualifiedName == RobolectricTest::class.qualifiedName | ||
} | ||
|
||
if (!hasRobolectricAnnotation) { | ||
return execute(testCase) | ||
} | ||
|
||
val containedRobolectricRunner = ContainedRobolectricRunner(testCase.spec::class.getConfig()) | ||
containedRobolectricRunner.containedBefore() | ||
val result = execute(testCase) | ||
containedRobolectricRunner.containedAfter() | ||
return result | ||
} | ||
} | ||
|
||
internal class KotestDefaultApplication : Application() | ||
|
||
annotation class RobolectricTest( | ||
val application: KClass<out Application> = KotestDefaultApplication::class, | ||
val sdk: Int = -1 | ||
) |
Oops, something went wrong.