Changeset 18574 in josm for trunk/test/unit/org
- Timestamp:
- 2022-10-17T16:22:45+02:00 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/gui/NavigatableComponentTest.java
r17275 r18574 4 4 import static org.hamcrest.MatcherAssert.assertThat; 5 5 import static org.junit.jupiter.api.Assertions.assertEquals; 6 import static org.junit.jupiter.api.Assertions.assertNotNull; 7 import static org.junit.jupiter.api.Assertions.assertNull; 8 import static org.junit.jupiter.api.Assertions.assertSame; 6 9 7 10 import java.awt.Point; 8 11 import java.awt.Rectangle; 12 import java.awt.event.MouseEvent; 9 13 import java.awt.geom.Point2D; 10 14 import java.util.Objects; 15 import java.util.concurrent.atomic.AtomicReference; 11 16 12 17 import javax.swing.JPanel; … … 22 27 import org.openstreetmap.josm.data.coor.EastNorth; 23 28 import org.openstreetmap.josm.data.coor.LatLon; 29 import org.openstreetmap.josm.data.osm.DataSet; 30 import org.openstreetmap.josm.data.osm.Node; 24 31 import org.openstreetmap.josm.data.projection.ProjectionRegistry; 32 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 25 33 import org.openstreetmap.josm.gui.util.GuiHelper; 26 34 import org.openstreetmap.josm.testutils.JOSMTestRules; … … 45 53 return true; 46 54 } 55 56 @Override 57 public void processMouseMotionEvent(MouseEvent mouseEvent) { 58 super.processMouseMotionEvent(mouseEvent); 59 } 47 60 } 48 61 49 62 private static final int HEIGHT = 200; 50 63 private static final int WIDTH = 300; 51 private NavigatableComponent component;64 private NavigatableComponentMock component; 52 65 53 66 /** … … 66 79 component.setBounds(new Rectangle(WIDTH, HEIGHT)); 67 80 // wait for the event to be propagated. 68 GuiHelper.runInEDTAndWait(new Runnable() { 69 @Override 70 public void run() { 71 } 72 }); 81 GuiHelper.runInEDTAndWait(() -> { /* Do nothing */ }); 73 82 component.setVisible(true); 74 83 JPanel parent = new JPanel(); … … 207 216 assertThat(bounds.getMin(), CustomMatchers.is(component.getLatLon(0, HEIGHT))); 208 217 assertThat(bounds.getMax(), CustomMatchers.is(component.getLatLon(WIDTH, 0))); 218 } 219 220 @Test 221 void testHoverListeners() { 222 AtomicReference<PrimitiveHoverListener.PrimitiveHoverEvent> hoverEvent = new AtomicReference<>(); 223 PrimitiveHoverListener testListener = hoverEvent::set; 224 assertNull(hoverEvent.get()); 225 component.addNotify(); 226 component.addPrimitiveHoverListener(testListener); 227 DataSet ds = new DataSet(); 228 MainApplication.getLayerManager().addLayer(new OsmDataLayer(ds, "testHoverListeners", null)); 229 LatLon center = component.getRealBounds().getCenter(); 230 Node node1 = new Node(center); 231 ds.addPrimitive(node1); 232 double x = component.getBounds().getCenterX(); 233 double y = component.getBounds().getCenterY(); 234 // Check hover over primitive 235 MouseEvent node1Event = new MouseEvent(component, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 236 0, (int) x, (int) y, 0, false, MouseEvent.NOBUTTON); 237 component.processMouseMotionEvent(node1Event); 238 GuiHelper.runInEDTAndWait(() -> { /* Sync */ }); 239 PrimitiveHoverListener.PrimitiveHoverEvent event = hoverEvent.getAndSet(null); 240 assertNotNull(event); 241 assertSame(node1, event.getHoveredPrimitive()); 242 assertNull(event.getPreviousPrimitive()); 243 assertSame(node1Event, event.getMouseEvent()); 244 // Check moving to the (same) primitive. No new mouse motion event should be called. 245 component.processMouseMotionEvent(node1Event); 246 GuiHelper.runInEDTAndWait(() -> { /* Sync */ }); 247 event = hoverEvent.getAndSet(null); 248 assertNull(event); 249 // Check moving off primitive. A new mouse motion event should be called with the previous primitive and null. 250 MouseEvent noNodeEvent = 251 new MouseEvent(component, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, 0, 0, 0, false, MouseEvent.NOBUTTON); 252 component.processMouseMotionEvent(noNodeEvent); 253 GuiHelper.runInEDTAndWait(() -> { /* Sync */ }); 254 event = hoverEvent.getAndSet(null); 255 assertNotNull(event); 256 assertSame(node1, event.getPreviousPrimitive()); 257 assertNull(event.getHoveredPrimitive()); 258 assertSame(noNodeEvent, event.getMouseEvent()); 259 // Check moving to area with no primitive with no previous hover primitive 260 component.processMouseMotionEvent( 261 new MouseEvent(component, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, 1, 1, 0, false, MouseEvent.NOBUTTON)); 262 assertNull(hoverEvent.get()); 209 263 } 210 264
Note:
See TracChangeset
for help on using the changeset viewer.