Changeset 13078 in josm for trunk/test/unit/org/openstreetmap/josm/gui/dialogs
- Timestamp:
- 2017-11-04T17:11:15+01:00 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java
r12564 r13078 2 2 package org.openstreetmap.josm.gui.dialogs; 3 3 4 import static org.junit.Assert.assertEquals; 4 5 import static org.junit.Assert.assertFalse; 5 6 import static org.junit.Assert.assertTrue; 7 import static org.junit.Assert.fail; 8 9 import java.awt.Color; 10 import java.awt.Component; 11 import java.awt.Graphics2D; 12 import java.awt.image.BufferedImage; 13 14 import javax.swing.JMenuItem; 15 import javax.swing.JPopupMenu; 6 16 7 17 import org.junit.Rule; 8 18 import org.junit.Test; 19 import org.openstreetmap.josm.TestUtils; 20 import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser; 21 import org.openstreetmap.josm.gui.bbox.SourceButton; 9 22 import org.openstreetmap.josm.testutils.JOSMTestRules; 10 23 … … 21 34 @Rule 22 35 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 23 public JOSMTestRules test = new JOSMTestRules().platform();36 public JOSMTestRules josmTestRules = new JOSMTestRules().main().platform().projection().fakeImagery(); 24 37 25 38 /** … … 34 47 assertFalse(dlg.isVisible()); 35 48 } 49 50 private static void assertSingleSelectedSourceLabel(JPopupMenu menu, String label) { 51 boolean found = false; 52 for (Component c: menu.getComponents()) { 53 if (JPopupMenu.Separator.class.isInstance(c)) { 54 break; 55 } else { 56 boolean equalText = ((JMenuItem) c).getText() == label; 57 boolean isSelected = ((JMenuItem) c).isSelected(); 58 assertEquals(equalText, isSelected); 59 if (equalText) { 60 assertFalse("Second selected source found", found); 61 found = true; 62 } 63 } 64 } 65 assertTrue("Selected source not found in menu", found); 66 } 67 68 private static JMenuItem getSourceMenuItemByLabel(JPopupMenu menu, String label) { 69 for (Component c: menu.getComponents()) { 70 if (JPopupMenu.Separator.class.isInstance(c)) { 71 break; 72 } else if (((JMenuItem) c).getText() == label) { 73 return (JMenuItem) c; 74 } 75 // else continue... 76 } 77 fail("Failed to find menu item with label " + label); 78 return null; 79 } 80 81 /** 82 * Tests to switch imagery source. 83 * @throws Exception if any error occurs 84 */ 85 @Test 86 public void testSourceSwitching() throws Exception { 87 MinimapDialog dlg = new MinimapDialog(); 88 dlg.setSize(300, 200); 89 dlg.showDialog(); 90 SlippyMapBBoxChooser slippyMap = (SlippyMapBBoxChooser) TestUtils.getPrivateField(dlg, "slippyMap"); 91 SourceButton sourceButton = (SourceButton) TestUtils.getPrivateField(slippyMap, "iSourceButton"); 92 93 // get dlg in a paintable state 94 dlg.addNotify(); 95 dlg.doLayout(); 96 97 BufferedImage image = new BufferedImage( 98 slippyMap.getSize().width, 99 slippyMap.getSize().height, 100 BufferedImage.TYPE_INT_RGB 101 ); 102 103 Graphics2D g = image.createGraphics(); 104 // an initial paint operation is required to trigger the tile fetches 105 slippyMap.paintAll(g); 106 g.setBackground(Color.BLUE); 107 g.clearRect(0, 0, image.getWidth(), image.getHeight()); 108 g.dispose(); 109 110 Thread.sleep(500); 111 112 g = image.createGraphics(); 113 slippyMap.paintAll(g); 114 115 assertEquals(0xffffffff, image.getRGB(0, 0)); 116 117 assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "White Tiles"); 118 119 getSourceMenuItemByLabel(sourceButton.getPopupMenu(), "Magenta Tiles").doClick(); 120 assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "Magenta Tiles"); 121 // call paint to trigger new tile fetch 122 slippyMap.paintAll(g); 123 124 // clear background to a recognizably "wrong" color & dispose our Graphics2D so we don't risk carrying over 125 // any state 126 g.setBackground(Color.BLUE); 127 g.clearRect(0, 0, image.getWidth(), image.getHeight()); 128 g.dispose(); 129 130 Thread.sleep(500); 131 132 g = image.createGraphics(); 133 slippyMap.paintAll(g); 134 135 assertEquals(0xffff00ff, image.getRGB(0, 0)); 136 137 getSourceMenuItemByLabel(sourceButton.getPopupMenu(), "Green Tiles").doClick(); 138 assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "Green Tiles"); 139 // call paint to trigger new tile fetch 140 slippyMap.paintAll(g); 141 142 g.setBackground(Color.BLUE); 143 g.clearRect(0, 0, image.getWidth(), image.getHeight()); 144 g.dispose(); 145 146 Thread.sleep(500); 147 148 g = image.createGraphics(); 149 slippyMap.paintAll(g); 150 151 assertEquals(0xff00ff00, image.getRGB(0, 0)); 152 } 36 153 }
Note:
See TracChangeset
for help on using the changeset viewer.