Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 287edb911dfbcca8945608e7230c7fc1f47649a4 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 Anton Gorenkov 
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Anton Gorenkov - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.testsrunner.internal.ui.view;


import org.eclipse.cdt.testsrunner.internal.TestsRunnerPlugin;
import org.eclipse.cdt.testsrunner.model.ITestItem;
import org.eclipse.cdt.testsrunner.model.ITestingSession;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import java.text.MessageFormat;


/**
 * Shows a simple tests count statics information (run/error/failed).
 */
public class CounterPanel extends Composite {

	/** Testing session to show statistics for. */
	private ITestingSession testingSession;
	
	/** Widget showing the failed tests count. */
	private Label failedCounterLabel;

	/** Widget showing the error tests count. */
	private Label abortedCounterLabel;

	/** Widget showing the run tests count. */
	private Label currentCounterLabel;

	/**
	 * Shows whether there were skipped tests. It is used to force layout of the
	 * counter widgets after skipped tests are appeared.
	 */
	private boolean hasSkipped;

	private final Image errorIcon = TestsRunnerPlugin.createAutoImage("ovr16/failed_counter.gif"); //$NON-NLS-1$
	private final Image failureIcon = TestsRunnerPlugin.createAutoImage("ovr16/aborted_counter.gif"); //$NON-NLS-1$

	
	public CounterPanel(Composite parent, ITestingSession testingSession) {
		super(parent, SWT.WRAP);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 9;
		gridLayout.makeColumnsEqualWidth = false;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);

		currentCounterLabel = createLabel(UIViewMessages.CounterPanel_tests_run, null);
		abortedCounterLabel = createLabel(UIViewMessages.CounterPanel_tests_erred, errorIcon);
		failedCounterLabel = createLabel(UIViewMessages.CounterPanel_tests_failed, failureIcon);
		setTestingSession(testingSession);
	}

	/**
	 * Creates counter label widget.
	 * 
	 * @param name widget text prefix
	 * @param image widget image or <code>null</code>
	 * @return created label
	 */
	private Label createLabel(String name, Image image) {
		Label label = new Label(this, SWT.NONE);
		if (image != null) {
			image.setBackground(label.getBackground());
			label.setImage(image);
		}
		label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));

		label = new Label(this, SWT.NONE);
		label.setText(name);
		label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));

		Label value = new Label(this, SWT.READ_ONLY);
		value.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING));
		return value;
	}

	/**
	 * Sets the testing session to show information about.
	 * 
	 * @param testingSession testing session (null is not acceptable)
	 */
	public void setTestingSession(ITestingSession testingSession) {
		this.testingSession = testingSession;
		this.hasSkipped = testingSession.getCount(ITestItem.Status.Skipped) != 0;
		updateInfoFromSession();
	}
	
	/**
	 * Updates the information on the panel from the currently set testing
	 * session.
	 */
	public void updateInfoFromSession() {
		setFailedCounter(testingSession.getCount(ITestItem.Status.Failed));
		setAbortedCounter(testingSession.getCount(ITestItem.Status.Aborted));
		setCurrentCounter(testingSession.getCurrentCounter(), testingSession.getCount(ITestItem.Status.Skipped));
		redraw();
	}
	
	/**
	 * Sets a new value for the failed tests counter.
	 * 
	 * @param newValue new counter value
	 */
	private void setFailedCounter(int newValue) {
		failedCounterLabel.setText(Integer.toString(newValue));
	}

	/**
	 * Sets a new value for the error tests counter.
	 * 
	 * @param newValue new counter value
	 */
	private void setAbortedCounter(int newValue) {
		abortedCounterLabel.setText(Integer.toString(newValue));
	}

	/**
	 * Sets a new value for the run tests counter.
	 * 
	 * @param currentValue new counter value
	 * @param skippedValue skipped tests counter
	 */
	private void setCurrentCounter(int currentValue, int skippedValue) {
		if (!hasSkipped && skippedValue != 0) {
			layout();
		}
		String runString = (skippedValue == 0)
				? Integer.toString(currentValue)
				: MessageFormat.format(UIViewMessages.CounterPanel_tests_skipped, currentValue, skippedValue);
		currentCounterLabel.setText(runString);
	}
	
}

Back to the top