Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
103f97e
Update to Android Gradle plugin version 8.4.2
mhsmith Jul 10, 2024
fb1b54a
Separate Python test runner from MainActivity so it can be run from a…
mhsmith Jul 10, 2024
b99b769
Add `android.py test` command
mhsmith Jul 10, 2024
6f07a39
--connected mode working
mhsmith Jul 23, 2024
c71f3cf
--managed mode working, and add instructions to README
mhsmith Jul 23, 2024
cd27b0f
Clarifications
mhsmith Jul 23, 2024
6bbdfda
Restore build command logging, and only require adb when running tests
mhsmith Jul 23, 2024
e738689
Make testbed include only those ABIs which have been built
mhsmith Jul 30, 2024
4561930
Merge branch 'android-2024-07' into android-test-script
mhsmith Jul 30, 2024
b519874
Fix Windows issues
mhsmith Jul 31, 2024
b3ed4c7
Merge branch 'main' into android-test-script
mhsmith Aug 5, 2024
32f79d7
Clarify documentation; remove default `python -m test` arguments
mhsmith Aug 8, 2024
6471512
Link to page about test options; add newlines at end of files
mhsmith Aug 8, 2024
5c3967e
Add `android.py build-testbed` command
mhsmith Aug 8, 2024
cb60cc4
If test script fails before logcat starts, show the Gradle output eve…
mhsmith Aug 11, 2024
b075842
Fix logcat error messages being hidden
mhsmith Aug 11, 2024
2266e82
Improve logging, add timeouts
mhsmith Aug 11, 2024
397d20b
Make the app use the same NDK version as the Python build
mhsmith Aug 11, 2024
8e80dd2
Install platform-tools automatically
mhsmith Aug 11, 2024
709061e
In --connected mode, uninstall app before running Gradle
mhsmith Aug 12, 2024
756bc13
Handle adb logcat returning failure when device disconnects
mhsmith Aug 12, 2024
6b2ed6c
Filter logs by PID rather than UID
mhsmith Aug 12, 2024
05a26cc
Try to terminate subprocesses with SIGTERM before sending SIGKILL
mhsmith Aug 12, 2024
a0dd03b
Handle SIGTERM the same way as SIGINT
mhsmith Aug 12, 2024
2d53549
Fix race condition with pre-run uninstall
mhsmith Aug 12, 2024
c9d5bf5
Fix race condition in a more efficient way
mhsmith Aug 12, 2024
706a1da
Make testbed pick up edits to pure-Python files in the Lib directory
mhsmith Aug 12, 2024
ae3a460
Merge branch 'main' into android-test-script
mhsmith Aug 12, 2024
cf15b99
Remove `boot_completed`, which is unnecessary since we're no longer r…
mhsmith Aug 13, 2024
c56373a
Automatically accept SDK licenses, and log Gradle package installatio…
mhsmith Aug 13, 2024
1e89e58
Stop Gradle test tasks being skipped as up to date
mhsmith Aug 13, 2024
91f356b
Fix CalledProcessError formatting
mhsmith Aug 13, 2024
bdaad24
Add note about testing on Windows
mhsmith Aug 13, 2024
ca2bae2
Implement `fileno` method on stdout and stderr
mhsmith Aug 13, 2024
7a3d674
Correct comment
mhsmith Aug 13, 2024
f4b06f5
Merge branch 'main' into android-test-script
freakboy3742 Aug 13, 2024
47c02a5
Merge branch 'main' into android-test-script
mhsmith Aug 15, 2024
602fbdd
Add note about RAM requirements
mhsmith Aug 15, 2024
305d786
Handle transient failure of pidof
mhsmith Aug 15, 2024
2ad8e4b
Merge branch 'main' into android-test-script
freakboy3742 Aug 16, 2024
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
Prev Previous commit
Next Next commit
Separate Python test runner from MainActivity so it can be run from a…
… JUnit test suite
  • Loading branch information
mhsmith committed Jul 10, 2024
commit fb1b54ab13f9d802ec69a7e1175b9be085257442
4 changes: 4 additions & 0 deletions Android/testbed/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ android {
externalNativeBuild.cmake.arguments(
"-DPYTHON_CROSS_DIR=$PYTHON_CROSS_DIR",
"-DPYTHON_VERSION=$PYTHON_VERSION")

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

externalNativeBuild.cmake {
Expand All @@ -61,6 +63,8 @@ dependencies {
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test:rules:1.5.0")
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.python.testbed

import androidx.test.annotation.UiThreadTest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*


@RunWith(AndroidJUnit4::class)
class PythonSuite {
@Test
@UiThreadTest
fun testPython() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val args = InstrumentationRegistry.getArguments().getString("pythonArgs", "")
val status = PythonTestRunner(context).run(args)
assertEquals(0, status)
}
}
12 changes: 6 additions & 6 deletions Android/testbed/app/src/main/c/main_activity.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static char *redirect_stream(StreamInfo *si) {
return 0;
}

JNIEXPORT void JNICALL Java_org_python_testbed_MainActivity_redirectStdioToLogcat(
JNIEXPORT void JNICALL Java_org_python_testbed_PythonTestRunner_redirectStdioToLogcat(
JNIEnv *env, jobject obj
) {
for (StreamInfo *si = STREAMS; si->file; si++) {
Expand Down Expand Up @@ -115,7 +115,7 @@ static void throw_status(JNIEnv *env, PyStatus status) {
throw_runtime_exception(env, status.err_msg ? status.err_msg : "");
}

JNIEXPORT void JNICALL Java_org_python_testbed_MainActivity_runPython(
JNIEXPORT int JNICALL Java_org_python_testbed_PythonTestRunner_runPython(
JNIEnv *env, jobject obj, jstring home, jstring runModule
) {
PyConfig config;
Expand All @@ -125,13 +125,13 @@ JNIEXPORT void JNICALL Java_org_python_testbed_MainActivity_runPython(
status = set_config_string(env, &config, &config.home, home);
if (PyStatus_Exception(status)) {
throw_status(env, status);
return;
return 1;
}

status = set_config_string(env, &config, &config.run_module, runModule);
if (PyStatus_Exception(status)) {
throw_status(env, status);
return;
return 1;
}

// Some tests generate SIGPIPE and SIGXFSZ, which should be ignored.
Expand All @@ -140,8 +140,8 @@ JNIEXPORT void JNICALL Java_org_python_testbed_MainActivity_runPython(
status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(status)) {
throw_status(env, status);
return;
return 1;
}

Py_RunMain();
return Py_RunMain();
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,56 @@
package org.python.testbed

import android.content.Context
import android.os.*
import android.system.Os
import android.widget.TextView
import androidx.appcompat.app.*
import java.io.*


// Launching the tests from an activity is OK for a quick check, but for
// anything more complicated it'll be more use `android.py test` to launch the
// tests via PythonSuite.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val status = PythonTestRunner(this).run()
findViewById<TextView>(R.id.tvHello).text = "Exit status $status"
}
}


class PythonTestRunner(val context: Context) {
/** @param args Extra arguments for `python -m test`.
* @return The Python exit status: zero if the tests passed, nonzero if
* they failed. */
fun run(args: String = "") : Int {
Os.setenv("PYTHON_ARGS", args, true)

// Python needs this variable to help it find the temporary directory,
// but Android only sets it on API level 33 and later.
Os.setenv("TMPDIR", cacheDir.toString(), false)
Os.setenv("TMPDIR", context.cacheDir.toString(), false)

val pythonHome = extractAssets()
System.loadLibrary("main_activity")
redirectStdioToLogcat()
runPython(pythonHome.toString(), "main")
findViewById<TextView>(R.id.tvHello).text = "Python complete"

// The main module is in src/main/python/main.py.
return runPython(pythonHome.toString(), "main")
}

private fun extractAssets() : File {
val pythonHome = File(filesDir, "python")
val pythonHome = File(context.filesDir, "python")
if (pythonHome.exists() && !pythonHome.deleteRecursively()) {
throw RuntimeException("Failed to delete $pythonHome")
}
extractAssetDir("python", filesDir)
extractAssetDir("python", context.filesDir)
return pythonHome
}

private fun extractAssetDir(path: String, targetDir: File) {
val names = assets.list(path)
val names = context.assets.list(path)
?: throw RuntimeException("Failed to list $path")
val targetSubdir = File(targetDir, path)
if (!targetSubdir.mkdirs()) {
Expand All @@ -43,7 +61,7 @@ class MainActivity : AppCompatActivity() {
val subPath = "$path/$name"
val input: InputStream
try {
input = assets.open(subPath)
input = context.assets.open(subPath)
} catch (e: FileNotFoundException) {
extractAssetDir(subPath, targetDir)
continue
Expand All @@ -57,5 +75,5 @@ class MainActivity : AppCompatActivity() {
}

private external fun redirectStdioToLogcat()
private external fun runPython(home: String, runModule: String)
private external fun runPython(home: String, runModule: String) : Int
}
12 changes: 7 additions & 5 deletions Android/testbed/app/src/main/python/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import runpy
import shlex
import signal
import sys

Expand All @@ -8,10 +10,10 @@
# profile save"), so disabling it should not weaken the tests.
signal.pthread_sigmask(signal.SIG_UNBLOCK, [signal.SIGUSR1])

# To run specific tests, or pass any other arguments to the test suite, edit
# this command line.
sys.argv[1:] = [
"--use", "all,-cpu",
"--verbose3",
]
"-uall", # Enable all resources
"-W", # Display test output on failure
] + shlex.split(os.environ["PYTHON_ARGS"])

# The test module will call sys.exit to indicate whether the tests passed.
runpy.run_module("test")