Ticket #15539: v1-0003-MinimapDialogTest-refactor-to-use-shared-paint-image.patch

File v1-0003-MinimapDialogTest-refactor-to-use-shared-paint-image.patch, 7.4 KB (added by ris, 9 years ago)
  • test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java

    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 {  
    8383    protected SlippyMapBBoxChooser slippyMap;
    8484    protected SourceButton sourceButton;
    8585
     86    protected static BufferedImage paintedSlippyMap;
     87
    8688    protected void setUpMiniMap() throws Exception {
    8789        this.minimap = new MinimapDialog();
    8890        this.minimap.setSize(300, 200);
    public class MinimapDialogTest {  
    9597        this.minimap.doLayout();
    9698    }
    9799
     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
    98122    /**
    99123     * Tests to switch imagery source.
    100124     * @throws Exception if any error occurs
    public class MinimapDialogTest {  
    106130
    107131        this.setUpMiniMap();
    108132
    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();
    116133        // 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();
    121135
    122136        Thread.sleep(500);
    123137
    124         g = image.createGraphics();
    125         this.slippyMap.paintAll(g);
     138        this.paintSlippyMap();
    126139
    127         assertEquals(0xffffffff, image.getRGB(0, 0));
     140        assertEquals(0xffffffff, paintedSlippyMap.getRGB(0, 0));
    128141
    129142        assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "White Tiles");
    130143
    131144        getSourceMenuItemByLabel(this.sourceButton.getPopupMenu(), "Magenta Tiles").doClick();
    132145        assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "Magenta Tiles");
    133146        // 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();
    141148
    142149        Thread.sleep(500);
    143150
    144         g = image.createGraphics();
    145         this.slippyMap.paintAll(g);
     151        this.paintSlippyMap();
    146152
    147         assertEquals(0xffff00ff, image.getRGB(0, 0));
     153        assertEquals(0xffff00ff, paintedSlippyMap.getRGB(0, 0));
    148154
    149155        getSourceMenuItemByLabel(this.sourceButton.getPopupMenu(), "Green Tiles").doClick();
    150156        assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "Green Tiles");
    151157        // 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();
    157159
    158160        Thread.sleep(500);
    159161
    160         g = image.createGraphics();
    161         this.slippyMap.paintAll(g);
     162        this.paintSlippyMap();
    162163
    163         assertEquals(0xff00ff00, image.getRGB(0, 0));
     164        assertEquals(0xff00ff00, paintedSlippyMap.getRGB(0, 0));
    164165
    165166        assertEquals("Green Tiles", Main.pref.get("slippy_map_chooser.mapstyle", "Fail"));
    166167    }
    167168
     169    /**
     170     * Tests minimap obeys a saved "mapstyle" preference on startup.
     171     * @throws Exception if any error occurs
     172     */
    168173    @Test
    169174    public void testSourcePrefObeyed() throws Exception {
    170175        Main.pref.put("slippy_map_chooser.mapstyle", "Green Tiles");
    public class MinimapDialogTest {  
    173178
    174179        assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "Green Tiles");
    175180
    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();
    183181        // 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();
    188183
    189184        Thread.sleep(500);
    190185
    191         g = image.createGraphics();
    192         this.slippyMap.paintAll(g);
     186        this.paintSlippyMap();
    193187
    194         assertEquals(0xff00ff00, image.getRGB(0, 0));
     188        assertEquals(0xff00ff00, paintedSlippyMap.getRGB(0, 0));
    195189
    196190        getSourceMenuItemByLabel(this.sourceButton.getPopupMenu(), "Magenta Tiles").doClick();
    197191        assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "Magenta Tiles");
    public class MinimapDialogTest {  
    199193        assertEquals("Magenta Tiles", Main.pref.get("slippy_map_chooser.mapstyle", "Fail"));
    200194    }
    201195
     196    /**
     197     * Tests minimap handles an unrecognized "mapstyle" preference on startup
     198     * @throws Exception if any error occurs
     199     */
    202200    @Test
    203201    public void testSourcePrefInvalid() throws Exception {
    204202        Main.pref.put("slippy_map_chooser.mapstyle", "Hooloovoo Tiles");
    public class MinimapDialogTest {  
    207205
    208206        assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "White Tiles");
    209207
    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();
    217208        // 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();
    222210
    223211        Thread.sleep(500);
    224212
    225         g = image.createGraphics();
    226         this.slippyMap.paintAll(g);
     213        this.paintSlippyMap();
    227214
    228         assertEquals(0xffffffff, image.getRGB(0, 0));
     215        assertEquals(0xffffffff, paintedSlippyMap.getRGB(0, 0));
    229216    }
    230217}