fixes waveform hover behvior

This commit is contained in:
eyck committed 2026-11-21 14:47:51 +01:00
1 parent 693fe402ed
commit 941676c21b
4 files changed
+599 -133

No files matched your search

@@ -0,0 +1,208 @@
/*******************************************************************************
* Copyright (c) 2026 MINRES Technologies GmbH and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* MINRES Technologies GmbH - initial API and implementation
*******************************************************************************/
package com.minres.scviewer.e4.application.parts;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;
import java.lang.reflect.Field;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.minres.scviewer.database.ui.swt.Constants;
import com.minres.scviewer.database.ui.swt.IToolTipContentProvider;
import com.minres.scviewer.database.ui.swt.IToolTipHelpTextProvider;
import com.minres.scviewer.database.ui.swt.WaveformViewFactory;
public class HoverWindowTest {
private Display display;
private Shell owner;
private Canvas canvas;
private Object handler;
private Field popupField;
private final AtomicReference<Point> pointer = new AtomicReference<>();
private Label first, second;
@Before public void createControls() throws Exception {
assumeTrue("GTK integration tests require DISPLAY", !"gtk".equals(SWT.getPlatform())
|| System.getenv("DISPLAY") != null);
display = new Display();
owner = new Shell(display);
owner.setBounds(100, 100, 600, 400);
owner.setLayout(new FillLayout());
canvas = new Canvas(owner, SWT.NONE);
owner.open();
drain();
pointer.set(canvas.toDisplay(10, 10));
Class<?> type = WaveformViewFactory.class.getClassLoader().loadClass(
"com.minres.scviewer.database.ui.swt.internal.ToolTipHandler");
var constructor = type.getDeclaredConstructor(Shell.class, Supplier.class);
constructor.setAccessible(true);
handler = constructor.newInstance(owner, (Supplier<Point>) pointer::get);
var activate = type.getDeclaredMethod("activateHoverHelp", org.eclipse.swt.widgets.Control.class);
activate.setAccessible(true);
activate.invoke(handler, canvas);
popupField = type.getDeclaredField("shell");
popupField.setAccessible(true);
canvas.setData(Constants.CONTENT_PROVIDER_TAG, (IToolTipContentProvider) (parent, point) -> {
Composite content = new Composite(parent, SWT.NONE);
content.setLayout(new FillLayout());
first = new Label(content, SWT.NONE); first.setText("First label");
second = new Label(content, SWT.NONE); second.setText("Second label");
content.pack();
return true;
});
}
@After public void disposeControls() throws Exception {
if (display == null) return;
try {
if (handler != null) {
var close = handler.getClass().getDeclaredMethod("close");
close.setAccessible(true); close.invoke(handler);
}
if (owner != null && !owner.isDisposed()) owner.dispose();
drain();
} finally { display.dispose(); }
}
private void drain() {
int count = 0;
while (display.readAndDispatch()) assertTrue("UI event loop did not settle", ++count < 2000);
}
private Shell popup() throws Exception { return (Shell) popupField.get(handler); }
private Shell hover() throws Exception {
pointer.set(canvas.toDisplay(10, 10));
Event event = new Event(); event.x = 10; event.y = 10;
canvas.notifyListeners(SWT.MouseHover, event);
Shell popup = popup();
assertNotNull(popup);
assertTrue(popup.isVisible());
return popup;
}
private void assertBoundedAndScrollable(Shell popup) {
Rectangle area = owner.getMonitor().getClientArea(), bounds = popup.getBounds();
assertTrue(bounds.x >= area.x && bounds.y >= area.y);
assertTrue(bounds.x + bounds.width <= area.x + area.width);
assertTrue(bounds.y + bounds.height <= area.y + area.height);
ScrolledComposite scroll = (ScrolledComposite) popup.getChildren()[0];
assertTrue(scroll.getHorizontalBar().getVisible());
assertTrue(scroll.getVerticalBar().getVisible());
scroll.setOrigin(area.width, area.height);
assertTrue("Wide content must be reachable", scroll.getOrigin().x > 0);
assertTrue("Tall content must be reachable", scroll.getOrigin().y > 0);
}
@Test public void oversizedContentStaysOnScreenAndScrollsInBothDirections() throws Exception {
Rectangle area = owner.getMonitor().getClientArea();
canvas.setData(Constants.CONTENT_PROVIDER_TAG, (IToolTipContentProvider) (parent, point) -> {
Label label = new Label(parent, SWT.NONE);
label.setText("Oversized hover content");
label.setLayoutData(new GridData(area.width * 2, area.height * 2));
return true;
});
assertBoundedAndScrollable(hover());
}
@Test public void contentLoadedAfterOpeningAlsoStaysBoundedAndScrollable() throws Exception {
Shell popup = hover();
ScrolledComposite scroll = (ScrolledComposite) popup.getChildren()[0];
Composite content = (Composite) scroll.getContent();
assertFalse(scroll.getHorizontalBar().getVisible());
assertFalse(scroll.getVerticalBar().getVisible());
Rectangle area = owner.getMonitor().getClientArea();
first.getParent().setLayoutData(new GridData(area.width * 2, area.height * 2));
// The transaction hover packs its content after asynchronous attributes arrive.
content.pack();
assertBoundedAndScrollable(popup);
}
@Test public void sourceMovementAndExitingTheCanvasDismissHover() throws Exception {
Shell moving = hover();
canvas.notifyListeners(SWT.MouseMove, new Event());
assertTrue(moving.isDisposed());
Shell exiting = hover();
pointer.set(new Point(-10000, -10000));
canvas.notifyListeners(SWT.MouseExit, new Event());
drain();
assertTrue(exiting.isDisposed());
assertNull(popup());
}
@Test public void enteringPopupAndCrossingChildrenKeepsItOpenButLeavingClosesIt() throws Exception {
Shell popup = hover();
pointer.set(second.toDisplay(1, 1));
assertTrue(popup.getBounds().contains(pointer.get()));
canvas.notifyListeners(SWT.MouseExit, new Event());
first.notifyListeners(SWT.MouseExit, new Event());
drain();
assertFalse(popup.isDisposed());
// Leaving a child for the original canvas is also leaving the hover window.
pointer.set(canvas.toDisplay(canvas.getSize().x - 5, canvas.getSize().y - 5));
assertFalse(popup.getBounds().contains(pointer.get()));
second.notifyListeners(SWT.MouseExit, new Event());
drain();
assertTrue(popup.isDisposed());
}
@Test public void pointerCheckDismissesHoverWhenNativeExitEventIsMissing() throws Exception {
Shell popup = hover();
pointer.set(new Point(-10000, -10000));
long deadline = System.nanoTime() + java.util.concurrent.TimeUnit.SECONDS.toNanos(5);
while (!popup.isDisposed()) {
drain();
assertTrue("Hover did not disappear outside the window", System.nanoTime() < deadline);
Thread.sleep(5);
}
assertNull(popup());
}
@Test public void ownerDisposalCancelsChecksAndQueuedExitCallbacks() throws Exception {
Shell popup = hover();
canvas.notifyListeners(SWT.MouseExit, new Event());
canvas.dispose();
drain();
assertTrue(popup.isDisposed());
assertNull(popup());
assertFalse(owner.isListening(SWT.Deactivate));
}
@Test public void emptyContentDisposesPopupAndHelpUsesTheActiveHoverShell() throws Exception {
canvas.setData(Constants.HELP_PROVIDER_TAG, (IToolTipHelpTextProvider) widget -> "Hover help");
Shell popup = hover();
canvas.notifyListeners(SWT.Help, new Event());
assertTrue(popup.isDisposed());
assertNull(popup());
assertTrue(java.util.Arrays.stream(display.getShells()).anyMatch(shell -> shell != owner && !shell.isDisposed()));
canvas.setData(Constants.CONTENT_PROVIDER_TAG, (IToolTipContentProvider) (parent, point) -> false);
Event event = new Event(); event.x = 10; event.y = 10;
canvas.notifyListeners(SWT.MouseHover, event);
assertNull(popup());
}
}
@@ -0,0 +1,159 @@
/*******************************************************************************
* Copyright (c) 2026 MINRES Technologies GmbH and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* MINRES Technologies GmbH - initial API and implementation
*******************************************************************************/
package com.minres.scviewer.e4.application.parts;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BooleanSupplier;
import org.eclipse.e4.ui.di.UISynchronize;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.minres.scviewer.database.DataType;
import com.minres.scviewer.database.tx.ITx;
import com.minres.scviewer.database.tx.ITxAttribute;
import com.minres.scviewer.database.ui.AsyncTransactionData;
import com.minres.scviewer.database.ui.UiDispatch;
public class TransactionHoverTest {
private Display display;
private WaveformViewer viewer;
private Method snapshot, refresh;
private final CountDownLatch loading = new CountDownLatch(1), release = new CountDownLatch(1);
private final AtomicInteger reads = new AtomicInteger();
private ITx transaction;
@Before public void setup() throws Exception {
assumeTrue("GTK integration tests require DISPLAY", !"gtk".equals(SWT.getPlatform())
|| System.getenv("DISPLAY") != null);
display = new Display();
viewer = new WaveformViewer();
viewer.sync = new UISynchronize() {
@Override public void asyncExec(Runnable runnable) { UiDispatch.async(display, runnable); }
@Override public void syncExec(Runnable runnable) { display.syncExec(runnable); }
@Override protected boolean isUIThread(Thread thread) { return thread == display.getThread(); }
@Override protected void showBusyWhile(Runnable runnable) { runnable.run(); }
@Override protected boolean dispatchEvents() { return display.readAndDispatch(); }
};
snapshot = WaveformViewer.class.getDeclaredMethod("transactionSnapshot", ITx.class);
snapshot.setAccessible(true);
refresh = WaveformViewer.class.getDeclaredMethod("refreshHoverWhenReady", Composite.class, ITx.class, Runnable.class);
refresh.setAccessible(true);
ITxAttribute attribute = (ITxAttribute) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{ITxAttribute.class},
(proxy, method, args) -> switch (method.getName()) {
case "getName" -> "address";
case "getValue" -> "0x1234";
case "getDataType" -> DataType.STRING;
default -> null;
});
transaction = (ITx) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{ITx.class},
(proxy, method, args) -> {
if (method.getName().equals("getAttributes")) {
assertNull("Hover attributes must load off SWT", Display.getCurrent());
reads.incrementAndGet(); loading.countDown();
assertTrue(release.await(5, TimeUnit.SECONDS));
return List.of(attribute);
}
if (method.getName().endsWith("Relations")) return List.of();
return null;
});
}
@After public void cleanup() {
release.countDown();
if (display == null) return;
viewer.closeDatabase();
for (Shell shell : display.getShells()) if (!shell.isDisposed()) shell.dispose();
while (display.readAndDispatch()) { }
display.dispose();
}
private AsyncTransactionData.Snapshot data() {
try { return (AsyncTransactionData.Snapshot) snapshot.invoke(viewer, transaction); }
catch (ReflectiveOperationException e) { throw new AssertionError(e); }
}
private Table show(Shell popup, AtomicInteger updates) throws Exception {
Table table = new Table(popup, SWT.NONE);
new TableColumn(table, SWT.NONE); new TableColumn(table, SWT.NONE);
refresh.invoke(viewer, popup, transaction, (Runnable) () -> {
assertSame(display, Display.getCurrent());
updates.incrementAndGet();
table.removeAll();
for (ITxAttribute attribute : data().attributes())
new TableItem(table, SWT.NONE).setText(new String[]{attribute.getName(), attribute.getValue().toString()});
});
return table;
}
private void await(BooleanSupplier condition) throws Exception {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
while (!condition.getAsBoolean()) {
while (display.readAndDispatch()) { }
assertTrue("Attribute completion was not published", System.nanoTime() < deadline);
Thread.sleep(5);
}
}
@Test public void firstHoverPopulatesWhenTheBackgroundReadCompletes() throws Exception {
AtomicInteger updates = new AtomicInteger();
Table table = show(new Shell(display), updates);
assertTrue(loading.await(5, TimeUnit.SECONDS));
assertEquals(0, table.getItemCount());
release.countDown();
await(() -> table.getItemCount() == 1);
assertEquals("address", table.getItem(0).getText(0));
assertEquals("0x1234", table.getItem(0).getText(1));
assertEquals(2, updates.get());
assertEquals(1, reads.get());
}
@Test public void closingAnOldPopupDoesNotDetachItsReplacement() throws Exception {
Shell old = new Shell(display);
AtomicInteger oldUpdates = new AtomicInteger(), newUpdates = new AtomicInteger();
show(old, oldUpdates);
assertTrue(loading.await(5, TimeUnit.SECONDS));
Table replacement = show(new Shell(display), newUpdates);
old.dispose();
release.countDown();
await(() -> replacement.getItemCount() == 1);
assertEquals(1, oldUpdates.get());
assertEquals(2, newUpdates.get());
assertEquals(1, reads.get());
}
@Test public void aClosedPopupIsNotRefreshedAfterLoading() throws Exception {
Shell popup = new Shell(display);
AtomicInteger updates = new AtomicInteger();
show(popup, updates);
assertTrue(loading.await(5, TimeUnit.SECONDS));
popup.dispose(); release.countDown();
var field = WaveformViewer.class.getDeclaredField("transactionSnapshots"); field.setAccessible(true);
AsyncTransactionData cache = (AsyncTransactionData) field.get(viewer);
await(() -> cache.isReady(transaction));
assertEquals(1, updates.get());
}
}