Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3c16f969233150039b5b3fb2af5f0ac51cc55c76 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/******************************************************************************
 *  Copyright (c) 2014 Tasktop Technologies.
 *  All rights reserved. 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:
 *    Tomasz Zarna (Tasktop) - initial API and implementation
 *****************************************************************************/
package org.eclipse.egit.ui.internal.actions;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.expressions.EvaluationContext;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.egit.ui.JobFamilies;
import org.eclipse.egit.ui.common.LocalRepositoryTestCase;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.test.ContextMenuHelper;
import org.eclipse.egit.ui.test.TestUtil;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.ui.ISources;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.services.IServiceLocator;
import org.junit.Before;
import org.junit.Test;

public class SwitchToMenuTest extends LocalRepositoryTestCase {

	private static final String TEAM_LABEL = util
			.getPluginLocalizedValue("TeamMenu.label");

	private static final String SWITCH_TO_LABEL_MULTIPLE = util
			.getPluginLocalizedValue("SwitchToMenuMultiple.label");

	private SwitchToMenu switchToMenu;

	private IHandlerService handlerService;

	@Before
	public void setUp() throws Exception {
		switchToMenu = new SwitchToMenu();
		handlerService = mock(IHandlerService.class);
		IServiceLocator serviceLocator = mock(IServiceLocator.class);
		when(serviceLocator.getService(IHandlerService.class)).thenReturn(
				handlerService);
		switchToMenu.initialize(serviceLocator);
	}

	@Test
	public void emptySelection() {
		mockSelection(new EmptySelection());

		MenuItem[] items = fillMenu();

		assertEquals(0, items.length);
	}

	@Test
	public void selectionNotAdaptableToRepository() {
		mockSelection(
				new StructuredSelection(new Object()));

		MenuItem[] items = fillMenu();

		assertEquals(0, items.length);
	}

	@Test
	public void selectionWithProj1() throws Exception {
		createProjectAndCommitToRepository();
		selectionWithProj1Common();
	}

	@Test
	public void selectionWithProj1AndReflog() throws Exception {
		File gitDir = createProjectAndCommitToRepository();

		// create additional reflog entries
		try (Git git = new Git(lookupRepository(gitDir))) {
			git.checkout().setName("stable").call();
			git.checkout().setName("master").call();
		}

		selectionWithProj1Common();

		// delete reflog again to not confuse other tests
		assertTrue(new File(gitDir, Constants.LOGS + "/" + Constants.HEAD)
				.delete());
	}

	@Test
	public void selectionWithCorruptedReflog() throws Exception {
		File gitDir = createProjectAndCommitToRepository();

		// create additional reflog entries
		try (Git git = new Git(lookupRepository(gitDir))) {
			git.checkout().setName("stable").call();
			git.checkout().setName("master").call();
		}

		// Corrupt the reflog
		File reflog = new File(gitDir, Constants.LOGS + "/" + Constants.HEAD);
		List<String> lines = Files.readAllLines(reflog.toPath());
		assertTrue("Expected some lines in the reflog", lines.size() > 1);
		lines.add(1,
				"INTENTIONALLY CORRUPTED REFLOG. Just some text that definitely shouldn't be in a reflog.");
		Files.write(reflog.toPath(), lines);

		selectionWithProj1Common();

		assertTrue(reflog.delete());
	}

	private void selectionWithProj1Common() {
		IProject project = ResourcesPlugin.getWorkspace().getRoot()
				.getProject(PROJ1);
		mockSelection(new StructuredSelection(project));

		MenuItem[] items = fillMenu();

		assertEquals(6, items.length);
		assertTextEquals(UIText.SwitchToMenu_NewBranchMenuLabel, items[0]);
		assertStyleEquals(SWT.SEPARATOR, items[1]);
		assertTextEquals("master", items[2]);
		assertTextEquals("stable", items[3]);
		assertStyleEquals(SWT.SEPARATOR, items[4]);
		assertTextEquals(UIText.SwitchToMenu_OtherMenuLabel, items[5]);
	}

	@Test
	public void selectionWithRepositoryHavingOver20Branches() throws Exception {
		Repository repo = lookupRepository(createProjectAndCommitToRepository());
		for (int i = 0; i < SwitchToMenu.MAX_NUM_MENU_ENTRIES; i++) {
			createBranch(repo, "refs/heads/change/" + i);
		}
		IProject project = ResourcesPlugin.getWorkspace().getRoot()
				.getProject(PROJ1);
		mockSelection(new StructuredSelection(project));

		MenuItem[] items = fillMenu();

		assertEquals(24, items.length);
		assertTextEquals(UIText.SwitchToMenu_NewBranchMenuLabel, items[0]);
		assertStyleEquals(SWT.SEPARATOR, items[1]);
		assertTextEquals("change/0", items[2]);
		assertTextEquals("change/1", items[3]);
		assertTextEquals("change/2", items[4]);
		assertTextEquals("change/3", items[5]);
		assertTextEquals("change/4", items[6]);
		assertTextEquals("change/5", items[7]);
		assertTextEquals("change/6", items[8]);
		assertTextEquals("change/7", items[9]);
		assertTextEquals("change/8", items[10]);
		assertTextEquals("change/9", items[11]);
		assertTextEquals("change/10", items[12]);
		assertTextEquals("change/11", items[13]);
		assertTextEquals("change/12", items[14]);
		assertTextEquals("change/13", items[15]);
		assertTextEquals("change/14", items[16]);
		assertTextEquals("change/15", items[17]);
		assertTextEquals("change/16", items[18]);
		assertTextEquals("change/17", items[19]);
		assertTextEquals("change/18", items[20]);
		assertTextEquals("change/19", items[21]);
		// "master" and "stable" didn't make it
		assertStyleEquals(SWT.SEPARATOR, items[22]);
		assertTextEquals(UIText.SwitchToMenu_OtherMenuLabel, items[23]);
	}

	@Test
	public void validateMenuEntriesForMultiSelectionWithMultipleRepositories()
			throws Exception {
		File gitOne = createProjectAndCommitToRepository(REPO1, PROJ1);
		File gitTwo = createProjectAndCommitToRepository(REPO2, PROJ2);

		Repository repoOne = lookupRepository(gitOne);
		Repository repoTwo = lookupRepository(gitTwo);
		for (int i = 0; i < SwitchToMenu.MAX_NUM_MENU_ENTRIES; i++) {
			createBranch(repoOne, "refs/heads/change/" + i);
			createBranch(repoTwo, "refs/heads/change/" + (i + 15));
		}

		mockMultiProjectSelection(PROJ1, PROJ2);

		MenuItem[] items = fillMenu();
		assertTextEquals("change/15", items[0]);
		assertTextEquals("change/16", items[1]);
		assertTextEquals("change/17", items[2]);
		assertTextEquals("change/18", items[3]);
		assertTextEquals("change/19", items[4]);
		assertTextEquals("master", items[5]);
		assertTextEquals("stable", items[6]);
	}

	@Test
	public void validateBranchSwitchingForForMultiSelectionWithMultipleRepositories()
			throws Exception {

		File gitOne = createProjectAndCommitToRepository(REPO1, PROJ1);
		File gitTwo = createProjectAndCommitToRepository(REPO2, PROJ2);
		Repository repoOne = lookupRepository(gitOne);
		Repository repoTwo = lookupRepository(gitTwo);

		// Set up different branch sources
		try (Git git = new Git(repoOne)) {
			git.checkout().setName("master").call();
		}

		try (Git git = new Git(repoTwo)) {
			git.checkout().setName("stable").call();
		}

		String branchName = "commonBranchAmongRepositories";
		String branchRef = "refs/heads/" + branchName;
		createBranch(repoOne, branchRef);
		createBranch(repoTwo, branchRef);

		assertEquals("master", repoOne.getBranch());
		assertEquals("stable", repoTwo.getBranch());

		// Multi repository Switch To
		SWTBotTree tree = TestUtil.getExplorerTree();
		SWTBotTree select = tree.select(tree.getAllItems());
		ContextMenuHelper.clickContextMenu(select, TEAM_LABEL,
				SWITCH_TO_LABEL_MULTIPLE, branchName);
		TestUtil.joinJobs(JobFamilies.CHECKOUT);

		assertEquals(branchName, repoOne.getBranch());
		assertEquals(branchName, repoTwo.getBranch());
	}

	@Test
	public void multipleSelectionWithMultipleRepositoriesAndNoCommonBranches()
			throws Exception {
		File gitOne = createProjectAndCommitToRepository(REPO1, PROJ1);
		File gitTwo = createProjectAndCommitToRepository(REPO2, PROJ2);

		try (Git git = new Git(lookupRepository(gitOne))) {
			git.checkout().setName("stable").call();
			git.branchDelete().setBranchNames("master").setForce(true).call();
		}

		try (Git git = new Git(lookupRepository(gitTwo))) {
			git.branchDelete().setBranchNames("stable").setForce(true).call();
		}

		mockMultiProjectSelection(PROJ1, PROJ2);

		MenuItem[] items = fillMenu();
		assertTextEquals(UIText.SwitchToMenu_NoCommonBranchesFound, items[0]);

		// delete reflog again to not confuse other tests
		new File(gitOne, Constants.LOGS + "/" + Constants.HEAD).delete();
		new File(gitTwo, Constants.LOGS + "/" + Constants.HEAD).delete();
	}

	private void mockSelection(ISelection selection) {
		EvaluationContext context = new EvaluationContext(null, new Object());
		context.addVariable(ISources.ACTIVE_MENU_SELECTION_NAME, selection);
		when(handlerService.getCurrentState()).thenReturn(context);
	}

	private void mockMultiProjectSelection(String... projNames) {

		List<IProject> projects = new ArrayList<>();
		for (String s : projNames) {
			projects.add(
					ResourcesPlugin.getWorkspace().getRoot().getProject(s));
		}
		mockSelection(new StructuredSelection(projects));
	}

	private MenuItem[] fillMenu() {
		final MenuItem[][] items = new MenuItem[1][];
		PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
			@Override
			public void run() {
				Menu menu = new Menu(new Shell(PlatformUI.getWorkbench().getDisplay()));
				switchToMenu.fill(menu, 0 /* index */);
				items[0] = menu.getItems();
			}
		});
		return items[0];
	}

	private static class EmptySelection implements ISelection {
		@Override
		public boolean isEmpty() {
			return true;
		}
	}

	private static void assertTextEquals(final String expectedText,
			final MenuItem item) {
		PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
			@Override
			public void run() {
				assertEquals(expectedText, item.getText());
			}
		});
	}

	private static void assertStyleEquals(final int expectedStyle,
			final MenuItem item) {
		PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
			@Override
			public void run() {
				assertEquals(expectedStyle, item.getStyle());
			}
		});
	}
}

Back to the top