| 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 | @Test |
| 82 | public void testSourceSwitching() throws Throwable { |
| 83 | MinimapDialog dlg = new MinimapDialog(); |
| 84 | dlg.setSize(300, 200); |
| 85 | dlg.showDialog(); |
| 86 | SlippyMapBBoxChooser slippyMap = (SlippyMapBBoxChooser)TestUtils.getPrivateField(dlg, "slippyMap"); |
| 87 | SourceButton sourceButton = (SourceButton)TestUtils.getPrivateField(slippyMap, "iSourceButton"); |
| 88 | |
| 89 | // get dlg in a paintable state |
| 90 | dlg.addNotify(); |
| 91 | dlg.doLayout(); |
| 92 | |
| 93 | BufferedImage image = new BufferedImage( |
| 94 | slippyMap.getSize().width, |
| 95 | slippyMap.getSize().height, |
| 96 | BufferedImage.TYPE_INT_RGB |
| 97 | ); |
| 98 | |
| 99 | Graphics2D g = image.createGraphics(); |
| 100 | // an initial paint operation is required to trigger the tile fetches |
| 101 | slippyMap.paintAll(g); |
| 102 | g.setBackground(Color.BLUE); |
| 103 | g.clearRect(0, 0, image.getWidth(), image.getHeight()); |
| 104 | g.dispose(); |
| 105 | |
| 106 | Thread.sleep(3500); |
| 107 | |
| 108 | g = image.createGraphics(); |
| 109 | slippyMap.paintAll(g); |
| 110 | |
| 111 | assertEquals(0xffffffff, image.getRGB(0, 0)); |
| 112 | |
| 113 | assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "White Tiles"); |
| 114 | |
| 115 | getSourceMenuItemByLabel(sourceButton.getPopupMenu(), "Magenta Tiles").doClick(); |
| 116 | assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "Magenta Tiles"); |
| 117 | // call paint to trigger new tile fetch |
| 118 | slippyMap.paintAll(g); |
| 119 | |
| 120 | // clear background to a recognizably "wrong" color & dispose our Graphics2D so we don't risk carrying over |
| 121 | // any state |
| 122 | g.setBackground(Color.BLUE); |
| 123 | g.clearRect(0, 0, image.getWidth(), image.getHeight()); |
| 124 | g.dispose(); |
| 125 | |
| 126 | Thread.sleep(500); |
| 127 | |
| 128 | g = image.createGraphics(); |
| 129 | slippyMap.paintAll(g); |
| 130 | |
| 131 | assertEquals(0xffff00ff, image.getRGB(0, 0)); |
| 132 | |
| 133 | getSourceMenuItemByLabel(sourceButton.getPopupMenu(), "Green Tiles").doClick(); |
| 134 | assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "Green Tiles"); |
| 135 | // call paint to trigger new tile fetch |
| 136 | slippyMap.paintAll(g); |
| 137 | |
| 138 | g.setBackground(Color.BLUE); |
| 139 | g.clearRect(0, 0, image.getWidth(), image.getHeight()); |
| 140 | g.dispose(); |
| 141 | |
| 142 | Thread.sleep(500); |
| 143 | |
| 144 | g = image.createGraphics(); |
| 145 | slippyMap.paintAll(g); |
| 146 | |
| 147 | assertEquals(0xff00ff00, image.getRGB(0, 0)); |
| 148 | |
| 149 | // useful for debugging |
| 150 | // try { |
| 151 | // javax.imageio.ImageIO.write(image, "png", new java.io.File("painted.png")); |
| 152 | // } catch (java.io.IOException ioe) { |
| 153 | // System.err.println("Failed writing image"); |
| 154 | // } |
| 155 | } |