source: josm/trunk/test/unit/org/openstreetmap/josm/gui/dialogs/relation/actions/RelationEditorActionsTest.java@ 14380

Last change on this file since 14380 was 14380, checked in by Don-vip, 5 years ago

fix #16906 - RelationEditorActionsTest: fix for non-headless mode (patch by ris)

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.relation.actions;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertTrue;
6
7import java.awt.Component;
8import java.awt.Container;
9import javax.swing.Icon;
10import javax.swing.JOptionPane;
11import javax.swing.text.JTextComponent;
12
13import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
14import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
15
16import com.google.common.collect.ImmutableMap;
17
18import mockit.Mock;
19import mockit.MockUp;
20
21import org.junit.Test;
22
23/**
24 * Unit tests for relation editor actions.
25 */
26public class RelationEditorActionsTest extends AbstractRelationEditorActionTest {
27
28 /**
29 * Check that all dialog-less actions do not crash.
30 */
31 @Test
32 public void testNoDialogActions() {
33 new AddSelectedAfterSelection(relationEditorAccess).actionPerformed(null);
34 new AddSelectedBeforeSelection(relationEditorAccess).actionPerformed(null);
35 new AddSelectedAtStartAction(relationEditorAccess).actionPerformed(null);
36 new AddSelectedAtEndAction(relationEditorAccess).actionPerformed(null);
37
38 new ApplyAction(relationEditorAccess).actionPerformed(null);
39 new RefreshAction(relationEditorAccess).actionPerformed(null);
40 new OKAction(relationEditorAccess).actionPerformed(null);
41 new CancelAction(relationEditorAccess).actionPerformed(null);
42
43 new CopyMembersAction(relationEditorAccess).actionPerformed(null);
44 new PasteMembersAction(relationEditorAccess).actionPerformed(null);
45
46 new SelectAction(relationEditorAccess).actionPerformed(null);
47
48 new DownloadIncompleteMembersAction(relationEditorAccess, "downloadincomplete").actionPerformed(null);
49 new DownloadSelectedIncompleteMembersAction(relationEditorAccess).actionPerformed(null);
50
51 new DuplicateRelationAction(relationEditorAccess).actionPerformed(null);
52 new EditAction(relationEditorAccess).actionPerformed(null);
53
54 new MoveDownAction(relationEditorAccess, "movedown").actionPerformed(null);
55 new MoveUpAction(relationEditorAccess, "moveup").actionPerformed(null);
56 new RemoveAction(relationEditorAccess, "remove").actionPerformed(null);
57
58 new RemoveSelectedAction(relationEditorAccess).actionPerformed(null);
59 new SelectedMembersForSelectionAction(relationEditorAccess).actionPerformed(null);
60
61 new SelectPrimitivesForSelectedMembersAction(relationEditorAccess).actionPerformed(null);
62
63 new SortAction(relationEditorAccess).actionPerformed(null);
64 new SortBelowAction(relationEditorAccess).actionPerformed(null);
65 new ReverseAction(relationEditorAccess).actionPerformed(null);
66 }
67
68 /**
69 * Test DeleteCurrentRelationAction
70 */
71 @Test
72 public void testDeleteCurrentRelationAction() {
73 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
74 ImmutableMap.<String, Object>of(
75 "<html>\n <head>\n \n </head>\n <body>\n You are about to delete 1 "
76 + "relation:\n\n "
77 + "<ul>\n <li>\n incomplete\n </li>\n </ul>\n <br>\n "
78 + "This step is rarely necessary and cannot be undone easily after being \n "
79 + "uploaded to the server.<br>Do you really want to delete?\n </body>\n</html>\n", JOptionPane.YES_OPTION,
80 "<html>\n <head>\n \n </head>\n <body>\n You are about to delete incomplete "
81 + "objects.<br>This will cause problems \n because you don\'t see the real object.<br>"
82 + "Do you really want to delete?\n </body>\n</html>\n",
83 JOptionPane.YES_OPTION
84 )
85 ) {
86 public String getStringFromOriginalMessage(Object originalMessage) {
87 return ((JTextComponent) ((Container) originalMessage).getComponent(0)).getText();
88 }
89 };
90
91 new DeleteCurrentRelationAction(relationEditorAccess).actionPerformed(null);
92
93 assertEquals(2, jopsMocker.getInvocationLog().size());
94
95 Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
96 assertEquals(JOptionPane.YES_OPTION, (int) invocationLogEntry[0]);
97 assertEquals("Delete relation?", invocationLogEntry[2]);
98
99 invocationLogEntry = jopsMocker.getInvocationLog().get(1);
100 assertEquals(JOptionPane.YES_OPTION, (int) invocationLogEntry[0]);
101 assertEquals("Delete confirmation", invocationLogEntry[2]);
102 }
103
104 /**
105 * Test SetRoleAction
106 */
107 @Test
108 public void testSetRoleAction() {
109 final JOptionPaneSimpleMocker.MessagePanelMocker mpMocker = new JOptionPaneSimpleMocker.MessagePanelMocker();
110 // JOptionPaneSimpleMocker doesn't handle showOptionDialog calls because of their potential
111 // complexity, but this is quite a simple use of showOptionDialog which we can mock from scratch.
112 final boolean[] jopMockerCalled = new boolean[] {false};
113 final MockUp<JOptionPane> jopMocker = new MockUp<JOptionPane>() {
114 @Mock
115 public int showOptionDialog(
116 Component parentComponent,
117 Object message,
118 String title,
119 int optionType,
120 int messageType,
121 Icon icon,
122 Object[] options,
123 Object initialValue
124 ) {
125 assertEquals(
126 "<html>You are setting an empty role on 0 objects.<br>This is equal to deleting the "
127 + "roles of these objects.<br>Do you really want to apply the new role?</html>",
128 mpMocker.getOriginalMessage((ConditionalOptionPaneUtil.MessagePanel) message).toString()
129 );
130 assertEquals(
131 "Confirm empty role",
132 title
133 );
134 jopMockerCalled[0] = true;
135 return JOptionPane.YES_OPTION;
136 }
137 };
138
139 new SetRoleAction(relationEditorAccess).actionPerformed(null);
140
141 assertTrue(jopMockerCalled[0]);
142 }
143}
Note: See TracBrowser for help on using the repository browser.