package tv.luma.launcher; import android.app.*; import android.content.*; import android.graphics.Rect; import android.net.NetworkCapabilities; import android.os.Bundle; import android.view.*; import android.widget.TextView; import java.lang.reflect.*; import java.util.*; /** Real menu/key dispatch regression, with layout/preferences restored in finally. */ public final class HomeActionsInstrumentation extends Instrumentation { private Activity activity; private Object get(Object target, String name) throws Exception { Field f = target.getClass().getDeclaredField(name); f.setAccessible(true); return f.get(target); } private void call(Object target, String name, Class type, Object arg) { runOnMainSync(() -> { try { Method m = type == null ? target.getClass().getDeclaredMethod(name) : target.getClass().getDeclaredMethod(name, type); m.setAccessible(true); if (type == null) m.invoke(target); else m.invoke(target, arg); } catch (Exception e) { throw new RuntimeException(e); } }); waitForIdleSync(); } private List order() throws Exception { return new ArrayList<>(((Map) get(activity, "tiles")).keySet()); } private void key(int key) { sendKeyDownUpSync(key); android.os.SystemClock.sleep(180); waitForIdleSync(); } private void check(boolean ok, String message) { if (!ok) throw new AssertionError(message); } private TextView text(View v, String prefix) { if (v instanceof TextView && ((TextView) v).getText().toString().startsWith(prefix)) return (TextView) v; if (v instanceof ViewGroup) for (int i = 0; i < ((ViewGroup) v).getChildCount(); i++) { TextView found = text(((ViewGroup) v).getChildAt(i), prefix); if (found != null) return found; } return null; } private void begin(String pkg) throws Exception { // A preceding tap-based check may leave Android in touch mode. A remote arrow exits it. key(KeyEvent.KEYCODE_DPAD_UP); View tile = (View) ((Map) get(activity, "tiles")).get(pkg); runOnMainSync(() -> { tile.requestFocus(); tile.performLongClick(); }); waitForIdleSync(); Thread.sleep(350); Dialog dialog = (Dialog) get(activity, "currentDialog"); TextView move = text(dialog.getWindow().getDecorView(), "Move in grid"); check(move != null, "move menu missing"); runOnMainSync(move::requestFocus); key(KeyEvent.KEYCODE_DPAD_CENTER); waitForIdleSync(); check((boolean) get(activity, "moving"), "remote OK did not enter move mode"); } private void move(int key, List expected, String pkg, int index) throws Exception { int old = expected.indexOf(pkg); Collections.swap(expected, old, index); key(key); waitForIdleSync(); check(order().equals(expected), "visible order did not follow arrow " + key); check(pkg.equals(get(activity, "selected")), "moving selection lost"); check(pkg.equals(activity.getCurrentFocus().getTag()), "focus left moving app"); } @Override public void onCreate(Bundle b) { super.onCreate(b); start(); } @Override public void onStart() { Bundle result = new Bundle(); StringBuilder report = new StringBuilder(); int status = Activity.RESULT_OK; SharedPreferences prefs = getTargetContext().getSharedPreferences("luma", 0); boolean hadOrder = prefs.contains("order"); String original = prefs.getString("order", ""); try { 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(2000); List initial = order(), expected = new ArrayList<>(initial); int columns = prefs.getInt("columns", 6); check(initial.size() > columns + 1, "not enough apps for vertical movement"); String pkg = initial.get(0); begin(pkg); move(KeyEvent.KEYCODE_DPAD_RIGHT, expected, pkg, 1); move(KeyEvent.KEYCODE_DPAD_DOWN, expected, pkg, columns + 1); move(KeyEvent.KEYCODE_DPAD_UP, expected, pkg, 1); move(KeyEvent.KEYCODE_DPAD_LEFT, expected, pkg, 0); move(KeyEvent.KEYCODE_DPAD_LEFT, expected, pkg, 0); move(KeyEvent.KEYCODE_DPAD_UP, expected, pkg, 0); move(KeyEvent.KEYCODE_DPAD_RIGHT, expected, pkg, 1); key(KeyEvent.KEYCODE_BACK); check(order().equals(initial), "Back did not restore order"); check(!(boolean) get(activity, "moving"), "Back did not exit move mode"); report.append("PASS: remote menu entry, all four arrows, bounds, focus, Back cancellation\n"); begin(pkg); expected = new ArrayList<>(initial); move(KeyEvent.KEYCODE_DPAD_DOWN, expected, pkg, columns); key(KeyEvent.KEYCODE_DPAD_CENTER); check(!(boolean) get(activity, "moving"), "OK did not save"); runOnMainSync(() -> { callActivityOnPause(activity); callActivityOnStop(activity); callActivityOnStart(activity); callActivityOnResume(activity); }); Thread.sleep(1200); check(order().equals(expected), "saved move lost on resume"); report.append("PASS: OK saves order across stop/start\n"); View network = (View) get(activity, "networkButton"); View input = (View) get(activity, "inputButton"); View settings = (View) get(activity, "settingsButton"); List actual = (List) get(activity, "inputSources"); call(activity, "applyInputSources", List.class, Collections.singletonList(new TvInputs.Source("test", "Test input", "Connected"))); Thread.sleep(250); Rect nr = new Rect(), ir = new Rect(), sr = new Rect(); network.getGlobalVisibleRect(nr); input.getGlobalVisibleRect(ir); settings.getGlobalVisibleRect(sr); check(!ir.isEmpty() && !nr.isEmpty() && !sr.isEmpty(), "header buttons clipped"); check(ir.right < nr.left && nr.right < sr.left, "header buttons overlap"); runOnMainSync(input::requestFocus); key(KeyEvent.KEYCODE_DPAD_RIGHT); check(network.hasFocus(), "input to network focus failed"); key(KeyEvent.KEYCODE_DPAD_RIGHT); check(settings.hasFocus(), "network to settings focus failed"); key(KeyEvent.KEYCODE_DPAD_LEFT); check(network.hasFocus(), "settings to network focus failed"); call(activity, "applyInputSources", List.class, Collections.emptyList()); check(input.getVisibility() == View.GONE, "input not hidden without capability"); call(activity, "applyInputSources", List.class, actual); report.append("PASS: simulated physical-input header has separate bounds and D-pad focus\n"); Object monitor = get(activity, "networkStatus"); call(monitor, "show", NetworkCapabilities.class, null); check(network.getContentDescription().toString().startsWith("Network disconnected"), "offline status"); call(monitor, "stop", null, null); check(get(monitor, "callback") == null, "callback retained while stopped"); call(monitor, "start", null, null); check(get(monitor, "callback") != null, "callback not restored"); check(!network.getContentDescription().toString().startsWith("Network disconnected"), "live network did not recover after offline state"); check(text(activity.getWindow().getDecorView(), "Personalize") == null, "duplicate footer remains"); report.append("PASS: offline presentation, live-network recovery, callback stop/restart, footer\n"); } catch (Throwable t) { status = Activity.RESULT_CANCELED; report.append("FAIL: ").append(t).append('\n'); } finally { if (activity != null) { try { if ((boolean) get(activity, "moving")) key(KeyEvent.KEYCODE_BACK); } catch (Exception e) { status = Activity.RESULT_CANCELED; report.append("FAIL: move cleanup ").append(e); } } SharedPreferences.Editor edit = prefs.edit(); if (hadOrder) edit.putString("order", original); else edit.remove("order"); if (!edit.commit()) { status = Activity.RESULT_CANCELED; report.append("FAIL: order restore\n"); } if (activity != null) { try { call(activity, "renderApps", null, null); } catch (Exception e) { status = Activity.RESULT_CANCELED; report.append("FAIL: cleanup ").append(e); } } } result.putString("stream", report.toString()); finish(status, result); } }