Skip to content

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

tests: a card the launcher never reaches is named, not only the file it sits in

xbsheng4e4cd45c6bde
diff --git a/tests/test_addon_module_cards.py b/tests/test_addon_module_cards.py
index d145daa15..337452f52 100644
--- a/tests/test_addon_module_cards.py
+++ b/tests/test_addon_module_cards.py
@@ -18,6 +18,18 @@ The limits and the alphabet are read out of `Cards.java` instead of being copied
lengths are read out of the spec's own table, so a card this test passes is one the launcher takes,
and the how-to and the contract are held to the same numbers.
+Half of the contract was still read by nothing: the card file can be perfect and the launcher never
+reaches it. `docs/specs/card.md`'s "Rules for the declaration" say the filter and the `meta-data` sit
+on the activity that already carries `LEANBACK_LAUNCHER`, that it is `exported` and enabled, and that a
+package declares one card - and `Cards.read()` refuses all of that in words a TV owner never sees
+(`card activity is not exported and enabled`, `card activity has no LEANBACK_LAUNCHER intent filter`,
+`no <meta-data android:name="com.gathin.tvhome.card" android:resource=...>`, `declares a second card`),
+one `TvHomeCards` line per Home. So an add-on could ship the card file this file reads, point its
+`meta-data` at another activity, drop `android:exported`, or leave the `DOCK_CARD` filter off the
+activity the launcher queries and the card would never appear - with every test here green. This file
+now reads each add-on's `AndroidManifest.xml` beside its card and holds the declaration to those rules,
+so a folder whose manifest and card disagree is named before a TV owner finds it.
+
Run from the repository root: python3 -m unittest discover -s tests -v
"""
import re
@@ -34,6 +46,16 @@ SPEC = ROOT / "docs" / "specs" / "card.md"
FIELDS = ("spec", "label", "caption", "accent")
+# The three names Cards.discover() and Cards.read() look for, in the manifest rather than in the card.
+MANIFEST_ANDROID = "{http://schemas.android.com/apk/res/android}"
+CARD_META_DATA = "com.gathin.tvhome.card" # Cards.META_DATA
+DOCK_CARD = "com.gathin.tvhome.category.DOCK_CARD" # Cards.CATEGORY
+MAIN_ACTION = "android.intent.action.MAIN"
+LEANBACK_CATEGORY = "android.intent.category.LEANBACK_LAUNCHER"
+# The elements PackageManager may hand back from its MAIN + DOCK_CARD query: an <activity> and the
+# <activity-alias> that stands for one. A meta-data anywhere else is never read.
+ACTIVITY_TAGS = ("activity", "activity-alias")
+
def card_files(root=ADDONS):
"""Every module card the add-ons in `root` ship, newest path order."""
@@ -298,5 +320,269 @@ class CardReaderTest(unittest.TestCase):
self.assertEqual(self.check('<card spec="1" label="Retro" caption="NES games" accent="#E8A33D" />'), [])
+# ---- the declaration the card hangs off ----------------------------------------------------------
+
+def manifest_of(card_path):
+ """The AndroidManifest.xml of the app that ships the card: addons/<name>/app/src/main/."""
+ return Path(card_path).parents[2] / "AndroidManifest.xml"
+
+
+def intent_filters(element):
+ """(actions, categories) for each <intent-filter> directly under `element`."""
+ found = []
+ for f in element.findall("intent-filter"):
+ actions = {a.get(MANIFEST_ANDROID + "name") for a in f.findall("action")}
+ categories = {c.get(MANIFEST_ANDROID + "name") for c in f.findall("category")}
+ found.append((actions, categories))
+ return found
+
+
+def carries(element, action, category):
+ """Whether one filter of `element` holds both the action and the category."""
+ return any(action in actions and category in categories
+ for actions, categories in intent_filters(element))
+
+
+def card_carriers(application):
+ """Every element under <application> that carries the card meta-data, in file order."""
+ found = []
+ for element in application:
+ for meta in element.findall("meta-data"):
+ if meta.get(MANIFEST_ANDROID + "name") == CARD_META_DATA:
+ found.append(element)
+ break
+ return found
+
+
+def named(element):
+ """What a message should call an element: its android:name, or its tag when it has none."""
+ return element.get(MANIFEST_ANDROID + "name") or "<%s>" % element.tag
+
+
+def declaration_problems(card_path):
+ """Why the launcher never reads the card at `card_path`, going by the manifest beside it.
+
+ [] when the manifest declares it the way `docs/specs/card.md`'s "Rules for the declaration" and
+ `Cards.read()` ask: the meta-data and the DOCK_CARD filter on the activity that also carries
+ LEANBACK_LAUNCHER, that activity exported and enabled, and the resource this folder ships.
+ """
+ card_path = Path(card_path)
+ manifest = manifest_of(card_path)
+ where = manifest.relative_to(ROOT) if str(manifest).startswith(str(ROOT)) else manifest
+ if not manifest.is_file():
+ return ["%s is not there, so nothing installs the card" % where]
+ try:
+ root = ET.parse(str(manifest)).getroot()
+ except ET.ParseError as e:
+ return ["%s is not XML the build takes: %s" % (where, e)]
+ application = root.find("application")
+ if application is None:
+ return ["%s has no <application>, so it declares no card" % where]
+
+ reasons = []
+ if application.get(MANIFEST_ANDROID + "enabled") == "false":
+ reasons.append("the <application> is android:enabled=\"false\", and Cards.read() answers "
+ "'card activity is not exported and enabled'")
+ for meta in application.findall("meta-data"):
+ if meta.get(MANIFEST_ANDROID + "name") == CARD_META_DATA:
+ reasons.append("the card meta-data sits on <application>, and Cards.read() loads it with "
+ "loadXmlMetaData() off the activity that answers the card query")
+
+ carriers = card_carriers(application)
+ wanted = "@xml/" + card_path.stem
+ if not carriers:
+ return reasons + ["no element carries <meta-data android:name=\"%s\">, so the card this folder "
+ "ships is never read" % CARD_META_DATA]
+ if len(carriers) > 1:
+ reasons.append("%d elements carry the card meta-data (%s), and Cards.read() answers "
+ "'declares a second card; one per package is read' for the second"
+ % (len(carriers), ", ".join(named(e) for e in carriers)))
+
+ for element in carriers:
+ name = named(element)
+ if element.tag not in ACTIVITY_TAGS:
+ reasons.append("<%s> %s carries the card meta-data, and PackageManager's card query only "
+ "returns activities" % (element.tag, name))
+ continue
+ if element.get(MANIFEST_ANDROID + "exported") != "true":
+ reasons.append("%s is not android:exported=\"true\", and Cards.read() answers 'card activity "
+ "is not exported and enabled'" % name)
+ if element.get(MANIFEST_ANDROID + "enabled") == "false":
+ reasons.append("%s is android:enabled=\"false\", so Cards.read() answers 'card activity is "
+ "not exported and enabled'" % name)
+ if not carries(element, MAIN_ACTION, DOCK_CARD):
+ reasons.append("%s has no MAIN + %s intent-filter, and Cards.discover() finds a card by "
+ "querying exactly that" % (name, DOCK_CARD))
+ if not carries(element, MAIN_ACTION, LEANBACK_CATEGORY):
+ reasons.append("%s has no MAIN + %s intent-filter, so it is not a tile, and Cards.read() "
+ "answers 'card activity has no LEANBACK_LAUNCHER intent filter'"
+ % (name, LEANBACK_CATEGORY))
+ for meta in element.findall("meta-data"):
+ if meta.get(MANIFEST_ANDROID + "name") != CARD_META_DATA:
+ continue
+ resource = meta.get(MANIFEST_ANDROID + "resource")
+ if resource is None:
+ reasons.append("the meta-data on %s uses android:value=%r, and Cards.read() reads "
+ "android:resource only" % (name, meta.get(MANIFEST_ANDROID + "value")))
+ elif resource != wanted:
+ reasons.append("the meta-data on %s names %s, and this folder ships %s"
+ % (name, resource, wanted))
+
+ # The other direction: a second activity that answers the card query with no meta-data is asked
+ # for a card on every Home and logged against ('no <meta-data ...>'), and never shows one.
+ for element in application:
+ if element.tag not in ACTIVITY_TAGS or element in carriers:
+ continue
+ if carries(element, MAIN_ACTION, DOCK_CARD):
+ reasons.append("%s carries MAIN + %s and no card meta-data, so it is queried for a card "
+ "every Home and answers with nothing" % (named(element), DOCK_CARD))
+ return reasons
+
+
+class ShippedDeclarationsTest(unittest.TestCase):
+ """Every card the add-ons ship is reached by the launcher's query, not dropped on the way."""
+
+ def test_the_walk_reads_the_manifests_this_file_was_written_for(self):
+ read = []
+ for path in card_files():
+ manifest = manifest_of(path)
+ with self.subTest(card=str(path.relative_to(ROOT))):
+ self.assertTrue(manifest.is_file(), "%s has no AndroidManifest.xml" % manifest.parents[2])
+ if manifest.is_file():
+ read.append(path)
+ self.assertGreaterEqual(len(read), 25, "the walk reached %d manifests" % len(read))
+
+ def test_every_shipped_card_is_declared_on_a_tile_a_tv_can_open(self):
+ for path in card_files():
+ with self.subTest(card=str(path.relative_to(ROOT))):
+ self.assertEqual(declaration_problems(path), [])
+
+
+class DeclarationReaderTest(unittest.TestCase):
+ """The same reader over manifests written here, one shape per test."""
+
+ CARD = '<card spec="1" label="Demo" />'
+
+ def check(self, manifest, card="module_card"):
+ with tempfile.TemporaryDirectory() as tmp:
+ addon = Path(tmp) / "demo"
+ xml_folder = addon / "app/src/main/res/xml"
+ xml_folder.mkdir(parents=True)
+ (xml_folder / (card + ".xml")).write_text(self.CARD, encoding="utf-8")
+ (addon / "app/src/main/AndroidManifest.xml").write_text(manifest, encoding="utf-8")
+ return declaration_problems(xml_folder / (card + ".xml"))
+
+ def manifest(self, activity, application_attrs="", extra=""):
+ return ('<manifest xmlns:android="http://schemas.android.com/apk/res/android">'
+ '<application%s>%s%s</application></manifest>' % (application_attrs, activity, extra))
+
+ TILE = ('<activity android:name=".Main" android:exported="true">'
+ '<intent-filter><action android:name="android.intent.action.MAIN" />'
+ '<category android:name="android.intent.category.LEANBACK_LAUNCHER" /></intent-filter>'
+ '<intent-filter><action android:name="android.intent.action.MAIN" />'
+ '<category android:name="com.gathin.tvhome.category.DOCK_CARD" /></intent-filter>'
+ '<meta-data android:name="com.gathin.tvhome.card" android:resource="@xml/module_card" />'
+ '</activity>')
+
+ def test_the_shipped_shape_is_quiet(self):
+ self.assertEqual(self.check(self.manifest(self.TILE)), [])
+
+ def test_an_activity_that_is_not_exported_is_named(self):
+ activity = self.TILE.replace(' android:exported="true"', "")
+ reasons = self.check(self.manifest(activity))
+ self.assertEqual(len(reasons), 1, reasons)
+ self.assertIn("is not android:exported", reasons[0])
+ self.assertIn("not exported and enabled", reasons[0])
+
+ def test_an_activity_that_is_disabled_is_named(self):
+ activity = self.TILE.replace('android:exported="true"',
+ 'android:exported="true" android:enabled="false"')
+ reasons = self.check(self.manifest(activity))
+ self.assertEqual(len(reasons), 1, reasons)
+ self.assertIn('android:enabled="false"', reasons[0])
+
+ def test_a_disabled_application_is_named(self):
+ reasons = self.check(self.manifest(self.TILE, ' android:enabled="false"'))
+ self.assertEqual(len(reasons), 1, reasons)
+ self.assertIn("the <application> is android:enabled", reasons[0])
+
+ def test_a_card_without_the_leanback_filter_is_named(self):
+ activity = self.TILE.replace('<category android:name="android.intent.category.LEANBACK_LAUNCHER" />',
+ "")
+ reasons = self.check(self.manifest(activity))
+ self.assertEqual(len(reasons), 1, reasons)
+ self.assertIn("no LEANBACK_LAUNCHER intent filter", reasons[0])
+
+ def test_a_card_without_the_dock_card_filter_is_named(self):
+ activity = self.TILE.replace('<category android:name="com.gathin.tvhome.category.DOCK_CARD" />', "")
+ reasons = self.check(self.manifest(activity))
+ self.assertEqual(len(reasons), 1, reasons)
+ self.assertIn("Cards.discover() finds a card by querying exactly that", reasons[0])
+
+ def test_a_meta_data_on_another_activity_is_named(self):
+ # The meta-data now sits on a second activity that is not the tile: the launcher reads it there
+ # and drops the card, and the tile answers the card query with nothing.
+ activity = self.TILE.replace(
+ '<meta-data android:name="com.gathin.tvhome.card" android:resource="@xml/module_card" />', "")
+ other = ('<activity android:name=".Settings"><meta-data android:name="com.gathin.tvhome.card" '
+ 'android:resource="@xml/module_card" /></activity>')
+ reasons = self.check(self.manifest(activity + other))
+ self.assertTrue(any(".Settings has no MAIN + com.gathin.tvhome.category.DOCK_CARD" in r
+ for r in reasons), reasons)
+ self.assertTrue(any(".Main carries" in r for r in reasons), reasons)
+
+ def test_a_meta_data_that_names_another_resource_is_named(self):
+ reasons = self.check(self.manifest(self.TILE.replace("@xml/module_card", "@xml/card")))
+ self.assertEqual(len(reasons), 1, reasons)
+ self.assertIn("@xml/card", reasons[0])
+ self.assertIn("@xml/module_card", reasons[0])
+
+ def test_a_meta_data_that_uses_value_is_named(self):
+ reasons = self.check(self.manifest(
+ self.TILE.replace('android:resource="@xml/module_card"', 'android:value="@xml/module_card"')))
+ self.assertEqual(len(reasons), 1, reasons)
+ self.assertIn("android:resource only", reasons[0])
+
+ def test_a_card_on_a_service_is_named(self):
+ activity = self.TILE.replace(
+ '<meta-data android:name="com.gathin.tvhome.card" android:resource="@xml/module_card" />', "")
+ reasons = self.check(self.manifest(activity + '<service android:name=".Sync">'
+ '<meta-data android:name="com.gathin.tvhome.card" '
+ 'android:resource="@xml/module_card" /></service>'))
+ self.assertEqual(len(reasons), 2, reasons) # the tile now answers a card query with nothing
+ self.assertTrue(any("only returns activities" in r for r in reasons), reasons)
+
+ def test_a_second_card_in_one_package_is_named(self):
+ extra = ('<activity android:name=".Second" android:exported="true"><intent-filter>'
+ '<action android:name="android.intent.action.MAIN" />'
+ '<category android:name="android.intent.category.LEANBACK_LAUNCHER" />'
+ '<category android:name="com.gathin.tvhome.category.DOCK_CARD" /></intent-filter>'
+ '<meta-data android:name="com.gathin.tvhome.card" android:resource="@xml/module_card" />'
+ '</activity>')
+ reasons = self.check(self.manifest(self.TILE + extra))
+ self.assertEqual(len(reasons), 1, reasons)
+ self.assertIn("one per package is read", reasons[0])
+
+ def test_a_card_filter_with_no_card_is_named(self):
+ extra = ('<activity android:name=".Settings" android:exported="true"><intent-filter>'
+ '<action android:name="android.intent.action.MAIN" />'
+ '<category android:name="com.gathin.tvhome.category.DOCK_CARD" /></intent-filter>'
+ '</activity>')
+ reasons = self.check(self.manifest(self.TILE + extra))
+ self.assertEqual(len(reasons), 1, reasons)
+ self.assertIn(".Settings", reasons[0])
+ self.assertIn("answers with nothing", reasons[0])
+
+ def test_a_manifest_that_is_not_there_is_named(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ folder = Path(tmp) / "addons/demo/app/src/main/res/xml"
+ folder.mkdir(parents=True)
+ card = folder / "module_card.xml"
+ card.write_text(self.CARD, encoding="utf-8")
+ reasons = declaration_problems(card)
+ self.assertEqual(len(reasons), 1, reasons)
+ self.assertIn("nothing installs the card", reasons[0])
+
+
if __name__ == "__main__":
unittest.main()