Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aa6a4e05edec0b60cba89874157f5fd61ba60342 (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
/*****************************************************************************
 * Copyright (c) 2016 Christian W. Damus and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.nattable.tests.tests;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat;

import java.util.Collections;

import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.infra.core.architecture.ArchitectureDescriptionLanguage;
import org.eclipse.papyrus.infra.core.architecture.ArchitectureDomain;
import org.eclipse.papyrus.infra.core.architecture.ArchitectureFactory;
import org.eclipse.papyrus.infra.core.architecture.ArchitectureDescriptionPreferences;
import org.eclipse.papyrus.infra.core.architecture.ArchitectureViewpoint;
import org.eclipse.papyrus.infra.architecture.ArchitectureDomainManager;
import org.eclipse.papyrus.infra.architecture.ArchitectureDomainPreferences;
import org.eclipse.papyrus.infra.architecture.ArchitectureDescriptionUtils;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.nattable.common.reconciler.TableVersioningUtils;
import org.eclipse.papyrus.infra.nattable.model.nattable.NattableFactory;
import org.eclipse.papyrus.infra.nattable.model.nattable.Table;
import org.eclipse.papyrus.infra.nattable.provider.TableLabelProvider;
import org.eclipse.papyrus.infra.nattable.representation.PapyrusTable;
import org.eclipse.papyrus.infra.nattable.representation.RepresentationFactory;
import org.eclipse.papyrus.infra.services.edit.context.TypeContext;
import org.eclipse.papyrus.junit.utils.rules.HouseKeeper;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.UMLFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

/**
 * Tests for the {@link TableLabelProvider} class.
 */
public class TableLabelProviderTest {

	@Rule
	public final HouseKeeper houseKeeper = new HouseKeeper();

	private ILabelProvider fixture;

	private Package package_;
	private PapyrusTable proto;
	private Table table;

	@Test
	public void getText_namedTable() {
		assertThat(fixture.getText(table), is("classes"));
	}

	@Test
	public void getText_unnamedTable() {
		table.setName(null);
		assertThat(fixture.getText(table), is("(Test Table of <Package> foo)"));
	}

	@Test
	public void getText_unnamedTableChangeContextName() {
		table.setName(null);
		assumeThat(fixture.getText(table), is("(Test Table of <Package> foo)"));
		package_.setName("bar");
		assumeThat(fixture.getText(table), is("(Test Table of <Package> bar)"));
	}

	@Test
	public void getText_namedTableNotifications() {
		boolean[] gotEvent = { false };
		ILabelProviderListener listener = event -> gotEvent[0] = gotEvent[0] || (event.getElement() == table);
		fixture.addListener(listener);

		fixture.getText(table); // Access it once to hook up the item adapters
		gotEvent[0] = false;

		table.setName("new name");

		assertThat("Label provider did not notify", gotEvent[0], is(true));
	}

	@Test
	public void getText_unnamedTableNotifications() {
		table.setName(null);

		boolean[] gotEvent = { false };
		ILabelProviderListener listener = event -> gotEvent[0] = gotEvent[0] || (event.getElement() == table);
		fixture.addListener(listener);

		fixture.getText(table); // Access it once to hook up the item adapters
		gotEvent[0] = false;

		package_.setName("bar");

		assertThat("Label provider did not notify", gotEvent[0], is(true));
	}

	@Test
	public void getText_selectionOfMultipleTables() {
		StructuredSelection selection = new StructuredSelection(new Object[] { table, table });

		assertThat(fixture.getText(selection), is("classes, classes"));
	}

	//
	// Test framework
	//

	@Before
	public void createFixture() {
		fixture = houseKeeper.cleanUpLater(new TableLabelProvider());

		ArchitectureDomain domain = ArchitectureFactory.eINSTANCE.createArchitectureDomain();
		domain.setName("Testing");
		
		ArchitectureDescriptionLanguage language = ArchitectureFactory.eINSTANCE.createArchitectureDescriptionLanguage();
		language.setId("Testing.TestTable");
		domain.getContexts().add(language);
		
		proto = RepresentationFactory.eINSTANCE.createPapyrusTable();
		proto.setName("Test Table");
		proto.setImplementationID("org.eclipse.papyrus.infra.nattable.tests.TestTable");
		proto.setConfiguration("TestTable");
		language.getRepresentationKinds().add(proto);
		
		ArchitectureViewpoint viewpoint = ArchitectureFactory.eINSTANCE.createArchitectureViewpoint();
		viewpoint.setId("tesing.TestTable.Testing");
		viewpoint.getRepresentationKinds().add(proto);
		language.getViewpoints().add(viewpoint);
		
		ArchitectureDomainManager.getInstance().getMerger().setDynamicDomains(Collections.singleton(domain));
		ArchitectureDomainManager.getInstance().getPreferences().setDefaultContextId("Testing.TestTable");
		
		package_ = UMLFactory.eINSTANCE.createPackage();
		package_.setName("foo");

		table = NattableFactory.eINSTANCE.createTable();
		TableVersioningUtils.stampCurrentVersion(table);
		table.setName("classes");
		table.setPrototype(proto);
		table.setContext(package_);
	}

	@After
	public void teardown() {
		ArchitectureDomainManager.getInstance().getMerger().setDynamicDomains(Collections.emptyList());
		ArchitectureDomainManager.getInstance().getPreferences().setDefaultContextId(TypeContext.getDefaultContextId());
	}
}

Back to the top