Ignore:
Timestamp:
2018-10-22T21:16:03+02:00 (6 years ago)
Author:
Don-vip
Message:

fix #16865 - UploadDialogTest: fix for non-headless mode (patch by ris, modified). Probably IUploadDialog should die

Location:
trunk/test/unit/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/gui/io/UploadDialogTest.java

    r12687 r14355  
    22package org.openstreetmap.josm.gui.io;
    33
     4import static org.junit.Assert.assertEquals;
    45import static org.junit.Assert.assertFalse;
    56import static org.junit.Assert.assertTrue;
    67
     8import java.awt.GraphicsEnvironment;
     9import java.util.List;
    710import java.util.Map;
    811import java.util.concurrent.ConcurrentHashMap;
     12import javax.swing.JButton;
     13import javax.swing.JOptionPane;
    914
    1015import org.junit.Rule;
    1116import org.junit.Test;
     17import org.openstreetmap.josm.gui.ExtendedDialog;
     18import org.openstreetmap.josm.TestUtils;
    1219import org.openstreetmap.josm.io.UploadStrategySpecification;
    1320import org.openstreetmap.josm.testutils.JOSMTestRules;
     21import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
     22import org.openstreetmap.josm.testutils.mockers.WindowMocker;
     23
     24import mockit.Invocation;
     25import mockit.Mock;
     26
     27import com.google.common.collect.ImmutableMap;
    1428
    1529import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    2741    public JOSMTestRules test = new JOSMTestRules().preferences();
    2842
    29     private static IUploadDialog newUploadDialog(final String comment, final String source) {
    30         return new IUploadDialog() {
    31 
    32             @Override
    33             public void rememberUserInput() {
    34                 // Do nothing
    35             }
    36 
    37             @Override
    38             public boolean isCanceled() {
    39                 return false;
    40             }
    41 
    42             @Override
    43             public void handleMissingSource() {
    44                 // Do nothing
    45             }
    46 
    47             @Override
    48             public void handleMissingComment() {
    49                 // Do nothing
    50             }
    51 
    52             @Override
    53             public void handleIllegalChunkSize() {
    54                 // Do nothing
    55             }
    56 
    57             @Override
    58             public UploadStrategySpecification getUploadStrategySpecification() {
    59                 return new UploadStrategySpecification();
    60             }
    61 
    62             @Override
    63             public String getUploadSource() {
    64                 return source;
    65             }
    66 
    67             @Override
    68             public String getUploadComment() {
    69                 return comment;
    70             }
    71 
    72             @Override
    73             public Map<String, String> getTags(boolean keepEmpty) {
    74                 return new ConcurrentHashMap<>();
     43    private static class MockUploadDialog extends JOptionPane implements IUploadDialog {
     44        private final String source;
     45        private final String comment;
     46
     47        public int handleMissingCommentCalls;
     48        public int handleMissingSourceCalls;
     49
     50        MockUploadDialog(final String comment, final String source) {
     51            this.source = source;
     52            this.comment = comment;
     53        }
     54
     55        @Override
     56        public void rememberUserInput() {
     57            // Do nothing
     58        }
     59
     60        @Override
     61        public boolean isCanceled() {
     62            return false;
     63        }
     64
     65        @Override
     66        public void handleMissingSource() {
     67            this.handleMissingSourceCalls += 1;
     68        }
     69
     70        @Override
     71        public void handleMissingComment() {
     72            this.handleMissingCommentCalls += 1;
     73        }
     74
     75        @Override
     76        public void handleIllegalChunkSize() {
     77            // Do nothing
     78        }
     79
     80        @Override
     81        public UploadStrategySpecification getUploadStrategySpecification() {
     82            return new UploadStrategySpecification();
     83        }
     84
     85        @Override
     86        public String getUploadSource() {
     87            return source;
     88        }
     89
     90        @Override
     91        public String getUploadComment() {
     92            return comment;
     93        }
     94
     95        @Override
     96        public Map<String, String> getTags(boolean keepEmpty) {
     97            return new ConcurrentHashMap<>();
     98        }
     99    }
     100
     101    /**
     102     * Test of {@link UploadDialog.CancelAction} class.
     103     */
     104    @Test
     105    public void testCancelAction() {
     106        if (GraphicsEnvironment.isHeadless()) {
     107            TestUtils.assumeWorkingJMockit();
     108            new WindowMocker();
     109        }
     110        MockUploadDialog uploadDialog = new MockUploadDialog(null, null);
     111        new UploadDialog.CancelAction(uploadDialog).actionPerformed(null);
     112    }
     113
     114    /**
     115     * Test of {@link UploadDialog.UploadAction} class.
     116     */
     117    @Test
     118    public void testUploadAction() {
     119        TestUtils.assumeWorkingJMockit();
     120        ExtendedDialogMocker edMocker = new ExtendedDialogMocker(
     121            ImmutableMap.<String, Object>of(
     122                "<html>Your upload comment is <i>empty</i>, or <i>very short</i>.<br /><br />This is "
     123                + "technically allowed, but please consider that many users who are<br />watching changes "
     124                + "in their area depend on meaningful changeset comments<br />to understand what is going "
     125                + "on!<br /><br />If you spend a minute now to explain your change, you will make life<br />"
     126                + "easier for many other mappers.</html>", "Revise",
     127                "<html>You did not specify a source for your changes.<br />It is technically allowed, "
     128                + "but this information helps<br />other users to understand the origins of the data."
     129                + "<br /><br />If you spend a minute now to explain your change, you will make life"
     130                + "<br />easier for many other mappers.</html>", "Revise"
     131            )
     132        ) {
     133            @Mock
     134            void setupDialog(Invocation invocation) throws Exception {
     135                if (GraphicsEnvironment.isHeadless()) {
     136                    final int nButtons = ((String[]) TestUtils.getPrivateField(
     137                            ExtendedDialog.class, invocation.getInvokedInstance(), "bTexts")).length;
     138                    @SuppressWarnings("unchecked")
     139                    final List<JButton> buttons = (List<JButton>) TestUtils.getPrivateField(
     140                            ExtendedDialog.class, invocation.getInvokedInstance(), "buttons");
     141
     142                    for (int i = 0; i < nButtons; i++) {
     143                        buttons.add(new JButton());
     144                    }
     145                } else {
     146                    invocation.proceed();
     147                }
    75148            }
    76149        };
    77     }
    78 
    79     /**
    80      * Test of {@link UploadDialog.CancelAction} class.
    81      */
    82     @Test
    83     public void testCancelAction() {
    84         new UploadDialog.CancelAction(newUploadDialog(null, null)).actionPerformed(null);
    85     }
    86 
    87     /**
    88      * Test of {@link UploadDialog.UploadAction} class.
    89      */
    90     @Test
    91     public void testUploadAction() {
    92         new UploadDialog.UploadAction(newUploadDialog("comment", "source")).actionPerformed(null);
    93         new UploadDialog.UploadAction(newUploadDialog("", "source")).actionPerformed(null);
    94         new UploadDialog.UploadAction(newUploadDialog("comment", "")).actionPerformed(null);
    95         new UploadDialog.UploadAction(newUploadDialog("a comment long enough", "a source long enough")).actionPerformed(null);
     150
     151        MockUploadDialog uploadDialog = new MockUploadDialog("comment", "source");
     152        new UploadDialog.UploadAction(uploadDialog).actionPerformed(null);
     153
     154        assertEquals(1, uploadDialog.handleMissingCommentCalls);
     155        assertEquals(0, uploadDialog.handleMissingSourceCalls);
     156        assertEquals(1, edMocker.getInvocationLog().size());
     157        Object[] invocationLogEntry = edMocker.getInvocationLog().get(0);
     158        assertEquals(1, (int) invocationLogEntry[0]);
     159        assertEquals("Please revise upload comment", invocationLogEntry[2]);
     160        edMocker.resetInvocationLog();
     161
     162        uploadDialog = new MockUploadDialog("", "source");
     163        new UploadDialog.UploadAction(uploadDialog).actionPerformed(null);
     164
     165        assertEquals(1, uploadDialog.handleMissingCommentCalls);
     166        assertEquals(0, uploadDialog.handleMissingSourceCalls);
     167        assertEquals(1, edMocker.getInvocationLog().size());
     168        invocationLogEntry = edMocker.getInvocationLog().get(0);
     169        assertEquals(1, (int) invocationLogEntry[0]);
     170        assertEquals("Please revise upload comment", invocationLogEntry[2]);
     171        edMocker.resetInvocationLog();
     172
     173        uploadDialog = new MockUploadDialog("comment", "");
     174        new UploadDialog.UploadAction(uploadDialog).actionPerformed(null);
     175
     176        assertEquals(1, uploadDialog.handleMissingCommentCalls);
     177        assertEquals(0, uploadDialog.handleMissingSourceCalls);
     178        assertEquals(1, edMocker.getInvocationLog().size());
     179        invocationLogEntry = edMocker.getInvocationLog().get(0);
     180        assertEquals(1, (int) invocationLogEntry[0]);
     181        assertEquals("Please revise upload comment", invocationLogEntry[2]);
     182        edMocker.resetInvocationLog();
     183
     184        uploadDialog = new MockUploadDialog("a comment long enough", "");
     185        new UploadDialog.UploadAction(uploadDialog).actionPerformed(null);
     186
     187        assertEquals(0, uploadDialog.handleMissingCommentCalls);
     188        assertEquals(1, uploadDialog.handleMissingSourceCalls);
     189        assertEquals(1, edMocker.getInvocationLog().size());
     190        invocationLogEntry = edMocker.getInvocationLog().get(0);
     191        assertEquals(1, (int) invocationLogEntry[0]);
     192        assertEquals("Please specify a changeset source", invocationLogEntry[2]);
     193        edMocker.resetInvocationLog();
     194
     195        uploadDialog = new MockUploadDialog("a comment long enough", "a source long enough");
     196        new UploadDialog.UploadAction(uploadDialog).actionPerformed(null);
     197
     198        assertEquals(0, uploadDialog.handleMissingCommentCalls);
     199        assertEquals(0, uploadDialog.handleMissingSourceCalls);
     200        assertEquals(0, edMocker.getInvocationLog().size());
    96201    }
    97202
  • trunk/test/unit/org/openstreetmap/josm/testutils/mockers/BaseDialogMockUp.java

    r14052 r14355  
    2626    }
    2727
     28    /**
     29     * Empties the invocation log.
     30     */
     31    public void resetInvocationLog() {
     32        this.invocationLogInternal.clear();
     33    }
     34
    2835    private final List<Object[]> invocationLogInternal = new ArrayList<>(4);
    2936
Note: See TracChangeset for help on using the changeset viewer.