1 /*******************************************************************************
2  * Copyright (c) 2019 Red Hat Inc. 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 package org.eclipse.jface.text.tests.source.inlined;
12 
13 import java.util.Collections;
14 
15 import org.junit.After;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.StyledText;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.swt.layout.FillLayout;
24 import org.eclipse.swt.widgets.Shell;
25 
26 import org.eclipse.jface.resource.JFaceResources;
27 
28 import org.eclipse.jface.text.Document;
29 import org.eclipse.jface.text.ITextViewerExtension2;
30 import org.eclipse.jface.text.Position;
31 import org.eclipse.jface.text.source.AnnotationModel;
32 import org.eclipse.jface.text.source.IAnnotationAccess;
33 import org.eclipse.jface.text.source.ISourceViewer;
34 import org.eclipse.jface.text.source.SourceViewer;
35 import org.eclipse.jface.text.source.inlined.InlinedAnnotationSupport;
36 import org.eclipse.jface.text.source.inlined.LineContentAnnotation;
37 import org.eclipse.jface.text.tests.source.inlined.LineContentBoundsDrawingTest.AccessAllAnnoations;
38 import org.eclipse.jface.text.tests.source.inlined.LineContentBoundsDrawingTest.TestAnnotationPainter;
39 import org.eclipse.jface.text.tests.util.DisplayHelper;
40 
41 public class AnnotationOnTabTest {
42 
43 	private Shell fParent;
44 
45 	@Before
setUp()46 	public void setUp() {
47 		fParent= new Shell();
48 	}
49 
50 	@After
tearDown()51 	public void tearDown() {
52 		fParent.dispose();
53 		fParent = null;
54 	}
55 
56 	@Test
testTextBoundsMatchPaintedArea()57 	public void testTextBoundsMatchPaintedArea() {
58 		fParent.setLayout(new FillLayout());
59 
60 		// Create source viewer and initialize the content
61 		ISourceViewer sourceViewer = new SourceViewer(fParent,null,
62 				SWT.V_SCROLL | SWT.BORDER);
63 		sourceViewer.setDocument(new Document("\t\treference\n\t\tannotated"), new AnnotationModel());
64 		StyledText textWidget= sourceViewer.getTextWidget();
65 		textWidget.setFont(JFaceResources.getTextFont());
66 
67 		// Initialize inlined annotations support
68 		InlinedAnnotationSupport support = new InlinedAnnotationSupport();
69 		IAnnotationAccess annotationAccess = new AccessAllAnnoations();
70 		TestAnnotationPainter painter = new TestAnnotationPainter(sourceViewer, annotationAccess);
71 		((ITextViewerExtension2) sourceViewer).addPainter(painter);
72 		support.install(sourceViewer, painter);
73 
74 		// add annotations
75 		int annotationIndex = sourceViewer.getDocument().get().indexOf("annotated");
76 		LineContentAnnotation annotation= new LineContentAnnotation(new Position(annotationIndex, 1), sourceViewer);
77 		annotation.setText("a"); // single char, so overall annoation is 3 chars, less than default 4 chars
78 		support.updateAnnotations(Collections.singleton(annotation));
79 		fParent.open();
80 		Assert.assertTrue(new DisplayHelper() {
81 			@Override
82 			protected boolean condition() {
83 				return textWidget.isVisible() && painter.wasPainted();
84 			}
85 		}.waitForCondition(textWidget.getDisplay(), 2000));
86 		DisplayHelper.sleep(textWidget.getDisplay(), 1000);
87 		int referenceIndex = textWidget.getText().indexOf("reference");
88 		Rectangle referenceBounds = textWidget.getTextBounds(referenceIndex, referenceIndex);
89 		Rectangle annotatedCharactedBounds = textWidget.getTextBounds(annotationIndex, annotationIndex);
90 		Assert.assertTrue("Annotation didn't shift target character to the right, it most likely replaced the tab instead of expanding it", referenceBounds.x < annotatedCharactedBounds.x);
91 	}
92 }
93