|
| 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 | + * Lars Vogel - initial implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.pde.genericeditor.extension.tests; |
| 15 | + |
| 16 | +import static org.junit.Assert.assertNotNull; |
| 17 | + |
| 18 | +import org.eclipse.jface.text.Document; |
| 19 | +import org.eclipse.jface.text.IDocument; |
| 20 | +import org.eclipse.jface.text.IRegion; |
| 21 | +import org.eclipse.jface.text.Region; |
| 22 | +import org.eclipse.jface.text.TextPresentation; |
| 23 | +import org.eclipse.jface.text.presentation.IPresentationRepairer; |
| 24 | +import org.eclipse.pde.internal.genericeditor.target.extension.reconciler.presentation.TargetPlatformPresentationReconciler; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +/** |
| 28 | + * Tests for {@link TargetPlatformPresentationReconciler}. |
| 29 | + * |
| 30 | + * <p>The reconciler overrides {@code createPresentation} to scan from offset 0 |
| 31 | + * so that multi-line rules (XML comments) are correctly recognised even when |
| 32 | + * the damaged region is deep inside the document. These tests verify that the |
| 33 | + * presentation is computed correctly for several representative document shapes |
| 34 | + * and damage positions. |
| 35 | + */ |
| 36 | +public class TargetPlatformPresentationReconcilerTest { |
| 37 | + |
| 38 | + /** Subclass that exposes the protected {@code createPresentation} for testing. */ |
| 39 | + private static class TestableReconciler extends TargetPlatformPresentationReconciler { |
| 40 | + TextPresentation createPresentation(IDocument document, IRegion damage) { |
| 41 | + IPresentationRepairer repairer = getRepairer(IDocument.DEFAULT_CONTENT_TYPE); |
| 42 | + if (repairer != null) { |
| 43 | + repairer.setDocument(document); |
| 44 | + } |
| 45 | + return createPresentation(damage, document); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testCreatePresentationReturnsNonNullForSimpleDocument() { |
| 51 | + TestableReconciler reconciler = new TestableReconciler(); |
| 52 | + String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //$NON-NLS-1$ |
| 53 | + + "<target name=\"test\">\n" //$NON-NLS-1$ |
| 54 | + + " <locations/>\n" //$NON-NLS-1$ |
| 55 | + + "</target>\n"; //$NON-NLS-1$ |
| 56 | + IDocument document = new Document(content); |
| 57 | + IRegion damage = new Region(0, content.length()); |
| 58 | + |
| 59 | + TextPresentation presentation = reconciler.createPresentation(document, damage); |
| 60 | + |
| 61 | + assertNotNull("Expected a non-null TextPresentation for a simple document", presentation); //$NON-NLS-1$ |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testCreatePresentationWithDamageSubsetOfDocument() { |
| 66 | + TestableReconciler reconciler = new TestableReconciler(); |
| 67 | + String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //$NON-NLS-1$ |
| 68 | + + "<target name=\"test\">\n" //$NON-NLS-1$ |
| 69 | + + " <locations>\n" //$NON-NLS-1$ |
| 70 | + + " </locations>\n" //$NON-NLS-1$ |
| 71 | + + "</target>\n"; //$NON-NLS-1$ |
| 72 | + IDocument document = new Document(content); |
| 73 | + // Damage is only the last few characters, not the full document. |
| 74 | + int damageOffset = content.indexOf("</target>"); //$NON-NLS-1$ |
| 75 | + IRegion damage = new Region(damageOffset, content.length() - damageOffset); |
| 76 | + |
| 77 | + TextPresentation presentation = reconciler.createPresentation(document, damage); |
| 78 | + |
| 79 | + assertNotNull("Expected a non-null TextPresentation when damage covers only end of document", presentation); //$NON-NLS-1$ |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testCreatePresentationWithMultilineCommentBeforeDamage() { |
| 84 | + TestableReconciler reconciler = new TestableReconciler(); |
| 85 | + // Multi-line comment appears before the damage region. The reconciler must |
| 86 | + // scan from offset 0 so it can correctly determine the scanner state |
| 87 | + // (comment vs normal) at the start of the damage. |
| 88 | + String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //$NON-NLS-1$ |
| 89 | + + "<target name=\"test\">\n" //$NON-NLS-1$ |
| 90 | + + "<!--\n" //$NON-NLS-1$ |
| 91 | + + " multi-line comment\n" //$NON-NLS-1$ |
| 92 | + + "-->\n" //$NON-NLS-1$ |
| 93 | + + " <locations/>\n" //$NON-NLS-1$ |
| 94 | + + "</target>\n"; //$NON-NLS-1$ |
| 95 | + IDocument document = new Document(content); |
| 96 | + // Damage is after the comment closes. |
| 97 | + int damageOffset = content.indexOf("<locations/>"); //$NON-NLS-1$ |
| 98 | + IRegion damage = new Region(damageOffset, content.length() - damageOffset); |
| 99 | + |
| 100 | + TextPresentation presentation = reconciler.createPresentation(document, damage); |
| 101 | + |
| 102 | + assertNotNull("Expected a non-null TextPresentation when a multi-line comment precedes the damage", //$NON-NLS-1$ |
| 103 | + presentation); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testCreatePresentationWithDamageInsideMultilineComment() { |
| 108 | + TestableReconciler reconciler = new TestableReconciler(); |
| 109 | + String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //$NON-NLS-1$ |
| 110 | + + "<target name=\"test\">\n" //$NON-NLS-1$ |
| 111 | + + "<!--\n" //$NON-NLS-1$ |
| 112 | + + " line one\n" //$NON-NLS-1$ |
| 113 | + + " line two\n" //$NON-NLS-1$ |
| 114 | + + "-->\n" //$NON-NLS-1$ |
| 115 | + + "</target>\n"; //$NON-NLS-1$ |
| 116 | + IDocument document = new Document(content); |
| 117 | + // Damage is inside the comment body. |
| 118 | + int damageOffset = content.indexOf(" line two"); //$NON-NLS-1$ |
| 119 | + IRegion damage = new Region(damageOffset, " line two\n".length()); //$NON-NLS-1$ |
| 120 | + |
| 121 | + TextPresentation presentation = reconciler.createPresentation(document, damage); |
| 122 | + |
| 123 | + assertNotNull("Expected a non-null TextPresentation when damage is inside a multi-line comment", presentation); //$NON-NLS-1$ |
| 124 | + } |
| 125 | +} |
0 commit comments