|
| 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.ui.tests.navigator; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +import org.eclipse.core.resources.IFile; |
| 23 | +import org.eclipse.core.resources.IResource; |
| 24 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 25 | +import org.eclipse.core.runtime.CoreException; |
| 26 | +import org.eclipse.jface.action.ActionContributionItem; |
| 27 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 28 | +import org.eclipse.jface.viewers.StructuredSelection; |
| 29 | +import org.eclipse.swt.dnd.Clipboard; |
| 30 | +import org.eclipse.swt.dnd.TextTransfer; |
| 31 | +import org.eclipse.swt.dnd.Transfer; |
| 32 | +import org.eclipse.swt.widgets.Display; |
| 33 | +import org.eclipse.ui.navigator.resources.ProjectExplorer; |
| 34 | +import org.eclipse.ui.part.ResourceTransfer; |
| 35 | +import org.junit.jupiter.api.AfterEach; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests for Copy and Paste actions in the Project Explorer. |
| 40 | + */ |
| 41 | +public class CopyPasteActionTest extends NavigatorTestBase { |
| 42 | + |
| 43 | + private Clipboard _clipboard; |
| 44 | + |
| 45 | + public CopyPasteActionTest() { |
| 46 | + _navigatorInstanceId = ProjectExplorer.VIEW_ID; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + @AfterEach |
| 51 | + public void tearDown() throws CoreException { |
| 52 | + if (_clipboard != null) { |
| 53 | + _clipboard.dispose(); |
| 54 | + _clipboard = null; |
| 55 | + } |
| 56 | + super.tearDown(); |
| 57 | + } |
| 58 | + |
| 59 | + private Clipboard getClipboard() { |
| 60 | + if (_clipboard == null) { |
| 61 | + _clipboard = new Clipboard(Display.getDefault()); |
| 62 | + } |
| 63 | + return _clipboard; |
| 64 | + } |
| 65 | + |
| 66 | + private ActionContributionItem getAction(IStructuredSelection sel, String label) { |
| 67 | + Object item = verifyMenu(sel, label); |
| 68 | + assertNotNull(item, label + " action not found in context menu"); |
| 69 | + assertTrue(item instanceof ActionContributionItem, label + " item should be an ActionContributionItem"); |
| 70 | + return (ActionContributionItem) item; |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testCopyEnablement() throws Exception { |
| 75 | + IFile file = _project.getFile("model.properties"); |
| 76 | + IStructuredSelection sel = new StructuredSelection(file); |
| 77 | + _viewer.setSelection(sel); |
| 78 | + |
| 79 | + ActionContributionItem copyActionItem = getAction(sel, "Copy"); |
| 80 | + assertTrue(copyActionItem.getAction().isEnabled(), "Copy action should be enabled for a file"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testCopyDisabledForMixedSelection() throws Exception { |
| 85 | + // Mixed selection (Project + File) should disable Copy action |
| 86 | + IFile file = _project.getFile("model.properties"); |
| 87 | + IStructuredSelection sel = new StructuredSelection(new Object[] { _project, file }); |
| 88 | + _viewer.setSelection(sel); |
| 89 | + |
| 90 | + ActionContributionItem copyActionItem = getAction(sel, "Copy"); |
| 91 | + assertFalse(copyActionItem.getAction().isEnabled(), "Copy action should be disabled for mixed selection"); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testCopyDisabledForEmptySelection() throws Exception { |
| 96 | + IStructuredSelection sel = StructuredSelection.EMPTY; |
| 97 | + _viewer.setSelection(sel); |
| 98 | + |
| 99 | + Object copyItem = verifyMenu(sel, "Copy"); |
| 100 | + // For an empty selection, the Resource action provider is not even matched, |
| 101 | + // so the action should be absent from the menu. |
| 102 | + assertNull(copyItem, "Copy action should be absent for empty selection"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testCopyToClipboard() throws Exception { |
| 107 | + IFile file = _project.getFile("model.properties"); |
| 108 | + IStructuredSelection sel = new StructuredSelection(file); |
| 109 | + _viewer.setSelection(sel); |
| 110 | + |
| 111 | + ActionContributionItem copyActionItem = getAction(sel, "Copy"); |
| 112 | + copyActionItem.getAction().run(); |
| 113 | + |
| 114 | + Object contents = getClipboard().getContents(ResourceTransfer.getInstance()); |
| 115 | + assertNotNull(contents, "Clipboard should contain resources"); |
| 116 | + IResource[] resources = (IResource[]) contents; |
| 117 | + assertEquals(1, resources.length); |
| 118 | + assertEquals(file, resources[0]); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testCopyTextTransfer() throws Exception { |
| 123 | + IFile file = _project.getFile("model.properties"); |
| 124 | + IStructuredSelection sel = new StructuredSelection(file); |
| 125 | + _viewer.setSelection(sel); |
| 126 | + |
| 127 | + ActionContributionItem copyActionItem = getAction(sel, "Copy"); |
| 128 | + copyActionItem.getAction().run(); |
| 129 | + |
| 130 | + String textContents = (String) getClipboard().getContents(TextTransfer.getInstance()); |
| 131 | + assertNotNull(textContents, "Clipboard should contain text"); |
| 132 | + assertEquals(file.getName(), textContents.trim()); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testPasteEnablement() throws Exception { |
| 137 | + IFile file = _project.getFile("model.properties"); |
| 138 | + |
| 139 | + // Manually put the file on the clipboard |
| 140 | + getClipboard().setContents(new Object[] { new IResource[] { file }, file.getName() }, |
| 141 | + new Transfer[] { ResourceTransfer.getInstance(), TextTransfer.getInstance() }); |
| 142 | + |
| 143 | + IStructuredSelection sel = new StructuredSelection(_p1); |
| 144 | + _viewer.setSelection(sel); |
| 145 | + |
| 146 | + ActionContributionItem pasteActionItem = getAction(sel, "Paste"); |
| 147 | + assertTrue(pasteActionItem.getAction().isEnabled(), "Paste action should be enabled when clipboard has resources"); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void testCopyPasteRoundTrip() throws Exception { |
| 152 | + IFile file = _project.getFile("model.properties"); |
| 153 | + assertTrue(file.exists()); |
| 154 | + |
| 155 | + // 1. Copy |
| 156 | + IStructuredSelection selCopy = new StructuredSelection(file); |
| 157 | + _viewer.setSelection(selCopy); |
| 158 | + ActionContributionItem copyActionItem = getAction(selCopy, "Copy"); |
| 159 | + copyActionItem.getAction().run(); |
| 160 | + |
| 161 | + // 2. Paste into _p1 |
| 162 | + IStructuredSelection selPaste = new StructuredSelection(_p1); |
| 163 | + _viewer.setSelection(selPaste); |
| 164 | + ActionContributionItem pasteActionItem = getAction(selPaste, "Paste"); |
| 165 | + assertTrue(pasteActionItem.getAction().isEnabled()); |
| 166 | + pasteActionItem.getAction().run(); |
| 167 | + |
| 168 | + // 3. Verify |
| 169 | + IFile pastedFile = _p1.getFile(file.getName()); |
| 170 | + waitForCondition("File should be pasted", () -> pastedFile.exists()); |
| 171 | + assertTrue(pastedFile.exists(), "Pasted file should exist in target project"); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void testContextMenuContainsCopyPasteDelete() throws Exception { |
| 176 | + IFile file = _project.getFile("model.properties"); |
| 177 | + IStructuredSelection sel = new StructuredSelection(file); |
| 178 | + _viewer.setSelection(sel); |
| 179 | + |
| 180 | + getAction(sel, "Copy"); |
| 181 | + getAction(sel, "Paste"); |
| 182 | + getAction(sel, "Delete"); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void testCopyDisabledForWorkspaceRoot() throws Exception { |
| 187 | + // Selecting the workspace root should enable the resource action provider |
| 188 | + // but the Copy action itself should be disabled for the root. |
| 189 | + IStructuredSelection sel = new StructuredSelection(ResourcesPlugin.getWorkspace().getRoot()); |
| 190 | + _viewer.setSelection(sel); |
| 191 | + |
| 192 | + ActionContributionItem copyActionItem = getAction(sel, "Copy"); |
| 193 | + assertFalse(copyActionItem.getAction().isEnabled(), "Copy action should be disabled for Workspace Root"); |
| 194 | + } |
| 195 | +} |
0 commit comments