Add scheduled TV updates from Forgejo
Check a commit-pinned manifest on a configurable schedule, notify about newer versions, verify downloaded APKs, and hand off installation to Android. Generate the feed with each signed build. Include the wake artwork recovery fix and on-device lifecycle and updater regression tests.
This commit is contained in:
parent
f5f1c0928f
commit
0d1a11d943
23 changed files with 1493 additions and 51 deletions
6
tests/device/AndroidManifest.xml
Normal file
6
tests/device/AndroidManifest.xml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="tv.luma.launcher.tests">
|
||||
<uses-sdk android:minSdkVersion="26" android:targetSdkVersion="34" />
|
||||
<application android:label="Luma wake regression tests" />
|
||||
<instrumentation android:name=".WakeRegressionInstrumentation" android:targetPackage="tv.luma.launcher" android:functionalTest="true" />
|
||||
<instrumentation android:name="tv.luma.launcher.UpdaterInstrumentation" android:targetPackage="tv.luma.launcher" android:functionalTest="true" />
|
||||
</manifest>
|
||||
133
tests/device/UpdaterInstrumentation.java
Normal file
133
tests/device/UpdaterInstrumentation.java
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
package tv.luma.launcher;
|
||||
|
||||
import android.app.*;
|
||||
import android.app.job.*;
|
||||
import android.content.*;
|
||||
import android.net.Uri;
|
||||
import android.os.*;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
/** Real Android archive/signature parsing, HTTP, private file grants, and job scheduling. */
|
||||
public final class UpdaterInstrumentation extends Instrumentation {
|
||||
private final StringBuilder results = new StringBuilder();
|
||||
private Updater updater;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle arguments) {
|
||||
super.onCreate(arguments);
|
||||
start();
|
||||
}
|
||||
|
||||
private void ok(String message) {
|
||||
results.append("PASS: ").append(message).append('\n');
|
||||
}
|
||||
|
||||
private void source(String name) throws Exception {
|
||||
updater.source("http://127.0.0.1:8766/" + name);
|
||||
}
|
||||
|
||||
private void rejected(String feed, String message) throws Exception {
|
||||
source(feed);
|
||||
try {
|
||||
Updater.Release release = updater.check(false);
|
||||
if (release != null) updater.download(release, percent -> {});
|
||||
} catch (Exception expected) {
|
||||
if (expected.getMessage() == null || !expected.getMessage().contains(message)) throw expected;
|
||||
if (new File(updater.apk().getParentFile(), "Luma.apk.part").exists())
|
||||
throw new AssertionError("partial file retained");
|
||||
ok(feed + " rejected: " + message);
|
||||
return;
|
||||
}
|
||||
throw new AssertionError(feed + " was accepted");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
Bundle result = new Bundle();
|
||||
Map<String, ?> original = null;
|
||||
int code = Activity.RESULT_CANCELED;
|
||||
try {
|
||||
updater = new Updater(getTargetContext());
|
||||
original = new HashMap<>(updater.prefs.getAll());
|
||||
if (updater.installedCode() != 3)
|
||||
throw new AssertionError("Install the 1.1.0 bootstrap before this fixture test");
|
||||
updater.prefs.edit().putInt("hours", 24).apply();
|
||||
UpdateJob.schedule(getTargetContext());
|
||||
JobInfo job =
|
||||
getTargetContext().getSystemService(JobScheduler.class).getPendingJob(UpdateJob.ID);
|
||||
if (job == null
|
||||
|| !job.isPersisted()
|
||||
|| job.getIntervalMillis() != 86400000L
|
||||
|| job.getNetworkType() != JobInfo.NETWORK_TYPE_ANY)
|
||||
throw new AssertionError("incorrect scheduled job");
|
||||
ok("daily job is persisted and requires a network");
|
||||
updater.source("http://127.0.0.1:8766/Elijah/Luma/raw/branch/main/output/update.json");
|
||||
Updater.Release release = updater.check(false);
|
||||
if (release == null
|
||||
|| !release.url.contains(
|
||||
"/raw/commit/" + String.join("", Collections.nCopies(40, "a")) + "/output/Luma.apk"))
|
||||
throw new AssertionError("APK is not pinned to the feed commit");
|
||||
updater.download(release, percent -> {});
|
||||
if (!updater.ready(release)) throw new AssertionError("verified APK not ready");
|
||||
try (ParcelFileDescriptor file =
|
||||
getTargetContext().getContentResolver().openFileDescriptor(UpdateProvider.URI, "r")) {
|
||||
if (file.getStatSize() != release.size)
|
||||
throw new AssertionError("installer cannot read APK");
|
||||
}
|
||||
ok("commit-pinned download passes checksum, package, version and signing-key verification");
|
||||
try {
|
||||
getTargetContext().getContentResolver().openFileDescriptor(UpdateProvider.URI, "w");
|
||||
throw new AssertionError("provider allowed writing");
|
||||
} catch (FileNotFoundException expected) {
|
||||
ok("installer provider denies write access");
|
||||
}
|
||||
try {
|
||||
getTargetContext()
|
||||
.getContentResolver()
|
||||
.openFileDescriptor(Uri.parse("content://tv.luma.launcher.updates/other"), "r");
|
||||
throw new AssertionError("provider exposed another path");
|
||||
} catch (FileNotFoundException expected) {
|
||||
ok("installer provider exposes only the APK URI");
|
||||
}
|
||||
rejected("badhash.json", "checksum");
|
||||
rejected("wrongkey.json", "different key");
|
||||
rejected("wrongpackage.json", "newer matching");
|
||||
rejected("wrongcode.json", "newer matching");
|
||||
rejected("oversized.json", "invalid");
|
||||
source("current.json");
|
||||
if (updater.check(false) != null || updater.available() != null)
|
||||
throw new AssertionError("current version offered as update");
|
||||
ok("current version is not offered as an update");
|
||||
try {
|
||||
Updater.validUrl("http://example.com/update.json");
|
||||
throw new AssertionError("public HTTP accepted");
|
||||
} catch (IOException expected) {
|
||||
ok("public sources require HTTPS");
|
||||
}
|
||||
updater.prefs.edit().putInt("hours", 0).apply();
|
||||
UpdateJob.schedule(getTargetContext());
|
||||
if (getTargetContext().getSystemService(JobScheduler.class).getPendingJob(UpdateJob.ID)
|
||||
!= null) throw new AssertionError("Off did not cancel the job");
|
||||
ok("Off cancels the automatic update job");
|
||||
code = Activity.RESULT_OK;
|
||||
} catch (Throwable error) {
|
||||
results.append("FAIL: ").append(error).append('\n');
|
||||
} finally {
|
||||
if (original != null) {
|
||||
SharedPreferences.Editor edit = updater.prefs.edit().clear();
|
||||
for (Map.Entry<String, ?> entry : original.entrySet()) {
|
||||
Object value = entry.getValue();
|
||||
if (value instanceof String) edit.putString(entry.getKey(), (String) value);
|
||||
else if (value instanceof Long) edit.putLong(entry.getKey(), (Long) value);
|
||||
else if (value instanceof Integer) edit.putInt(entry.getKey(), (Integer) value);
|
||||
else if (value instanceof Boolean) edit.putBoolean(entry.getKey(), (Boolean) value);
|
||||
}
|
||||
edit.commit();
|
||||
UpdateJob.schedule(getTargetContext());
|
||||
}
|
||||
result.putString("stream", results.toString());
|
||||
finish(code, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
207
tests/device/WakeRegressionInstrumentation.java
Normal file
207
tests/device/WakeRegressionInstrumentation.java
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
package tv.luma.launcher.tests;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Instrumentation;
|
||||
import android.content.ComponentCallbacks2;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Bundle;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/** Same-signature test APK. Exercises real activity callbacks without shipping a test interface. */
|
||||
public final class WakeRegressionInstrumentation extends Instrumentation {
|
||||
private Activity activity;
|
||||
private final StringBuilder results = new StringBuilder();
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle arguments) {
|
||||
super.onCreate(arguments);
|
||||
start();
|
||||
}
|
||||
|
||||
private Object field(Object owner, String name) throws Exception {
|
||||
Field field = owner.getClass().getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
return field.get(owner);
|
||||
}
|
||||
|
||||
private boolean imagesReady() throws Exception {
|
||||
Map<?, ?> tiles = (Map<?, ?>) field(activity, "tiles");
|
||||
if (tiles.isEmpty()) return false;
|
||||
for (Object tile : tiles.values()) {
|
||||
Bitmap image = (Bitmap) field(field(tile, "art"), "bitmap");
|
||||
if (image == null || image.isRecycled()) return false;
|
||||
}
|
||||
Bitmap wallpaper = (Bitmap) field(field(activity, "scene"), "wallpaper");
|
||||
return wallpaper != null && !wallpaper.isRecycled();
|
||||
}
|
||||
|
||||
private void awaitImages(String message) throws Exception {
|
||||
long end = android.os.SystemClock.uptimeMillis() + 7000;
|
||||
boolean[] ready = {false};
|
||||
while (android.os.SystemClock.uptimeMillis() < end) {
|
||||
runOnMainSync(
|
||||
() -> {
|
||||
try {
|
||||
ready[0] = imagesReady();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
if (ready[0]) {
|
||||
results.append("PASS: ").append(message).append('\n');
|
||||
return;
|
||||
}
|
||||
Thread.sleep(50);
|
||||
}
|
||||
throw new AssertionError(message + ": app icons or wallpaper still missing");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
Bundle result = new Bundle();
|
||||
try {
|
||||
activity =
|
||||
startActivitySync(
|
||||
new Intent(Intent.ACTION_MAIN)
|
||||
.addCategory(Intent.CATEGORY_HOME)
|
||||
.setComponent(
|
||||
new ComponentName("tv.luma.launcher", "tv.luma.launcher.MainActivity"))
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
|
||||
awaitImages("cold launch loads all app icons and wallpaper");
|
||||
ThreadPoolExecutor worker = (ThreadPoolExecutor) field(activity, "work");
|
||||
CountDownLatch blocked = new CountDownLatch(1), release = new CountDownLatch(1);
|
||||
worker.execute(
|
||||
() -> {
|
||||
blocked.countDown();
|
||||
try {
|
||||
release.await(10, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
if (!blocked.await(3, TimeUnit.SECONDS)) throw new AssertionError("worker did not block");
|
||||
// Recorded TV sequence: restart/start -> resume -> pause -> resume, with decoding
|
||||
// interrupted.
|
||||
runOnMainSync(
|
||||
() -> {
|
||||
callActivityOnPause(activity);
|
||||
callActivityOnStop(activity);
|
||||
callActivityOnRestart(activity);
|
||||
callActivityOnStart(activity);
|
||||
callActivityOnResume(activity);
|
||||
callActivityOnPause(activity);
|
||||
});
|
||||
CountDownLatch drained = new CountDownLatch(1);
|
||||
worker.execute(drained::countDown);
|
||||
release.countDown();
|
||||
if (!drained.await(3, TimeUnit.SECONDS)) throw new AssertionError("worker did not drain");
|
||||
waitForIdleSync();
|
||||
runOnMainSync(() -> callActivityOnResume(activity));
|
||||
awaitImages("interrupted wake recovers without another onStart or app restart");
|
||||
|
||||
Object[] firstTile = new Object[1], firstBitmap = new Object[1];
|
||||
runOnMainSync(
|
||||
() -> {
|
||||
try {
|
||||
firstTile[0] = ((Map<?, ?>) field(activity, "tiles")).values().iterator().next();
|
||||
firstBitmap[0] = field(field(firstTile[0], "art"), "bitmap");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
callActivityOnPause(activity);
|
||||
activity.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN);
|
||||
callActivityOnResume(activity);
|
||||
});
|
||||
awaitImages("pause-only memory trim restores wallpaper");
|
||||
runOnMainSync(
|
||||
() -> {
|
||||
try {
|
||||
Object tile = ((Map<?, ?>) field(activity, "tiles")).values().iterator().next();
|
||||
if (tile != firstTile[0] || field(field(tile, "art"), "bitmap") != firstBitmap[0])
|
||||
throw new AssertionError(
|
||||
"pause-only resume unnecessarily rebuilt loaded app tiles");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
results.append("PASS: existing tile views and bitmaps survive pause-only resume\n");
|
||||
|
||||
// Let discovery render the real grid, but hold decoding until after the next pause.
|
||||
CountDownLatch beforeDiscovery = new CountDownLatch(1), discover = new CountDownLatch(1);
|
||||
CountDownLatch beforeIcons = new CountDownLatch(1), decode = new CountDownLatch(1);
|
||||
worker.execute(() -> block(beforeDiscovery, discover));
|
||||
if (!beforeDiscovery.await(3, TimeUnit.SECONDS))
|
||||
throw new AssertionError("discovery blocker");
|
||||
runOnMainSync(
|
||||
() -> {
|
||||
callActivityOnPause(activity);
|
||||
callActivityOnStop(activity);
|
||||
callActivityOnRestart(activity);
|
||||
callActivityOnStart(activity);
|
||||
callActivityOnResume(activity);
|
||||
worker.execute(() -> block(beforeIcons, decode));
|
||||
});
|
||||
discover.countDown();
|
||||
if (!beforeIcons.await(3, TimeUnit.SECONDS)) throw new AssertionError("icon blocker");
|
||||
waitForIdleSync();
|
||||
runOnMainSync(
|
||||
() -> {
|
||||
try {
|
||||
firstTile[0] = ((Map<?, ?>) field(activity, "tiles")).values().iterator().next();
|
||||
if (field(field(firstTile[0], "art"), "bitmap") != null)
|
||||
throw new AssertionError("icons were not held before pause");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
callActivityOnPause(activity);
|
||||
activity.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN);
|
||||
});
|
||||
CountDownLatch iconsDrained = new CountDownLatch(1);
|
||||
worker.execute(iconsDrained::countDown);
|
||||
decode.countDown();
|
||||
if (!iconsDrained.await(3, TimeUnit.SECONDS)) throw new AssertionError("icon queue drain");
|
||||
runOnMainSync(() -> callActivityOnResume(activity));
|
||||
awaitImages("partially loaded app grid recovers after pause-only resume");
|
||||
runOnMainSync(
|
||||
() -> {
|
||||
try {
|
||||
if (((Map<?, ?>) field(activity, "tiles")).values().iterator().next() != firstTile[0])
|
||||
throw new AssertionError("partial-grid recovery rebuilt the tiles");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
for (int i = 0; i < 5; i++) {
|
||||
runOnMainSync(
|
||||
() -> {
|
||||
callActivityOnPause(activity);
|
||||
callActivityOnStop(activity);
|
||||
callActivityOnRestart(activity);
|
||||
callActivityOnStart(activity);
|
||||
callActivityOnResume(activity);
|
||||
});
|
||||
awaitImages("stop/start recovery cycle " + (i + 1));
|
||||
}
|
||||
result.putString("stream", results.toString());
|
||||
finish(Activity.RESULT_OK, result);
|
||||
} catch (Throwable error) {
|
||||
result.putString("stream", results + "FAIL: " + error + "\n");
|
||||
finish(Activity.RESULT_CANCELED, result);
|
||||
}
|
||||
}
|
||||
|
||||
private void block(CountDownLatch entered, CountDownLatch release) {
|
||||
entered.countDown();
|
||||
try {
|
||||
release.await(10, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException error) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue