|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Vegard IT GmbH and others. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Sebastian Thomschke (Vegard IT GmbH) - initial implementation |
| 11 | + *******************************************************************************/ |
| 12 | +package org.eclipse.lsp4e.test.callhierarchy; |
| 13 | + |
| 14 | +import static org.eclipse.lsp4e.test.utils.TestUtils.waitForAndAssertCondition; |
| 15 | +import static org.junit.Assert.*; |
| 16 | + |
| 17 | +import org.eclipse.core.resources.IFile; |
| 18 | +import org.eclipse.core.resources.IProject; |
| 19 | +import org.eclipse.jface.text.IDocument; |
| 20 | +import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider; |
| 21 | +import org.eclipse.jface.viewers.TreeViewer; |
| 22 | +import org.eclipse.lsp4e.LSPEclipseUtils; |
| 23 | +import org.eclipse.lsp4e.callhierarchy.CallHierarchyContentProvider; |
| 24 | +import org.eclipse.lsp4e.callhierarchy.CallHierarchyLabelProvider; |
| 25 | +import org.eclipse.lsp4e.callhierarchy.CallHierarchyViewTreeNode; |
| 26 | +import org.eclipse.lsp4e.test.utils.AbstractTestWithProject; |
| 27 | +import org.eclipse.lsp4e.test.utils.TestUtils; |
| 28 | +import org.eclipse.lsp4e.tests.mock.MockLanguageServer; |
| 29 | +import org.eclipse.lsp4e.ui.views.HierarchyViewInput; |
| 30 | +import org.eclipse.lsp4j.ServerCapabilities; |
| 31 | +import org.eclipse.swt.layout.FillLayout; |
| 32 | +import org.eclipse.swt.widgets.Shell; |
| 33 | +import org.eclipse.swt.widgets.Tree; |
| 34 | +import org.eclipse.swt.widgets.TreeItem; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +/** |
| 38 | + * UI-level test that opens a file, initializes Call Hierarchy and verifies that |
| 39 | + * the view model contains expected nodes. Uses the mock LS. |
| 40 | + */ |
| 41 | +public class CallHierarchyViewContentTest extends AbstractTestWithProject { |
| 42 | + |
| 43 | + @Override |
| 44 | + public void setUpProject() throws Exception { |
| 45 | + super.setUpProject(); |
| 46 | + // Ensure the mock server advertises callHierarchyProvider |
| 47 | + MockLanguageServer.reset(() -> { |
| 48 | + ServerCapabilities caps = MockLanguageServer.defaultServerCapabilities(); |
| 49 | + caps.setCallHierarchyProvider(Boolean.TRUE); |
| 50 | + return caps; |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testCallHierarchyShowsCalleeAndCaller() throws Exception { |
| 56 | + IProject p = project; |
| 57 | + IFile file = TestUtils.createUniqueTestFile(p, "// mock content for call hierarchy\nfunction f(){}\n"); |
| 58 | + |
| 59 | + // Open the file in Generic Editor to bind the LS |
| 60 | + var editor = TestUtils.openEditor(file); |
| 61 | + IDocument document = LSPEclipseUtils.getDocument(editor.getEditorInput()); |
| 62 | + assertTrue(document != null); |
| 63 | + |
| 64 | + // Create a lightweight view embedding a TreeViewer with the real content |
| 65 | + // provider |
| 66 | + Shell shell = new Shell(editor.getSite().getShell()); |
| 67 | + shell.setLayout(new FillLayout()); |
| 68 | + TreeViewer viewer = new TreeViewer(shell); |
| 69 | + viewer.setContentProvider(new CallHierarchyContentProvider()); |
| 70 | + viewer.setLabelProvider(new DelegatingStyledCellLabelProvider(new CallHierarchyLabelProvider())); |
| 71 | + shell.open(); |
| 72 | + |
| 73 | + // Initialize input similar to CallHierarchyView.initialize |
| 74 | + viewer.setInput(new HierarchyViewInput(document, 0)); |
| 75 | + |
| 76 | + // Wait until the placeholder ("Finding callers ...") is replaced by a node |
| 77 | + waitForAndAssertCondition(5_000, shell.getDisplay(), () -> { |
| 78 | + Tree tree = viewer.getTree(); |
| 79 | + if (tree.getItemCount() == 0) return false; |
| 80 | + Object data = tree.getItem(0).getData(); |
| 81 | + return data instanceof CallHierarchyViewTreeNode; |
| 82 | + }); |
| 83 | + |
| 84 | + Tree tree = viewer.getTree(); |
| 85 | + TreeItem root = tree.getItem(0); |
| 86 | + Object rootData = root.getData(); |
| 87 | + assertTrue("Expected CallHierarchyViewTreeNode root", rootData instanceof CallHierarchyViewTreeNode); |
| 88 | + var rootNode = (CallHierarchyViewTreeNode) rootData; |
| 89 | + assertEquals("callee", rootNode.getCallContainer().getName()); |
| 90 | + |
| 91 | + // Expand and wait for children |
| 92 | + viewer.expandToLevel(2); |
| 93 | + waitForAndAssertCondition(5_000, shell.getDisplay(), () -> { |
| 94 | + return root.getItemCount() > 0 && root.getItem(0).getData() instanceof CallHierarchyViewTreeNode; |
| 95 | + }); |
| 96 | + Object childData = root.getItem(0).getData(); |
| 97 | + assertTrue(childData instanceof CallHierarchyViewTreeNode); |
| 98 | + var childNode = (CallHierarchyViewTreeNode) childData; |
| 99 | + assertEquals("caller", childNode.getCallContainer().getName()); |
| 100 | + |
| 101 | + shell.close(); |
| 102 | + } |
| 103 | +} |
0 commit comments