Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.
Python adbadblockandroid-tvandroid-tv-launcherapple-tvbloatware-removalclaude-codedebloatfire-tvgoogle-tvlauncherprivacysmart-tvsony-braviatclvibe-codingxiaomi
tests: every picture in a switches row is checked, not only the last one
xbsheng52294f7ef9f6
diff --git a/docs/switches.md b/docs/switches.mdindex 31cf1dc1a..83fa12316 100644--- a/docs/switches.md+++ b/docs/switches.md@@ -18,7 +18,9 @@ repo that shows the key at work also counts) and add a row below:- Blur anything personal: Wi-Fi names, people, addresses.`tests/test_switches.py` checks that every new key has a row here with an image that exists and-fits the rules; keys from before the rule sit on its wanted list until a screenshot arrives.+fits the rules. A row may show more than one picture - `vpn_pill` carries the pill up and the pill at+rest - and every picture in a row is held to those rules, not only the last one. Keys from before the+rule sit on its wanted list until a screenshot arrives.## Screenshotsdiff --git a/tests/test_switches.py b/tests/test_switches.pyindex d238c8b0c..28fc09f43 100644--- a/tests/test_switches.py+++ b/tests/test_switches.py@@ -1,11 +1,20 @@"""Every theme key (a switch) has a screenshot of it on a real TV, listed in docs/switches.md.The site's Switches tab reads the same table, so a key added with its screenshot shows up there too.++A row may show more than one picture - `vpn_pill` carries the pill up and the pill at rest,+`icon_only_vpn` the lock open and the lock closed - and the page's rules (a JPEG or PNG, 16:9, no+wider than 1920 px, under 300 KB) are about every picture in the table, not one per row. The reader+used to take the last `<img>` of a row (`.*<img` is greedy), so the picture before it in a two-picture+row was read by nothing: `switches/vpn_pill.jpg` and `switches/icon_only_vpn.jpg` could have been+missing, 4:3, over 300 KB or a WebP named .jpg and the suite stayed green.+Run from the repository root: python3 -m unittest discover -s tests -v"""import reimport structimport sys+import tempfileimport unittestfrom pathlib import Path@@ -17,6 +26,16 @@ DOCS = ROOT / "docs"THEMES_README = ROOT / "themes" / "README.md"SWITCHES = DOCS / "switches.md"+# A row of the Screenshots table: `| `key` | the pictures |`.+ROW_RE = re.compile(r"^\|\s*`([a-z_]+)`\s*\|(.*)$")+IMG_RE = re.compile(r"<img\b[^>]*>")+SRC_RE = re.compile(r'\bsrc="([^"]*)"')++# The page's limits (docs/switches.md, "Adding a key"): a listed picture is a JPEG or PNG, 16:9, no+# wider than this and under MAX_SCREENSHOT. tests/test_switches_page_rules.py reads this number back+# out of the file, so the page cannot drift from the checker.+MAX_WIDTH = 1920+# Keys from before screenshots were required. This list only shrinks: once a key has a row in# docs/switches.md, take it off here.SCREENSHOT_WANTED = {@@ -53,14 +72,31 @@ def theme_keys():return keys-def screenshot_rows():- """key -> image path relative to docs/, from the Screenshots table of docs/switches.md."""- rows = {}- for line in SWITCHES.read_text(encoding="utf-8").split("\n"):- m = re.match(r'^\|\s*`([a-z_]+)`\s*\|.*<img[^>]*\bsrc="([^"]+)"', line)- if m:- rows[m.group(1)] = m.group(2)- return rows+def rows(text):+ """(key, the pictures in that row in the order the page shows them) for every table row.++ An `<img>` with no src, or with `src=""`, keeps its place as an empty string: a picture the page+ cannot draw is a row to fix, not a row to skip.+ """+ found = []+ for line in text.split("\n"):+ match = ROW_RE.match(line)+ if not match:+ continue+ pictures = []+ for tag in IMG_RE.findall(match.group(2)):+ src = SRC_RE.search(tag)+ pictures.append(src.group(1) if src else "")+ found.append((match.group(1), pictures))+ return found+++def screenshot_rows(text=None):+ """key -> every picture its row shows, in page order. A key named in two rows keeps both."""+ table = {}+ for key, pictures in rows(SWITCHES.read_text(encoding="utf-8") if text is None else text):+ table.setdefault(key, []).extend(pictures)+ return tabledef png_size(data):@@ -69,33 +105,169 @@ def png_size(data):return struct.unpack(">II", data[16:24])+def picture_problem(docs, src):+ """Why the picture `src`, read below the folder `docs`, breaks the rules the page states, or None."""+ if not src:+ return "the <img> in this row has no src, so nothing is drawn there"+ path = (Path(docs) / src).resolve()+ if not path.is_file():+ return f"{src} does not exist"+ data = path.read_bytes()+ if len(data) > MAX_SCREENSHOT:+ return f"{src} is over 300 KB"+ size = jpeg_size(data) or png_size(data)+ if size is None:+ return f"{src} is not a JPEG or PNG"+ width, height = size+ if width * 9 != height * 16:+ return f"{src} is {width}x{height}, not 16:9"+ if width > MAX_WIDTH:+ return f"{src} is {width} px wide; the page says no wider than {MAX_WIDTH}, and 1280x720 is enough"+ return None+++def problems(docs, table):+ """(key, src, why) for every picture in `table` that breaks the rules, in page order."""+ return [(key, src, why)+ for key, pictures in table.items()+ for src in pictures+ for why in [picture_problem(docs, src)]+ if why]++class SwitchScreenshotTest(unittest.TestCase):def test_every_key_has_a_screenshot(self):- rows = screenshot_rows()+ rows_by_key = screenshot_rows()for key in theme_keys():with self.subTest(key=key):if key in SCREENSHOT_WANTED:- self.assertNotIn(key, rows, "it has a screenshot now: take it off SCREENSHOT_WANTED")+ self.assertNotIn(key, rows_by_key,+ "it has a screenshot now: take it off SCREENSHOT_WANTED")continue- self.assertIn(key, rows, "no screenshot: add one to docs/switches.md (see its Adding a key section)")+ self.assertTrue(rows_by_key.get(key),+ "no screenshot: add one to docs/switches.md (see its Adding a key section)")- def test_screenshots_exist_and_fit_the_rules(self):- for key, src in screenshot_rows().items():- with self.subTest(key=key):- path = (DOCS / src).resolve()- self.assertTrue(path.is_file(), f"{src} does not exist")- data = path.read_bytes()- self.assertLessEqual(len(data), MAX_SCREENSHOT, f"{src} is over 300 KB")- size = jpeg_size(data) or png_size(data)- self.assertIsNotNone(size, f"{src} is not a JPEG or PNG")- width, height = size- self.assertEqual(width * 9, height * 16, f"{src} is {width}x{height}, not 16:9")- self.assertLessEqual(width, 1920, f"{src} is wider than 1920; 1280x720 is enough")+ def test_every_picture_in_a_row_exists_and_fits_the_rules(self):+ bad = problems(DOCS, screenshot_rows())+ self.assertEqual([], [f"{key}: {why}" for key, src, why in bad],+ "a picture in docs/switches.md that breaks the rules its Adding a key section states")def test_rows_and_wanted_list_name_real_keys(self):keys = set(theme_keys())- self.assertEqual(set(screenshot_rows()) - keys, set(), "docs/switches.md lists a key themes/README.md does not")- self.assertEqual(SCREENSHOT_WANTED - keys, set(), "SCREENSHOT_WANTED names a key themes/README.md does not")+ self.assertEqual(set(screenshot_rows()) - keys, set(),+ "docs/switches.md lists a key themes/README.md does not")+ self.assertEqual(SCREENSHOT_WANTED - keys, set(),+ "SCREENSHOT_WANTED names a key themes/README.md does not")++ def test_the_page_shows_every_picture_it_is_checked_for(self):+ # A reader that finds no row, or one picture per row, would pass the checks above by reading+ # nothing: the page holds a row per key that has its screenshot, and a few rows hold two.+ table = screenshot_rows()+ self.assertGreaterEqual(len(table), 30, "the Screenshots table was not read")+ self.assertGreaterEqual(sum(len(p) for p in table.values()), len(table),+ "the pictures in the Screenshots table were not read")+++class TheReader(unittest.TestCase):+ """The row reader, over pages written here rather than the one in the tree."""++ def test_a_row_with_two_pictures_gives_both_in_page_order(self):+ page = "\n".join([+ "| Key | Screenshot |",+ "|---|---|",+ '| `vpn_pill` | <img src="switches/vpn_pill.jpg" width="320"> <img src="switches/vpn_pill_rest.jpg" width="320"> |',+ ])+ self.assertEqual({"vpn_pill": ["switches/vpn_pill.jpg", "switches/vpn_pill_rest.jpg"]},+ screenshot_rows(page))++ def test_a_row_with_one_picture_and_a_row_with_none(self):+ page = "\n".join([+ '| `dock_band` | <img src="switches/dock_band.jpg" width="320" alt="dock_band"> |',+ "| `tile_menu` | the menu is described in prose |",+ ])+ self.assertEqual({"dock_band": ["switches/dock_band.jpg"], "tile_menu": []},+ screenshot_rows(page))++ def test_an_img_without_a_src_keeps_its_place(self):+ page = '| `dock_band` | <img src="switches/dock_band.jpg"> <img alt="no source"> |'+ self.assertEqual({"dock_band": ["switches/dock_band.jpg", ""]}, screenshot_rows(page))++ def test_an_empty_src_is_not_a_picture(self):+ page = '| `dock_band` | <img src=""> |'+ self.assertEqual({"dock_band": [""]}, screenshot_rows(page))+ self.assertIn("no src", problems(DOCS, screenshot_rows(page))[0][2])++ def test_a_key_named_in_two_rows_keeps_both_rows_pictures(self):+ page = "\n".join([+ '| `dock_band` | <img src="switches/dock_band.jpg"> |',+ '| `dock_band` | <img src="switches/single_row.jpg"> |',+ ])+ self.assertEqual({"dock_band": ["switches/dock_band.jpg", "switches/single_row.jpg"]},+ screenshot_rows(page))++ def test_a_line_that_is_not_a_row_is_not_read(self):+ # The header, the separator and the prose around the table.+ page = "\n".join([+ "| Key | Screenshot |",+ "|---|---|",+ "A new key needs a screenshot.",+ "| `dock_band` | <img src=\"switches/dock_band.jpg\"> |",+ "`tests/test_switches.py` checks it.",+ ])+ self.assertEqual(["dock_band"], list(screenshot_rows(page)))+++class ThePictureRules(unittest.TestCase):+ """The rules, over pictures written here rather than the ones in the tree."""++ def setUp(self):+ self.tmp = tempfile.TemporaryDirectory()+ self.docs = Path(self.tmp.name)+ self.addCleanup(self.tmp.cleanup)++ def jpeg(self, name, width, height, size=None):+ segment = b"\xff\xc0" + struct.pack(">H", 11) + b"\x08" + struct.pack(">HH", height, width) \+ + b"\x01\x01\x11\x00"+ data = b"\xff\xd8" + segment + b"\xff\xd9"+ path = self.docs / name+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_bytes(data + b"\x00" * max(0, (size or 0) - len(data)))+ return path++ def png(self, name, width, height):+ data = b"\x89PNG\r\n\x1a\n" + struct.pack(">I", 13) + b"IHDR" \+ + struct.pack(">II", width, height) + b"\x08\x02\x00\x00\x00"+ path = self.docs / name+ path.write_bytes(data)+ return path++ def test_a_1280x720_jpeg_and_png_pass(self):+ self.jpeg("switches/a.jpg", 1280, 720)+ self.png("switches/b.png", 1280, 720)+ self.assertEqual([], problems(self.docs, {"a": ["switches/a.jpg"], "b": ["switches/b.png"]}))++ def test_a_picture_that_is_not_16_by_9_is_reported(self):+ self.jpeg("switches/a.jpg", 640, 480)+ bad = problems(self.docs, {"a": ["switches/a.jpg", "switches/a.jpg"]})+ self.assertEqual(2, len(bad), "both pictures are judged, not one per key")+ self.assertIn("not 16:9", bad[0][2])++ def test_a_picture_over_300_kb_is_reported(self):+ self.jpeg("switches/a.jpg", 1280, 720, size=MAX_SCREENSHOT + 1)+ self.assertIn("over 300 KB", problems(self.docs, {"a": ["switches/a.jpg"]})[0][2])++ def test_a_picture_wider_than_1920_is_reported(self):+ self.jpeg("switches/a.jpg", 3840, 2160)+ self.assertIn("wider than 1920", problems(self.docs, {"a": ["switches/a.jpg"]})[0][2])++ def test_a_text_file_named_jpg_is_reported(self):+ (self.docs / "switches").mkdir()+ (self.docs / "switches" / "a.jpg").write_text("iVBORw0KGgo", encoding="utf-8")+ self.assertIn("not a JPEG or PNG", problems(self.docs, {"a": ["switches/a.jpg"]})[0][2])++ def test_a_picture_that_is_not_there_is_reported(self):+ self.assertIn("does not exist",+ problems(self.docs, {"a": ["switches/nowhere.jpg"]})[0][2])if __name__ == "__main__":diff --git a/tests/test_switches_page_rules.py b/tests/test_switches_page_rules.pyindex c040d1e68..5056d72b0 100644--- a/tests/test_switches_page_rules.py+++ b/tests/test_switches_page_rules.py@@ -58,13 +58,13 @@ def size_limit_bytes():def width_limit_px():- """The width cap test_switches.py puts on a listed picture (1280x720 is plenty, 4K is not)."""- m = re.search(r"assertLessEqual\(\s*width\s*,\s*(\d+)", SWITCH_TEST.read_text(encoding="utf-8"))+ """MAX_WIDTH in tests/test_switches.py, the width cap it puts on a listed picture (1280x720 is plenty, 4K is not)."""+ m = re.search(r"^MAX_WIDTH\s*=\s*(\d+)", SWITCH_TEST.read_text(encoding="utf-8"), re.M)return int(m.group(1)) if m else Nonedef aspect_checked():- return bool(re.search(r"assertEqual\(\s*width\s*\*\s*9\s*,\s*height\s*\*\s*16",+ return bool(re.search(r"width\s*\*\s*9\s*!=\s*height\s*\*\s*16",SWITCH_TEST.read_text(encoding="utf-8")))