Browse Source

added instrumented tests

sirekanian 2 years ago
parent
commit
c66e91f1ec

+ 20 - 0
.github/workflows/tests.yml

@@ -0,0 +1,20 @@
+on:
+  push:
+  workflow_dispatch:
+jobs:
+  tests:
+    runs-on: macos-latest
+    steps:
+      - uses: actions/checkout@v3
+      - uses: actions/setup-java@v3
+        with:
+          distribution: 'temurin'
+          java-version: '11'
+      - shell: bash
+        run: ./emulator
+      - shell: bash
+        run: ./gradlew connectedAndroidTest
+      - if: ${{ always() }}
+        uses: actions/upload-artifact@v3
+        with:
+          path: app/build/reports/androidTests/connected/*.html

+ 3 - 0
README.md

@@ -2,6 +2,9 @@
 
 Searchable list of bribetakers and warmongers. More info on https://acf.international.
 
+[![Build Status](https://img.shields.io/github/actions/workflow/status/sirekanian/warmongr/build.yml?label=build)](https://github.com/sirekanian/warmongr/actions/workflows/build.yml)
+[![Tests Status](https://img.shields.io/github/actions/workflow/status/sirekanian/warmongr/tests.yml?label=tests)](https://github.com/sirekanian/warmongr/actions/workflows/tests.yml)
+
 <picture>
   <source media="(prefers-color-scheme: dark)" srcset="app/src/main/play/listings/en-US/graphics/phone-screenshots/1.png">
   <img src="app/src/main/play/listings/en-US/graphics/phone-screenshots/3.png">

+ 5 - 0
app/build.gradle

@@ -24,6 +24,7 @@ android {
                 arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
             }
         }
+        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
     }
     buildTypes {
         release {
@@ -77,4 +78,8 @@ dependencies {
     implementation "io.ktor:ktor-serialization-kotlinx-json:$ktorVersion"
      */
 
+    // tests
+    androidTestImplementation("androidx.test.ext:junit:1.1.5")
+    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
+    androidTestImplementation("androidx.test.uiautomator:uiautomator:2.2.0")
 }

+ 38 - 0
app/src/androidTest/java/com/sirekanian/warmongr/AndroidTest.kt

@@ -0,0 +1,38 @@
+package com.sirekanian.warmongr
+
+import android.content.ComponentName
+import android.content.Intent
+import androidx.test.core.app.ApplicationProvider
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.platform.app.InstrumentationRegistry
+import androidx.test.uiautomator.*
+import org.junit.Test
+import org.junit.runner.RunWith
+
+private const val PACKAGE = "com.sirekanian.warmongr"
+private const val ACTIVITY = "com.sirekanian.warmongr.MainActivity"
+
+@RunWith(AndroidJUnit4::class)
+class AndroidTest {
+
+    private val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
+    private val app = ApplicationProvider.getApplicationContext<App>()
+    private val intent =
+        Intent.makeMainActivity(ComponentName(PACKAGE, ACTIVITY))
+            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
+
+    @Test
+    fun putin() {
+        app.startActivity(intent)
+        wait(By.desc("Search…")).click()
+        wait(By.focused(true)).text = "путин"
+        wait(By.text("Putin Vladimir Vladimirovich"))
+    }
+
+    private fun wait(selector: BySelector): UiObject2 {
+        val condition = Until.hasObject(selector)
+        check(device.wait(condition, 60000)) { "Timeout reached" }
+        return device.findObject(selector)
+    }
+
+}

+ 68 - 0
emulator

@@ -0,0 +1,68 @@
+#!/usr/bin/env bash
+
+set -e
+set -o pipefail
+
+export ANDROID_HOME="tmp/AndroidSdk"
+export ANDROID_AVD_HOME="tmp/AndroidAvd"
+export ANDROID_EMULATOR_HOME="tmp/AndroidEmu"
+
+mkdir -p "$ANDROID_HOME"
+mkdir -p "$ANDROID_AVD_HOME"
+mkdir -p "$ANDROID_EMULATOR_HOME"
+
+case "$(uname -s)" in
+  Linux*) machine=linux ;;
+  *) machine=mac ;;
+esac
+
+download() {
+  output="tmp/$(basename "$2")"
+  if [ ! -f "$output" ]; then
+    wget "$2" -O "$output"
+  fi
+  eval $1="$output"
+}
+
+download cmdtools "https://dl.google.com/android/repository/commandlinetools-$machine-9477386_latest.zip"
+download firefox "https://github.com/mozilla-mobile/firefox-android/releases/download/fenix-v111.1.1/fenix-111.1.1-x86_64.apk"
+
+TOOLS="$ANDROID_HOME/cmdline-tools/latest"
+if [ ! -d "$TOOLS" ]; then
+  unzip -oq "$cmdtools"
+  mkdir -p "$ANDROID_HOME/cmdline-tools"
+  mv "cmdline-tools" "$TOOLS"
+fi
+
+SDKM="$TOOLS/bin/sdkmanager"
+AVDM="$TOOLS/bin/avdmanager"
+ADB="$ANDROID_HOME/platform-tools/adb"
+EMU="$ANDROID_HOME/emulator/emulator -no-audio -no-snapshot -gpu swiftshader_indirect -no-boot-anim"
+
+if [ "$1" != "window" ]; then
+  EMU="$EMU -no-window"
+fi
+
+if [ "$1" = "stop" ]; then
+  killall qemu-system-x86_64 qemu-system-x86_64-headless || true
+  $AVDM delete avd -n "my_emulator"
+  exit 0
+fi
+
+yes | $SDKM --licenses || true
+PACKAGE="system-images;android-31;default;x86_64"
+$SDKM "platform-tools" "platforms;android-31" "$PACKAGE"
+$AVDM create avd -f -n "my_emulator" -b "default/x86_64" -k "$PACKAGE" -d "pixel_3a"
+grep -q 'Boot completed' <($EMU -avd "my_emulator")
+echo "Boot completed!"
+
+while [ -z "$($ADB devices | grep emulator | grep device)" ]; do
+  $ADB devices
+  sleep 5
+done
+
+$ADB devices
+sleep 5
+
+$ADB install "$firefox"
+$ADB uninstall --user 0 "org.chromium.webview_shell"