/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion rootProject.buildToolsVersion

    kotlinOptions {
        freeCompilerArgs = ['-Xjvm-default=all']
    }

    defaultConfig {
        minSdkVersion rootProject.minSdkVersion
    }

    testOptions {
        unitTests.includeAndroidResources = true

        unitTests.all {
            // Because of native libraries loading (Yoga), we can never reuse a class loader and
            // need to fork a new process per class.
            forkEvery = 1
            maxParallelForks = 4

            jvmArgs '-Dcom.facebook.litho.is_oss=true', '-Dlitho.animation.disabled=true'
            testLogging {
                events 'skipped', 'failed', 'standardOut', 'standardError'
                exceptionFormat = 'full'
            }

            // This test requires some resources configuration with Gradle and Robolectric
            // that we haven't had time to figure out
            exclude '/com/facebook/litho/ApplyStylesTest.class'

            // This test has failed on gradle since we upgraded to robolectric 4.9
            exclude 'com/facebook/litho/MountStateTest.class'

            // When using Gradle sometimes resources needed for this test are not copied into the
            // proper folder (see block around line 120). So, skip them for Gradle as they still
            // will be run for BUCK.
            exclude '/com/facebook/litho/processor/integration/ProcessorIntegrationTest.class'

            /*
              T94395240
              The following tests fail when run using gradle. The javac tools used in the test
              environment skips some method parameters when API/Build Tools version >=v30. These
              tests will still run for BUCK. The test class needs to be excluded because the
              android plugin's UnitTest options doesn't allow excluding specific tests.
            */
            exclude 'com/facebook/litho/sections/specmodels/generator/GroupSectionSpecGeneratorTest.class'
        }
    }

    compileOptions {
        sourceCompatibility rootProject.sourceCompatibilityVersion
        targetCompatibility rootProject.targetCompatibilityVersion
    }


    sourceSets.test.java.srcDir project(':litho-processor').file("src/test/java")
    sourceSets.test.java.srcDir project(':litho-sections-processor').file("src/test/java")
    namespace 'com.facebook.litho.it'
    lint {
        abortOnError false
    }
}

dependencies {
    // For main targets
    implementation project(':litho-core')
    implementation project(':litho-widget')
    implementation project(':litho-sections-core')
    implementation project(':litho-sections-widget')
    implementation project(':litho-testing')
    implementation deps.inferAnnotations
    implementation deps.supportAppCompat
    implementation deps.supportRecyclerView
    implementation deps.supportSwipeRefresh
    implementation deps.kotlinStandardLib
    implementation files('../lib/tools.jar')
    implementation files('../lib/rt.jar')

    compileOnly project(':litho-sections-annotations')
    annotationProcessor project(':litho-processor')
    annotationProcessor project(':litho-sections-processor')
    kapt project(':litho-processor')
    kapt project(':litho-sections-processor')

    // Test project dependencies
    testCompileOnly project(':litho-sections-annotations')
    testImplementation project(':litho-rendercore-testing')
    testImplementation project(':litho-widget')
    testImplementation project(':litho-widget-kotlin')
    testImplementation project(':litho-processor')
    testImplementation project(':litho-sections-core')
    testImplementation project(':litho-sections-processor')
    testImplementation project(':litho-sections-widget')
    testImplementation project(':litho-perf-logger')
    testImplementation project(':litho-intellij-plugin')
    testImplementation project(':test-specs')
    testImplementation files('../lib/tools.jar')
    testImplementation files('../lib/rt.jar')

    // Testing deps
    testCompileOnly deps.jsr305
    testImplementation deps.assertjCore
    testImplementation deps.compileTesting
    testImplementation deps.junit
    testImplementation deps.mockitoCore
    testImplementation deps.mockitokotlin
    testImplementation deps.javapoet
    testImplementation deps.robolectric
    testImplementation deps.soloader
    testImplementation deps.supportAppCompat
    testImplementation deps.supportRecyclerView
    testImplementation deps.supportTestCore
    testImplementation deps.khronos
}

gradle.projectsEvaluated {
    // Base path which is recognized by android studio.
    def testClassesPath = "${buildDir}/intermediates/classes/test/"
    // Copy must be done for each variant.
    def variants = android.libraryVariants.collect()

    variants.each { variant ->
        def variationName = variant.name.capitalize()
        def variationPath = variant.buildType.name

        task "copyTest${variationName}Resources"(type: Copy) {
            from "${projectDir}/src/test/resources"
            into "${testClassesPath}/${variationPath}"
        }

        test.dependsOn "copyTest${variationName}Resources"
    }
}
