# stolen from:
# https://github.com/andstatus/todoagenda/blob/master/.travis.yml
# https://travis-ci.org/github/andstatus/todoagenda/jobs/662342170/config

sudo: true
language: bash
# ignored on non-linux platforms, but bionic is required for nested virtualization
dist: bionic

env:
  global:
    - NODE_VERSION=12
    - COMPILE_API=29
    - ANDROID_BUILD_TOOLS=29.0.2
    - ABI=x86_64
    - ADB_INSTALL_TIMEOUT=8
    - ANDROID_HOME=${HOME}/android-sdk
    - ANDROID_TOOLS_URL="https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip"
    - EMU_FLAVOR=default # use google_apis flavor if no default flavor emulator
    - GRAVIS="https://raw.githubusercontent.com/DanySK/Gravis-CI/master/"
    - JDK="1.8"
    - TOOLS=${ANDROID_HOME}/tools
    # PATH order is incredibly important. e.g. the 'emulator' script exists in more than one place!
    - PATH=${ANDROID_HOME}:${ANDROID_HOME}/emulator:${TOOLS}:${TOOLS}/bin:${ANDROID_HOME}/platform-tools:${PATH}
  matrix:
    - API=28

before_install:
  # Set up JDK 8 for Android SDK
  - curl "${GRAVIS}.install-jdk-travis.sh" --output ~/.install-jdk-travis.sh
  - export TARGET_JDK="${JDK}"
  - JDK="1.8"
  - source ~/.install-jdk-travis.sh

  # Set up Android SDK
  - wget -q "${ANDROID_TOOLS_URL}" -O android-sdk-tools.zip
  - unzip -q android-sdk-tools.zip -d ${ANDROID_HOME}
  - rm android-sdk-tools.zip
  - mkdir ~/.android # avoid harmless sdkmanager warning
  - echo 'count=0' > ~/.android/repositories.cfg # avoid harmless sdkmanager warning
  - yes | sdkmanager --licenses >/dev/null # accept all sdkmanager warnings
  - echo y | sdkmanager --no_https "platform-tools" >/dev/null
  - echo y | sdkmanager --no_https "tools" >/dev/null # A second time per Travis docs, gets latest versions
  - echo y | sdkmanager --no_https "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null # Implicit gradle dependency - gradle drives changes
  - echo y | sdkmanager --no_https "platforms;android-${COMPILE_API}" >/dev/null # We need the API of the current compileSdkVersion from gradle.properties

install:
  # Download required emulator tools
  - echo y | sdkmanager --no_https "platforms;android-$API" >/dev/null # We need the API of the emulator we will run
  - echo y | sdkmanager --no_https "emulator" >/dev/null
  - echo y | sdkmanager --no_https "system-images;android-$API;$EMU_FLAVOR;$ABI" >/dev/null # install our emulator

  # Set up KVM on linux for hardware acceleration. Manually here so it only happens for emulator tests, takes ~30s
  - sudo -E apt-get -yq --no-install-suggests --no-install-recommends install bridge-utils libpulse0 libvirt-bin libxcursor1 qemu-kvm virtinst ubuntu-vm-builder
  - sudo adduser $USER libvirt
  - sudo adduser $USER kvm

  # Create an Android emulator
  - echo no | avdmanager create avd --force -n Pixel_API_29_AOSP -k "system-images;android-$API;$EMU_FLAVOR;$ABI" -c 10M
  - printf "\nhw.lcd.height=1334\nhw.lcd.width=750\nhw.lcd.density=320\nskin.name=750x1334" >> /home/travis/.android/avd/Pixel_API_29_AOSP.avd/config.ini
  - |
    EMU_PARAMS="-verbose -no-snapshot -no-window -camera-back none -camera-front none -selinux permissive -qemu -m 2048"
    EMU_COMMAND="emulator"
    # This double "sudo" monstrosity is used to have Travis execute the
    # emulator with its new group permissions and help preserve the rule
    # of least privilege.
    sudo -E sudo -u $USER -E bash -c "${ANDROID_HOME}/emulator/${EMU_COMMAND} -avd Pixel_API_29_AOSP ${EMU_PARAMS} &"

  # Wait for emulator to be ready
  - chmod +x ./tests/e2e/android-wait-for-emulator.sh
  - ./tests/e2e/android-wait-for-emulator.sh
  - adb shell input keyevent 82 &

  # Switch back to our target JDK version to build and run tests
  - JDK="${TARGET_JDK}"
  - source ~/.install-jdk-travis.sh

  - curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
  - export NVM_DIR="$HOME/.nvm" && [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
  - nvm install $NODE_VERSION
  - nvm use $NODE_VERSION
  - nvm alias default $NODE_VERSION

script:
  - echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
  - npm i
  - npm i -g detox-cli
  - npm run e2e:release-build
  - npm run e2e:release-test || npm run e2e:release-test || npm run e2e:release-test

after_failure: ./tests/e2e/upload-artifacts.sh