improved layout for view.
[phpeclipse.git] / net.sourceforge.phpeclipse.phpunit / src / net / sourceforge / phpeclipse / phpunit / PHPUnitView.java
1 package net.sourceforge.phpeclipse.phpunit;
2
3
4 import org.eclipse.swt.SWT;
5 import org.eclipse.swt.events.MouseEvent;
6 import org.eclipse.swt.events.MouseListener;
7 import org.eclipse.swt.layout.FillLayout;
8 import org.eclipse.swt.layout.GridData;
9 import org.eclipse.swt.layout.GridLayout;
10 import org.eclipse.swt.widgets.Button;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.swt.widgets.Group;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.ProgressBar;
15 import org.eclipse.swt.widgets.Text;
16 import org.eclipse.ui.part.ViewPart;
17
18 /**
19  * @author Ali Echihabi
20  *
21  * To change the template for this generated type comment go to
22  * Window>Preferences>Java>Code Generation>Code and Comments
23  */
24 /*
25  * Created on May 22, 2004
26  *
27  * To change the template for this generated file go to
28  * Window>Preferences>Java>Code Generation>Code and Comments
29  */
30
31 /**
32  * @author Ali Echihabi (ali_echihabi@ieee.org)
33  *
34  * Plugin for PHP unit Testing.
35  * www.phpeclipse.de
36  * 
37  * This the main view showing the progress and reports.
38  * 
39  */
40
41
42 public class PHPUnitView extends ViewPart {
43
44         /*
45          * like J Unit
46          * a tree.
47          * The first level nodes are the test suites.
48          * children are nested test suites.
49          * leafs: test functions.
50          * hierarchy: package->testsuite1->testcase->test_function
51          */
52         
53         
54         private int numTests; // total number of tests
55         private int numTestsRun; // number of tests run so far
56         private int numFailures; // number of failures so far
57         private int numErrors; // number of errors so far
58         private int numPasses; // number of passes so far (they should add up)   
59         
60
61         private XMLReportHandler handler;
62
63         Label labelRuns, labelRunsVal; // Runs: 12
64         Label labelErrors, labelErrorsVal;
65         Label labelFailures, labelFailuresVal;
66         
67         Text reportArea; // TODO: replace with Tree display like JUnit
68
69         Button startButton;
70
71         public PHPUnitView() {
72                 handler = new XMLReportHandler();
73         }
74         
75
76         
77         public void createPartControl(Composite parent) {
78                 
79                 
80                 //layout: 
81                 FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
82                 parent.setLayout(fillLayout);
83                 
84                 Group progressInfoComposite = new Group(parent, SWT.SHADOW_ETCHED_IN);
85                 Group resultsInfoComposite = new Group(parent, SWT.NONE);
86                 
87                 
88                 //Build the progress info Composite             
89                 progressInfoComposite.setText("Progress:");
90                 GridLayout gridLayout = new GridLayout();
91                 gridLayout.numColumns = 1;
92                 
93                 progressInfoComposite.setLayout(gridLayout);
94                 
95                 
96                 ProgressBar progressBar = new ProgressBar(progressInfoComposite, SWT.HORIZONTAL);
97                 progressBar.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL|GridData.FILL_HORIZONTAL));
98                 
99                 Composite labelsComposite = new Composite(progressInfoComposite, SWT.NONE);
100                 labelsComposite.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL|GridData.FILL_HORIZONTAL));
101                 
102                 labelsComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
103                 
104                 labelRuns = new Label(labelsComposite, SWT.NONE);
105                 labelRuns.setText("Runs: ");
106                 labelRunsVal = new Label(labelsComposite, SWT.NONE);
107                 labelRunsVal.setText("0 / 0");
108                 
109                 labelFailures = new Label(labelsComposite, SWT.NONE);
110                 labelFailures.setText("Failures: ");
111                 labelFailuresVal = new Label(labelsComposite, SWT.NONE);
112                 labelFailuresVal.setText("0");
113                 
114                 labelErrors = new Label(labelsComposite, SWT.NONE);
115                 labelErrors.setText("Errors: ");
116                 labelErrorsVal = new Label(labelsComposite, SWT.NONE);
117                 labelErrorsVal.setText("0");
118
119                 //Build the result info composite
120                 resultsInfoComposite.setText("Results:");
121                 resultsInfoComposite.setLayout(fillLayout);
122                 
123                 reportArea = new Text(resultsInfoComposite, SWT.MULTI | SWT.BORDER |
124                 SWT.WRAP | SWT.V_SCROLL | SWT.READ_ONLY);
125                 
126                 startButton = new Button(parent, SWT.CENTER);
127                 startButton.setText("Start Tests");
128                 startButton.addMouseListener( new MouseListener() {
129
130                         public void mouseDoubleClick(MouseEvent arg0) {
131                                 // TODO Auto-generated method stub
132                                 
133                         }
134
135                         public void mouseDown(MouseEvent arg0) {
136                                 // TODO Auto-generated method stub
137                                 startTests();
138                         }
139
140                         public void mouseUp(MouseEvent arg0) {
141                                 // TODO Auto-generated method stub
142                                 
143                         }
144
145
146                         
147                         
148                         }); // end add action listener.
149
150         }
151
152         /* (non-Javadoc)
153          * @see org.eclipse.ui.IWorkbenchPart#setFocus()
154          */
155         public void setFocus() {
156                 //markTestPass("hello");
157         }
158
159         /**
160          * mark the given test as passed in the GUI.
161          * 
162          * @param testID
163          */
164         public void markTestPassed(String testID) {
165                 
166                 // testid, use it in hashmap to retrieve tree item of test and
167                 // change icon color, increment pass counter, etc...
168                 
169                 
170                 //for now:
171                 reportArea.append("test : " + testID + " passed \n");
172         }
173
174         public void markTestStarted(String testID) {
175                 
176                 reportArea.append("test started: " + testID + " \n");
177         }
178          
179         public void createNewTest(String testName, String testID) {
180           
181                 reportArea.append("new test: " + testName + " - testID " + testID + " \n");     
182         
183         }
184         
185         public void markTestFail(String testID) {
186                 reportArea.append("test "  + testID + " failed \n");
187         }
188         
189         public void markTestingFinished() {
190                 
191                 reportArea.append("end all tests \n");  
192                 
193         }
194         
195         // action to start tests:
196         private void startTests() {
197                 
198                 // preparation:
199                 // take the full test suite (could containt other test suites).
200                 // create temp php file that starts that suite and uses socketTestReport 
201                 // as a test result reporter.
202                 // add listener: localhost , port 13579
203                 // start listening at port.
204                 
205                 reportArea.append("Tests started \n");
206                 listenForReports();
207                 
208                 
209                 
210         }
211         
212         /**
213          * 
214          */
215         private void listenForReports() {
216
217
218
219                 ConnectionListener conListener = new ConnectionListener();
220                 conListener.start(this);
221
222         } //end of method
223         
224         /**
225          * handle this report: test passed, faile, end of all.
226          * @param report
227          */
228         public void handleReport(String report) {
229
230                 //delegate to the XML report handler.           
231                 //reportArea.append("msg: " + report + "\n");
232                 handler.handle(report, this);
233
234         
235                 
236         }
237         
238         /**
239          * @param command
240          * @param testCount
241          * @param testID
242          */
243         public void handleCommand(String command, String testCount, String testID) {
244
245                 if (command.equals("testStarted")) {
246                                 
247                         createNewTest("testName", testID);
248                         markTestStarted(testID);
249                                 
250                 } else if (command.equals("testFinished")) {
251                         
252                                 
253                         // do nothing wait for verdict
254                 } else if (command.equals("endAll")) {
255                                 
256                         markTestingFinished();
257                 }
258                                         
259                 
260         }
261
262         /**
263          * @param currentTestID
264          * @param verdict
265          */
266         public void setTestVerdict(String currentTestID, String verdict) {
267
268                 if( verdict.equals("passed")) 
269                         markTestPassed(currentTestID);
270                 else
271                         markTestFail(currentTestID);
272
273
274         }
275
276         /**
277          * @param currentTestID
278          * @param exception
279          */
280         public void addTestException(String currentTestID, String exception) {
281                 
282                 //TODO: decide how to show exceptions. don't show them for now.
283                 //reportArea.append("   test " + currentTestID + " exception: " + exception + "\n");
284                 
285         }
286         
287
288
289
290 } //end of class
291         
292
293
294
295