Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6be393b0818201ca1d81b6cc44a7742671a8bca9 (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*******************************************************************************
 * Copyright (c) 2012, 2013 SAP AG and others.
 * 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:
 *    Mathias Kinzler (SAP AG) - initial implementation
 *    Chris Aniszczyk <caniszczyk@gmail.com> - tag API changes
 *    Mathias Kinzler (SAP AG) - compare with previous actions
 *******************************************************************************/
package org.eclipse.egit.ui.test.team.actions;

import static org.eclipse.jface.dialogs.MessageDialogWithToggle.NEVER;
import static org.eclipse.team.internal.ui.IPreferenceIds.SYNCHRONIZING_COMPLETE_PERSPECTIVE;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.concurrent.TimeUnit;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.mapping.ModelProvider;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.egit.core.op.ResetOperation;
import org.eclipse.egit.core.op.TagOperation;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.UIPreferences;
import org.eclipse.egit.ui.common.LocalRepositoryTestCase;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.dialogs.CompareTreeView;
import org.eclipse.egit.ui.test.ContextMenuHelper;
import org.eclipse.egit.ui.test.JobJoiner;
import org.eclipse.egit.ui.test.TestUtil;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.TagBuilder;
import org.eclipse.jgit.util.RawParseUtils;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.waits.Conditions;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotLabel;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.eclipse.team.internal.ui.TeamUIPlugin;
import org.eclipse.team.ui.TeamUI;
import org.eclipse.team.ui.mapping.ITeamContentProviderDescriptor;
import org.eclipse.team.ui.mapping.ITeamContentProviderManager;
import org.eclipse.team.ui.synchronize.ISynchronizeManager;
import org.eclipse.team.ui.synchronize.ISynchronizeView;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Tests for the Compare With actions
 */
@RunWith(SWTBotJunit4ClassRunner.class)
public class CompareActionsTest extends LocalRepositoryTestCase {
	private File repositoryFile;

	private static String TAGS;
	private static ObjectId commitOfTag;

	@Before
	public void setup() throws Exception {
		repositoryFile = createProjectAndCommitToRepository();
		Repository repo = lookupRepository(repositoryFile);

		disablePerspectiveSwitchPrompt();
		setEnabledModelProvider(ModelProvider.RESOURCE_MODEL_PROVIDER_ID);

		TagBuilder tag = new TagBuilder();
		tag.setTag("SomeTag");
		tag.setTagger(RawParseUtils.parsePersonIdent(TestUtil.TESTAUTHOR));
		tag.setMessage("I'm just a little tag");
		tag.setObjectId(repo.resolve(repo.getFullBranch()),
				Constants.OBJ_COMMIT);
		commitOfTag = tag.getObjectId();
		TagOperation top = new TagOperation(repo, tag, false);
		top.execute(null);
		touchAndSubmit(null);

		TAGS = UIText.RepositoriesViewLabelProvider_TagsNodeText;
	}

	private static void setEnabledModelProvider(String modelProviderId) {
		ITeamContentProviderManager contentProviderManager = TeamUI
				.getTeamContentProviderManager();
		ITeamContentProviderDescriptor descriptor = contentProviderManager
				.getDescriptor(modelProviderId);
		contentProviderManager.setEnabledDescriptors(
				new ITeamContentProviderDescriptor[] { descriptor });
	}

	@SuppressWarnings("restriction")
	private static void disablePerspectiveSwitchPrompt() {
		// disable perspective synchronize selection
		TeamUIPlugin.getPlugin().getPreferenceStore()
				.setValue(SYNCHRONIZING_COMPLETE_PERSPECTIVE, NEVER);
		Activator.getDefault().getPreferenceStore()
				.setValue(UIPreferences.SYNC_VIEW_FETCH_BEFORE_LAUNCH, false);
	}

	@Test
	public void testCompareWithCommit() throws Exception {
		String compareWithCommitMenuText = util
				.getPluginLocalizedValue("CompareWithCommitAction.label");
		String dialogTitle = UIText.CommitSelectionDialog_WindowTitle;
		SWTBotShell dialog = openCompareWithDialog(compareWithCommitMenuText,
				dialogTitle);
		// use the default (the last commit) -> no changes
		assertEquals(3, dialog.bot().table().rowCount());
		dialog.bot().table().select(0);

		JobJoiner jobJoiner = JobJoiner.startListening(
				ISynchronizeManager.FAMILY_SYNCHRONIZE_OPERATION, 60,
				TimeUnit.SECONDS);
		dialog.bot().button(UIText.HistoryCommitSelectionDialog_ButtonOK)
				.click();
		jobJoiner.join();

		closeFirstEmptySynchronizeDialog();

		assertSynchronizeNoChange();

		// use the second (previous) -> should have a change
		dialog = openCompareWithDialog(compareWithCommitMenuText, dialogTitle);
		dialog.bot().table().select(1);

		jobJoiner = JobJoiner.startListening(
				ISynchronizeManager.FAMILY_SYNCHRONIZE_OPERATION, 60,
				TimeUnit.SECONDS);
		dialog.bot().button(UIText.HistoryCommitSelectionDialog_ButtonOK)
				.click();
		jobJoiner.join();

		assertSynchronizeFile1Changed();
	}

	@Test
	public void testCompareWithRef() throws Exception {
		String compareWithRefActionLabel = util
				.getPluginLocalizedValue("CompareWithBranchOrTagAction.label");
		String dialogTitle = UIText.CompareTargetSelectionDialog_WindowTitle;
		SWTBotShell dialog = openCompareWithDialog(compareWithRefActionLabel,
				dialogTitle);

		// use the default (the last commit) -> no changes
		JobJoiner jobJoiner = JobJoiner.startListening(
				ISynchronizeManager.FAMILY_SYNCHRONIZE_OPERATION, 60,
				TimeUnit.SECONDS);
		dialog.bot().button(UIText.CompareTargetSelectionDialog_CompareButton)
				.click();
		jobJoiner.join();

		closeFirstEmptySynchronizeDialog();

		assertSynchronizeNoChange();

		// use the tag -> should have a change
		dialog = openCompareWithDialog(compareWithRefActionLabel, dialogTitle);
		SWTBotTreeItem tags = TestUtil
				.expandAndWait(dialog.bot().tree().getTreeItem(TAGS));
		TestUtil.getChildNode(tags, "SomeTag").select();

		jobJoiner = JobJoiner.startListening(
				ISynchronizeManager.FAMILY_SYNCHRONIZE_OPERATION, 60,
				TimeUnit.SECONDS);
		dialog.bot().button(UIText.CompareTargetSelectionDialog_CompareButton)
				.click();
		jobJoiner.join();

		assertSynchronizeFile1Changed();
	}

	@Test
	public void testCompareWithPrevious() throws Exception {
		String menuLabel = util
				.getPluginLocalizedValue("CompareWithPreviousAction.label");
		clickCompareWithAndWaitForSync(menuLabel);

		closeFirstEmptySynchronizeDialog();

		assertSynchronizeFile1Changed();
	}

	@Test
	public void testCompareWithPreviousWithMerge() throws Exception {
		Repository repo = lookupRepository(repositoryFile);

		try (Git git = new Git(repo)) {
			ObjectId masterId = repo.resolve("refs/heads/master");
			Ref newBranch = git.checkout().setCreateBranch(true)
					.setStartPoint(commitOfTag.name()).setName("toMerge")
					.call();
			ByteArrayInputStream bis = new ByteArrayInputStream(
					"Modified".getBytes("UTF-8"));
			ResourcesPlugin.getWorkspace().getRoot().getProject(PROJ1)
					.getFolder(FOLDER).getFile(FILE2)
					.setContents(bis, false, false, null);
			bis.close();
			git.commit().setAll(true).setMessage("To be merged").call();
			git.merge().include(masterId).call();
			String menuLabel = util
					.getPluginLocalizedValue("CompareWithPreviousAction.label");
			SWTBotShell selectDialog = openCompareWithDialog(menuLabel,
					UIText.CommitSelectDialog_WindowTitle);
			assertEquals(2, selectDialog.bot().table().rowCount());
			selectDialog.close();
			// cleanup: checkout again master and delete merged branch
			git.checkout().setName("refs/heads/master").call();
			git.branchDelete().setBranchNames(newBranch.getName())
					.setForce(true).call();
		}
	}

	@Test
	public void testCompareWithIndex() throws Exception {
		String compareWithIndexActionLabel = util
				.getPluginLocalizedValue("CompareWithIndexAction_label");
		clickCompareWith(compareWithIndexActionLabel);

		// compare with index should not have any changes
		assertTreeCompareNoChange();
		// change test file -> should have one change
		setTestFileContent("Hello there");

		clickCompareWith(compareWithIndexActionLabel);

		assertTreeCompareChanges(1);

		// add to index -> no more changes
		try (Git git = new Git(lookupRepository(repositoryFile))) {
			git.add().addFilepattern(PROJ1 + "/" + FOLDER + "/" + FILE1).call();
		}

		clickCompareWith(compareWithIndexActionLabel);

		assertTreeCompareNoChange();

		// reset -> there should be no more changes
		ResetOperation rop = new ResetOperation(
				lookupRepository(repositoryFile), "refs/heads/master",
				ResetType.HARD);
		rop.execute(new NullProgressMonitor());

		clickCompareWith(compareWithIndexActionLabel);

		assertTreeCompareNoChange();
	}

	@Test
	public void testCompareWithHead() throws Exception {
		String compareWithHeadMenuLabel = util
				.getPluginLocalizedValue("CompareWithHeadAction_label");
		clickCompareWithAndWaitForSync(compareWithHeadMenuLabel);
		closeFirstEmptySynchronizeDialog();
		assertSynchronizeNoChange();

		// change test file -> should have one change
		setTestFileContent("Hello there");

		clickCompareWithAndWaitForSync(compareWithHeadMenuLabel);

		assertSynchronizeFile1Changed();

		// add to index -> should still show as change
		try (Git git = new Git(lookupRepository(repositoryFile))) {
			git.add().addFilepattern(PROJ1 + "/" + FOLDER + "/" + FILE1).call();
		}

		clickCompareWithAndWaitForSync(compareWithHeadMenuLabel);

		assertSynchronizeFile1Changed();

		// reset -> there should be no more changes
		ResetOperation rop = new ResetOperation(
				lookupRepository(repositoryFile), "refs/heads/master",
				ResetType.HARD);
		rop.execute(new NullProgressMonitor());

		clickCompareWithAndWaitForSync(compareWithHeadMenuLabel);

		assertSynchronizeNoChange();
	}

	private void clickCompareWith(String menuLabel) {
		SWTBotTree projectExplorerTree = selectProjectItem();
		ContextMenuHelper.clickContextMenuSync(projectExplorerTree, "Compare With",
				menuLabel);
	}

	private void clickCompareWithAndWaitForSync(String menuLabel) {
		JobJoiner jobJoiner = JobJoiner.startListening(
				ISynchronizeManager.FAMILY_SYNCHRONIZE_OPERATION, 60,
				TimeUnit.SECONDS);
		clickCompareWith(menuLabel);
		jobJoiner.join();
	}

	private SWTBotShell openCompareWithDialog(String menuLabel,
			String dialogTitle) {
		SWTBotTree projectExplorerTree = selectProjectItem();
		ContextMenuHelper.clickContextMenu(projectExplorerTree, "Compare With",
				menuLabel);
		SWTBotShell dialog = bot.shell(dialogTitle);
		return dialog;
	}

	private SWTBotTree selectProjectItem() {
		SWTBotTree projectExplorerTree = TestUtil.getExplorerTree();
		getProjectItem(projectExplorerTree, PROJ1).select();
		return projectExplorerTree;
	}

	private SWTBotTree waitUntilCompareTreeViewTreeHasNodeCount(int nodeCount) {
		SWTBotTree tree = bot.viewById(CompareTreeView.ID).bot().tree();
		bot.waitUntil(Conditions.treeHasRows(tree, nodeCount), 10000);
		return tree;
	}

	/**
	 * On the very first synchronization with no result, Team will display a
	 * modal dialog. This aims at closing it if visible.
	 */
	private void closeFirstEmptySynchronizeDialog() {
		// Do not use bot.shell(String) : we don't want to fail if not present.
		SWTBotShell[] shells = bot.shells();
		for (int i = 0; i < shells.length; i++) {
			SWTBotShell shell = shells[i];
			if ("Synchronize Complete - Git".equals(shell.getText()))
				shell.close();
		}
	}

	private void assertSynchronizeNoChange() {
		// 0 => title, 1 => ?, 2 => "no result" Label
		SWTBotLabel syncViewLabel = bot.viewById(ISynchronizeView.VIEW_ID).bot()
				.label(0);

		String noResultLabel = syncViewLabel.getText();
		String expected = "No changes in 'Git (" + PROJ1 + ")'.";
		if (!noResultLabel.contains(expected)) {
			syncViewLabel = bot.viewById(ISynchronizeView.VIEW_ID).bot().label(2);
			noResultLabel = syncViewLabel.getText();
			assertTrue(noResultLabel.contains(expected));
		}
	}

	private void assertSynchronizeFile1Changed() {
		SWTBotTree syncViewTree = bot.viewById(ISynchronizeView.VIEW_ID).bot().tree();
		SWTBotTreeItem[] syncItems = syncViewTree.getAllItems();
		assertEquals(syncItems.length, 1);
		String text = syncItems[0].getText();
		assertTrue("Received unexpected text: " + text, text.contains(PROJ1));

		TestUtil.expandAndWait(syncItems[0]);
		SWTBotTreeItem[] level1Children = syncItems[0].getItems();
		assertEquals(level1Children.length, 1);
		assertTrue(level1Children[0].getText().contains(FOLDER));

		TestUtil.expandAndWait(level1Children[0]);
		SWTBotTreeItem[] level2Children = level1Children[0].getItems();
		assertEquals(level2Children.length, 1);
		assertTrue(level2Children[0].getText().contains(FILE1));
	}

	private void assertTreeCompareNoChange() {
		SWTBotTree tree = waitUntilCompareTreeViewTreeHasNodeCount(1);
		SWTBotTreeItem[] items = tree.getAllItems();
		assertEquals(1, items.length);
		assertEquals(UIText.CompareTreeView_NoDifferencesFoundMessage,
				items[0].getText());
	}

	private void assertTreeCompareChanges(int nodeCount) {
		SWTBotTree tree = waitUntilCompareTreeViewTreeHasNodeCount(nodeCount);
		SWTBotTreeItem[] items = tree.getAllItems();
		assertThat(items[0].getText(),
				not(UIText.CompareTreeView_NoDifferencesFoundMessage));
	}
}

Back to the top