Skip to content

Package: SelectSubclassAndTemplateWizard_PTest

SelectSubclassAndTemplateWizard_PTest

nameinstructionbranchcomplexitylinemethod
SelectSubclassAndTemplateWizard_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%
createTemplate(String, EClass)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getButtonComposite(WizardDialog)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
openWizardDialog(Wizard)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
setUp()
M: 0 C: 40
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
tearDown()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
testBackButton()
M: 0 C: 241
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 55
100%
M: 0 C: 1
100%
testCancel()
M: 0 C: 152
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 37
100%
M: 0 C: 1
100%
testGetAvailableTemplates()
M: 0 C: 87
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 18
100%
M: 0 C: 1
100%
testSubClassAndTemplateSelection()
M: 0 C: 210
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 51
100%
M: 0 C: 1
100%
testSubClassSelectionOnly()
M: 0 C: 173
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 40
100%
M: 0 C: 1
100%
testTemplateSelectionOnly()
M: 0 C: 157
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 39
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 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.core.services.datatemplate;
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.mockito.Matchers.any;
20: import static org.mockito.Mockito.mock;
21: import static org.mockito.Mockito.when;
22:
23: import java.util.Collections;
24: import java.util.LinkedHashSet;
25: import java.util.Set;
26:
27: import org.eclipse.emf.ecore.EClass;
28: import org.eclipse.emf.ecore.EObject;
29: import org.eclipse.emf.ecore.EcorePackage;
30: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTTestUtil;
31: import org.eclipse.emfforms.common.Optional;
32: import org.eclipse.emfforms.core.services.datatemplate.test.model.audit.AuditPackage;
33: import org.eclipse.emfforms.datatemplate.DataTemplateFactory;
34: import org.eclipse.emfforms.datatemplate.Template;
35: import org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService;
36: import org.eclipse.jface.window.Window;
37: import org.eclipse.jface.wizard.Wizard;
38: import org.eclipse.jface.wizard.WizardDialog;
39: import org.eclipse.swt.SWTException;
40: import org.eclipse.swt.widgets.Button;
41: import org.eclipse.swt.widgets.Composite;
42: import org.eclipse.swt.widgets.Shell;
43: import org.eclipse.swt.widgets.Table;
44: import org.eclipse.swt.widgets.Tree;
45: import org.eclipse.swt.widgets.TreeItem;
46: import org.junit.After;
47: import org.junit.Before;
48: import org.junit.Test;
49: import org.mockito.invocation.InvocationOnMock;
50: import org.mockito.stubbing.Answer;
51: import org.osgi.framework.Bundle;
52:
53: /**
54: * Plugin tests for the {@link SelectSubclassAndTemplateWizard}.
55: * <p>
56: * <strong>Note:</strong> The wizard itself would be unit testable but the used selection composites use the
57: * LocalizationServiceHelper which only works in a Plugin execution context.
58: *
59: * @author Lucas Koehler
60: *
61: */
62: public class SelectSubclassAndTemplateWizard_PTest {
63:
64:         private EMFFormsLocalizationService localizationService;
65:         private Shell shell;
66:
67:         /**
68:          * @throws SWTException if the Shell cannot be instantiated
69:          */
70:         @Before
71:         public void setUp() throws SWTException {
72:                 localizationService = mock(EMFFormsLocalizationService.class);
73:                 when(localizationService.getString(any(Class.class), any(String.class))).thenReturn(""); //$NON-NLS-1$
74:                 when(localizationService.getString(any(Bundle.class), any(String.class))).thenAnswer(
75:                         new Answer<String>() {
76:                                 @SuppressWarnings("nls")
77:                                 @Override
78:                                 public String answer(InvocationOnMock invocation) throws Throwable {
79:                                         final String key = (String) invocation.getArguments()[1];
80:                                         if ("_UI_AbstractSubUser_type".equals(key)) {
81:                                                 return "Abstract Sub User";
82:                                         }
83:                                         if ("_UI_AdminUser_type".equals(key)) {
84:                                                 return "Admin User";
85:                                         }
86:                                         if ("_UI_GuestUser_type".equals(key)) {
87:                                                 return "Guest User";
88:                                         }
89:                                         if ("_UI_PrivilegedUser_type".equals(key)) {
90:                                                 return "Privileged User";
91:                                         }
92:                                         if ("_UI_RegisteredUser_type".equals(key)) {
93:                                                 return "Registered User";
94:                                         }
95:                                         if ("_UI_UserGroup_type".equals(key)) {
96:                                                 return "User Group";
97:                                         }
98:                                         if ("_UI_User_type".equals(key)) {
99:                                                 return "User";
100:                                         }
101:                                         return "";
102:                                 }
103:                         });
104:                 shell = new Shell();
105:         }
106:
107:         @After
108:         public void tearDown() {
109:                 shell.dispose();
110:         }
111:
112:         /**
113:          * Tests the standard use case: first selecting an EClass and then a template on the next page.
114:          */
115:         @Test
116:         public void testSubClassAndTemplateSelection() {
117:                 // Add selectable EClasses
118:                 final LinkedHashSet<EClass> subClasses = new LinkedHashSet<EClass>();
119:                 subClasses.add(AuditPackage.eINSTANCE.getAdminUser());
120:                 subClasses.add(AuditPackage.eINSTANCE.getRegisteredUser());
121:
122:                 // Configure templates
123:                 final Template template1 = createTemplate("AdminTemplate1", AuditPackage.eINSTANCE.getAdminUser()); //$NON-NLS-1$
124:                 final Template template2 = createTemplate("RegisteredUserTemplate1", //$NON-NLS-1$
125:                         AuditPackage.eINSTANCE.getRegisteredUser());
126:                 final Template template3 = createTemplate("RegisteredUserTemplate2", //$NON-NLS-1$
127:                         AuditPackage.eINSTANCE.getRegisteredUser());
128:
129:                 final LinkedHashSet<Template> templates = new LinkedHashSet<Template>();
130:                 templates.add(template1);
131:                 templates.add(template2);
132:                 templates.add(template3);
133:
134:                 // Create and open wizard
135:                 final SelectSubclassAndTemplateWizard wizard = new SelectSubclassAndTemplateWizard("Wizard", subClasses, //$NON-NLS-1$
136:                         templates, localizationService);
137:
138:                 final WizardDialog wizardDialog = openWizardDialog(wizard);
139:
140:                 // Get the wizard dialog's shell from our parent shell
141:                 final Shell wizardShell = shell.getShells()[0];
142:                 final Composite dialogComposite = (Composite) Composite.class.cast(wizardShell.getChildren()[0])
143:                         .getChildren()[0];
144:
145:                 // Get wizard buttons
146:                 final Composite buttonComposite = getButtonComposite(wizardDialog);
147:                 final Composite innerButtonComposite = (Composite) buttonComposite.getChildren()[0];
148:                 final Button backButton = (Button) innerButtonComposite.getChildren()[0];
149:                 final Button nextButton = (Button) innerButtonComposite.getChildren()[1];
150:                 final Button cancelButton = (Button) buttonComposite.getChildren()[1];
151:                 final Button finishButton = (Button) buttonComposite.getChildren()[2];
152:
153:                 // Get the composites containing the wizard pages
154:                 final Composite pagesComposite = (Composite) Composite.class.cast(dialogComposite.getChildren()[0])
155:                         .getChildren()[1];
156:                 final Composite subClassPageComposite = (Composite) pagesComposite.getChildren()[0];
157:                 final Composite templateSelectionPageComposite = (Composite) pagesComposite.getChildren()[1];
158:
159:                 final Tree subClassTable = (Tree) subClassPageComposite.getChildren()[2];
160:                 assertEquals(2, subClassTable.getItemCount());
161:
162:                 // Select the registered user class
163:                 final TreeItem item = SWTTestUtil.selectTreeItem(subClassTable, 1);
164:                 SWTTestUtil.waitForUIThread();
165:                 assertEquals("RegisteredUser", item.getText()); //$NON-NLS-1$
166:
167:                 // verify button enablement
168:                 assertFalse(backButton.isEnabled());
169:                 assertTrue(nextButton.isEnabled());
170:                 assertTrue(cancelButton.isEnabled());
171:                 assertFalse(finishButton.isEnabled());
172:
173:                 // click next button and wait for next page
174:                 SWTTestUtil.clickButton(nextButton);
175:                 SWTTestUtil.waitForUIThread();
176:
177:                 final Table templateTable = (Table) templateSelectionPageComposite.getChildren()[2];
178:
179:                 // Next button must not be enabled on final page
180:                 assertFalse(nextButton.isEnabled());
181:
182:                 // the two templates for the registered user
183:                 assertEquals(2, templateTable.getItemCount());
184:                 SWTTestUtil.selectTableItem(templateTable, 0);
185:                 SWTTestUtil.waitForUIThread();
186:
187:                 // Finish
188:                 SWTTestUtil.clickButton(finishButton);
189:                 SWTTestUtil.waitForUIThread();
190:
191:                 // Evaluate wizard result
192:                 assertEquals(Window.OK, wizardDialog.getReturnCode());
193:                 final Optional<Template> templateOptional = wizard.getSelectedTemplate();
194:                 assertTrue(templateOptional.isPresent());
195:                 assertEquals(template2, templateOptional.get());
196:         }
197:
198:         /**
199:          * Test that the sub class selection page is skipped if there is only one selectable {@link EClass}.
200:          */
201:         @Test
202:         public void testTemplateSelectionOnly() {
203:                 // Only add one sub class
204:                 final LinkedHashSet<EClass> subClasses = new LinkedHashSet<EClass>();
205:                 subClasses.add(AuditPackage.eINSTANCE.getRegisteredUser());
206:
207:                 // Configure templates
208:                 final Template template1 = createTemplate("AdminTemplate1", AuditPackage.eINSTANCE.getAdminUser()); //$NON-NLS-1$
209:                 final Template template2 = createTemplate("RegisteredUserTemplate1", //$NON-NLS-1$
210:                         AuditPackage.eINSTANCE.getRegisteredUser());
211:                 final Template template3 = createTemplate("RegisteredUserTemplate2", //$NON-NLS-1$
212:                         AuditPackage.eINSTANCE.getRegisteredUser());
213:
214:                 final LinkedHashSet<Template> templates = new LinkedHashSet<Template>();
215:                 templates.add(template1);
216:                 templates.add(template2);
217:                 templates.add(template3);
218:
219:                 // Create and open wizard
220:                 final SelectSubclassAndTemplateWizard wizard = new SelectSubclassAndTemplateWizard("Wizard", subClasses, //$NON-NLS-1$
221:                         templates, localizationService);
222:                 final WizardDialog wizardDialog = openWizardDialog(wizard);
223:
224:                 // Get the wizard dialog's shell from our parent shell
225:                 final Shell wizardShell = shell.getShells()[0];
226:                 final Composite dialogComposite = (Composite) Composite.class.cast(wizardShell.getChildren()[0])
227:                         .getChildren()[0];
228:
229:                 // Get wizard buttons
230:                 // final Composite buttonComposite = (Composite) Composite.class.cast(dialogComposite.getChildren()[1])
231:                 // .getChildren()[0];
232:                 final Composite buttonComposite = getButtonComposite(wizardDialog);
233:                 final Button cancelButton = (Button) buttonComposite.getChildren()[0];
234:                 final Button finishButton = (Button) buttonComposite.getChildren()[1];
235:
236:                 // Get the composites containing the template selection page
237:                 final Composite pagesComposite = (Composite) Composite.class.cast(dialogComposite.getChildren()[0])
238:                         .getChildren()[1];
239:                 final Composite templateSelectionPageComposite = (Composite) pagesComposite.getChildren()[0];
240:
241:                 // Verify that the wizard starts at the template selection page
242:                 assertTrue(wizardDialog.getCurrentPage() instanceof SelectSubclassAndTemplateWizard.TemplateSelectionPage);
243:
244:                 SWTTestUtil.waitForUIThread();
245:
246:                 assertTrue(cancelButton.isEnabled());
247:                 assertFalse(finishButton.isEnabled());
248:
249:                 final Table templateTable = (Table) templateSelectionPageComposite.getChildren()[2];
250:
251:                 assertEquals(2, templateTable.getItemCount());
252:                 SWTTestUtil.selectTableItem(templateTable, 0);
253:                 SWTTestUtil.waitForUIThread();
254:
255:                 // Finish
256:                 assertTrue(finishButton.isEnabled());
257:                 SWTTestUtil.clickButton(finishButton);
258:                 SWTTestUtil.waitForUIThread();
259:
260:                 // Evaluate wizard result
261:                 assertEquals(Window.OK, wizardDialog.getReturnCode());
262:                 final Optional<Template> templateOptional = wizard.getSelectedTemplate();
263:                 assertTrue(templateOptional.isPresent());
264:                 assertEquals(template2, templateOptional.get());
265:         }
266:
267:         /**
268:          * Tests that the template selection page is not shown if there is only one template for the selected EClass.
269:          */
270:         @Test
271:         public void testSubClassSelectionOnly() {
272:                 // Add selectable EClasses
273:                 final LinkedHashSet<EClass> subClasses = new LinkedHashSet<EClass>();
274:                 subClasses.add(AuditPackage.eINSTANCE.getAdminUser());
275:                 subClasses.add(AuditPackage.eINSTANCE.getGuestUser());
276:
277:                 // Configure templates
278:                 final Template template1 = createTemplate("AdminTemplate1", AuditPackage.eINSTANCE.getAdminUser()); //$NON-NLS-1$
279:                 final Template template2 = createTemplate("GuestUserTemplate1", AuditPackage.eINSTANCE.getGuestUser()); //$NON-NLS-1$
280:
281:                 final LinkedHashSet<Template> templates = new LinkedHashSet<Template>();
282:                 templates.add(template1);
283:                 templates.add(template2);
284:
285:                 // Create and open wizard
286:                 final SelectSubclassAndTemplateWizard wizard = new SelectSubclassAndTemplateWizard("Wizard", subClasses, //$NON-NLS-1$
287:                         templates, localizationService);
288:                 final WizardDialog wizardDialog = openWizardDialog(wizard);
289:
290:                 SWTTestUtil.waitForUIThread();
291:
292:                 // Get the wizard dialog's shell from our parent shell
293:                 final Shell wizardShell = shell.getShells()[0];
294:                 final Composite dialogComposite = (Composite) Composite.class.cast(wizardShell.getChildren()[0])
295:                         .getChildren()[0];
296:
297:                 // Get wizard buttons
298:                 final Composite buttonComposite = getButtonComposite(wizardDialog);
299:                 final Composite innerButtonComposite = (Composite) buttonComposite.getChildren()[0];
300:                 final Button backButton = (Button) innerButtonComposite.getChildren()[0];
301:                 final Button nextButton = (Button) innerButtonComposite.getChildren()[1];
302:                 final Button cancelButton = (Button) buttonComposite.getChildren()[1];
303:                 final Button finishButton = (Button) buttonComposite.getChildren()[2];
304:
305:                 // Get the composites containing the wizard pages
306:                 final Composite pagesComposite = (Composite) Composite.class.cast(dialogComposite.getChildren()[0])
307:                         .getChildren()[1];
308:                 final Composite subClassPageComposite = (Composite) pagesComposite.getChildren()[0];
309:
310:                 final Tree subClassTree = (Tree) subClassPageComposite.getChildren()[2];
311:                 assertEquals(2, subClassTree.getItemCount());
312:
313:                 // Select the guest user class
314:                 final TreeItem item = SWTTestUtil.selectTreeItem(subClassTree, 1);
315:                 SWTTestUtil.waitForUIThread();
316:                 assertEquals("GuestUser", item.getText()); //$NON-NLS-1$
317:
318:                 // Because there is only one template for the guest user EClass the wizard should offer to finish directly but
319:                 // not to go to the template selection page
320:                 assertFalse(backButton.isEnabled());
321:                 assertFalse(nextButton.isEnabled());
322:                 assertTrue(cancelButton.isEnabled());
323:                 assertTrue(finishButton.isEnabled());
324:
325:                 // Finish
326:                 SWTTestUtil.clickButton(finishButton);
327:                 SWTTestUtil.waitForUIThread();
328:
329:                 // Evaluate wizard result
330:                 assertEquals(Window.OK, wizardDialog.getReturnCode());
331:                 final Optional<Template> templateOptional = wizard.getSelectedTemplate();
332:                 assertTrue(templateOptional.isPresent());
333:                 assertEquals(template2, templateOptional.get());
334:         }
335:
336:         /**
337:          * Tests that the template selection table gets updated correctly after the back functionality of the wizard was
338:          * used.
339:          */
340:         @Test
341:         public void testBackButton() {
342:                 // Add selectable EClasses
343:                 final LinkedHashSet<EClass> subClasses = new LinkedHashSet<EClass>();
344:                 subClasses.add(AuditPackage.eINSTANCE.getAdminUser());
345:                 subClasses.add(AuditPackage.eINSTANCE.getGuestUser());
346:
347:                 // Configure 2 templates for each EClass
348:                 final Template adminTemplate1 = createTemplate("AdminTemplate1", AuditPackage.eINSTANCE.getAdminUser()); //$NON-NLS-1$
349:                 final Template adminTemplate2 = createTemplate("AdminTemplate2", AuditPackage.eINSTANCE.getAdminUser()); //$NON-NLS-1$
350:                 final Template guestTemplate1 = createTemplate("GuestUserTemplate1", AuditPackage.eINSTANCE.getGuestUser()); //$NON-NLS-1$
351:                 final Template guestTemplate2 = createTemplate("GuestUserTemplate2", AuditPackage.eINSTANCE.getGuestUser()); //$NON-NLS-1$
352:
353:                 final LinkedHashSet<Template> templates = new LinkedHashSet<Template>();
354:                 templates.add(adminTemplate1);
355:                 templates.add(adminTemplate2);
356:                 templates.add(guestTemplate1);
357:                 templates.add(guestTemplate2);
358:
359:                 // Create and open wizard
360:                 final SelectSubclassAndTemplateWizard wizard = new SelectSubclassAndTemplateWizard("Wizard", subClasses, //$NON-NLS-1$
361:                         templates, localizationService);
362:                 final WizardDialog wizardDialog = openWizardDialog(wizard);
363:
364:                 // Get the wizard dialog's shell from our parent shell
365:                 final Shell wizardShell = shell.getShells()[0];
366:                 final Composite dialogComposite = (Composite) Composite.class.cast(wizardShell.getChildren()[0])
367:                         .getChildren()[0];
368:
369:                 // Get wizard buttons
370:                 final Composite buttonComposite = getButtonComposite(wizardDialog);
371:                 final Composite innerButtonComposite = (Composite) buttonComposite.getChildren()[0];
372:                 final Button backButton = (Button) innerButtonComposite.getChildren()[0];
373:                 final Button nextButton = (Button) innerButtonComposite.getChildren()[1];
374:                 // final Button cancelButton = (Button) buttonComposite.getChildren()[1];
375:                 final Button finishButton = (Button) buttonComposite.getChildren()[2];
376:
377:                 // Get the composites containing the wizard pages
378:                 final Composite pagesComposite = (Composite) Composite.class.cast(dialogComposite.getChildren()[0])
379:                         .getChildren()[1];
380:                 final Composite subClassPageComposite = (Composite) pagesComposite.getChildren()[0];
381:                 final Composite templateSelectionPageComposite = (Composite) pagesComposite.getChildren()[1];
382:
383:                 final Tree subClassTable = (Tree) subClassPageComposite.getChildren()[2];
384:                 assertEquals(2, subClassTable.getItemCount());
385:
386:                 // Select the admin user class
387:                 SWTTestUtil.selectTreeItem(subClassTable, 0);
388:
389:                 SWTTestUtil.waitForUIThread();
390:
391:                 // Next
392:                 SWTTestUtil.clickButton(nextButton);
393:                 SWTTestUtil.waitForUIThread();
394:
395:                 final Table templateTable = (Table) templateSelectionPageComposite.getChildren()[2];
396:
397:                 assertEquals(2, templateTable.getItemCount());
398:                 assertTrue(templateTable.getItem(0).getText().contains("AdminTemplate1")); //$NON-NLS-1$
399:                 assertTrue(templateTable.getItem(1).getText().contains("AdminTemplate2")); //$NON-NLS-1$
400:
401:                 // Go back
402:                 SWTTestUtil.waitForUIThread();
403:                 SWTTestUtil.clickButton(backButton);
404:
405:                 // Select guest user class and click next again
406:                 SWTTestUtil.selectTreeItem(subClassTable, 1);
407:                 SWTTestUtil.waitForUIThread();
408:                 assertTrue(nextButton.isEnabled());
409:                 SWTTestUtil.clickButton(nextButton);
410:                 SWTTestUtil.waitForUIThread();
411:
412:                 // Verify that the template selection table was updated correctly
413:                 assertEquals(2, templateTable.getItemCount());
414:                 assertTrue(templateTable.getItem(0).getText().contains("GuestUserTemplate1")); //$NON-NLS-1$
415:                 assertTrue(templateTable.getItem(1).getText().contains("GuestUserTemplate2")); //$NON-NLS-1$
416:
417:                 SWTTestUtil.selectTableItem(templateTable, 1);
418:                 SWTTestUtil.clickButton(finishButton);
419:                 SWTTestUtil.waitForUIThread();
420:
421:                 // Evaluate wizard result
422:                 assertEquals(Window.OK, wizardDialog.getReturnCode());
423:                 final Optional<Template> templateOptional = wizard.getSelectedTemplate();
424:                 assertTrue(templateOptional.isPresent());
425:                 assertEquals(guestTemplate2, templateOptional.get());
426:         }
427:
428:         /**
429:          * If the wizard is cancelled it should return an empty Optional as a result even if a template was selected by the
430:          * user.
431:          */
432:         @Test
433:         public void testCancel() {
434:                 // Only add one sub class
435:                 final LinkedHashSet<EClass> subClasses = new LinkedHashSet<EClass>();
436:                 subClasses.add(AuditPackage.eINSTANCE.getRegisteredUser());
437:
438:                 // Configure templates
439:                 final Template template1 = createTemplate("AdminTemplate1", AuditPackage.eINSTANCE.getAdminUser()); //$NON-NLS-1$
440:                 final Template template2 = createTemplate("RegisteredUserTemplate1", //$NON-NLS-1$
441:                         AuditPackage.eINSTANCE.getRegisteredUser());
442:                 final Template template3 = createTemplate("RegisteredUserTemplate2", //$NON-NLS-1$
443:                         AuditPackage.eINSTANCE.getRegisteredUser());
444:
445:                 final LinkedHashSet<Template> templates = new LinkedHashSet<Template>();
446:                 templates.add(template1);
447:                 templates.add(template2);
448:                 templates.add(template3);
449:
450:                 // Create and open wizard
451:                 final SelectSubclassAndTemplateWizard wizard = new SelectSubclassAndTemplateWizard("Wizard", subClasses, //$NON-NLS-1$
452:                         templates, localizationService);
453:                 final WizardDialog wizardDialog = openWizardDialog(wizard);
454:
455:                 // Get the wizard dialog's shell from our parent shell
456:                 final Shell wizardShell = shell.getShells()[0];
457:                 final Composite dialogComposite = (Composite) Composite.class.cast(wizardShell.getChildren()[0])
458:                         .getChildren()[0];
459:
460:                 // Get wizard buttons
461:                 final Composite buttonComposite = getButtonComposite(wizardDialog);
462:                 final Button cancelButton = (Button) buttonComposite.getChildren()[0];
463:                 final Button finishButton = (Button) buttonComposite.getChildren()[1];
464:
465:                 // Get the composites containing the template selection page
466:                 final Composite pagesComposite = (Composite) Composite.class.cast(dialogComposite.getChildren()[0])
467:                         .getChildren()[1];
468:                 final Composite templateSelectionPageComposite = (Composite) pagesComposite.getChildren()[0];
469:
470:                 // Verify that the wizard starts at the template selection page
471:                 assertTrue(wizardDialog.getCurrentPage() instanceof SelectSubclassAndTemplateWizard.TemplateSelectionPage);
472:
473:                 assertTrue(cancelButton.isEnabled());
474:                 assertFalse(finishButton.isEnabled());
475:
476:                 final Table templateTable = (Table) templateSelectionPageComposite.getChildren()[2];
477:
478:                 // Select a template
479:                 assertEquals(2, templateTable.getItemCount());
480:                 SWTTestUtil.selectTableItem(templateTable, 0);
481:                 SWTTestUtil.waitForUIThread();
482:
483:                 // Cancel after a template was selected
484:                 assertTrue(cancelButton.isEnabled());
485:                 SWTTestUtil.clickButton(cancelButton);
486:                 SWTTestUtil.waitForUIThread();
487:
488:                 // Evaluate wizard result: the result Optional should be empty
489:                 assertEquals(Window.CANCEL, wizardDialog.getReturnCode());
490:                 final Optional<Template> templateOptional = wizard.getSelectedTemplate();
491:                 assertFalse(templateOptional.isPresent());
492:         }
493:
494:         @Test
495:         public void testGetAvailableTemplates() {
496:                 final Set<EClass> subClasses = Collections.emptySet();
497:                 final Set<Template> templates = new LinkedHashSet<Template>();
498:                 // corupt eclass
499:                 final Template templateInvalid = mock(Template.class);
500:                 final EObject templateInstanceInvalid = mock(EObject.class);
501:                 when(templateInvalid.getInstance()).thenReturn(templateInstanceInvalid);
502:                 when(templateInstanceInvalid.eClass()).thenThrow(new IllegalArgumentException());
503:                 templates.add(templateInvalid);
504:                 // valid template
505:                 final Template templateValid = mock(Template.class);
506:                 final EObject templateInstanceValid = mock(EObject.class);
507:                 when(templateValid.getInstance()).thenReturn(templateInstanceValid);
508:                 when(templateInstanceValid.eClass()).thenReturn(EcorePackage.eINSTANCE.getEAttribute());
509:                 templates.add(templateValid);
510:
511:                 final SelectSubclassAndTemplateWizard wizard = new SelectSubclassAndTemplateWizard("", subClasses, templates, //$NON-NLS-1$
512:                         localizationService);
513:                 final Set<Template> filteredTemplates = wizard.getAvailableTemplates(EcorePackage.eINSTANCE.getEAttribute());
514:                 assertEquals(1, filteredTemplates.size());
515:                 assertEquals(templateValid, filteredTemplates.iterator().next());
516:         }
517:
518:         /**
519:          * Gets the button composite containing the cancel, finish, and sometimes the back and next buttons.
520:          *
521:          * @param wizardDialog The {@link WizardDialog} to get the button composite from
522:          * @return The {@link Composite} containing the wizard's buttons
523:          */
524:         private Composite getButtonComposite(final WizardDialog wizardDialog) {
525:                 final Composite buttonBar = (Composite) wizardDialog.buttonBar;
526:                 // take the second as the first is a toolbar with help entries
527:                 final Composite buttonComposite = (Composite) buttonBar.getChildren()[1];
528:                 return buttonComposite;
529:         }
530:
531:         /**
532:          * Open the wizard in a wizard dialog and wait for the UI to finish opening it.
533:          *
534:          * @param wizard The wizard to open
535:          * @return The opened {@link WizardDialog}
536:          */
537:         private WizardDialog openWizardDialog(final Wizard wizard) {
538:                 final WizardDialog wizardDialog = new WizardDialog(shell, wizard);
539:                 wizardDialog.setBlockOnOpen(false);
540:                 wizardDialog.open();
541:                 SWTTestUtil.waitForUIThread();
542:                 return wizardDialog;
543:         }
544:
545:         /**
546:          * Creates a new {@link Template} with the given name and an instance of the given {@link EClass}
547:          *
548:          * @param name The template's name
549:          * @param instanceClass The instance's type
550:          * @return The created {@link Template}
551:          */
552:         private Template createTemplate(String name, EClass instanceClass) {
553:                 final Template template = DataTemplateFactory.eINSTANCE.createTemplate();
554:                 template.setName(name);
555:                 final EObject instance = instanceClass.getEPackage().getEFactoryInstance().create(instanceClass);
556:                 template.setInstance(instance);
557:                 return template;
558:         }
559: }