Ticket #16906: v1-0002-RelationEditorActionsTest-fix-for-non-headless-mo.patch
| File v1-0002-RelationEditorActionsTest-fix-for-non-headless-mo.patch, 9.0 KB (added by , 7 years ago) |
|---|
-
src/org/openstreetmap/josm/gui/ConditionalOptionPaneUtil.java
From 0646ed443239ec4213af0fa7141d68a1ce97542c Mon Sep 17 00:00:00 2001 From: Robert Scott <code@humanleg.org.uk> Date: Sun, 28 Oct 2018 13:35:42 +0000 Subject: [PATCH v1 2/2] RelationEditorActionsTest: fix for non-headless mode Splitting DeleteCurrentRelationAction and SetRoleAction out into their own tests --- .../josm/gui/ConditionalOptionPaneUtil.java | 16 +--- .../actions/RelationEditorActionsTest.java | 97 +++++++++++++++++++++- 2 files changed, 96 insertions(+), 17 deletions(-) diff --git a/src/org/openstreetmap/josm/gui/ConditionalOptionPaneUtil.java b/src/org/openstreetmap/josm/gui/ConditionalOptionPaneUtil.java index 0f4d3ac0a..3a304e341 100644
a b public final class ConditionalOptionPaneUtil { 115 115 * 116 116 * @return the option selected by user. 117 117 * {@link JOptionPane#CLOSED_OPTION} if the dialog was closed. 118 * {@link JOptionPane#YES_OPTION} if <code>GraphicsEnvironment.isHeadless</code> returns <code>true</code>119 118 */ 120 119 public static int showOptionDialog(String preferenceKey, Component parent, Object message, String title, int optionType, 121 120 int messageType, Object[] options, Object defaultOption) { … … public final class ConditionalOptionPaneUtil { 123 122 if (isYesOrNo(ret)) 124 123 return ret; 125 124 MessagePanel pnl = new MessagePanel(message, isInBulkOperation(preferenceKey)); 126 if (GraphicsEnvironment.isHeadless()) { 127 // for unit tests 128 ret = JOptionPane.YES_OPTION; 129 } else { 130 ret = JOptionPane.showOptionDialog(parent, pnl, title, optionType, messageType, null, options, defaultOption); 131 } 125 ret = JOptionPane.showOptionDialog(parent, pnl, title, optionType, messageType, null, options, defaultOption); 132 126 if (isYesOrNo(ret)) { 133 127 pnl.getNotShowAgain().store(preferenceKey, ret); 134 128 } … … public final class ConditionalOptionPaneUtil { 161 155 * 162 156 * 163 157 * @return true, if the selected option is equal to <code>trueOption</code>, otherwise false. 164 * {@code trueOption} if <code>GraphicsEnvironment.isHeadless</code> returns <code>true</code>165 158 * 166 159 * @see JOptionPane#INFORMATION_MESSAGE 167 160 * @see JOptionPane#WARNING_MESSAGE … … public final class ConditionalOptionPaneUtil { 173 166 if (isYesOrNo(ret)) 174 167 return ret == trueOption; 175 168 MessagePanel pnl = new MessagePanel(message, isInBulkOperation(preferenceKey)); 176 if (GraphicsEnvironment.isHeadless()) { 177 // for unit tests 178 ret = trueOption; 179 } else { 180 ret = JOptionPane.showConfirmDialog(parent, pnl, title, optionType, messageType); 181 } 169 ret = JOptionPane.showConfirmDialog(parent, pnl, title, optionType, messageType); 182 170 if (isYesOrNo(ret)) { 183 171 pnl.getNotShowAgain().store(preferenceKey, ret); 184 172 } -
test/unit/org/openstreetmap/josm/gui/dialogs/relation/actions/RelationEditorActionsTest.java
diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/relation/actions/RelationEditorActionsTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/relation/actions/RelationEditorActionsTest.java index 87c18675e..a5e06b227 100644
a b 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.gui.dialogs.relation.actions; 3 3 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertTrue; 6 7 import java.awt.Component; 8 import java.awt.Container; 9 import javax.swing.Icon; 10 import javax.swing.JOptionPane; 11 import javax.swing.text.JTextComponent; 12 13 import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker; 14 import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil; 15 16 import com.google.common.collect.ImmutableMap; 17 18 import mockit.Mock; 19 import mockit.MockUp; 20 4 21 import org.junit.Test; 5 22 6 23 /** … … import org.junit.Test; 9 26 public class RelationEditorActionsTest extends AbstractRelationEditorActionTest { 10 27 11 28 /** 12 * Check that all actions do not crash.29 * Check that all dialog-less actions do not crash. 13 30 */ 14 31 @Test 15 public void test AllActions() {32 public void testNoDialogActions() { 16 33 new AddSelectedAfterSelection(relationEditorAccess).actionPerformed(null); 17 34 new AddSelectedBeforeSelection(relationEditorAccess).actionPerformed(null); 18 35 new AddSelectedAtStartAction(relationEditorAccess).actionPerformed(null); … … public class RelationEditorActionsTest extends AbstractRelationEditorActionTest 27 44 new PasteMembersAction(relationEditorAccess).actionPerformed(null); 28 45 29 46 new SelectAction(relationEditorAccess).actionPerformed(null); 30 new DeleteCurrentRelationAction(relationEditorAccess).actionPerformed(null);31 47 32 48 new DownloadIncompleteMembersAction(relationEditorAccess, "downloadincomplete").actionPerformed(null); 33 49 new DownloadSelectedIncompleteMembersAction(relationEditorAccess).actionPerformed(null); … … public class RelationEditorActionsTest extends AbstractRelationEditorActionTest 47 63 new SortAction(relationEditorAccess).actionPerformed(null); 48 64 new SortBelowAction(relationEditorAccess).actionPerformed(null); 49 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 112 // from scratch. 113 final boolean[] jopMockerCalled = new boolean[] { false }; 114 final MockUp<JOptionPane> jopMocker = new MockUp<JOptionPane> () { 115 @Mock 116 public int showOptionDialog( 117 Component parentComponent, 118 Object message, 119 String title, 120 int optionType, 121 int messageType, 122 Icon icon, 123 Object[] options, 124 Object initialValue 125 ) { 126 assertEquals( 127 "<html>You are setting an empty role on 0 objects.<br>This is equal to deleting the " 128 + "roles of these objects.<br>Do you really want to apply the new role?</html>", 129 mpMocker.getOriginalMessage((ConditionalOptionPaneUtil.MessagePanel) message).toString() 130 ); 131 assertEquals( 132 "Confirm empty role", 133 title 134 ); 135 jopMockerCalled[0] = true; 136 return JOptionPane.YES_OPTION; 137 } 138 }; 50 139 51 140 new SetRoleAction(relationEditorAccess).actionPerformed(null); 141 142 assertTrue(jopMockerCalled[0]); 52 143 } 53 144 }
