| | 48 | |
| | 49 | private static void assertSingleSelectedSourceLabel(JPopupMenu menu, String label) { |
| | 50 | boolean found = false; |
| | 51 | for (Component c: menu.getComponents()) { |
| | 52 | if (JPopupMenu.Separator.class.isInstance(c)) { |
| | 53 | break; |
| | 54 | } else { |
| | 55 | boolean equalText = ((JMenuItem) c).getText() == label; |
| | 56 | boolean isSelected = ((JMenuItem) c).isSelected(); |
| | 57 | assertEquals(equalText, isSelected); |
| | 58 | if (equalText) { |
| | 59 | assertFalse("Second selected source found", found); |
| | 60 | found = true; |
| | 61 | } |
| | 62 | } |
| | 63 | } |
| | 64 | assertTrue("Selected source not found in menu", found); |
| | 65 | } |
| | 66 | |
| | 67 | private static JMenuItem getSourceMenuItemByLabel(JPopupMenu menu, String label) { |
| | 68 | for (Component c: menu.getComponents()) { |
| | 69 | if (JPopupMenu.Separator.class.isInstance(c)) { |
| | 70 | break; |
| | 71 | } else if (((JMenuItem) c).getText() == label) { |
| | 72 | return (JMenuItem) c; |
| | 73 | } |
| | 74 | // else continue... |
| | 75 | } |
| | 76 | fail("Failed to find menu item with label " + label); |
| | 77 | return null; |
| | 78 | } |
| | 79 | |
| | 80 | @Test |
| | 81 | public void testSourceSwitching() throws Throwable { |
| | 82 | MinimapDialog dlg = new MinimapDialog(); |
| | 83 | dlg.setSize(300, 200); |
| | 84 | dlg.showDialog(); |
| | 85 | SlippyMapBBoxChooser slippyMap = (SlippyMapBBoxChooser) TestUtils.getPrivateField(dlg, "slippyMap"); |
| | 86 | SourceButton sourceButton = (SourceButton) TestUtils.getPrivateField(slippyMap, "iSourceButton"); |
| | 87 | |
| | 88 | // get dlg in a paintable state |
| | 89 | dlg.addNotify(); |
| | 90 | dlg.doLayout(); |
| | 91 | |
| | 92 | BufferedImage image = new BufferedImage( |
| | 93 | slippyMap.getSize().width, |
| | 94 | slippyMap.getSize().height, |
| | 95 | BufferedImage.TYPE_INT_RGB |
| | 96 | ); |
| | 97 | |
| | 98 | Graphics2D g = image.createGraphics(); |
| | 99 | // an initial paint operation is required to trigger the tile fetches |
| | 100 | slippyMap.paintAll(g); |
| | 101 | g.setBackground(Color.BLUE); |
| | 102 | g.clearRect(0, 0, image.getWidth(), image.getHeight()); |
| | 103 | g.dispose(); |
| | 104 | |
| | 105 | Thread.sleep(500); |
| | 106 | |
| | 107 | g = image.createGraphics(); |
| | 108 | slippyMap.paintAll(g); |
| | 109 | |
| | 110 | assertEquals(0xffffffff, image.getRGB(0, 0)); |
| | 111 | |
| | 112 | assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "White Tiles"); |
| | 113 | |
| | 114 | getSourceMenuItemByLabel(sourceButton.getPopupMenu(), "Magenta Tiles").doClick(); |
| | 115 | assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "Magenta Tiles"); |
| | 116 | // call paint to trigger new tile fetch |
| | 117 | slippyMap.paintAll(g); |
| | 118 | |
| | 119 | // clear background to a recognizably "wrong" color & dispose our Graphics2D so we don't risk carrying over |
| | 120 | // any state |
| | 121 | g.setBackground(Color.BLUE); |
| | 122 | g.clearRect(0, 0, image.getWidth(), image.getHeight()); |
| | 123 | g.dispose(); |
| | 124 | |
| | 125 | Thread.sleep(500); |
| | 126 | |
| | 127 | g = image.createGraphics(); |
| | 128 | slippyMap.paintAll(g); |
| | 129 | |
| | 130 | assertEquals(0xffff00ff, image.getRGB(0, 0)); |
| | 131 | |
| | 132 | getSourceMenuItemByLabel(sourceButton.getPopupMenu(), "Green Tiles").doClick(); |
| | 133 | assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "Green Tiles"); |
| | 134 | // call paint to trigger new tile fetch |
| | 135 | slippyMap.paintAll(g); |
| | 136 | |
| | 137 | g.setBackground(Color.BLUE); |
| | 138 | g.clearRect(0, 0, image.getWidth(), image.getHeight()); |
| | 139 | g.dispose(); |
| | 140 | |
| | 141 | Thread.sleep(500); |
| | 142 | |
| | 143 | g = image.createGraphics(); |
| | 144 | slippyMap.paintAll(g); |
| | 145 | |
| | 146 | assertEquals(0xff00ff00, image.getRGB(0, 0)); |
| | 147 | |
| | 148 | // useful for debugging |
| | 149 | // try { |
| | 150 | // javax.imageio.ImageIO.write(image, "png", new java.io.File("painted.png")); |
| | 151 | // } catch (java.io.IOException ioe) { |
| | 152 | // System.err.println("Failed writing image"); |
| | 153 | // } |
| | 154 | } |