001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.testplugin.util;
011:
012: import junit.framework.Assert;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.graphics.Point;
016: import org.eclipse.swt.widgets.Button;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Control;
019: import org.eclipse.swt.widgets.Label;
020: import org.eclipse.swt.widgets.Shell;
021:
022: import org.eclipse.jface.dialogs.Dialog;
023: import org.eclipse.jface.dialogs.IDialogConstants;
024:
025: import org.eclipse.ui.PlatformUI;
026:
027: /**
028: * A <code>DialogCheck</code> is used test a dialog in
029: * various ways.
030: * <p>
031: * For interactive tests use <code>assertDialog</code>.
032: * For automated tests use <code>assert DialogTexts</code>.
033: * </p>
034: */
035: public class DialogCheck {
036: private DialogCheck() {
037: }
038:
039: private static VerifyDialog _verifyDialog;
040:
041: /**
042: * Asserts that a given dialog is not null and that it passes
043: * certain visual tests. These tests will be verified manually
044: * by the tester using an input dialog. Use this assert method
045: * to verify a dialog's sizing, initial focus, or accessiblity.
046: * To ensure that both the input dialog and the test dialog are
047: * accessible by the tester, the getShell() method should be used
048: * when creating the test dialog.
049: *
050: * Example usage:
051: * <code>Dialog dialog = new AboutDialog( DialogCheck.getShell() );
052: * DialogCheck.assertDialog(dialog, this);</code>
053: *
054: * @param dialog the test dialog to be verified.
055: */
056: public static void assertDialog(Dialog dialog) {
057: Assert.assertNotNull(dialog);
058: if (_verifyDialog.getShell() == null) {
059: //force the creation of the verify dialog
060: getShell();
061: }
062: if (_verifyDialog.open(dialog) == IDialogConstants.NO_ID) {
063: Assert.assertTrue(_verifyDialog.getFailureText(), false);
064: }
065: }
066:
067: /**
068: * Automated test that checks all the labels and buttons of a dialog
069: * to make sure there is enough room to display all the text. Any
070: * text that wraps is only approximated and is currently not accurate.
071: *
072: * @param dialog the test dialog to be verified.
073: */
074: public static void assertDialogTexts(Dialog dialog) {
075: Assert.assertNotNull(dialog);
076: dialog.setBlockOnOpen(false);
077: dialog.open();
078: Shell shell = dialog.getShell();
079: verifyCompositeText(shell);
080: dialog.close();
081: }
082:
083: /**
084: * This method should be called when creating dialogs to test. This
085: * ensures that the dialog's parent shell will be that of the
086: * verification dialog.
087: *
088: * @return Shell The shell of the verification dialog to be used as
089: * the parent shell of the test dialog.
090: */
091: public static Shell getShell() {
092: Shell shell = PlatformUI.getWorkbench()
093: .getActiveWorkbenchWindow().getShell();
094: _verifyDialog = new VerifyDialog(shell);
095: _verifyDialog.create();
096: return _verifyDialog.getShell();
097: }
098:
099: /*
100: * Looks at all the child widgets of a given composite and
101: * verifies the text on all labels and widgets.
102: * @param composite The composite to look through
103: */
104: private static void verifyCompositeText(Composite composite) {
105: Control children[] = composite.getChildren();
106: for (int i = 0; i < children.length; i++) {
107: try {
108: //verify the text if the child is a button
109: verifyButtonText((Button) children[i]);
110: } catch (ClassCastException exNotButton) {
111: try {
112: //child is not a button, maybe a label
113: verifyLabelText((Label) children[i]);
114: } catch (ClassCastException exNotLabel) {
115: try {
116: //child is not a label, make a recursive call if it is a composite
117: verifyCompositeText((Composite) children[i]);
118: } catch (ClassCastException exNotComposite) {
119: //the child is not a button, label, or composite - ignore it.
120: }
121: }
122: }
123: }
124: }
125:
126: /*
127: * Verifies that a given button is large enough to display its text.
128: * @param button The button to verify,
129: */
130: private static void verifyButtonText(Button button) {
131: String widget = button.toString();
132: Point size = button.getSize();
133:
134: //compute the size with no line wrapping
135: Point preferred = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
136: //if (size.y/preferred.y) == X, then label spans X lines, so divide
137: //the calculated value of preferred.x by X
138: if (preferred.y * size.y > 0) {
139: preferred.y /= countLines(button.getText()); //check for '\n\'
140: if (size.y / preferred.y > 1) {
141: preferred.x /= (size.y / preferred.y);
142: }
143: }
144:
145: String message = new StringBuffer("Warning: ").append(widget)
146: .append("\n\tActual Width -> ").append(size.x).append(
147: "\n\tRecommended Width -> ")
148: .append(preferred.x).toString();
149: if (preferred.x > size.x) {
150: //close the dialog
151: button.getShell().dispose();
152: Assert.assertTrue(message.toString(), false);
153: }
154: }
155:
156: /*
157: * Verifies that a given label is large enough to display its text.
158: * @param label The label to verify,
159: */
160: private static void verifyLabelText(Label label) {
161: String widget = label.toString();
162: Point size = label.getSize();
163:
164: //compute the size with no line wrapping
165: Point preferred = label.computeSize(SWT.DEFAULT, SWT.DEFAULT);
166: //if (size.y/preferred.y) == X, then label spans X lines, so divide
167: //the calculated value of preferred.x by X
168: if (preferred.y * size.y > 0) {
169: preferred.y /= countLines(label.getText());
170: if (size.y / preferred.y > 1) {
171: preferred.x /= (size.y / preferred.y);
172: }
173: }
174: String message = new StringBuffer("Warning: ").append(widget)
175: .append("\n\tActual Width -> ").append(size.x).append(
176: "\n\tRecommended Width -> ")
177: .append(preferred.x).toString();
178: if (preferred.x > size.x) {
179: //close the dialog
180: label.getShell().dispose();
181: Assert.assertTrue(message.toString(), false);
182: }
183: }
184:
185: /*
186: * Counts the number of lines in a given String.
187: * For example, if a string contains one (1) newline character,
188: * a value of two (2) would be returned.
189: * @param text The string to look through.
190: * @return int the number of lines in text.
191: */
192: private static int countLines(String text) {
193: int newLines = 1;
194: for (int i = 0; i < text.length(); i++) {
195: if (text.charAt(i) == '\n') {
196: newLines++;
197: }
198: }
199: return newLines;
200: }
201: }
|