source: trunk/org.modelica.mdt.test/src/org/modelica/mdt/test/TestNewClassWizard.java @ 101

Last change on this file since 101 was 101, checked in by boris, 19 years ago
  • new package org.modelica.mdt.ui.wizards created and current two wizards moved to it
File size: 9.6 KB
Line 
1/*
2 * This file is part of Modelica Development Tooling.
3 *
4 * Copyright (c) 2005, Linköpings universitet, Department of
5 * Computer and Information Science, PELAB
6 *
7 * All rights reserved.
8 *
9 * (The new BSD license, see also
10 * http://www.opensource.org/licenses/bsd-license.php)
11 *
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are
15 * met:
16 *
17 * * Redistributions of source code must retain the above copyright
18 *   notice, this list of conditions and the following disclaimer.
19 *
20 * * Redistributions in binary form must reproduce the above copyright
21 *   notice, this list of conditions and the following disclaimer in
22 *   the documentation and/or other materials provided with the
23 *   distribution.
24 *
25 * * Neither the name of Linköpings universitet nor the names of its
26 *   contributors may be used to endorse or promote products derived from
27 *   this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42package org.modelica.mdt.test;
43
44import java.io.IOException;
45import java.io.InputStream;
46
47import org.eclipse.core.resources.IFile;
48import org.eclipse.core.resources.IProject;
49import org.eclipse.core.resources.ResourcesPlugin;
50import org.eclipse.core.runtime.CoreException;
51import org.eclipse.jface.viewers.StructuredSelection;
52import org.eclipse.jface.wizard.IWizard;
53import org.eclipse.swt.widgets.Button;
54import org.eclipse.swt.widgets.Combo;
55import org.eclipse.swt.widgets.Text;
56import org.eclipse.ui.PlatformUI;
57import org.modelica.mdt.core.ModelicaCore;
58import org.modelica.mdt.ui.wizards.NewClassWizard;
59
60import abbot.tester.swt.ButtonTester;
61import abbot.tester.swt.ComboTester;
62import abbot.tester.swt.TextTester;
63
64import junit.framework.TestCase;
65
66/**
67 * @author Elmir Jagudin
68 *
69 * Test the New Class Wizard functionality
70 */
71public class TestNewClassWizard extends TestCase
72{
73    private static final String PROJECT_NAME = "testNewCLassWizard";
74    private StructuredSelection fileDestination; 
75   
76    private IProject project;
77    private TextTester ttester;
78    private ButtonTester btester;
79   
80    private Text sourceFolder;
81    private Text className;
82    private Combo classType;
83    private Button initialEquation;
84    private Button partialClass;
85    private Button finish;
86   
87   
88
89    @Override
90    protected void setUp() throws Exception
91    {
92        /*
93         * setup project
94         */
95        project = 
96            ModelicaCore.createProject(PROJECT_NAME,
97                    PlatformUI.getWorkbench().getActiveWorkbenchWindow());
98        assertNotNull("failed to create project", project);
99       
100        /*
101         * create the selection that points at the root of the created project
102         */
103        fileDestination = 
104            new StructuredSelection(ResourcesPlugin.getWorkspace().getRoot().getProject(PROJECT_NAME));
105       
106        /*
107         * setup some testing support objects
108         */
109        ttester = TextTester.getTextTester();
110        btester = ButtonTester.getButtonTester();
111               
112    }
113   
114    private boolean compareContent(IFile file, String expectedContent)
115    {
116        InputStream fileContent = null;
117       
118        try
119        {
120            fileContent = file.getContents();
121        }
122        catch (CoreException e) 
123        {
124            fail("could not fetch contents of the created class");
125        }
126
127        byte[] buf = new byte[expectedContent.length()];
128       
129        try
130        {
131            fileContent.read(buf);
132            int i = fileContent.read();
133           
134            assertEquals("file is to long", i, -1);
135        }
136        catch (IOException e)
137        {
138            fail("could not read contents of the file");
139        }
140       
141        return expectedContent.equals(new String(buf));
142    }
143
144   
145    public void openWizardAndFetchWidgets()
146    {
147        /*
148         * pop-up the wizard
149         */
150        IWizard wizard = 
151            Utility.openWizard("org.modelica.mdt.NewClassWizard", fileDestination);
152        assertFalse(wizard.canFinish());
153
154        /* fetch widgets */
155        className = TextTester.getInstrumentedText(NewClassWizard.CLASS_NAME_TAG);
156        sourceFolder = TextTester.getInstrumentedText(NewClassWizard.SOURCE_FOLDER_TAG);
157        initialEquation = ButtonTester.getInstrumentedButton(NewClassWizard.INITIAL_EQUATION_TAG);             
158        partialClass =  ButtonTester.getInstrumentedButton(NewClassWizard.PARTIAL_CLASS_TAG);
159        finish = Utility.findFinishButton();
160       
161        /* make some checks on the state of the wizards */
162        assertEquals("Wrong source folder selected", 
163                sourceFolder.getText(), PROJECT_NAME);     
164        assertEquals("Junk present in class name field", className.getText(), "");
165        assertFalse("initial equation unexpectedly selected", initialEquation.getSelection());
166        assertFalse("partial class unexpectedly selected", partialClass.getSelection());
167        assertFalse("Finish button not disabled", finish.getEnabled());
168       
169
170    }
171   
172    public void testCreateModel()
173    {
174        openWizardAndFetchWidgets();       
175       
176        /*
177         * create model
178         */
179        ttester.actionEnterText(className, "m1");
180        assertTrue(finish.getEnabled());
181
182        /* wait for the name change to propogate to enable the finish button */
183        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
184        btester.actionClick(finish);
185       
186        while(!project.isOpen()){ Utility.sleep(this, 100); }       
187       
188        /*
189         * check that the generated source code is sane
190         */
191        boolean same = 
192            compareContent(project.getFile("m1.mo"), 
193                "model m1\n"+
194                "\n"+
195                "equation\n"+
196                "\n"+
197                "end m1;");
198        assertTrue("unexpected conted created in the source file", same);
199   
200    }
201       
202    public void testCreateModelWithInitEquation()
203    {
204        openWizardAndFetchWidgets();       
205       
206        /*
207         * create model
208         */
209        ttester.actionEnterText(className, "m2");
210        assertTrue(finish.getEnabled());
211       
212        btester.actionClick(initialEquation);
213       
214        assertTrue(initialEquation.getSelection());
215
216        /* wait for the name change to propogate to enable the finish button */
217        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
218        btester.actionClick(finish);
219       
220       
221        while(!project.isOpen()){ Utility.sleep(this, 100); }
222       
223        /*
224         * check that the generated source code is sane
225         */
226        boolean same = 
227            compareContent(project.getFile("m2.mo"), 
228                "model m2\n"+
229                "\n"+
230                "equation\n"+
231                "\n"+
232                "initial equation\n"+
233                "\n"+
234                "end m2;");
235        assertTrue("unexpected conted created in the source file", same);
236    }
237
238    public void testCreatePartialModel()
239    {
240        openWizardAndFetchWidgets();       
241       
242        String name = "m3";
243        /*
244         * create model
245         */
246        ttester.actionEnterText(className, name);
247        assertTrue(finish.getEnabled());
248       
249        btester.actionClick(partialClass);
250       
251        assertTrue(partialClass.getSelection());
252
253        /* wait for the name change to propogate to enable the finish button */
254        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
255        btester.actionClick(finish);
256       
257       
258        while(!project.isOpen()){ Utility.sleep(this, 100); }
259       
260        /*
261         * check that the generated source code is sane
262         */
263        boolean same = 
264            compareContent(project.getFile(name + ".mo"), 
265                "partial model "+ name +"\n"+
266                "\n"+
267                "equation\n"+
268                "\n"+
269                "end "+ name + ";");
270        assertTrue("unexpected conted created in the source file", same);
271   
272    }
273
274    public void testCreatePartialModelWithInitBlock()
275    {
276        openWizardAndFetchWidgets();       
277       
278        String name = "m4";
279        /*
280         * create model
281         */
282        ttester.actionEnterText(className, name);
283        assertTrue(finish.getEnabled());
284       
285        btester.actionClick(partialClass);     
286        assertTrue(partialClass.getSelection());
287
288        btester.actionClick(initialEquation);       
289        assertTrue(initialEquation.getSelection());
290       
291       
292        /* wait for the name change to propogate to enable the finish button */
293        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
294        btester.actionClick(finish);
295       
296       
297        while(!project.isOpen()){ Utility.sleep(this, 100); }
298       
299        /*
300         * check that the generated source code is sane
301         */
302        boolean same = 
303            compareContent(project.getFile(name + ".mo"), 
304                "partial model "+ name +"\n"+
305                "\n"+
306                "equation\n"+
307                "\n"+
308                "initial equation\n"+
309                "\n"+
310                "end "+ name + ";");
311        assertTrue("unexpected conted created in the source file", same);
312   
313       
314    }
315
316    public void testCreateClass()
317    {
318   
319    }
320
321    public void testCreateClassWithInitBlock()
322    {
323       
324    }
325
326    public void testCreatePartialClass()
327    {
328       
329    }
330
331    public void testCreatePartialClassWithInitBlock()
332    {
333       
334    }
335   
336   
337    public void testCreateConnector()
338    {
339       
340    }
341
342    public void testCreatePartialConnector()
343    {
344       
345    }
346
347    public void testCreateBlock()
348    {
349       
350    }
351    public void testCreateBlockWithInitBlock()
352    {
353       
354    }
355
356    public void testCreatePartialBlock()
357    {
358       
359    }
360    public void testCreatePartialBlockWithInitBlock()
361    {
362       
363    }
364
365    public void testCreateType()
366    {
367       
368    }
369
370    public void testCreateFunction()
371    {
372       
373    }
374
375    public void testCreateFunctionWithExternalBody()
376    {
377       
378    }
379
380     //some ideas for more tests
381        /*
382         * TODO enter some text into source folder field and
383         * check the error/warning messages
384         */
385       
386        /*
387         * TODO test to create a class in a sub-directory
388         */
389}
Note: See TracBrowser for help on using the repository browser.