Skip to content

Package: AbstractRuleConditionDmrStrategyProviderTest$1

AbstractRuleConditionDmrStrategyProviderTest$1

nameinstructionbranchcomplexitylinemethod
getFailureMessage()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
test()
M: 0 C: 9
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
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.editor.handler;
15:
16: import static org.junit.Assert.assertFalse;
17: import static org.junit.Assert.assertTrue;
18:
19: import org.eclipse.emf.ecore.EAttribute;
20: import org.eclipse.emf.ecore.EClass;
21: import org.eclipse.emf.ecore.EObject;
22: import org.eclipse.emf.ecore.EReference;
23: import org.eclipse.emf.ecore.EcoreFactory;
24: import org.eclipse.emf.ecore.EcorePackage;
25: import org.eclipse.emf.ecore.resource.Resource;
26: import org.eclipse.emf.ecp.test.common.TestUtil;
27: import org.eclipse.emf.ecp.ui.view.swt.reference.ReferenceServiceCustomizationVendor;
28: import org.eclipse.emf.ecp.view.spi.model.VControl;
29: import org.eclipse.emf.ecp.view.spi.model.VView;
30: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
31: import org.eclipse.emf.ecp.view.spi.rule.model.LeafCondition;
32: import org.eclipse.emf.ecp.view.spi.rule.model.Rule;
33: import org.eclipse.emf.ecp.view.spi.rule.model.RuleFactory;
34: import org.eclipse.emf.ecp.view.spi.rule.model.RulePackage;
35: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTTestUtil;
36: import org.eclipse.swt.widgets.Display;
37: import org.eclipse.swt.widgets.Shell;
38: import org.eclipse.swtbot.swt.finder.SWTBot;
39: import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
40: import org.eclipse.swtbot.swt.finder.waits.Conditions;
41: import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
42: import org.junit.After;
43: import org.junit.Before;
44: import org.junit.Test;
45:
46: /**
47: * Abstract base classes for the tests for {@link LeafConditionDmrNewModelElementStrategyProvider} and
48: * {@link LeafConditionDmrOpenInNewContextStrategyProvider}.
49: *
50: * @author Lucas Koehler
51: */
52: public abstract class AbstractRuleConditionDmrStrategyProviderTest<T extends ReferenceServiceCustomizationVendor<?> & TestableStrategyProvider> {
53:
54:         private T strategyProvider;
55:         private EClass testEClass;
56:         // Use LeafCondition as the condition instance for these tests
57:         private LeafCondition leafCondition;
58:
59:         /** @return the tested strategy provider */
60:         protected T getStrategyProvider() {
61:                 return strategyProvider;
62:         }
63:
64:         /** @see #getStrategyProvider() */
65:         protected void setStrategyProvider(T strategyProvider) {
66:                 this.strategyProvider = strategyProvider;
67:         }
68:
69:         /** The {@link LeafCondition} to edit */
70:         protected LeafCondition getLeafCondition() {
71:                 return leafCondition;
72:         }
73:
74:         @Before
75:         public void setUp() {
76:                 testEClass = EcoreFactory.eINSTANCE.createEClass();
77:                 testEClass.setName("TestClass"); //$NON-NLS-1$
78:                 // ePackage.getEClassifiers().add(eClass);
79:                 final EReference eReference = EcoreFactory.eINSTANCE.createEReference();
80:                 eReference.setUpperBound(-1);
81:                 eReference.setName("TestReference"); //$NON-NLS-1$
82:                 eReference.setEType(EcorePackage.Literals.ECLASS);
83:                 testEClass.getEStructuralFeatures().add(eReference);
84:                 final EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
85:                 eAttribute.setName("TestAttribute");
86:                 eAttribute.setEType(EcorePackage.Literals.ESTRING);
87:                 testEClass.getEStructuralFeatures().add(eAttribute);
88:
89:                 // Create view model with a leaf condition
90:                 leafCondition = RuleFactory.eINSTANCE.createLeafCondition();
91:                 final Rule rule = RuleFactory.eINSTANCE.createEnableRule();
92:                 rule.setCondition(leafCondition);
93:                 final VControl control = VViewFactory.eINSTANCE.createControl();
94:                 control.getAttachments().add(rule);
95:                 final VView view = VViewFactory.eINSTANCE.createView();
96:                 view.setRootEClass(testEClass);
97:                 view.getChildren().add(control);
98:
99:                 // Add the view to a resource with editing domain
100:                 final Resource resource = TestUtil.createResourceWithEditingDomain();
101:                 resource.getContents().add(view);
102:
103:                 initStrategyProvider();
104:
105:                 // Wait for the UI harness to be ready before starting the test execution
106:                 UIThreadRunnable.syncExec(SWTTestUtil::waitForUIThread);
107:         }
108:
109:         @After
110:         public void tearDown() {
111:                 // Shell 0 and 1 must not be disposed. They belong to the UI harness around the tests
112:                 UIThreadRunnable.syncExec(Display.getDefault(), () -> {
113:                         final Shell[] shells = Display.getDefault().getShells();
114:                         for (int i = 2; i < shells.length; i++) {
115:                                 if (shells[i] != null && !shells[i].isDisposed()) {
116:                                         shells[i].dispose();
117:                                 }
118:                         }
119:                 });
120:         }
121:
122:         protected abstract void initStrategyProvider();
123:
124:         protected abstract boolean executeHandles(EObject owner, EReference reference);
125:
126:         @Test
127:         public void handles_generationEnabled_true() {
128:                 strategyProvider.setSegmentToolingEnabled(true);
129:                 assertTrue(executeHandles(leafCondition, RulePackage.Literals.LEAF_CONDITION__DOMAIN_MODEL_REFERENCE));
130:         }
131:
132:         @Test
133:         public void handles_generationEnabled_incorrectReference() {
134:                 strategyProvider.setSegmentToolingEnabled(true);
135:                 assertFalse(executeHandles(leafCondition, RulePackage.Literals.RULE__CONDITION));
136:         }
137:
138:         @Test
139:         public void handles_generationEnabled_incorrectParent() {
140:                 strategyProvider.setSegmentToolingEnabled(true);
141:                 assertFalse(executeHandles(VViewFactory.eINSTANCE.createControl(),
142:                         RulePackage.Literals.LEAF_CONDITION__DOMAIN_MODEL_REFERENCE));
143:         }
144:
145:         @Test
146:         public void handles_generationEnabled_incorrectBoth() {
147:                 strategyProvider.setSegmentToolingEnabled(true);
148:                 assertFalse(
149:                         executeHandles(VViewFactory.eINSTANCE.createControl(), EcorePackage.Literals.ECLASS__ESTRUCTURAL_FEATURES));
150:         }
151:
152:         @Test
153:         public void handles_generationDisabled() {
154:                 strategyProvider.setSegmentToolingEnabled(false);
155:                 assertFalse(executeHandles(leafCondition, RulePackage.Literals.LEAF_CONDITION__DOMAIN_MODEL_REFERENCE));
156:         }
157:
158:         //
159:         // Test utility
160:         /**
161:          * Waits until the {@link Shell} with the given title is active. The active shell is checked every 100 milli seconds
162:          * and the wait times out after 10 seconds.
163:          *
164:          * @param title The title of the {@link Shell}.
165:          */
166:         static void waitForShell(String title) {
167:                 new SWTBot().waitUntil(Conditions.shellIsActive(title), 10000, 100);
168:         }
169:
170:         /**
171:          * Waits until the element at index 0 of the given array is not null. The check is repeated every 100 milli seconds
172:          * and the wait times out after 20 seconds.
173:          *
174:          * @param array The array which will contain the wanted element at index 0
175:          * @return the result if the wait succeeds
176:          */
177:         static <T> T waitUntilNotNull(T[] array) {
178:                 new SWTBot().waitUntil(new DefaultCondition() {
179:
180:                         @Override
181:                         public boolean test() throws Exception {
182:•                                return array[0] != null;
183:                         }
184:
185:                         @Override
186:                         public String getFailureMessage() {
187:                                 return "Strategy did not return!"; //$NON-NLS-1$
188:                         }
189:                 }, 20000, 100);
190:                 return array[0];
191:         }
192: }