|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 vogella GmbH and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * vogella GmbH - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.swt.widgets; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 20 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.TreeSet; |
| 25 | + |
| 26 | +import org.eclipse.swt.SWT; |
| 27 | +import org.eclipse.swt.graphics.GC; |
| 28 | +import org.eclipse.swt.graphics.Image; |
| 29 | +import org.eclipse.swt.graphics.ImageData; |
| 30 | +import org.eclipse.swt.graphics.Rectangle; |
| 31 | +import org.eclipse.swt.internal.DPIUtil; |
| 32 | +import org.eclipse.swt.internal.PlatformSpecificExecutionExtension; |
| 33 | +import org.eclipse.swt.internal.win32.OS; |
| 34 | +import org.junit.jupiter.api.AfterEach; |
| 35 | +import org.junit.jupiter.api.BeforeEach; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 38 | + |
| 39 | +/** Pins how a multi-column Tree paints a row hovered through native messages, which public API cannot send. */ |
| 40 | +@ExtendWith(PlatformSpecificExecutionExtension.class) |
| 41 | +class TreeWin32Tests { |
| 42 | + |
| 43 | + private static final int COLUMNS = 3; |
| 44 | + private static final int COLUMN_WIDTH = 120; |
| 45 | + |
| 46 | + private Display display; |
| 47 | + private Shell shell; |
| 48 | + private Button focusTarget; |
| 49 | + private Tree paintedTree; |
| 50 | + private TreeItem paintedItem; |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + public void setUp() { |
| 54 | + display = Display.getDefault(); |
| 55 | + shell = new Shell(display); |
| 56 | + focusTarget = new Button(shell, SWT.PUSH); |
| 57 | + focusTarget.setBounds(0, 0, 50, 20); |
| 58 | + } |
| 59 | + |
| 60 | + @AfterEach |
| 61 | + public void tearDown() { |
| 62 | + shell.dispose(); |
| 63 | + display.dispose(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void test_hoverIsPainted() { |
| 68 | + openPaintedTree(SWT.FULL_SELECTION | SWT.MULTI); |
| 69 | + // The real pointer may rest on the row. |
| 70 | + OS.SendMessage(paintedTree.handle, OS.WM_MOUSELEAVE, 0, 0); |
| 71 | + int[] normal = captureColumn(0); |
| 72 | + hover(); |
| 73 | + assertFalse(Arrays.equals(normal, captureColumn(0)), "the simulated hover did not reach the tree"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void test_hoveredSelectedRowHasOneBackground() { |
| 78 | + openPaintedTree(SWT.FULL_SELECTION | SWT.MULTI); |
| 79 | + paintedTree.setSelection(paintedItem); |
| 80 | + hover(); |
| 81 | + assertArrayEquals(captureColumn(0), captureColumn(1)); |
| 82 | + assertArrayEquals(captureColumn(0), captureColumn(2)); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void test_hoveredSelectedRowReportsHotForAllColumns() { |
| 87 | + openPaintedTree(SWT.FULL_SELECTION | SWT.MULTI); |
| 88 | + Set<Integer> hotColumns = new TreeSet<>(); |
| 89 | + paintedTree.addListener(SWT.EraseItem, event -> { |
| 90 | + if (event.item == paintedItem && (event.detail & SWT.HOT) != 0) { |
| 91 | + hotColumns.add(event.index); |
| 92 | + } |
| 93 | + }); |
| 94 | + paintedTree.setSelection(paintedItem); |
| 95 | + hover(); |
| 96 | + paintedTree.redraw(); |
| 97 | + paintedTree.update(); |
| 98 | + assertEquals(allColumns(), hotColumns); |
| 99 | + assertArrayEquals(captureColumn(0), captureColumn(1)); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void test_clearingHotAppliesToTheWholeRow() { |
| 104 | + openPaintedTree(SWT.FULL_SELECTION | SWT.MULTI); |
| 105 | + paintedTree.addListener(SWT.EraseItem, event -> event.detail &= ~SWT.HOT); |
| 106 | + paintedTree.setSelection(paintedItem); |
| 107 | + int[] selected = captureColumn(1); |
| 108 | + hover(); |
| 109 | + assertArrayEquals(selected, captureColumn(0)); |
| 110 | + assertArrayEquals(selected, captureColumn(1)); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void test_hoveredRowWithEraseItemListenerHasOneBackground() { |
| 115 | + openPaintedTree(SWT.FULL_SELECTION | SWT.MULTI); |
| 116 | + paintedTree.addListener(SWT.EraseItem, event -> {}); |
| 117 | + hover(); |
| 118 | + assertArrayEquals(captureColumn(0), captureColumn(1)); |
| 119 | + assertArrayEquals(captureColumn(0), captureColumn(2)); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void test_unfocusedSelectedRowHasOneBackground() { |
| 124 | + openPaintedTree(SWT.FULL_SELECTION | SWT.MULTI); |
| 125 | + paintedTree.setSelection(paintedItem); |
| 126 | + focusTarget.setFocus(); |
| 127 | + assertNotEquals(paintedTree.handle, OS.GetFocus(), "the tree kept the focus"); |
| 128 | + assertArrayEquals(captureColumn(0), captureColumn(1)); |
| 129 | + hover(); |
| 130 | + assertArrayEquals(captureColumn(0), captureColumn(1)); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void test_reorderedColumnsHaveOneBackground() { |
| 135 | + openPaintedTree(SWT.FULL_SELECTION | SWT.MULTI); |
| 136 | + paintedTree.addListener(SWT.EraseItem, event -> {}); |
| 137 | + paintedTree.setColumnOrder(new int[] { 2, 0, 1 }); |
| 138 | + paintedTree.setSelection(paintedItem); |
| 139 | + hover(); |
| 140 | + int[] rowEnd = capture(paintedTree.getClientArea().width - 5); |
| 141 | + assertArrayEquals(rowEnd, captureColumn(0)); |
| 142 | + assertArrayEquals(rowEnd, captureColumn(1)); |
| 143 | + assertArrayEquals(rowEnd, captureColumn(2)); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void test_eraseItemListenerKeepsHoveredSelectionWithoutFullSelection() { |
| 148 | + openPaintedTree(SWT.MULTI); |
| 149 | + // At 125% the selection edges of the two paint paths differ by a pixel. |
| 150 | + assumeTrue(DPIUtil.getZoomForAutoscaleProperty(paintedTree.getMonitor().getZoom()) == 100); |
| 151 | + paintedItem.setText("text"); |
| 152 | + int[] unselected = captureRowTop(); |
| 153 | + paintedTree.setSelection(paintedItem); |
| 154 | + hover(); |
| 155 | + int[] painted = captureRowTop(); |
| 156 | + assertFalse(Arrays.equals(unselected, painted), "the selection was not painted"); |
| 157 | + paintedTree.addListener(SWT.EraseItem, event -> {}); |
| 158 | + assertArrayEquals(painted, captureRowTop()); |
| 159 | + } |
| 160 | + |
| 161 | + private static Set<Integer> allColumns() { |
| 162 | + Set<Integer> columns = new TreeSet<>(); |
| 163 | + for (int i = 0; i < COLUMNS; i++) { |
| 164 | + columns.add(i); |
| 165 | + } |
| 166 | + return columns; |
| 167 | + } |
| 168 | + |
| 169 | + /** Opens a focused tree without an EraseItem listener, because adding one changes how the tree paints. */ |
| 170 | + private void openPaintedTree(int style) { |
| 171 | + paintedTree = new Tree(shell, style); |
| 172 | + paintedTree.setBounds(0, 30, 4 * COLUMN_WIDTH, 150); |
| 173 | + for (int i = 0; i < COLUMNS; i++) { |
| 174 | + new TreeColumn(paintedTree, SWT.NONE).setWidth(COLUMN_WIDTH); |
| 175 | + } |
| 176 | + for (int i = 0; i < 3; i++) { |
| 177 | + new TreeItem(paintedTree, SWT.NONE); |
| 178 | + } |
| 179 | + paintedItem = paintedTree.getItem(1); |
| 180 | + shell.setSize(5 * COLUMN_WIDTH, 250); |
| 181 | + shell.open(); |
| 182 | + paintedTree.setFocus(); |
| 183 | + while (display.readAndDispatch()) { |
| 184 | + // process all pending events |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + /** Hovers the item with a message, so that the real mouse pointer is not involved. */ |
| 189 | + private void hover() { |
| 190 | + Rectangle bounds = paintedItem.getBounds(0); |
| 191 | + int zoom = DPIUtil.getZoomForAutoscaleProperty(paintedTree.getMonitor().getZoom()); |
| 192 | + int x = DPIUtil.pointToPixel(bounds.x + bounds.width / 2, zoom); |
| 193 | + int y = DPIUtil.pointToPixel(bounds.y + bounds.height / 2, zoom); |
| 194 | + OS.SendMessage(paintedTree.handle, OS.WM_MOUSEMOVE, 0, OS.MAKELPARAM(x, y)); |
| 195 | + } |
| 196 | + |
| 197 | + /** Returns the pixels of the hovered item's row, from top to bottom, in the middle of a column. */ |
| 198 | + private int[] captureColumn(int column) { |
| 199 | + return capture(COLUMN_WIDTH * column + COLUMN_WIDTH - 10); |
| 200 | + } |
| 201 | + |
| 202 | + private int[] capture(int x) { |
| 203 | + ImageData data = captureTree(); |
| 204 | + Rectangle bounds = paintedItem.getBounds(0); |
| 205 | + int[] pixels = new int[bounds.height]; |
| 206 | + for (int i = 0; i < pixels.length; i++) { |
| 207 | + pixels[i] = data.getPixel(x, bounds.y + i); |
| 208 | + } |
| 209 | + return pixels; |
| 210 | + } |
| 211 | + |
| 212 | + /** Returns the pixels across the whole row, just above the text. */ |
| 213 | + private int[] captureRowTop() { |
| 214 | + ImageData data = captureTree(); |
| 215 | + int[] pixels = new int[data.width]; |
| 216 | + for (int i = 0; i < pixels.length; i++) { |
| 217 | + pixels[i] = data.getPixel(i, paintedItem.getBounds(0).y + 1); |
| 218 | + } |
| 219 | + return pixels; |
| 220 | + } |
| 221 | + |
| 222 | + private ImageData captureTree() { |
| 223 | + // No events are dispatched here: the WM_MOUSELEAVE of the simulated hover would end it. |
| 224 | + paintedTree.redraw(); |
| 225 | + paintedTree.update(); |
| 226 | + Rectangle clientArea = paintedTree.getClientArea(); |
| 227 | + Image image = new Image(display, clientArea.width, clientArea.height); |
| 228 | + try { |
| 229 | + GC gc = new GC(paintedTree); |
| 230 | + try { |
| 231 | + gc.copyArea(image, 0, 0); |
| 232 | + } finally { |
| 233 | + gc.dispose(); |
| 234 | + } |
| 235 | + return image.getImageData(); |
| 236 | + } finally { |
| 237 | + image.dispose(); |
| 238 | + } |
| 239 | + } |
| 240 | +} |
0 commit comments