Skip to content
Closed
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
2 changes: 1 addition & 1 deletion scripts/android/tests/Cn1ssChunkTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static void runTests(String[] args) throws IOException {
}
Path path = Path.of(args[0]);
List<String> names = new ArrayList<>();
for (Chunk chunk : iterateChunks(path, Optional.empty(), Optional.of(DEFAULT_CHANNEL))) {
for (Chunk chunk : iterateChunks(path, Optional.empty(), Optional.empty())) {
if (!names.contains(chunk.testName)) {
names.add(chunk.testName);
}
Expand Down
18 changes: 18 additions & 0 deletions scripts/device-runner-app/tests/BaseTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.codenameone.examples.hellocodenameone.tests;

import com.codename1.io.Log;
import com.codename1.testing.AbstractTest;
import com.codename1.ui.Form;
import com.codename1.ui.util.UITimer;
Expand All @@ -8,13 +9,22 @@

public abstract class BaseTest extends AbstractTest {
private boolean done;
private boolean logEmitted;
private String currentScreenshotName = "default";

protected Form createForm(String title, Layout layout, final String imageName) {
done = false;
currentScreenshotName = Cn1ssDeviceRunnerHelper.sanitizeTestName(imageName);
logEmitted = false;
Cn1ssDeviceRunnerHelper.resetLogCapture(currentScreenshotName);
Cn1ssDeviceRunnerHelper.emitTestStartMarker(currentScreenshotName);
return new Form(title, layout) {
@Override
protected void onShowCompleted() {
Log.p("CN1SS: form ready for screenshot -> " + imageName);
registerReadyCallback(this, () -> {
Cn1ssDeviceRunnerHelper.emitCurrentFormScreenshot(imageName);
logEmitted = true;
done = true;
});
}
Expand All @@ -32,11 +42,19 @@ protected boolean waitForDone() {
TestUtils.waitFor(20);
timeout--;
if(timeout == 0) {
Log.p("CN1SS: timeout waiting for screenshot emission");
Cn1ssDeviceRunnerHelper.emitLogChannel(currentScreenshotName);
logEmitted = true;
return false;
}
}
// give the test a few additional milliseconds for the screenshot emission
TestUtils.waitFor(100);
Log.p("CN1SS: screenshot emission completed");
if (!logEmitted) {
Cn1ssDeviceRunnerHelper.emitLogChannel(currentScreenshotName);
logEmitted = true;
}
return true;
}
}
5 changes: 5 additions & 0 deletions scripts/device-runner-app/tests/Cn1ssDeviceRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.codename1.testing.DeviceRunner;
import com.codename1.testing.TestReporting;
import com.codename1.io.Log;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.testing.AbstractTest;
Expand All @@ -18,18 +19,22 @@ public final class Cn1ssDeviceRunner extends DeviceRunner {
};

public void runSuite() {
Log.p("CN1SS: starting device runner suite with " + TEST_CLASSES.length + " tests");
for (AbstractTest testClass : TEST_CLASSES) {
log("CN1SS:INFO:suite starting test=" + testClass);
try {
Log.p("CN1SS: preparing test " + testClass);
testClass.prepare();
testClass.runTest();
testClass.cleanup();
Log.p("CN1SS: finished test " + testClass);
log("CN1SS:INFO:suite finished test=" + testClass);
} catch (Throwable t) {
log("CN1SS:ERR:suite test=" + testClass + " failed=" + t);
t.printStackTrace();
}
}
Log.p("CN1SS: device runner suite complete");
log("CN1SS:SUITE:FINISHED");
TestReporting.getInstance().testExecutionFinished(getClass().getName());
}
Expand Down
81 changes: 81 additions & 0 deletions scripts/device-runner-app/tests/Cn1ssDeviceRunnerHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.codenameone.examples.hellocodenameone.tests;

import com.codename1.io.FileSystemStorage;
import com.codename1.io.Util;
import com.codename1.io.Log;
import com.codename1.ui.Display;
Expand All @@ -10,16 +11,53 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

final class Cn1ssDeviceRunnerHelper {
private static final int CHUNK_SIZE = 900;
private static final int MAX_PREVIEW_BYTES = 20 * 1024;
private static final String PREVIEW_CHANNEL = "PREVIEW";
private static final String LOG_CHANNEL = "LOG";
private static final String START_CHANNEL = "START";
private static final String LOG_FILE_NAME = "cn1ss-device-runner.log";
private static final int[] PREVIEW_QUALITIES = new int[] {60, 50, 40, 35, 30, 25, 20, 18, 16, 14, 12, 10, 8, 6, 5, 4, 3, 2, 1};
private static final String LOG_FILE_PATH = initializeLogFile();

private Cn1ssDeviceRunnerHelper() {
}

private static String initializeLogFile() {
FileSystemStorage storage = FileSystemStorage.getInstance();
String path = storage.getAppHomePath() + LOG_FILE_NAME;
if (storage.exists(path)) {
storage.delete(path);
}
Log.getInstance().setFileURL(path);
Log.getInstance().setFileWriteEnabled(true);
Log.p("CN1SS logging initialized at " + path);
return path;
}

static void resetLogCapture(String testName) {
if (LOG_FILE_PATH == null || LOG_FILE_PATH.length() == 0) {
return;
}
String safeName = sanitizeTestName(testName);
FileSystemStorage storage = FileSystemStorage.getInstance();
if (storage.exists(LOG_FILE_PATH)) {
storage.delete(LOG_FILE_PATH);
}
Log.getInstance().setFileURL(LOG_FILE_PATH);
Log.getInstance().setFileWriteEnabled(true);
println("CN1SS:INFO:test=" + safeName + " log_reset=true path=" + LOG_FILE_PATH);
}

static void emitTestStartMarker(String testName) {
String safeName = sanitizeTestName(testName);
emitChannel(("start:" + safeName).getBytes(StandardCharsets.UTF_8), safeName, START_CHANNEL);
}

static void runOnEdtSync(Runnable runnable) {
Display display = Display.getInstance();
if (display.isEdt()) {
Expand All @@ -39,6 +77,7 @@ static boolean emitCurrentFormScreenshot(String testName) {
Form current = Display.getInstance().getCurrent();
if (current == null) {
println("CN1SS:ERR:test=" + safeName + " message=Current form is null");
emitLogOutput(safeName);
println("CN1SS:END:" + safeName);
return false;
}
Expand All @@ -52,12 +91,14 @@ static boolean emitCurrentFormScreenshot(String testName) {
Util.sleep(50);
// timeout
if (System.currentTimeMillis() - time > 2000) {
emitLogOutput(safeName);
return;
}
}
});
if (img[0] == null) {
println("CN1SS:ERR:test=" + safeName + " message=Screenshot process timed out");
emitLogOutput(safeName);
println("CN1SS:END:" + safeName);
return false;
}
Expand All @@ -66,6 +107,7 @@ static boolean emitCurrentFormScreenshot(String testName) {
ImageIO io = ImageIO.getImageIO();
if (io == null || !io.isFormatSupported(ImageIO.FORMAT_PNG)) {
println("CN1SS:ERR:test=" + safeName + " message=PNG encoding unavailable");
emitLogOutput(safeName);
println("CN1SS:END:" + safeName);
return false;
}
Expand All @@ -81,17 +123,25 @@ static boolean emitCurrentFormScreenshot(String testName) {
} else {
println("CN1SS:INFO:test=" + safeName + " preview_jpeg_bytes=0 preview_quality=0");
}
emitLogOutput(safeName);
return true;
} catch (IOException ex) {
println("CN1SS:ERR:test=" + safeName + " message=" + ex);
Log.e(ex);
emitLogOutput(safeName);
println("CN1SS:END:" + safeName);
return false;
} finally {
screenshot.dispose();
}
}

static void emitLogChannel(String testName) {
String safeName = sanitizeTestName(testName);
emitLogOutput(safeName);
println("CN1SS:END:" + safeName);
}

private static byte[] encodePreview(ImageIO io, Image screenshot, String safeName) throws IOException {
byte[] chosenPreview = null;
int chosenQuality = 0;
Expand Down Expand Up @@ -180,6 +230,37 @@ private static String zeroPad(int value, int width) {
return builder.toString();
}

private static void emitLogOutput(String safeName) {
byte[] logBytes = readLogBytes();
if (logBytes == null || logBytes.length == 0) {
println("CN1SS:INFO:test=" + safeName + " log_bytes=0");
emitChannel("(no log entries captured)".getBytes(), safeName, LOG_CHANNEL);
return;
}
println("CN1SS:INFO:test=" + safeName + " log_bytes=" + logBytes.length);
emitChannel(logBytes, safeName, LOG_CHANNEL);
}

private static byte[] readLogBytes() {
if (LOG_FILE_PATH == null || LOG_FILE_PATH.length() == 0) {
return null;
}
FileSystemStorage storage = FileSystemStorage.getInstance();
if (!storage.exists(LOG_FILE_PATH)) {
return null;
}
InputStream input = null;
try {
input = storage.openInputStream(LOG_FILE_PATH);
return Util.readInputStream(input);
} catch (IOException ex) {
Log.e(ex);
return null;
} finally {
Util.cleanup(input);
}
}

private static void println(String line) {
System.out.println(line);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.codename1.media.MediaManager;
import com.codename1.testing.AbstractTest;
import com.codename1.testing.TestUtils;
import com.codename1.io.Log;
import com.codename1.ui.Container;
import com.codename1.ui.Form;
import com.codename1.ui.Label;
Expand All @@ -28,9 +29,11 @@ public boolean runTest() throws Exception {
if (tonePath == null) {
updateStatus(statusLabel, form, "Failed to generate tone file");
} else {
Log.p("CN1SS: attempting media playback from " + tonePath);
Media media = MediaManager.createMedia(tonePath, false);
if (media == null) {
updateStatus(statusLabel, form, "Media creation returned null");
Log.p("CN1SS: media creation returned null for " + tonePath);
}
media.setTime(0);
media.play();
Expand Down Expand Up @@ -68,6 +71,7 @@ private static void cleanupMedia(Media media) {
private static String writeToneWav() {
byte[] wav = buildToneWav();
if (wav == null || wav.length == 0) {
Log.p("CN1SS: generated tone WAV is empty");
return null;
}
String path = FileSystemStorage.getInstance().getAppHomePath() + "media-playback-test.wav";
Expand All @@ -76,9 +80,11 @@ private static String writeToneWav() {
try {
out = FileSystemStorage.getInstance().openOutputStream(path);
out.write(wav);
Log.p("CN1SS: wrote tone WAV to " + path + " (" + wav.length + " bytes)");
return path;
} catch (IOException ex) {
TestUtils.log("Unable to write tone wav: " + ex.getMessage());
Log.e(ex);
return null;
} finally {
if (out != null) {
Expand Down
11 changes: 11 additions & 0 deletions scripts/lib/cn1ss.sh
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ cn1ss_verify_png() {
head -c 8 "$file" | od -An -t x1 | tr -d ' \n' | grep -qi '^89504e470d0a1a0a$'
}

cn1ss_verify_text() {
local file="$1"
[ -f "$file" ]
}

cn1ss_verify_jpeg() {
local file="$1"
[ -s "$file" ] || return 1
Expand Down Expand Up @@ -234,6 +239,12 @@ cn1ss_decode_test_preview() {
cn1ss_decode_test_asset "$test" "$dest" "PREVIEW" cn1ss_verify_jpeg "$@"
}

cn1ss_decode_test_log() {
local test="$1"; shift
local dest="$1"; shift
cn1ss_decode_test_asset "$test" "$dest" "LOG" cn1ss_verify_text "$@"
}

cn1ss_file_size() {
local file="$1"
if [ ! -f "$file" ]; then
Expand Down
Loading
Loading