1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
6 | import static org.openstreetmap.josm.data.osm.OsmPrimitiveType.NODE;
|
---|
7 |
|
---|
8 | import java.util.Collections;
|
---|
9 |
|
---|
10 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
11 | import org.junit.jupiter.api.Test;
|
---|
12 | import org.openstreetmap.josm.TestUtils;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
15 | import org.openstreetmap.josm.data.osm.User;
|
---|
16 | import org.openstreetmap.josm.data.preferences.NamedColorProperty;
|
---|
17 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
18 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
19 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
20 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
21 |
|
---|
22 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Unit tests of {@link Functions}.
|
---|
26 | */
|
---|
27 | class FunctionsTest {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Setup rule
|
---|
31 | */
|
---|
32 | @RegisterExtension
|
---|
33 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
34 | public JOSMTestRules test = new JOSMTestRules();
|
---|
35 |
|
---|
36 | private static class EnvBuilder {
|
---|
37 | private final OsmPrimitive osm;
|
---|
38 |
|
---|
39 | EnvBuilder(OsmPrimitiveType type) {
|
---|
40 | switch (type) {
|
---|
41 | case NODE : osm = TestUtils.newNode(""); break;
|
---|
42 | case WAY : osm = TestUtils.newWay(""); break;
|
---|
43 | case RELATION : osm = TestUtils.newRelation(""); break;
|
---|
44 | default: throw new IllegalArgumentException();
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | EnvBuilder setUser(User user) {
|
---|
49 | osm.setUser(user);
|
---|
50 | return this;
|
---|
51 | }
|
---|
52 |
|
---|
53 | Environment build() {
|
---|
54 | return new Environment(osm);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Unit test of {@link Functions#osm_user_name}.
|
---|
60 | */
|
---|
61 | @Test
|
---|
62 | void testOsmUserName() {
|
---|
63 | assertEquals("<anonymous>", Functions.osm_user_name(new EnvBuilder(NODE).setUser(User.getAnonymous()).build()));
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Unit test of {@link Functions#osm_user_id}.
|
---|
68 | */
|
---|
69 | @Test
|
---|
70 | void testOsmUserId() {
|
---|
71 | assertEquals(-1, Functions.osm_user_id(new EnvBuilder(NODE).setUser(User.getAnonymous()).build()));
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Unit test of {@link Functions#osm_version}.
|
---|
76 | */
|
---|
77 | @Test
|
---|
78 | void testOsmVersion() {
|
---|
79 | assertEquals(0, Functions.osm_version(new EnvBuilder(NODE).build()));
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Unit test of {@link Functions#osm_changeset_id}.
|
---|
84 | */
|
---|
85 | @Test
|
---|
86 | void testOsmChangesetId() {
|
---|
87 | assertEquals(0, Functions.osm_changeset_id(new EnvBuilder(NODE).build()));
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Unit test of {@link Functions#osm_timestamp}.
|
---|
92 | */
|
---|
93 | @Test
|
---|
94 | void testOsmTimestamp() {
|
---|
95 | assertEquals(0, Functions.osm_timestamp(new EnvBuilder(NODE).build()));
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Unit test of {@code Functions#to_xxx}
|
---|
100 | */
|
---|
101 | @Test
|
---|
102 | void testParseFunctions() {
|
---|
103 | assertTrue(Functions.to_boolean("true"));
|
---|
104 | assertEquals(1, Functions.to_byte("1"));
|
---|
105 | assertEquals(1, Functions.to_short("1"));
|
---|
106 | assertEquals(1, Functions.to_int("1"));
|
---|
107 | assertEquals(1L, Functions.to_long("1"));
|
---|
108 | assertEquals(1f, Functions.to_float("1"), 1e-10);
|
---|
109 | assertEquals(1d, Functions.to_double("1"), 1e-10);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Unit test of {@link Functions#JOSM_pref}
|
---|
114 | */
|
---|
115 | @Test
|
---|
116 | void testPref() {
|
---|
117 | String key = "Functions.JOSM_pref";
|
---|
118 | Config.getPref().put(key, null);
|
---|
119 | assertEquals("foobar", Functions.JOSM_pref(null, key, "foobar"));
|
---|
120 | Config.getPref().put(key, "baz");
|
---|
121 | GuiHelper.runInEDTAndWait(() -> {
|
---|
122 | // await org.openstreetmap.josm.gui.mappaint.ElemStyles.clearCached
|
---|
123 | });
|
---|
124 | assertEquals("baz", Functions.JOSM_pref(null, key, "foobar"));
|
---|
125 | Config.getPref().put(key, null);
|
---|
126 | GuiHelper.runInEDTAndWait(() -> {
|
---|
127 | // await org.openstreetmap.josm.gui.mappaint.ElemStyles.clearCached
|
---|
128 | });
|
---|
129 | assertEquals("foobar", Functions.JOSM_pref(null, key, "foobar"));
|
---|
130 | Config.getPref().put(key, null);
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Unit test of {@link Functions#JOSM_pref}, color handling
|
---|
135 | */
|
---|
136 | @Test
|
---|
137 | void testPrefColor() {
|
---|
138 | String key = "Functions.JOSM_pref";
|
---|
139 | String colorKey = NamedColorProperty.NAMED_COLOR_PREFIX + NamedColorProperty.COLOR_CATEGORY_MAPPAINT + ".unknown." + key;
|
---|
140 | Config.getPref().put(colorKey, null);
|
---|
141 | assertEquals("#000000", Functions.JOSM_pref(null, key, "#000000"));
|
---|
142 | Config.getPref().putList(colorKey, Collections.singletonList("#00FF00"));
|
---|
143 | GuiHelper.runInEDTAndWait(() -> {
|
---|
144 | // await org.openstreetmap.josm.gui.mappaint.ElemStyles.clearCached
|
---|
145 | });
|
---|
146 | assertEquals("#00FF00", Functions.JOSM_pref(null, key, "#000000"));
|
---|
147 | Config.getPref().put(colorKey, null);
|
---|
148 | GuiHelper.runInEDTAndWait(() -> {
|
---|
149 | // await org.openstreetmap.josm.gui.mappaint.ElemStyles.clearCached
|
---|
150 | });
|
---|
151 | assertEquals("#000000", Functions.JOSM_pref(null, key, "#000000"));
|
---|
152 | Config.getPref().put(colorKey, null);
|
---|
153 | }
|
---|
154 |
|
---|
155 | }
|
---|