|
| 1 | +package ui; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import static org.junit.jupiter.api.Assertions.*; |
| 5 | + |
| 6 | +/** |
| 7 | + * Unit tests for the zoom calculation logic in {@link ZoomCalculator}. |
| 8 | + * |
| 9 | + * <p>All tests exercise {@link ZoomCalculator#computeNewZoom} as a pure, |
| 10 | + * JavaFX-free function. This avoids the need for a running JavaFX runtime and |
| 11 | + * makes the suite fast and portable.</p> |
| 12 | + * |
| 13 | + * <p>The primary motivation for these tests is the Linux scroll-wheel bug: |
| 14 | + * on Linux, {@code ScrollEvent.getDeltaY()} can return {@code 0.0} for |
| 15 | + * physical mouse-wheel rotations. The previous code treated every zero-delta |
| 16 | + * event as a zoom-out step, making zoom-in impossible. The fixed |
| 17 | + * {@link ZoomCalculator#computeNewZoom} method falls back to |
| 18 | + * {@code textDeltaY} when {@code deltaY} is zero, and ignores the event |
| 19 | + * completely when both are zero.</p> |
| 20 | + */ |
| 21 | +public class VisualLinkPanelZoomTest { |
| 22 | + |
| 23 | + // ── constants that mirror VisualLinkPanel's zoom configuration ───────── |
| 24 | + private static final double FACTOR = ZoomCalculator.ZOOM_FACTOR; // 1.15 |
| 25 | + private static final double MIN = ZoomCalculator.ZOOM_MIN; // 0.1 |
| 26 | + private static final double MAX = ZoomCalculator.ZOOM_MAX; // 8.0 |
| 27 | + private static final double CURRENT = 1.0; |
| 28 | + private static final double DELTA = 1e-9; |
| 29 | + |
| 30 | + /** Helper: call computeNewZoom with the standard constants. */ |
| 31 | + private static double zoom(double deltaY, double textDeltaY, double current) { |
| 32 | + return ZoomCalculator.computeNewZoom(deltaY, textDeltaY, current, FACTOR, MIN, MAX); |
| 33 | + } |
| 34 | + |
| 35 | + // ── normal scroll (getDeltaY() != 0) ────────────────────────────────── |
| 36 | + |
| 37 | + @Test |
| 38 | + void scrollUp_positiveDelta_zoomsIn() { |
| 39 | + double result = zoom(40.0, 0.0, CURRENT); |
| 40 | + assertEquals(CURRENT * FACTOR, result, DELTA, |
| 41 | + "positive deltaY must produce a zoom-in (multiply by factor)"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void scrollDown_negativeDelta_zoomsOut() { |
| 46 | + double result = zoom(-40.0, 0.0, CURRENT); |
| 47 | + assertEquals(CURRENT / FACTOR, result, DELTA, |
| 48 | + "negative deltaY must produce a zoom-out (divide by factor)"); |
| 49 | + } |
| 50 | + |
| 51 | + // ── Linux fallback: getDeltaY() == 0, use textDeltaY ───────────────── |
| 52 | + |
| 53 | + @Test |
| 54 | + void linuxScrollUp_zeroDelta_positiveTextDelta_zoomsIn() { |
| 55 | + // Simulates a Linux mouse-wheel-up event where getDeltaY() == 0 |
| 56 | + double result = zoom(0.0, 1.0, CURRENT); |
| 57 | + assertEquals(CURRENT * FACTOR, result, DELTA, |
| 58 | + "when deltaY == 0, positive textDeltaY must still produce a zoom-in"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void linuxScrollDown_zeroDelta_negativeTextDelta_zoomsOut() { |
| 63 | + // Simulates a Linux mouse-wheel-down event where getDeltaY() == 0 |
| 64 | + double result = zoom(0.0, -1.0, CURRENT); |
| 65 | + assertEquals(CURRENT / FACTOR, result, DELTA, |
| 66 | + "when deltaY == 0, negative textDeltaY must still produce a zoom-out"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void bothDeltasZero_returnsCurrentZoom() { |
| 71 | + // Both deltas zero → no scroll occurred; zoom must not change |
| 72 | + double result = zoom(0.0, 0.0, CURRENT); |
| 73 | + assertEquals(CURRENT, result, DELTA, |
| 74 | + "when both deltas are zero, zoom must remain unchanged"); |
| 75 | + } |
| 76 | + |
| 77 | + // ── clamping ────────────────────────────────────────────────────────── |
| 78 | + |
| 79 | + @Test |
| 80 | + void zoomIn_atMaximum_clampedToMax() { |
| 81 | + double result = zoom(40.0, 0.0, MAX); |
| 82 | + assertEquals(MAX, result, DELTA, |
| 83 | + "zoom-in at maximum must be clamped to ZOOM_MAX"); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void zoomOut_atMinimum_clampedToMin() { |
| 88 | + double result = zoom(-40.0, 0.0, MIN); |
| 89 | + assertEquals(MIN, result, DELTA, |
| 90 | + "zoom-out at minimum must be clamped to ZOOM_MIN"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void zoomIn_nearMaximum_clampedToMax() { |
| 95 | + // A zoom level just below MAX × FACTOR would exceed the limit |
| 96 | + double nearMax = MAX / FACTOR + 0.001; |
| 97 | + double result = zoom(40.0, 0.0, nearMax); |
| 98 | + assertEquals(MAX, result, DELTA, |
| 99 | + "zoom-in result exceeding ZOOM_MAX must be clamped to ZOOM_MAX"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void zoomOut_nearMinimum_clampedToMin() { |
| 104 | + double nearMin = MIN * FACTOR - 0.001; |
| 105 | + double result = zoom(-40.0, 0.0, nearMin); |
| 106 | + assertEquals(MIN, result, DELTA, |
| 107 | + "zoom-out result below ZOOM_MIN must be clamped to ZOOM_MIN"); |
| 108 | + } |
| 109 | + |
| 110 | + // ── symmetry: zoom-in then zoom-out should round-trip ───────────────── |
| 111 | + |
| 112 | + @Test |
| 113 | + void zoomInThenOut_isApproximatelySymmetric() { |
| 114 | + double afterIn = zoom(40.0, 0.0, CURRENT); |
| 115 | + double afterOut = zoom(-40.0, 0.0, afterIn); |
| 116 | + assertEquals(CURRENT, afterOut, 1e-6, |
| 117 | + "one zoom-in followed by one zoom-out must return to the original level"); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void linuxFallback_zoomInThenOut_isApproximatelySymmetric() { |
| 122 | + // Same round-trip test but using the Linux textDeltaY path |
| 123 | + double afterIn = zoom(0.0, 1.0, CURRENT); |
| 124 | + double afterOut = zoom(0.0, -1.0, afterIn); |
| 125 | + assertEquals(CURRENT, afterOut, 1e-6, |
| 126 | + "Linux textDeltaY zoom-in then zoom-out must round-trip correctly"); |
| 127 | + } |
| 128 | + |
| 129 | + // ── deltaY takes precedence over textDeltaY ─────────────────────────── |
| 130 | + |
| 131 | + @Test |
| 132 | + void nonZeroDeltaY_overridesTextDeltaY() { |
| 133 | + // deltaY positive → zoom in, even though textDeltaY is negative |
| 134 | + double result = zoom(40.0, -999.0, CURRENT); |
| 135 | + assertEquals(CURRENT * FACTOR, result, DELTA, |
| 136 | + "non-zero deltaY must take precedence over textDeltaY"); |
| 137 | + } |
| 138 | +} |
| 139 | + |
0 commit comments