Ignore:
Timestamp:
2018-10-26T01:55:07+02:00 (6 years ago)
Author:
Don-vip
Message:

fix #16893 - DownloadOpenChangesetsTaskTest: fix for non-headless mode (patch by ris)

File:
1 edited

Legend:

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

    r13435 r14366  
    22package org.openstreetmap.josm.gui.io;
    33
     4import static org.junit.Assert.assertEquals;
    45import static org.junit.Assert.assertNotNull;
    56import static org.junit.Assert.assertNull;
    67import static org.junit.Assert.assertTrue;
    78
     9import java.awt.GraphicsEnvironment;
     10import java.net.URL;
     11
     12import javax.swing.JOptionPane;
    813import javax.swing.JPanel;
    914
    1015import org.junit.Rule;
    1116import org.junit.Test;
     17import org.openstreetmap.josm.TestUtils;
    1218import org.openstreetmap.josm.data.UserIdentityManager;
     19import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard;
    1320import org.openstreetmap.josm.testutils.JOSMTestRules;
     21import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
     22import org.openstreetmap.josm.testutils.mockers.WindowMocker;
     23import org.openstreetmap.josm.tools.UserCancelException;
     24
     25import com.google.common.collect.ImmutableMap;
    1426
    1527import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     28import mockit.Invocation;
     29import mockit.Mock;
     30import mockit.MockUp;
    1631
    1732/**
     
    2540    @Rule
    2641    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    27     public JOSMTestRules test = new JOSMTestRules().preferences().devAPI().timeout(20000);
     42    public JOSMTestRules test = new JOSMTestRules().preferences().devAPI();
    2843
    2944    /**
    30      * Test of {@link DownloadOpenChangesetsTask} class.
     45     * OAuth wizard mocker.
     46     */
     47    public static class OAuthWizardMocker extends MockUp<OAuthAuthorizationWizard> {
     48        /** {@code true} if wizard has been called */
     49        public boolean called;
     50
     51        @Mock
     52        void showDialog() throws UserCancelException {
     53            this.called = true;
     54            throw new UserCancelException();
     55        }
     56
     57        @Mock
     58        void obtainAccessToken(final Invocation invocation, final URL serverUrl) {
     59            if (GraphicsEnvironment.isHeadless()) {
     60                // we can't really let execution proceed any further as construction of the ui
     61                // elements will fail with a mocked Window
     62                this.called = true;
     63                return;
     64            }
     65            // else we can allow a bit more of the code to be covered before we raise
     66            // UserCancelException in showDialog
     67            invocation.proceed(serverUrl);
     68        }
     69    }
     70
     71    /**
     72     * Test of {@link DownloadOpenChangesetsTask} class when anonymous.
    3173     */
    3274    @Test
    33     public void testDownloadOpenChangesetsTask() {
     75    public void testAnonymous() {
     76        TestUtils.assumeWorkingJMockit();
     77        if (GraphicsEnvironment.isHeadless()) {
     78            new WindowMocker();
     79        }
     80        final OAuthWizardMocker oaWizardMocker = new OAuthWizardMocker();
     81        final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
     82            ImmutableMap.<String, Object>of(
     83                "<html>Could not retrieve the list of your open changesets because<br>JOSM does not know "
     84                + "your identity.<br>You have either chosen to work anonymously or you are not "
     85                + "entitled<br>to know the identity of the user on whose behalf you are working.</html>", JOptionPane.OK_OPTION
     86            )
     87        );
     88
    3489        DownloadOpenChangesetsTask task = new DownloadOpenChangesetsTask(new JPanel());
    3590        assertNull(task.getChangesets());
     
    3994        assertNull(task.getChangesets());
    4095
    41         task = new DownloadOpenChangesetsTask(new JPanel());
     96        assertEquals(1, jopsMocker.getInvocationLog().size());
     97        Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
     98        assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
     99        assertEquals("Missing user identity", invocationLogEntry[2]);
     100
     101        assertTrue(oaWizardMocker.called);
     102    }
     103
     104    /**
     105     * Test of {@link DownloadOpenChangesetsTask} class when "partially identified".
     106     */
     107    @Test
     108    public void testPartiallyIdentified() {
     109        TestUtils.assumeWorkingJMockit();
     110        if (GraphicsEnvironment.isHeadless()) {
     111            new WindowMocker();
     112        }
     113        final OAuthWizardMocker oaWizardMocker = new OAuthWizardMocker();
     114        final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
     115            ImmutableMap.<String, Object>of("There are no open changesets", JOptionPane.OK_OPTION)
     116        );
     117
     118        DownloadOpenChangesetsTask task = new DownloadOpenChangesetsTask(new JPanel());
    42119        UserIdentityManager.getInstance().setPartiallyIdentified(System.getProperty("osm.username", "josm_test"));
    43120        assertTrue(UserIdentityManager.getInstance().isPartiallyIdentified());
    44121        task.run();
    45122        assertNotNull(task.getChangesets());
     123
     124        assertEquals(1, jopsMocker.getInvocationLog().size());
     125        Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
     126        assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
     127        assertEquals("No open changesets", invocationLogEntry[2]);
     128
     129        assertTrue(oaWizardMocker.called);
    46130    }
    47131}
Note: See TracChangeset for help on using the changeset viewer.