package tv.luma.launcher.tests; import android.app.*; import android.content.*; import android.os.*; import android.view.*; import java.lang.reflect.*; import java.util.concurrent.atomic.AtomicInteger; /** Repeated interaction must not dispatch identical window attributes and request relayout. */ public final class WindowFlagsInstrumentation extends Instrumentation { @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 existed = prefs.contains("saverTimeout"); int original = prefs.getInt("saverTimeout", 10); Activity activity = null; Window.Callback callback = null; try { prefs.edit().putInt("saverTimeout", 10).commit(); 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(1800); final Activity a = activity; callback = a.getWindow().getCallback(); final Window.Callback delegate = callback; AtomicInteger changes = new AtomicInteger(); Window.Callback observer = (Window.Callback) Proxy.newProxyInstance( Window.Callback.class.getClassLoader(), new Class[] {Window.Callback.class}, (proxy, method, args) -> { if (method.getName().equals("onWindowAttributesChanged")) changes.incrementAndGet(); try { return method.invoke(delegate, args); } catch (InvocationTargetException e) { throw e.getCause(); } }); runOnMainSync(() -> a.getWindow().setCallback(observer)); for (int i = 0; i < 20; i++) runOnMainSync(a::onUserInteraction); if (changes.get() != 0) throw new AssertionError( "repeated interaction dispatched " + changes.get() + " redundant window updates"); report.append("PASS: twenty interactions caused zero redundant window-attribute updates\n"); prefs.edit().putInt("saverTimeout", 0).commit(); runOnMainSync(a::onUserInteraction); if (changes.get() != 1 || (a.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) != 0) throw new AssertionError("screensaver Off did not release keep-screen-on once"); runOnMainSync(a::onUserInteraction); if (changes.get() != 1) throw new AssertionError("repeated Off interaction updated the window"); prefs.edit().putInt("saverTimeout", 10).commit(); runOnMainSync(a::onUserInteraction); if (changes.get() != 2 || (a.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) == 0) throw new AssertionError("screensaver timer did not reacquire keep-screen-on once"); runOnMainSync(() -> callActivityOnPause(a)); if ((a.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) != 0) throw new AssertionError("pause retained screen-on flag"); runOnMainSync(() -> callActivityOnResume(a)); if ((a.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) == 0) throw new AssertionError("resume did not restore timer screen-on flag"); report.append("PASS: timeout changes and pause/resume update keep-screen-on correctly\n"); } catch (Throwable error) { report.append("FAIL: ").append(error).append('\n'); status = Activity.RESULT_CANCELED; } finally { if (activity != null && callback != null) { final Activity a = activity; final Window.Callback c = callback; runOnMainSync(() -> a.getWindow().setCallback(c)); } SharedPreferences.Editor edit = prefs.edit(); if (existed) edit.putInt("saverTimeout", original); else edit.remove("saverTimeout"); if (!edit.commit()) { report.append("FAIL: timeout not restored\n"); status = Activity.RESULT_CANCELED; } } result.putString("stream", report.toString()); finish(status, result); } }