- Timestamp:
- 2018-11-03T22:09:03+01:00 (6 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetCacheManager.java
r14121 r14410 393 393 public void actionPerformed(ActionEvent evt) { 394 394 Window parent = GuiHelper.getWindowAncestorFor(evt); 395 if (!GraphicsEnvironment.isHeadless()) { 396 ChangesetQueryDialog dialog = new ChangesetQueryDialog(parent); 397 dialog.initForUserInput(); 398 dialog.setVisible(true); 399 if (dialog.isCanceled()) 400 return; 401 402 try { 403 ChangesetQuery query = dialog.getChangesetQuery(); 404 if (query != null) { 405 ChangesetCacheManager.getInstance().runDownloadTask(new ChangesetQueryTask(parent, query)); 406 } 407 } catch (IllegalStateException e) { 408 Logging.error(e); 409 JOptionPane.showMessageDialog(parent, e.getMessage(), tr("Error"), JOptionPane.ERROR_MESSAGE); 395 ChangesetQueryDialog dialog = new ChangesetQueryDialog(parent); 396 dialog.initForUserInput(); 397 dialog.setVisible(true); 398 if (dialog.isCanceled()) 399 return; 400 401 try { 402 ChangesetQuery query = dialog.getChangesetQuery(); 403 if (query != null) { 404 ChangesetCacheManager.getInstance().runDownloadTask(new ChangesetQueryTask(parent, query)); 410 405 } 406 } catch (IllegalStateException e) { 407 Logging.error(e); 408 JOptionPane.showMessageDialog(parent, e.getMessage(), tr("Error"), JOptionPane.ERROR_MESSAGE); 411 409 } 412 410 } -
trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/query/ChangesetQueryDialog.java
r12678 r14410 73 73 JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER)); 74 74 75 pnl.add(new JButton(new QueryAction())); 76 pnl.add(new JButton(new CancelAction())); 75 final JButton queryButton = new JButton(new QueryAction()); 76 queryButton.setName("queryButton"); 77 pnl.add(queryButton); 78 final JButton cancelButton = new JButton(new CancelAction()); 79 cancelButton.setName("cancelButton"); 80 pnl.add(cancelButton); 77 81 pnl.add(new JButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Dialog/ChangesetQuery")))); 78 82 -
trunk/test/unit/org/openstreetmap/josm/TestUtils.java
r14318 r14410 204 204 205 205 /** 206 * Sets a private field value. 207 * @param obj object 208 * @param fieldName private field name 209 * @param value replacement value 210 * @throws ReflectiveOperationException if a reflection operation error occurs 211 */ 212 public static void setPrivateField( 213 final Object obj, 214 final String fieldName, 215 final Object value 216 ) throws ReflectiveOperationException { 217 setPrivateField(obj.getClass(), obj, fieldName, value); 218 } 219 220 /** 221 * Sets a private field value. 222 * @param cls object class 223 * @param obj object 224 * @param fieldName private field name 225 * @param value replacement value 226 * @throws ReflectiveOperationException if a reflection operation error occurs 227 */ 228 public static void setPrivateField( 229 final Class<?> cls, 230 final Object obj, 231 final String fieldName, 232 final Object value 233 ) throws ReflectiveOperationException { 234 Field f = cls.getDeclaredField(fieldName); 235 AccessController.doPrivileged((PrivilegedAction<Void>) () -> { 236 f.setAccessible(true); 237 return null; 238 }); 239 f.set(obj, value); 240 } 241 242 /** 206 243 * Returns a private static field value. 207 244 * @param cls object class -
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.