From ee43ceeb35372f9c751e63debc4c7bdaf84593c9 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sat, 11 Nov 2017 13:33:41 +0000
Subject: [PATCH 3/6] MinimapDialogTest: refactor to use shared paint & image
allocation routine
---
.../josm/gui/dialogs/MinimapDialogTest.java | 107 +++++++++------------
1 file changed, 47 insertions(+), 60 deletions(-)
diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java
index ba4fafc53..3635b3448 100644
|
a
|
b
|
public class MinimapDialogTest {
|
| 83 | 83 | protected SlippyMapBBoxChooser slippyMap; |
| 84 | 84 | protected SourceButton sourceButton; |
| 85 | 85 | |
| | 86 | protected static BufferedImage paintedSlippyMap; |
| | 87 | |
| 86 | 88 | protected void setUpMiniMap() throws Exception { |
| 87 | 89 | this.minimap = new MinimapDialog(); |
| 88 | 90 | this.minimap.setSize(300, 200); |
| … |
… |
public class MinimapDialogTest {
|
| 95 | 97 | this.minimap.doLayout(); |
| 96 | 98 | } |
| 97 | 99 | |
| | 100 | protected void paintSlippyMap() { |
| | 101 | if (paintedSlippyMap == null || |
| | 102 | paintedSlippyMap.getWidth() != this.slippyMap.getSize().width || |
| | 103 | paintedSlippyMap.getHeight() != this.slippyMap.getSize().height) { |
| | 104 | paintedSlippyMap = new BufferedImage( |
| | 105 | this.slippyMap.getSize().width, |
| | 106 | this.slippyMap.getSize().height, |
| | 107 | BufferedImage.TYPE_INT_RGB |
| | 108 | ); |
| | 109 | } // else reuse existing one - allocation is expensive |
| | 110 | |
| | 111 | // clear background to a recognizably "wrong" color & dispose our Graphics2D so we don't risk carrying over |
| | 112 | // any state |
| | 113 | Graphics2D g = paintedSlippyMap.createGraphics(); |
| | 114 | g.setBackground(Color.BLUE); |
| | 115 | g.clearRect(0, 0, paintedSlippyMap.getWidth(), paintedSlippyMap.getHeight()); |
| | 116 | g.dispose(); |
| | 117 | |
| | 118 | g = paintedSlippyMap.createGraphics(); |
| | 119 | this.slippyMap.paintAll(g); |
| | 120 | } |
| | 121 | |
| 98 | 122 | /** |
| 99 | 123 | * Tests to switch imagery source. |
| 100 | 124 | * @throws Exception if any error occurs |
| … |
… |
public class MinimapDialogTest {
|
| 106 | 130 | |
| 107 | 131 | this.setUpMiniMap(); |
| 108 | 132 | |
| 109 | | BufferedImage image = new BufferedImage( |
| 110 | | this.slippyMap.getSize().width, |
| 111 | | this.slippyMap.getSize().height, |
| 112 | | BufferedImage.TYPE_INT_RGB |
| 113 | | ); |
| 114 | | |
| 115 | | Graphics2D g = image.createGraphics(); |
| 116 | 133 | // an initial paint operation is required to trigger the tile fetches |
| 117 | | this.slippyMap.paintAll(g); |
| 118 | | g.setBackground(Color.BLUE); |
| 119 | | g.clearRect(0, 0, image.getWidth(), image.getHeight()); |
| 120 | | g.dispose(); |
| | 134 | this.paintSlippyMap(); |
| 121 | 135 | |
| 122 | 136 | Thread.sleep(500); |
| 123 | 137 | |
| 124 | | g = image.createGraphics(); |
| 125 | | this.slippyMap.paintAll(g); |
| | 138 | this.paintSlippyMap(); |
| 126 | 139 | |
| 127 | | assertEquals(0xffffffff, image.getRGB(0, 0)); |
| | 140 | assertEquals(0xffffffff, paintedSlippyMap.getRGB(0, 0)); |
| 128 | 141 | |
| 129 | 142 | assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "White Tiles"); |
| 130 | 143 | |
| 131 | 144 | getSourceMenuItemByLabel(this.sourceButton.getPopupMenu(), "Magenta Tiles").doClick(); |
| 132 | 145 | assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "Magenta Tiles"); |
| 133 | 146 | // call paint to trigger new tile fetch |
| 134 | | this.slippyMap.paintAll(g); |
| 135 | | |
| 136 | | // clear background to a recognizably "wrong" color & dispose our Graphics2D so we don't risk carrying over |
| 137 | | // any state |
| 138 | | g.setBackground(Color.BLUE); |
| 139 | | g.clearRect(0, 0, image.getWidth(), image.getHeight()); |
| 140 | | g.dispose(); |
| | 147 | this.paintSlippyMap(); |
| 141 | 148 | |
| 142 | 149 | Thread.sleep(500); |
| 143 | 150 | |
| 144 | | g = image.createGraphics(); |
| 145 | | this.slippyMap.paintAll(g); |
| | 151 | this.paintSlippyMap(); |
| 146 | 152 | |
| 147 | | assertEquals(0xffff00ff, image.getRGB(0, 0)); |
| | 153 | assertEquals(0xffff00ff, paintedSlippyMap.getRGB(0, 0)); |
| 148 | 154 | |
| 149 | 155 | getSourceMenuItemByLabel(this.sourceButton.getPopupMenu(), "Green Tiles").doClick(); |
| 150 | 156 | assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "Green Tiles"); |
| 151 | 157 | // call paint to trigger new tile fetch |
| 152 | | this.slippyMap.paintAll(g); |
| 153 | | |
| 154 | | g.setBackground(Color.BLUE); |
| 155 | | g.clearRect(0, 0, image.getWidth(), image.getHeight()); |
| 156 | | g.dispose(); |
| | 158 | this.paintSlippyMap(); |
| 157 | 159 | |
| 158 | 160 | Thread.sleep(500); |
| 159 | 161 | |
| 160 | | g = image.createGraphics(); |
| 161 | | this.slippyMap.paintAll(g); |
| | 162 | this.paintSlippyMap(); |
| 162 | 163 | |
| 163 | | assertEquals(0xff00ff00, image.getRGB(0, 0)); |
| | 164 | assertEquals(0xff00ff00, paintedSlippyMap.getRGB(0, 0)); |
| 164 | 165 | |
| 165 | 166 | assertEquals("Green Tiles", Main.pref.get("slippy_map_chooser.mapstyle", "Fail")); |
| 166 | 167 | } |
| 167 | 168 | |
| | 169 | /** |
| | 170 | * Tests minimap obeys a saved "mapstyle" preference on startup. |
| | 171 | * @throws Exception if any error occurs |
| | 172 | */ |
| 168 | 173 | @Test |
| 169 | 174 | public void testSourcePrefObeyed() throws Exception { |
| 170 | 175 | Main.pref.put("slippy_map_chooser.mapstyle", "Green Tiles"); |
| … |
… |
public class MinimapDialogTest {
|
| 173 | 178 | |
| 174 | 179 | assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "Green Tiles"); |
| 175 | 180 | |
| 176 | | BufferedImage image = new BufferedImage( |
| 177 | | this.slippyMap.getSize().width, |
| 178 | | this.slippyMap.getSize().height, |
| 179 | | BufferedImage.TYPE_INT_RGB |
| 180 | | ); |
| 181 | | |
| 182 | | Graphics2D g = image.createGraphics(); |
| 183 | 181 | // an initial paint operation is required to trigger the tile fetches |
| 184 | | this.slippyMap.paintAll(g); |
| 185 | | g.setBackground(Color.BLUE); |
| 186 | | g.clearRect(0, 0, image.getWidth(), image.getHeight()); |
| 187 | | g.dispose(); |
| | 182 | this.paintSlippyMap(); |
| 188 | 183 | |
| 189 | 184 | Thread.sleep(500); |
| 190 | 185 | |
| 191 | | g = image.createGraphics(); |
| 192 | | this.slippyMap.paintAll(g); |
| | 186 | this.paintSlippyMap(); |
| 193 | 187 | |
| 194 | | assertEquals(0xff00ff00, image.getRGB(0, 0)); |
| | 188 | assertEquals(0xff00ff00, paintedSlippyMap.getRGB(0, 0)); |
| 195 | 189 | |
| 196 | 190 | getSourceMenuItemByLabel(this.sourceButton.getPopupMenu(), "Magenta Tiles").doClick(); |
| 197 | 191 | assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "Magenta Tiles"); |
| … |
… |
public class MinimapDialogTest {
|
| 199 | 193 | assertEquals("Magenta Tiles", Main.pref.get("slippy_map_chooser.mapstyle", "Fail")); |
| 200 | 194 | } |
| 201 | 195 | |
| | 196 | /** |
| | 197 | * Tests minimap handles an unrecognized "mapstyle" preference on startup |
| | 198 | * @throws Exception if any error occurs |
| | 199 | */ |
| 202 | 200 | @Test |
| 203 | 201 | public void testSourcePrefInvalid() throws Exception { |
| 204 | 202 | Main.pref.put("slippy_map_chooser.mapstyle", "Hooloovoo Tiles"); |
| … |
… |
public class MinimapDialogTest {
|
| 207 | 205 | |
| 208 | 206 | assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "White Tiles"); |
| 209 | 207 | |
| 210 | | BufferedImage image = new BufferedImage( |
| 211 | | this.slippyMap.getSize().width, |
| 212 | | this.slippyMap.getSize().height, |
| 213 | | BufferedImage.TYPE_INT_RGB |
| 214 | | ); |
| 215 | | |
| 216 | | Graphics2D g = image.createGraphics(); |
| 217 | 208 | // an initial paint operation is required to trigger the tile fetches |
| 218 | | this.slippyMap.paintAll(g); |
| 219 | | g.setBackground(Color.BLUE); |
| 220 | | g.clearRect(0, 0, image.getWidth(), image.getHeight()); |
| 221 | | g.dispose(); |
| | 209 | this.paintSlippyMap(); |
| 222 | 210 | |
| 223 | 211 | Thread.sleep(500); |
| 224 | 212 | |
| 225 | | g = image.createGraphics(); |
| 226 | | this.slippyMap.paintAll(g); |
| | 213 | this.paintSlippyMap(); |
| 227 | 214 | |
| 228 | | assertEquals(0xffffffff, image.getRGB(0, 0)); |
| | 215 | assertEquals(0xffffffff, paintedSlippyMap.getRGB(0, 0)); |
| 229 | 216 | } |
| 230 | 217 | } |