From a9282d0559114b0d34d8dba23200148ccca2e265 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Tue, 31 Oct 2017 22:09:21 +0000
Subject: [PATCH 4/4] MinimapDialogTest: add testSourceSwitching

---
 .../josm/gui/dialogs/MinimapDialogTest.java        | 124 ++++++++++++++++++++-
 1 file changed, 122 insertions(+), 2 deletions(-)

diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java
index 36eee87b3..5e7a92666 100644
--- a/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java
+++ b/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java
@@ -1,12 +1,26 @@
 // License: GPL. For details, see LICENSE file.
 package org.openstreetmap.josm.gui.dialogs;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import org.junit.Rule;
 import org.junit.Test;
+import org.openstreetmap.josm.TestUtils;
 import org.openstreetmap.josm.testutils.JOSMTestRules;
+import org.openstreetmap.josm.gui.MainApplication;
+import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
+import org.openstreetmap.josm.gui.bbox.SourceButton;
+
+import java.awt.Color;
+import java.awt.Component;
+import java.awt.Graphics2D;
+import java.awt.image.BufferedImage;
+
+import javax.swing.JMenuItem;
+import javax.swing.JPopupMenu;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 
@@ -14,13 +28,12 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
  * Unit tests of {@link MinimapDialog} class.
  */
 public class MinimapDialogTest {
-
     /**
      * Setup tests
      */
     @Rule
     @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
-    public JOSMTestRules test = new JOSMTestRules().platform();
+    public JOSMTestRules josmTestRules = new JOSMTestRules().main().commands().platform().projection().fakeImagery();
 
     /**
      * Unit test of {@link MinimapDialog} class.
@@ -33,4 +46,111 @@ public class MinimapDialogTest {
         dlg.hideDialog();
         assertFalse(dlg.isVisible());
     }
+
+    private static void assertSingleSelectedSourceLabel(JPopupMenu menu, String label) {
+        boolean found = false;
+        for (Component c: menu.getComponents()) {
+            if (JPopupMenu.Separator.class.isInstance(c)) {
+                break;
+            } else {
+                boolean equalText = ((JMenuItem) c).getText() == label;
+                boolean isSelected = ((JMenuItem) c).isSelected();
+                assertEquals(equalText, isSelected);
+                if (equalText) {
+                    assertFalse("Second selected source found", found);
+                    found = true;
+                }
+            }
+        }
+        assertTrue("Selected source not found in menu", found);
+    }
+
+    private static JMenuItem getSourceMenuItemByLabel(JPopupMenu menu, String label) {
+        for (Component c: menu.getComponents()) {
+            if (JPopupMenu.Separator.class.isInstance(c)) {
+                break;
+            } else if (((JMenuItem) c).getText() == label) {
+                return (JMenuItem) c;
+            }
+            // else continue...
+        }
+        fail("Failed to find menu item with label " + label);
+        return null;
+    }
+
+    @Test
+    public void testSourceSwitching() throws Throwable {
+        MinimapDialog dlg = new MinimapDialog();
+        dlg.setSize(300, 200);
+        dlg.showDialog();
+        SlippyMapBBoxChooser slippyMap = (SlippyMapBBoxChooser)TestUtils.getPrivateField(dlg, "slippyMap");
+        SourceButton sourceButton = (SourceButton)TestUtils.getPrivateField(slippyMap, "iSourceButton");
+
+        // get dlg in a paintable state
+        dlg.addNotify();
+        dlg.doLayout();
+
+        BufferedImage image = new BufferedImage(
+            slippyMap.getSize().width,
+            slippyMap.getSize().height,
+            BufferedImage.TYPE_INT_RGB
+        );
+
+        Graphics2D g = image.createGraphics();
+        // an initial paint operation is required to trigger the tile fetches
+        slippyMap.paintAll(g);
+        g.setBackground(Color.BLUE);
+        g.clearRect(0, 0, image.getWidth(), image.getHeight());
+        g.dispose();
+
+        Thread.sleep(3500);
+
+        g = image.createGraphics();
+        slippyMap.paintAll(g);
+
+        assertEquals(0xffffffff, image.getRGB(0, 0));
+
+        assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "White Tiles");
+
+        getSourceMenuItemByLabel(sourceButton.getPopupMenu(), "Magenta Tiles").doClick();
+        assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "Magenta Tiles");
+        // call paint to trigger new tile fetch
+        slippyMap.paintAll(g);
+
+        // clear background to a recognizably "wrong" color & dispose our Graphics2D so we don't risk carrying over
+        // any state
+        g.setBackground(Color.BLUE);
+        g.clearRect(0, 0, image.getWidth(), image.getHeight());
+        g.dispose();
+
+        Thread.sleep(500);
+
+        g = image.createGraphics();
+        slippyMap.paintAll(g);
+
+        assertEquals(0xffff00ff, image.getRGB(0, 0));
+
+        getSourceMenuItemByLabel(sourceButton.getPopupMenu(), "Green Tiles").doClick();
+        assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "Green Tiles");
+        // call paint to trigger new tile fetch
+        slippyMap.paintAll(g);
+
+        g.setBackground(Color.BLUE);
+        g.clearRect(0, 0, image.getWidth(), image.getHeight());
+        g.dispose();
+
+        Thread.sleep(500);
+
+        g = image.createGraphics();
+        slippyMap.paintAll(g);
+
+        assertEquals(0xff00ff00, image.getRGB(0, 0));
+
+//      useful for debugging
+//         try {
+//             javax.imageio.ImageIO.write(image, "png", new java.io.File("painted.png"));
+//         } catch (java.io.IOException ioe) {
+//             System.err.println("Failed writing image");
+//         }
+    }
 }
-- 
2.11.0

