Black-box reverse-engineered .HMI format (container, three checksums, pages, all 31 component types, pictures, fonts), readable JSON, page renderer matching Nextion Editor, demo project built from code and a test suite (66 tests). See docs/COVERAGE.md for status. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
116 lines
4.6 KiB
Python
116 lines
4.6 KiB
Python
import json
|
|
import os
|
|
import shutil
|
|
import struct
|
|
import tempfile
|
|
import unittest
|
|
|
|
from helpers import FIXTURE, HAVE_PIL, demo_project
|
|
|
|
from nxhmi import checksum, models, project
|
|
from nxhmi.container import HMIFile
|
|
from nxhmi.page import Page
|
|
|
|
|
|
class NewProject(unittest.TestCase):
|
|
def test_new_project(self):
|
|
h = project.new_project("NX4832T035_011", "start")
|
|
self.assertEqual(h.names(), ["main.HMI", "Program.s", "0.pa"])
|
|
main = h.read("main.HMI")
|
|
self.assertEqual(checksum.main_crc(main), struct.unpack_from("<I", main, 0)[0])
|
|
self.assertEqual(project.parse_main(main)["resources"], [["pa", "0.pa"]])
|
|
self.assertEqual(Page.parse(h.read("0.pa")).name, "start")
|
|
self.assertEqual(project.project_size(h), (480, 320))
|
|
|
|
def test_every_model(self):
|
|
for name, (mid, series, w, hgt) in models.MODELS.items():
|
|
h = project.new_project(name)
|
|
main = h.read("main.HMI")
|
|
self.assertEqual(struct.unpack_from("<I", main, 16)[0], mid)
|
|
self.assertEqual(main[11], series)
|
|
self.assertEqual(Page.parse(h.read("0.pa")).components[0].get("w"), w)
|
|
|
|
def test_unknown_model(self):
|
|
with self.assertRaises(KeyError):
|
|
project.new_project("NX0000X000_000")
|
|
|
|
def test_add_page_registers_resource(self):
|
|
h = project.new_project()
|
|
project.add_page(h, "two")
|
|
h2 = HMIFile.from_bytes(h.to_bytes())
|
|
self.assertIn(["pa", "1.pa"], project.parse_main(h2.read("main.HMI"))["resources"])
|
|
with self.assertRaises(ValueError):
|
|
project.add_page(h2, "two")
|
|
|
|
def test_resource_order(self):
|
|
main = project.make_main("NX4832T035_011", [("pa", "0.pa")])
|
|
main = project.main_insert_resource(main, "i", "0.i")
|
|
main = project.main_insert_resource(main, "zi", "0.zi")
|
|
main = project.main_insert_resource(main, "i", "1.i")
|
|
self.assertEqual([r[1] for r in project.parse_main(main)["resources"]], ["0.i", "1.i", "0.zi", "0.pa"])
|
|
|
|
|
|
class DecodeEncode(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.mkdtemp()
|
|
|
|
def tearDown(self):
|
|
shutil.rmtree(self.tmp)
|
|
|
|
def test_editor_file_decode_encode_identical(self):
|
|
out = os.path.join(self.tmp, "dec")
|
|
project.decode(FIXTURE, out)
|
|
re = os.path.join(self.tmp, "re.HMI")
|
|
project.encode(out, re)
|
|
a, b = HMIFile.load(FIXTURE), HMIFile.load(re)
|
|
self.assertEqual(sorted(a.names()), sorted(b.names()))
|
|
for n in a.names():
|
|
self.assertEqual(a.read(n), b.read(n), n)
|
|
|
|
def test_readable_json(self):
|
|
out = os.path.join(self.tmp, "dec")
|
|
project.decode(FIXTURE, out)
|
|
pj = json.load(open(os.path.join(out, "pages", "0_home.json"), encoding="utf-8"))
|
|
att = {c["att"][2][2]: {a[0]: a[2] for a in c["att"]} for c in pj["components"]}
|
|
self.assertEqual(att["home"]["sta"], "image")
|
|
self.assertEqual(att["tStatus"]["pco"], "#00ff00")
|
|
self.assertEqual(att["tStatus"]["style"], "border")
|
|
|
|
def test_raw_json(self):
|
|
out = os.path.join(self.tmp, "raw")
|
|
project.decode(FIXTURE, out, readable=False)
|
|
pj = json.load(open(os.path.join(out, "pages", "0_home.json"), encoding="utf-8"))
|
|
self.assertEqual({a[0]: a[2] for a in pj["components"][0]["att"]}["sta"], 2)
|
|
|
|
def test_edit_through_json(self):
|
|
out = os.path.join(self.tmp, "dec")
|
|
project.decode(FIXTURE, out)
|
|
path = os.path.join(out, "pages", "0_home.json")
|
|
pj = json.load(open(path, encoding="utf-8"))
|
|
for c in pj["components"]:
|
|
for a in c["att"]:
|
|
if c["att"][2][2] == "title" and a[0] == "txt":
|
|
a[2] = "changed"
|
|
if c["att"][2][2] == "title" and a[0] == "xcen":
|
|
a[2] = "right"
|
|
json.dump(pj, open(path, "w", encoding="utf-8"))
|
|
re = os.path.join(self.tmp, "re.HMI")
|
|
project.encode(out, re)
|
|
t = Page.parse(HMIFile.load(re).read("0.pa")).component("title")
|
|
self.assertEqual((t.get("txt"), t.get("xcen")), ("changed", 2))
|
|
|
|
|
|
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
|
|
class Demo(unittest.TestCase):
|
|
def test_demo_matches_editor_resave(self):
|
|
"""The demo built by nxhmi is byte-identical to the same project re-saved by Nextion Editor."""
|
|
ours = HMIFile.from_bytes(demo_project().to_bytes())
|
|
editor = HMIFile.load(FIXTURE)
|
|
self.assertEqual(sorted(ours.names()), sorted(editor.names()))
|
|
for n in ours.names():
|
|
self.assertEqual(ours.read(n), editor.read(n), n)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|