-
Notifications
You must be signed in to change notification settings - Fork 86
/
WorkingSetDropAdapterTest.java
253 lines (212 loc) · 9.49 KB
/
WorkingSetDropAdapterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*******************************************************************************
* Copyright (c) 2000, 2020 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ui.tests.packageview;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.eclipse.jdt.testplugin.JavaProjectHelper;
import org.eclipse.swt.dnd.DND;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.resources.IFolder;
import org.eclipse.text.tests.Accessor;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.IWorkingSetManager;
import org.eclipse.ui.PlatformUI;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jdt.internal.ui.dnd.JdtViewerDropAdapter;
import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
import org.eclipse.jdt.internal.ui.packageview.WorkingSetDropAdapter;
import org.eclipse.jdt.internal.ui.workingsets.IWorkingSetIDs;
import org.eclipse.jdt.internal.ui.workingsets.WorkingSetModel;
public class WorkingSetDropAdapterTest {
private IJavaProject fProject;
private PackageExplorerPart fPackageExplorer;
private Accessor fPackageExplorerPartAccessor;
private WorkingSetDropAdapter fAdapter;
@Before
public void setUp() throws Exception {
fProject= JavaProjectHelper.createJavaProject("Test", "bin");
IWorkbenchPage activePage= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
fPackageExplorer= (PackageExplorerPart)activePage.showView(JavaUI.ID_PACKAGES);
fPackageExplorerPartAccessor= new Accessor(fPackageExplorer, PackageExplorerPart.class.getName(), getClass().getClassLoader());
fAdapter= new WorkingSetDropAdapter(fPackageExplorer);
}
@After
public void tearDown() throws Exception {
JavaProjectHelper.delete(fProject);
IWorkbenchPage activePage= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
activePage.hideView(fPackageExplorer);
assertTrue(fPackageExplorer.getTreeViewer().getTree().isDisposed());
}
@Test
public void testInvalidTarget2() throws Exception {
ITreeSelection selection= createSelection(fProject, null);
performDnD(DND.DROP_NONE, selection, fProject);
}
@Test
public void testInvalidSource1() throws Exception {
IPackageFragmentRoot root= JavaProjectHelper.addSourceContainer(fProject, "src");
ITreeSelection selection= createSelection(root, null);
IWorkingSet target= PlatformUI.getWorkbench().getWorkingSetManager().createWorkingSet(
"Target", new IAdaptable[] {fProject});
performDnD(DND.DROP_NONE, selection, target);
}
@Test
public void testInvalidSource2() throws Exception {
JavaProjectHelper.addSourceContainer(fProject, "src");
IFolder folder= fProject.getProject().getFolder("folder");
folder.create(true, true, null);
ITreeSelection selection= createSelection(folder, null);
IWorkingSet target= PlatformUI.getWorkbench().getWorkingSetManager().createWorkingSet(
"Target", new IAdaptable[] {fProject});
performDnD(DND.DROP_NONE, selection, target);
}
@Test
public void testAddProject() throws Exception {
ITreeSelection selection= createSelection(fProject, null);
IWorkingSet target= PlatformUI.getWorkbench().getWorkingSetManager().createWorkingSet(
"Target", new IAdaptable[0]);
performDnD(DND.DROP_COPY, selection, target);
IAdaptable[] elements= target.getElements();
assertEquals(1, elements.length);
assertEquals(fProject, elements[0]);
}
@Test
public void testMoveProject() throws Exception {
IWorkingSet source= PlatformUI.getWorkbench().getWorkingSetManager().createWorkingSet(
"Source", new IAdaptable[] {fProject});
List<TreePath> treePathes= new ArrayList<>();
treePathes.add(new TreePath(new Object[] {source, fProject}));
ITreeSelection selection= createSelection(fProject, treePathes);
IWorkingSet target= PlatformUI.getWorkbench().getWorkingSetManager().createWorkingSet(
"Target", new IAdaptable[0]);
performDnD(DND.DROP_MOVE, selection, target);
IAdaptable[] elements= target.getElements();
assertEquals(1, elements.length);
assertEquals(fProject, elements[0]);
elements= source.getElements();
assertEquals(0, elements.length);
}
@Test
public void testMoveToOthersProject() throws Exception {
IWorkingSet source= PlatformUI.getWorkbench().getWorkingSetManager().createWorkingSet(
"Source", new IAdaptable[] {fProject});
List<TreePath> treePathes= new ArrayList<>();
treePathes.add(new TreePath(new Object[] {source, fProject}));
ITreeSelection selection= createSelection(fProject, treePathes);
IWorkingSet target= PlatformUI.getWorkbench().getWorkingSetManager().createWorkingSet(
"Target", new IAdaptable[0]);
target.setId(IWorkingSetIDs.OTHERS);
performDnD(DND.DROP_MOVE, selection, target);
IAdaptable[] elements= target.getElements();
// assert that the target doesn't have an element yet. The others working set
// is updated by the updater through a change of the source working set
assertEquals(0, elements.length);
elements= source.getElements();
assertEquals(0, elements.length);
}
@Test
public void testRearrange1() throws Exception {
IWorkingSet workingSets[]= createJavaWorkingSets(new String[] { "ws1", "ws2", "ws3" });
setWorkingSets(workingSets);
ITreeSelection selection= createSelection(workingSets[2], null);
performDnD(DND.DROP_MOVE, selection, workingSets[0], JdtViewerDropAdapter.LOCATION_BEFORE);
IWorkingSet[] actual= fPackageExplorer.getWorkingSetModel().getActiveWorkingSets();
assertEquals(workingSets[2], actual[0]);
assertEquals(workingSets[0], actual[1]);
assertEquals(workingSets[1], actual[2]);
}
private static IWorkingSet[] createJavaWorkingSets(String[] names) {
IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager();
for (String name : names) {
IWorkingSet workingSet= workingSetManager.getWorkingSet(name);
if (workingSet != null)
workingSetManager.removeWorkingSet(workingSet);
}
IWorkingSet[] sets= new IWorkingSet[names.length];
for (int i= 0; i < names.length; i++) {
IWorkingSet workingSet= workingSetManager.createWorkingSet(names[i], new IAdaptable[0]);
workingSet.setId(IWorkingSetIDs.JAVA);
workingSetManager.addWorkingSet(workingSet);
sets[i]= workingSet;
}
return sets;
}
@Test
public void testRearrange2() throws Exception {
IWorkingSet workingSets[]= createJavaWorkingSets(new String[] { "ws1", "ws2", "ws3" });
setWorkingSets(workingSets);
ITreeSelection selection= createSelection(workingSets[2], null);
performDnD(DND.DROP_MOVE, selection, workingSets[0], JdtViewerDropAdapter.LOCATION_AFTER);
IWorkingSet[] actual= fPackageExplorer.getWorkingSetModel().getActiveWorkingSets();
assertEquals(workingSets[0], actual[0]);
assertEquals(workingSets[2], actual[1]);
assertEquals(workingSets[1], actual[2]);
}
@Test
public void testRearrange3() throws Exception {
IWorkingSet workingSets[]= createJavaWorkingSets(new String[] { "ws1", "ws2", "ws3" });
setWorkingSets(workingSets);
ITreeSelection selection= createSelection(workingSets[0], null);
performDnD(DND.DROP_MOVE, selection, workingSets[2], JdtViewerDropAdapter.LOCATION_AFTER);
IWorkingSet[] actual= fPackageExplorer.getWorkingSetModel().getActiveWorkingSets();
assertEquals(workingSets[1], actual[0]);
assertEquals(workingSets[2], actual[1]);
assertEquals(workingSets[0], actual[2]);
}
private void setWorkingSets(IWorkingSet[] workingSets) {
WorkingSetModel model= fPackageExplorer.getWorkingSetModel();
if (model == null) {
fPackageExplorerPartAccessor.invoke("createWorkingSetModel", null);
model= fPackageExplorer.getWorkingSetModel();
}
model.setActiveWorkingSets(workingSets);
model.configured();
fPackageExplorer.rootModeChanged(PackageExplorerPart.WORKING_SETS_AS_ROOTS);
}
private ITreeSelection createSelection(Object selectedElement, List<TreePath> treePathes) {
if (treePathes == null) {
treePathes= new ArrayList<>();
treePathes.add(new TreePath(new Object[] { selectedElement }));
}
return new TreeSelection(treePathes.toArray(new TreePath[treePathes.size()]),
fPackageExplorer.getTreeViewer().getComparer());
}
private void performDnD(int validateResult, ITreeSelection selection, Object target) throws Exception {
performDnD(validateResult, selection, target, DND.FEEDBACK_SELECT);
}
private void performDnD(int validateResult, ITreeSelection selection, Object target, int location) throws Exception {
try {
LocalSelectionTransfer.getTransfer().setSelection(selection);
fAdapter.internalTestSetLocation(location);
int result= fAdapter.internalTestValidateTarget(target, DND.DROP_DEFAULT);
assertEquals(validateResult, result);
if (validateResult != DND.DROP_NONE)
fAdapter.internalTestDrop(result);
} finally {
LocalSelectionTransfer.getTransfer().setSelection(null);
}
}
}