Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bfc2c0e4eaae6c47ac21062d048200709e69c52a (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2007 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.team.tests.ccvs.ui;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.tests.ccvs.core.EclipseTest;
import org.eclipse.ui.IActionDelegate;

public class EnablementTest extends EclipseTest {
	
	/**
	 * Constructor for CVSProviderTest
	 */
	public EnablementTest() {
		super();
	}

	/**
	 * Constructor for CVSProviderTest
	 */
	public EnablementTest(String name) {
		super(name);
	}
	
	/**
	 * Create a test project for the given action delegate. The structure of
	 * this test project is used by the get resource methods to return resources
	 * of the proper type.
	 * 
	 * @param actionDelegate
	 * @throws CoreException
	 * @throws TeamException
	 */
	protected IProject createTestProject(IActionDelegate actionDelegate) throws CoreException, TeamException {
		String actionName = getName(actionDelegate);
		return createProject(actionName, new String[] { "file.txt", "folder1/", "folder1/a.txt" });
	}
	
	protected List getManagedResources(IProject testProject, boolean includeFolders, boolean multiple) {
		List result = new ArrayList();
		if (includeFolders) {
			result.add(testProject.getFolder("folder1"));
		} else {
			result.add(testProject.getFile("folder1/a.txt"));
		}
		if (multiple) {
			result.add(testProject.getFile("file.txt"));
		}
		return result;
	}
	
	protected List getAddedResources(IProject testProject) throws CoreException, TeamException {
		List result = new ArrayList();
		IFile file = testProject.getFile("added.txt");
		if (!file.exists()) {
			addResources(testProject, new String[] {"added.txt"}, false);
		}
		result.add(file);
		return result;
	}
	
	protected List getIgnoredResources(IProject testProject) throws CoreException, TeamException {
		List result = new ArrayList();
		IFile file = testProject.getFile("ignored.txt");
		if (!file.exists()) {
			file.create(getRandomContents(), false, null);
		}
		result.add(file);
		IFile ignoreFile = testProject.getFile(".cvsignore");
		InputStream contents = new ByteArrayInputStream("ignored.txt".getBytes());
		if (ignoreFile.exists()) {
			ignoreFile.setContents(contents, false, false, null);
		} else {
			ignoreFile.create(contents, false, null);
		}
		return result;
	}
	
	protected List getUnmanagedResources(IProject testProject) throws CoreException, TeamException {
		List result = new ArrayList();
		IFile file = testProject.getFile("unmanaged.txt");
		if (!file.exists()) {
			file.create(getRandomContents(), false, null);
		}
		result.add(file);
		return result;
	}

	/**
	 * Method getResourceWithUnmanagedParent.
	 * @param project
	 * @return Collection
	 */
	protected List getResourceWithUnmanagedParent(IProject project) throws CoreException {
		List result = new ArrayList();
		IFolder folder = project.getFolder("newFolder");
		if(!folder.exists()) folder.create(false, true, null);
		IFile file = folder.getFile("unmanaged.txt");
		if (!file.exists()) {
			file.create(getRandomContents(), false, null);
		}
		result.add(file);
		return result;
	}
		
	protected List getOverlappingResources(IProject testProject, boolean includeFiles) {
		List result = new ArrayList();
		result.add(testProject);
		result.add(testProject.getFolder("folder1"));
		if (includeFiles) {
			result.add(testProject.getFile("folder1/a.txt"));
		}
		return result;
	}

	protected ISelection asSelection(List resources) {
		return new StructuredSelection(resources);
	}
	
	protected String getName(IActionDelegate actionDelegate) {
		return actionDelegate.getClass().getName();
	}

	/**
	 * Assert that the enablement for the given IActionDelegate and ISelection
	 * match that provided as expectedEnablement.
	 * 
	 * @param actionDelegate
	 * @param selection
	 * @param expectedEnablement
	 */
	protected void assertEnablement(IActionDelegate actionDelegate, ISelection selection, boolean expectedEnablement) {
		IAction action = new Action() {};
		actionDelegate.selectionChanged(action, selection);
		assertEquals(getName(actionDelegate) + " enablement wrong!", expectedEnablement, action.isEnabled());
	}
}

Back to the top