source: josm/trunk/test/unit/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelperTest.java@ 15927

Last change on this file since 15927 was 15927, checked in by simon04, 4 years ago

see #18764, see #14088 - Add non-regression test (fix for headless)

  • Property svn:eol-style set to native
File size: 5.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.properties;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNotNull;
7
8import java.awt.GraphicsEnvironment;
9import java.lang.reflect.Field;
10import java.lang.reflect.InvocationTargetException;
11import java.lang.reflect.Method;
12import java.util.ArrayList;
13import java.util.Arrays;
14import java.util.Collections;
15import java.util.HashMap;
16import java.util.List;
17import java.util.Map;
18import java.util.stream.Collectors;
19
20import javax.swing.JTable;
21import javax.swing.table.DefaultTableModel;
22
23import org.junit.Rule;
24import org.junit.Test;
25import org.openstreetmap.josm.TestUtils;
26import org.openstreetmap.josm.data.coor.LatLon;
27import org.openstreetmap.josm.data.osm.DataSet;
28import org.openstreetmap.josm.data.osm.Node;
29import org.openstreetmap.josm.data.osm.OsmDataManager;
30import org.openstreetmap.josm.data.osm.Way;
31import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem;
32import org.openstreetmap.josm.gui.MainApplication;
33import org.openstreetmap.josm.gui.dialogs.properties.TagEditHelper.AddTagsDialog;
34import org.openstreetmap.josm.gui.layer.OsmDataLayer;
35import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
36import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
37import org.openstreetmap.josm.testutils.JOSMTestRules;
38import org.openstreetmap.josm.testutils.mockers.WindowMocker;
39
40import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
41
42/**
43 * Unit tests of {@link TagEditHelper} class.
44 */
45public 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}
Note: See TracBrowser for help on using the repository browser.