name: Coverage Reporting

on:
  push:
    branches:
      - master
      - develop

  pull_request:
    branches:
      - '**'

jobs:

  coverageReport:
    strategy:
      matrix:
        api-level: [21,30]
        target: [ google_apis ]
      fail-fast: false
    runs-on: macos-11
    steps:
      - name: checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 1

      - name: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11

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

      - name: create instrumentation coverage
        uses: ReactiveCircus/android-emulator-runner@v2.23.0
        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 }}
          arch: x86_64
          profile: pixel_2
          ndk: 21.4.7075529
          sdcard-path-or-size: '1000M'
          disable-animations: false
          script: bash contrib/instrumentation.sh

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

      - name: create unit coverage
        if: ${{ matrix.api-level==21  }}
        run: ./gradlew jacocoTestDebugUnitTestReport jacocoTestCustomExampleDebugUnitTestReport

      - name: Upload coverage to Codecov
        if: ${{ matrix.api-level==21  }}
        uses: codecov/codecov-action@v2

      - name: Upload Coverage to GH-Actions
        uses: actions/upload-artifact@v2.2.0
        if: ${{ always()  }}
        with:
          name: Tests Coverage Report
          path: |
            **/reports/
