Skip to content

Package: SWTValidationUiServiceImpl_Test

SWTValidationUiServiceImpl_Test

nameinstructionbranchcomplexitylinemethod
SWTValidationUiServiceImpl_Test()
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%
getValidationBackgroundColor()
M: 0 C: 90
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
getValidationBackgroundColor_caching()
M: 0 C: 85
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
getValidationBackgroundColor_emptyVDiagnostic()
M: 0 C: 56
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
getValidationBackgroundColor_nullDiagnostic()
M: 0 C: 37
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getValidationBackgroundColor_nullVDiagnostic()
M: 0 C: 53
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
getValidationForegroundColor()
M: 0 C: 90
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
getValidationForegroundColor_caching()
M: 0 C: 85
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
getValidationForegroundColor_emptyVDiagnostic()
M: 0 C: 56
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
getValidationForegroundColor_nullDiagnostic()
M: 0 C: 37
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getValidationForegroundColor_nullVDiagnostic()
M: 0 C: 53
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
getValidationIcon()
M: 0 C: 89
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
getValidationIcon_caching()
M: 0 C: 84
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
getValidationIcon_emptyVDiagnostic()
M: 0 C: 55
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
getValidationIcon_nullDiagnostic()
M: 0 C: 36
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getValidationIcon_nullVDiagnostic()
M: 0 C: 52
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
setUp()
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
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.emfforms.internal.swt.core.ui;
15:
16: import static org.junit.Assert.assertSame;
17: import static org.mockito.Mockito.mock;
18: import static org.mockito.Mockito.spy;
19: import static org.mockito.Mockito.times;
20: import static org.mockito.Mockito.verify;
21: import static org.mockito.Mockito.when;
22:
23: import org.eclipse.emf.common.util.Diagnostic;
24: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
25: import org.eclipse.emf.ecp.view.spi.model.VDiagnostic;
26: import org.eclipse.emf.ecp.view.spi.model.VElement;
27: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
28: import org.eclipse.emfforms.spi.swt.core.ui.SWTValidationHelper;
29: import org.eclipse.swt.graphics.Color;
30: import org.eclipse.swt.graphics.Image;
31: import org.eclipse.swt.widgets.Display;
32: import org.junit.Before;
33: import org.junit.Test;
34:
35: /**
36: * Unit tests for {@link SWTValidationUiServiceImpl}.
37: *
38: * @author Lucas Koehler
39: *
40: */
41: public class SWTValidationUiServiceImpl_Test {
42:
43:         private SWTValidationHelper validationHelper;
44:         private SWTValidationUiServiceImpl fixture;
45:         private VElement vElement;
46:         private ViewModelContext viewContext;
47:
48:         @Before
49:         public void setUp() {
50:                 validationHelper = mock(SWTValidationHelper.class);
51:                 fixture = new SWTValidationUiServiceImpl(validationHelper);
52:                 vElement = mock(VElement.class);
53:                 viewContext = mock(ViewModelContext.class);
54:         }
55:
56:         /**
57:          * Test that getValidationIcon(VElement, ViewModelContext) correctly delegates to getValidationIcon(Diagnostic,
58:          * VElement, ViewModelContext) and that the latter correctly delegates to SWTValidationHelper.
59:          */
60:         @Test
61:         public void getValidationIcon() {
62:                 // Spy to be able to verify method calls
63:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
64:                 final VDiagnostic vDiagnostic = VViewFactory.eINSTANCE.createDiagnostic();
65:                 final Diagnostic ok = mock(Diagnostic.class);
66:                 when(ok.getSeverity()).thenReturn(Diagnostic.OK);
67:                 final Diagnostic error = mock(Diagnostic.class);
68:                 when(error.getSeverity()).thenReturn(Diagnostic.ERROR);
69:                 vDiagnostic.getDiagnostics().add(ok);
70:                 vDiagnostic.getDiagnostics().add(error);
71:                 when(vElement.getDiagnostic()).thenReturn(vDiagnostic);
72:                 final Image expected = new Image(Display.getDefault(), 1, 1);
73:                 when(validationHelper.getValidationIcon(Diagnostic.ERROR, vElement, viewContext)).thenReturn(expected);
74:
75:                 final Image result = spiedFixture.getValidationIcon(vElement, viewContext);
76:
77:                 assertSame(expected, result);
78:                 verify(spiedFixture).getValidationIcon(error, vElement, viewContext);
79:         }
80:
81:         /** If the VElement doesn't have a VDiagnostic, it should be assumed that there is no validation error. */
82:         @Test
83:         public void getValidationIcon_nullVDiagnostic() {
84:                 // Spy to be able to verify method calls
85:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
86:                 when(vElement.getDiagnostic()).thenReturn(null);
87:                 final Image expected = new Image(Display.getDefault(), 1, 1);
88:                 when(validationHelper.getValidationIcon(Diagnostic.OK, vElement, viewContext)).thenReturn(expected);
89:
90:                 final Image result = spiedFixture.getValidationIcon(vElement, viewContext);
91:
92:                 assertSame(expected, result);
93:                 verify(spiedFixture).getValidationIcon(Diagnostic.OK_INSTANCE, vElement, viewContext);
94:         }
95:
96:         /**
97:          * If the VElement's VDiagnostic doesn't have any Diagnostics, it should be assumed that there is no validation
98:          * error.
99:          */
100:         @Test
101:         public void getValidationIcon_emptyVDiagnostic() {
102:                 // Spy to be able to verify method calls
103:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
104:                 final VDiagnostic vDiagnostic = VViewFactory.eINSTANCE.createDiagnostic();
105:                 when(vElement.getDiagnostic()).thenReturn(vDiagnostic);
106:                 final Image expected = new Image(Display.getDefault(), 1, 1);
107:                 when(validationHelper.getValidationIcon(Diagnostic.OK, vElement, viewContext)).thenReturn(expected);
108:
109:                 final Image result = spiedFixture.getValidationIcon(vElement, viewContext);
110:
111:                 assertSame(expected, result);
112:                 verify(spiedFixture).getValidationIcon(Diagnostic.OK_INSTANCE, vElement, viewContext);
113:         }
114:
115:         /**
116:          * If the given Diagnostic is null, it should be assumed that there is no validation error.
117:          */
118:         @Test
119:         public void getValidationIcon_nullDiagnostic() {
120:                 // Spy to be able to verify method calls
121:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
122:                 final Image expected = new Image(Display.getDefault(), 1, 1);
123:                 when(validationHelper.getValidationIcon(Diagnostic.OK, vElement, viewContext)).thenReturn(expected);
124:
125:                 final Image result = spiedFixture.getValidationIcon(null, vElement, viewContext);
126:
127:                 assertSame(expected, result);
128:         }
129:
130:         /** The service should cache icons by severity. */
131:         @Test
132:         public void getValidationIcon_caching() {
133:                 // Spy to be able to verify method calls
134:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
135:                 final Image expected = new Image(Display.getDefault(), 1, 1);
136:                 when(validationHelper.getValidationIcon(Diagnostic.OK, vElement, viewContext)).thenReturn(expected);
137:                 final Diagnostic ok1 = mock(Diagnostic.class);
138:                 when(ok1.getSeverity()).thenReturn(Diagnostic.OK);
139:                 final Diagnostic ok2 = mock(Diagnostic.class);
140:                 when(ok2.getSeverity()).thenReturn(Diagnostic.OK);
141:
142:                 final Image result1 = spiedFixture.getValidationIcon(ok1, vElement, viewContext);
143:                 final Image result2 = spiedFixture.getValidationIcon(ok2, vElement, viewContext);
144:
145:                 assertSame(expected, result1);
146:                 assertSame(expected, result2);
147:                 // if caching is used, the validation helper only needs to be called once
148:                 verify(validationHelper, times(1)).getValidationIcon(Diagnostic.OK, vElement, viewContext);
149:         }
150:
151:         /**
152:          * Test that getValidationForegroundColor(VElement, ViewModelContext) correctly delegates to
153:          * getValidationForegroundColor(Diagnostic, VElement, ViewModelContext) and that the latter correctly delegates to
154:          * SWTValidationHelper.
155:          */
156:         @Test
157:         public void getValidationForegroundColor() {
158:                 // Spy to be able to verify method calls
159:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
160:                 final VDiagnostic vDiagnostic = VViewFactory.eINSTANCE.createDiagnostic();
161:                 final Diagnostic ok = mock(Diagnostic.class);
162:                 when(ok.getSeverity()).thenReturn(Diagnostic.OK);
163:                 final Diagnostic error = mock(Diagnostic.class);
164:                 when(error.getSeverity()).thenReturn(Diagnostic.ERROR);
165:                 vDiagnostic.getDiagnostics().add(ok);
166:                 vDiagnostic.getDiagnostics().add(error);
167:                 when(vElement.getDiagnostic()).thenReturn(vDiagnostic);
168:                 final Color expected = new Color(Display.getDefault(), 1, 2, 3);
169:                 when(validationHelper.getValidationForegroundColor(Diagnostic.ERROR, vElement, viewContext))
170:                         .thenReturn(expected);
171:
172:                 final Color result = spiedFixture.getValidationForegroundColor(vElement, viewContext);
173:
174:                 assertSame(expected, result);
175:                 verify(spiedFixture).getValidationForegroundColor(error, vElement, viewContext);
176:         }
177:
178:         /** If the VElement doesn't have a VDiagnostic, it should be assumed that there is no validation error. */
179:         @Test
180:         public void getValidationForegroundColor_nullVDiagnostic() {
181:                 // Spy to be able to verify method calls
182:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
183:                 when(vElement.getDiagnostic()).thenReturn(null);
184:                 final Color expected = new Color(Display.getDefault(), 1, 2, 3);
185:                 when(validationHelper.getValidationForegroundColor(Diagnostic.OK, vElement, viewContext)).thenReturn(expected);
186:
187:                 final Color result = spiedFixture.getValidationForegroundColor(vElement, viewContext);
188:
189:                 assertSame(expected, result);
190:                 verify(spiedFixture).getValidationForegroundColor(Diagnostic.OK_INSTANCE, vElement, viewContext);
191:         }
192:
193:         /**
194:          * If the VElement's VDiagnostic doesn't have any Diagnostics, it should be assumed that there is no validation
195:          * error.
196:          */
197:         @Test
198:         public void getValidationForegroundColor_emptyVDiagnostic() {
199:                 // Spy to be able to verify method calls
200:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
201:                 final VDiagnostic vDiagnostic = VViewFactory.eINSTANCE.createDiagnostic();
202:                 when(vElement.getDiagnostic()).thenReturn(vDiagnostic);
203:                 final Color expected = new Color(Display.getDefault(), 1, 2, 3);
204:                 when(validationHelper.getValidationForegroundColor(Diagnostic.OK, vElement, viewContext)).thenReturn(expected);
205:
206:                 final Color result = spiedFixture.getValidationForegroundColor(vElement, viewContext);
207:
208:                 assertSame(expected, result);
209:                 verify(spiedFixture).getValidationForegroundColor(Diagnostic.OK_INSTANCE, vElement, viewContext);
210:         }
211:
212:         /**
213:          * If the given Diagnostic is null, it should be assumed that there is no validation error.
214:          */
215:         @Test
216:         public void getValidationForegroundColor_nullDiagnostic() {
217:                 // Spy to be able to verify method calls
218:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
219:                 final Color expected = new Color(Display.getDefault(), 1, 2, 3);
220:                 when(validationHelper.getValidationForegroundColor(Diagnostic.OK, vElement, viewContext)).thenReturn(expected);
221:
222:                 final Color result = spiedFixture.getValidationForegroundColor(null, vElement, viewContext);
223:
224:                 assertSame(expected, result);
225:         }
226:
227:         /** The service should cache icons by severity. */
228:         @Test
229:         public void getValidationForegroundColor_caching() {
230:                 // Spy to be able to verify method calls
231:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
232:                 final Color expected = new Color(Display.getDefault(), 1, 2, 3);
233:                 when(validationHelper.getValidationForegroundColor(Diagnostic.OK, vElement, viewContext)).thenReturn(expected);
234:                 final Diagnostic ok1 = mock(Diagnostic.class);
235:                 when(ok1.getSeverity()).thenReturn(Diagnostic.OK);
236:                 final Diagnostic ok2 = mock(Diagnostic.class);
237:                 when(ok2.getSeverity()).thenReturn(Diagnostic.OK);
238:
239:                 final Color result1 = spiedFixture.getValidationForegroundColor(ok1, vElement, viewContext);
240:                 final Color result2 = spiedFixture.getValidationForegroundColor(ok2, vElement, viewContext);
241:
242:                 assertSame(expected, result1);
243:                 assertSame(expected, result2);
244:                 // if caching is used, the validation helper only needs to be called once
245:                 verify(validationHelper, times(1)).getValidationForegroundColor(Diagnostic.OK, vElement, viewContext);
246:         }
247:
248:         /**
249:          * Test that getValidationBackgroundColor(VElement, ViewModelContext) correctly delegates to
250:          * getValidationBackgroundColor(Diagnostic, VElement, ViewModelContext) and that the latter correctly delegates to
251:          * SWTValidationHelper.
252:          */
253:         @Test
254:         public void getValidationBackgroundColor() {
255:                 // Spy to be able to verify method calls
256:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
257:                 final VDiagnostic vDiagnostic = VViewFactory.eINSTANCE.createDiagnostic();
258:                 final Diagnostic ok = mock(Diagnostic.class);
259:                 when(ok.getSeverity()).thenReturn(Diagnostic.OK);
260:                 final Diagnostic error = mock(Diagnostic.class);
261:                 when(error.getSeverity()).thenReturn(Diagnostic.ERROR);
262:                 vDiagnostic.getDiagnostics().add(ok);
263:                 vDiagnostic.getDiagnostics().add(error);
264:                 when(vElement.getDiagnostic()).thenReturn(vDiagnostic);
265:                 final Color expected = new Color(Display.getDefault(), 1, 2, 3);
266:                 when(validationHelper.getValidationBackgroundColor(Diagnostic.ERROR, vElement, viewContext))
267:                         .thenReturn(expected);
268:
269:                 final Color result = spiedFixture.getValidationBackgroundColor(vElement, viewContext);
270:
271:                 assertSame(expected, result);
272:                 verify(spiedFixture).getValidationBackgroundColor(error, vElement, viewContext);
273:         }
274:
275:         /** If the VElement doesn't have a VDiagnostic, it should be assumed that there is no validation error. */
276:         @Test
277:         public void getValidationBackgroundColor_nullVDiagnostic() {
278:                 // Spy to be able to verify method calls
279:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
280:                 when(vElement.getDiagnostic()).thenReturn(null);
281:                 final Color expected = new Color(Display.getDefault(), 1, 2, 3);
282:                 when(validationHelper.getValidationBackgroundColor(Diagnostic.OK, vElement, viewContext)).thenReturn(expected);
283:
284:                 final Color result = spiedFixture.getValidationBackgroundColor(vElement, viewContext);
285:
286:                 assertSame(expected, result);
287:                 verify(spiedFixture).getValidationBackgroundColor(Diagnostic.OK_INSTANCE, vElement, viewContext);
288:         }
289:
290:         /**
291:          * If the VElement's VDiagnostic doesn't have any Diagnostics, it should be assumed that there is no validation
292:          * error.
293:          */
294:         @Test
295:         public void getValidationBackgroundColor_emptyVDiagnostic() {
296:                 // Spy to be able to verify method calls
297:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
298:                 final VDiagnostic vDiagnostic = VViewFactory.eINSTANCE.createDiagnostic();
299:                 when(vElement.getDiagnostic()).thenReturn(vDiagnostic);
300:                 final Color expected = new Color(Display.getDefault(), 1, 2, 3);
301:                 when(validationHelper.getValidationBackgroundColor(Diagnostic.OK, vElement, viewContext)).thenReturn(expected);
302:
303:                 final Color result = spiedFixture.getValidationBackgroundColor(vElement, viewContext);
304:
305:                 assertSame(expected, result);
306:                 verify(spiedFixture).getValidationBackgroundColor(Diagnostic.OK_INSTANCE, vElement, viewContext);
307:         }
308:
309:         /**
310:          * If the given Diagnostic is null, it should be assumed that there is no validation error.
311:          */
312:         @Test
313:         public void getValidationBackgroundColor_nullDiagnostic() {
314:                 // Spy to be able to verify method calls
315:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
316:                 final Color expected = new Color(Display.getDefault(), 1, 2, 3);
317:                 when(validationHelper.getValidationBackgroundColor(Diagnostic.OK, vElement, viewContext)).thenReturn(expected);
318:
319:                 final Color result = spiedFixture.getValidationBackgroundColor(null, vElement, viewContext);
320:
321:                 assertSame(expected, result);
322:         }
323:
324:         /** The service should cache icons by severity. */
325:         @Test
326:         public void getValidationBackgroundColor_caching() {
327:                 // Spy to be able to verify method calls
328:                 final SWTValidationUiServiceImpl spiedFixture = spy(fixture);
329:                 final Color expected = new Color(Display.getDefault(), 1, 2, 3);
330:                 when(validationHelper.getValidationBackgroundColor(Diagnostic.OK, vElement, viewContext)).thenReturn(expected);
331:                 final Diagnostic ok1 = mock(Diagnostic.class);
332:                 when(ok1.getSeverity()).thenReturn(Diagnostic.OK);
333:                 final Diagnostic ok2 = mock(Diagnostic.class);
334:                 when(ok2.getSeverity()).thenReturn(Diagnostic.OK);
335:
336:                 final Color result1 = spiedFixture.getValidationBackgroundColor(ok1, vElement, viewContext);
337:                 final Color result2 = spiedFixture.getValidationBackgroundColor(ok2, vElement, viewContext);
338:
339:                 assertSame(expected, result1);
340:                 assertSame(expected, result2);
341:                 // if caching is used, the validation helper only needs to be called once
342:                 verify(validationHelper, times(1)).getValidationBackgroundColor(Diagnostic.OK, vElement, viewContext);
343:         }
344: }