Luma/tests/device/RenderingInstrumentation.java

270 lines
9.2 KiB
Java

package tv.luma.launcher.tests;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.*;
import android.os.*;
import android.view.*;
import java.lang.reflect.*;
import java.util.concurrent.*;
/**
* Compare real GPU crossfade pixels with the compatible renderer, including differently sized
* inputs.
*/
public final class RenderingInstrumentation extends Instrumentation {
private Field member(Object o, String name) throws Exception {
Field f = o.getClass().getDeclaredField(name);
f.setAccessible(true);
return f;
}
private Object get(Object o, String name) throws Exception {
return member(o, name).get(o);
}
private void set(Object o, String name, Object value) throws Exception {
member(o, name).set(o, value);
}
private void invoke(Object o, String name) throws Exception {
Method m = o.getClass().getDeclaredMethod(name);
m.setAccessible(true);
m.invoke(o);
}
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
start();
}
private Bitmap capture(Activity activity) throws Exception {
Bitmap[] copy = new Bitmap[1];
CountDownLatch done = new CountDownLatch(1);
int[] code = {-1};
runOnMainSync(
() -> {
View v = activity.getWindow().getDecorView();
copy[0] = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
PixelCopy.request(
activity.getWindow(),
copy[0],
r -> {
code[0] = r;
done.countDown();
},
new Handler(Looper.getMainLooper()));
});
if (!done.await(4, TimeUnit.SECONDS) || code[0] != PixelCopy.SUCCESS)
throw new AssertionError("PixelCopy failed: " + code[0]);
return copy[0];
}
private Bitmap pattern(int w, int h, int a, int b) {
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
bitmap.setHasAlpha(false);
Paint paint = new Paint();
paint.setShader(new LinearGradient(0, 0, w, h, a, b, Shader.TileMode.CLAMP));
new Canvas(bitmap).drawRect(0, 0, w, h, paint);
return bitmap;
}
private Object call(Object object, String name, Class<?>[] types, Object... args)
throws Exception {
Method method = object.getClass().getDeclaredMethod(name, types);
method.setAccessible(true);
return method.invoke(object, args);
}
private Bitmap compose(Object scene, Bitmap image, String theme, int color) throws Exception {
return (Bitmap)
call(
scene,
"compose",
new Class<?>[] {
int.class, int.class, Bitmap.class, String.class, float.class, int.class
},
960,
540,
image,
theme,
.20f,
color);
}
private Bitmap mask(Object scene, boolean image) throws Exception {
return (Bitmap)
call(
scene,
"makeMask",
new Class<?>[] {int.class, int.class, boolean.class},
960,
540,
image);
}
private void show(
View scene,
Bitmap frame,
Bitmap mask,
Bitmap previous,
Bitmap previousMask,
int color,
float mix) {
runOnMainSync(
() -> {
try {
set(scene, "frame", frame);
set(scene, "mask", mask);
set(scene, "previousFrame", previous);
set(scene, "previousMask", previousMask);
set(scene, "accent", color);
set(scene, "targetAccent", color);
set(scene, "mix", mix);
scene.invalidate();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
private int compare(Bitmap expected, Bitmap actual) {
int worst = 0;
// Avoid the ticking clock and app rows; compare the cinematic area through the real window.
for (int y = 150; y < actual.getHeight() * .6; y += 13)
for (int x = 20; x < actual.getWidth() - 20; x += 13) {
int a = expected.getPixel(x, y), b = actual.getPixel(x, y);
for (int shift : new int[] {0, 8, 16})
worst = Math.max(worst, Math.abs(((a >> shift) & 255) - ((b >> shift) & 255)));
}
return worst;
}
@Override
public void onStart() {
Bundle result = new Bundle();
StringBuilder report = new StringBuilder();
try {
Activity activity =
startActivitySync(
new Intent(Intent.ACTION_MAIN)
.setComponent(
new ComponentName("tv.luma.launcher", "tv.luma.launcher.MainActivity"))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
Thread.sleep(3000);
View scene = (View) get(activity, "scene");
runOnMainSync(
() -> {
try {
set(scene, "useTintMask", true);
invoke(scene, "requestRender");
} catch (Exception e) {
throw new RuntimeException(e);
}
});
Thread.sleep(700);
Object base = get(scene, "frame"), tint = get(scene, "mask");
if (base == null || tint == null) throw new AssertionError("initial image not loaded");
runOnMainSync(
() -> {
try {
call(scene, "accent", new Class<?>[] {int.class, boolean.class}, Color.CYAN, false);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
if (get(scene, "frame") != base || get(scene, "mask") != tint)
throw new AssertionError("accent changed cached wallpaper/mask");
report.append("PASS: changing accent reuses the same wallpaper and mask\n");
runOnMainSync(
() -> {
try {
invoke(scene, "pause");
} catch (Exception e) {
throw new RuntimeException(e);
}
});
Bitmap artwork = pattern(320, 180, Color.RED, Color.CYAN);
Bitmap darkArtwork = compose(scene, artwork, "alpine", Color.BLACK),
artMask = mask(scene, true);
Bitmap darkGradient = compose(scene, null, "aurora", Color.BLACK),
gradientMask = mask(scene, false);
for (boolean hasArt : new boolean[] {true, false})
for (int color : new int[] {0xFF5B4125, 0xFF193C6A, Color.WHITE}) {
Bitmap referenceImage =
compose(scene, hasArt ? artwork : null, hasArt ? "alpine" : "aurora", color);
show(scene, referenceImage, null, null, null, color, 1);
Thread.sleep(120);
Bitmap reference = capture(activity);
show(
scene,
hasArt ? darkArtwork : darkGradient,
hasArt ? artMask : gradientMask,
null,
null,
color,
1);
Thread.sleep(120);
Bitmap actual = capture(activity);
int worst = compare(reference, actual);
if (worst > 5)
throw new AssertionError(
"mask/reference pixels differ by " + worst + " artwork=" + hasArt);
report
.append("PASS: mask matches original background; artwork=")
.append(hasArt)
.append(" max difference ")
.append(worst)
.append('\n');
reference.recycle();
actual.recycle();
}
int color = 0xFF193C6A;
Bitmap before = compose(scene, artwork, "alpine", color),
after = compose(scene, null, "aurora", color);
for (float mix : new float[] {.25f, .5f, .75f}) {
Bitmap expected = Bitmap.createBitmap(960, 540, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(expected);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(before, 0, 0, paint);
paint.setAlpha((int) (255 * mix));
canvas.drawBitmap(after, 0, 0, paint);
show(scene, expected, null, null, null, color, 1);
Thread.sleep(120);
Bitmap reference = capture(activity);
show(scene, darkGradient, gradientMask, darkArtwork, artMask, color, mix);
Thread.sleep(120);
Bitmap actual = capture(activity);
int worst = compare(reference, actual);
if (worst > 5) throw new AssertionError("art crossfade differs by " + worst + " at " + mix);
report
.append("PASS: artwork crossfade ")
.append(mix)
.append(" matches original; max difference ")
.append(worst)
.append('\n');
reference.recycle();
actual.recycle();
}
runOnMainSync(
() -> {
try {
invoke(scene, "release");
} catch (Exception e) {
throw new RuntimeException(e);
}
});
for (String name : new String[] {"frame", "previousFrame", "mask", "previousMask"})
if (get(scene, name) != null) throw new AssertionError("retained background " + name);
report.append("PASS: source images and tint masks released on stop\n");
result.putString("stream", report.toString());
finish(Activity.RESULT_OK, result);
} catch (Throwable t) {
result.putString("stream", report + "FAIL: " + t + "\n");
finish(Activity.RESULT_CANCELED, result);
}
}
}