#!/usr/bin/env python3 """ UI Layout and Asset Alignment Test Suite Verifies that layout files enforce aligned button heights, prevent line wrapping, and supply valid non-clipping launcher icon assets across all screen densities. """ import os import unittest import xml.etree.ElementTree as ET from PIL import Image BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) class TestUILayoutAndAlignment(unittest.TestCase): def test_sloproot_main_buttons_aligned(self): """Verify that all action buttons in SlopRoot have identical heights and weights.""" layout_path = os.path.join(BASE_DIR, "app/res/layout/main.xml") self.assertTrue(os.path.exists(layout_path)) tree = ET.parse(layout_path) root = tree.getroot() # Find all Button elements buttons = [] for elem in root.iter("Button"): buttons.append(elem) self.assertEqual(len(buttons), 6, "Expected exactly 6 action buttons in SlopRoot") expected_height = "44dp" expected_weight = "1" for btn in buttons: btn_id = btn.attrib.get("{http://schemas.android.com/apk/res/android}id", "") btn_height = btn.attrib.get("{http://schemas.android.com/apk/res/android}layout_height", "") btn_weight = btn.attrib.get("{http://schemas.android.com/apk/res/android}layout_weight", "") btn_single_line = btn.attrib.get("{http://schemas.android.com/apk/res/android}singleLine", "") self.assertEqual(btn_height, expected_height, f"Button {btn_id} must have height {expected_height}") self.assertEqual(btn_weight, expected_weight, f"Button {btn_id} must have weight {expected_weight}") self.assertEqual(btn_single_line, "true", f"Button {btn_id} must have singleLine=true") def test_sumanager_main_buttons_aligned(self): """Verify that tabs and action buttons in SuManager have uniform heights and singleLine.""" layout_path = os.path.join(BASE_DIR, "sumanager/res/layout/activity_main.xml") self.assertTrue(os.path.exists(layout_path)) tree = ET.parse(layout_path) root = tree.getroot() # Check tabs tab_ids = ["@+id/tab_btn_apps", "@+id/tab_btn_logs", "@+id/tab_btn_settings"] for tab_id in tab_ids: found = False for btn in root.iter("Button"): if btn.attrib.get("{http://schemas.android.com/apk/res/android}id") == tab_id: found = True self.assertEqual(btn.attrib.get("{http://schemas.android.com/apk/res/android}layout_height"), "40dp") self.assertEqual(btn.attrib.get("{http://schemas.android.com/apk/res/android}layout_weight"), "1") self.assertEqual(btn.attrib.get("{http://schemas.android.com/apk/res/android}singleLine"), "true") self.assertTrue(found, f"Tab {tab_id} not found in activity_main.xml") # Check action buttons in view_apps action_ids = ["@+id/btn_check_root", "@+id/btn_terminal"] for action_id in action_ids: found = False for btn in root.iter("Button"): if btn.attrib.get("{http://schemas.android.com/apk/res/android}id") == action_id: found = True self.assertEqual(btn.attrib.get("{http://schemas.android.com/apk/res/android}layout_height"), "44dp") self.assertEqual(btn.attrib.get("{http://schemas.android.com/apk/res/android}layout_weight"), "1") self.assertEqual(btn.attrib.get("{http://schemas.android.com/apk/res/android}singleLine"), "true") self.assertTrue(found, f"Button {action_id} not found in activity_main.xml") def test_button_strings_fit_without_wrap(self): """Verify that button string lengths are under 15 characters to prevent awkward wrapping.""" # SlopRoot strings slop_strings_path = os.path.join(BASE_DIR, "app/res/values/strings.xml") tree = ET.parse(slop_strings_path) for string_elem in tree.getroot().iter("string"): name = string_elem.attrib.get("name", "") if name.startswith("action_"): text = string_elem.text or "" self.assertLessEqual(len(text), 14, f"Action string '{name}' ({text}) is too long ({len(text)} > 14)") # SuManager strings su_strings_path = os.path.join(BASE_DIR, "sumanager/res/values/strings.xml") tree = ET.parse(su_strings_path) for string_elem in tree.getroot().iter("string"): name = string_elem.attrib.get("name", "") if name.startswith("tab_") or name.startswith("btn_"): text = string_elem.text or "" self.assertLessEqual(len(text), 14, f"SuManager string '{name}' ({text}) is too long ({len(text)} > 14)") def test_icon_assets_dimensions_and_channels(self): """Verify that all density icon PNGs have expected square dimensions and 4 channels (RGBA).""" density_expected = { "drawable-ldpi": 36, "drawable-mdpi": 48, "drawable": 48, "drawable-hdpi": 72, "drawable-xhdpi": 96, "drawable-xxhdpi": 144 } for app in ["app", "sumanager"]: for folder, expected_size in density_expected.items(): icon_path = os.path.join(BASE_DIR, app, "res", folder, "icon.png") self.assertTrue(os.path.exists(icon_path), f"Missing icon asset at {icon_path}") with Image.open(icon_path) as im: self.assertEqual(im.size, (expected_size, expected_size), f"{icon_path} dimensions {im.size} != {(expected_size, expected_size)}") self.assertEqual(im.mode, "RGBA", f"{icon_path} mode {im.mode} != RGBA") def test_sumanager_icon_nail_padding(self): """Verify that the nail tip has clearance and does not touch the bottom border.""" icon_path = os.path.join(BASE_DIR, "sumanager/res/drawable-hdpi/icon.png") self.assertTrue(os.path.exists(icon_path)) with Image.open(icon_path) as im: pixels = im.load() w, h = im.size # The nail shaft sits around x=50..56 in 72x72. # Verify clearance between nail tip (ends around y=57) and bottom border (y=68) nail_x_min = int(w * 0.68) nail_x_max = int(w * 0.77) clearance_y_start = int(h * 0.81) clearance_y_end = int(h * 0.92) for y in range(clearance_y_start, clearance_y_end): for x in range(nail_x_min, nail_x_max): r, g, b, a = pixels[x, y] if a > 100 and r > 180 and g > 120 and b < 80: self.fail(f"Yellow/orange nail glyph pixel found in clearance zone at ({x}, {y})") def test_tablet_dual_pane_layouts(self): """Verify that tablet layouts define a 320dp scrollable control pane and 44dp singleLine buttons.""" layouts = [ os.path.join(BASE_DIR, "app/res/layout-large/main.xml"), os.path.join(BASE_DIR, "app/res/layout-sw600dp/main.xml"), os.path.join(BASE_DIR, "sumanager/res/layout-large/activity_main.xml"), os.path.join(BASE_DIR, "sumanager/res/layout-sw600dp/activity_main.xml"), ] for path in layouts: self.assertTrue(os.path.exists(path), f"Missing tablet layout: {path}") tree = ET.parse(path) root = tree.getroot() # Check that left control pane uses 320dp width ScrollView found_320dp = False for elem in root.iter(): width = elem.attrib.get("{http://schemas.android.com/apk/res/android}layout_width", "") if width == "320dp": found_320dp = True break self.assertTrue(found_320dp, f"Layout {path} must define a 320dp control pane") # Check button alignment for btn in root.iter("Button"): height = btn.attrib.get("{http://schemas.android.com/apk/res/android}layout_height", "") single = btn.attrib.get("{http://schemas.android.com/apk/res/android}singleLine", "") self.assertIn(height, ["40dp", "44dp"], f"Button height {height} must be 40dp or 44dp in {path}") self.assertEqual(single, "true", f"Button must have singleLine=true in {path}") def test_sumanager_request_overlay_and_remove_root(self): """Verify RequestActivity is isolated from MainActivity's task and Remove root button exists.""" manifest_path = os.path.join(BASE_DIR, "sumanager/AndroidManifest.xml") ns = "{http://schemas.android.com/apk/res/android}" tree = ET.parse(manifest_path) req_activity = None for act in tree.getroot().iter("activity"): if act.attrib.get(f"{ns}name") == ".RequestActivity": req_activity = act break self.assertIsNotNone(req_activity, "RequestActivity missing from SuManager manifest") self.assertEqual(req_activity.attrib.get(f"{ns}taskAffinity"), "") self.assertEqual(req_activity.attrib.get(f"{ns}excludeFromRecents"), "true") self.assertEqual(req_activity.attrib.get(f"{ns}noHistory"), "true") self.assertEqual(req_activity.attrib.get(f"{ns}launchMode"), "singleInstance") self.assertEqual(req_activity.attrib.get(f"{ns}theme"), "@style/Theme.SuManager.Dialog") for rel in ( "sumanager/res/layout/activity_main.xml", "sumanager/res/layout-large/activity_main.xml", "sumanager/res/layout-sw600dp/activity_main.xml", ): layout_tree = ET.parse(os.path.join(BASE_DIR, rel)) ids = [b.attrib.get(f"{ns}id") for b in layout_tree.getroot().iter("Button")] self.assertIn("@+id/btn_remove_root", ids, f"Missing btn_remove_root in {rel}") if __name__ == "__main__": unittest.main()