Luma/tests/device/JankInstrumentation.java

155 lines
5.7 KiB
Java

package tv.luma.launcher.tests;
import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
/**
* Current gfxinfo jank with original or matched app pairs; runtime isolations never change
* defaults.
*/
public final class JankInstrumentation extends Instrumentation {
private String mode = "matched", memory = "keep";
private Object get(Object o, String n) throws Exception {
Field f = o.getClass().getDeclaredField(n);
f.setAccessible(true);
return f.get(o);
}
private void call(Object o, String n, Class<?>[] types, Object... args) throws Exception {
Method m = o.getClass().getDeclaredMethod(n, types);
m.setAccessible(true);
runOnMainSync(
() -> {
try {
m.invoke(o, args);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
private String shell(String cmd) throws Exception {
try (ParcelFileDescriptor fd = getUiAutomation().executeShellCommand(cmd);
InputStream in = new ParcelFileDescriptor.AutoCloseInputStream(fd);
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
byte[] b = new byte[8192];
int n;
while ((n = in.read(b)) != -1) out.write(b, 0, n);
return out.toString("UTF-8");
}
}
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
if (b != null) {
mode = b.getString("mode", "matched");
memory = b.getString("memory", "keep");
}
start();
}
@Override
public void onStart() {
Bundle result = new Bundle();
StringBuilder report = new StringBuilder();
int status = Activity.RESULT_OK;
SharedPreferences p = getTargetContext().getSharedPreferences("luma", 0);
Map<String, ?> old = new HashMap<>(p.getAll());
try {
SharedPreferences.Editor edit = p.edit().putBoolean("media", false);
if (!mode.equals("original"))
edit.putString(
"order", "com.plexapp.android,com.spotify.tv.android," + p.getString("order", ""));
if (mode.equals("fixed")) edit.putInt("accent", 0xff5b4125);
else edit.putInt("accent", 0);
if (!edit.commit()) throw new AssertionError("test preferences");
Activity a =
startActivitySync(
new Intent(Intent.ACTION_MAIN)
.setComponent(
new ComponentName("tv.luma.launcher", "tv.luma.launcher.MainActivity"))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
Thread.sleep(2500);
Object scene = get(a, "scene");
if (!memory.equals("keep"))
call(scene, "setMemorySaver", new Class<?>[] {boolean.class}, memory.equals("on"));
call(scene, "configure", new Class<?>[] {String.class, float.class}, "alpine", .2f);
call(scene, "setMotion", new Class<?>[] {boolean.class}, true);
Map<?, ?> tiles = (Map<?, ?>) get(a, "tiles");
View[] items = tiles.values().toArray(new View[0]);
if (items.length < 2) throw new AssertionError("insufficient app tiles");
if (!mode.equals("original")
&& (!items[0].getTag().equals("com.plexapp.android")
|| !items[1].getTag().equals("com.spotify.tv.android")))
throw new AssertionError("matched pair unavailable");
if (mode.equals("hidden")) runOnMainSync(() -> ((View) scene).setVisibility(View.INVISIBLE));
if (mode.equals("noshadow"))
runOnMainSync(
() -> {
for (View tile : items)
try {
((Paint) get(tile, "shadowPaint"))
.setColorFilter(
new PorterDuffColorFilter(Color.TRANSPARENT, PorterDuff.Mode.SRC_IN));
} catch (Exception e) {
throw new RuntimeException(e);
}
});
runOnMainSync(items[0]::requestFocus);
Thread.sleep(900);
report
.append("mode=")
.append(mode)
.append(" memorySaver=")
.append(get(scene, "useTintMask"))
.append(" pair=")
.append(items[0].getTag())
.append(',')
.append(items[1].getTag())
.append('\n');
shell("dumpsys gfxinfo tv.luma.launcher reset");
Trace.beginSection("LumaJankWorkload");
long start = SystemClock.uptimeMillis();
for (int i = 0; i < 60; i++) {
long due = start + i * 200L;
long wait = due - SystemClock.uptimeMillis();
if (wait > 0) Thread.sleep(wait);
sendKeyDownUpSync(i % 2 == 0 ? KeyEvent.KEYCODE_DPAD_RIGHT : KeyEvent.KEYCODE_DPAD_LEFT);
}
Thread.sleep(500);
Trace.endSection();
report
.append("injected=60 elapsedMs=")
.append(SystemClock.uptimeMillis() - start)
.append('\n');
report.append(shell("dumpsys gfxinfo tv.luma.launcher framestats"));
report.append("\nPASS: current jank capture completed\n");
} catch (Throwable t) {
report.append("FAIL: ").append(t).append('\n');
status = Activity.RESULT_CANCELED;
} finally {
SharedPreferences.Editor edit = p.edit();
for (String k : new String[] {"order", "media", "accent"}) {
Object v = old.get(k);
if (!old.containsKey(k)) edit.remove(k);
else if (v instanceof Boolean) edit.putBoolean(k, (Boolean) v);
else if (v instanceof Integer) edit.putInt(k, (Integer) v);
else edit.putString(k, (String) v);
}
if (!edit.commit()) {
report.append("FAIL: preferences not restored\n");
status = Activity.RESULT_CANCELED;
}
}
result.putString("stream", report.toString());
finish(status, result);
}
}