Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

tools: bench.py takes --app and never leaves Home for the app chooser

Andrew Borisenko4156c4d5cc80
diff --git a/tests/test_bench_other_app.py b/tests/test_bench_other_app.py
new file mode 100644
index 000000000..8dd8d929b
--- /dev/null
+++ b/tests/test_bench_other_app.py
@@ -0,0 +1,84 @@
+"""bench.py leaves Home for a real app: --app when given, else the TV's Settings, else a leanback app.
+
+A plain LAUNCHER lookup answers with android's ResolverActivity on some sets (a TCL C755, a HOTACK L018
+projector), so the cold start and the return to Home were measured against the app chooser dialog.
+tools/trace.py already picks its other app this way; these checks hold bench.py to the same rule.
+
+The TV is faked, so this needs no TV and no adb_shell.
+
+Run from the repository root: python3 -m unittest discover -s tests -v
+"""
+import os
+import re
+import tempfile
+import unittest
+from unittest import mock
+
+from tools import bench
+from tests.test_bench_counts import FakeTV, run
+
+
+class AnsweringTV:
+ """A TV whose shell answers each lookup from a fixed table."""
+
+ def __init__(self, answers):
+ self._other_app = None
+ self.answers = answers
+
+ def home_component(self):
+ return "com.gathin.tvhome/.MainActivity"
+
+ def package_of(self, component):
+ return component.split("/", 1)[0]
+
+ def shell(self, command):
+ return next((v for k, v in self.answers.items() if k in command), "")
+
+
+class DefaultOtherAppTest(unittest.TestCase):
+ def test_settings_first(self):
+ tv = AnsweringTV({"android.settings.SETTINGS": "priority=0\ncom.android.tv.settings/.MainSettings\n",
+ "LEANBACK_LAUNCHER": "org.videolan.vlc/.StartActivity\n"})
+ self.assertEqual(bench.other_app(tv), "com.android.tv.settings/.MainSettings")
+
+ def test_never_the_chooser_or_the_launcher(self):
+ tv = AnsweringTV({"android.settings.SETTINGS": "android/com.android.internal.app.ResolverActivity\n",
+ "category.LAUNCHER": "android/com.android.internal.app.ResolverActivity\n",
+ "LEANBACK_LAUNCHER": "com.gathin.tvhome/.MainActivity\norg.videolan.vlc/.StartActivity\n"})
+ self.assertEqual(bench.other_app(tv), "org.videolan.vlc/.StartActivity")
+
+
+def run_and_keep_the_tv(argv):
+ """tests.test_bench_counts.run(), plus the fake TVs it built, to read what the run asked of them."""
+ made = []
+ init = FakeTV.__init__
+
+ def keep(self, *args, **kwargs):
+ init(self, *args, **kwargs)
+ made.append(self)
+
+ with mock.patch.object(FakeTV, "__init__", keep):
+ status, out, err = run(argv)
+ return status, err, made
+
+
+class AppOptionTest(unittest.TestCase):
+ def test_the_given_app_is_the_one_left_home_for(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ status, err, made = run_and_keep_the_tv(
+ ["--host", "192.0.2.7", "--out-dir", tmp, "--app", "org.videolan.vlc/.StartActivity",
+ "--keys", "1", "--series", "1", "--cold", "1", "--returns", "1", "--idle", "1"])
+ self.assertEqual(status, 0, err)
+ started = {m.group(1) for c in made[0].commands for m in [re.search(r"^am start.* -n (\S+)", c)] if m}
+ self.assertEqual(started, {"org.videolan.vlc/.StartActivity", "com.gathin.tvhome/.MainActivity"})
+
+ def test_a_component_without_a_slash_is_refused_before_the_tv(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ status, err, made = run_and_keep_the_tv(["--host", "192.0.2.7", "--out-dir", tmp,
+ "--app", "org.videolan.vlc"])
+ self.assertEqual((status, made, os.listdir(tmp)), (2, [], []), err)
+ self.assertIn("--app needs package/activity, not 'org.videolan.vlc'", err)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/bench.py b/tools/bench.py
index a47b57a1e..490b17dc2 100644
--- a/tools/bench.py
+++ b/tools/bench.py
@@ -18,6 +18,10 @@ platform tools are not installed by default.
Usage:
TV_HOST=192.168.1.50 python tools/bench.py --keys 36 --series 3
TV_HOST=192.168.1.50 python tools/bench.py --keys 36 --series 3 --out-dir my-results
+ TV_HOST=192.168.1.50 python tools/bench.py --app org.videolan.vlc/.StartActivity
+
+--app names the app the cold start and the return to Home leave Home for. Without it, the TV's
+Settings, else the first leanback app that is not the launcher.
The output is a summary plus the raw per-key numbers, so a run can be compared with another one or
re-analysed later.
@@ -50,6 +54,9 @@ KEYCODE_NAMES = {19: "KEYCODE_DPAD_UP", 20: "KEYCODE_DPAD_DOWN", 21: "KEYCODE_DP
22: "KEYCODE_DPAD_RIGHT", 66: "KEYCODE_ENTER", 3: "KEYCODE_HOME",
4: "KEYCODE_BACK"}
+# An activity as package/activity, e.g. org.videolan.vlc/.StartActivity (the same pattern as tools/trace.py).
+COMPONENT = re.compile(r"^[A-Za-z][A-Za-z0-9_.]*/[A-Za-z0-9_.$]+$")
+
class TV:
"""The TV, reached with tools/tv.py's client over one ADB connection.
@@ -300,20 +307,27 @@ def frame_rows(framestats):
def other_app(tv):
- """An app to send the TV to, so the launcher really starts cold.
+ """An app to send the TV to, so the launcher really starts cold: --app when given, else the TV's
+ Settings, else the first leanback app that isn't the launcher. tools/trace.py picks its own the same way.
- The Settings app is found by the category it answers, not by a hardcoded activity: the
- activity name differs between manufacturers (com.android.tv.settings/.MainSettings on
- Google TV, others elsewhere), and a fixed name simply does not exist on some sets.
+ Settings is found by the action it answers, not by a hardcoded activity: the activity name differs
+ between manufacturers (com.android.tv.settings/.MainSettings on Google TV, others elsewhere).
+ Never the chooser: a plain LAUNCHER lookup resolves to android's ResolverActivity, a dialog, on a
+ TCL C755 and a HOTACK L018 projector, and the run then measured against that dialog.
"""
if tv._other_app:
return tv._other_app
- out = tv.shell(
- "cmd package resolve-activity --brief -a android.intent.action.MAIN "
- "-c android.intent.category.LAUNCHER")
- lines = [line.strip() for line in out.splitlines() if "/" in line]
- tv._other_app = lines[-1] if lines else None
- return tv._other_app
+ launcher = tv.package_of(tv.home_component() or "")
+ candidates = tv.shell("cmd package resolve-activity --brief -a android.settings.SETTINGS").splitlines()
+ candidates += tv.shell("cmd package query-activities --brief -a android.intent.action.MAIN "
+ "-c android.intent.category.LEANBACK_LAUNCHER").splitlines()
+ for line in candidates:
+ line = line.strip()
+ if COMPONENT.match(line) and not line.startswith(("android/", launcher + "/")) \
+ and "Resolver" not in line:
+ tv._other_app = line
+ return line
+ return None
def accessibility_services(tv, package):
@@ -553,7 +567,11 @@ def main():
parser.add_argument("--idle", type=int, default=60, help="idle seconds")
parser.add_argument("--out-dir", type=out_dir, default="bench-results",
metavar="DIR", help="where to write the results (default bench-results)")
+ parser.add_argument("--app", metavar="COMPONENT",
+ help="the app to leave Home for, as package/activity (default: found on the TV)")
args = parser.parse_args()
+ if args.app and not COMPONENT.match(args.app):
+ parser.error(f"--app needs package/activity, not {args.app!r}")
# Each of these five is a count of what the run measures, and zero of one measures nothing there
# while the run still exits 0: --series 0 and --keys 0 wrote a result.json whose key numbers are
# all null, and --cold 0 / --returns 0 the same for their fields, which in that file cannot be
@@ -579,6 +597,7 @@ def main():
tv = TV(args.host, args.port)
atexit.register(tv.close)
+ tv._other_app = args.app
component = tv.home_component()
if not component:
raise SystemExit("no component answers HOME on this TV")