import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent

android {
    defaultConfig {
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }

    testOptions {
        execution 'ANDROIDX_TEST_ORCHESTRATOR'
        animationsDisabled true

        unitTests {
            includeAndroidResources true
            returnDefaultValues true

            all {
                int maxTestForks = Integer.parseInt(System.getProperty('maxTestForks', project.path == ':app' ? '4' : '1'))
                maxParallelForks Math.max(1, (int) Math.min(maxTestForks, (int) (Runtime.getRuntime().availableProcessors() / 2.0)))

                forkEvery Integer.parseInt(System.getProperty('TEST_FORK_EVERY', '1000'))

                testLogging {
                    if (isCi) {
                        events TestLogEvent.FAILED, TestLogEvent.SKIPPED, TestLogEvent.STARTED, TestLogEvent.PASSED
                    } else {
                        events TestLogEvent.FAILED, TestLogEvent.SKIPPED
                    }

                    exceptionFormat TestExceptionFormat.FULL

                    showCauses true
                    showExceptions true
                    showStackTraces true
                }

                systemProperty 'TestsGroupingFilter_TEST_GROUP_TO_EXECUTE_SYSTEM_PROPERTY_KEY', System.getenv().getOrDefault('TEST_GROUP_INDEX', '0')
                systemProperty 'TestsGroupingFilter_TEST_GROUPS_COUNT_SYSTEM_PROPERTY_KEY', System.getenv().getOrDefault('TEST_GROUPS_COUNT', '1')

                systemProperty 'java.awt.headless', true
                systemProperty 'robolectric.logging', 'stdout'
                //disabling since there is just too much logging happening in the Robolectric layer.
                systemProperty 'robolectric.logging.enabled', true

                systemProperty 'robolectric.resourcesMode', "binary"

                systemProperty 'robolectric.dependency.repo.url', 'https://repo1.maven.org/maven2'

                if (isCi) {
                    // In CI, we want a much more stable test-runs.
                    // We will tell Roboletric where exactly it can find the android-all artifacts.
                    // These files are downloaded using the "./scripts/download_robolectric_jars_to_machine.sh"
                    // script (which is called as part of the CI scripts).
                    systemProperty 'robolectric.dependency.dir', "${rootDir.absolutePath}/.robolectric-android-all-jars/"
                }

                if (project.hasProperty('excludeTestClasses')) {
                    exclude project.property('excludeTestClasses')
                }

                // set heap size for the test JVM(s)
                // NOTE: This is needed since Gradle does not propagate the heap-size restrictions from gradle.properties file.
                minHeapSize = "128m"
                maxHeapSize = "4096m"
            }
        }
    }
}

apply from: "${rootDir}/gradle/jacoco.gradle"
