A pair of people can have a kitten, a puppy or a lion cub in place of TikTok's streak creature: it grows with the days of the streak (cub, teen, adult, elder), goes grey while the streak is about to burn out, can be dragged in the chat and is also shown on the big streak screen. An animal is given by a link (lihont.space/pet) that the friend accepts. The drawings are made by scripts/make_pet_designs.py and served as strips of frames. Also here: the promo video scripts (make_pet_video.py on top of make_store_video.py), a retry pause for strips that failed to load, stricter checks in the answer to a gift. Work in progress: the gift service is not switched on for everyone yet and there is no public build with the animals. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
210 lines
12 KiB
Python
210 lines
12 KiB
Python
import unittest
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
from lihont import manifest as manifest_module
|
|
from lihont import widget_patch
|
|
from lihont.axml import Axml, RES_XML_END_ELEMENT, RES_XML_START_ELEMENT
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
APKS = sorted((ROOT / "OriginalAPKtiktok").glob("*.apk")) if (ROOT / "OriginalAPKtiktok").is_dir() else []
|
|
|
|
|
|
class FakeApk:
|
|
"""Just enough of lihont.apkzip.Apk: reads from the original apk, remembers what is replaced."""
|
|
|
|
def __init__(self, path):
|
|
self.zip = zipfile.ZipFile(path)
|
|
self.replaced = {}
|
|
|
|
def read(self, name):
|
|
return self.replaced.get(name) or self.zip.read(name)
|
|
|
|
def replace(self, name, data):
|
|
self.replaced[name] = data
|
|
|
|
def add(self, name, data, method=None):
|
|
self.replaced[name] = data
|
|
|
|
|
|
def provider_node(axml):
|
|
return next(n for n in axml.nodes
|
|
if n.kind == RES_XML_START_ELEMENT and axml.pool.get(n.name) == "appwidget-provider")
|
|
|
|
|
|
@unittest.skipUnless(APKS, "the original TikTok apk is not in OriginalAPKtiktok/")
|
|
class WidgetPatchTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.apk = FakeApk(APKS[0])
|
|
self.xml = Axml.parse(self.apk.read("AndroidManifest.xml"))
|
|
|
|
def receivers(self, xml):
|
|
return {xml.attr_string(n, "name"): n for n in xml.elements("receiver")}
|
|
|
|
def test_four_widgets_become_ours_with_one_name(self):
|
|
report = widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
rebuilt = Axml.parse(self.apk.replaced["AndroidManifest.xml"])
|
|
receivers = self.receivers(rebuilt)
|
|
for size in widget_patch.SIZES:
|
|
self.assertIn(size.widget, receivers)
|
|
self.assertNotIn(size.original, receivers)
|
|
self.assertEqual(widget_patch.LABEL, rebuilt.attr_string(receivers[size.widget], "label"))
|
|
self.assertEqual({"1x1", "2x2", "3x3", "4x4"}, set(report["receivers"]))
|
|
|
|
def test_the_configuration_screen_is_declared_and_exported(self):
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
rebuilt = Axml.parse(self.apk.replaced["AndroidManifest.xml"])
|
|
self.assertTrue(manifest_module.has_activity(rebuilt, widget_patch.CONFIG_CLASS))
|
|
node = next(n for n in rebuilt.elements("activity")
|
|
if rebuilt.attr_string(n, "name") == widget_patch.CONFIG_CLASS)
|
|
self.assertNotEqual(0, rebuilt.attr(node, "exported").data)
|
|
actions = [rebuilt.attr_string(n, "name") for n in rebuilt.elements("action")]
|
|
self.assertIn(widget_patch.CONFIGURE_ACTION, actions)
|
|
|
|
def test_every_provider_has_its_size_its_layout_and_can_be_resized(self):
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
for size in widget_patch.SIZES:
|
|
provider = Axml.parse(self.apk.replaced[size.provider])
|
|
node = provider_node(provider)
|
|
self.assertEqual(widget_patch.CONFIG_CLASS, provider.attr_string(node, "configure"))
|
|
self.assertEqual(size.cells, provider.attr(node, "targetCellWidth").data)
|
|
self.assertEqual(size.cells, provider.attr(node, "targetCellHeight").data)
|
|
self.assertEqual(widget_patch.dp(widget_patch.cell_dp(size.cells)), provider.attr(node, "minWidth").data)
|
|
self.assertEqual(widget_patch.LAYOUT, provider.attr(node, "initialLayout").data)
|
|
self.assertEqual(3, provider.attr(node, "resizeMode").data)
|
|
self.assertEqual(widget_patch.dp(40), provider.attr(node, "minResizeWidth").data)
|
|
self.assertIsNone(provider.attr(node, "description"))
|
|
self.assertIsNone(provider.attr(node, "previewLayout"))
|
|
|
|
def test_provider_attributes_stay_in_ascending_id_order(self):
|
|
# Android reads the attributes side by side with its own sorted list and skips what is out of
|
|
# order; with widgetCategory ahead of resizeMode the launcher saw a widget that could not be resized
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
for size in widget_patch.SIZES:
|
|
provider = Axml.parse(self.apk.replaced[size.provider])
|
|
node = provider_node(provider)
|
|
ids = [provider.resource_map[a.name] for a in node.attributes]
|
|
self.assertEqual(sorted(ids), ids, size.provider)
|
|
self.assertEqual(len(ids), len(set(ids)), "an attribute is written twice in " + size.provider)
|
|
|
|
def test_the_layout_fills_the_widget_instead_of_a_fixed_square(self):
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
layout = Axml.parse(self.apk.replaced[widget_patch.LAYOUT_FILE])
|
|
elements = [n for n in layout.nodes if n.kind == RES_XML_START_ELEMENT]
|
|
picture = next(n for n in elements if layout.pool.get(n.name) == "ImageView")
|
|
for node in (elements[0], picture):
|
|
for resource_id in (widget_patch.ATTR_LAYOUT_WIDTH, widget_patch.ATTR_LAYOUT_HEIGHT):
|
|
attr = widget_patch._attr_by_id(layout, node, resource_id)
|
|
self.assertEqual(widget_patch.MATCH_PARENT, attr.data)
|
|
# the picture view of TikTok's layout is still the one the widget draws into
|
|
self.assertEqual(widget_patch.PICTURE,
|
|
widget_patch._attr_by_id(layout, picture, widget_patch.ATTR_ID).data)
|
|
|
|
def test_the_layout_flips_between_four_pictures_by_itself(self):
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
layout = Axml.parse(self.apk.replaced[widget_patch.LAYOUT_FILE])
|
|
starts = [n for n in layout.nodes if n.kind == RES_XML_START_ELEMENT]
|
|
ends = [n for n in layout.nodes if n.kind == RES_XML_END_ELEMENT]
|
|
self.assertEqual(len(starts), len(ends), "every element is closed")
|
|
names = [layout.pool.get(n.name) for n in starts]
|
|
self.assertEqual(["RelativeLayout", "ViewFlipper", "ImageView", "ImageView", "ImageView", "ImageView"], names[:6])
|
|
|
|
flipper = starts[1]
|
|
flipper_ids = [layout.resource_map[a.name] for a in flipper.attributes]
|
|
self.assertEqual(sorted(flipper_ids), flipper_ids, "the flipper's attributes are in ascending id order")
|
|
by_id = {layout.resource_map[a.name]: a for a in flipper.attributes}
|
|
self.assertEqual(widget_patch.FLIPPER_VIEW, by_id[widget_patch.ATTR_ID].data)
|
|
self.assertEqual(0xFFFFFFFF, by_id[0x010102B5].data, "it starts flipping on its own")
|
|
self.assertEqual(widget_patch.FLIP_INTERVAL_MS, by_id[0x01010179].data)
|
|
self.assertEqual(widget_patch.ANIM_FADE_IN, by_id[0x01010177].data)
|
|
self.assertEqual(widget_patch.ANIM_FADE_OUT, by_id[0x01010178].data)
|
|
|
|
# the old picture view is the first frame; the three new ones copy it with ids of their own
|
|
frames = starts[2:6]
|
|
got = [widget_patch._attr_by_id(layout, f, widget_patch.ATTR_ID).data for f in frames]
|
|
self.assertEqual([widget_patch.PICTURE] + list(widget_patch.FRAME_VIEWS), got)
|
|
sizes = {tuple(widget_patch._attr_by_id(layout, f, rid).data
|
|
for rid in (widget_patch.ATTR_LAYOUT_WIDTH, widget_patch.ATTR_LAYOUT_HEIGHT)) for f in frames}
|
|
self.assertEqual({(widget_patch.MATCH_PARENT, widget_patch.MATCH_PARENT)}, sizes)
|
|
# the hidden badge and label views of TikTok's layout are still there
|
|
self.assertIn("FrameLayout", names)
|
|
self.assertIn("TextView", names)
|
|
|
|
def run_java(self, *args):
|
|
"""WidgetIds compiled against the Android stubs and run on a plain JVM (its parser touches no Android class)."""
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
javac, java = shutil.which("javac"), shutil.which("java")
|
|
if not javac or not java:
|
|
self.skipTest("a JDK is needed")
|
|
jar = ROOT / "tools" / "android.jar"
|
|
with tempfile.TemporaryDirectory() as out:
|
|
done = subprocess.run([javac, "-encoding", "UTF-8", "-cp", str(jar), "-d", out,
|
|
str(ROOT / "src" / "lihont" / "two" / "widget" / "WidgetIds.java"),
|
|
str(ROOT / "tests" / "java" / "lihont" / "two" / "widget" / "WidgetIdsTest.java")], capture_output=True, text=True)
|
|
self.assertEqual(0, done.returncode, done.stdout + done.stderr)
|
|
run = subprocess.run([java, "-cp", out + __import__("os").pathsep + str(jar), "lihont.two.widget.WidgetIdsTest", *args], capture_output=True, text=True)
|
|
self.assertEqual(0, run.returncode, run.stderr)
|
|
return run.stdout.strip()
|
|
|
|
@staticmethod
|
|
def line(ids):
|
|
return "layout=0x%08X picture=0x%08X badge=0x%08X label=0x%08X frames=%s" % (
|
|
ids.layout, ids.picture, ids.badge, ids.label, ",".join("0x%08X" % f for f in ids.frames))
|
|
|
|
def test_the_java_side_has_the_numbers_of_47_1_3_for_builds_without_the_file(self):
|
|
self.assertEqual(self.line(widget_patch.DEFAULT), self.run_java())
|
|
|
|
def test_the_java_side_reads_the_file_the_patch_writes(self):
|
|
table = __import__("lihont.arsc", fromlist=["Arsc"]).Arsc(self.apk.zip.read("resources.arsc"))
|
|
ids = widget_patch.discover(self.apk, self.xml, table) # looked up in the build as TikTok made it, before the patch changes the layout
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
text = self.apk.replaced[widget_patch.IDS_ASSET].decode("ascii")
|
|
self.assertEqual(self.line(ids), self.run_java(text.replace(chr(10), "|")))
|
|
|
|
def test_a_broken_ids_file_is_refused_by_the_java_side(self):
|
|
self.assertEqual("NULL", self.run_java("layout=0x7F0D0178|picture=0x7F0A1DAB"))
|
|
self.assertEqual("NULL", self.run_java("garbage"))
|
|
|
|
def test_discovery_finds_what_was_typed_in_for_47_1_3(self):
|
|
table = __import__("lihont.arsc", fromlist=["Arsc"]).Arsc(self.apk.zip.read("resources.arsc"))
|
|
self.assertEqual(widget_patch.DEFAULT, widget_patch.discover(self.apk, self.xml, table))
|
|
|
|
def test_the_widget_list_shows_our_flower_instead_of_tiktoks_pictures(self):
|
|
report = widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
picture = widget_patch.preview_asset().read_bytes()
|
|
self.assertTrue(picture.startswith(b"\x89PNG"))
|
|
# the pictures of 47.1.3's own widgets: a nine-patch, a webp and a png
|
|
self.assertEqual(["res/c/i2.9.png", "res/c/i6.png", "res/c/i9.webp"], report["previews"])
|
|
for path in report["previews"]:
|
|
self.assertEqual(picture, self.apk.replaced[path])
|
|
|
|
def test_a_layout_that_is_not_the_expected_one_is_refused(self):
|
|
layout = Axml.parse(self.apk.read("res/b/pr.xml"))
|
|
for node in layout.nodes:
|
|
if node.kind == RES_XML_START_ELEMENT and layout.pool.get(node.name) == "ImageView":
|
|
node.name = layout.pool.index("TextView")
|
|
with self.assertRaises(widget_patch.WidgetError):
|
|
widget_patch.fill_layout(layout)
|
|
|
|
def test_a_second_pass_is_refused(self):
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
with self.assertRaises(widget_patch.WidgetError):
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
|
|
def test_a_manifest_without_the_widget_is_refused(self):
|
|
for node in self.xml.elements("receiver"):
|
|
if self.xml.attr_string(node, "name") == widget_patch.SIZES[0].original:
|
|
self.xml.set_attr_string(node, "name", "x.Other")
|
|
with self.assertRaises(widget_patch.WidgetError):
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
|
|
def test_cells_are_sized_the_way_launchers_count_them(self):
|
|
self.assertEqual([40, 110, 180, 250], [widget_patch.cell_dp(n) for n in (1, 2, 3, 4)])
|
|
self.assertEqual(0x6E01, widget_patch.dp(110))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|