|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Vector Informatik 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 | +package org.eclipse.search.tests; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.AfterEach; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import org.eclipse.swt.SWT; |
| 23 | +import org.eclipse.swt.widgets.Display; |
| 24 | +import org.eclipse.swt.widgets.Shell; |
| 25 | + |
| 26 | +import org.eclipse.jface.viewers.ArrayContentProvider; |
| 27 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 28 | +import org.eclipse.jface.viewers.SelectionChangedEvent; |
| 29 | +import org.eclipse.jface.viewers.TableViewer; |
| 30 | +import org.eclipse.jface.viewers.TreeNode; |
| 31 | +import org.eclipse.jface.viewers.TreeNodeContentProvider; |
| 32 | +import org.eclipse.jface.viewers.TreeViewer; |
| 33 | + |
| 34 | +import org.eclipse.search.internal.ui.SelectAllAction; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for {@link SelectAllAction}. |
| 38 | + */ |
| 39 | +public class SelectAllActionTest { |
| 40 | + |
| 41 | + private Shell fShell; |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + public void setUp() { |
| 45 | + fShell= new Shell(Display.getDefault()); |
| 46 | + } |
| 47 | + |
| 48 | + @AfterEach |
| 49 | + public void tearDown() { |
| 50 | + if (fShell != null && !fShell.isDisposed()) { |
| 51 | + fShell.dispose(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testSelectAllWithTableViewer() { |
| 57 | + final SelectionChangedEvent[] event= new SelectionChangedEvent[1]; |
| 58 | + TableViewer tableViewer= new TableViewer(fShell, SWT.MULTI); |
| 59 | + tableViewer.setContentProvider(new ArrayContentProvider()); |
| 60 | + tableViewer.addSelectionChangedListener((e) -> { |
| 61 | + event[0]= e; |
| 62 | + }); |
| 63 | + Object[] elements= { "item1", "item2", "item3" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 64 | + tableViewer.setInput(elements); |
| 65 | + |
| 66 | + SelectAllAction action= new SelectAllAction(); |
| 67 | + action.setViewer(tableViewer); |
| 68 | + action.run(); |
| 69 | + |
| 70 | + IStructuredSelection selection= tableViewer.getStructuredSelection(); |
| 71 | + assertEquals(3, selection.size(), "All items should be selected"); //$NON-NLS-1$ |
| 72 | + assertNotNull(event[0], "SelectionChangedEvent should have been fired"); //$NON-NLS-1$ |
| 73 | + assertEquals(3, ((IStructuredSelection) event[0].getSelection()).size(), "Selection in event should include all items"); //$NON-NLS-1$ |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testSelectAllWithTreeViewerFlatList() { |
| 78 | + final SelectionChangedEvent[] event= new SelectionChangedEvent[1]; |
| 79 | + TreeViewer treeViewer= new TreeViewer(fShell, SWT.MULTI); |
| 80 | + treeViewer.setContentProvider(new TreeNodeContentProvider()); |
| 81 | + treeViewer.addSelectionChangedListener((e) -> { |
| 82 | + event[0]= e; |
| 83 | + }); |
| 84 | + |
| 85 | + final TreeNode[] nodes= new TreeNode[3]; |
| 86 | + nodes[0]= new TreeNode("node1"); //$NON-NLS-1$ |
| 87 | + nodes[1]= new TreeNode("node2"); //$NON-NLS-1$ |
| 88 | + nodes[2]= new TreeNode("node3"); //$NON-NLS-1$ |
| 89 | + treeViewer.setInput(nodes); |
| 90 | + |
| 91 | + SelectAllAction action= new SelectAllAction(); |
| 92 | + action.setViewer(treeViewer); |
| 93 | + action.run(); |
| 94 | + |
| 95 | + IStructuredSelection selection= treeViewer.getStructuredSelection(); |
| 96 | + assertEquals(3, selection.size(), "All items should be selected"); //$NON-NLS-1$ |
| 97 | + assertNotNull(event[0], "SelectionChangedEvent should have been fired"); //$NON-NLS-1$ |
| 98 | + assertEquals(3, ((IStructuredSelection) event[0].getSelection()).size(), "Selection in event should include all items"); //$NON-NLS-1$ |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testSelectAllWithNoViewer() { |
| 103 | + // Should not throw any exception when no viewer is set |
| 104 | + SelectAllAction action= new SelectAllAction(); |
| 105 | + assertDoesNotThrow(() -> action.run(), "Running SelectAllAction without a viewer should not throw an exception"); //$NON-NLS-1$ |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testSelectAllWithEmptyTableViewer() { |
| 110 | + final SelectionChangedEvent[] event= new SelectionChangedEvent[1]; |
| 111 | + TableViewer tableViewer= new TableViewer(fShell, SWT.MULTI); |
| 112 | + tableViewer.setContentProvider(new ArrayContentProvider()); |
| 113 | + tableViewer.setInput(new Object[0]); |
| 114 | + tableViewer.addSelectionChangedListener((e) -> { |
| 115 | + event[0]= e; |
| 116 | + }); |
| 117 | + |
| 118 | + SelectAllAction action= new SelectAllAction(); |
| 119 | + action.setViewer(tableViewer); |
| 120 | + action.run(); |
| 121 | + |
| 122 | + IStructuredSelection selection= tableViewer.getStructuredSelection(); |
| 123 | + assertTrue(selection.isEmpty(), "Selection should be empty for an empty table"); //$NON-NLS-1$ |
| 124 | + assertNotNull(event[0], "SelectionChangedEvent should have been fired"); //$NON-NLS-1$ |
| 125 | + assertTrue(event[0].getSelection().isEmpty(), "Selection in event should be empty for an empty table"); //$NON-NLS-1$ |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testSelectAllWithEmptyTreeViewer() { |
| 130 | + final SelectionChangedEvent[] event= new SelectionChangedEvent[1]; |
| 131 | + TreeViewer treeViewer= new TreeViewer(fShell, SWT.MULTI); |
| 132 | + treeViewer.setContentProvider(new TreeNodeContentProvider()); |
| 133 | + treeViewer.setInput(new TreeNode[0]); |
| 134 | + treeViewer.addSelectionChangedListener((e) -> { |
| 135 | + event[0]= e; |
| 136 | + }); |
| 137 | + |
| 138 | + SelectAllAction action= new SelectAllAction(); |
| 139 | + action.setViewer(treeViewer); |
| 140 | + action.run(); |
| 141 | + |
| 142 | + IStructuredSelection selection= treeViewer.getStructuredSelection(); |
| 143 | + assertTrue(selection.isEmpty(), "Selection should be empty for an empty tree"); //$NON-NLS-1$ |
| 144 | + assertNotNull(event[0], "SelectionChangedEvent should have been fired"); //$NON-NLS-1$ |
| 145 | + assertTrue(event[0].getSelection().isEmpty(), "Selection in event should be empty for an empty tree"); //$NON-NLS-1$ |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testSelectAllWithTreeViewerOnlySelectsExpandedItems() { |
| 150 | + final SelectionChangedEvent[] event= new SelectionChangedEvent[1]; |
| 151 | + TreeViewer treeViewer= new TreeViewer(fShell, SWT.MULTI); |
| 152 | + treeViewer.setContentProvider(new TreeNodeContentProvider()); |
| 153 | + treeViewer.addSelectionChangedListener((e) -> { |
| 154 | + event[0]= e; |
| 155 | + }); |
| 156 | + |
| 157 | + final TreeNode[] nodes= new TreeNode[2]; |
| 158 | + TreeNode parent= new TreeNode("parent"); //$NON-NLS-1$ |
| 159 | + parent.setChildren(new TreeNode[] { new TreeNode("child1"), new TreeNode("child2") }); //$NON-NLS-1$ //$NON-NLS-2$ |
| 160 | + nodes[0]= parent; |
| 161 | + nodes[1]= new TreeNode("sibling"); //$NON-NLS-1$ |
| 162 | + treeViewer.setInput(nodes); |
| 163 | + |
| 164 | + // collapse all (default state) - children not expanded |
| 165 | + SelectAllAction action= new SelectAllAction(); |
| 166 | + action.setViewer(treeViewer); |
| 167 | + action.run(); |
| 168 | + |
| 169 | + IStructuredSelection collapsedSelection= treeViewer.getStructuredSelection(); |
| 170 | + assertEquals(2, collapsedSelection.size(), |
| 171 | + "Only top-level items should be selected when tree is collapsed"); //$NON-NLS-1$ |
| 172 | + assertNotNull(event[0], "SelectionChangedEvent should have been fired"); //$NON-NLS-1$ |
| 173 | + assertEquals(2, ((IStructuredSelection) event[0].getSelection()).size(), "Selection in event should include only top-level items when tree is collapsed"); //$NON-NLS-1$ |
| 174 | + |
| 175 | + // expand "parent" and re-run select all |
| 176 | + treeViewer.expandToLevel(parent, 1); |
| 177 | + action.run(); |
| 178 | + |
| 179 | + IStructuredSelection expandedSelection= treeViewer.getStructuredSelection(); |
| 180 | + assertEquals(4, expandedSelection.size(), |
| 181 | + "Top-level items plus expanded children should be selected"); //$NON-NLS-1$ |
| 182 | + assertEquals(4, ((IStructuredSelection) event[0].getSelection()).size(), "Selection in event should include top-level items plus expanded children when tree is expanded"); //$NON-NLS-1$ |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | +} |
0 commit comments