Skip to content

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

tools: tv.py judges --wait before it presses a key, not time.sleep after

xbsheng6b3763cbe1ee
diff --git a/tests/test_tv_screenshot.py b/tests/test_tv_screenshot.py
index 33032efb1..9b480c8c3 100644
--- a/tests/test_tv_screenshot.py
+++ b/tests/test_tv_screenshot.py
@@ -9,7 +9,9 @@ some MediaTek sets), leave nothing behind on the TV, and name the 0-byte case fo
TV and HDMI inputs capture empty.
It can also press remote keys first (#560), so one command shows what a key did: key names are checked
-before anything reaches the TV, since they end up in a shell command there.
+before anything reaches the TV, since they end up in a shell command there. The same goes for --wait: a
+value float() takes but time.sleep() cannot ("nan", "inf", 1e18) was a traceback out of the capture with
+the keys already pressed, and the run answered 1, the code kept for a capture that came back empty.
The TV itself is faked, so these run anywhere adb_shell is installed or not.
@@ -220,6 +222,36 @@ class ScreenshotTest(unittest.TestCase):
self.assertIn("usage:", err)
self.assertEqual(device.calls, [], "nothing may reach the TV")
+ def test_a_wait_that_time_sleep_cannot_take_is_refused_before_the_keys(self):
+ # float() takes these, so they went straight to time.sleep() in screenshot(), after the keys had
+ # been pressed on the TV: NaN is a ValueError there and an infinity (or 1e18) an OverflowError, as
+ # a traceback with exit 1 - the code this client keeps for a capture that came back empty. The last
+ # two are the unit typo instead: seconds typed as nanoseconds sleeps for years rather than failing.
+ for value in ("nan", "NaN", "inf", "1e18", "1000000001"):
+ with self.subTest(wait=value):
+ code, out, err, device = self.run_tv(["--screenshot", "a.png", "HOME", "--wait", value])
+ self.assertEqual(code, 2, err)
+ self.assertIn(f"--wait needs a number of seconds between 0 and {TV.WAIT_MAX_S:g}, "
+ f"not {value!r}", err)
+ self.assertIn("usage:", err)
+ self.assertEqual("", out)
+ self.assertNotIn("Traceback", err)
+ self.assertEqual(device.calls, [], "the pause is judged before the TV is opened")
+ self.assertEqual([], self.sleeps, "no key press is started for a pause that cannot be kept")
+
+ def test_the_ends_of_the_pause_still_capture(self):
+ # 0 is the shape a script writes for "no pause", and the ceiling is a real pause: neither may be
+ # refused by the check the values above meet.
+ for value, sleeps in (("0", []), ("300", [300.0])):
+ with self.subTest(wait=value):
+ with tempfile.TemporaryDirectory() as tmp:
+ local = Path(tmp) / "home.png"
+ code, out, err, _ = self.run_tv(["--screenshot", str(local), "HOME", "--wait", value])
+ self.assertEqual(code, 0, err)
+ self.assertEqual(local.read_bytes(), PNG)
+ self.assertEqual(self.sleeps, sleeps)
+ self.assertIn(f"screenshot {local}", out)
+
def test_a_flag_without_its_argument_is_a_usage_error_not_a_tv_command(self):
for args in (["--screenshot"], ["--push", "only-one"], ["--pull"]):
with self.subTest(args=args):
diff --git a/tools/tv.py b/tools/tv.py
index 0cea64b6f..43a265625 100755
--- a/tools/tv.py
+++ b/tools/tv.py
@@ -318,6 +318,10 @@ PNG = b"\x89PNG\r\n\x1a\n"
# Key names go into a shell command on the TV, so only plain key names and numeric keycodes get through.
KEY_NAME = re.compile(r"^[A-Z0-9_]+$")
KEY_GAP_S = 0.6 # between two keys, so the UI has finished reacting to the first
+# The pause after the last key is there for a screen to settle. The capture's read timeout is 120 s and
+# the pull's 300 s, so a pause longer than that is a unit typo rather than a settle: "seconds" typed as
+# milliseconds or nanoseconds ("--wait 1000000000") used to hang the run for years instead of returning.
+WAIT_MAX_S = 300.0
def check_arity(args):
@@ -429,7 +433,8 @@ def screenshot_args(rest):
KEY is an Android key name without the KEYCODE_ prefix (HOME, DPAD_RIGHT), a full name (KEYCODE_BACK)
or a numeric keycode (83). The screenshot is taken --wait seconds after the last key (default 1). With no
keys, --wait still pauses before the capture, so a screen that is still loading can settle; without it,
- no keys means no pause."""
+ no keys means no pause. The pause is 0 to 300 seconds: a value time.sleep() cannot take, or one that is
+ a unit typo, is a usage error here rather than a traceback after the keys have been pressed."""
local, keys, wait = None, [], None
i = 0
while i < len(rest):
@@ -441,8 +446,15 @@ def screenshot_args(rest):
wait = float(rest[i + 1])
except ValueError:
wait = -1
- if wait < 0:
- usage_error(f"--wait needs a number of seconds, not {rest[i + 1]!r}")
+ # Judged here, with the destination and before connect(), because time.sleep() cannot take
+ # every float: NaN is a ValueError and an infinity (or 1e18) an OverflowError, both raised in
+ # screenshot() *after* the keys have been pressed on the TV - exit 1, the code this client
+ # keeps for a capture that came back empty, so a typo in the pause read as a TV with nothing
+ # on screen, with no PNG and the capture never taken. A NaN fails both comparisons, so this
+ # one test covers it too.
+ if not 0 <= wait <= WAIT_MAX_S:
+ usage_error(f"--wait needs a number of seconds between 0 and {WAIT_MAX_S:g}, "
+ f"not {rest[i + 1]!r}")
i += 2
continue
if local is None and not arg.startswith("--"):