Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 608bc2506dade7df77d62def247d7d26c3aa225f (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
/*******************************************************************************
 * Copyright (c) 2006, 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.core.subscriber;

import java.io.ByteArrayInputStream;

import junit.framework.Test;

import org.eclipse.compare.*;
import org.eclipse.compare.internal.Utilities;
import org.eclipse.compare.structuremergeviewer.ICompareInput;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.widgets.Display;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.subscribers.Subscriber;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.internal.ui.history.FileRevisionTypedElement;
import org.eclipse.team.internal.ui.mapping.ModelCompareEditorInput;
import org.eclipse.team.internal.ui.mapping.ResourceDiffCompareInput;
import org.eclipse.team.internal.ui.synchronize.EditableSharedDocumentAdapter;
import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement;
import org.eclipse.team.internal.ui.synchronize.actions.OpenInCompareAction;
import org.eclipse.team.tests.ccvs.ui.ModelParticipantSyncInfoSource;
import org.eclipse.ui.*;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;

/**
 * Test the behavior of compare editors opened on diffs.
 */
public class CompareEditorTests extends CVSSyncSubscriberTest {

	public static Test suite() {
		return suite(CompareEditorTests.class);
	}
	
	public CompareEditorTests() {
		super();
	}
	
	public CompareEditorTests(String name) {
		super(name);
	}
	
	protected CVSSyncTreeSubscriber getSubscriber() throws TeamException {
		return (CVSSyncTreeSubscriber)getWorkspaceSubscriber();
	}
	
	private IEditorInput openEditor(Subscriber subscriber, Object element) {
		IEditorInput input = OpenInCompareAction.openCompareEditor(((ModelParticipantSyncInfoSource)getSyncInfoSource()).getConfiguration(subscriber), element, false, false);
		assertNotNull(input);
		assertEditorOpen(input, subscriber);
		return input;
	}
	
	private IEditorInput openEditor(Object element) throws TeamException {
		return openEditor(getSubscriber(), element);
	}
	
	private void assertEditorOpen(IEditorInput input, Subscriber subscriber) {
		waitForCollectors(subscriber);
		while (Display.getCurrent().readAndDispatch()) {};
		IEditorPart part = findOpenEditor(input);
		assertNotNull("The editor is not open", part);
	}

	private void assertEditorClosed(IEditorInput input) {
		while (Display.getCurrent().readAndDispatch()) {};
		IEditorPart part = findOpenEditor(input);
		assertNull("The editor is not closed", part);
	}
	
	private void assertRevisionsEquals(IEditorInput input, IFile file) throws CVSException {
		while (Display.getCurrent().readAndDispatch()) {};
		CompareEditorInput cei = (CompareEditorInput)input;
		ICompareInput node = (ICompareInput)cei.getCompareResult();
		String remoteRevision = ((FileRevisionTypedElement)node.getRight()).getContentIdentifier();
		String localRevision = CVSWorkspaceRoot.getCVSFileFor(file).getSyncInfo().getRevision();
		assertEquals(localRevision, remoteRevision);
	}
	
	private void assertEditorState(IEditorInput input) throws TeamException {
		waitForCollectors();
		while (Display.getCurrent().readAndDispatch()) {};
		CompareEditorInput cei = (CompareEditorInput)input;
		if (cei instanceof ModelCompareEditorInput) {
			ModelCompareEditorInput mcei = (ModelCompareEditorInput) cei;
			ICompareInput ci = (ICompareInput)mcei.getCompareResult();
			if (ci instanceof ResourceDiffCompareInput) {
				ResourceDiffCompareInput rdci = (ResourceDiffCompareInput) ci;
				IFile file = (IFile)rdci.getResource();
				LocalResourceTypedElement element = (LocalResourceTypedElement)rdci.getLeft();
				EditableSharedDocumentAdapter adapter = (EditableSharedDocumentAdapter)element.getAdapter(ISharedDocumentAdapter.class);
				assertTrue(element.exists() == file.exists());
				assertTrue(file.exists() == (adapter != null));
				if (file.exists())
					assertTrue(adapter.isConnected());
				return;
			}
		}
		fail("Unexpected compare input type");
	}
	
	private void closeAllEditors() {
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeAllEditors(false);
	}
	
	private IEditorPart findOpenEditor(IEditorInput input) {
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IEditorReference[] editorRefs = page.getEditorReferences();						
		for (int i = 0; i < editorRefs.length; i++) {
			final IEditorPart part = editorRefs[i].getEditor(false /* don't restore editor */);
			if(part != null) {
				IEditorInput testInput = part.getEditorInput();
				if (testInput == input)
					return part;
			}
		}
		return null;
	}
	
	private void dirtyEditor(IFile file, IEditorInput input, String string) {
		FileEditorInput fileEditorInput = new FileEditorInput(file);
		IDocumentProvider provider = SharedDocumentAdapter.getDocumentProvider(fileEditorInput);
		IDocument document = provider.getDocument(fileEditorInput);
		document.set(string);
	}
	
	protected void setUp() throws Exception {
		super.setUp();
		// Need to set both the Compare and Team test flags to true
		Utilities.RUNNING_TESTS = true;
		Utils.RUNNING_TESTS = true;
	}
	
	protected void setTestingFlushOnCompareInputChange(boolean b) {
		// Need to set both the Compare and Team test flags
		Utilities.TESTING_FLUSH_ON_COMPARE_INPUT_CHANGE = b;
		Utils.TESTING_FLUSH_ON_COMPARE_INPUT_CHANGE = b;
	}
	
	protected void tearDown() throws Exception {
		closeAllEditors();
		super.tearDown();
	}
	
	private void waitForCollectors() throws TeamException {
		waitForCollectors(getSubscriber());
	}
	
	private void waitForCollectors(Subscriber subcriber) {
		((ModelParticipantSyncInfoSource)getSyncInfoSource()).waitForCollectionToFinish(subcriber);
	}

	public void testCloseOnUpdate() throws CoreException {
		// setup a project with an incoming change that is open in an editor
		IProject project = createProject(new String[] { "file1.txt"});
		IProject copy = checkoutCopy(project, "-copy");
		setContentsAndEnsureModified(copy.getFile("file1.txt"));
		commitProject(copy);
		refresh(getSubscriber(), project);
		IEditorInput input = openEditor(project.getFile("file1.txt"));
		
		// Update and assert that the editor gets closed
		updateProject(project, null, false);
		waitForCollectors();
		assertEditorClosed(input);
	}
	
	public void testCloseOnCommit() throws CoreException {
		// Setup a project with an outgoing change that is open in an editor
		IProject project = createProject(new String[] { "file1.txt"});
		setContentsAndEnsureModified(project.getFile("file1.txt"));
		refresh(getSubscriber(), project);
		IEditorInput input = openEditor(project.getFile("file1.txt"));
		
		// Commit and assert that the editor gets closed
		commitProject(project);
		waitForCollectors();
		assertEditorClosed(input);
	}
	
	public void testSaveOnUpdateWhenDirty() throws CoreException {
		// setup a project with an incoming change that is open in an editor
		IProject project = createProject(new String[] { "file1.txt"});
		IProject copy = checkoutCopy(project, "-copy");
		setContentsAndEnsureModified(copy.getFile("file1.txt"));
		commitProject(copy);
		refresh(getSubscriber(), project);
		IEditorInput input = openEditor(project.getFile("file1.txt"));
		
		// Dirty the editor
		String contents = "this is the file contents";
		dirtyEditor(project.getFile("file1.txt"), input, contents);
		
		// Update and ensure that the contents are written and the editor remains open
		setTestingFlushOnCompareInputChange(true);
		updateProject(project, null, false);
		waitForCollectors();
		assertContentsEqual(project.getFile("file1.txt"), contents);
		// We would like the editor to stay open but its too complicated
		// assertEditorOpen(input);
	}
	
	public void testCloseOnUpdateWhenDirty() throws CoreException {
		// setup a project with an incoming change that is open in an editor
		IProject project = createProject(new String[] { "file1.txt"});
		IProject copy = checkoutCopy(project, "-copy");
		String incomingContents = "Incoming change";
		setContentsAndEnsureModified(copy.getFile("file1.txt"), incomingContents);
		commitProject(copy);
		refresh(getSubscriber(), project);
		IEditorInput input = openEditor(project.getFile("file1.txt"));
		
		// Dirty the editor
		String contents = "this is the file contents";
		dirtyEditor(project.getFile("file1.txt"), input, contents);
		
		// Update and ensure that the editor is closed
		setTestingFlushOnCompareInputChange(false);
		updateProject(project, null, false);
		waitForCollectors();
		assertContentsEqual(project.getFile("file1.txt"), incomingContents);
		// We would like the editor to close but its too complicated to guarantee
		// assertEditorClosed(input);
	}

	public void testSaveOnCommitWhenDirty() throws CoreException {
		// Setup a project with an outgoing change that is open in an editor
		IProject project = createProject(new String[] { "file1.txt"});
		setContentsAndEnsureModified(project.getFile("file1.txt"));
		refresh(getSubscriber(), project);
		IEditorInput input = openEditor(project.getFile("file1.txt"));
		
		// Dirty the editor
		String contents = "this is the file contents";
		dirtyEditor(project.getFile("file1.txt"), input, contents);
		
		// Commit and ensure that the contents are written and the editor remains open
		setTestingFlushOnCompareInputChange(true);
		commitProject(project);
		waitForCollectors();
		assertContentsEqual(project.getFile("file1.txt"), contents);
		// We would like the editor to stay open but its too complicated
		// assertEditorOpen(input);
	}
	
	public void testCloseOnCommitWhenDirty() throws CoreException {
		// Setup a project with an outgoing change that is open in an editor
		IProject project = createProject(new String[] { "file1.txt"});
		String committedContents = "Committed contents";
		setContentsAndEnsureModified(project.getFile("file1.txt"), committedContents);
		refresh(getSubscriber(), project);
		IEditorInput input = openEditor(project.getFile("file1.txt"));
		
		// Dirty the editor
		String contents = "this is the file contents";
		dirtyEditor(project.getFile("file1.txt"), input, contents);
		
		// Commit and ensure that the editor is closed
		setTestingFlushOnCompareInputChange(false);
		commitProject(project);
		waitForCollectors();
		assertContentsEqual(project.getFile("file1.txt"), committedContents);
		// We would like the editor to close but its too complicated to guarantee
		// assertEditorClosed(input);
	}
	
	public void testCloseOnParticipantDispose() throws CoreException {
		IProject project = createProject(new String[] { "file1.txt"});
		CVSTag v1 = new CVSTag("v1", CVSTag.VERSION);
		tagProject(project, v1, true);
		setContentsAndEnsureModified(project.getFile("file1.txt"));
		commitProject(project);
		
		Subscriber subscriber = getSyncInfoSource().createCompareSubscriber(project, v1);
		
		refresh(subscriber, project);
		IEditorInput input = openEditor(subscriber, project.getFile("file1.txt"));
		getSyncInfoSource().disposeSubscriber(subscriber);
		assertEditorClosed(input);
	}
	
	public void testStayOpenOnParticipantDisposeWhenDirty() throws CoreException {
		IProject project = createProject(new String[] { "file1.txt"});
		CVSTag v1 = new CVSTag("v1", CVSTag.VERSION);
		tagProject(project, v1, true);
		setContentsAndEnsureModified(project.getFile("file1.txt"));
		commitProject(project);
		
		Subscriber subscriber = getSyncInfoSource().createCompareSubscriber(project, v1);
		
		refresh(subscriber, project);
		IEditorInput input = openEditor(subscriber, project.getFile("file1.txt"));
		String contents = "this is the file contents";
		dirtyEditor(project.getFile("file1.txt"), input, contents);
		setTestingFlushOnCompareInputChange(true);
		getSyncInfoSource().disposeSubscriber(subscriber);
		assertEditorOpen(input, subscriber);
	}
	
	public void testUpdateOnRemoteChange() throws CoreException {
		IProject project = createProject(new String[] { "file1.txt"});
		
		// First open the editor on an outgoing change
		setContentsAndEnsureModified(project.getFile("file1.txt"));
		IEditorInput input = openEditor(project.getFile("file1.txt"));
		assertRevisionsEquals(input, project.getFile("file1.txt"));
		
		// Now change the remote and refresh the project
		IProject copy = checkoutCopy(project, "-copy");
		setContentsAndEnsureModified(copy.getFile("file1.txt"));
		commitProject(copy);
		refresh(getSubscriber(), project);
		
		// The input revision should now match the remote revision
		assertRevisionsEquals(input, copy.getFile("file1.txt"));
		
	}
	
	public void testUpdateOnRemoteChangeWhenDirty() throws CoreException {
		IProject project = createProject(new String[] { "file1.txt"});
		
		// First open the editor and dirty it
		setContentsAndEnsureModified(project.getFile("file1.txt"));
		IEditorInput input = openEditor(project.getFile("file1.txt"));
		assertRevisionsEquals(input, project.getFile("file1.txt"));
		String contents = "this is the file contents";
		dirtyEditor(project.getFile("file1.txt"), input, contents);
		
		// Now change the remote and refresh the project
		IProject copy = checkoutCopy(project, "-copy");
		setContentsAndEnsureModified(copy.getFile("file1.txt"));
		commitProject(copy);
		setTestingFlushOnCompareInputChange(true);
		refresh(getSubscriber(), project);
		
		// The revision should be changed and the contents written to disk
		assertRevisionsEquals(input, copy.getFile("file1.txt"));
		assertContentsEqual(project.getFile("file1.txt"), contents);
		
	}
	
	public void testFileCreation() throws CoreException {
		// Create an outgoing deletion and open an editor
		IProject project = createProject(new String[] { "file1.txt"});
		project.getFile("file1.txt").delete(false, null);
		IEditorInput input = openEditor(project.getFile("file1.txt"));
		assertEditorState(input);
		
		// Recreate the file
		project.getFile("file1.txt").create(new ByteArrayInputStream("Recreated file".getBytes()), false, null);
		assertEditorState(input);
	}
	
	public void testFileDeletion() throws CoreException {
		// Create an outgoing change and open an editor
		IProject project = createProject(new String[] { "file1.txt"});
		setContentsAndEnsureModified(project.getFile("file1.txt"));
		IEditorInput input = openEditor(project.getFile("file1.txt"));
		assertEditorState(input);
		
		// Delete the file
		project.getFile("file1.txt").delete(false, null);
		assertEditorState(input);
	}
	
}

Back to the top