package com.ttstd.launcher; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import java.nio.charset.StandardCharsets; import java.util.List; import org.junit.jupiter.api.Test; import org.springframework.core.io.ClassPathResource; import tools.jackson.databind.json.JsonMapper; class LayoutValidatorTest { private final LayoutValidator validator = new LayoutValidator(); private final JsonMapper mapper = JsonMapper.builder().build(); @Test void validatesSettingsActionsAndTypedExtras() throws Exception { var json = """ {"pages":[{"items":[{"id":"wifi","type":"image","x":0,"y":200,"w":100,"h":100, "payload":{"imageId":"%s","settingsAction":"ACTION_WIFI_SETTINGS", "extras":[{"key":"example","type":"long","value":"9223372036854775807"}]}}]}]} """.formatted("a".repeat(64)); var reader = tools.jackson.databind.json.JsonMapper.builder().build(); var validator = new LayoutValidator(); assertTrue(validator.validate(reader.readTree(json)).stream().noneMatch(i -> i.level().equals("error"))); assertTrue(validator.validate(reader.readTree(json.replace("9223372036854775807", "9223372036854775808"))) .stream().anyMatch(i -> i.path().endsWith(".extras"))); assertTrue(validator.validate(reader.readTree(json.replace("ACTION_WIFI_SETTINGS", "ACTION_CALL"))) .stream().anyMatch(i -> i.path().endsWith(".settingsAction"))); var component = json.replace("\"settingsAction\":\"ACTION_WIFI_SETTINGS\"", """ "launchMode":"component","componentName":"com.example.app/.MainActivity" """.strip()); assertTrue(validator.validate(reader.readTree(component)).stream().noneMatch(i -> i.level().equals("error"))); assertTrue(validator.validate(reader.readTree(component.replace("/.MainActivity", ""))) .stream().anyMatch(i -> i.path().endsWith(".componentName"))); } @Test void builtInLayoutHasNoErrors() throws Exception { try (var input = new ClassPathResource("launcher/default_layout.json").getInputStream()) { var layout = mapper.readTree(new String(input.readAllBytes(), StandardCharsets.UTF_8)); assertTrue(validator.validate(layout).stream().noneMatch(issue -> issue.level().equals("error"))); } } @Test void rejectsBrokenProtocolFields() throws Exception { var issues = issues(""" { "schemaVersion": 2, "canvas": {"designWidth": 100, "designHeight": 100, "safeTop": -1, "safeBottom": 0}, "fit": {"mode": "contain", "anchorY": 0.5}, "pages": [{ "index": 0, "background": {"type": "wallpaper"}, "items": [ {"id": "", "type": "app_icon", "x": -1, "y": 0, "w": 0, "h": 10, "z": 1, "payload": {"package": ""}}, {"id": "", "type": "widget", "x": 10, "y": 10, "w": 10, "h": 10, "z": 1, "payload": {"provider": "nocomponent"}} ] }] } """); assertTrue(hasError(issues, "schemaVersion")); assertTrue(hasError(issues, "canvas")); assertTrue(hasError(issues, "pages[0].items[0].id")); assertTrue(hasError(issues, "pages[0].items[0].payload.package")); assertTrue(hasError(issues, "pages[0].items[1].payload.provider")); } @Test void rejectsOversizedCanvasAndEmptyPages() throws Exception { var issues = issues(""" {"schemaVersion":3,"canvas":{"designWidth":9000,"designHeight":9000},"pages":[]} """); assertTrue(hasError(issues, "canvas")); assertTrue(hasError(issues, "pages")); } @Test void warnsOnProtocolEdgeCases() throws Exception { var issues = issues(""" { "schemaVersion": 3, "canvas": {"designWidth": 1080, "designHeight": 1920, "safeTop": 132, "safeBottom": 72}, "fit": {"mode": "unknown", "anchorY": 1.4}, "pages": [ {"index": 1, "background": {"type": "image", "color": "white"}, "items": [{"id": "%s", "type": "mystery", "x": 0, "y": 0, "w": 10, "h": 10, "z": 1, "payload": {"label": "%s", "radius": -1, "intent": "bad"}}]}, {"index": 1, "background": {"type": "gradient", "from": "#ffffff"}, "items": []} ] } """.formatted("x".repeat(65), "标题".repeat(60))); assertTrue(issues.stream().noneMatch(issue -> issue.level().equals("error"))); for (String path : List.of("fit.mode", "fit.anchorY", "pages[0].index", "pages[1].index", "pages[0].background.type", "pages[0].background.color", "pages[1].background", "pages[0].items[0].id", "pages[0].items[0].type", "pages[0].items[0].payload.label", "pages[0].items[0].payload.radius", "pages[0].items[0].payload.intent")) { assertTrue(issues.stream().anyMatch(issue -> issue.level().equals("warning") && issue.path().equals(path)), "缺少告警:" + path); } } @Test void warnsOnSmallSafeAreasLikeTheEditor() throws Exception { var issues = issues(""" {"schemaVersion":3, "canvas":{"designWidth":1080,"designHeight":1920,"safeTop":100,"safeBottom":20}, "pages":[{"index":0,"background":{"type":"wallpaper"},"items":[]}]} """); assertTrue(issues.stream().anyMatch(issue -> issue.level().equals("warning") && issue.path().equals("canvas.safeTop"))); assertTrue(issues.stream().anyMatch(issue -> issue.level().equals("warning") && issue.path().equals("canvas.safeBottom"))); assertTrue(issues.stream().noneMatch(issue -> issue.level().equals("error"))); } @Test void acceptsUnknownTopLevelFields() throws Exception { var issues = issues(""" {"schemaVersion":3,"futureField":{"a":1}, "canvas":{"designWidth":1080,"designHeight":1920}, "pages":[{"index":0,"background":{"type":"wallpaper"},"items":[]}]} """); assertEquals(List.of(), issues.stream().filter(issue -> issue.level().equals("error")).toList()); } private List issues(String json) throws Exception { return validator.validate(mapper.readTree(json)); } private static boolean hasError(List issues, String path) { return issues.stream().anyMatch(issue -> issue.level().equals("error") && issue.path().equals(path)); } }