Skip to content

Package: LinkControlSWTRenderer_CrossReference_PTest

LinkControlSWTRenderer_CrossReference_PTest

nameinstructionbranchcomplexitylinemethod
LinkControlSWTRenderer_CrossReference_PTest()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
before()
M: 0 C: 169
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 30
100%
M: 0 C: 1
100%
createAndLinkButton_noReferenceStyleProperty()
M: 0 C: 46
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
createAndLinkButton_referenceStylePropertyFalse()
M: 8 C: 57
88%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 12
80%
M: 0 C: 1
100%
createAndLinkButton_referenceStylePropertyFalse_readOnly()
M: 8 C: 90
92%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 17
85%
M: 0 C: 1
100%
createAndLinkButton_referenceStylePropertyTrue()
M: 0 C: 66
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
linkButton_referenceStyleFalse()
M: 0 C: 66
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
linkButton_referenceStyleFalse_readonly()
M: 0 C: 99
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 17
100%
M: 0 C: 1
100%
linkExistingButton_noReferenceStyle()
M: 0 C: 46
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
mockControl()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
tearDown()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.internal.core.swt.renderer;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertFalse;
18: import static org.junit.Assert.assertTrue;
19: import static org.junit.Assert.fail;
20: import static org.mockito.Matchers.any;
21: import static org.mockito.Matchers.eq;
22: import static org.mockito.Mockito.mock;
23: import static org.mockito.Mockito.when;
24:
25: import java.util.Collections;
26: import java.util.NoSuchElementException;
27:
28: import org.eclipse.emf.ecore.EObject;
29: import org.eclipse.emf.ecore.EReference;
30: import org.eclipse.emf.ecp.test.common.DefaultRealm;
31: import org.eclipse.emf.ecp.view.internal.core.swt.MessageKeys;
32: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
33: import org.eclipse.emf.ecp.view.spi.model.VControl;
34: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
35: import org.eclipse.emf.ecp.view.spi.model.VElement;
36: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
37: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
38: import org.eclipse.emf.ecp.view.spi.util.swt.ImageRegistryService;
39: import org.eclipse.emf.ecp.view.template.model.VTStyleProperty;
40: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
41: import org.eclipse.emf.ecp.view.template.style.reference.model.VTReferenceFactory;
42: import org.eclipse.emf.ecp.view.template.style.reference.model.VTReferenceStyleProperty;
43: import org.eclipse.emf.ecp.view.test.common.swt.SWTTestUtil;
44: import org.eclipse.emf.emfstore.bowling.BowlingFactory;
45: import org.eclipse.emf.emfstore.bowling.BowlingPackage;
46: import org.eclipse.emfforms.spi.common.report.ReportService;
47: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
48: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
49: import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
50: import org.eclipse.emfforms.spi.core.services.label.NoLabelFoundException;
51: import org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService;
52: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
53: import org.eclipse.emfforms.swt.common.test.AbstractControl_PTest;
54: import org.eclipse.swt.layout.GridData;
55: import org.eclipse.swt.widgets.Button;
56: import org.eclipse.swt.widgets.Control;
57: import org.junit.After;
58: import org.junit.Before;
59: import org.junit.Test;
60:
61: /**
62: * Unit tests for the {@link LinkControlSWTRenderer} which use a non-containment {@link EReference}.
63: *
64: * @author Lucas Koehler
65: *
66: */
67: @SuppressWarnings("restriction")
68: public class LinkControlSWTRenderer_CrossReference_PTest extends AbstractControl_PTest<VControl> {
69:
70:         private DefaultRealm realm;
71:         private EReference eReference;
72:         private EObject eObject;
73:         private VTViewTemplateProvider templateProvider;
74:
75:         @Before
76:         public void before() throws DatabindingFailedException, NoLabelFoundException {
77:                 realm = new DefaultRealm();
78:                 final ReportService reportService = mock(ReportService.class);
79:                 final EMFFormsLocalizationService localizationService = mock(EMFFormsLocalizationService.class);
80:                 when(localizationService.getString(any(Class.class), any(String.class))).thenReturn("TEST"); //$NON-NLS-1$
81:                 when(localizationService.getString(any(Class.class), eq(MessageKeys.LinkControl_AddReference)))
82:                         .thenReturn("Link "); //$NON-NLS-1$
83:                 when(localizationService.getString(any(Class.class), eq(MessageKeys.LinkControl_NewReference)))
84:                         .thenReturn("Create and link new "); //$NON-NLS-1$
85:                 when(localizationService.getString(any(Class.class), eq(MessageKeys.LinkControl_DeleteReference)))
86:                         .thenReturn("Delete"); //$NON-NLS-1$
87:                 final ImageRegistryService imageRegistryService = mock(ImageRegistryService.class);
88:                 setDatabindingService(mock(EMFFormsDatabinding.class));
89:                 setLabelProvider(mock(EMFFormsLabelProvider.class));
90:                 templateProvider = mock(VTViewTemplateProvider.class);
91:                 setTemplateProvider(templateProvider);
92:                 setup();
93:                 setRenderer(new LinkControlSWTRenderer(getvControl(), getContext(), reportService, getDatabindingService(),
94:                         getLabelProvider(), getTemplateProvider(), localizationService, imageRegistryService));
95:                 getRenderer().init();
96:
97:                 final TestObservableValue observableValue = mock(TestObservableValue.class);
98:                 when(observableValue.getRealm()).thenReturn(realm);
99:                 when(observableValue.getValueType()).thenReturn(eReference);
100:                 when(observableValue.getObserved()).thenReturn(eObject);
101:                 when(getDatabindingService().getObservableValue(any(VDomainModelReference.class), any(EObject.class)))
102:                         .thenReturn(observableValue);
103:
104:                 final TestObservableValue labelObservable = mock(TestObservableValue.class);
105:                 when(labelObservable.getValue()).thenReturn("Player"); //$NON-NLS-1$
106:                 when(getLabelProvider().getDisplayName(any(VDomainModelReference.class), any(EObject.class)))
107:                         .thenReturn(labelObservable);
108:
109:         }
110:
111:         @After
112:         public void tearDown() {
113:                 realm.dispose();
114:                 dispose();
115:         }
116:
117:         @Override
118:         protected void mockControl() throws DatabindingFailedException {
119:                 eObject = BowlingFactory.eINSTANCE.createFan();
120:                 eReference = BowlingPackage.Literals.FAN__FAVOURITE_PLAYER;
121:                 super.mockControl(eObject, eReference);
122:         }
123:
124:         /**
125:          * By default, the 'create and link new' button should be shown for cross references (legacy behavior).
126:          */
127:         @Test
128:         public void createAndLinkButton_noReferenceStyleProperty()
129:                 throws DatabindingFailedException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
130:
131:                 final Control renderControl = renderControl(new SWTGridCell(0, 2, getRenderer()));
132:                 getRenderer().finalizeRendering(getShell());
133:
134:                 final Button linkButton = SWTTestUtil.findControl(renderControl, 0, Button.class);
135:                 assertEquals("Link Player", linkButton.getToolTipText()); //$NON-NLS-1$
136:                 final Button createAndLinkButton = SWTTestUtil.findControl(renderControl, 1, Button.class);
137:                 assertEquals("Create and link new Player", createAndLinkButton.getToolTipText()); //$NON-NLS-1$
138:                 final Button deleteButton = SWTTestUtil.findControl(renderControl, 2, Button.class);
139:                 assertEquals("Delete", deleteButton.getToolTipText()); //$NON-NLS-1$
140:         }
141:
142:         @Test
143:         public void createAndLinkButton_referenceStylePropertyTrue()
144:                 throws DatabindingFailedException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
145:
146:                 final VTReferenceStyleProperty property = VTReferenceFactory.eINSTANCE.createReferenceStyleProperty();
147:                 property.setShowCreateAndLinkButtonForCrossReferences(true);
148:                 when(templateProvider.getStyleProperties(any(VElement.class), any(ViewModelContext.class)))
149:                         .thenReturn(Collections.<VTStyleProperty> singleton(property));
150:
151:                 final Control renderControl = renderControl(new SWTGridCell(0, 2, getRenderer()));
152:                 getRenderer().finalizeRendering(getShell());
153:
154:                 final Button linkButton = SWTTestUtil.findControl(renderControl, 0, Button.class);
155:                 assertEquals("Link Player", linkButton.getToolTipText()); //$NON-NLS-1$
156:                 final Button createAndLinkButton = SWTTestUtil.findControl(renderControl, 1, Button.class);
157:                 assertEquals("Create and link new Player", createAndLinkButton.getToolTipText()); //$NON-NLS-1$
158:                 final Button deleteButton = SWTTestUtil.findControl(renderControl, 2, Button.class);
159:                 assertEquals("Delete", deleteButton.getToolTipText()); //$NON-NLS-1$
160:         }
161:
162:         @Test
163:         public void createAndLinkButton_referenceStylePropertyFalse()
164:                 throws DatabindingFailedException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
165:
166:                 final VTReferenceStyleProperty property = VTReferenceFactory.eINSTANCE.createReferenceStyleProperty();
167:                 property.setShowCreateAndLinkButtonForCrossReferences(false);
168:                 when(templateProvider.getStyleProperties(any(VElement.class), any(ViewModelContext.class)))
169:                         .thenReturn(Collections.<VTStyleProperty> singleton(property));
170:
171:                 final Control renderControl = renderControl(new SWTGridCell(0, 2, getRenderer()));
172:                 getRenderer().finalizeRendering(getShell());
173:
174:                 // Readonly => Buttons should be present but invisible
175:                 final Button linkButton = SWTTestUtil.findControl(renderControl, 0, Button.class);
176:                 assertEquals("Link Player", linkButton.getToolTipText()); //$NON-NLS-1$
177:                 final Button deleteButton = SWTTestUtil.findControl(renderControl, 1, Button.class);
178:                 assertEquals("Delete", deleteButton.getToolTipText()); //$NON-NLS-1$
179:                 try {
180:                         SWTTestUtil.findControl(renderControl, 2, Button.class);
181:                         fail(
182:                                 "There must not be a third button for a non-containment reference with disabled 'create and link' button."); //$NON-NLS-1$
183:                 } catch (final NoSuchElementException ex) {
184:                         // This is what we expect => Test is successful
185:                         // Cannot use expected in @Test annotation because the test must not succeed if the link or the delete
186:                         // button are not found.
187:                 }
188:         }
189:
190:         /**
191:          * Test that buttons are still created but invisible when the VControl is set to readonly.
192:          * This also implicitly tests that no null pointer exception is thrown if not all buttons are rendered.
193:          */
194:         @Test
195:         public void createAndLinkButton_referenceStylePropertyFalse_readOnly()
196:                 throws DatabindingFailedException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
197:
198:                 final VTReferenceStyleProperty property = VTReferenceFactory.eINSTANCE.createReferenceStyleProperty();
199:                 property.setShowCreateAndLinkButtonForCrossReferences(false);
200:                 when(templateProvider.getStyleProperties(any(VElement.class), any(ViewModelContext.class)))
201:                         .thenReturn(Collections.<VTStyleProperty> singleton(property));
202:                 when(getvControl().isEffectivelyReadonly()).thenReturn(true);
203:
204:                 final Control renderControl = renderControl(new SWTGridCell(0, 2, getRenderer()));
205:                 getRenderer().finalizeRendering(getShell());
206:
207:                 // Readonly => Buttons should be present but invisible
208:                 final Button linkButton = SWTTestUtil.findControl(renderControl, 0, Button.class);
209:                 assertEquals("Link Player", linkButton.getToolTipText()); //$NON-NLS-1$
210:                 assertFalse("Button visibility for readonly VControl.", linkButton.isVisible()); //$NON-NLS-1$
211:                 assertTrue("Button excluded from layout", GridData.class.cast(linkButton.getLayoutData()).exclude); //$NON-NLS-1$
212:                 final Button deleteButton = SWTTestUtil.findControl(renderControl, 1, Button.class);
213:                 assertEquals("Delete", deleteButton.getToolTipText()); //$NON-NLS-1$
214:                 assertFalse("Button visibility for readonly VControl.", deleteButton.isVisible()); //$NON-NLS-1$
215:                 assertTrue("Button excluded from layout", GridData.class.cast(deleteButton.getLayoutData()).exclude); //$NON-NLS-1$
216:                 try {
217:                         SWTTestUtil.findControl(renderControl, 2, Button.class);
218:                         fail(
219:                                 "There must not be a third button for a non-containment reference with disabled 'create and link' button."); //$NON-NLS-1$
220:                 } catch (final NoSuchElementException ex) {
221:                         // This is what we expect => Test is successful
222:                         // Cannot use expected in @Test annotation because the test must not succeed if the link or the delete
223:                         // button are not found.
224:                 }
225:         }
226:
227:         /** For cross references, the 'link' button must be shown by default (:= reference style == true). */
228:         @Test
229:         public void linkExistingButton_noReferenceStyle()
230:                 throws DatabindingFailedException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
231:                 final Control renderControl = renderControl(new SWTGridCell(0, 2, getRenderer()));
232:                 getRenderer().finalizeRendering(getShell());
233:
234:                 final Button linkButton = SWTTestUtil.findControl(renderControl, 0, Button.class);
235:                 assertEquals("Link Player", linkButton.getToolTipText()); //$NON-NLS-1$
236:                 final Button createAndLinkButton = SWTTestUtil.findControl(renderControl, 1, Button.class);
237:                 assertEquals("Create and link new Player", createAndLinkButton.getToolTipText()); //$NON-NLS-1$
238:                 final Button deleteButton = SWTTestUtil.findControl(renderControl, 2, Button.class);
239:                 assertEquals("Delete", deleteButton.getToolTipText()); //$NON-NLS-1$
240:         }
241:
242:         /**
243:          * For cross references, the 'link' button must also be shown if the reference style property
244:          * is set to false.
245:          */
246:         @Test
247:         public void linkButton_referenceStyleFalse()
248:                 throws DatabindingFailedException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
249:
250:                 final VTReferenceStyleProperty property = VTReferenceFactory.eINSTANCE.createReferenceStyleProperty();
251:                 property.setShowLinkButtonForContainmentReferences(false);
252:                 when(templateProvider.getStyleProperties(any(VElement.class), any(ViewModelContext.class)))
253:                         .thenReturn(Collections.<VTStyleProperty> singleton(property));
254:
255:                 final Control renderControl = renderControl(new SWTGridCell(0, 2, getRenderer()));
256:                 getRenderer().finalizeRendering(getShell());
257:
258:                 final Button linkButton = SWTTestUtil.findControl(renderControl, 0, Button.class);
259:                 assertEquals("Link Player", linkButton.getToolTipText()); //$NON-NLS-1$
260:                 final Button createAndLinkButton = SWTTestUtil.findControl(renderControl, 1, Button.class);
261:                 assertEquals("Create and link new Player", createAndLinkButton.getToolTipText()); //$NON-NLS-1$
262:                 final Button deleteButton = SWTTestUtil.findControl(renderControl, 2, Button.class);
263:                 assertEquals("Delete", deleteButton.getToolTipText()); //$NON-NLS-1$
264:         }
265:
266:         @Test
267:         public void linkButton_referenceStyleFalse_readonly()
268:                 throws DatabindingFailedException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
269:
270:                 final VTReferenceStyleProperty property = VTReferenceFactory.eINSTANCE.createReferenceStyleProperty();
271:                 property.setShowLinkButtonForContainmentReferences(false);
272:                 when(templateProvider.getStyleProperties(any(VElement.class), any(ViewModelContext.class)))
273:                         .thenReturn(Collections.<VTStyleProperty> singleton(property));
274:                 when(getvControl().isEffectivelyReadonly()).thenReturn(true);
275:                 final Control renderControl = renderControl(new SWTGridCell(0, 2, getRenderer()));
276:                 getRenderer().finalizeRendering(getShell());
277:
278:                 final Button linkButton = SWTTestUtil.findControl(renderControl, 0, Button.class);
279:                 assertFalse("Button visibility for readonly VControl.", linkButton.isVisible()); //$NON-NLS-1$
280:                 assertTrue("Button excluded from layout", GridData.class.cast(linkButton.getLayoutData()).exclude); //$NON-NLS-1$
281:                 final Button createAndLinkButton = SWTTestUtil.findControl(renderControl, 1, Button.class);
282:                 assertFalse("Button visibility for readonly VControl.", createAndLinkButton.isVisible()); //$NON-NLS-1$
283:                 assertTrue("Button excluded from layout", GridData.class.cast(createAndLinkButton.getLayoutData()).exclude); //$NON-NLS-1$
284:                 final Button deleteButton = SWTTestUtil.findControl(renderControl, 2, Button.class);
285:                 assertFalse("Button visibility for readonly VControl.", deleteButton.isVisible()); //$NON-NLS-1$
286:                 assertTrue("Button excluded from layout", GridData.class.cast(deleteButton.getLayoutData()).exclude); //$NON-NLS-1$
287:         }
288: }