1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.properties;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
7 |
|
---|
8 | import java.awt.GraphicsEnvironment;
|
---|
9 | import java.lang.reflect.Field;
|
---|
10 | import java.lang.reflect.Method;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Map;
|
---|
17 | import java.util.function.Function;
|
---|
18 | import java.util.stream.Collectors;
|
---|
19 |
|
---|
20 | import javax.swing.JTable;
|
---|
21 | import javax.swing.table.DefaultTableModel;
|
---|
22 |
|
---|
23 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
24 | import org.junit.jupiter.api.Test;
|
---|
25 | import org.openstreetmap.josm.TestUtils;
|
---|
26 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
27 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
28 | import org.openstreetmap.josm.data.osm.Node;
|
---|
29 | import org.openstreetmap.josm.data.osm.OsmDataManager;
|
---|
30 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
31 | import org.openstreetmap.josm.data.osm.Way;
|
---|
32 | import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem;
|
---|
33 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
34 | import org.openstreetmap.josm.gui.dialogs.properties.TagEditHelper.AddTagsDialog;
|
---|
35 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
36 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
37 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
---|
38 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
39 | import org.openstreetmap.josm.testutils.mockers.WindowMocker;
|
---|
40 |
|
---|
41 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Unit tests of {@link TagEditHelper} class.
|
---|
45 | */
|
---|
46 | class TagEditHelperTest {
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Setup tests
|
---|
50 | */
|
---|
51 | @RegisterExtension
|
---|
52 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
53 | public JOSMTestRules test = new JOSMTestRules().territories().projection();
|
---|
54 |
|
---|
55 | private static TagEditHelper newTagEditHelper() {
|
---|
56 | DefaultTableModel propertyData = new DefaultTableModel();
|
---|
57 | JTable tagTable = new JTable(propertyData);
|
---|
58 | Map<String, Map<String, Integer>> valueCount = new HashMap<>();
|
---|
59 | return new TagEditHelper(tagTable, propertyData, valueCount);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Checks that autocompleting list items are sorted correctly.
|
---|
64 | */
|
---|
65 | @Test
|
---|
66 | void testAcItemComparator() {
|
---|
67 | List<AutoCompletionItem> list = new ArrayList<>();
|
---|
68 | list.add(new AutoCompletionItem("Bing Sat"));
|
---|
69 | list.add(new AutoCompletionItem("survey"));
|
---|
70 | list.add(new AutoCompletionItem("Bing"));
|
---|
71 | list.add(new AutoCompletionItem("digitalglobe"));
|
---|
72 | list.add(new AutoCompletionItem("bing"));
|
---|
73 | list.add(new AutoCompletionItem("DigitalGlobe"));
|
---|
74 | list.sort(TagEditHelper.DEFAULT_AC_ITEM_COMPARATOR);
|
---|
75 | assertEquals(Arrays.asList("Bing", "bing", "Bing Sat", "digitalglobe", "DigitalGlobe", "survey"),
|
---|
76 | list.stream().map(AutoCompletionItem::getValue).collect(Collectors.toList()));
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Unit test of {@link TagEditHelper#containsDataKey}.
|
---|
81 | */
|
---|
82 | @Test
|
---|
83 | void testContainsDataKey() {
|
---|
84 | assertFalse(newTagEditHelper().containsDataKey("foo"));
|
---|
85 | // TODO: complete test
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/18764>#18764</a>
|
---|
90 | *
|
---|
91 | * @throws Exception if any error occurs
|
---|
92 | */
|
---|
93 | @Test
|
---|
94 | void testTicket18764() throws Exception {
|
---|
95 | testIcon("*[building] ⧉ *[highway] { text: tr(\"Building crossing highway\"); }", ds -> {
|
---|
96 | Way way = TestUtils.newWay("", new Node(LatLon.NORTH_POLE), new Node(LatLon.SOUTH_POLE));
|
---|
97 | way.getNodes().forEach(ds::addPrimitive);
|
---|
98 | return way;
|
---|
99 | }, "highway", "");
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/18798>#18798</a>
|
---|
104 | *
|
---|
105 | * @throws Exception if any error occurs
|
---|
106 | */
|
---|
107 | @Test
|
---|
108 | void testTicket18798() throws Exception {
|
---|
109 | testIcon("node:righthandtraffic[junction=roundabout] { text: tr(\"Roundabout node\"); }", ds -> {
|
---|
110 | Node node = new Node(LatLon.NORTH_POLE);
|
---|
111 | ds.addPrimitive(node);
|
---|
112 | return node;
|
---|
113 | }, "junction", "roundabout");
|
---|
114 | }
|
---|
115 |
|
---|
116 | void testIcon(String cssString, Function<DataSet, OsmPrimitive> prepare, String key, String value) throws Exception {
|
---|
117 | TestUtils.assumeWorkingJMockit();
|
---|
118 | if (GraphicsEnvironment.isHeadless()) {
|
---|
119 | new WindowMocker();
|
---|
120 | }
|
---|
121 | MapCSSStyleSource css = new MapCSSStyleSource(cssString);
|
---|
122 | css.loadStyleSource();
|
---|
123 | MapPaintStyles.addStyle(css);
|
---|
124 | DataSet ds = new DataSet();
|
---|
125 | final OsmPrimitive primitive = prepare.apply(ds);
|
---|
126 | OsmDataManager.getInstance().setActiveDataSet(ds);
|
---|
127 | MainApplication.getLayerManager().addLayer(new OsmDataLayer(ds, "Test Layer", null));
|
---|
128 | TagEditHelper helper = newTagEditHelper();
|
---|
129 | Field sel = TagEditHelper.class.getDeclaredField("sel");
|
---|
130 | sel.set(helper, Collections.singletonList(primitive));
|
---|
131 | AddTagsDialog addTagsDialog = helper.getAddTagsDialog();
|
---|
132 | Method findIcon = TagEditHelper.AbstractTagsDialog.class.getDeclaredMethod("findIcon", String.class, String.class);
|
---|
133 | findIcon.setAccessible(true);
|
---|
134 | Object val = findIcon.invoke(addTagsDialog, key, value);
|
---|
135 | assertNotNull(val);
|
---|
136 | }
|
---|
137 | }
|
---|