Skip to content

Commit c5c3f2e

Browse files
committed
Align Android coverage with DeviceRunner instrumentation
1 parent 9eb9d07 commit c5c3f2e

File tree

3 files changed

+45
-44
lines changed

3 files changed

+45
-44
lines changed

scripts/android/lib/PatchGradleFiles.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ private static Result ensureJacocoConfiguration(String content) {
274274
tasks.register("jacocoAndroidReport", JacocoReport) {
275275
group = "verification"
276276
description = "Generates Jacoco coverage report for the debug variant"
277-
dependsOn("connectedDebugAndroidTest")
278277
outputs.upToDateWhen { false }
279278
280279
reports {
@@ -323,10 +322,6 @@ private static Result ensureJacocoConfiguration(String content) {
323322
logger.lifecycle("Jacoco coverage inputs: ${existing}")
324323
}
325324
}
326-
327-
tasks.matching { it.name == "connectedDebugAndroidTest" }.configureEach {
328-
outputs.upToDateWhen { false }
329-
}
330325
""".stripTrailing();
331326

332327
return new Result(ensureTrailingNewline(content) + "\n" + jacocoBlock + "\n", true);

scripts/device-runner-app/androidTest/DeviceRunnerInstrumentationTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88

99
import org.junit.Test;
1010
import org.junit.runner.RunWith;
11+
12+
import java.io.BufferedReader;
13+
import java.io.InputStreamReader;
14+
1115
import static org.junit.Assert.assertNotNull;
16+
import static org.junit.Assert.assertTrue;
1217

1318
@RunWith(AndroidJUnit4.class)
1419
public class DeviceRunnerInstrumentationTest {
@@ -20,6 +25,33 @@ public void launchMainActivityAndWaitForDeviceRunner() throws Exception {
2025
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2126
context.startActivity(intent);
2227

23-
Thread.sleep(8000);
28+
boolean finished = waitForDeviceRunner();
29+
assertTrue("DeviceRunner did not emit completion marker within timeout", finished);
30+
}
31+
32+
private boolean waitForDeviceRunner() throws Exception {
33+
final long timeoutMs = 300_000L;
34+
final String endMarker = "CN1SS:SUITE:FINISHED";
35+
36+
Process logcat = new ProcessBuilder("logcat", "-v", "brief")
37+
.redirectErrorStream(true)
38+
.start();
39+
40+
long deadline = System.currentTimeMillis() + timeoutMs;
41+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(logcat.getInputStream()))) {
42+
String line;
43+
while (System.currentTimeMillis() < deadline) {
44+
if (reader.ready() && (line = reader.readLine()) != null) {
45+
if (line.contains(endMarker)) {
46+
return true;
47+
}
48+
} else {
49+
Thread.sleep(200);
50+
}
51+
}
52+
} finally {
53+
logcat.destroyForcibly();
54+
}
55+
return false;
2456
}
2557
}

scripts/run-android-instrumentation-tests.sh

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,6 @@ cn1ss_setup "$JAVA17_BIN" "$CN1SS_HELPER_SOURCE_DIR"
7070
[ -x "$GRADLE_PROJECT_DIR/gradlew" ] || chmod +x "$GRADLE_PROJECT_DIR/gradlew"
7171

7272
# ---- Prepare app + emulator state -----------------------------------------
73-
74-
APK_PATH="${2:-}"
75-
if [ -z "$APK_PATH" ]; then
76-
APK_PATH="$(find "$GRADLE_PROJECT_DIR" -type f -path '*/outputs/apk/debug/*.apk' | head -n 1 || true)"
77-
fi
78-
if [ -z "$APK_PATH" ] || [ ! -f "$APK_PATH" ]; then
79-
ra_log "FATAL: Unable to locate debug APK under $GRADLE_PROJECT_DIR" >&2
80-
exit 10
81-
fi
82-
ra_log "Using APK: $APK_PATH"
83-
8473
MANIFEST="$GRADLE_PROJECT_DIR/app/src/main/AndroidManifest.xml"
8574
if [ ! -f "$MANIFEST" ]; then
8675
ra_log "FATAL: AndroidManifest.xml not found at $MANIFEST" >&2
@@ -104,18 +93,6 @@ ADB_BIN="$(command -v adb)"
10493
ra_log "ADB connected devices:"
10594
"$ADB_BIN" devices -l | sed 's/^/[run-android-instrumentation-tests] /'
10695

107-
ra_log "Installing APK onto device"
108-
"$ADB_BIN" shell am force-stop "$PACKAGE_NAME" >/dev/null 2>&1 || true
109-
"$ADB_BIN" uninstall "$PACKAGE_NAME" >/dev/null 2>&1 || true
110-
if ! "$ADB_BIN" install -r "$APK_PATH"; then
111-
ra_log "adb install failed; retrying after explicit uninstall"
112-
"$ADB_BIN" uninstall "$PACKAGE_NAME" >/dev/null 2>&1 || true
113-
if ! "$ADB_BIN" install "$APK_PATH"; then
114-
ra_log "FATAL: adb install failed after retry"
115-
exit 10
116-
fi
117-
fi
118-
11996
ra_log "Clearing logcat buffer"
12097
"$ADB_BIN" logcat -c || true
12198

@@ -134,24 +111,21 @@ ra_log "Capturing device logcat to $TEST_LOG"
134111
LOGCAT_PID=$!
135112
sleep 2
136113

137-
ra_log "Launching Codename One DeviceRunner"
138-
"$ADB_BIN" shell pm clear "$PACKAGE_NAME" >/dev/null 2>&1 || true
139-
if ! "$ADB_BIN" shell monkey -p "$PACKAGE_NAME" -c android.intent.category.LAUNCHER 1 >/dev/null 2>&1; then
140-
ra_log "monkey launch failed; attempting am start fallback"
141-
MAIN_ACTIVITY="$("$ADB_BIN" shell cmd package resolve-activity --brief "$PACKAGE_NAME" 2>/dev/null | head -n 1 | tr -d '\r' | sed 's/ .*//')"
142-
if [[ "$MAIN_ACTIVITY" == */* ]]; then
143-
if ! "$ADB_BIN" shell am start -n "$MAIN_ACTIVITY" >/dev/null 2>&1; then
144-
ra_log "FATAL: Failed to start application via am start"
145-
exit 10
146-
fi
147-
else
148-
ra_log "FATAL: Unable to determine launchable activity"
149-
exit 10
150-
fi
114+
GRADLEW="$GRADLE_PROJECT_DIR/gradlew"
115+
[ -x "$GRADLEW" ] || chmod +x "$GRADLEW"
116+
GRADLE_CMD=("$GRADLEW" --no-daemon connectedDebugAndroidTest)
117+
118+
ra_log "Executing connectedDebugAndroidTest via Gradle"
119+
if ! (
120+
cd "$GRADLE_PROJECT_DIR"
121+
JAVA_HOME="$JAVA17_HOME" "${GRADLE_CMD[@]}"
122+
); then
123+
ra_log "FATAL: connectedDebugAndroidTest failed"
124+
exit 10
151125
fi
152126

153127
END_MARKER="CN1SS:SUITE:FINISHED"
154-
TIMEOUT_SECONDS=300
128+
TIMEOUT_SECONDS=60
155129
START_TIME="$(date +%s)"
156130
ra_log "Waiting for DeviceRunner completion marker ($END_MARKER)"
157131
while true; do

0 commit comments

Comments
 (0)