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 | |
---|
42 | package org.modelica.mdt.test; |
---|
43 | |
---|
44 | import java.util.concurrent.Semaphore; |
---|
45 | |
---|
46 | import junit.framework.Assert; |
---|
47 | |
---|
48 | import org.eclipse.core.runtime.CoreException; |
---|
49 | import org.eclipse.jface.viewers.IStructuredSelection; |
---|
50 | import org.eclipse.jface.viewers.StructuredSelection; |
---|
51 | import org.eclipse.jface.wizard.WizardDialog; |
---|
52 | import org.eclipse.swt.widgets.Button; |
---|
53 | import org.eclipse.ui.IWorkbench; |
---|
54 | import org.eclipse.ui.IWorkbenchWizard; |
---|
55 | import org.eclipse.ui.PlatformUI; |
---|
56 | import org.eclipse.ui.wizards.IWizardDescriptor; |
---|
57 | |
---|
58 | import abbot.finder.matchers.swt.TextMatcher; |
---|
59 | import abbot.finder.swt.BasicFinder; |
---|
60 | import abbot.finder.swt.MultipleWidgetsFoundException; |
---|
61 | import abbot.finder.swt.TestHierarchy; |
---|
62 | import abbot.finder.swt.WidgetNotFoundException; |
---|
63 | |
---|
64 | /** |
---|
65 | * @author Elmir Jagudin |
---|
66 | * |
---|
67 | * This class contains some utility code for assisting the testcases |
---|
68 | */ |
---|
69 | public class Utility |
---|
70 | { |
---|
71 | |
---|
72 | /** |
---|
73 | * creates and opens a wizard |
---|
74 | * |
---|
75 | * @param wizardID the ID of the wizard to create and open |
---|
76 | * @param selection the selection which is used to initialize the wizard |
---|
77 | */ |
---|
78 | public static IWorkbenchWizard openWizard(String wizardID, |
---|
79 | IStructuredSelection selection) |
---|
80 | { |
---|
81 | |
---|
82 | IWorkbench workbench = PlatformUI.getWorkbench(); |
---|
83 | IWizardDescriptor wizDesc = |
---|
84 | workbench.getNewWizardRegistry().findWizard(wizardID); |
---|
85 | Assert.assertNotNull("wizard " + wizardID + " not found", wizDesc); |
---|
86 | |
---|
87 | IWorkbenchWizard wizard = null; |
---|
88 | try |
---|
89 | { |
---|
90 | wizard = wizDesc.createWizard(); |
---|
91 | } |
---|
92 | catch (CoreException e) |
---|
93 | { |
---|
94 | Assert.fail("Could not create " + wizardID + |
---|
95 | " wizard, CoreException thrown\n" + e.getMessage()); |
---|
96 | } |
---|
97 | Assert.assertNotNull(wizard); |
---|
98 | |
---|
99 | wizard.init(workbench, selection); |
---|
100 | final WizardDialog dialog = |
---|
101 | new WizardDialog(workbench.getActiveWorkbenchWindow().getShell(), wizard); |
---|
102 | dialog.create(); |
---|
103 | |
---|
104 | final Semaphore sem = new Semaphore(0); |
---|
105 | |
---|
106 | dialog.getShell().getDisplay().syncExec(new Runnable() |
---|
107 | { |
---|
108 | public void run() |
---|
109 | { |
---|
110 | dialog.setBlockOnOpen(false); |
---|
111 | dialog.open(); |
---|
112 | sem.release(); |
---|
113 | } |
---|
114 | }); |
---|
115 | |
---|
116 | try |
---|
117 | { |
---|
118 | sem.acquire(); |
---|
119 | } |
---|
120 | catch (InterruptedException e) |
---|
121 | { |
---|
122 | Assert.fail("interruped while waiting for dialog to open"); |
---|
123 | } |
---|
124 | |
---|
125 | return wizard; |
---|
126 | } |
---|
127 | /** |
---|
128 | * creates and opens a wizard initialized with empty selection |
---|
129 | * |
---|
130 | * @param wizardID the ID of the wizard to create and open |
---|
131 | */ |
---|
132 | public static IWorkbenchWizard openWizard(String wizardID) |
---|
133 | { |
---|
134 | return openWizard(wizardID, StructuredSelection.EMPTY); |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * |
---|
139 | * @return currently displayed button with text '&Finish' |
---|
140 | * this function will fail if not exactly one finish button is |
---|
141 | * on the screen |
---|
142 | */ |
---|
143 | public static Button findFinishButton() |
---|
144 | { |
---|
145 | BasicFinder finder = /* find finish button */ |
---|
146 | new BasicFinder(new TestHierarchy(PlatformUI.getWorkbench().getDisplay())); |
---|
147 | |
---|
148 | try |
---|
149 | { |
---|
150 | return (Button) finder.find(new TextMatcher("&Finish")); |
---|
151 | } |
---|
152 | catch (WidgetNotFoundException e) |
---|
153 | { |
---|
154 | Assert.fail("Finish button not found."); |
---|
155 | } |
---|
156 | catch (MultipleWidgetsFoundException e) |
---|
157 | { |
---|
158 | Assert.fail("Multiple finish buttons found."); |
---|
159 | } |
---|
160 | |
---|
161 | Assert.fail("this is not happening"); |
---|
162 | return null; |
---|
163 | } |
---|
164 | /* |
---|
165 | * sleeps for approx time seconds |
---|
166 | * this method does not garantee that it will sleep any particular time |
---|
167 | */ |
---|
168 | public static void sleep(Object mutex, long time) |
---|
169 | { |
---|
170 | try |
---|
171 | { |
---|
172 | synchronized (mutex) |
---|
173 | { |
---|
174 | mutex.wait(time); |
---|
175 | } |
---|
176 | } |
---|
177 | catch (InterruptedException e) |
---|
178 | { |
---|
179 | e.printStackTrace(); |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|