2026-11-16 18:13:11 +01:00
|
|
|
"""The .tft builder. Real Editor output cannot be in the repo (it embeds Nextion firmware), so
|
|
|
|
|
these tests use a fake "Editor folder" with generated stand-in files and check the structure.
|
|
|
|
|
Byte-identity with the Editor is checked by test_against_editor_samples when NXHMI_TFT_SAMPLES
|
|
|
|
|
points to a folder of <name>.HMI + <name>.tft pairs made with Nextion Editor 1.68.2."""
|
|
|
|
|
import glob
|
|
|
|
|
import os
|
|
|
|
|
import shutil
|
|
|
|
|
import struct
|
|
|
|
|
import tempfile
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
from helpers import HAVE_PIL
|
|
|
|
|
|
|
|
|
|
from nxhmi import cli, components as C, project, tft, tft_build
|
|
|
|
|
from nxhmi.page import Page
|
|
|
|
|
|
|
|
|
|
SAMPLES = os.environ.get("NXHMI_TFT_SAMPLES")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Build(unittest.TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.tmp = tempfile.mkdtemp()
|
|
|
|
|
for i, n in enumerate(tft_build.FIRMWARE_FILES):
|
|
|
|
|
with open(os.path.join(self.tmp, n), "wb") as fh:
|
|
|
|
|
fh.write(bytes([i + 1]) * (100 + 7 * i))
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
|
shutil.rmtree(self.tmp)
|
|
|
|
|
|
|
|
|
|
def project(self):
|
|
|
|
|
h = project.new_project("NX4832T035_011")
|
|
|
|
|
p = Page.parse(h.read("0.pa"))
|
|
|
|
|
C.add(p, "hotspot", x=10, y=20, w=30, h=40)
|
|
|
|
|
C.add(p, "variable")
|
|
|
|
|
h.write("0.pa", p.pack())
|
|
|
|
|
project.add_page(h, "second")
|
|
|
|
|
return h
|
|
|
|
|
|
|
|
|
|
def test_structure(self):
|
|
|
|
|
out = tft_build.build_tft(self.project(), self.tmp)
|
|
|
|
|
self.assertEqual(len(out) % 4, 0)
|
|
|
|
|
self.assertTrue(tft.verify_header(out))
|
|
|
|
|
self.assertTrue(tft.verify_trailer(out))
|
|
|
|
|
self.assertEqual(struct.unpack_from("<I", out, 0x3C)[0], len(out))
|
|
|
|
|
lay = tft.layout_table(out)
|
|
|
|
|
base = lay[5]
|
|
|
|
|
self.assertEqual(base, 0x20000) # resources fit in one 64 KB block
|
|
|
|
|
self.assertEqual(lay[14], 2 | 4 << 16) # 2 pages, 4 components
|
|
|
|
|
# resource directory: first firmware file right after the 0x90-byte directory
|
|
|
|
|
self.assertEqual(struct.unpack_from("<II", out, 0x10000), (0x90, 100))
|
|
|
|
|
self.assertEqual(out[0x10090:0x10090 + 100], b"\x01" * 100)
|
|
|
|
|
# page table: page 0 has 3 components, 33 + 27 + 11 record slots, then page 1
|
|
|
|
|
first, count, names, recs, ram = struct.unpack_from("<HHIII", out, lay[6])
|
|
|
|
|
self.assertEqual((first, count, recs, ram), (0, 3, lay[0], 16))
|
|
|
|
|
self.assertEqual(struct.unpack_from("<HHIII", out, lay[6] + 16)[3], lay[0] + 71 * 24)
|
|
|
|
|
# name list of page 0: sorted hashes
|
|
|
|
|
n, = struct.unpack_from("<I", out, base + names)
|
|
|
|
|
hashes = [struct.unpack_from("<I", out, base + names + 4 + 6 * i)[0] for i in range(n // 6)]
|
|
|
|
|
self.assertEqual(hashes, sorted(tft.name_hash(x) for x in ("page0", "m0", "va0")))
|
|
|
|
|
# hotspot object record: type 109, id 1, rectangle
|
|
|
|
|
obj = out[lay[7] + 232:lay[7] + 464]
|
|
|
|
|
self.assertEqual(obj[:2], bytes([109, 1]))
|
|
|
|
|
self.assertEqual(struct.unpack_from("<6H", obj, 0x28), (10, 20, 30, 40, 39, 59))
|
|
|
|
|
|
2026-11-16 18:29:12 +01:00
|
|
|
def test_text_timer_touch_cap_and_script(self):
|
|
|
|
|
from nxhmi import fonts
|
|
|
|
|
h = project.new_project("NX4832T035_011", program="int n=0\r\nbaud=115200\r\npage 0\r\n")
|
|
|
|
|
project.add_font(h, fonts.from_ttf(None, 16)) if HAVE_PIL else None
|
|
|
|
|
p = Page.parse(h.read("0.pa"))
|
|
|
|
|
if HAVE_PIL:
|
|
|
|
|
C.add(p, "text", x=5, y=5, w=100, h=30, txt="hi")
|
|
|
|
|
C.add(p, "timer")
|
|
|
|
|
C.add(p, "touch_cap")
|
|
|
|
|
tm = [c for c in p.components if c.get("objname") == "tm0"][0]
|
|
|
|
|
tm.set_event("codestimer", ["if(n<1000)", "{", " n++", "}else", "{", " tm0.en=0", "}"])
|
|
|
|
|
h.write("0.pa", p.pack())
|
|
|
|
|
out = tft_build.build_tft(h, self.tmp)
|
|
|
|
|
self.assertTrue(tft.verify_header(out) and tft.verify_trailer(out))
|
|
|
|
|
lay = tft.layout_table(out)
|
|
|
|
|
self.assertEqual(lay[2], 1) # one global variable
|
|
|
|
|
self.assertEqual(lay[3], lay[0] + 12) # its 12-byte descriptor
|
|
|
|
|
blob = out[lay[5]:lay[5] + lay[0]]
|
|
|
|
|
self.assertIn(b"\x05\x00\x00\x00\x00++", blob) # n++
|
|
|
|
|
self.assertIn(b"timer", blob)
|
|
|
|
|
self.assertIn(b"\x09\x16\x041", blob) # touch cap init line
|
|
|
|
|
if HAVE_PIL:
|
|
|
|
|
self.assertIn(b"\x09\x1c\x085,5,100,30,", blob) # text draw line
|
|
|
|
|
|
2026-11-16 18:13:11 +01:00
|
|
|
def test_name_hash(self):
|
|
|
|
|
self.assertEqual(tft.name_hash("page0"), 0xAC967926) # measured in Editor output
|
|
|
|
|
|
|
|
|
|
def test_unsupported_is_reported(self):
|
|
|
|
|
h = self.project()
|
|
|
|
|
p = Page.parse(h.read("0.pa"))
|
|
|
|
|
C.add(p, "slider", x=1, y=1, w=50, h=20)
|
|
|
|
|
h.write("0.pa", p.pack())
|
|
|
|
|
with self.assertRaises(tft_build.Unsupported):
|
|
|
|
|
tft_build.build_tft(h, self.tmp)
|
2026-11-16 18:29:12 +01:00
|
|
|
# a text component without any font in the project (the Editor refuses to compile that too)
|
|
|
|
|
h = self.project()
|
|
|
|
|
p = Page.parse(h.read("0.pa"))
|
|
|
|
|
C.add(p, "text", x=1, y=1, w=50, h=20)
|
|
|
|
|
h.write("0.pa", p.pack())
|
|
|
|
|
with self.assertRaises(tft_build.Unsupported):
|
|
|
|
|
tft_build.build_tft(h, self.tmp)
|
2026-11-16 18:13:11 +01:00
|
|
|
|
|
|
|
|
def test_cli(self):
|
|
|
|
|
hmi, out = os.path.join(self.tmp, "p.HMI"), os.path.join(self.tmp, "p.tft")
|
|
|
|
|
self.project().save(hmi)
|
|
|
|
|
import contextlib
|
|
|
|
|
import io
|
|
|
|
|
with contextlib.redirect_stdout(io.StringIO()) as text:
|
|
|
|
|
self.assertEqual(cli.main(["build-tft", hmi, out, "--editor-dir", self.tmp]), 0)
|
|
|
|
|
self.assertEqual(cli.main(["tft-info", out]), 0)
|
|
|
|
|
self.assertIn('"trailer_ok": true', text.getvalue())
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
|
|
|
|
|
def test_pictures_and_fonts_blocks(self):
|
|
|
|
|
import io
|
|
|
|
|
from PIL import Image
|
|
|
|
|
from nxhmi import fonts
|
|
|
|
|
h = self.project()
|
|
|
|
|
for size in ((8, 8), (20, 10)):
|
|
|
|
|
b = io.BytesIO()
|
|
|
|
|
Image.new("RGB", size, (9, 9, 9)).save(b, "PNG")
|
|
|
|
|
project.add_picture(h, b.getvalue())
|
|
|
|
|
project.add_font(h, fonts.from_ttf(None, 16))
|
|
|
|
|
out = tft_build.build_tft(h, self.tmp)
|
|
|
|
|
lay = tft.layout_table(out)
|
|
|
|
|
self.assertEqual((lay[15], lay[17]), (2, 1))
|
|
|
|
|
pics = lay[8]
|
|
|
|
|
# two 24-byte headers first; u32@4 = picture number, u32@8 = data offset in the block
|
|
|
|
|
self.assertEqual(struct.unpack_from("<II", out, pics + 4), (0, 48))
|
|
|
|
|
self.assertEqual(struct.unpack_from("<II", out, pics + 28), (1, 48 + len(h.read("0.i")) - 24))
|
|
|
|
|
self.assertEqual(out[lay[12]:lay[12] + 44][:8], h.read("0.zi")[:8])
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(SAMPLES and os.path.isdir(tft_build.DEFAULT_EDITOR_DIR), "needs Editor-made samples")
|
|
|
|
|
def test_against_editor_samples(self):
|
|
|
|
|
from nxhmi.container import HMIFile
|
|
|
|
|
done = 0
|
2026-11-16 23:31:12 +01:00
|
|
|
for ref in sorted(glob.glob(os.path.join(SAMPLES, "**", "*.tft"), recursive=True)):
|
2026-11-16 18:13:11 +01:00
|
|
|
hmi = ref[:-4] + ".HMI"
|
|
|
|
|
if not os.path.exists(hmi):
|
|
|
|
|
continue
|
|
|
|
|
try:
|
|
|
|
|
out = tft_build.build_tft(HMIFile.load(hmi))
|
|
|
|
|
except (tft_build.Unsupported, KeyError):
|
|
|
|
|
continue
|
|
|
|
|
with open(ref, "rb") as fh:
|
|
|
|
|
self.assertEqual(out, fh.read(), os.path.basename(ref))
|
|
|
|
|
done += 1
|
2026-11-16 23:31:12 +01:00
|
|
|
print("%d samples reproduced byte for byte" % done, end=" ")
|
2026-11-16 18:13:11 +01:00
|
|
|
self.assertGreater(done, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|