Changeset 14410 in josm for trunk/test/unit/org/openstreetmap/josm/gui/dialogs/changeset
- Timestamp:
- 2018-11-03T22:09:03+01:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetCacheManagerTest.java
r14138 r14410 2 2 package org.openstreetmap.josm.gui.dialogs.changeset; 3 3 4 import static org.junit.Assert.assertEquals; 4 5 import static org.junit.Assert.assertNotNull; 6 import static org.junit.Assert.assertTrue; 7 8 import java.awt.GraphicsEnvironment; 9 import java.awt.event.ActionEvent; 10 import javax.swing.JButton; 11 import javax.swing.JDialog; 5 12 6 13 import java.util.Collections; … … 9 16 import org.junit.Rule; 10 17 import org.junit.Test; 18 import org.openstreetmap.josm.TestUtils; 11 19 import org.openstreetmap.josm.data.osm.Changeset; 12 20 import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.CancelAction; … … 19 27 import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.RemoveFromCacheAction; 20 28 import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.ShowDetailAction; 29 import org.openstreetmap.josm.gui.dialogs.changeset.query.ChangesetQueryDialog; 21 30 import org.openstreetmap.josm.testutils.JOSMTestRules; 31 import org.openstreetmap.josm.testutils.mockers.HelpAwareOptionPaneMocker; 32 import org.openstreetmap.josm.testutils.mockers.WindowMocker; 33 34 import com.google.common.collect.ImmutableMap; 35 36 import mockit.Invocation; 37 import mockit.Mock; 38 import mockit.MockUp; 22 39 23 40 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; … … 103 120 @Test 104 121 public void testDownloadMyChangesets() { 122 TestUtils.assumeWorkingJMockit(); 123 final HelpAwareOptionPaneMocker haMocker = new HelpAwareOptionPaneMocker( 124 ImmutableMap.<String, Object>of( 125 "<html>JOSM is currently running with an anonymous user. It cannot download<br>" 126 + "your changesets from the OSM server unless you enter your OSM user name<br>" 127 + "in the JOSM preferences.</html>", 128 "OK" 129 ) 130 ); 131 105 132 new DownloadMyChangesets().actionPerformed(null); 133 134 assertEquals(1, haMocker.getInvocationLog().size()); 135 Object[] invocationLogEntry = haMocker.getInvocationLog().get(0); 136 assertEquals(0, (int) invocationLogEntry[0]); 137 assertEquals("Warning", invocationLogEntry[2]); 106 138 } 107 139 … … 111 143 @Test 112 144 public void testDownloadSelectedChangesetContentAction() { 145 if (GraphicsEnvironment.isHeadless()) { 146 TestUtils.assumeWorkingJMockit(); 147 // to allow us to construct a JDialog 148 new WindowMocker(); 149 } 150 113 151 DownloadSelectedChangesetContentAction action = new DownloadSelectedChangesetContentAction(ChangesetCacheManager.buildModel()); 114 152 action.valueChanged(null); 115 action.actionPerformed(n ull);153 action.actionPerformed(new ActionEvent(new JDialog().getComponent(0), ActionEvent.ACTION_PERFORMED, "foo")); 116 154 } 117 155 … … 121 159 @Test 122 160 public void testDownloadSelectedChangesetsAction() { 161 if (GraphicsEnvironment.isHeadless()) { 162 TestUtils.assumeWorkingJMockit(); 163 // to allow us to construct a JDialog 164 new WindowMocker(); 165 } 166 123 167 DownloadSelectedChangesetsAction action = new DownloadSelectedChangesetsAction(ChangesetCacheManager.buildModel()); 124 168 action.valueChanged(null); 125 action.actionPerformed(n ull);169 action.actionPerformed(new ActionEvent(new JDialog().getComponent(0), ActionEvent.ACTION_PERFORMED, "foo")); 126 170 } 127 171 … … 131 175 @Test 132 176 public void testQueryAction() { 177 TestUtils.assumeWorkingJMockit(); 178 179 // set up mockers to simulate the dialog being cancelled 180 final boolean[] dialogShown = new boolean[] {false}; 181 if (GraphicsEnvironment.isHeadless()) { 182 new WindowMocker(); 183 } 184 new MockUp<JDialog>() { 185 @Mock 186 void setVisible(final Invocation invocation, final boolean visible) throws Exception { 187 if (visible) { 188 ((JButton) TestUtils.getComponentByName((JDialog) invocation.getInvokedInstance(), "cancelButton")).doClick(); 189 dialogShown[0] = true; 190 } 191 // critically, don't proceed into implementation 192 } 193 }; 194 new MockUp<ChangesetQueryDialog>() { 195 @Mock 196 void setVisible(final Invocation invocation, final boolean visible) throws Exception { 197 if (GraphicsEnvironment.isHeadless()) { 198 // we have to mock the behaviour quite coarsely as much of ChangesetQueryDialog will 199 // raise a HeadlessException 200 if (visible) { 201 TestUtils.setPrivateField(ChangesetQueryDialog.class, invocation.getInvokedInstance(), "canceled", true); 202 dialogShown[0] = true; 203 } 204 } else { 205 // proceeding into the implementation allows a bit more of the target code to be 206 // covered, actual mocking is performed on JDialog's setVisible() 207 invocation.proceed(visible); 208 } 209 } 210 }; 211 133 212 new QueryAction().actionPerformed(null); 213 214 assertTrue(dialogShown[0]); 134 215 } 135 216
Note:
See TracChangeset
for help on using the changeset viewer.