Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 117a7b3beec673396371bf5ea2f3822817a2b57e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*******************************************************************************
 * Copyright (c) 2011 Anton Gorenkov 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Anton Gorenkov - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.testsrunner.internal.ui.view;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.internal.ui.viewsupport.ColoringLabelProvider;
import org.eclipse.cdt.testsrunner.internal.TestsRunnerPlugin;
import org.eclipse.cdt.testsrunner.internal.ui.view.actions.CopySelectedTestsAction;
import org.eclipse.cdt.testsrunner.internal.ui.view.actions.RedebugSelectedAction;
import org.eclipse.cdt.testsrunner.internal.ui.view.actions.RelaunchSelectedAction;
import org.eclipse.cdt.testsrunner.internal.ui.view.actions.RerunSelectedAction;
import org.eclipse.cdt.testsrunner.internal.ui.view.actions.TestsHierarchyCollapseAllAction;
import org.eclipse.cdt.testsrunner.internal.ui.view.actions.TestsHierarchyExpandAllAction;
import org.eclipse.cdt.testsrunner.model.IModelVisitor;
import org.eclipse.cdt.testsrunner.model.ITestCase;
import org.eclipse.cdt.testsrunner.model.ITestItem;
import org.eclipse.cdt.testsrunner.model.ITestMessage;
import org.eclipse.cdt.testsrunner.model.ITestSuite;
import org.eclipse.cdt.testsrunner.model.ITestingSession;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.actions.ActionFactory;

/**
 * Shows the tests hierarchy in a flat or hierarchical view.
 */
public class TestsHierarchyViewer {
	
	/**
	 * The content provider for the tests hierarchy viewer.
	 */
	private class TestTreeContentProvider implements ITreeContentProvider {

		/**
		 * Utility class: recursively collects all the test cases of the
		 * specified test item.
		 * 
		 * It is used for flat view of tests hierarchy.
		 */
		private class TestCasesCollector implements IModelVisitor {
			
			public List<ITestCase> testCases = new ArrayList<ITestCase>();
			
			@Override
			public void visit(ITestCase testCase) {
				testCases.add(testCase);
			}
			
			@Override
			public void visit(ITestMessage testMessage) {}
			@Override
			public void visit(ITestSuite testSuite) {}
			@Override
			public void leave(ITestSuite testSuite) {}
			@Override
			public void leave(ITestCase testCase) {}
			@Override
			public void leave(ITestMessage testMessage) {}
		}
		
		@Override
		public Object[] getChildren(Object parentElement) {
			return ((ITestItem) parentElement).getChildren();
		}

		@Override
		public Object[] getElements(Object rootTestSuite) {
			if (showTestsHierarchy) {
				return getChildren(rootTestSuite);
			} else {
				TestCasesCollector testCasesCollector = new TestCasesCollector();
				((ITestItem)rootTestSuite).visit(testCasesCollector);
				return testCasesCollector.testCases.toArray();
			}
		}

		@Override
		public Object getParent(Object object) {
			return ((ITestItem) object).getParent();
		}

		@Override
		public boolean hasChildren(Object object) {
			return ((ITestItem) object).hasChildren();
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}

		@Override
		public void dispose() {}
	}

	/**
	 * The label provider for the tests hierarchy viewer.
	 */
	private class TestLabelProvider extends LabelProvider implements IStyledLabelProvider {

		/** Images for the test cases with the different statuses. */
		private Map<ITestItem.Status, Image> testCaseImages = new HashMap<ITestItem.Status, Image>();
		{
			testCaseImages.put(ITestItem.Status.NotRun, TestsRunnerPlugin.createAutoImage("obj16/test_notrun.gif")); //$NON-NLS-1$
			testCaseImages.put(ITestItem.Status.Skipped, TestsRunnerPlugin.createAutoImage("obj16/test_skipped.gif")); //$NON-NLS-1$
			testCaseImages.put(ITestItem.Status.Passed, TestsRunnerPlugin.createAutoImage("obj16/test_passed.gif")); //$NON-NLS-1$
			testCaseImages.put(ITestItem.Status.Failed, TestsRunnerPlugin.createAutoImage("obj16/test_failed.gif")); //$NON-NLS-1$
			testCaseImages.put(ITestItem.Status.Aborted, TestsRunnerPlugin.createAutoImage("obj16/test_aborted.gif")); //$NON-NLS-1$
		}
		
		/** Running test case image (overrides the test case status image). */
		private Image testCaseRunImage = TestsRunnerPlugin.createAutoImage("obj16/test_run.gif"); //$NON-NLS-1$

		/** Images for the test suites with the different statuses. */
		private Map<ITestItem.Status, Image> testSuiteImages = new HashMap<ITestItem.Status, Image>();
		{
			// NOTE: There is no skipped-icon for test suite, but it seems it is not a problem
			testSuiteImages.put(ITestItem.Status.NotRun, TestsRunnerPlugin.createAutoImage("obj16/tsuite_notrun.gif")); //$NON-NLS-1$
			testSuiteImages.put(ITestItem.Status.Skipped, TestsRunnerPlugin.createAutoImage("obj16/tsuite_notrun.gif")); //$NON-NLS-1$
			testSuiteImages.put(ITestItem.Status.Passed, TestsRunnerPlugin.createAutoImage("obj16/tsuite_passed.gif")); //$NON-NLS-1$
			testSuiteImages.put(ITestItem.Status.Failed, TestsRunnerPlugin.createAutoImage("obj16/tsuite_failed.gif")); //$NON-NLS-1$
			testSuiteImages.put(ITestItem.Status.Aborted, TestsRunnerPlugin.createAutoImage("obj16/tsuite_aborted.gif")); //$NON-NLS-1$
		}

		/** Running test suite image (overrides the test suite status image). */
		private Image testSuiteRunImage = TestsRunnerPlugin.createAutoImage("obj16/tsuite_run.gif"); //$NON-NLS-1$
		
		/** Small optimization: the last test item cache */
		private ITestItem lastTestItemCache = null;

		/** Small optimization: test path for the last test item is cache */
		private String lastTestItemPathCache = null;


	    @Override
		public Image getImage(Object element) {
	    	Map<ITestItem.Status, Image> imagesMap = null;
	    	Image runImage = null;
	    	if (element instanceof ITestCase) {
	    		imagesMap = testCaseImages;
	    		runImage = testCaseRunImage;
	    		
	    	} else if (element instanceof ITestSuite) {
	    		imagesMap = testSuiteImages;
	    		runImage = testSuiteRunImage;
	    	}
	    	if (imagesMap != null) {
	    		ITestItem testItem = (ITestItem)element;
				if (testingSession.getModelAccessor().isCurrentlyRunning(testItem)) {
					return runImage;
				}
				return imagesMap.get(testItem.getStatus());
	    	}
	    	
	    	return null;
	    }

		@Override
		public String getText(Object element) {
			ITestItem testItem = (ITestItem)element;
			StringBuilder sb = new StringBuilder();
			sb.append(testItem.getName());
			if (!showTestsHierarchy) {
				appendTestItemPath(sb, testItem);
			}
			if (showTime) {
				sb.append(getTestingTimeString(element));
			}
			return sb.toString();
		}

		@Override
		public StyledString getStyledText(Object element) {
			ITestItem testItem = (ITestItem)element;
			StringBuilder labelBuf = new StringBuilder();
			labelBuf.append(testItem.getName());
			StyledString name = new StyledString(labelBuf.toString());
			if (!showTestsHierarchy) {
				appendTestItemPath(labelBuf, testItem);
				name = StyledCellLabelProvider.styleDecoratedString(labelBuf.toString(), StyledString.QUALIFIER_STYLER, name);
			}
			if (showTime) {
				String time = getTestingTimeString(element);
				labelBuf.append(time);
				name = StyledCellLabelProvider.styleDecoratedString(labelBuf.toString(), StyledString.COUNTER_STYLER, name);
			}
			return name;
		}
		
		/**
		 * Appends path to the parent of the specified test item. Also
		 * implements caching of the last path (cause the test item parent is
		 * often the same).
		 * 
		 * @param sb string builder to append test item path to
		 * @param testItem specified test item
		 */
		private void appendTestItemPath(StringBuilder sb, ITestItem testItem) {
			ITestSuite testItemParent = testItem.getParent();
			if (lastTestItemCache != testItemParent) {
				lastTestItemCache = testItemParent;
				lastTestItemPathCache = TestPathUtils.getTestItemPath(lastTestItemCache);
			}
			sb.append(MessageFormat.format(
					UIViewMessages.TestsHierarchyViewer_test_path_format, 
					new Object[] { lastTestItemPathCache }
			));
		}
		
		/**
		 * Returns the execution time suffix for the test item.
		 * 
		 * @param element test item
		 * @return execution time suffix
		 */
		private String getTestingTimeString(Object element) {
			return (element instanceof ITestItem)
				? MessageFormat.format(UIViewMessages.TestsHierarchyViewer_test_time_format, Double.toString(((ITestItem)element).getTestingTime()/1000.0))
				: ""; //$NON-NLS-1$
		}
		
	}
	
	/**
	 * Filters passed test cases and test suites.
	 */
	private class FailedOnlyFilter extends ViewerFilter {

		@Override
		public boolean select(Viewer viewer, Object parentElement, Object element) {
			return ((ITestItem)element).getStatus().isError();
		}
	}

	/** Testing session to show hierarchy of. */
	private ITestingSession testingSession;
	
	/** Main widget. */
	private TreeViewer treeViewer;
	
	/** Specifies whether test items execution time should be shown in hierarchy. */
	private boolean showTime = true;

	/** Specifies whether tests hierarchy should be shown in flat or hierarchical view. */
	private boolean showTestsHierarchy = true;
	
	/** Failed only tree filter instance. Created on first demand. */
	private FailedOnlyFilter failedOnlyFilter = null;
	
	/** System clipboard access to provide copy operations. */
	private Clipboard clipboard;
	
	// Context menu actions
	private Action expandAllAction;
	private Action collapseAllAction;
	private Action copyAction;
	private RelaunchSelectedAction rerunAction;
	private RelaunchSelectedAction redebugAction;

	
	public TestsHierarchyViewer(Composite parent, IViewSite viewSite, Clipboard clipboard) {
		this.clipboard = clipboard;
		treeViewer = new TreeViewer(parent, SWT.V_SCROLL | SWT.MULTI);
		treeViewer.setContentProvider(new TestTreeContentProvider());
		treeViewer.setLabelProvider(new ColoringLabelProvider(new TestLabelProvider()));
		initContextMenu(viewSite);
	}
	
	/**
	 * Initializes the viewer context menu. 
	 * 
	 * @param viewSite view
	 */
	private void initContextMenu(IViewSite viewSite) {
		expandAllAction = new TestsHierarchyExpandAllAction(treeViewer);
		collapseAllAction = new TestsHierarchyCollapseAllAction(treeViewer);
		copyAction = new CopySelectedTestsAction(treeViewer, clipboard);
		rerunAction = new RerunSelectedAction(testingSession, treeViewer);
		redebugAction = new RedebugSelectedAction(testingSession, treeViewer);

		MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
		menuMgr.addMenuListener(new IMenuListener() {
			@Override
			public void menuAboutToShow(IMenuManager manager) {
				handleMenuAboutToShow(manager);
			}
		});
		viewSite.registerContextMenu(menuMgr, treeViewer);
		Menu menu = menuMgr.createContextMenu(treeViewer.getTree());
		treeViewer.getTree().setMenu(menu);

		menuMgr.add(copyAction);
		menuMgr.add(new Separator());
		menuMgr.add(rerunAction);
		menuMgr.add(redebugAction);
		menuMgr.add(new Separator());
		menuMgr.add(expandAllAction);
		menuMgr.add(collapseAllAction);

		IActionBars actionBars = viewSite.getActionBars();
		actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction);
		actionBars.updateActionBars();
	}
	
	/**
	 * Handles the context menu showing.
	 * 
	 * @param manager context menu manager
	 */
	private void handleMenuAboutToShow(IMenuManager manager) {
		IStructuredSelection selection = (IStructuredSelection)treeViewer.getSelection();
		boolean isRelaunchEnabledForSelection = !selection.isEmpty() && 
				(testingSession.getTestsRunnerProviderInfo().isAllowedMultipleTestFilter() || (selection.size() == 1));
		rerunAction.setEnabled(isRelaunchEnabledForSelection);
		rerunAction.setTestingSession(testingSession);
		redebugAction.setEnabled(isRelaunchEnabledForSelection);
		redebugAction.setTestingSession(testingSession);
		copyAction.setEnabled(!selection.isEmpty());

		boolean hasAnything = treeViewer.getInput() != null;
		expandAllAction.setEnabled(hasAnything);
		collapseAllAction.setEnabled(hasAnything);
	}

	/**
	 * Sets the testing session to show.
	 * 
	 * @param testingSession testing session or null to set default empty
	 * session
	 */
	public void setTestingSession(ITestingSession testingSession) {
		this.testingSession = testingSession;
		treeViewer.setInput(testingSession != null ? testingSession.getModelAccessor().getRootSuite() : null);
	}
	
	/**
	 * Provides access to the main widget of the tests hierarchy viewer.
	 * 
	 * @return main widget of the tests hierarchy viewer
	 */
	public TreeViewer getTreeViewer() {
		return treeViewer;
	}

	/**
	 * Move the selection to the next failed test case.
	 */
	public void showNextFailure() {
		showFailure(true);
	}
	
	/**
	 * Move the selection to the previous failed test case.
	 */
	public void showPreviousFailure() {
		showFailure(false);
	}
	
	/**
	 * Common implementation for movement the selection to the next or previous
	 * failed test case.
	 * 
	 * @param next true if the next failed test case should be selected and false otherwise
	 */
	private void showFailure(boolean next) {
		IStructuredSelection selection = (IStructuredSelection) getTreeViewer().getSelection();
		ITestItem selected = (ITestItem) selection.getFirstElement();
		ITestItem failedItem;

		if (selected == null) {
			ITestItem rootSuite = (ITestItem)treeViewer.getInput();
			// For next element we should also check its children, for previous shouldn't.
			failedItem = findFailedImpl(rootSuite, null, next, next);
		} else {
			// For next element we should also check its children, for previous shouldn't.
			failedItem = findFailedImpl(selected.getParent(), selected, next, next);
		}

		if (failedItem != null)
			getTreeViewer().setSelection(new StructuredSelection(failedItem), true);
	}
	
	/**
	 * Returns the next or previous failed test case relatively to the
	 * <code>currItem</code> that should be a child of <code>parentItem</code>.
	 * If the such item was not found through the children, it steps up to the
	 * parent and continues search.
	 * 
	 * @param parentItem parent test item to the current one
	 * @param currItem current item search should be started from or null if
	 * there is no any
	 * @param next true if the next failed test case should be looked for and
	 * false otherwise
	 * @param checkCurrentChild specifies whether the search should be also
	 * through the children for the current item
	 * @return found item or null
	 */
	private ITestItem findFailedImpl(ITestItem parentItem, ITestItem currItem, boolean next, boolean checkCurrentChild) {
		ITestItem result = findFailedChild(parentItem, currItem, next, checkCurrentChild);
		if (result != null) {
			return result;
		}
		// Nothing found at this level - try to step up
		ITestSuite grandParentItem = parentItem.getParent();
		if (grandParentItem != null) {
			return findFailedImpl(grandParentItem, parentItem, next, false);
		}
		return null;
	}
	
	/**
	 * Returns the next or previous failed test case relatively to the
	 * <code>currItem</code> that should be a child of <code>parentItem</code>.
	 * Note that unlike <code>findFailedImpl()</code> this method search only
	 * through the children items.
	 * 
	 * @param parentItem parent test item to the current one
	 * @param currItem current item search should be started from or null if
	 * there is no any
	 * @param next true if the next failed test case should be looked for and
	 * false otherwise
	 * @param checkCurrentChild specifies whether the search should be also
	 * through the children for the current item
	 * @return found item or null
	 */
	private ITestItem findFailedChild(ITestItem parentItem, ITestItem currItem, boolean next, boolean checkCurrentChild) {
		ITestItem[] children = parentItem.getChildren();
		boolean doSearch = (currItem == null);
		int increment = next ? 1 : -1;
		int startIndex = next ? 0 : children.length-1;
		int endIndex = next ? children.length : -1;
		for (int index = startIndex; index != endIndex; index += increment) {
			ITestItem item = children[index];
			// Check element
			if (doSearch) {
				if (item instanceof ITestCase && item.getStatus().isError()) {
					return item;
				}
			}
			// If children of current element should be checked we should enable search here (if necessary)
			if (checkCurrentChild && item == currItem) {
				doSearch = true;
			}
			// Search element's children
			if (doSearch) {
				ITestItem result = findFailedChild(item, null, next, checkCurrentChild);
				if (result != null) {
					return result;
				}
			}
			// If children of current element should NOT be checked we should enable search here
			if (!checkCurrentChild && item == currItem) {
				doSearch = true;
			}
		}
		return null;
	}

	/**
	 * Returns whether test items execution time should be shown in tests
	 * hierarchy.
	 * 
	 * @return true if time should be shown and false otherwise
	 */
	public boolean showTime() {
		return showTime;
	}

	/**
	 * Sets whether test items execution time should be shown in tests
	 * hierarchy. Updates tests hierarchy viewer if the view is changed.
	 * 
	 * @param showTime true if time is shown and false otherwise
	 */
	public void setShowTime(boolean showTime) {
		if (this.showTime != showTime) {
			this.showTime = showTime;
			getTreeViewer().refresh();
		}
	}
	
	/**
	 * Sets whether only failed tests should be shown.
	 * 
	 * @param showFailedOnly new filter state
	 */
	public void setShowFailedOnly(boolean showFailedOnly) {
		// Create filter on first demand
		if (failedOnlyFilter == null) {
			failedOnlyFilter = new FailedOnlyFilter();
		}
		if (showFailedOnly) {
			getTreeViewer().addFilter(failedOnlyFilter);
		} else {
			getTreeViewer().removeFilter(failedOnlyFilter);
		}
	}

	/**
	 * Returns whether tests hierarchy should be shown in flat or hierarchical
	 * mode.
	 * 
	 * @return tests hierarchy view mode
	 */
	public boolean showTestsHierarchy() {
		return showTestsHierarchy;
	}

	/**
	 * Sets whether tests hierarchy should be shown in flat or hierarchical
	 * mode. Updates tests hierarchy viewer if the view is changed.
	 * 
	 * @param showTestsHierarchy true if tests hierarchy is shown in
	 * hierarchical mode and false otherwise
	 */
	public void setShowTestsHierarchy(boolean showTestsHierarchy) {
		if (this.showTestsHierarchy != showTestsHierarchy) {
			this.showTestsHierarchy = showTestsHierarchy;
			getTreeViewer().refresh();
		}
	}

}

Back to the top