package tv.luma.launcher.tests; import android.animation.ValueAnimator; import android.app.*; import android.content.*; import android.graphics.Color; import android.os.Bundle; import java.lang.reflect.*; /** Passing focus colors must not start image jobs or escape a paused scene. */ public final class AccentSchedulingInstrumentation extends Instrumentation { private Object scene; private Object get(String name) throws Exception { Field f = scene.getClass().getDeclaredField(name); f.setAccessible(true); return f.get(scene); } private void call(String name, Class[] types, Object... args) { runOnMainSync( () -> { try { Method m = scene.getClass().getDeclaredMethod(name, types); m.setAccessible(true); m.invoke(scene, args); } catch (Exception e) { throw new RuntimeException(e); } }); } private void color(int color, boolean animate) { call("accent", new Class[] {int.class, boolean.class}, color, animate); } private void settled() throws Exception { long until = android.os.SystemClock.uptimeMillis() + 4000; boolean[] ready = {false}; while (android.os.SystemClock.uptimeMillis() < until) { runOnMainSync( () -> { try { ValueAnimator a = (ValueAnimator) get("animator"), c = (ValueAnimator) get("colorAnimator"); ready[0] = get("frame") != null && !(boolean) get("dirty") && !(boolean) get("accentPending") && (a == null || !a.isRunning()) && (c == null || !c.isRunning()); } catch (Exception e) { throw new RuntimeException(e); } }); if (ready[0]) return; Thread.sleep(20); } throw new AssertionError("scene did not settle"); } @Override public void onCreate(Bundle args) { super.onCreate(args); start(); } @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); Field f = activity.getClass().getDeclaredField("scene"); f.setAccessible(true); scene = f.get(activity); for (boolean memory : new boolean[] {false, true}) { call("setMemorySaver", new Class[] {boolean.class}, memory); call("setMotion", new Class[] {boolean.class}, false); color(Color.WHITE, false); settled(); Object frame = get("frame"), mask = get("mask"); int generation = (int) get("generation"); call("setMotion", new Class[] {boolean.class}, true); for (int color : new int[] {Color.RED, Color.CYAN, Color.GREEN}) { color(color, true); Thread.sleep(200); if (get("frame") != frame || (int) get("generation") != generation) throw new AssertionError("passing selection composed a background"); } settled(); if ((int) get("accent") != Color.GREEN || (int) get("targetAccent") != Color.GREEN) throw new AssertionError("latest selected color was lost"); if (memory && (get("frame") != frame || get("mask") != mask)) throw new AssertionError("memory saver rebuilt textures for an accent"); if (!memory && (get("frame") == frame || (int) get("generation") != generation + 1)) throw new AssertionError("expected exactly one final background composition"); report .append("PASS: coalesced passing colors, retained final color; memorySaver=") .append(memory) .append('\n'); color(Color.WHITE, false); settled(); Object settledFrame = get("frame"), settledAnimator = get("colorAnimator"); int settledGeneration = (int) get("generation"); color(Color.RED, true); Thread.sleep(200); color(Color.WHITE, true); settled(); if (get("frame") != settledFrame || (int) get("generation") != settledGeneration || get("colorAnimator") != settledAnimator) throw new AssertionError("returning to the displayed color started redundant work"); report .append( "PASS: returning to displayed color needs no composition or color animation;" + " memorySaver=") .append(memory) .append('\n'); color(Color.MAGENTA, true); call("pause", new Class[0]); generation = (int) get("generation"); Thread.sleep(350); if ((boolean) get("accentPending") || (int) get("generation") != generation) throw new AssertionError("delayed accent escaped pause"); call("resume", new Class[0]); settled(); if ((int) get("accent") != Color.MAGENTA) throw new AssertionError("resume lost pending color"); call("setMotion", new Class[] {boolean.class}, false); color(Color.BLUE, true); if ((boolean) get("accentPending") || (int) get("accent") != Color.BLUE) throw new AssertionError("reduced motion deferred the color"); settled(); report .append( "PASS: pause cancels pending work, resume restores, reduced motion immediate;" + " memorySaver=") .append(memory) .append('\n'); } result.putString("stream", report.toString()); finish(Activity.RESULT_OK, result); } catch (Throwable error) { result.putString("stream", report + "FAIL: " + error + "\n"); finish(Activity.RESULT_CANCELED, result); } } }