Skip to content

Package: GridControlRenderer_PTest$1

GridControlRenderer_PTest$1

nameinstructionbranchcomplexitylinemethod
describeTo(Description)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
matches(Object)
M: 0 C: 19
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
{...}
M: 0 C: 9
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: * Mat Hansen - initial API and implementation
13: * Christian W. Damus - bug 534829
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.internal.table.nebula.grid;
16:
17: import static org.hamcrest.CoreMatchers.hasItem;
18: import static org.hamcrest.CoreMatchers.is;
19: import static org.junit.Assert.assertEquals;
20: import static org.junit.Assert.assertFalse;
21: import static org.junit.Assert.assertThat;
22: import static org.junit.Assert.assertTrue;
23: import static org.mockito.Matchers.any;
24: import static org.mockito.Mockito.mock;
25: import static org.mockito.Mockito.when;
26:
27: import java.util.ArrayList;
28: import java.util.Arrays;
29: import java.util.Collections;
30: import java.util.LinkedHashMap;
31: import java.util.LinkedHashSet;
32: import java.util.List;
33: import java.util.Map;
34: import java.util.Map.Entry;
35: import java.util.Set;
36:
37: import org.eclipse.core.databinding.observable.Observables;
38: import org.eclipse.core.databinding.observable.list.WritableList;
39: import org.eclipse.core.databinding.observable.value.IObservableValue;
40: import org.eclipse.emf.common.util.BasicEList;
41: import org.eclipse.emf.databinding.IEMFValueProperty;
42: import org.eclipse.emf.databinding.internal.EMFValueProperty;
43: import org.eclipse.emf.databinding.internal.EMFValuePropertyDecorator;
44: import org.eclipse.emf.ecore.EClass;
45: import org.eclipse.emf.ecore.EObject;
46: import org.eclipse.emf.ecore.EStructuralFeature;
47: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
48: import org.eclipse.emf.ecp.test.common.DefaultRealm;
49: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
50: import org.eclipse.emf.ecp.view.spi.model.LabelAlignment;
51: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
52: import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
53: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
54: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
55: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
56: import org.eclipse.emf.ecp.view.spi.table.model.VTableColumnConfiguration;
57: import org.eclipse.emf.ecp.view.spi.table.model.VTableControl;
58: import org.eclipse.emf.ecp.view.spi.table.model.VTableDomainModelReference;
59: import org.eclipse.emf.ecp.view.spi.table.model.VTableFactory;
60: import org.eclipse.emf.ecp.view.spi.table.nebula.grid.GridControlSWTRenderer;
61: import org.eclipse.emf.ecp.view.spi.table.nebula.grid.GridTableViewerComposite;
62: import org.eclipse.emf.ecp.view.spi.table.nebula.grid.messages.Messages;
63: import org.eclipse.emf.ecp.view.spi.table.swt.action.AddRowAction;
64: import org.eclipse.emf.ecp.view.spi.table.swt.action.DuplicateRowAction;
65: import org.eclipse.emf.ecp.view.spi.table.swt.action.RemoveRowAction;
66: import org.eclipse.emf.ecp.view.spi.util.swt.ImageRegistryService;
67: import org.eclipse.emf.ecp.view.table.ui.nebula.grid.test.model.audit.AuditFactory;
68: import org.eclipse.emf.ecp.view.table.ui.nebula.grid.test.model.audit.AuditPackage;
69: import org.eclipse.emf.ecp.view.table.ui.nebula.grid.test.model.audit.Member;
70: import org.eclipse.emf.ecp.view.table.ui.nebula.grid.test.model.audit.Organization;
71: import org.eclipse.emf.ecp.view.table.ui.nebula.grid.test.model.audit.RegisteredUser;
72: import org.eclipse.emf.ecp.view.template.model.VTStyleProperty;
73: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
74: import org.eclipse.emf.ecp.view.template.style.keybinding.model.VTKeyBinding;
75: import org.eclipse.emf.ecp.view.template.style.keybinding.model.VTKeyBindings;
76: import org.eclipse.emf.ecp.view.template.style.keybinding.model.VTKeybindingFactory;
77: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTTestUtil;
78: import org.eclipse.emf.edit.domain.EditingDomain;
79: import org.eclipse.emfforms.common.Optional;
80: import org.eclipse.emfforms.spi.common.converter.EStructuralFeatureValueConverterService;
81: import org.eclipse.emfforms.spi.common.report.ReportService;
82: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
83: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
84: import org.eclipse.emfforms.spi.core.services.editsupport.EMFFormsEditSupport;
85: import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
86: import org.eclipse.emfforms.spi.core.services.label.NoLabelFoundException;
87: import org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService;
88: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
89: import org.eclipse.emfforms.spi.swt.table.AbstractTableViewerComposite;
90: import org.eclipse.emfforms.spi.swt.table.ColumnConfiguration;
91: import org.eclipse.emfforms.spi.swt.table.TableConfiguration;
92: import org.eclipse.emfforms.swt.common.test.AbstractControl_PTest;
93: import org.eclipse.nebula.widgets.grid.Grid;
94: import org.eclipse.nebula.widgets.grid.GridColumn;
95: import org.eclipse.nebula.widgets.grid.GridItem;
96: import org.eclipse.swt.SWT;
97: import org.eclipse.swt.graphics.Rectangle;
98: import org.eclipse.swt.widgets.Composite;
99: import org.eclipse.swt.widgets.Control;
100: import org.eclipse.swt.widgets.Display;
101: import org.eclipse.swt.widgets.Event;
102: import org.eclipse.swt.widgets.Menu;
103: import org.eclipse.swt.widgets.MenuItem;
104: import org.eclipse.swt.widgets.Shell;
105: import org.hamcrest.BaseMatcher;
106: import org.hamcrest.Description;
107: import org.junit.After;
108: import org.junit.Assert;
109: import org.junit.Before;
110: import org.junit.Test;
111: import org.junit.runner.RunWith;
112: import org.junit.runners.Parameterized;
113: import org.junit.runners.Parameterized.Parameter;
114: import org.junit.runners.Parameterized.Parameters;
115: import org.mockito.Matchers;
116: import org.mockito.Mockito;
117:
118: /**
119: * Plugin Test for Grid Table.
120: *
121: * Uses two different data sets:
122: * 1) SimpleDataSet, consist of three members of the same type (RegisteredUser)
123: * * which have three fields each: name, isActive, joinDate
124: * 2) ComplexDataSet, consist of different member types (RegisteredUser, AdminUser, Bot)
125: * * which have one unique field each: login, executionInterval, createdBy
126: *
127: * @author Mat Hansen <mhansen@eclipsesource.com>
128: *
129: */
130: @SuppressWarnings("restriction")
131: @RunWith(Parameterized.class)
132: public class GridControlRenderer_PTest extends AbstractControl_PTest<VTableControl> {
133:
134:         private DefaultRealm realm;
135:
136:         private static final String MEMBER1_NAME = "Kliver Oahn";
137:         private static final String MEMBER2_NAME = "Branz Feckenbauer";
138:         private static final String MEMBER3_NAME = "Vudi Roeller";
139:
140:         private static final Map<String, EClass> MEMBERS;
141:         static {
142:
143:                 final Map<String, EClass> tmp = new LinkedHashMap<String, EClass>();
144:                 tmp.put(MEMBER1_NAME, AuditPackage.eINSTANCE.getRegisteredUser());
145:                 tmp.put(MEMBER2_NAME, AuditPackage.eINSTANCE.getAdminUser());
146:                 tmp.put(MEMBER3_NAME, AuditPackage.eINSTANCE.getBot());
147:                 MEMBERS = Collections.unmodifiableMap(tmp);
148:         }
149:
150:         private int expectedRows = -1;
151:         private int expectedColumns = -1;
152:
153:         public enum DataSet {
154:                 Simple, Complex
155:         }
156:
157:         // BEGIN COMPLEX CODE
158:         // Field must be public for JUnit's @Parameter to work: https://github.com/junit-team/junit4/pull/737
159:         @Parameter
160:         public DataSet dataset;
161:         // END COMPLEX CODE
162:
163:         @Parameters(name = "{0}")
164:         public static DataSet[] data() {
165:                 return DataSet.values();
166:         }
167:
168:         @Before
169:         public void before() throws DatabindingFailedException {
170:                 realm = new DefaultRealm();
171:                 final ReportService reportService = mock(ReportService.class);
172:
173:                 setDatabindingService(mock(EMFFormsDatabindingEMF.class));
174:                 setLabelProvider(mock(EMFFormsLabelProvider.class));
175:                 setTemplateProvider(mock(VTViewTemplateProvider.class));
176:
177:                 final EMFFormsEditSupport editSupport = mock(EMFFormsEditSupport.class);
178:                 final EStructuralFeatureValueConverterService converterService = mock(
179:                         EStructuralFeatureValueConverterService.class);
180:                 final ImageRegistryService imageRegistryService = mock(ImageRegistryService.class);
181:                 final EMFFormsLocalizationService localizationService = mock(EMFFormsLocalizationService.class);
182:
183:                 setup();
184:
185:                 setRenderer(new GridControlSWTRenderer(
186:                         getvControl(), getContext(), reportService,
187:                         (EMFFormsDatabindingEMF) getDatabindingService(),
188:                         getLabelProvider(), getTemplateProvider(), imageRegistryService, editSupport, converterService,
189:                         localizationService));
190:
191:                 getRenderer().init();
192:         }
193:
194:         @After
195:         public void testTearDown() {
196:                 realm.dispose();
197:                 dispose();
198:         }
199:
200:         @Override
201:         protected void setup() throws DatabindingFailedException {
202:                 setvControl(Mockito.mock(VTableControl.class));
203:                 mockControl();
204:                 setContext(Mockito.mock(ViewModelContext.class));
205:                 when(getContext().getDomainModel()).thenReturn(mock(EObject.class));
206:                 setShell(new Shell(Display.getDefault(), SWT.NONE));
207:
208:                 final IObservableValue testDescription = Observables.constantObservableValue("test-description", String.class);
209:                 final IObservableValue testDisplayName = Observables.constantObservableValue("test-displayname", String.class);
210:
211:                 try {
212:
213:                         when(getLabelProvider().getDisplayName(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
214:                                 Observables.constantObservableValue("antiException", String.class));
215:                         setMockLabelAlignment(LabelAlignment.LEFT);
216:
217:                         when(getLabelProvider().getDescription(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
218:                                 testDescription);
219:                         when(getLabelProvider().getDescription(any(VDomainModelReference.class), any(EClass.class))).thenReturn(
220:                                 testDescription);
221:                         when(getLabelProvider().getDisplayName(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
222:                                 testDisplayName);
223:                         when(getLabelProvider().getDisplayName(any(VDomainModelReference.class), any(EClass.class))).thenReturn(
224:                                 testDisplayName);
225:                 } catch (final NoLabelFoundException ex) {
226:                         Assert.fail(ex.getMessage());
227:                 }
228:
229:                 when(getvControl().getColumnConfigurations())
230:                         .thenReturn(new BasicEList<VTableColumnConfiguration>());
231:
232:         }
233:
234:         @Override
235:         protected void mockControl() throws DatabindingFailedException {
236:
237:                 final TestObservableValue mockedObservableValue = mock(TestObservableValue.class);
238:
239:                 when(mockedObservableValue.getRealm()).thenReturn(realm);
240:                 final EObject mockedEObject = mock(EObject.class);
241:                 when(mockedEObject.eIsSet(any(EStructuralFeature.class))).thenReturn(true);
242:                 when(mockedObservableValue.getObserved()).thenReturn(mockedEObject);
243:                 final EStructuralFeature mockedEStructuralFeature = mock(EStructuralFeature.class);
244:                 when(mockedEStructuralFeature.isUnsettable()).thenReturn(false);
245:                 when(mockedObservableValue.getValueType()).thenReturn(mockedEStructuralFeature);
246:                 when(getDatabindingService().getObservableValue(any(VDomainModelReference.class), any(EObject.class)))
247:                         .thenReturn(
248:                                 mockedObservableValue);
249:
250:                 final EStructuralFeature structuralFeature = mock(EStructuralFeature.class);
251:                 when(structuralFeature.isChangeable()).thenReturn(true);
252:                 final IEMFValueProperty valueProperty = mock(IEMFValueProperty.class);
253:                 when(valueProperty.getValueType()).thenReturn(structuralFeature);
254:                 when(getDatabindingService().getValueProperty(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
255:                         valueProperty);
256:
257:                 when(getvControl().getColumnConfigurations())
258:                         .thenReturn(new BasicEList<VTableColumnConfiguration>());
259:
260:         }
261:
262:         private void mockDataset() throws DatabindingFailedException {
263:
264:                 switch (dataset) {
265:                 case Simple:
266:                         mockDataSetSimple();
267:                         break;
268:                 case Complex:
269:                         mockDataSetComplex();
270:                         break;
271:                 default:
272:                         throw new IllegalArgumentException("Unsupported dataset");
273:                 }
274:
275:         }
276:
277:         /**
278:          * Provide a simple data set which has no abstract types.
279:          */
280:         protected void mockDataSetSimple() throws DatabindingFailedException {
281:                 final Organization org = AuditFactory.eINSTANCE.createOrganization();
282:
283:                 // add members
284:                 for (final String memberName : MEMBERS.keySet()) {
285:                         final Member member = AuditFactory.eINSTANCE.createRegisteredUser();
286:                         member.setName(memberName);
287:                         org.getMembers().add(member);
288:
289:                         mockSetting(mock(Setting.class), member, AuditPackage.eINSTANCE.getMember_Name());
290:                 }
291:
292:                 final WritableList<Member> memberList = new WritableList<Member>(org.getMembers(),
293:                         AuditPackage.eINSTANCE.getOrganization_Members());
294:
295:                 when(getDatabindingService().getObservableList(any(VDomainModelReference.class), any(EObject.class)))
296:                         .thenReturn(
297:                                 memberList);
298:
299:                 final Setting memberSetting = mock(Setting.class);
300:                 mockSetting(memberSetting, org, AuditPackage.eINSTANCE.getOrganization_Members());
301:
302:                 final VTableDomainModelReference tableDomainModelReference = VTableFactory.eINSTANCE
303:                         .createTableDomainModelReference();
304:                 tableDomainModelReference.setDomainModelEFeature(AuditPackage.eINSTANCE.getOrganization_Members());
305:
306:                 // add three columns
307:                 for (final EStructuralFeature eStructuralFeature : Arrays.asList(
308:                         AuditPackage.eINSTANCE.getMember_Name(),
309:                         AuditPackage.eINSTANCE.getMember_IsActive(),
310:                         AuditPackage.eINSTANCE.getMember_JoinDate() //
311:                 )) {
312:
313:                         final VFeaturePathDomainModelReference dmr = //
314:                                 VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
315:                         dmr.setDomainModelEFeature(eStructuralFeature);
316:                         tableDomainModelReference.getColumnDomainModelReferences().add(dmr);
317:
318:                         mockColumnFeature(dmr);
319:                 }
320:
321:                 when(((EMFFormsDatabindingEMF) getDatabindingService())
322:                         .getSetting(any(VDomainModelReference.class),
323:                                 any(EObject.class)))
324:                                         .thenReturn(memberSetting);
325:
326:                 when(getvControl().getDomainModelReference()).thenReturn(
327:                         tableDomainModelReference);
328:
329:                 expectedRows = MEMBERS.size();
330:                 expectedColumns = 3;
331:         }
332:
333:         protected void mockDataSetComplex() throws DatabindingFailedException {
334:                 final Organization org = AuditFactory.eINSTANCE.createOrganization();
335:
336:                 // add three players
337:                 for (final Entry<String, EClass> m : MEMBERS.entrySet()) {
338:
339:                         final String name = m.getKey();
340:                         final Member member = (Member) AuditFactory.eINSTANCE.create(m.getValue());
341:                         member.setName(name);
342:
343:                         if (RegisteredUser.class.isInstance(member)) { // will include MEMBER1 and MEMBER2, as MEMBER3 is a Bot
344:                                 final RegisteredUser user = RegisteredUser.class.cast(member);
345:                                 user.setLogin(name.toLowerCase().replaceAll(" ", ""));
346:                         }
347:
348:                         org.getMembers().add(member);
349:
350:                         mockSetting(mock(Setting.class), member, AuditPackage.eINSTANCE.getMember_Name());
351:                 }
352:
353:                 final WritableList<Member> memberList = new WritableList<Member>(org.getMembers(),
354:                         AuditPackage.eINSTANCE.getOrganization_Members());
355:
356:                 when(getDatabindingService().getObservableList(any(VDomainModelReference.class), any(EObject.class)))
357:                         .thenReturn(
358:                                 memberList);
359:
360:                 final Setting memberSetting = mock(Setting.class);
361:                 mockSetting(memberSetting, org, AuditPackage.eINSTANCE.getOrganization_Members());
362:
363:                 final VTableDomainModelReference tableDomainModelReference = VTableFactory.eINSTANCE
364:                         .createTableDomainModelReference();
365:                 tableDomainModelReference.setDomainModelEFeature(AuditPackage.eINSTANCE.getOrganization_Members());
366:
367:                 // add three columns
368:                 for (final EStructuralFeature eStructuralFeature : Arrays.asList(
369:                         AuditPackage.eINSTANCE.getMember_Name(),
370:                         AuditPackage.eINSTANCE.getMember_IsActive(),
371:                         AuditPackage.eINSTANCE.getMember_JoinDate() //
372:                 )) {
373:
374:                         final VFeaturePathDomainModelReference dmr = //
375:                                 VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
376:                         dmr.setDomainModelEFeature(eStructuralFeature);
377:                         tableDomainModelReference.getColumnDomainModelReferences().add(dmr);
378:
379:                         mockColumnFeature(dmr);
380:                 }
381:
382:                 for (final EStructuralFeature eStructuralFeature : Arrays.asList(
383:                         AuditPackage.eINSTANCE.getRegisteredUser_Login(),
384:                         AuditPackage.eINSTANCE.getBot_ExecutionIntervalSeconds(),
385:                         AuditPackage.eINSTANCE.getAdminUser_CreatedBy() //
386:                 )) {
387:
388:                         final VFeaturePathDomainModelReference dmr = //
389:                                 VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
390:                         dmr.setDomainModelEFeature(eStructuralFeature);
391:                         tableDomainModelReference.getColumnDomainModelReferences().add(dmr);
392:
393:                         mockColumnFeature(dmr);
394:                 }
395:
396:                 when(((EMFFormsDatabindingEMF) getDatabindingService())
397:                         .getSetting(any(VDomainModelReference.class),
398:                                 any(EObject.class)))
399:                                         .thenReturn(memberSetting);
400:
401:                 when(getvControl().getDomainModelReference()).thenReturn(
402:                         tableDomainModelReference);
403:
404:                 expectedRows = MEMBERS.size();
405:                 expectedColumns = 6;
406:         }
407:
408:         /**
409:          * @param dmr
410:          * @throws DatabindingFailedException
411:          */
412:         private void mockColumnFeature(final VFeaturePathDomainModelReference dmr) throws DatabindingFailedException {
413:                 when(EMFFormsDatabindingEMF.class.cast(getDatabindingService())
414:                         .getValueProperty(Matchers.argThat(new BaseMatcher<VDomainModelReference>() {
415:
416:                                 @Override
417:                                 public boolean matches(Object item) {
418:•                                        if (item == null) {
419:                                                 return false;
420:                                         }
421:                                         final VFeaturePathDomainModelReference cast = VFeaturePathDomainModelReference.class.cast(item);
422:•                                        return cast.getDomainModelEFeature() == dmr.getDomainModelEFeature();
423:                                 }
424:
425:                                 @Override
426:                                 public void describeTo(Description description) {
427:                                 }
428:
429:                         }), any(EClass.class), any(EditingDomain.class)))
430:                                 .thenReturn(new EMFValuePropertyDecorator(
431:                                         new EMFValueProperty(dmr.getDomainModelEFeature()), dmr.getDomainModelEFeature()));
432:         }
433:
434:         /**
435:          * @param setting
436:          * @param player_Name
437:          * @throws DatabindingFailedException
438:          */
439:         private void mockSetting(final Setting setting, final EObject eObject,
440:                 final EStructuralFeature eStructuralFeature) throws DatabindingFailedException {
441:
442:                 when(setting.getEObject()).thenReturn(eObject);
443:                 when(setting.getEStructuralFeature()).thenReturn(eStructuralFeature);
444:
445:         }
446:
447:         private void assertControl(Control tableViewerComposite) {
448:                 assertTrue(AbstractTableViewerComposite.class.isInstance(tableViewerComposite));
449:                 final Control gridComposite = Composite.class.cast(tableViewerComposite).getChildren()[0];
450:                 final Control grid = Composite.class.cast(Composite.class.cast(gridComposite).getChildren()[1])
451:                         .getChildren()[0];
452:
453:                 assertTrue(Grid.class.isInstance(grid));
454:         }
455:
456:         private Grid getGrid(Control tableViewerComposite) {
457:                 final Control gridComposite = Composite.class.cast(tableViewerComposite).getChildren()[0];
458:                 return (Grid) Composite.class.cast(Composite.class.cast(gridComposite).getChildren()[1])
459:                         .getChildren()[0];
460:         }
461:
462:         private ColumnConfiguration extractGridColumnConfigByIndex(Grid grid, int i) {
463:                 final GridColumn colum = grid.getColumn(i);
464:                 final Object data = colum.getData(ColumnConfiguration.ID);
465:                 assertTrue(ColumnConfiguration.class.isInstance(data));
466:
467:                 final ColumnConfiguration columnConfiguration = ColumnConfiguration.class.cast(data);
468:                 return columnConfiguration;
469:         }
470:
471:         @Test
472:         public void testColumnHideShow()
473:                 throws DatabindingFailedException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
474:
475:                 mockDataset();
476:
477:                 final Control rendered = renderControl(new SWTGridCell(0, 2, getRenderer()));
478:                 assertControl(rendered);
479:
480:                 final GridTableViewerComposite tableViewerComposite = (GridTableViewerComposite) rendered;
481:                 assertTrue(tableViewerComposite.getEnabledFeatures().contains(TableConfiguration.FEATURE_COLUMN_HIDE_SHOW));
482:
483:                 final Grid grid = getGrid(rendered);
484:                 assertEquals(expectedColumns + 1, grid.getColumns().length); // columns defined by mockDataset()
485:                                                                                                                                                 // +1 for validation column
486:                 final GridColumn nameColumn = grid.getColumn(1);
487:                 assertTrue(nameColumn.isVisible());
488:
489:                 final Object data = nameColumn.getData(ColumnConfiguration.ID);
490:                 assertTrue(ColumnConfiguration.class.isInstance(data));
491:
492:                 final ColumnConfiguration columnConfiguration = extractGridColumnConfigByIndex(grid, 1); // name column
493:                 assertTrue("Feature not enabled/supported. Check ColumnConfiguration.FEATURES?",
494:                         columnConfiguration.getEnabledFeatures()
495:                                 .contains(ColumnConfiguration.FEATURE_COLUMN_HIDE_SHOW));
496:
497:                 columnConfiguration.visible().setValue(Boolean.FALSE);
498:                 assertFalse(nameColumn.isVisible());
499:
500:                 columnConfiguration.visible().resetToDefault();
501:                 assertTrue(nameColumn.isVisible());
502:
503:                 // TODO: check context menu items
504:         }
505:
506:         @Test
507:         public void testColumnFilter()
508:                 throws DatabindingFailedException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
509:
510:                 mockDataset();
511:
512:                 final Control rendered = renderControl(new SWTGridCell(0, 2, getRenderer()));
513:                 assertControl(rendered);
514:
515:                 final GridTableViewerComposite tableViewerComposite = (GridTableViewerComposite) rendered;
516:                 assertTrue(tableViewerComposite.getEnabledFeatures().contains(TableConfiguration.FEATURE_COLUMN_FILTER));
517:
518:                 final Grid grid = getGrid(rendered);
519:                 final ColumnConfiguration columnConfiguration = extractGridColumnConfigByIndex(grid,
520:                         DataSet.Simple.equals(dataset) ? 1 : 4); // name or login column
521:
522:                 assertTrue("Feature not enabled/supported. Check ColumnConfiguration.FEATURES?",
523:                         columnConfiguration.getEnabledFeatures()
524:                                 .contains(ColumnConfiguration.FEATURE_COLUMN_FILTER));
525:
526:                 assertEquals(expectedRows, countItems(grid)); // 3 players/rows defined in mockSampleDataSet()
527:
528:                 /*
529:                  * test filtering
530:                  */
531:
532:                 assertFilterMenuChecked(tableViewerComposite, null);
533:
534:                 tableViewerComposite.setFilteringMode(TableConfiguration.FEATURE_COLUMN_FILTER);
535:                 assertThat("Filter control not showing", columnConfiguration.showFilterControl().getValue(),
536:                         is(true));
537:                 assertFilterMenuChecked(tableViewerComposite, Messages.GridTableViewerComposite_toggleFilterControlsAction);
538:                 columnConfiguration.matchFilter().setValue("er");
539:                 switch (dataset) {
540:                 case Simple:
541:                         assertEquals(3, countVisible(grid));
542:                         break;
543:                 default: // complex case, has only two logins (a bot doesn't have one)
544:                         assertEquals(2, countVisible(grid));
545:                 }
546:
547:                 columnConfiguration.matchFilter().setValue("foo");
548:                 assertEquals(0, countVisible(grid));
549:
550:                 // double check, that extending a filter which lead to an empty result still created an empty result
551:                 columnConfiguration.matchFilter().setValue("foo2");
552:                 assertEquals(0, countVisible(grid));
553:
554:                 columnConfiguration.matchFilter().resetToDefault();
555:                 assertEquals(expectedRows, countVisible(grid));
556:
557:                 columnConfiguration.matchFilter().setValue("Kliver");
558:                 assertEquals(1, countVisible(grid));
559:
560:                 columnConfiguration.matchFilter().resetToDefault();
561:                 columnConfiguration.matchFilter().setValue("branzfeckenbauer");
562:
563:                 switch (dataset) {
564:                 case Simple:
565:                         assertEquals(0, countVisible(grid));
566:                         break;
567:                 default: // complex case, there is exactly one matching login
568:                         assertEquals(1, countVisible(grid));
569:                 }
570:
571:                 /*
572:                  * test clearing of filter
573:                  */
574:
575:                 tableViewerComposite.setFilteringMode(null);
576:                 assertThat("Filter control still showing", columnConfiguration.showFilterControl().getValue(),
577:                         is(false));
578:                 assertFilterMenuChecked(tableViewerComposite, null);
579:
580:                 assertEquals(3, countItems(grid));
581:                 assertEquals(3, countVisible(grid));
582:         }
583:
584:         /**
585:          * Assert that the indicated filtering item in the context menu is checked and no other.
586:          *
587:          * @param composite the grid composite
588:          * @param label the label of the menu item that should be checked, or {@code null} if
589:          * in fact no filtering item should be checked
590:          */
591:         void assertFilterMenuChecked(GridTableViewerComposite composite, String label) {
592:                 final Control grid = composite.getTableViewer().getControl();
593:                 final Menu menu = grid.getMenu();
594:                 final Event event = new Event();
595:
596:                 // Some menu actions are not defined if the mouse is not over a grid cell
597:                 final Rectangle cell = allItems(composite.getTableViewer().getGrid())[0].getBounds(1);
598:                 event.type = SWT.MouseMove;
599:                 event.x = cell.x + cell.width / 2;
600:                 event.y = cell.y + cell.height / 2;
601:                 grid.notifyListeners(SWT.MouseMove, event);
602:
603:                 event.type = SWT.Show;
604:                 event.widget = menu;
605:                 menu.notifyListeners(SWT.Show, event);
606:
607:                 boolean asserted = false;
608:
609:                 try {
610:                         for (final MenuItem next : menu.getItems()) {
611:                                 if (next.getText().contains("filter")) {
612:                                         assertThat("Wrong check state", next.getSelection(),
613:                                                 is(label != null && label.equals(next.getText())));
614:                                         asserted = true;
615:                                 }
616:                         }
617:                 } finally {
618:                         event.type = SWT.Hide;
619:                         menu.notifyListeners(SWT.Hide, event);
620:                 }
621:
622:                 assertThat("No context-menu items found to assert their check state", asserted, is(true));
623:         }
624:
625:         @Test
626:         public void testColumnFilterWithColumnHideShow()
627:                 throws DatabindingFailedException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
628:
629:                 mockDataset();
630:
631:                 final Control rendered = renderControl(new SWTGridCell(0, 2, getRenderer()));
632:                 final Grid grid = getGrid(rendered);
633:                 final GridColumn nameColumn = grid.getColumn(1); // name column
634:
635:                 final Object data = nameColumn.getData(ColumnConfiguration.ID);
636:                 final ColumnConfiguration columnConfiguration = ColumnConfiguration.class.cast(data);
637:
638:                 assertTrue("Feature not enabled/supported. Check ColumnConfiguration.FEATURES?",
639:                         columnConfiguration.getEnabledFeatures()
640:                                 .contains(ColumnConfiguration.FEATURE_COLUMN_HIDE_SHOW));
641:                 assertTrue("Feature not enabled/supported. Check ColumnConfiguration.FEATURES?",
642:                         columnConfiguration.getEnabledFeatures()
643:                                 .contains(ColumnConfiguration.FEATURE_COLUMN_FILTER));
644:
645:                 /*
646:                  * test clearing of filter
647:                  */
648:
649:                 final GridTableViewerComposite tableViewerComposite = (GridTableViewerComposite) rendered;
650:                 tableViewerComposite.setFilteringMode(TableConfiguration.FEATURE_COLUMN_FILTER);
651:                 assertThat("Filter control not showing", columnConfiguration.showFilterControl().getValue(),
652:                         is(true));
653:                 columnConfiguration.matchFilter().setValue("foo");
654:
655:                 assertEquals(0, countVisible(grid));
656:
657:                 tableViewerComposite.setFilteringMode(null);
658:                 columnConfiguration.visible().setValue(Boolean.FALSE);
659:
660:                 assertEquals(expectedRows, countVisible(grid));
661:
662:                 /*
663:                  * test for Gerrit #110529 (filter again after filters have been hidden)
664:                  */
665:
666:                 tableViewerComposite.setFilteringMode(TableConfiguration.FEATURE_COLUMN_FILTER);
667:                 columnConfiguration.visible().setValue(Boolean.TRUE);
668:                 assertThat("Filter control not showing", columnConfiguration.showFilterControl().getValue(),
669:                         is(true));
670:                 columnConfiguration.matchFilter().setValue("bar");
671:
672:                 assertEquals(0, countVisible(grid));
673:
674:                 columnConfiguration.matchFilter().resetToDefault();
675:                 tableViewerComposite.setFilteringMode(null);
676:                 assertThat("Filter control still showing", columnConfiguration.showFilterControl().getValue(),
677:                         is(false));
678:                 // will result in a NPE without Gerrit #110529
679:                 columnConfiguration.visible().setValue(Boolean.FALSE);
680:
681:                 assertEquals(expectedRows, countVisible(grid));
682:
683:         }
684:
685:         @Test
686:         public void testColumnRegexFilter()
687:                 throws DatabindingFailedException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
688:
689:                 mockDataset();
690:
691:                 final Control rendered = renderControl(new SWTGridCell(0, 2, getRenderer()));
692:                 assertControl(rendered);
693:
694:                 final GridTableViewerComposite tableViewerComposite = (GridTableViewerComposite) rendered;
695:                 assertThat("Bad table features configuration", tableViewerComposite.getEnabledFeatures(),
696:                         hasItem(TableConfiguration.FEATURE_COLUMN_REGEX_FILTER));
697:
698:                 final Grid grid = getGrid(rendered);
699:                 final ColumnConfiguration columnConfiguration = extractGridColumnConfigByIndex(grid,
700:                         DataSet.Simple.equals(dataset) ? 1 : 4); // name or login column
701:
702:                 assertThat("Feature not enabled/supported. Check ColumnConfiguration.FEATURES?",
703:                         columnConfiguration.getEnabledFeatures(), hasItem(ColumnConfiguration.FEATURE_COLUMN_REGEX_FILTER));
704:
705:                 assertThat("Wrong number of rows filtered",
706:                         countItems(grid), is(expectedRows)); // 3 players/rows defined in mockSampleDataSet()
707:
708:                 /*
709:                  * test filtering
710:                  */
711:
712:                 tableViewerComposite.setFilteringMode(TableConfiguration.FEATURE_COLUMN_REGEX_FILTER);
713:                 assertFilterMenuChecked(tableViewerComposite,
714:                         Messages.GridTableViewerComposite_toggleRegexFilterControlsAction);
715:                 columnConfiguration.matchFilter().setValue("er");
716:                 switch (dataset) {
717:                 case Simple:
718:                         assertThat("No rows should be filtered", countVisible(grid), is(3));
719:                         break;
720:                 default: // complex case, has only two logins (a bot doesn't have one)
721:                         assertThat("One row should be filtered", countVisible(grid), is(2));
722:                 }
723:
724:                 // Match only 'er' at the end of the string
725:                 columnConfiguration.matchFilter().setValue("er$");
726:                 switch (dataset) {
727:                 case Simple:
728:                         assertThat("One row should be filtered", countVisible(grid), is(2));
729:                         break;
730:                 default: // complex case, has only two logins (a bot doesn't have one)
731:                         assertThat("Two rows should be filtered", countVisible(grid), is(1));
732:                 }
733:
734:                 columnConfiguration.matchFilter().setValue("foo");
735:                 assertThat("All rows should be filtered", countVisible(grid), is(0));
736:
737:                 // double check, that extending a filter which lead to an empty result still created an empty result
738:                 columnConfiguration.matchFilter().setValue("foo2");
739:                 assertThat("All rows should be filtered", countVisible(grid), is(0));
740:
741:                 // but an invalid regex doesn't filter anything (user should see examples in the grid to
742:                 // be guides in formulating the regex)
743:                 columnConfiguration.matchFilter().setValue("([a].*\\1");
744:                 assertThat("No rows should be filtered", countVisible(grid), is(expectedRows));
745:
746:                 columnConfiguration.matchFilter().setValue("([a]).*\\1");
747:                 switch (dataset) {
748:                 case Simple:
749:                         assertThat("Two rows should be filtered", countVisible(grid), is(1));
750:                         break;
751:                 default: // complex case, where the bot hasn't a login
752:                         assertThat("Two rows should be filtered", countVisible(grid), is(1));
753:                 }
754:
755:                 columnConfiguration.matchFilter().setValue("([ae]).*\\1");
756:                 switch (dataset) {
757:                 case Simple:
758:                         assertThat("One row should be filtered", countVisible(grid), is(2));
759:                         break;
760:                 default: // complex case, where the bot hasn't a login
761:                         assertThat("Two rows should be filtered", countVisible(grid), is(1));
762:                 }
763:
764:                 columnConfiguration.matchFilter().resetToDefault();
765:                 assertThat("No rows should be filtered", countVisible(grid), is(expectedRows));
766:
767:                 columnConfiguration.matchFilter().setValue("branzfeckenbauer");
768:
769:                 switch (dataset) {
770:                 case Simple:
771:                         assertThat("All rows should be filtered", countVisible(grid), is(0));
772:                         break;
773:                 default: // complex case, there is exactly one matching login
774:                         assertThat("Two rows should be filtered", countVisible(grid), is(1));
775:                 }
776:
777:                 /*
778:                  * test clearing of filter
779:                  */
780:
781:                 tableViewerComposite.setFilteringMode(null);
782:                 assertFilterMenuChecked(tableViewerComposite, null);
783:                 assertThat("Filter control still showing", columnConfiguration.showFilterControl().getValue(),
784:                         is(false));
785:
786:                 assertThat("Wrong number of items in the grid", countItems(grid), is(3));
787:                 assertThat("No rows should be filtered", countVisible(grid), is(3));
788:         }
789:
790:         private GridItem[] filterVisible(Grid grid) {
791:                 final List<GridItem> visibleItems = new ArrayList<GridItem>();
792:                 for (final GridItem item : allItems(grid)) {
793:                         if (!item.isVisible()) {
794:                                 continue;
795:                         }
796:                         visibleItems.add(item);
797:                 }
798:                 return visibleItems.toArray(new GridItem[] {});
799:         }
800:
801:         private GridItem[] allItems(Grid grid) {
802:                 SWTTestUtil.waitForUIThread(); // The table refresh is asynchronous
803:                 return grid.getItems();
804:         }
805:
806:         private int countItems(Grid grid) {
807:                 return allItems(grid).length;
808:         }
809:
810:         private int countVisible(Grid grid) {
811:                 return filterVisible(grid).length;
812:         }
813:
814:         @Override
815:         public void renderValidationIconLabelAlignmentLeft()
816:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption {
817:         }
818:
819:         @Override
820:         public void renderValidationIconLabelAlignmentNone()
821:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption {
822:         }
823:
824:         @Override
825:         public void testGridDescriptionLabelAlignmentLeft() {
826:         }
827:
828:         @Override
829:         public void testGridDescriptionLabelAlignmentNone() {
830:         }
831:
832:         @Test
833:         public void testEffectivelyReadOnlyHidesAddRemoveButtons()
834:                 throws DatabindingFailedException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
835:
836:                 mockDataset();
837:
838:                 when(getvControl().isAddRemoveDisabled()).thenReturn(false);
839:                 when(getvControl().isEffectivelyEnabled()).thenReturn(true);
840:                 when(getvControl().isEffectivelyReadonly()).thenReturn(true);
841:                 when(getvControl().isVisible()).thenReturn(true);
842:
843:                 getShell().open();
844:                 final Control rendered = renderControl(new SWTGridCell(0, 2, getRenderer()));
845:                 getRenderer().finalizeRendering(getShell());
846:                 assertControl(rendered);
847:                 final GridTableViewerComposite tableViewerComposite = (GridTableViewerComposite) rendered;
848:                 final Composite gridComposite = Composite.class.cast(tableViewerComposite.getChildren()[0]);
849:                 final Composite buttonComposite = (Composite) Composite.class.cast(gridComposite.getChildren()[0])
850:                         .getChildren()[2];
851:
852:                 // there should be no buttons in case the table is read-only
853:                 assertEquals(0, buttonComposite.getChildren().length);
854:
855:                 final GridControlSWTRenderer swtRenderer = GridControlSWTRenderer.class.cast(getRenderer());
856:
857:                 final Optional<Control> addRowButton = swtRenderer.getControlForAction(AddRowAction.ACTION_ID);
858:                 final Optional<Control> removeRowButton = swtRenderer.getControlForAction(RemoveRowAction.ACTION_ID);
859:                 final Optional<Control> duplicateRowButton = swtRenderer.getControlForAction(DuplicateRowAction.ACTION_ID);
860:
861:                 assertFalse(addRowButton.isPresent());
862:                 assertFalse(removeRowButton.isPresent());
863:                 assertFalse(duplicateRowButton.isPresent());
864:         }
865:
866:         @Test
867:         public void testActionKeyBindings()
868:                 throws DatabindingFailedException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
869:
870:                 mockDataset();
871:
872:                 final Set<VTStyleProperty> properties = new LinkedHashSet<VTStyleProperty>();
873:                 final VTKeyBindings keyBindings = VTKeybindingFactory.eINSTANCE.createKeyBindings();
874:                 addKeyBinding(keyBindings, AddRowAction.ACTION_ID, "M1+e");
875:                 properties.add(keyBindings);
876:
877:                 final VTViewTemplateProvider viewTemplateProvider = mock(VTViewTemplateProvider.class);
878:                 when(viewTemplateProvider.getStyleProperties(getvControl(), getContext()))
879:                         .thenReturn(properties);
880:
881:                 setTemplateProvider(viewTemplateProvider);
882:
883:                 getShell().open();
884:                 final Control rendered = renderControl(new SWTGridCell(0, 2, getRenderer()));
885:                 getRenderer().finalizeRendering(getShell());
886:                 assertControl(rendered);
887:                 final GridTableViewerComposite tableViewerComposite = (GridTableViewerComposite) rendered;
888:                 final Composite gridComposite = Composite.class.cast(tableViewerComposite.getChildren()[0]);
889:                 final Composite buttonComposite = (Composite) Composite.class.cast(gridComposite.getChildren()[0])
890:                         .getChildren()[2];
891:
892:                 final GridControlSWTRenderer swtRenderer = GridControlSWTRenderer.class.cast(getRenderer());
893:
894:         }
895:
896:         private void addKeyBinding(VTKeyBindings bindings, String actionId, String sequence) {
897:                 final VTKeyBinding binding = VTKeybindingFactory.eINSTANCE.createKeyBinding();
898:                 binding.setId(actionId);
899:                 binding.setKeySequence(sequence);
900:
901:                 bindings.getBindings().add(binding);
902:         }
903: }