|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 SAP SE |
| 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 | +package org.eclipse.jface.text.tests.codemining; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertNotEquals; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | + |
| 17 | +import org.junit.After; |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import org.eclipse.swt.SWT; |
| 23 | +import org.eclipse.swt.custom.StyledText; |
| 24 | +import org.eclipse.swt.layout.FillLayout; |
| 25 | +import org.eclipse.swt.widgets.Display; |
| 26 | +import org.eclipse.swt.widgets.Shell; |
| 27 | + |
| 28 | +import org.eclipse.jface.internal.text.codemining.CodeMiningLineHeaderAnnotation; |
| 29 | + |
| 30 | +import org.eclipse.jface.text.Document; |
| 31 | +import org.eclipse.jface.text.Position; |
| 32 | +import org.eclipse.jface.text.codemining.LineHeaderCodeMining; |
| 33 | +import org.eclipse.jface.text.source.AnnotationModel; |
| 34 | +import org.eclipse.jface.text.source.AnnotationPainter; |
| 35 | +import org.eclipse.jface.text.source.SourceViewer; |
| 36 | +import org.eclipse.jface.text.source.inlined.AbstractInlinedAnnotation; |
| 37 | +import org.eclipse.jface.text.source.inlined.InlinedAnnotationSupport; |
| 38 | +import org.eclipse.jface.text.tests.util.DisplayHelper; |
| 39 | + |
| 40 | +public class CodeMiningLineHeaderAnnotationTest { |
| 41 | + |
| 42 | + private SourceViewer fViewer; |
| 43 | + |
| 44 | + private Shell fShell; |
| 45 | + |
| 46 | + private Document document; |
| 47 | + |
| 48 | + @Before |
| 49 | + public void setUp() { |
| 50 | + fShell= new Shell(Display.getDefault()); |
| 51 | + fShell.setSize(500, 200); |
| 52 | + fShell.setLayout(new FillLayout()); |
| 53 | + fViewer= new SourceViewer(fShell, null, SWT.NONE); |
| 54 | + final StyledText textWidget= fViewer.getTextWidget(); |
| 55 | + document= new Document("a"); |
| 56 | + textWidget.setText(document.get()); |
| 57 | + fViewer.setDocument(document, new AnnotationModel()); |
| 58 | + final Display display= textWidget.getDisplay(); |
| 59 | + fShell.open(); |
| 60 | + Assert.assertTrue(new DisplayHelper() { |
| 61 | + @Override |
| 62 | + protected boolean condition() { |
| 63 | + return fViewer.getTextWidget().isVisible(); |
| 64 | + } |
| 65 | + }.waitForCondition(display, 3000)); |
| 66 | + DisplayHelper.sleep(textWidget.getDisplay(), 1000); |
| 67 | + } |
| 68 | + |
| 69 | + @After |
| 70 | + public void tearDown() { |
| 71 | + fViewer= null; |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testGetHeightDoesNotReturnZero() throws Exception { |
| 76 | + var cut= new CodeMiningLineHeaderAnnotation(new Position(0, 0), fViewer); |
| 77 | + var s= new InlinedAnnotationSupport(); |
| 78 | + s.install(fViewer, new AnnotationPainter(fViewer, null)); |
| 79 | + var m= AbstractInlinedAnnotation.class.getDeclaredMethod("setSupport", InlinedAnnotationSupport.class); |
| 80 | + m.setAccessible(true); |
| 81 | + m.invoke(cut, s); |
| 82 | + cut.update(Arrays.asList(new LineHeaderCodeMining(0, document, null) { |
| 83 | + @Override |
| 84 | + public String getLabel() { |
| 85 | + return "mining"; |
| 86 | + } |
| 87 | + }), null); |
| 88 | + // https: //github.com/eclipse-platform/eclipse.platform.ui/issues/2786 |
| 89 | + assertNotEquals(0, cut.getHeight()); // getHeight should not return 0, otherwise editor content starts jumping around |
| 90 | + } |
| 91 | +} |
0 commit comments