Changeset 18574 in josm for trunk/test/unit/org


Ignore:
Timestamp:
2022-10-17T16:22:45+02:00 (2 years ago)
Author:
taylor.smock
Message:

Fix #22378: Preview object properties on mouse hover (patch by Woazboat, modified)

Show tags and relation memberships of an object in the properties dialog when
moving the mouser pointer over it (similar to iD).

Can be enabled/disabled via settings (enabled by default)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/gui/NavigatableComponentTest.java

    r17275 r18574  
    44import static org.hamcrest.MatcherAssert.assertThat;
    55import static org.junit.jupiter.api.Assertions.assertEquals;
     6import static org.junit.jupiter.api.Assertions.assertNotNull;
     7import static org.junit.jupiter.api.Assertions.assertNull;
     8import static org.junit.jupiter.api.Assertions.assertSame;
    69
    710import java.awt.Point;
    811import java.awt.Rectangle;
     12import java.awt.event.MouseEvent;
    913import java.awt.geom.Point2D;
    1014import java.util.Objects;
     15import java.util.concurrent.atomic.AtomicReference;
    1116
    1217import javax.swing.JPanel;
     
    2227import org.openstreetmap.josm.data.coor.EastNorth;
    2328import org.openstreetmap.josm.data.coor.LatLon;
     29import org.openstreetmap.josm.data.osm.DataSet;
     30import org.openstreetmap.josm.data.osm.Node;
    2431import org.openstreetmap.josm.data.projection.ProjectionRegistry;
     32import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    2533import org.openstreetmap.josm.gui.util.GuiHelper;
    2634import org.openstreetmap.josm.testutils.JOSMTestRules;
     
    4553            return true;
    4654        }
     55
     56        @Override
     57        public void processMouseMotionEvent(MouseEvent mouseEvent) {
     58            super.processMouseMotionEvent(mouseEvent);
     59        }
    4760    }
    4861
    4962    private static final int HEIGHT = 200;
    5063    private static final int WIDTH = 300;
    51     private NavigatableComponent component;
     64    private NavigatableComponentMock component;
    5265
    5366    /**
     
    6679        component.setBounds(new Rectangle(WIDTH, HEIGHT));
    6780        // 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 */ });
    7382        component.setVisible(true);
    7483        JPanel parent = new JPanel();
     
    207216        assertThat(bounds.getMin(), CustomMatchers.is(component.getLatLon(0, HEIGHT)));
    208217        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());
    209263    }
    210264
Note: See TracChangeset for help on using the changeset viewer.