From 43973ad26cf7a7f009e6e423d70ed66cd3f8c89e Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sat, 25 Nov 2017 20:38:52 +0000
Subject: [PATCH v2 2/3] MinimapDialogTest: perform event-related operations in
EDT thread
---
.../josm/gui/dialogs/MinimapDialogTest.java | 76 +++++++++++++++-------
1 file changed, 51 insertions(+), 25 deletions(-)
diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java
index e302a7c51..b4ac23820 100644
a
|
b
|
import org.openstreetmap.josm.Main;
|
24 | 24 | import org.openstreetmap.josm.TestUtils; |
25 | 25 | import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser; |
26 | 26 | import org.openstreetmap.josm.gui.bbox.SourceButton; |
| 27 | import org.openstreetmap.josm.gui.util.GuiHelper; |
27 | 28 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
28 | 29 | |
29 | 30 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
… |
… |
public class MinimapDialogTest {
|
54 | 55 | assertFalse(dlg.isVisible()); |
55 | 56 | } |
56 | 57 | |
57 | | protected void assertSingleSelectedSourceLabel(String label) { |
| 58 | @FunctionalInterface |
| 59 | protected interface ThrowingRunnable { |
| 60 | void run() throws Throwable; |
| 61 | } |
| 62 | |
| 63 | protected static Runnable uncheckExceptions(final ThrowingRunnable tr) { |
| 64 | return (() -> { |
| 65 | try { |
| 66 | tr.run(); |
| 67 | } catch (Throwable e) { |
| 68 | throw new RuntimeException(e); |
| 69 | } |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | protected void assertSingleSelectedSourceLabel(final String label) { |
58 | 74 | JPopupMenu menu = this.sourceButton.getPopupMenu(); |
59 | 75 | boolean found = false; |
60 | 76 | for (Component c: menu.getComponents()) { |
… |
… |
public class MinimapDialogTest {
|
73 | 89 | assertTrue("Selected source not found in menu", found); |
74 | 90 | } |
75 | 91 | |
76 | | protected JMenuItem getSourceMenuItemByLabel(String label) { |
77 | | JPopupMenu menu = this.sourceButton.getPopupMenu(); |
78 | | for (Component c: menu.getComponents()) { |
79 | | if (JPopupMenu.Separator.class.isInstance(c)) { |
80 | | break; |
81 | | } else if (((JMenuItem) c).getText() == label) { |
82 | | return (JMenuItem) c; |
83 | | } |
84 | | // else continue... |
| 92 | protected void clickSourceMenuItemByLabel(final String label) { |
| 93 | try { |
| 94 | GuiHelper.runInEDTAndWaitWithException(() -> { |
| 95 | JPopupMenu menu = this.sourceButton.getPopupMenu(); |
| 96 | for (Component c: menu.getComponents()) { |
| 97 | if (JPopupMenu.Separator.class.isInstance(c)) { |
| 98 | // sources should all come before any separators |
| 99 | break; |
| 100 | } else if (((JMenuItem) c).getText() == label) { |
| 101 | ((JMenuItem) c).doClick(); |
| 102 | return; |
| 103 | } |
| 104 | // else continue... |
| 105 | } |
| 106 | fail(); |
| 107 | }); |
| 108 | } catch (Throwable e) { |
| 109 | // need to turn this *back* into an AssertionFailedError |
| 110 | fail(String.format("Failed to find menu item with label %s: %s", label, e)); |
85 | 111 | } |
86 | | fail("Failed to find menu item with label " + label); |
87 | | return null; |
88 | 112 | } |
89 | 113 | |
90 | 114 | protected MinimapDialog minimap; |
… |
… |
public class MinimapDialogTest {
|
94 | 118 | |
95 | 119 | protected static BufferedImage paintedSlippyMap; |
96 | 120 | |
97 | | protected void setUpMiniMap() throws Exception { |
98 | | this.minimap = new MinimapDialog(); |
99 | | this.minimap.setSize(300, 200); |
100 | | this.minimap.showDialog(); |
101 | | this.slippyMap = (SlippyMapBBoxChooser) TestUtils.getPrivateField(this.minimap, "slippyMap"); |
102 | | this.sourceButton = (SourceButton) TestUtils.getPrivateField(this.slippyMap, "iSourceButton"); |
| 121 | protected void setUpMiniMap() { |
| 122 | GuiHelper.runInEDTAndWaitWithException(uncheckExceptions(() -> { |
| 123 | this.minimap = new MinimapDialog(); |
| 124 | this.minimap.setSize(300, 200); |
| 125 | this.minimap.showDialog(); |
| 126 | this.slippyMap = (SlippyMapBBoxChooser) TestUtils.getPrivateField(this.minimap, "slippyMap"); |
| 127 | this.sourceButton = (SourceButton) TestUtils.getPrivateField(this.slippyMap, "iSourceButton"); |
103 | 128 | |
104 | | this.slippyMapTasksFinished = () -> !this.slippyMap.getTileController().getTileLoader().hasOutstandingTasks(); |
| 129 | // get minimap in a paintable state |
| 130 | this.minimap.addNotify(); |
| 131 | this.minimap.doLayout(); |
| 132 | })); |
105 | 133 | |
106 | | // get minimap in a paintable state |
107 | | this.minimap.addNotify(); |
108 | | this.minimap.doLayout(); |
| 134 | this.slippyMapTasksFinished = () -> !this.slippyMap.getTileController().getTileLoader().hasOutstandingTasks(); |
109 | 135 | } |
110 | 136 | |
111 | 137 | protected void paintSlippyMap() { |
… |
… |
public class MinimapDialogTest {
|
152 | 178 | |
153 | 179 | this.assertSingleSelectedSourceLabel("White Tiles"); |
154 | 180 | |
155 | | this.getSourceMenuItemByLabel("Magenta Tiles").doClick(); |
| 181 | this.clickSourceMenuItemByLabel("Magenta Tiles"); |
156 | 182 | this.assertSingleSelectedSourceLabel("Magenta Tiles"); |
157 | 183 | // call paint to trigger new tile fetch |
158 | 184 | this.paintSlippyMap(); |
… |
… |
public class MinimapDialogTest {
|
163 | 189 | |
164 | 190 | assertEquals(0xffff00ff, paintedSlippyMap.getRGB(0, 0)); |
165 | 191 | |
166 | | this.getSourceMenuItemByLabel("Green Tiles").doClick(); |
| 192 | this.clickSourceMenuItemByLabel("Green Tiles"); |
167 | 193 | this.assertSingleSelectedSourceLabel("Green Tiles"); |
168 | 194 | // call paint to trigger new tile fetch |
169 | 195 | this.paintSlippyMap(); |
… |
… |
public class MinimapDialogTest {
|
198 | 224 | |
199 | 225 | assertEquals(0xff00ff00, paintedSlippyMap.getRGB(0, 0)); |
200 | 226 | |
201 | | this.getSourceMenuItemByLabel("Magenta Tiles").doClick(); |
| 227 | this.clickSourceMenuItemByLabel("Magenta Tiles"); |
202 | 228 | this.assertSingleSelectedSourceLabel("Magenta Tiles"); |
203 | 229 | |
204 | 230 | assertEquals("Magenta Tiles", Main.pref.get("slippy_map_chooser.mapstyle", "Fail")); |