source: josm/trunk/test/unit/org/openstreetmap/josm/gui/tagging/presets/items/ComboTest.java@ 16693

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

see #16031 - Fix ComboTest.testUseLastAsDefault

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging.presets.items;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertTrue;
6
7import java.awt.Color;
8import java.util.Arrays;
9import java.util.Collections;
10
11import javax.swing.JPanel;
12
13import org.junit.Rule;
14import org.junit.Test;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.OsmUtils;
17import org.openstreetmap.josm.testutils.JOSMTestRules;
18
19import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20
21/**
22 * Unit tests of {@link Combo} class.
23 */
24public class ComboTest {
25
26 /**
27 * Setup test.
28 */
29 @Rule
30 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
31 public JOSMTestRules test = new JOSMTestRules().main().i18n("de");
32
33 /**
34 * Unit test for {@link Combo#addToPanel}.
35 */
36 @Test
37 public void testAddToPanel() {
38 JPanel p = new JPanel();
39 assertEquals(0, p.getComponentCount());
40 assertTrue(new Combo().addToPanel(p, Collections.<OsmPrimitive>emptyList(), false));
41 assertTrue(p.getComponentCount() > 0);
42 }
43
44 /**
45 * Unit test for {@link ComboMultiSelect#use_last_as_default} and {@link ComboMultiSelect#getItemToSelect}
46 */
47 @Test
48 public void testUseLastAsDefault() {
49 Combo combo = new Combo();
50 combo.key = "addr:country";
51 combo.use_last_as_default = 1;
52 combo.values_from = "java.util.Locale#getISOCountries";
53 OsmPrimitive way = OsmUtils.createPrimitive("way");
54 OsmPrimitive wayAT = OsmUtils.createPrimitive("way addr:country=AT");
55 OsmPrimitive waySI = OsmUtils.createPrimitive("way addr:country=SI");
56
57 combo.addToPanel(new JPanel(), Collections.singleton(way), false);
58 assertEquals("", combo.getSelectedValue());
59
60 combo.default_ = "SI";
61 combo.addToPanel(new JPanel(), Collections.singleton(way), false);
62 assertEquals("SI", combo.getSelectedValue());
63 combo.addToPanel(new JPanel(), Collections.singleton(wayAT), false);
64 assertEquals("AT", combo.getSelectedValue());
65 combo.default_ = null;
66
67 KeyedItem.LAST_VALUES.clear();
68 KeyedItem.LAST_VALUES.put("addr:country", "AT");
69 combo.addToPanel(new JPanel(), Collections.singleton(way), false);
70 assertEquals("AT", combo.getSelectedValue());
71 combo.addToPanel(new JPanel(), Collections.singleton(wayAT), true);
72 assertEquals("AT", combo.getSelectedValue());
73 combo.addToPanel(new JPanel(), Collections.singleton(way), true);
74 assertEquals("", combo.getSelectedValue());
75 combo.use_last_as_default = 2; // "force"
76 combo.addToPanel(new JPanel(), Collections.singleton(way), true);
77 assertEquals("AT", combo.getSelectedValue());
78 KeyedItem.LAST_VALUES.clear();
79
80 combo.addToPanel(new JPanel(), Arrays.asList(wayAT, waySI), true);
81 assertEquals(Combo.DIFFERENT, combo.getSelectedValue());
82 }
83
84 @Test
85 public void testColor() {
86 Combo combo = new Combo();
87 combo.key = "colour";
88 combo.values = "red;green;blue;black";
89 combo.values_context = "color";
90 combo.delimiter = ';';
91 combo.addToPanel(new JPanel(), Collections.<OsmPrimitive>emptyList(), false);
92 assertEquals(5, combo.combobox.getItemCount());
93 combo.presetListEntries.stream().filter(e -> "red".equals(e.value)).findFirst().ifPresent(combo.combobox::setSelectedItem);
94 assertEquals("red", combo.getSelectedValue());
95 assertEquals("Rot", combo.getSelectedItem().toString());
96 assertEquals(new Color(0xFF0000), combo.getColor());
97 combo.presetListEntries.stream().filter(e -> "green".equals(e.value)).findFirst().ifPresent(combo.combobox::setSelectedItem);
98 assertEquals("green", combo.getSelectedValue());
99 assertEquals("Grün", combo.getSelectedItem().toString());
100 assertEquals(new Color(0x008000), combo.getColor());
101 combo.combobox.setSelectedItem("#135");
102 assertEquals("#135", combo.getSelectedValue());
103 assertEquals(new Color(0x113355), combo.getColor());
104 combo.combobox.setSelectedItem("#123456");
105 assertEquals("#123456", combo.getSelectedValue());
106 assertEquals(new Color(0x123456), combo.getColor());
107 combo.setColor(new Color(0x448822));
108 assertEquals("#448822", combo.getSelectedValue());
109 }
110}
Note: See TracBrowser for help on using the repository browser.