Reuse Home tiles and Plex artwork to reduce allocation
This commit is contained in:
parent
5f035d909a
commit
ac0a892c5f
14 changed files with 741 additions and 40 deletions
145
tests/device/GridReuseInstrumentation.java
Normal file
145
tests/device/GridReuseInstrumentation.java
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
package tv.luma.launcher.tests;
|
||||
|
||||
import android.app.*;
|
||||
import android.content.*;
|
||||
import android.os.*;
|
||||
import android.view.*;
|
||||
import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Exercises user layout changes and focus callbacks against a reused grid, restoring preferences.
|
||||
*/
|
||||
public final class GridReuseInstrumentation extends Instrumentation {
|
||||
private Activity activity;
|
||||
|
||||
private Object get(Object o, String name) throws Exception {
|
||||
Field f = o.getClass().getDeclaredField(name);
|
||||
f.setAccessible(true);
|
||||
return f.get(o);
|
||||
}
|
||||
|
||||
private Map<String, View> tiles() throws Exception {
|
||||
return new LinkedHashMap<>((Map<String, View>) get(activity, "tiles"));
|
||||
}
|
||||
|
||||
private void render() throws Exception {
|
||||
Method m = activity.getClass().getDeclaredMethod("renderApps");
|
||||
m.setAccessible(true);
|
||||
runOnMainSync(
|
||||
() -> {
|
||||
try {
|
||||
m.invoke(activity);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
Thread.sleep(150);
|
||||
waitForIdleSync();
|
||||
}
|
||||
|
||||
private void check(boolean value, String message) {
|
||||
if (!value) throw new AssertionError(message);
|
||||
}
|
||||
|
||||
@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);
|
||||
Map<String, ?> original = new HashMap<>(prefs.getAll());
|
||||
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(2500);
|
||||
Map<String, View> before = tiles();
|
||||
check(before.size() > 2, "insufficient apps");
|
||||
String first = before.keySet().iterator().next();
|
||||
prefs.edit().putString("labels", "never").commit();
|
||||
render();
|
||||
Map<String, View> after = tiles();
|
||||
for (String p : before.keySet()) {
|
||||
check(before.get(p) == after.get(p), "label setting rebuilt tiles");
|
||||
check(
|
||||
((View) get(after.get(p), "label")).getVisibility() == View.GONE,
|
||||
"labels still visible");
|
||||
}
|
||||
View last = new ArrayList<>(after.values()).get(after.size() - 1);
|
||||
runOnMainSync(last::requestFocus);
|
||||
Thread.sleep(350);
|
||||
check(
|
||||
last.getTag().equals(get(activity, "selected")), "reused focus callback lost selection");
|
||||
// Stop halfway through a focus animation; a reused focused tile must resume at its final
|
||||
// scale.
|
||||
runOnMainSync(
|
||||
() -> {
|
||||
after.get(first).requestFocus();
|
||||
callActivityOnPause(activity);
|
||||
callActivityOnStop(activity);
|
||||
});
|
||||
check(
|
||||
Math.abs(after.get(first).getScaleX() - 1.065f) < .001f,
|
||||
"interrupted lift left wrong scale");
|
||||
runOnMainSync(
|
||||
() -> {
|
||||
callActivityOnStart(activity);
|
||||
callActivityOnResume(activity);
|
||||
});
|
||||
Thread.sleep(800);
|
||||
report.append(
|
||||
"PASS: label updates reuse views; rebound callbacks retain selection; interrupted focus"
|
||||
+ " lift normalizes\n");
|
||||
Set<String> hidden = new HashSet<>(prefs.getStringSet("hidden", Collections.emptySet()));
|
||||
hidden.add(first);
|
||||
prefs.edit().putStringSet("hidden", hidden).commit();
|
||||
render();
|
||||
check(
|
||||
!tiles().containsKey(first) && tiles().size() == before.size() - 1,
|
||||
"hidden app remained in reused grid");
|
||||
List<String> reverse = new ArrayList<>(before.keySet());
|
||||
Collections.reverse(reverse);
|
||||
SharedPreferences.Editor edit = prefs.edit().putString("order", String.join(",", reverse));
|
||||
if (original.containsKey("hidden"))
|
||||
edit.putStringSet("hidden", new HashSet<>((Set<String>) original.get("hidden")));
|
||||
else edit.remove("hidden");
|
||||
edit.commit();
|
||||
render();
|
||||
check(new ArrayList<>(tiles().keySet()).equals(reverse), "reordering ignored");
|
||||
Map<String, View> reordered = tiles();
|
||||
int cols = prefs.getInt("columns", 6);
|
||||
prefs.edit().putInt("columns", cols == 6 ? 7 : 6).commit();
|
||||
render();
|
||||
check(tiles().get(first) != reordered.get(first), "column change reused stale dimensions");
|
||||
report.append("PASS: hiding, reordering, and column changes rebuild the correct grid\n");
|
||||
} catch (Throwable error) {
|
||||
report.append("FAIL: ").append(error).append('\n');
|
||||
status = Activity.RESULT_CANCELED;
|
||||
} finally {
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
for (String k : new String[] {"labels", "hidden", "order", "columns"}) {
|
||||
Object v = original.get(k);
|
||||
if (!original.containsKey(k)) edit.remove(k);
|
||||
else if (v instanceof String) edit.putString(k, (String) v);
|
||||
else if (v instanceof Integer) edit.putInt(k, (Integer) v);
|
||||
else edit.putStringSet(k, new HashSet<>((Set<String>) v));
|
||||
}
|
||||
if (!edit.commit()) {
|
||||
status = Activity.RESULT_CANCELED;
|
||||
report.append("FAIL: preferences not restored\n");
|
||||
} else report.append("PASS: original layout preferences restored\n");
|
||||
}
|
||||
result.putString("stream", report.toString());
|
||||
finish(status, result);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue