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