Skip to content

Package: VElement_Test$DefaultState

VElement_Test$DefaultState

nameinstructionbranchcomplexitylinemethod
VElement_Test.DefaultState()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
defaultState()
M: 50 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017 Christian W. Damus 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: * Christian W. Damus - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.model.test;
15:
16: import static org.hamcrest.CoreMatchers.is;
17: import static org.hamcrest.MatcherAssert.assertThat;
18:
19: import java.util.ArrayList;
20: import java.util.Arrays;
21: import java.util.List;
22:
23: import org.eclipse.emf.ecp.view.spi.group.model.VGroup;
24: import org.eclipse.emf.ecp.view.spi.group.model.VGroupFactory;
25: import org.eclipse.emf.ecp.view.spi.model.VControl;
26: import org.eclipse.emf.ecp.view.spi.model.VElement;
27: import org.eclipse.emf.ecp.view.spi.model.VView;
28: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
29: import org.junit.Before;
30: import org.junit.Test;
31: import org.junit.experimental.runners.Enclosed;
32: import org.junit.runner.RunWith;
33: import org.junit.runners.JUnit4;
34: import org.junit.runners.Parameterized;
35: import org.junit.runners.Parameterized.Parameters;
36:
37: /**
38: * Tests for the {@link VElement} custom code.
39: *
40: * @author Christian W. Damus
41: */
42: @RunWith(Enclosed.class)
43: public abstract class VElement_Test {
44:
45:         // BEGIN COMPLEX CODE - visibility for nested classes
46:         VElement fixture;
47:         VElement parent;
48:         VElement root;
49:         // END COMPLEX CODE
50:
51:         @Before
52:         public void createFixture() {
53:                 root = VViewFactory.eINSTANCE.createView();
54:                 parent = VGroupFactory.eINSTANCE.createGroup();
55:                 ((VView) root).getChildren().add((VGroup) parent);
56:                 fixture = VViewFactory.eINSTANCE.createControl();
57:                 ((VGroup) parent).getChildren().add((VControl) fixture);
58:         }
59:
60:         //
61:         // Test partitions
62:         //
63:
64:         @RunWith(JUnit4.class)
65:         public static class DefaultState extends VElement_Test {
66:                 /**
67:                  * Initialies me.
68:                  */
69:                 public DefaultState() {
70:                         super();
71:                 }
72:
73:                 @Test
74:                 public void defaultState() {
75:•                        for (final VElement next : Arrays.asList(fixture, parent, root)) {
76:                                 assertThat(next.isEffectivelyEnabled(), is(true));
77:                                 assertThat(next.isEffectivelyVisible(), is(true));
78:                                 assertThat(next.isEffectivelyReadonly(), is(false));
79:                         }
80:                 }
81:         }
82:
83:         @RunWith(Parameterized.class)
84:         public static class EffectiveState extends VElement_Test {
85:                 private final Which which;
86:                 private final What what;
87:
88:                 /**
89:                  * Initializes me with {@code which} element is to be made explicitly
90:                  * {@code what}.
91:                  *
92:                  * @param which which element is to be made
93:                  * @param what what
94:                  */
95:                 public EffectiveState(Which which, What what) {
96:                         super();
97:
98:                         this.which = which;
99:                         this.what = what;
100:                 }
101:
102:                 /**
103:                  * Asserts that the target fixture is effectively what it is explicitly set to.
104:                  */
105:                 @Test
106:                 public void explicitlyWhat() {
107:                         assertThat(which.name() + " is not effectively " + what,
108:                                 what.isEffectivelyWhat(which.get(this)));
109:                 }
110:
111:                 /**
112:                  * Asserts that the target fixture is effectively what some container is
113:                  * explicitly set to.
114:                  */
115:                 @Test
116:                 public void effectivelyWhat() {
117:                         if (which == Which.FIXTURE) {
118:                                 // This is the explicit case, tested separately, so paranoiac check that
119:                                 // we don't somehow infer state down the tree from containers
120:                                 assertThat("root is effectively " + what,
121:                                         !what.isEffectivelyWhat(root));
122:                         } else {
123:                                 assertThat("fixture is not effectively " + what,
124:                                         what.isEffectivelyWhat(fixture));
125:                         }
126:                 }
127:
128:                 //
129:                 // Test framework
130:                 //
131:
132:                 @Parameters(name = "{index}: {0}, {1}")
133:                 public static Iterable<Object[]> parameters() {
134:                         final List<Object[]> result = new ArrayList<Object[]>(Which.values().length * What.values().length);
135:
136:                         for (final Which which : Which.values()) {
137:                                 for (final What what : What.values()) {
138:                                         result.add(new Object[] { which, what });
139:                                 }
140:                         }
141:
142:                         return result;
143:                 }
144:
145:                 @Override
146:                 @Before
147:                 public void createFixture() {
148:                         super.createFixture();
149:
150:                         what.setWhat(which.get(this));
151:                 }
152:
153:                 enum Which {
154:                         FIXTURE, PARENT, ROOT;
155:
156:                         VElement get(EffectiveState test) {
157:                                 switch (this) {
158:                                 case FIXTURE:
159:                                         return test.fixture;
160:                                 case PARENT:
161:                                         return test.parent;
162:                                 case ROOT:
163:                                         return test.root;
164:                                 default:
165:                                         throw new LinkageError("Missing case for " + this); //$NON-NLS-1$
166:                                 }
167:                         }
168:                 }
169:
170:                 enum What {
171:                         READ_ONLY, DISABLED, INVISIBLE;
172:
173:                         void setWhat(VElement element) {
174:                                 switch (this) {
175:                                 case READ_ONLY:
176:                                         element.setReadonly(true);
177:                                         break;
178:                                 case INVISIBLE:
179:                                         element.setVisible(false);
180:                                         break;
181:                                 case DISABLED:
182:                                         element.setEnabled(false);
183:                                         break;
184:                                 default:
185:                                         throw new LinkageError("Missing case for " + this); //$NON-NLS-1$
186:                                 }
187:                         }
188:
189:                         boolean isEffectivelyWhat(VElement element) {
190:                                 switch (this) {
191:                                 case READ_ONLY:
192:                                         return element.isEffectivelyReadonly();
193:                                 case INVISIBLE:
194:                                         return !element.isEffectivelyVisible();
195:                                 case DISABLED:
196:                                         return !element.isEffectivelyEnabled();
197:                                 default:
198:                                         throw new LinkageError("Missing case for " + this); //$NON-NLS-1$
199:                                 }
200:                         }
201:                 }
202:         }
203: }