1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.internal;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23
24 import java.io.File;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.Set;
28
29 import org.junit.jupiter.api.Test;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Node;
32 import org.w3c.dom.NodeList;
33
34 import com.puppycrawl.tools.checkstyle.internal.utils.XdocUtil;
35 import com.puppycrawl.tools.checkstyle.internal.utils.XmlUtil;
36
37 public class XdocsMobileWrapperTest {
38
39 private static final Set<String> NODES_TO_WRAP = Set.of(
40 "pre",
41 "table",
42 "svg",
43 "img"
44 );
45
46 @Test
47 public void testAllCheckSectionMobileWrapper() throws Exception {
48 for (Path path : XdocUtil.getXdocsFilePaths()) {
49 final File file = path.toFile();
50 final String fileName = file.getName();
51
52 final String input = Files.readString(path);
53 assertWithMessage(fileName + ": input file cannot be empty")
54 .that(input)
55 .isNotEmpty();
56 final Document document = XmlUtil.getRawXml(fileName, input, input);
57 final NodeList sources = document.getElementsByTagName("section");
58
59 for (int position = 0; position < sources.getLength(); position++) {
60 final Node section = sources.item(position);
61 final String sectionName = XmlUtil.getNameAttributeOfNode(section);
62
63 if ("Content".equals(sectionName) || "Overview".equals(sectionName)) {
64 continue;
65 }
66
67 iterateNode(section, fileName, sectionName);
68 }
69 }
70 }
71
72 private static void iterateNode(Node node, String fileName, String sectionName) {
73 for (Node child : XmlUtil.getChildrenElements(node)) {
74 if (NODES_TO_WRAP.contains(child.getNodeName())) {
75 final String wrapperMessage = fileName + "/" + sectionName + ": Tag '"
76 + child.getNodeName() + "' in '" + node.getNodeName()
77 + "' needs a wrapping <span> or <div> with the class 'wrapper'.";
78 assertWithMessage(wrapperMessage)
79 .that(node.getNodeName())
80 .isAnyOf("div", "span");
81 assertWithMessage(wrapperMessage)
82 .that(node.hasAttributes())
83 .isTrue();
84 assertWithMessage(wrapperMessage)
85 .that(node.getAttributes().getNamedItem("class"))
86 .isNotNull();
87 assertWithMessage(wrapperMessage)
88 .that(node.getAttributes().getNamedItem("class")
89 .getNodeValue())
90 .contains("wrapper");
91
92 if ("table".equals(child.getNodeName())) {
93 iterateNode(child, fileName, sectionName);
94 }
95 if ("img".equals(child.getNodeName())) {
96 final String dataImageInlineMessage = fileName + "/" + sectionName + ": img "
97 + "needs the additional class inline if it should be displayed inline "
98 + "or block if scrolling in mobile view should be enabled.";
99 assertWithMessage(dataImageInlineMessage)
100 .that(node.getAttributes().getNamedItem("class")
101 .getNodeValue().matches(".*(block|inline).*"))
102 .isTrue();
103 }
104 }
105 else {
106 iterateNode(child, fileName, sectionName);
107 }
108 }
109 }
110 }