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
3 changes: 2 additions & 1 deletion crates/nsh-cli/src/test_android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
//! The host is built (for the device's ABI, as `nsh run`'s) and installed
//! once; each `open` builds the entry, serves it in a dev server's test
//! session of its own, forwards its port to the device and starts the host
//! on it with a data directory of its own, empty when it starts. The host's
//! on it with a data directory of its own, empty when it starts (and kept
//! when Android restores the activity after killing its process). The host's
//! test agent takes the test's steps with `MotionEvent`s and `KeyEvent`s its
//! views get as a finger's and a keyboard's (`nsh-platform-android`'s
//! `agent.rs`, `TestAgent.java`); the host is stopped when the test closes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ protected void onCreate(Bundle state) {
applyEnv(getIntent().getExtras());
boolean trace = getIntent().getBooleanExtra("trace", false);
// A test session's app (`nsh test --target android`) keeps its data
// in a directory of its own, empty when it starts.
// in a directory of its own, empty when it is launched and kept when
// Android restores the activity (`state`) after its process was killed.
String data = getIntent().getStringExtra("dataDir");
if (data != null) {
java.io.File dir = new java.io.File(new java.io.File(getCacheDir(), "nsh-test"), data);
deleteTree(dir);
java.io.File dir = TestData.open(new java.io.File(getCacheDir(), "nsh-test"), data, state != null);
// Its caches too (downloaded images): the app starts with none.
view = new NshView(new android.content.MutableContextWrapper(this), trace, dir, new java.io.File(dir, "cache"));
} else {
Expand Down Expand Up @@ -107,12 +107,6 @@ static void applyEnv(Bundle extras) {
}
}

private static void deleteTree(java.io.File file) {
java.io.File[] children = file.listFiles();
if (children != null) for (java.io.File c : children) deleteTree(c);
file.delete();
}

/** A notification pressed, or a URL opened, while this activity is in its task. */
@Override
protected void onNewIntent(android.content.Intent intent) {
Expand Down
32 changes: 32 additions & 0 deletions crates/nsh-platform-android/java/com/longbridge/nsh/TestData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.longbridge.nsh;

import java.io.File;

/**
* The data directory of an app a test session runs in the development host
* ({@code nsh test --target android}). Free of Android APIs so the rule can be
* exercised with plain {@code javac} / {@code java}: see {@code
* tests/java/TestDataTest.java}.
*/
final class TestData {
private TestData() {}

/**
* The directory {@code name} under {@code root}. A launch starts the app
* with it empty. An activity Android restores ({@code restored}: it was
* destroyed with its process, or with "Don't keep activities") continues
* that app, as a packaged app continues with its storage: the directory
* keeps what the app wrote.
*/
static File open(File root, String name, boolean restored) {
File dir = new File(root, name);
if (!restored) deleteTree(dir);
return dir;
}

private static void deleteTree(File file) {
File[] children = file.listFiles();
if (children != null) for (File c : children) deleteTree(c);
file.delete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.longbridge.nsh;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Coverage for a test session's data directory: emptied when the app is
* launched, kept when Android restores the activity. Plain JDK, no Android
* APIs, so it runs on a developer machine or in CI without a device:
*
* <pre>
* javac -d target/java-tests \
* crates/nsh-platform-android/java/com/longbridge/nsh/TestData.java \
* crates/nsh-platform-android/tests/java/com/longbridge/nsh/TestDataTest.java
* java -cp target/java-tests com.longbridge.nsh.TestDataTest
* </pre>
*/
public final class TestDataTest {
public static void main(String[] args) throws Exception {
File root = Files.createTempDirectory("nsh-test-data-").toFile();

File dir = TestData.open(root, "app-0", false);
check("a launch names the app's directory", dir.equals(new File(root, "app-0")));
check("a first launch has nothing to empty", !dir.exists());

Path stored = write(dir, "storage/draft.json");
Path cached = write(dir, "cache/image.png");
Path other = write(new File(root, "app-1"), "storage/draft.json");

TestData.open(root, "app-0", true);
check("a restored activity keeps the app's storage", Files.exists(stored));
check("a restored activity keeps the app's caches", Files.exists(cached));

TestData.open(root, "app-0", false);
check("a launch empties the app's directory", !new File(root, "app-0").exists());
check("a launch leaves other apps' directories", Files.exists(other));

System.out.println("TestDataTest: PASS");
}

private static Path write(File dir, String path) throws Exception {
Path file = dir.toPath().resolve(path);
Files.createDirectories(file.getParent());
return Files.write(file, new byte[] {1});
}

private static void check(String what, boolean ok) {
if (!ok) throw new AssertionError(what);
}
}
2 changes: 1 addition & 1 deletion docs/dev/tooling.md
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ app; the screen keeps its device's size. Android (`nsh-platform-android`, featur
`nsh-cli/src/test_android.rs`): the CLI installs the host on the device `--device` or `ANDROID_SERIAL` names, else the
first one `adb devices` lists (`android::device`), and starts it per
`open` with the dev server's port forwarded (`adb reverse`) and a data directory of its own in the host's cache
(`dataDir`, emptied as it starts). As `nsh run --target android` does for a TSX app, it passes the variables of its
(`dataDir`, emptied as it starts and kept when Android restores the activity after killing its process). As `nsh run --target android` does for a TSX app, it passes the variables of its
environment that the app's `capabilities.process.env` declares as `nsh.env.<name>` string extras
(`run::android_app_env`; values quoted for the device's shell, which `adb shell` hands the command line), and
`DevActivity` sets them in its process's environment before the engine starts, so `process.env` reads them as on the
Expand Down
Loading