Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions .github/workflows/emulator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Repository workflow (not synced from android-config): end-to-end tests on an Android emulator against a throw-away
# Nextcloud (scripts/test-server.sh). GitHub-hosted runners have KVM; the emulator reaches the host at 10.0.2.2.
name: Emulator tests

on:
pull_request:
branches: [main]
schedule:
- cron: '17 3 * * *'
workflow_dispatch:

permissions:
contents: read

concurrency:
group: emulator-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
emulator:
# Not ubuntu-latest: in the Nextcloud organisation that label is mostly served by self-hosted runners without KVM.
runs-on: ubuntu-24.04
timeout-minutes: 75
env:
API_LEVEL: 35
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- name: Set up JDK 21
uses: actions/setup-java@d1374f8ac74474200d16ef8ecee22c7ae7a51880 # v6.3.0
with:
distribution: temurin
java-version: 21

- name: Setup Gradle
uses: gradle/actions/setup-gradle@8da46b539cd8fe6ab27ca75128919a18d757604c # v6.4.1

- name: Use Google's Maven Central mirror
# Maven Central rate-limits GitHub-hosted runners (HTTP 429). The mirror serves the same files, so dependency
# verification still applies.
run: |
mkdir -p ~/.gradle/init.d
cat > ~/.gradle/init.d/maven-central-mirror.init.gradle.kts <<'KTS'
val mirror = "https://maven-central.storage-download.googleapis.com/maven2/"
beforeSettings {
// Builds without own plugin repositories (build-logic) would otherwise lose the plugin portal.
pluginManagement.repositories.maven(mirror)
pluginManagement.repositories.gradlePluginPortal()
dependencyResolutionManagement.repositories.maven(mirror)
}
KTS

- name: Enable KVM
run: |
if [ ! -e /dev/kvm ]; then
echo "::error::This runner ($RUNNER_NAME) has no /dev/kvm; the emulator needs a GitHub-hosted runner."
exit 1
fi
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: Start test server (Nextcloud 33, self-signed HTTPS proxy)
env:
TLS_HOST: 10.0.2.2
run: scripts/test-server.sh start

- name: Build app and tests
run: ./gradlew --console=plain assembleQaDebug assembleQaDebugAndroidTest

- name: AVD cache
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
id: avd-cache
with:
path: |
~/.android/avd/*
~/.android/adb*
key: avd-${{ env.API_LEVEL }}-google_apis-x86_64

- name: Create AVD snapshot
if: steps.avd-cache.outputs.cache-hit != 'true'
uses: ReactiveCircus/android-emulator-runner@c9c93e6ba9c4194322e114c40970d5983d9a07cc # v2.38.0
with:
api-level: ${{ env.API_LEVEL }}
target: google_apis
arch: x86_64
force-avd-creation: false
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
disable-animations: true
script: echo "AVD snapshot created"

- name: Run end-to-end tests
uses: ReactiveCircus/android-emulator-runner@c9c93e6ba9c4194322e114c40970d5983d9a07cc # v2.38.0
with:
api-level: ${{ env.API_LEVEL }}
target: google_apis
arch: x86_64
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: >-
./gradlew --console=plain connectedQaDebugAndroidTest
-Pandroid.testInstrumentationRunnerArguments.testServer=http://10.0.2.2:8089
-Pandroid.testInstrumentationRunnerArguments.testServerTls=https://10.0.2.2:8443

- name: Upload test reports
if: failure()
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
with:
name: emulator-test-reports
path: |
app/build/reports/androidTests/
app/build/outputs/androidTest-results/
6 changes: 6 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ photos.localAndroidCommon=../android-common
`./gradlew :core:network:testDebugUnitTest -Pphotos.testServer=http://127.0.0.1:8089 -Pphotos.testServerTls=https://127.0.0.1:8443`
runs `ServerIntegrationTest` and `CertificateTrustIntegrationTest` (skipped without the properties).

**Emulator tests:** `app/src/androidTest` holds end-to-end tests that run on an Android emulator against the same
Docker server, in `.github/workflows/emulator.yml` (GitHub-hosted runner with KVM; the emulator reaches the host at
`10.0.2.2`). Local development machines without KVM rely on that workflow. Locally with a device or emulator:
`TLS_HOST=10.0.2.2 scripts/test-server.sh` and
`./gradlew connectedQaDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.testServer=http://10.0.2.2:8089 -Pandroid.testInstrumentationRunnerArguments.testServerTls=https://10.0.2.2:8443`.

**Dependency verification** is on (`gradle/verification-metadata.xml`). After adding or updating a dependency, run
`./gradlew --write-verification-metadata sha256,pgp --export-keys <the build and test tasks>` and commit the changed
metadata and keyring.
Expand Down
16 changes: 16 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ android {
applicationId = "com.nextcloud.android.photos"
versionCode = 1
versionName = "0.1.0"
// Every instrumented test starts with a fresh app (no accounts), see androidTest/README
testInstrumentationRunnerArguments["clearPackageData"] = "true"
}

testOptions {
execution = "ANDROIDX_TEST_ORCHESTRATOR"
}

flavorDimensions += "version"
Expand Down Expand Up @@ -68,4 +74,14 @@ dependencies {
implementation(libs.kotlinx.serialization.json)

testImplementation(projects.core.testing)

// End-to-end tests on an emulator against scripts/test-server.sh (.github/workflows/emulator.yml)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
androidTestImplementation(libs.androidx.test.runner)
androidTestImplementation(libs.androidx.test.rules)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.okhttp)
androidTestUtil(libs.androidx.test.orchestrator)
androidTestUtil(libs.androidx.test.services)
}
109 changes: 109 additions & 0 deletions app/src/androidTest/kotlin/com/nextcloud/photos/e2e/SignInTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Nextcloud Photos - Android Client
*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nextcloud.photos.e2e

import android.content.Intent
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.hasAnyAncestor
import androidx.compose.ui.test.hasContentDescription
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.isDialog
import androidx.compose.ui.test.junit4.createEmptyComposeRule
import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.performClick
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.nextcloud.photos.MainActivity
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* Sign-in end to end on an emulator against a real Nextcloud (SPEC §5.1, §14): login links with confirmation,
* self-signed certificates, several accounts. The Login Flow v2 browser hand-off and SSO need a real browser or Files
* app and stay on the manual test plan.
*/
@OptIn(ExperimentalTestApi::class)
@RunWith(AndroidJUnit4::class)
class SignInTest {
@get:Rule
val compose = createEmptyComposeRule()

private fun open(link: android.net.Uri): ActivityScenario<MainActivity> = ActivityScenario.launch(
Intent(
Intent.ACTION_VIEW,
link,
).setClass(ApplicationProvider.getApplicationContext(), MainActivity::class.java),
)

private fun inDialog(text: String): SemanticsMatcher = hasText(text) and hasAnyAncestor(isDialog())

private fun confirmLogin() {
compose.waitUntilAtLeastOneExists(hasText("Log in?"), TIMEOUT)
compose.onNode(inDialog("Log in")).performClick()
}

private fun waitForAccount(server: String) =
compose.waitUntilAtLeastOneExists(hasText(TestServer.label(server), substring = true), TIMEOUT)

@Test
fun loginLinkAsksFirstThenSignsIn() {
val server = TestServer.url
open(TestServer.loginLink(server)).use {
confirmLogin()
waitForAccount(server)
}
}

@Test
fun selfSignedServerAsksToTrustTheCertificate() {
val server = TestServer.tlsUrl
open(TestServer.loginLink(server)).use {
confirmLogin()
compose.waitUntilAtLeastOneExists(hasText("Trust this certificate?"), TIMEOUT)
compose.waitUntilAtLeastOneExists(hasText("SHA-256", substring = true), TIMEOUT)
compose.onNode(inDialog("Trust")).performClick()
waitForAccount(server)
}
}

@Test
fun secondAccountThenRemoveTheFirst() {
val first = TestServer.url
val second = TestServer.tlsUrl
open(TestServer.loginLink(first)).use { scenario ->
confirmLogin()
waitForAccount(first)
// A second link while the app runs adds a second account (onNewIntent). The app password is created here,
// off the main thread that onActivity runs on.
val secondLink = TestServer.loginLink(second)
scenario.onActivity {
it.startActivity(Intent(Intent.ACTION_VIEW, secondLink).setClass(it, MainActivity::class.java))
}
confirmLogin()
compose.waitUntilAtLeastOneExists(hasText("Trust this certificate?"), TIMEOUT)
compose.onNode(inDialog("Trust")).performClick()
waitForAccount(second)

compose.onNode(hasContentDescription("Settings")).performClick()
compose.waitUntilAtLeastOneExists(hasText("2 accounts"), TIMEOUT)
compose.onNode(hasText("2 accounts")).performClick()
compose.waitUntilAtLeastOneExists(hasText(TestServer.label(first), substring = true), TIMEOUT)
// Accounts are listed oldest first: remove the first one.
compose.onAllNodesWithText("Remove")[0].performClick()
compose.onNode(inDialog("Remove")).performClick()
compose.waitUntilDoesNotExist(hasText(TestServer.label(first), substring = true), TIMEOUT)
compose.waitUntilAtLeastOneExists(hasText(TestServer.label(second), substring = true), TIMEOUT)
}
}

private companion object {
const val TIMEOUT = 30_000L
}
}
56 changes: 56 additions & 0 deletions app/src/androidTest/kotlin/com/nextcloud/photos/e2e/TestServer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Nextcloud Photos - Android Client
*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nextcloud.photos.e2e

import android.net.Uri
import androidx.test.platform.app.InstrumentationRegistry
import okhttp3.Credentials
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONObject
import org.junit.Assume.assumeTrue

/**
* The Nextcloud of scripts/test-server.sh, as the emulator sees it (10.0.2.2 is the host). Given as instrumentation
* arguments testServer and testServerTls; tests are skipped without them.
*/
object TestServer {
private val arguments get() = InstrumentationRegistry.getArguments()

val url: String get() = arguments.getString("testServer").orEmpty().also {
assumeTrue("No testServer", it.isNotEmpty())
}

val tlsUrl: String get() = arguments.getString("testServerTls").orEmpty().also {
assumeTrue("No testServerTls", it.isNotEmpty())
}

const val USER = "admin"
private const val PASSWORD = "admin"

/** A new app password for [USER], as the QR code in the web settings would carry. */
fun createAppPassword(): String {
val request = Request.Builder()
.url("$url/ocs/v2.php/core/getapppassword?format=json")
.header("Authorization", Credentials.basic(USER, PASSWORD))
.header("OCS-APIRequest", "true")
.build()
OkHttpClient().newCall(request).execute().use { response ->
check(response.isSuccessful) { "getapppassword: HTTP ${response.code}" }
return JSONObject(
response.body.string(),
).getJSONObject("ocs").getJSONObject("data").getString("apppassword")
}
}

/** nc://login link with the app password (SPEC §5.1), the same content as the web's QR code. */
fun loginLink(server: String, password: String = createAppPassword()): Uri =
Uri.parse("nc://login/server:$server&user:$USER&password:$password")

/** "admin@10.0.2.2:8089", as the app labels the account. */
fun label(server: String) = "$USER@" + server.substringAfter("://")
}
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ zxing = "3.5.4"
# Tests
androidxTest = "1.7.0"
androidxTestJunit = "1.3.0"
androidxTestOrchestrator = "1.6.1"
androidxTestServices = "1.6.0"
junit = "4.13.2"
mockk = "1.14.12"
robolectric = "4.18.1"
Expand Down Expand Up @@ -123,6 +125,10 @@ aboutlibraries-compose-m3 = { module = "com.mikepenz:aboutlibraries-compose-m3",

# Tests
androidx-test-core = { module = "androidx.test:core", version.ref = "androidxTest" }
androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidxTest" }
androidx-test-rules = { module = "androidx.test:rules", version.ref = "androidxTest" }
androidx-test-orchestrator = { module = "androidx.test:orchestrator", version.ref = "androidxTestOrchestrator" }
androidx-test-services = { module = "androidx.test.services:test-services", version.ref = "androidxTestServices" }
androidx-test-ext-junit = { module = "androidx.test.ext:junit", version.ref = "androidxTestJunit" }
junit = { module = "junit:junit", version.ref = "junit" }
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
Expand Down
Loading
Loading