name: CI

on:
  push:
    branches:
      - main
  pull_request:

jobs:

  automated-tests:
    name: Automated tests
    strategy:
      matrix:
        api-level: [ 25, 30, 33 ]
      fail-fast: true
    runs-on: ubuntu-22.04
    steps:
      - name: checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - name: Set up JDK 11
        uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 11

      - name: Restore Cache
        uses: actions/cache@v3
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
          restore-keys: |
            ${{ runner.os }}-gradle-

      - name: Enable KVM
        run: |
          echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
          sudo udevadm control --reload-rules
          sudo udevadm trigger --name-match=kvm

      - name: AVD cache
        uses: actions/cache@v4
        id: avd-cache
        with:
          path: |
            ~/.android/avd/*
            ~/.android/adb*
          key: avd-${{ matrix.api-level }}

      - name: Create AVD and generate snapshot for caching
        if: steps.avd-cache.outputs.cache-hit != 'true'
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: ${{ matrix.api-level }}
          target: ${{ matrix.api-level != 33 && 'default' || 'aosp_atd' }}
          arch: x86_64
          profile: pixel_2
          ram-size: 4096M
          force-avd-creation: false
          sdcard-path-or-size: 2048M
          cores: 4
          disable-animations: false
          heap-size: 512M
          emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
          channel: canary
          script: echo "Generated AVD snapshot for caching."

      - name: create instrumentation coverage
        uses: reactivecircus/android-emulator-runner@v2
        env:
          GRADLE_OPTS: "-Dorg.gradle.internal.http.connectionTimeout=60000 -Dorg.gradle.internal.http.socketTimeout=60000 -Dorg.gradle.internal.network.retry.max.attempts=6 -Dorg.gradle.internal.network.retry.initial.backOff=2000"
        with:
          api-level: ${{ matrix.api-level }}
          target: ${{ matrix.api-level != 33 && 'default' || 'aosp_atd' }}
          arch: x86_64
          profile: pixel_2
          emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
          ram-size: 4096M
          cores: 4
          force-avd-creation: false
          sdcard-path-or-size: 2048M
          disable-animations: true
          heap-size: 512M
          channel: canary
          script: bash contrib/instrumentation.sh


      - name: Upload screenshot result
        uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: ${{ matrix.api-level }}
          path: screencap.png

      - name: create unit coverage
        if: ${{ matrix.api-level==25 }}
        run: ./gradlew testDebugUnitTest testCustomexampleDebugUnitTest

      - name: Upload coverage to Codecov
        if: ${{ matrix.api-level==25 }}
        uses: codecov/codecov-action@v3
        with:
          token: ${{ secrets.CODECOV_TOKEN }}

      - name: Upload Coverage to GH-Actions
        uses: actions/upload-artifact@v3
        if: ${{ matrix.api-level==25 }}
        with:
          name: Tests Coverage Report
          path: |
            **/reports/
