Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9c6ffba850c9132bdb50e6adfe6389bf5e1c7d78 (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
/*******************************************************************************
 * Copyright (c) 2011 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 org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.synchronize.SyncInfoSet;
import org.eclipse.team.internal.ccvs.core.mapping.CVSActiveChangeSet;
import org.eclipse.team.internal.ui.synchronize.ChangeSetCapability;
import org.eclipse.team.internal.ui.synchronize.ChangeSetDiffNode;
import org.eclipse.team.internal.ui.synchronize.ChangeSetModelProvider;
import org.eclipse.team.internal.ui.synchronize.ChangeSetModelSorter;
import org.eclipse.team.internal.ui.synchronize.SynchronizeModelElementSorter;
import org.eclipse.team.internal.ui.synchronize.SynchronizePageConfiguration;
import org.eclipse.team.internal.ui.synchronize.TreeViewerAdvisor;
import org.eclipse.team.internal.ui.synchronize.UnchangedResourceModelElement;
import org.eclipse.team.tests.ccvs.core.EclipseTest;
import org.eclipse.team.ui.synchronize.ISynchronizePage;
import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
import org.eclipse.team.ui.synchronize.ISynchronizePageSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import junit.framework.Test;

public class PatchTreeTest extends EclipseTest {

	public void testChangeSetModelSorter() throws CoreException {
		MyTreeViewer tree = new MyTreeViewer();
		tree.init();
		ViewerComparator sorter = tree.getSorter();

		UnchangedResourceModelElement elementZ = new UnchangedResourceModelElement(
				null, getUniqueTestProject("z"));
		ChangeSetDiffNode nodeA = new ChangeSetDiffNode(null,
				new CVSActiveChangeSet(null, "a"));
		ChangeSetDiffNode nodeB = new ChangeSetDiffNode(null,
				new CVSActiveChangeSet(null, "b"));
		ChangeSetDiffNode nodeC = new ChangeSetDiffNode(null,
				new CVSActiveChangeSet(null, "c"));
		Object[] elements = { nodeB, nodeC, elementZ, nodeA };

		sorter.sort(tree, elements);

		assertEquals(nodeA, elements[0]);
		assertEquals(nodeB, elements[1]);
		assertEquals(nodeC, elements[2]);
		assertEquals(elementZ, elements[3]);
	}

	public void testDuplicatedElementsInPatchTree() throws TeamException,
			CoreException {
		MyTreeViewer tree = new MyTreeViewer();
		tree.init();

		UnchangedResourceModelElement element = new UnchangedResourceModelElement(
				null, getUniqueTestProject("z"));
		ChangeSetDiffNode nodeA = new ChangeSetDiffNode(null,
				new CVSActiveChangeSet(null, "a"));
		ChangeSetDiffNode nodeB = new ChangeSetDiffNode(null,
				new CVSActiveChangeSet(null, "b"));
		ChangeSetDiffNode nodeC = new ChangeSetDiffNode(null,
				new CVSActiveChangeSet(null, "c"));
		ChangeSetDiffNode nodeD = new ChangeSetDiffNode(null,
				new CVSActiveChangeSet(null, "d"));

		Item[] treeChildren = tree.testGetChildren(tree.getTree());
		assertEquals(0, treeChildren.length);

		Object children[] = { nodeC, nodeB, element, nodeA };
		tree.testInternalAdd(children);
		treeChildren = tree.testGetChildren(tree.getTree());
		assertEquals(children.length, treeChildren.length);

		Object childrenToAdd[] = { nodeD, element, nodeA, nodeB };
		tree.testInternalAdd(childrenToAdd);
		treeChildren = tree.testGetChildren(tree.getTree());

		Object[] expected = { nodeC, nodeB, element, nodeA, nodeD };
		assertEquals(expected.length, treeChildren.length);

		for (int i = 0; i < treeChildren.length; i++) {
			assertEquals(1, countByData(treeChildren, treeChildren[i]));
		}
	}

	private int countByData(final Item[] a, Item o) {
		int c = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i].getData() == o.getData())
				c++;
		}
		return c;
	}

	private SynchronizePageConfiguration getMyConfiguration(final Viewer viewer) {
		SynchronizePageConfiguration conf = new SynchronizePageConfiguration(
				null);
		conf.setPage(new ISynchronizePage() {

			public void init(ISynchronizePageSite site)
					throws PartInitException {
			}

			public Viewer getViewer() {
				return viewer;
			}

			public boolean aboutToChangeProperty(
					ISynchronizePageConfiguration configuration, String key,
					Object newValue) {
				return false;
			}
		});
		return conf;
	}

	private class MyModelProvider extends ChangeSetModelProvider implements
			IPropertyChangeListener {

		public ChangeSetCapability getChangeSetCapability() {
			return new ChangeSetCapability() {
			};
		}

		public MyModelProvider(final Viewer viewer) {
			super(getMyConfiguration(viewer), new SyncInfoSet(),
					"sampleProviderId");
			addPropertyChangeListener(this);
		}

		public void propertyChange(PropertyChangeEvent event) {
			// nothing to do
		}
	}

	private class MyTreeViewer extends
			TreeViewerAdvisor.NavigableCheckboxTreeViewer {

		public MyTreeViewer() {
			super(new Composite(PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow().getShell(), SWT.NONE),
					SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		}

		public void testInternalAdd(Object[] childElements) {
			internalAdd(this.getTree(), null, childElements);
		}

		public Item[] testGetChildren(Widget widget) {
			return this.getChildren(widget);
		}

		public void init() {
			ChangeSetModelProvider provider = new MyModelProvider(this);
			provider.setViewerSorter(new SynchronizeModelElementSorter());
			ChangeSetModelSorter sorter = new ChangeSetModelSorter(provider, 0);
			setSorter(sorter);
		}
	}
	
	public static Test suite() {
		return suite(PatchTreeTest.class);
	}

}

Back to the top