Skip to content

Commit 4151c2a

Browse files
committed
Trying another direction for screenshot grabbing
1 parent e624c99 commit 4151c2a

File tree

3 files changed

+123
-4
lines changed

3 files changed

+123
-4
lines changed

.github/workflows/scripts-android.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,12 @@ jobs:
3737
target: google_apis
3838
script: |
3939
./scripts/run-android-instrumentation-tests.sh "${{ steps.build-android-app.outputs.gradle_project_dir }}"
40+
- name: Upload emulator screenshot
41+
if: always() # still collect it if tests fail
42+
uses: actions/upload-artifact@v4
43+
with:
44+
name: emulator-screenshot
45+
path: artifacts/*.png
46+
if-no-files-found: warn
47+
retention-days: 14
48+
compression-level: 6

scripts/build-android-app.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,51 @@ package $PACKAGE_NAME;
325325
import android.content.Context;
326326
import android.test.InstrumentationTestCase;
327327
328+
import android.app.Instrumentation;
329+
import android.os.ParcelFileDescriptor;
330+
import android.util.Base64;
331+
332+
import java.io.FileInputStream;
333+
import java.io.IOException;
334+
import java.io.InputStream;
335+
328336
public class HelloCodenameOneInstrumentedTest extends InstrumentationTestCase {
329337
330338
public void testUseAppContext() {
331339
Context appContext = getInstrumentation().getTargetContext();
332340
assertEquals("$PACKAGE_NAME", appContext.getPackageName());
333341
}
342+
343+
private static final int CHUNK = 200_000;
344+
345+
public static void printPngToStdout(Instrumentation inst) {
346+
try {
347+
ParcelFileDescriptor pfd = inst.getUiAutomation().executeShellCommand("screencap -p");
348+
byte[] png;
349+
try (InputStream in = new FileInputStream(pfd.getFileDescriptor())) {
350+
png = readAll(in);
351+
}
352+
String b64 = Base64.encodeToString(png, Base64.NO_WRAP);
353+
System.out.println("<<CN1_SCREENSHOT_BEGIN>>");
354+
for (int i = 0; i < b64.length(); i += CHUNK) {
355+
int end = Math.min(i + CHUNK, b64.length());
356+
System.out.println(b64.substring(i, end));
357+
}
358+
System.out.println("<<CN1_SCREENSHOT_END>>");
359+
System.out.flush();
360+
} catch (IOException err) {
361+
err.printStackTrace();
362+
throw new RuntimeException(err);
363+
}
364+
}
365+
366+
private static byte[] readAll(InputStream in) throws IOException {
367+
byte[] buf = new byte[64 * 1024];
368+
int n;
369+
java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
370+
while ((n = in.read(buf)) != -1) out.write(buf, 0, n);
371+
return out.toByteArray();
372+
}
334373
}
335374
EOF
336375
ba_log "Created instrumentation test at $TEST_CLASS"

scripts/run-android-instrumentation-tests.sh

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ if [ $# -lt 1 ]; then
99
exit 1
1010
fi
1111

12+
# near the top
13+
ARTIFACTS_DIR="${ARTIFACTS_DIR:-${GITHUB_WORKSPACE:-$REPO_ROOT}/artifacts}"
14+
mkdir -p "$ARTIFACTS_DIR"
15+
TEST_LOG="$ARTIFACTS_DIR/connectedAndroidTest.log"
16+
1217
GRADLE_PROJECT_DIR="$1"
1318

1419
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -102,11 +107,77 @@ if [ ! -x "$GRADLE_PROJECT_DIR/gradlew" ]; then
102107
chmod +x "$GRADLE_PROJECT_DIR/gradlew"
103108
fi
104109

105-
ra_log "Running instrumentation tests from $GRADLE_PROJECT_DIR"
106-
ORIGINAL_JAVA_HOME="${JAVA_HOME:-}"; export JAVA_HOME="$JAVA17_HOME"
110+
set -o pipefail
111+
112+
ra_log "Running instrumentation tests (stdout -> $TEST_LOG; stderr -> terminal)"
113+
status=0
107114
(
108115
cd "$GRADLE_PROJECT_DIR"
109-
./gradlew --no-daemon connectedDebugAndroidTest
116+
# stdout goes to tee+file, stderr remains on the terminal
117+
./gradlew --no-daemon --console=plain connectedDebugAndroidTest | tee "$TEST_LOG"
118+
) || status=$?
119+
120+
# Show the log now (helpful for review even on success)
121+
echo
122+
ra_log "==== Begin connectedAndroidTest.log (tail -n 200) ===="
123+
tail -n 200 "$TEST_LOG" || true
124+
ra_log "==== End connectedAndroidTest.log ===="
125+
echo
126+
127+
# --- Harvest stdout from connected tests (AGP old & new layouts) ---
128+
RESULTS_ROOT="$GRADLE_PROJECT_DIR/app/build/outputs/androidTest-results/connected"
129+
ARTIFACTS_DIR="${ARTIFACTS_DIR:-${GITHUB_WORKSPACE:-$REPO_ROOT}/artifacts}"
130+
mkdir -p "$ARTIFACTS_DIR"
131+
132+
# Debug: show what exists
133+
ra_log "Listing connected test outputs under: $RESULTS_ROOT"
134+
find "$RESULTS_ROOT" -maxdepth 4 -printf '%y %p\n' 2>/dev/null | sed 's/^/[run-android-instrumentation-tests] /' || true
135+
136+
# Gather candidate XMLs (new: test-result.xml; old: TEST-*.xml)
137+
mapfile -t CANDIDATES < <(
138+
find "$RESULTS_ROOT" -type f \( -name 'test-result.xml' -o -name 'TEST-*.xml' \) -printf '%T@ %p\n' 2>/dev/null \
139+
| sort -nr \
140+
| awk '{ $1=""; sub(/^ /,""); print }'
110141
)
111-
export JAVA_HOME="$ORIGINAL_JAVA_HOME"
142+
143+
if [ "${#CANDIDATES[@]}" -eq 0 ]; then
144+
ra_log "No connected test XML files found under $RESULTS_ROOT"
145+
else
146+
ra_log "Found ${#CANDIDATES[@]} test result file(s). Will scan for screenshot markers."
147+
fi
148+
149+
# Try each candidate until one yields a non-empty PNG
150+
SCREENSHOT_PATH="$ARTIFACTS_DIR/emulator-screenshot.png"
151+
: > "$SCREENSHOT_PATH"
152+
153+
extract_from_xml() {
154+
local xml="$1" out="$2"
155+
# Pull lines strictly between our markers, regardless of XML tag nesting or CDATA
156+
awk '
157+
/<<CN1_SCREENSHOT_BEGIN>>/ {on=1; next}
158+
/<<CN1_SCREENSHOT_END>>/ {on=0}
159+
on { gsub(/\r/,""); printf "%s", $0 } # collapse to a single line for decoder robustness
160+
' "$xml" | base64 -d > "$out" 2>/dev/null
161+
}
162+
163+
EXTRACTED=0
164+
for xml in "${CANDIDATES[@]}"; do
165+
ra_log "Scanning: $xml"
166+
if extract_from_xml "$xml" "$SCREENSHOT_PATH" && [ -s "$SCREENSHOT_PATH" ]; then
167+
ra_log "Screenshot saved: $(ls -lh "$SCREENSHOT_PATH" | awk '{print $5, $9}')"
168+
EXTRACTED=1
169+
break
170+
fi
171+
done
172+
173+
if [ "$EXTRACTED" -ne 1 ]; then
174+
ra_log "No markers found in XML. Dumping any marker snippets for debugging:"
175+
for xml in "${CANDIDATES[@]}"; do
176+
ra_log "---- ${xml} (BEGIN..END) ----"
177+
sed -n '/<<CN1_SCREENSHOT_BEGIN>>/,/<<CN1_SCREENSHOT_END>>/p' "$xml" || true
178+
done
179+
fi
180+
181+
ra_log "Latest connected test report: ${NEWEST_XML:-<none>}"
182+
112183
ra_log "Instrumentation tests completed successfully"

0 commit comments

Comments
 (0)