1 | package org.modelica.mdt.test; |
---|
2 | |
---|
3 | import java.util.concurrent.Semaphore; |
---|
4 | |
---|
5 | import org.eclipse.core.resources.IProject; |
---|
6 | import org.eclipse.core.resources.ResourcesPlugin; |
---|
7 | import org.eclipse.core.runtime.CoreException; |
---|
8 | import org.eclipse.jface.viewers.StructuredSelection; |
---|
9 | import org.eclipse.jface.wizard.WizardDialog; |
---|
10 | import org.eclipse.swt.widgets.Display; |
---|
11 | import org.eclipse.swt.widgets.Text; |
---|
12 | import org.eclipse.ui.IWorkbench; |
---|
13 | import org.eclipse.ui.IWorkbenchWizard; |
---|
14 | import org.eclipse.ui.PlatformUI; |
---|
15 | import org.eclipse.ui.wizards.IWizardDescriptor; |
---|
16 | import org.modelica.mdt.NewProjectWizard; |
---|
17 | |
---|
18 | import abbot.tester.swt.TextTester; |
---|
19 | |
---|
20 | import junit.framework.TestCase; |
---|
21 | |
---|
22 | public class TestNewProjectWizard extends TestCase |
---|
23 | { |
---|
24 | /* name of the project that test creates */ |
---|
25 | public static final String PROJECT_NAME = "test"; |
---|
26 | |
---|
27 | public void testWizard() |
---|
28 | { |
---|
29 | /* |
---|
30 | * init and display wizard |
---|
31 | */ |
---|
32 | IWorkbench workbench = PlatformUI.getWorkbench(); |
---|
33 | IWizardDescriptor wizDesc = |
---|
34 | workbench.getNewWizardRegistry().findWizard("org.modelica.mdt.NewProjectWizard"); |
---|
35 | assertNotNull("new project wizard not found", wizDesc); |
---|
36 | |
---|
37 | IWorkbenchWizard wizard = null; |
---|
38 | try |
---|
39 | { |
---|
40 | wizard = wizDesc.createWizard(); |
---|
41 | } |
---|
42 | catch (CoreException e) |
---|
43 | { |
---|
44 | fail("Could not create new project wizard, CoreException thrown\n" |
---|
45 | + e.getMessage()); |
---|
46 | } |
---|
47 | assertNotNull(wizard); |
---|
48 | |
---|
49 | wizard.init(workbench, StructuredSelection.EMPTY); |
---|
50 | final WizardDialog dialog = |
---|
51 | new WizardDialog(workbench.getActiveWorkbenchWindow().getShell(), wizard); |
---|
52 | dialog.create(); |
---|
53 | |
---|
54 | openDialog(dialog); |
---|
55 | |
---|
56 | assertFalse(wizard.canFinish()); |
---|
57 | |
---|
58 | /* fetch project name text field */ |
---|
59 | Text name = TextTester.getInstrumentedText(NewProjectWizard.PROJECT_NAME_TAG); |
---|
60 | /* enter project name */ |
---|
61 | TextTester.getTextTester().actionEnterText(name, PROJECT_NAME); |
---|
62 | |
---|
63 | assertTrue(wizard.canFinish()); |
---|
64 | |
---|
65 | /* create project */ |
---|
66 | boolean finishRes = wizard.performFinish(); |
---|
67 | assertTrue("New wizard dialog failed to finish", finishRes); |
---|
68 | |
---|
69 | /* check that project was created */ |
---|
70 | IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(PROJECT_NAME); |
---|
71 | |
---|
72 | assertTrue("new wizard fail to create project", project.exists()); |
---|
73 | assertTrue("new wizard didnt open the created project", project.isOpen()); |
---|
74 | |
---|
75 | /* check that it have modelica nature */ |
---|
76 | try |
---|
77 | { |
---|
78 | assertTrue("modelica nature not added to the project", |
---|
79 | project.hasNature("org.modelica.mdt.ModelicaNature")); |
---|
80 | assertTrue("modelica nature was not enabled on the project", |
---|
81 | project.isNatureEnabled("org.modelica.mdt.ModelicaNature")); |
---|
82 | |
---|
83 | } |
---|
84 | catch (CoreException e) |
---|
85 | { |
---|
86 | fail("CoreException thrown while probing for modelica nature " + e.getMessage()); |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | /* clean up */ |
---|
91 | dialog.close(); |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * opens (runs dialog.open() method ) a dialog in UI thread and waits until it |
---|
96 | * is open before returning |
---|
97 | * |
---|
98 | * @param dialog the dialog to open, it is assumed that dialog is fully initialized |
---|
99 | * |
---|
100 | */ |
---|
101 | private void openDialog(final WizardDialog dialog) |
---|
102 | { |
---|
103 | final Semaphore sem = new Semaphore(0); |
---|
104 | |
---|
105 | dialog.getShell().getDisplay().syncExec(new Runnable() |
---|
106 | { |
---|
107 | public void run() |
---|
108 | { |
---|
109 | dialog.setBlockOnOpen(false); |
---|
110 | dialog.open(); |
---|
111 | sem.release(); |
---|
112 | } |
---|
113 | }); |
---|
114 | |
---|
115 | try |
---|
116 | { |
---|
117 | sem.acquire(); |
---|
118 | } |
---|
119 | catch (InterruptedException e) |
---|
120 | { |
---|
121 | fail("interruped while waiting for dialog to open"); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | } |
---|