diff --git a/.github/workflows/make.pas b/.github/workflows/make.pas index 8c0b246..11d58ac 100644 --- a/.github/workflows/make.pas +++ b/.github/workflows/make.pas @@ -241,6 +241,7 @@ TMakeRunner = class function ExtractBinaryFromBuildLog(const AOutput, AFallback: string): string; function IsGUIProject(const ALpiPath: string): Boolean; function IsTestProject(const ALpiPath: string): Boolean; + function IsLibraryProject(const ALpiPath: string): Boolean; procedure RunTestProject(const APath: string); procedure RunSampleProject(const APath: string); procedure InitSslForDownloads; @@ -2024,7 +2025,7 @@ function TMakeRunner.CollectProjectRequiredNames: TStringList; try for Each in List do begin - if IsGUIProject(Each) then + if IsGUIProject(Each) or IsLibraryProject(Each) then Continue; Proj := TLpiProject.CreateFromFile(Each, FTargetCpu, FTargetOs); try @@ -2204,6 +2205,14 @@ function TMakeRunner.IsTestProject(const ALpiPath: string): Boolean; Result := Pos('consoletestrunner', Content) > 0; end; +function TMakeRunner.IsLibraryProject(const ALpiPath: string): Boolean; +begin + Result := False; + if not FileExists(ALpiPath) then + Exit; + Result := Pos('', TLazXml.ReadFile(ALpiPath)) > 0; +end; + procedure TMakeRunner.RunTestProject(const APath: string); var BinaryPath: string; @@ -2275,6 +2284,12 @@ procedure TMakeRunner.BuildAllProjects; Continue; end; + if IsLibraryProject(Each) then + begin + Log(CSI_Yellow, 'skip library project ' + Each); + Continue; + end; + if IsTestProject(Each) then RunTestProject(Each) else diff --git a/.gitignore b/.gitignore index b90e5f5..582841b 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,7 @@ *.dcp *.so *.apk +SimpleBaseLib.Tests/FreePascal.Tests/Mobile/Android/build/ *.drc *.map *.dres diff --git a/SimpleBaseLib.Tests/FreePascal.Tests/Mobile/Android/AndroidManifest.xml b/SimpleBaseLib.Tests/FreePascal.Tests/Mobile/Android/AndroidManifest.xml new file mode 100644 index 0000000..3d4886c --- /dev/null +++ b/SimpleBaseLib.Tests/FreePascal.Tests/Mobile/Android/AndroidManifest.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + diff --git a/SimpleBaseLib.Tests/FreePascal.Tests/Mobile/Android/AndroidTestHost.pas b/SimpleBaseLib.Tests/FreePascal.Tests/Mobile/Android/AndroidTestHost.pas new file mode 100644 index 0000000..14d13ae --- /dev/null +++ b/SimpleBaseLib.Tests/FreePascal.Tests/Mobile/Android/AndroidTestHost.pas @@ -0,0 +1,414 @@ +{ List before the test units: initialization enters the unpacked test data folder. } +unit AndroidTestHost; + +{$mode objfpc}{$H+} + +interface + +uses + SysUtils, + Classes, + jni, + fpcunit, + fphttpclient, + testregistry, + TestInsightClient, + FpcUnitTestInsight, + TestInsightProtocol, + jsonparser; + +type + TAndroidTestHost = class sealed + strict private + class function SkipOutside(AResult: TTestResult; ASuite: TTest; AAllowed: TList; AInside: Boolean): Int32; static; + class function FindAllowed(const ANames: string): TList; static; + class function JavaToString(AEnv: PJNIEnv; AValue: jstring): string; static; + class procedure RunRegistered(AClient: TAbstractTestInsightClient); static; + class function NativeRun(AEnv: PJNIEnv; AThis: jobject; ABaseUrl: jstring): jint; cdecl; static; + public + class procedure LogLine(APriority: LongInt; const AText: string); static; + /// Registers the host activity's native run method; call it from JNI_OnLoad. + class function RegisterHost(AVm: PJavaVM): jint; static; + end; + +implementation + +const + HostClass = 'org/fpctests/host/MainActivity'; + WorkDirVariable = 'FPCTESTS_WORKDIR'; + LogTag = 'FpcTests'; + SettingsFile = 'TestInsightSettings.ini'; + MaxBatchResults = 100; + MaxBatchMs = 1000; + MaxFailedRequests = 2; + LogInfo = 4; + LogWarn = 5; + LogError = 6; + +{$linklib log} +function android_log_write(APriority: LongInt; ATag, AText: PAnsiChar): LongInt; cdecl; + external 'log' name '__android_log_write'; + +type + // logs each start, skip, failure and error to logcat so a run can be read without the IDE + TLogcatListener = class(TInterfacedObject, ITestListener) + public + procedure AddFailure(ATest: TTest; AFailure: TTestFailure); + procedure AddError(ATest: TTest; AError: TTestFailure); + procedure StartTest(ATest: TTest); + procedure EndTest(ATest: TTest); + procedure StartTestSuite(ATestSuite: TTestSuite); + procedure EndTestSuite(ATestSuite: TTestSuite); + end; + + TBlockingTestInsightClient = class(TAbstractTestInsightClient) + private + FHttp: TFPHTTPClient; + FInError: Boolean; + FLastError: string; + FBatch: string; + FBatchCount: Int32; + FBatchSentAt: QWord; + FFailedRequests: Int32; + procedure Send(const AUrl, AContent: string); + procedure FlushResults; + procedure Fail(AError: Exception); + protected + function GetHasError: Boolean; override; + function GetLastErrorMessage: string; override; + procedure ServerPost(aURL: string; const aContent: string = ''); override; + function ServerGet(aURL: string): string; override; + procedure ServerDelete(aURL: string); override; + public + constructor Create(const aBaseURL: string); override; + destructor Destroy; override; + end; + +class procedure TAndroidTestHost.LogLine(APriority: LongInt; const AText: string); +begin + android_log_write(APriority, LogTag, PAnsiChar(AnsiString(AText))); +end; + +procedure TLogcatListener.AddFailure(ATest: TTest; AFailure: TTestFailure); +begin + if AFailure.IsIgnoredTest then + TAndroidTestHost.LogLine(LogInfo, 'SKIP ' + ATest.TestSuiteName + '.' + ATest.TestName + ': ' + + AFailure.ExceptionMessage) + else + TAndroidTestHost.LogLine(LogWarn, 'FAIL ' + ATest.TestSuiteName + '.' + ATest.TestName + ': ' + + AFailure.ExceptionMessage); +end; + +procedure TLogcatListener.AddError(ATest: TTest; AError: TTestFailure); +begin + TAndroidTestHost.LogLine(LogError, 'ERROR ' + ATest.TestSuiteName + '.' + ATest.TestName + ': ' + + AError.ExceptionClassName + ': ' + AError.ExceptionMessage); +end; + +procedure TLogcatListener.StartTest(ATest: TTest); +begin + TAndroidTestHost.LogLine(LogInfo, 'START ' + ATest.TestSuiteName + '.' + ATest.TestName); +end; + +procedure TLogcatListener.EndTest(ATest: TTest); +begin +end; + +procedure TLogcatListener.StartTestSuite(ATestSuite: TTestSuite); +begin +end; + +procedure TLogcatListener.EndTestSuite(ATestSuite: TTestSuite); +begin +end; + +constructor TBlockingTestInsightClient.Create(const aBaseURL: string); +begin + inherited Create(aBaseURL); + FHttp := TFPHTTPClient.Create(nil); + // a server that goes away mid-run must not hang the run for ever + FHttp.ConnectTimeout := 0; + FHttp.IOTimeout := 30000; + FBatchSentAt := GetTickCount64; +end; + +destructor TBlockingTestInsightClient.Destroy; +begin + FlushResults; + FHttp.Free; + inherited Destroy; +end; + +function TBlockingTestInsightClient.GetHasError: Boolean; +begin + Result := FInError; +end; + +function TBlockingTestInsightClient.GetLastErrorMessage: string; +begin + Result := FLastError; +end; + +procedure TBlockingTestInsightClient.Fail(AError: Exception); +begin + FInError := True; + FLastError := AError.ClassName + ': ' + AError.Message; +end; + +procedure TBlockingTestInsightClient.FlushResults; +begin + if FBatch = '' then + Exit; + Send(pathResults, '[' + FBatch + ']'); + FBatch := ''; + FBatchCount := 0; + FBatchSentAt := GetTickCount64; +end; + +// a request per test stops the IDE repainting +procedure TBlockingTestInsightClient.ServerPost(aURL: string; const aContent: string); +begin + if (aURL <> pathResults) or (Length(aContent) <= 2) then + begin + FlushResults; + Send(aURL, aContent); + Exit; + end; + if FBatch <> '' then + FBatch := FBatch + ','; + FBatch := FBatch + Copy(aContent, 2, Length(aContent) - 2); + Inc(FBatchCount); + if (FBatchCount >= MaxBatchResults) or (GetTickCount64 - FBatchSentAt >= MaxBatchMs) then + FlushResults; +end; + +procedure TBlockingTestInsightClient.Send(const AUrl, AContent: string); +var + LBody, LResponse: TStringStream; +begin + // a server that stopped answering must not cost a timeout per request + if FFailedRequests >= MaxFailedRequests then + Exit; + LBody := nil; + LResponse := TStringStream.Create(''); + try + if AContent <> '' then + LBody := TStringStream.Create(AContent); + FHttp.RequestBody := LBody; + try + FHttp.AddHeader('Content-Type', 'application/json'); + FHttp.Post(ConcatURL(AUrl), LResponse); + FInError := False; + FLastError := ''; + FFailedRequests := 0; + except + on E: Exception do + begin + Fail(E); + Inc(FFailedRequests); + if FFailedRequests = MaxFailedRequests then + TAndroidTestHost.LogLine(LogWarn, 'TestInsight stopped answering (' + FLastError + + '); the run goes on in logcat only'); + end; + end; + finally + FHttp.RequestBody := nil; + LResponse.Free; + LBody.Free; + end; +end; + +function TBlockingTestInsightClient.ServerGet(aURL: string): string; +var + LResponse: TStringStream; +begin + Result := ''; + LResponse := TStringStream.Create(''); + try + try + FHttp.Get(ConcatURL(aURL), LResponse); + Result := LResponse.DataString; + FInError := False; + FLastError := ''; + except + on E: Exception do + Fail(E); + end; + finally + LResponse.Free; + end; +end; + +procedure TBlockingTestInsightClient.ServerDelete(aURL: string); +begin + try + FHttp.Delete(ConcatURL(aURL)); + FInError := False; + FLastError := ''; + except + on E: Exception do + Fail(E); + end; +end; + +// returns how many test cases stay enabled +class function TAndroidTestHost.SkipOutside(AResult: TTestResult; ASuite: TTest; AAllowed: TList; + AInside: Boolean): Int32; +var + LI: Int32; + LTest: TTest; + LInside: Boolean; +begin + Result := 0; + for LI := 0 to ASuite.GetChildTestCount - 1 do + begin + LTest := ASuite.GetChildTest(LI); + LInside := AInside or (AAllowed.IndexOf(LTest) >= 0); + if LTest is TTestCase then + begin + if LInside then + Inc(Result) + else + AResult.AddToSkipList(LTest as TTestCase); + end + else + Inc(Result, SkipOutside(AResult, LTest, AAllowed, LInside)); + end; +end; + +// the Suite setting names suites or tests, comma separated +class function TAndroidTestHost.FindAllowed(const ANames: string): TList; +var + LNames: TStringList; + LI: Int32; + LTest: TTest; +begin + Result := TList.Create; + LNames := TStringList.Create; + try + LNames.CommaText := ANames; + for LI := 0 to LNames.Count - 1 do + begin + LTest := GetTestRegistry.FindTest(LNames[LI]); + if LTest <> nil then + Result.Add(LTest) + else + LogLine(LogWarn, 'unknown suite ' + LNames[LI]); + end; + finally + LNames.Free; + end; +end; + +class function TAndroidTestHost.JavaToString(AEnv: PJNIEnv; AValue: jstring): string; +var + LChars: PAnsiChar; +begin + Result := ''; + if AValue = nil then + Exit; + LChars := AEnv^^.GetStringUTFChars(AEnv, AValue, nil); + if LChars = nil then + Exit; + try + Result := LChars; + finally + AEnv^^.ReleaseStringUTFChars(AEnv, AValue, LChars); + end; +end; + +class procedure TAndroidTestHost.RunRegistered(AClient: TAbstractTestInsightClient); +var + LListener, LLogListener: ITestListener; + LResult: TTestResult; + LAllowed: TList; + LCount: Int32; +begin + LListener := TTestInsightListener.Create(AClient, GetTestRegistry) as ITestListener; + // the IDE asks for the test list alone when it only wants to discover + if not AClient.Options.ExecuteTests then + Exit; + LResult := TTestResult.Create; + try + LCount := GetTestRegistry.CountTestCases; + if AClient.Options.TestSuite <> '' then + begin + LAllowed := FindAllowed(AClient.Options.TestSuite); + try + LCount := SkipOutside(LResult, GetTestRegistry, LAllowed, False); + finally + LAllowed.Free; + end; + end; + LogLine(LogInfo, 'running ' + IntToStr(LCount) + ' tests'); + LResult.AddListener(LListener); + LLogListener := TLogcatListener.Create as ITestListener; + LResult.AddListener(LLogListener); + GetTestRegistry.Run(LResult); + finally + LResult.Free; + end; +end; + +class function TAndroidTestHost.NativeRun(AEnv: PJNIEnv; AThis: jobject; ABaseUrl: jstring): jint; cdecl; +var + LClient: TBlockingTestInsightClient; + LError: AnsiString; + LException: jclass; +begin + Result := 0; + try + LClient := TBlockingTestInsightClient.Create(JavaToString(AEnv, ABaseUrl)); + try + LClient.GetServerOptions; + if LClient.HasError then + begin + LError := LClient.LastErrorMessage; + LException := AEnv^^.FindClass(AEnv, 'java/lang/RuntimeException'); + if LException = nil then + Result := 1 + else + AEnv^^.ThrowNew(AEnv, LException, PAnsiChar(LError)); + Exit; + end; + if FileExists(SettingsFile) then + LClient.LoadConfig(SettingsFile); + RunRegistered(LClient); + finally + LClient.Free; + end; + except + on E: Exception do + begin + LogLine(LogError, 'run failed: ' + E.ClassName + ': ' + E.Message); + Result := 1; + end; + end; +end; + +class function TAndroidTestHost.RegisterHost(AVm: PJavaVM): jint; +var + LEnv: PJNIEnv; + LClass: jclass; + LMethod: JNINativeMethod; +begin + Result := JNI_ERR; + if AVm^^.GetEnv(AVm, @LEnv, JNI_VERSION_1_6) <> JNI_OK then + Exit; + LClass := LEnv^^.FindClass(LEnv, HostClass); + if LClass = nil then + Exit; + LMethod.name := 'nativeRun'; + LMethod.signature := '(Ljava/lang/String;)I'; + LMethod.fnPtr := @TAndroidTestHost.NativeRun; + if LEnv^^.RegisterNatives(LEnv, LClass, @LMethod, 1) <> JNI_OK then + Exit; + Result := JNI_VERSION_1_6; +end; + +initialization + RaiseMaxFrameCount := 0; + if GetEnvironmentVariable(WorkDirVariable) <> '' then + SetCurrentDir(GetEnvironmentVariable(WorkDirVariable)); + +end. diff --git a/SimpleBaseLib.Tests/FreePascal.Tests/Mobile/Android/BuildApk.java b/SimpleBaseLib.Tests/FreePascal.Tests/Mobile/Android/BuildApk.java new file mode 100644 index 0000000..77518a8 --- /dev/null +++ b/SimpleBaseLib.Tests/FreePascal.Tests/Mobile/Android/BuildApk.java @@ -0,0 +1,360 @@ +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.time.Duration; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +/** Packages an FPC test library and the Java host into a signed arm64-v8a APK (JDK 11+): java BuildApk.java --library NAME --app-id ID [options]. */ +public final class BuildApk { + private static final String DebugPassword = "android"; + private static final List Known = List.of("library", "app-id", "title", "jdk", "build-tools", + "android-jar", "strip", "keystore", "version-code", "install", "target-os", "project-dir"); + + private final boolean windows = System.getProperty("os.name").toLowerCase().contains("win"); + private final boolean mac = System.getProperty("os.name").toLowerCase().contains("mac"); + private final Map env = System.getenv(); + private final Map options = new HashMap<>(); + + public static void main(String[] args) { + try { + new BuildApk().run(args); + } catch (Exception e) { + System.err.println("build-apk: " + (e.getMessage() == null ? e.toString() : e.getMessage())); + System.exit(1); + } + } + + private void run(String[] args) throws Exception { + for (int i = 0; i < args.length; i++) { + if (!args[i].startsWith("--")) { + throw new IllegalArgumentException("unexpected argument " + args[i]); + } + String name = args[i].substring(2); + if (!Known.contains(name)) { + throw new IllegalArgumentException("unknown option " + args[i] + " (options: --" + + String.join(", --", Known) + ")"); + } + options.put(name, name.equals("install") ? "true" : value(args, ++i)); + } + if (!options.getOrDefault("target-os", "android").equals("android")) { + return; + } + + String libraryName = required("library"); + String appId = required("app-id"); + if (!appId.matches("[a-z][a-z0-9_]*")) { + throw new IllegalArgumentException("--app-id must be lower case letters, digits and underscores: " + appId); + } + String title = options.getOrDefault("title", libraryName); + String libraryFile = "lib" + libraryName + ".so"; + + Path projectDir = Paths.get(options.getOrDefault("project-dir", ".")).toAbsolutePath().normalize(); + Path hostDir = projectDir.resolve("Mobile").resolve("Android"); + if (!Files.exists(hostDir.resolve("AndroidManifest.xml"))) { + throw new IllegalStateException("run from the FreePascal.Tests folder or pass --project-dir; no " + + hostDir.resolve("AndroidManifest.xml")); + } + Path build = hostDir.resolve("build"); + + Path jdk = jdk(); + Path java = require(tool(jdk.resolve("bin"), "java"), "java was not found in the JDK; pass --jdk or set JAVA_HOME"); + Path sdk = fromEnv("ANDROID_HOME", "ANDROID_SDK_ROOT"); + Path buildTools = buildTools(sdk); + Path aapt2 = require(tool(buildTools, "aapt2"), "aapt2 was not found in build-tools"); + Path zipalign = require(tool(buildTools, "zipalign"), "zipalign was not found in build-tools"); + Path d8Jar = require(buildTools.resolve("lib").resolve("d8.jar"), "lib/d8.jar was not found in build-tools"); + Path apksignerJar = require(buildTools.resolve("lib").resolve("apksigner.jar"), + "lib/apksigner.jar was not found in build-tools"); + Path androidJar = androidJar(sdk); + Path strip = strip(sdk); + Path keystore = keystore(); + Path library = projectDir.resolve("bin").resolve(libraryFile); + Path testsRoot = projectDir.getParent(); + Path dataRoot = testsRoot.resolve("Data"); + require(library, "the library was not found; build it first"); + + deleteTree(build); + Path classes = Files.createDirectories(build.resolve("classes")); + Path dex = Files.createDirectories(build.resolve("dex")); + Path assets = Files.createDirectories(build.resolve("assets")); + Path libFolder = Files.createDirectories(build.resolve("lib").resolve("arm64-v8a")); + + // the test data goes in as one archive; the app unpacks it beside the tests' own folder name + zipTestData(dataRoot, assets.resolve("testdata.zip")); + Properties host = new Properties(); + host.setProperty("library", libraryName); + host.setProperty("title", title); + try (OutputStream out = Files.newOutputStream(assets.resolve("host.properties"))) { + host.store(out, null); + } + Path stripped = libFolder.resolve(libraryFile); + exec(List.of(strip.toString(), "--strip-unneeded", "-o", stripped.toString(), library.toString())); + + List javac = new ArrayList<>(List.of(tool(jdk.resolve("bin"), "javac").toString(), + "--release", "8", "-Xlint:-options", "-cp", androidJar.toString(), "-d", classes.toString())); + try (Stream sources = Files.walk(hostDir.resolve("src"))) { + sources.filter(p -> p.toString().endsWith(".java")).forEach(p -> javac.add(p.toString())); + } + exec(javac); + + List d8 = new ArrayList<>(List.of(java.toString(), "-cp", d8Jar.toString(), + "com.android.tools.r8.D8", "--lib", androidJar.toString(), "--min-api", "24", "--output", dex.toString())); + try (Stream files = Files.walk(classes)) { + files.filter(p -> p.toString().endsWith(".class")).forEach(p -> d8.add(p.toString())); + } + exec(d8); + + Path manifest = build.resolve("AndroidManifest.xml"); + String template = new String(Files.readAllBytes(hostDir.resolve("AndroidManifest.xml")), StandardCharsets.UTF_8); + Files.write(manifest, template.replace("@PACKAGE@", "org.fpctests." + appId) + .replace("@LABEL@", escapeXml(title)).getBytes(StandardCharsets.UTF_8)); + + int versionCode = (int) Duration.between(Instant.parse("2026-01-01T00:00:00Z"), Instant.now()).toMinutes(); + if (options.containsKey("version-code")) { + try { + versionCode = Integer.parseInt(options.get("version-code")); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("--version-code must be an integer"); + } + } + Path unsigned = build.resolve("unsigned.apk"); + exec(List.of(aapt2.toString(), "link", "-o", unsigned.toString(), + "--manifest", manifest.toString(), "-I", androidJar.toString(), + "-A", assets.toString(), "--min-sdk-version", "24", "--target-sdk-version", "35", + "--version-code", String.valueOf(versionCode), "--version-name", "1." + versionCode)); + + addToZip(unsigned, "classes.dex", dex.resolve("classes.dex")); + addToZip(unsigned, "lib/arm64-v8a/" + libraryFile, stripped); + + Path aligned = build.resolve("aligned.apk"); + exec(List.of(zipalign.toString(), "-f", "-p", "4", unsigned.toString(), aligned.toString())); + + if (!Files.exists(keystore)) { + Path keytool = require(tool(jdk.resolve("bin"), "keytool"), "keytool was not found in the JDK"); + Files.createDirectories(keystore.getParent()); + exec(List.of(keytool.toString(), "-genkeypair", "-keystore", keystore.toString(), + "-alias", "fpctestsdebug", "-keyalg", "RSA", "-keysize", "2048", "-validity", "10000", + "-storepass", DebugPassword, "-keypass", DebugPassword, "-dname", "CN=FPC Tests Debug")); + } + Path apk = build.resolve(appId + ".apk"); + exec(List.of(java.toString(), "-jar", apksignerJar.toString(), "sign", "--ks", keystore.toString(), + "--ks-pass", "pass:" + DebugPassword, "--key-pass", "pass:" + DebugPassword, + "--out", apk.toString(), aligned.toString())); + exec(List.of(java.toString(), "-jar", apksignerJar.toString(), "verify", "--verbose", apk.toString())); + + System.out.println(apk + " " + Files.size(apk) + " bytes"); + + if (options.containsKey("install")) { + Path adb = sdk == null ? null : sdk.resolve("platform-tools").resolve(windows ? "adb.exe" : "adb"); + exec(List.of(adb != null && Files.exists(adb) ? adb.toString() : "adb", "install", "-r", apk.toString())); + } + } + + private static String value(String[] args, int index) { + if (index >= args.length) { + throw new IllegalArgumentException("missing value for " + args[index - 1]); + } + return args[index]; + } + + private String required(String option) { + String given = options.get(option); + if (given == null || given.isEmpty()) { + throw new IllegalArgumentException("--" + option + " is required"); + } + return given; + } + + private String setting(String option, String environment) { + String fromOption = options.get(option); + if (fromOption != null && !fromOption.isEmpty()) { + return fromOption; + } + String fromEnv = environment == null ? null : env.get(environment); + return fromEnv == null || fromEnv.isEmpty() ? null : fromEnv; + } + + private Path jdk() { + String given = setting("jdk", "JAVA_HOME"); + Path jdk = Paths.get(given != null ? given : System.getProperty("java.home")); + require(tool(jdk.resolve("bin"), "javac"), "javac was not found in the JDK; pass --jdk or set JAVA_HOME"); + return jdk; + } + + // the folder named by the first of these variables that is set + private Path fromEnv(String... names) { + for (String name : names) { + String given = env.get(name); + if (given != null && !given.isEmpty()) { + return Paths.get(given); + } + } + return null; + } + + private Path buildTools(Path sdk) { + String given = setting("build-tools", "ANDROID_BUILD_TOOLS"); + Path folder = given != null ? Paths.get(given) : newest(sdk == null ? null : sdk.resolve("build-tools"), null); + return require(folder, "no build-tools folder; pass --build-tools, or set ANDROID_BUILD_TOOLS or ANDROID_HOME"); + } + + private Path androidJar(Path sdk) { + String given = setting("android-jar", null); + Path jar = given != null ? Paths.get(given) : null; + if (jar == null && sdk != null) { + Path platform = newest(sdk.resolve("platforms"), "android.jar"); + jar = platform == null ? null : platform.resolve("android.jar"); + } + return require(jar, "no android.jar; pass --android-jar, or set ANDROID_HOME to an SDK with a platform installed"); + } + + private Path strip(Path sdk) { + String given = setting("strip", null); + if (given != null) { + return require(Paths.get(given), "llvm-strip was not found"); + } + Path ndk = fromEnv("ANDROID_NDK_ROOT", "ANDROID_NDK_HOME"); + if (ndk == null) { + ndk = newest(sdk == null ? null : sdk.resolve("ndk"), null); + } + Path strip = ndk == null ? null : tool(ndk.resolve("toolchains").resolve("llvm").resolve("prebuilt") + .resolve(hostTag()).resolve("bin"), "llvm-strip"); + return require(strip, + "no llvm-strip; pass --strip, or set ANDROID_NDK_ROOT or ANDROID_NDK_HOME (or ANDROID_HOME with an ndk folder)"); + } + + private Path keystore() { + String given = setting("keystore", "FPCTESTS_ANDROID_KEYSTORE"); + Path keystore = given != null ? Paths.get(given) + : Paths.get(System.getProperty("user.home"), ".android", "fpctests-debug.keystore"); + return keystore.toAbsolutePath(); + } + + // the NDK ships one prebuilt folder per host; macOS has a single one for both CPUs + private String hostTag() { + if (windows) { + return "windows-x86_64"; + } + return mac ? "darwin-x86_64" : "linux-x86_64"; + } + + private Path tool(Path folder, String name) { + return folder.resolve(windows ? name + ".exe" : name); + } + + /** The folder under parent with the highest version in its name; one holding the given file if named. */ + private static Path newest(Path parent, String mustContain) { + if (parent == null || !Files.isDirectory(parent)) { + return null; + } + try (Stream folders = Files.list(parent)) { + return folders.filter(Files::isDirectory) + .filter(p -> mustContain == null || Files.exists(p.resolve(mustContain))) + .max(Comparator.comparing(p -> numbers(p.getFileName().toString()), BuildApk::compareNumbers)) + .orElse(null); + } catch (IOException e) { + return null; + } + } + + private static List numbers(String text) { + List result = new ArrayList<>(); + Matcher matcher = Pattern.compile("\\d+").matcher(text); + while (matcher.find()) { + result.add(Integer.parseInt(matcher.group())); + } + return result; + } + + private static int compareNumbers(List left, List right) { + for (int i = 0; i < Math.min(left.size(), right.size()); i++) { + int order = Integer.compare(left.get(i), right.get(i)); + if (order != 0) { + return order; + } + } + return Integer.compare(left.size(), right.size()); + } + + private static Path require(Path path, String what) { + if (path == null || !Files.exists(path)) { + throw new IllegalStateException(what + (path == null ? "" : " (looked at " + path + ")")); + } + return path; + } + + // aapt2 reads a leading @ or ? in an attribute value as a resource reference + private static String escapeXml(String text) { + String escaped = text.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """); + return escaped.startsWith("@") || escaped.startsWith("?") ? "\\" + escaped : escaped; + } + + private static void zipTestData(Path dataRoot, Path target) throws IOException { + Path testsRoot = dataRoot.getParent(); + String prefix = testsRoot.getFileName() + "/"; + try (ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(target))) { + // a library whose tests carry no data folder gets an empty archive + if (!Files.isDirectory(dataRoot)) { + return; + } + try (Stream files = Files.walk(dataRoot)) { + for (Path file : files.filter(Files::isRegularFile).collect(Collectors.toList())) { + String name = prefix + testsRoot.relativize(file).toString().replace('\\', '/'); + zip.putNextEntry(new ZipEntry(name)); + Files.copy(file, zip); + zip.closeEntry(); + } + } + } + } + + private static void addToZip(Path zip, String entry, Path file) throws IOException { + try (FileSystem fs = FileSystems.newFileSystem(zip, (ClassLoader) null)) { + Path inside = fs.getPath(entry); + if (inside.getParent() != null) { + Files.createDirectories(inside.getParent()); + } + Files.copy(file, inside, StandardCopyOption.REPLACE_EXISTING); + } + } + + private static void deleteTree(Path folder) throws IOException { + if (!Files.exists(folder)) { + return; + } + try (Stream files = Files.walk(folder)) { + for (Path file : files.sorted(Comparator.reverseOrder()).collect(Collectors.toList())) { + Files.delete(file); + } + } + } + + // a tool's stderr is not an error in itself; the exit code decides + private static void exec(List command) throws IOException, InterruptedException { + Process process = new ProcessBuilder(command).inheritIO().start(); + int code = process.waitFor(); + if (code != 0) { + throw new IllegalStateException("failed (" + code + "): " + command.stream() + .map(argument -> argument.contains(" ") ? "\"" + argument + "\"" : argument) + .collect(Collectors.joining(" "))); + } + } +} diff --git a/SimpleBaseLib.Tests/FreePascal.Tests/Mobile/Android/src/org/fpctests/host/MainActivity.java b/SimpleBaseLib.Tests/FreePascal.Tests/Mobile/Android/src/org/fpctests/host/MainActivity.java new file mode 100644 index 0000000..ca3b773 --- /dev/null +++ b/SimpleBaseLib.Tests/FreePascal.Tests/Mobile/Android/src/org/fpctests/host/MainActivity.java @@ -0,0 +1,273 @@ +package org.fpctests.host; + +import android.app.Activity; +import android.content.pm.PackageManager; +import android.os.Bundle; +import android.system.Os; +import android.text.InputType; +import android.widget.Button; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.TextView; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.util.Properties; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +/** Runs an FPC test library and reports it to a TestInsight server; host.properties names the library. */ +public class MainActivity extends Activity { + private static final String LogTag = "FpcTests"; + private static final String UrlKey = "baseUrl"; + private static final String DataArchive = "testdata.zip"; + private static final String DataStamp = ".data-stamp"; + private static final String WorkDirVariable = "FPCTESTS_WORKDIR"; + private static final String SettingsFile = "TestInsightSettings.ini"; + private static final long StackBytes = 64L << 20; + + // one worker with a big stack for loading the library and for every run + private final ExecutorService worker = Executors.newSingleThreadExecutor(task -> { + Thread thread = new Thread(null, task, "fpc-tests", StackBytes); + thread.setDaemon(true); + return thread; + }); + private EditText urlField; + private TextView status; + private Button runButton; + private String suite = ""; + private volatile boolean libraryLoaded; + // static: a recreated activity (rotation) must not start a second run in the same process + private static final AtomicBoolean running = new AtomicBoolean(); + + public native int nativeRun(String baseUrl); + + @Override + @SuppressWarnings("deprecation") + protected void onCreate(Bundle state) { + super.onCreate(state); + // adb shell am start ... --ez autorun true --es url http://host:6789/tests --es suite TestName + final boolean autorun = state == null && getIntent().getBooleanExtra("autorun", false); + final String autorunUrl = getIntent().getStringExtra("url"); + String suiteExtra = getIntent().getStringExtra("suite"); + // only suite and test names may reach the ini file, so no extra keys can be injected + suite = state != null || suiteExtra == null ? "" : suiteExtra.replaceAll("[^A-Za-z0-9_.,]", ""); + Properties host = new Properties(); + try (InputStream in = getAssets().open("host.properties")) { + host.load(in); + } catch (IOException e) { + throw new IllegalStateException("host.properties is missing from the APK", e); + } + final String library = host.getProperty("library"); + + LinearLayout root = new LinearLayout(this); + root.setOrientation(LinearLayout.VERTICAL); + int pad = (int) (16 * getResources().getDisplayMetrics().density); + root.setPadding(pad, pad, pad, pad); + // the window is drawn edge to edge on recent Android, so keep clear of the system bars + root.setOnApplyWindowInsetsListener((v, insets) -> { + v.setPadding(pad + insets.getSystemWindowInsetLeft(), pad + insets.getSystemWindowInsetTop(), + pad + insets.getSystemWindowInsetRight(), pad + insets.getSystemWindowInsetBottom()); + return insets; + }); + + TextView title = new TextView(this); + title.setText(host.getProperty("title", library)); + title.setTextSize(22); + title.setPadding(0, 0, 0, pad); + root.addView(title); + + urlField = new EditText(this); + urlField.setHint("http://192.168.0.10:6789/tests"); + urlField.setSingleLine(true); + urlField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI); + urlField.setText(getPreferences(MODE_PRIVATE).getString(UrlKey, "")); + root.addView(urlField); + + Button saveButton = new Button(this); + saveButton.setText("Save URL"); + saveButton.setOnClickListener(v -> { + String url = normalise(urlField.getText().toString()); + urlField.setText(url); + getPreferences(MODE_PRIVATE).edit().putString(UrlKey, url).apply(); + setStatus("Saved " + url); + }); + root.addView(saveButton); + + runButton = new Button(this); + runButton.setText("Run tests"); + runButton.setOnClickListener(v -> startRun()); + root.addView(runButton); + + status = new TextView(this); + status.setPadding(0, pad, 0, 0); + root.addView(status); + setContentView(root); + + runButton.setEnabled(false); + setStatus("Preparing the test data for " + library + "..."); + worker.execute(() -> loadLibrary(library, autorun, autorunUrl)); + } + + @Override + protected void onDestroy() { + worker.shutdown(); + super.onDestroy(); + } + + /** Unpacks the test data, then loads the library: its units may read that data while they load. */ + private void loadLibrary(String library, boolean autorun, String autorunUrl) { + try { + File work = getFilesDir(); + extractData(work); + Os.setenv(WorkDirVariable, work.getAbsolutePath(), true); + System.loadLibrary(library); + } catch (Exception | UnsatisfiedLinkError e) { + setStatus("Could not load " + library + ": " + e); + return; + } + libraryLoaded = true; + runOnUiThread(() -> runButton.setEnabled(true)); + setStatus("Ready. Enter the TestInsight URL, save it, and tap Run tests."); + + if (autorun) { + runOnUiThread(() -> { + if (autorunUrl != null) { + urlField.setText(normalise(autorunUrl)); + } + startRun(); + }); + } + } + + /** A bare host:port becomes http://host:port/tests, the base path of the IDE's server. */ + private static String normalise(String text) { + String url = text.trim(); + if (url.isEmpty()) { + return url; + } + if (!url.contains("://")) { + url = "http://" + url; + } + int schemeEnd = url.indexOf("://") + 3; + if (url.indexOf('/', schemeEnd) < 0) { + url = url + "/tests"; + } + return url; + } + + private void setStatus(String message) { + android.util.Log.i(LogTag, message); + runOnUiThread(() -> status.setText(message)); + } + + private void startRun() { + if (!libraryLoaded) { + return; + } + final String url = normalise(urlField.getText().toString()); + if (!url.startsWith("http://") && !url.startsWith("https://")) { + setStatus("Enter an http:// or https:// TestInsight URL first."); + return; + } + if (!running.compareAndSet(false, true)) { + setStatus("A run is already in progress."); + return; + } + getPreferences(MODE_PRIVATE).edit().putString(UrlKey, url).apply(); + runButton.setEnabled(false); + setStatus("Running against " + url); + // the suite filter applies to the first run only + final String runSuite = suite; + suite = ""; + worker.execute(() -> { + String result; + try { + File settings = new File(getFilesDir(), SettingsFile); + if (runSuite.isEmpty()) { + settings.delete(); + } else { + try (OutputStream out = new FileOutputStream(settings)) { + out.write(("[Config]\nSuite=" + runSuite + "\n").getBytes(StandardCharsets.UTF_8)); + } + } + int code = nativeRun(url); + result = code == 0 ? "Finished. Results are in TestInsight." : "Run failed (code " + code + ")."; + } catch (Throwable e) { + result = "Run failed: " + e; + } finally { + running.set(false); + } + final String message = result; + android.util.Log.i(LogTag, message); + runOnUiThread(() -> { + status.setText(message); + runButton.setEnabled(true); + }); + }); + } + + /** Unpacks the test data under the work folder once per install, where the tests look for it. */ + private void extractData(File work) throws IOException, PackageManager.NameNotFoundException { + String stamp = String.valueOf(getPackageManager().getPackageInfo(getPackageName(), 0).lastUpdateTime); + File stampFile = new File(work, DataStamp); + if (stampFile.exists() && stamp.equals(readText(stampFile))) { + return; + } + deleteChildren(work); + String root = work.getCanonicalPath() + File.separator; + try (ZipInputStream zip = new ZipInputStream(getAssets().open(DataArchive))) { + ZipEntry entry; + while ((entry = zip.getNextEntry()) != null) { + File target = new File(work, entry.getName()); + if (!target.getCanonicalPath().startsWith(root)) { + throw new IOException("unexpected entry " + entry.getName()); + } + if (entry.isDirectory()) { + target.mkdirs(); + continue; + } + target.getParentFile().mkdirs(); + try (OutputStream out = new FileOutputStream(target)) { + byte[] buffer = new byte[65536]; + int read; + while ((read = zip.read(buffer)) > 0) { + out.write(buffer, 0, read); + } + } + } + } + try (OutputStream out = new FileOutputStream(stampFile)) { + out.write(stamp.getBytes(StandardCharsets.UTF_8)); + } + } + + private static String readText(File file) throws IOException { + try (InputStream in = new FileInputStream(file)) { + byte[] bytes = new byte[(int) file.length()]; + int read = in.read(bytes); + return new String(bytes, 0, Math.max(read, 0), StandardCharsets.UTF_8); + } + } + + private static void deleteChildren(File folder) { + File[] children = folder.listFiles(); + if (children == null) { + return; + } + for (File child : children) { + if (child.isDirectory()) { + deleteChildren(child); + } + child.delete(); + } + } +} diff --git a/SimpleBaseLib.Tests/FreePascal.Tests/SimpleBaseLib.Tests.Android.lpi b/SimpleBaseLib.Tests/FreePascal.Tests/SimpleBaseLib.Tests.Android.lpi new file mode 100644 index 0000000..3c42ad9 --- /dev/null +++ b/SimpleBaseLib.Tests/FreePascal.Tests/SimpleBaseLib.Tests.Android.lpi @@ -0,0 +1,71 @@ + + + + + + + + + + + <UseAppBundle Value="False"/> + <ResourceType Value="res"/> + </General> + <BuildModes Count="1"> + <Item1 Name="Default" Default="True"/> + </BuildModes> + <PublishOptions> + <Version Value="2"/> + </PublishOptions> + <RunParams> + <FormatVersion Value="2"/> + </RunParams> + <RequiredPackages Count="3"> + <Item1> + <PackageName Value="SimpleBaseLib4PascalPackage"/> + </Item1> + <Item2> + <PackageName Value="FCL"/> + </Item2> + <Item3> + <PackageName Value="LazTestInsight"/> + </Item3> + </RequiredPackages> + <Units Count="1"> + <Unit0> + <Filename Value="SimpleBaseLib.Tests.Android.lpr"/> + <IsPartOfProject Value="True"/> + </Unit0> + </Units> + </ProjectOptions> + <CompilerOptions> + <Version Value="11"/> + <PathDelim Value="\"/> + <Target> + <Filename Value=".\bin\libSimpleBaseLibAndroidTests.so"/> + </Target> + <SearchPaths> + <IncludeFiles Value="$(ProjOutDir)"/> + <OtherUnitFiles Value="..\src;..\src\Base64;..\src\Multibase;..\src\Misc;..\src\Base85;..\src\Base8;..\src\Base62;..\src\Base58;..\src\Base45;..\src\Base32;..\src\Base2;..\src\Base16;..\src\Base10;..\src\Base36;..\src\Coders;Mobile\Android"/> + <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/> + </SearchPaths> + <CodeGeneration> + <TargetCPU Value="aarch64"/> + <TargetOS Value="android"/> + </CodeGeneration> + <Linking> + <Options> + <ExecutableType Value="Library"/> + </Options> + </Linking> + <Other> + <ConfigFile> + <CustomConfigFile Value="True"/> + <ConfigFilePath Value="..\..\BuildConfig\FPC\SimpleBaseLibFPCMessages.cfg"/> + </ConfigFile> + <ExecuteAfter> + <Command Value="java "$(ProjPath)Mobile/Android/BuildApk.java" --project-dir "$(ProjPath)." --library SimpleBaseLibAndroidTests --app-id simplebaselib --title "SimpleBaseLib Tests" --target-os $(TargetOS)"/> + </ExecuteAfter> + </Other> + </CompilerOptions> +</CONFIG> diff --git a/SimpleBaseLib.Tests/FreePascal.Tests/SimpleBaseLib.Tests.Android.lpr b/SimpleBaseLib.Tests/FreePascal.Tests/SimpleBaseLib.Tests.Android.lpr new file mode 100644 index 0000000..fee3325 --- /dev/null +++ b/SimpleBaseLib.Tests/FreePascal.Tests/SimpleBaseLib.Tests.Android.lpr @@ -0,0 +1,26 @@ +library SimpleBaseLibAndroidTests; + +{$mode objfpc}{$H+} + +uses + cthreads, cwstring, jni, AndroidTestHost, + SimpleBaseLibTestBase, Base36Tests, Base10Tests, + Base16StreamRegressionTests, Base16Tests, Base2StreamRegressionTests, + Base2Tests, Base32Tests, Base32StreamRegressionTests, Base32AlphabetTests, + Base45Tests, + Base45StreamRegressionTests, Base58Tests, Base62Tests, + Base8Tests, Base8StreamRegressionTests, Base85Tests, + Base85StreamRegressionTests, + EncodingAlphabetTests, CodingAlphabetTests, BitsTests, MultibaseTests, + Base64Tests, Base64StreamRegressionTests, DividingCoderTests; + +function JNI_OnLoad(AVm: PJavaVM; AReserved: Pointer): jint; cdecl; +begin + Result := TAndroidTestHost.RegisterHost(AVm); +end; + +exports + JNI_OnLoad; + +begin +end.