Skip to main content
summaryrefslogtreecommitdiffstats
blob: b961172daacb3a3a902f91a53f5ab7767e30005b (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
package org.eclipse.ui.tests.util;


/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */


import junit.framework.Assert;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.tests.internal.util.VerifyDialog;


/**
 * A <code>DialogCheck</code> is used test a dialog in
 * various ways. 
 * <p>
 * For interactive tests use <code>assertDialog</code>.
 * For automated tests use <code>assert DialogTexts</code>.
 * </p> 
 */
public class DialogCheck {
	private DialogCheck() {
	}
	private static VerifyDialog _verifyDialog;


	/**
	 * Asserts that a given dialog is not null and that it passes
	 * certain visual tests.  These tests will be verified manually
	 * by the tester using an input dialog.  Use this assert method
	 * to verify a dialog's sizing, initial focus, or accessiblity.
	 * To ensure that both the input dialog and the test dialog are
	 * accessible by the tester, the getShell() method should be used
	 * when creating the test dialog.
	 * 
	 * Example usage:
	 * <code>Dialog dialog = new AboutDialog( DialogCheck.getShell() );
	 * DialogCheck.assertDialog(dialog, this);</code>
	 * 
	 * @param dialog the test dialog to be verified.
	 * @param assert this is the test case object, assertions will be
	 * executed on this object.
	 */
	public static void assertDialog(Dialog dialog, Assert assert) {
		assert.assertNotNull(dialog);
		if (_verifyDialog.getShell() == null) {
			//force the creation of the verify dialog
			getShell();
		}
		if (_verifyDialog.open(dialog) == IDialogConstants.NO_ID) {
			assert.assertTrue(_verifyDialog.getFailureText(), false);
		}
	}


	/**
	 * Automated test that checks all the labels and buttons of a dialog
	 * to make sure there is enough room to display all the text.  Any
	 * text that wraps is only approximated and is currently not accurate.
	 * 
	 * @param dialog the test dialog to be verified.
	 * @param assert this is the test case object, assertions will be
	 * executed on this object.
	 */
	public static void assertDialogTexts(Dialog dialog, Assert assert) {
		assert.assertNotNull(dialog);
		dialog.setBlockOnOpen(false);
		dialog.open();
		Shell shell = dialog.getShell();
		verifyCompositeText(shell, assert);
		dialog.close();
	}


	/**
	 * This method should be called when creating dialogs to test.  This
	 * ensures that the dialog's parent shell will be that of the
	 * verification dialog.
	 * 
	 * @return Shell The shell of the verification dialog to be used as
	 * the parent shell of the test dialog.
	 */
	public static Shell getShell() {
		Shell shell =
			WorkbenchPlugin
				.getDefault()
				.getWorkbench()
				.getActiveWorkbenchWindow()
				.getShell();
		_verifyDialog = new VerifyDialog(shell);
		_verifyDialog.create();
		return _verifyDialog.getShell();
	}


	/*
	 * Looks at all the child widgets of a given composite and
	 * verifies the text on all labels and widgets.
	 * @param composite The composite to look through
	 * @param assert The object to invoke assertions on.
	 */
	private static void verifyCompositeText(Composite composite, Assert assert) {
		Control children[] = composite.getChildren();
		for (int i = 0; i < children.length; i++) {
			try {
				//verify the text if the child is a button
				verifyButtonText((Button) children[i], assert);
			} catch (ClassCastException exNotButton) {
				try {
					//child is not a button, maybe a label
					verifyLabelText((Label) children[i], assert);
				} catch (ClassCastException exNotLabel) {
					try {
						//child is not a label, make a recursive call if it is a composite
						verifyCompositeText((Composite) children[i], assert);
					} catch (ClassCastException exNotComposite) {
						//the child is not a button, label, or composite - ignore it.
					}
				}
			}
		}
	}
	
	/*
	 * Verifies that a given button is large enough to display its text.
	 * @param button The button to verify,
	 * @param assert The object to invoke assertions on.
	 */
	private static void verifyButtonText(Button button, Assert assert) {
		String widget = button.toString();
		Point size = button.getSize();


		//compute the size with no line wrapping
		Point preferred = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		//if (size.y/preferred.y) == X, then label spans X lines, so divide
		//the calculated value of preferred.x by X
		if (preferred.y * size.y > 0) {
			preferred.y /= countLines(button.getText()); //check for '\n\'
			if (size.y / preferred.y > 1) {
				preferred.x /= (size.y / preferred.y);
			}
		}


		String message =
			new StringBuffer("Warning: ")
				.append(widget)
				.append("\n\tActual Width -> ")
				.append(size.x)
				.append("\n\tRecommended Width -> ")
				.append(preferred.x)
				.toString();
		if (preferred.x > size.x) {
			//close the dialog
			button.getShell().dispose();
			assert.assertTrue(message.toString(), false);
		}
	}
	
	/*
	 * Verifies that a given label is large enough to display its text.
	 * @param label The label to verify,
	 * @param assert The object to invoke assertions on.
	 */
	private static void verifyLabelText(Label label, Assert assert) {
		String widget = label.toString();
		Point size = label.getSize();


		//compute the size with no line wrapping
		Point preferred = label.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		//if (size.y/preferred.y) == X, then label spans X lines, so divide
		//the calculated value of preferred.x by X
		if (preferred.y * size.y > 0) {
			preferred.y /= countLines(label.getText());
			if (size.y / preferred.y > 1) {
				preferred.x /= (size.y / preferred.y);
			}
		}
		String message =
			new StringBuffer("Warning: ")
				.append(widget)
				.append("\n\tActual Width -> ")
				.append(size.x)
				.append("\n\tRecommended Width -> ")
				.append(preferred.x)
				.toString();
		if (preferred.x > size.x) {
			//close the dialog
			label.getShell().dispose();
			assert.assertTrue(message.toString(), false);
		}
	}
	
	/*
	 * Counts the number of lines in a given String.
	 * For example, if a string contains one (1) newline character,
	 * a value of two (2) would be returned.
	 * @param text The string to look through.
	 * @return int the number of lines in text.
	 */
	private static int countLines(String text) {
		int newLines = 1;
		for (int i = 0; i < text.length(); i++) {
			if (text.charAt(i) == '\n') {
				newLines++;
			}
		}
		return newLines;
	}
}

Back to the top