Skip to main content
summaryrefslogtreecommitdiffstats
blob: 27a4edf1ab89b0e56c1a975678144135a12178ad (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
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
254
255
256
257
258
/*******************************************************************************
 * Copyright (c) 2010, 2018 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.debug.tests.launching;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchHistory;
import org.eclipse.debug.ui.IDebugUIConstants;

/**
 * Test the launch history favorites get updated properly as configurations as
 * modified.
 *
 * @since 3.6
 */
public class LaunchFavoriteTests extends AbstractLaunchTest {

	/**
	 * Configuration to use for the test. One is created for each test during
	 * setup and deleted during tear down.
	 */
	private ILaunchConfiguration fConfig;

	/**
	 * Constructor
	 * @param name
	 */
	public LaunchFavoriteTests(String name) {
		super(name);
	}

	/**
	 * Returns the run launch history
	 * @return
	 */
	private LaunchHistory getRunLaunchHistory() {
		return getLaunchConfigurationManager().getLaunchHistory(IDebugUIConstants.ID_RUN_LAUNCH_GROUP);
	}

	/**
	 * Returns the debug launch history
	 * @return
	 */
	private LaunchHistory getDebugLaunchHistory() {
		return getLaunchConfigurationManager().getLaunchHistory(IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		// clear the favorites
		getRunLaunchHistory().setFavorites(new ILaunchConfiguration[0]);
		getDebugLaunchHistory().setFavorites(new ILaunchConfiguration[0]);
		fConfig = getLaunchConfiguration(getName());
	}

	@Override
	protected void tearDown() throws Exception {
		// delete the configuration used during this test
		ILaunchConfiguration configuration = getLaunchConfiguration();
		if (configuration.exists()) {
			configuration.delete();
		}
		super.tearDown();
	}

	/**
	 * Returns the new/initial launch configuration to use for this test.
	 *
	 * @return
	 */
	private ILaunchConfiguration getLaunchConfiguration() {
		return fConfig;
	}

	/**
	 * Makes the configuration a favorite in the specified group.
	 *
	 * @param config configuration to make a favorite - may be a working copy already
	 * @param groupId group to add it to
	 * @return working copy after making it a favorite
	 *
	 * @throws CoreException
	 */
	private ILaunchConfigurationWorkingCopy addFavorite(ILaunchConfiguration config, String groupId) throws CoreException {
		ILaunchConfigurationWorkingCopy wc = getWorkingCopy(config);
		List<String> list = config.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List<String>) null);
		if (list == null) {
			list = new ArrayList<>();
		}
		list.add(groupId);
		wc.setAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, list);
		return wc;
	}

	/**
	 * Removes the favorite group from the configuration's favorites attribute. Returns
	 * the resulting working copy.
	 *
	 * @param config configuration to remove favorite attribute from, may already be a working copy
	 * @param groupId group to remove
	 * @return working copy after removing favorite
	 * @throws CoreException
	 */
	private ILaunchConfigurationWorkingCopy removeFavorite(ILaunchConfiguration config, String groupId) throws CoreException {
		ILaunchConfigurationWorkingCopy wc = getWorkingCopy(config);
		List<String> list = config.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List<String>) null);
		if (list != null) {
			if (list.remove(groupId)) {
				if (list.isEmpty()) {
					list = null;
				}
				wc.setAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, list);
			}
		}
		return wc;
	}

	private ILaunchConfigurationWorkingCopy getWorkingCopy(ILaunchConfiguration config) throws CoreException {
		ILaunchConfigurationWorkingCopy wc = null;
		if (config.isWorkingCopy()) {
			wc = (ILaunchConfigurationWorkingCopy) config;
		} else {
			wc = config.getWorkingCopy();
		}
		return wc;
	}

	/**
	 * Returns whether the launch history contains the given configuration as a favorite and is
	 * the expected size.
	 *
	 * @param history launch history
	 * @param configuration launch configuration
	 * @param size expected size of favorites or -1 if unknown
	 * @return whether the launch history contains the given configuration as a favorite and is
	 * 	the expected size
	 */
	private boolean containsFavorite(LaunchHistory history, ILaunchConfiguration configuration, int size) {
		ILaunchConfiguration[] favorites = history.getFavorites();
		assertNotNull("No favorites", favorites); //$NON-NLS-1$
		if (size != -1) {
			assertEquals("Favorites wrong size", size, favorites.length); //$NON-NLS-1$
		}
		for (int i = 0; i < favorites.length; i++) {
			if (configuration.equals(favorites[i])) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Tests that a configuration becoming a favorite appears in the favorites.
	 *
	 * @throws CoreException
	 */
	public void testBecomeFavorite() throws CoreException {
		ILaunchConfigurationWorkingCopy wc = addFavorite(getLaunchConfiguration(), IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP);
		addFavorite(wc, IDebugUIConstants.ID_RUN_LAUNCH_GROUP);
		ILaunchConfiguration saved = wc.doSave();
		assertTrue("Missing from debug favorites", containsFavorite(getDebugLaunchHistory(), saved, 1)); //$NON-NLS-1$
		assertTrue("Missing from run favorites", containsFavorite(getRunLaunchHistory(), saved, 1)); //$NON-NLS-1$
	}

	/**
	 * Tests that a when a favorite is no longer a favorite, it gets removed from the favorites.
	 *
	 * @throws CoreException
	 */
	public void testUnFavorite() throws CoreException {
		testBecomeFavorite(); // create favorite in two histories
		ILaunchConfigurationWorkingCopy wc = removeFavorite(getLaunchConfiguration(), IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP);
		removeFavorite(wc, IDebugUIConstants.ID_RUN_LAUNCH_GROUP);
		ILaunchConfiguration saved = wc.doSave();
		assertFalse("Should not be a debug favorite", containsFavorite(getDebugLaunchHistory(), saved, 0)); //$NON-NLS-1$
		assertFalse("Should not be a run favorite", containsFavorite(getRunLaunchHistory(), saved, 0)); //$NON-NLS-1$
	}

	/**
	 * When a configuration is deleted, it should no longer be in the list.
	 *
	 * @throws CoreException
	 */
	public void testDeleteConfiguration() throws CoreException {
		testBecomeFavorite(); // create a favorite in two histories
		ILaunchConfiguration configuration = getLaunchConfiguration();
		configuration.delete();
		assertFalse("Should not be a debug favorite", containsFavorite(getDebugLaunchHistory(), configuration, 0)); //$NON-NLS-1$
		assertFalse("Should not be a run favorite", containsFavorite(getRunLaunchHistory(), configuration, 0)); //$NON-NLS-1$
	}

	/**
	 * When a favorite is renamed, it should still be in the list, with the new name.
	 *
	 * @throws CoreException
	 */
	public void testRenameFavorite() throws CoreException {
		testBecomeFavorite();
		ILaunchConfiguration original = getLaunchConfiguration();
		ILaunchConfigurationWorkingCopy copy = original.getWorkingCopy();
		copy.rename("rename" + original.getName()); //$NON-NLS-1$
		ILaunchConfiguration saved = copy.doSave();
		assertTrue("Missing from debug favorites", containsFavorite(getDebugLaunchHistory(), saved, 1)); //$NON-NLS-1$
		assertTrue("Missing from run favorites", containsFavorite(getRunLaunchHistory(), saved, 1)); //$NON-NLS-1$
		saved.delete();
	}

	/**
	 * Renaming a configuration and making it a favorite at the same time - should appear in the list.
	 *
	 * @throws CoreException
	 */
	public void testRenameBecomeFavorite() throws CoreException {
		ILaunchConfiguration original = getLaunchConfiguration();
		ILaunchConfigurationWorkingCopy copy = original.getWorkingCopy();
		copy.rename("rename" + original.getName()); //$NON-NLS-1$
		addFavorite(copy, IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP);
		addFavorite(copy, IDebugUIConstants.ID_RUN_LAUNCH_GROUP);
		ILaunchConfiguration saved = copy.doSave();
		assertTrue("Missing from debug favorites", containsFavorite(getDebugLaunchHistory(), saved, 1)); //$NON-NLS-1$
		assertTrue("Missing from run favorites", containsFavorite(getRunLaunchHistory(), saved, 1)); //$NON-NLS-1$
		saved.delete();
	}

	/**
	 * Renaming a configuration and removing its favorite status should remove it from the list.
	 *
	 * @throws CoreException
	 */
	public void testRenameUnFavorite() throws CoreException {
		testBecomeFavorite();
		ILaunchConfiguration original = getLaunchConfiguration();
		ILaunchConfigurationWorkingCopy copy = original.getWorkingCopy();
		copy.rename("rename" + original.getName()); //$NON-NLS-1$
		removeFavorite(copy, IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP);
		removeFavorite(copy, IDebugUIConstants.ID_RUN_LAUNCH_GROUP);
		ILaunchConfiguration saved = copy.doSave();
		assertFalse("Should not be a debug favorite", containsFavorite(getDebugLaunchHistory(), saved, 0)); //$NON-NLS-1$
		assertFalse("Should not be a run favorite", containsFavorite(getRunLaunchHistory(), saved, 0)); //$NON-NLS-1$
		saved.delete();
	}
}

Back to the top