From 2ceadbcb285b59d57ee820d0171e9599cd95b630 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Thu, 1 Nov 2018 22:05:13 +0000
Subject: [PATCH v1 2/4] ChangesetCacheManagerTest: fix testQueryAction for
non-headless mode, also increasing coverage of test
---
.../dialogs/changeset/ChangesetCacheManager.java | 28 ++++++-------
.../changeset/query/ChangesetQueryDialog.java | 8 +++-
.../changeset/ChangesetCacheManagerTest.java | 49 ++++++++++++++++++++++
3 files changed, 68 insertions(+), 17 deletions(-)
diff --git a/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetCacheManager.java b/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetCacheManager.java
index 062945664..a24638b72 100644
|
a
|
b
|
public class ChangesetCacheManager extends JFrame {
|
| 392 | 392 | @Override |
| 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 | } |
| 413 | 411 | } |
diff --git a/src/org/openstreetmap/josm/gui/dialogs/changeset/query/ChangesetQueryDialog.java b/src/org/openstreetmap/josm/gui/dialogs/changeset/query/ChangesetQueryDialog.java
index 6766ad84d..fbcf78be2 100644
|
a
|
b
|
public class ChangesetQueryDialog extends JDialog {
|
| 72 | 72 | protected JPanel buildButtonPanel() { |
| 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 | |
| 79 | 83 | return pnl; |
diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetCacheManagerTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetCacheManagerTest.java
index 589462645..df518ea21 100644
|
a
|
b
|
|
| 2 | 2 | package org.openstreetmap.josm.gui.dialogs.changeset; |
| 3 | 3 | |
| 4 | 4 | import static org.junit.Assert.assertNotNull; |
| | 5 | import static org.junit.Assert.assertTrue; |
| | 6 | |
| | 7 | import java.awt.GraphicsEnvironment; |
| | 8 | import javax.swing.JButton; |
| | 9 | import javax.swing.JDialog; |
| 5 | 10 | |
| 6 | 11 | import java.util.Collections; |
| 7 | 12 | import java.util.List; |
| 8 | 13 | |
| 9 | 14 | import org.junit.Rule; |
| 10 | 15 | import org.junit.Test; |
| | 16 | import org.openstreetmap.josm.TestUtils; |
| 11 | 17 | import org.openstreetmap.josm.data.osm.Changeset; |
| 12 | 18 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.CancelAction; |
| 13 | 19 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.ChangesetDetailViewSynchronizer; |
| … |
… |
import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.Downlo
|
| 18 | 24 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.QueryAction; |
| 19 | 25 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.RemoveFromCacheAction; |
| 20 | 26 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager.ShowDetailAction; |
| | 27 | import org.openstreetmap.josm.gui.dialogs.changeset.query.ChangesetQueryDialog; |
| 21 | 28 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
| | 29 | import org.openstreetmap.josm.testutils.mockers.WindowMocker; |
| | 30 | |
| | 31 | import mockit.Invocation; |
| | 32 | import mockit.Mock; |
| | 33 | import mockit.MockUp; |
| 22 | 34 | |
| 23 | 35 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 24 | 36 | |
| … |
… |
public class ChangesetCacheManagerTest {
|
| 130 | 142 | */ |
| 131 | 143 | @Test |
| 132 | 144 | public void testQueryAction() { |
| | 145 | TestUtils.assumeWorkingJMockit(); |
| | 146 | |
| | 147 | // set up mockers to simulate the dialog being cancelled |
| | 148 | final boolean[] dialogShown = new boolean[] {false}; |
| | 149 | if (GraphicsEnvironment.isHeadless()) { |
| | 150 | new WindowMocker(); |
| | 151 | } |
| | 152 | new MockUp<JDialog>() { |
| | 153 | @Mock |
| | 154 | void setVisible(final Invocation invocation, final boolean visible) throws Exception { |
| | 155 | if (visible) { |
| | 156 | ((JButton) TestUtils.getComponentByName((JDialog) invocation.getInvokedInstance(), "cancelButton")).doClick(); |
| | 157 | dialogShown[0] = true; |
| | 158 | } |
| | 159 | // critically, don't proceed into implementation |
| | 160 | } |
| | 161 | }; |
| | 162 | new MockUp<ChangesetQueryDialog>() { |
| | 163 | @Mock |
| | 164 | void setVisible(final Invocation invocation, final boolean visible) throws Exception { |
| | 165 | if (GraphicsEnvironment.isHeadless()) { |
| | 166 | // we have to mock the behaviour quite coarsely as much of ChangesetQueryDialog will |
| | 167 | // raise a HeadlessException |
| | 168 | if (visible) { |
| | 169 | TestUtils.setPrivateField(ChangesetQueryDialog.class, invocation.getInvokedInstance(), "canceled", true); |
| | 170 | dialogShown[0] = true; |
| | 171 | } |
| | 172 | } else { |
| | 173 | // proceeding into the implementation allows a bit more of the target code to be |
| | 174 | // covered, actual mocking is performed on JDialog's setVisible() |
| | 175 | invocation.proceed(visible); |
| | 176 | } |
| | 177 | } |
| | 178 | }; |
| | 179 | |
| 133 | 180 | new QueryAction().actionPerformed(null); |
| | 181 | |
| | 182 | assertTrue(dialogShown[0]); |
| 134 | 183 | } |
| 135 | 184 | |
| 136 | 185 | /** |