name: Android tests

on:
  push:
    branches-ignore:
      - 'test-*'
  pull_request_target:
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:

  build:
    name: Test
    runs-on: macos-latest
    strategy:
      matrix:
        api-level: [29]

    steps:
    - name: Check out code
      uses: actions/checkout@v3

    - name: Set up Go 1.19
      uses: actions/setup-go@v4
      with:
        go-version: 1.19

    - uses: actions/setup-java@v3
      with:
        java-version: '11'
        distribution: 'temurin'

    - name: AVD cache
      uses: actions/cache@v3
      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 }}
        force-avd-creation: false
        emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
        disable-animations: false
        script: echo "Generated AVD snapshot for caching."

    - name: Run tests
      uses: reactivecircus/android-emulator-runner@v2
      with:
        working-directory: ./testrig/android
        api-level: ${{ matrix.api-level }}
        force-avd-creation: false
        emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
        disable-animations: true
        script: ./run.sh

