Initialize Compose Launcher source and deployment
This commit is contained in:
commit
12a8c24441
77 files changed
+9594
No files matched your search
@@ -0,0 +1,115 @@
|
||||
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 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<LayoutValidator.Issue> issues(String json) throws Exception {
|
||||
return validator.validate(mapper.readTree(json));
|
||||
}
|
||||
|
||||
private static boolean hasError(List<LayoutValidator.Issue> issues, String path) {
|
||||
return issues.stream().anyMatch(issue -> issue.level().equals("error") && issue.path().equals(path));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user