source: josm/trunk/test/unit/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditorTest.java @ 12933

Last change on this file since 12933 was 12933, checked in by Don-vip, 2 weeks ago

fix #15368 - Button that selects the relation from relation editor

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.relation;
3
4import static org.junit.Assert.assertNotNull;
5import static org.junit.Assert.assertNull;
6
7import java.util.Collections;
8
9import javax.swing.JPanel;
10
11import org.junit.Rule;
12import org.junit.Test;
13import org.openstreetmap.josm.data.osm.DataSet;
14import org.openstreetmap.josm.data.osm.Node;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.Relation;
17import org.openstreetmap.josm.data.osm.Way;
18import org.openstreetmap.josm.gui.dialogs.relation.GenericRelationEditor.LeftButtonToolbar;
19import org.openstreetmap.josm.gui.dialogs.relation.actions.ApplyAction;
20import org.openstreetmap.josm.gui.dialogs.relation.actions.CancelAction;
21import org.openstreetmap.josm.gui.dialogs.relation.actions.DeleteCurrentRelationAction;
22import org.openstreetmap.josm.gui.dialogs.relation.actions.DuplicateRelationAction;
23import org.openstreetmap.josm.gui.dialogs.relation.actions.OKAction;
24import org.openstreetmap.josm.gui.dialogs.relation.actions.RefreshAction;
25import org.openstreetmap.josm.gui.dialogs.relation.actions.SelectAction;
26import org.openstreetmap.josm.gui.layer.OsmDataLayer;
27import org.openstreetmap.josm.gui.tagging.TagEditorModel;
28import org.openstreetmap.josm.gui.tagging.TagEditorPanel;
29import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
30import org.openstreetmap.josm.testutils.JOSMTestRules;
31
32import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
33
34/**
35 * Unit tests of {@link GenericRelationEditor} class.
36 */
37public class GenericRelationEditorTest {
38
39    /**
40     * Setup test.
41     */
42    @Rule
43    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
44    public JOSMTestRules test = new JOSMTestRules().preferences().platform().main();
45
46    /**
47     * Returns a new relation editor for unit tests.
48     * @param orig relation
49     * @param layer data layer
50     * @return new relation editor for unit tests
51     */
52    public static IRelationEditor newRelationEditor(final Relation orig, final OsmDataLayer layer) {
53        return new IRelationEditor() {
54            private Relation r = orig;
55
56            @Override
57            public void setRelation(Relation relation) {
58                r = relation;
59            }
60
61            @Override
62            public boolean isDirtyRelation() {
63                return false;
64            }
65
66            @Override
67            public Relation getRelationSnapshot() {
68                return r;
69            }
70
71            @Override
72            public Relation getRelation() {
73                return r;
74            }
75
76            @Override
77            public void reloadDataFromRelation() {
78                // Do nothing
79            }
80
81            @Override
82            public OsmDataLayer getLayer() {
83                return layer;
84            }
85        };
86    }
87
88    /**
89     * Unit test of {@link GenericRelationEditor#addPrimitivesToRelation}.
90     */
91    @Test
92    public void testAddPrimitivesToRelation() {
93        Relation r = new Relation(1);
94        new DataSet(r);
95        assertNull(GenericRelationEditor.addPrimitivesToRelation(r, Collections.<OsmPrimitive>emptyList()));
96        assertNull(GenericRelationEditor.addPrimitivesToRelation(r, Collections.singleton(new Relation(1))));
97
98        assertNotNull(GenericRelationEditor.addPrimitivesToRelation(r, Collections.singleton(new Node(1))));
99        assertNotNull(GenericRelationEditor.addPrimitivesToRelation(r, Collections.singleton(new Way(1))));
100        assertNotNull(GenericRelationEditor.addPrimitivesToRelation(r, Collections.singleton(new Relation(2))));
101    }
102
103    /**
104     * Unit test of {@code GenericRelationEditor#build*} methods.
105     */
106    @Test
107    public void testBuild() {
108        DataSet ds = new DataSet();
109        Relation relation = new Relation(1);
110        ds.addPrimitive(relation);
111        OsmDataLayer layer = new OsmDataLayer(ds, "test", null);
112        IRelationEditor re = newRelationEditor(relation, layer);
113
114        MemberTableModel memberTableModel = new MemberTableModel(relation, layer, null);
115        MemberTable memberTable = new MemberTable(layer, relation, memberTableModel);
116
117        SelectionTableModel selectionTableModel = new SelectionTableModel(layer);
118        SelectionTable selectionTable = new SelectionTable(selectionTableModel, memberTableModel);
119
120        LeftButtonToolbar leftButtonToolbar = new LeftButtonToolbar(memberTable, memberTableModel, re);
121        assertNotNull(leftButtonToolbar.sortBelowButton);
122
123        AutoCompletingTextField tfRole = GenericRelationEditor.buildRoleTextField(re);
124        assertNotNull(tfRole);
125
126        TagEditorPanel tagEditorPanel = new TagEditorPanel(relation, null);
127
128        JPanel top = GenericRelationEditor.buildTagEditorPanel(tagEditorPanel);
129        JPanel bottom = GenericRelationEditor.buildMemberEditorPanel(
130                memberTable, memberTableModel, selectionTable, selectionTableModel, re, leftButtonToolbar, tfRole);
131        assertNotNull(top);
132        assertNotNull(bottom);
133        assertNotNull(GenericRelationEditor.buildSplitPane(top, bottom, re));
134
135        TagEditorModel tagModel = tagEditorPanel.getModel();
136
137        assertNotNull(GenericRelationEditor.buildOkCancelButtonPanel(
138                new OKAction(memberTable, memberTableModel, tagModel, layer, re, tfRole),
139                new CancelAction(memberTable, memberTableModel, tagModel, layer, re, tfRole)));
140        assertNotNull(GenericRelationEditor.buildSelectionControlButtonToolbar(memberTable, memberTableModel, selectionTableModel, re));
141        assertNotNull(GenericRelationEditor.buildSelectionTablePanel(selectionTable));
142
143        assertNotNull(GenericRelationEditor.buildToolBar(
144                new RefreshAction(memberTable, memberTableModel, tagModel, layer, re),
145                new ApplyAction(memberTable, memberTableModel, tagModel, layer, re),
146                new SelectAction(layer, re),
147                new DuplicateRelationAction(memberTableModel, tagModel, layer),
148                new DeleteCurrentRelationAction(layer, re)));
149    }
150}
Note: See TracBrowser for help on using the repository browser.