001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.ui.tests.harness.util;
011:
012: import junit.framework.Assert;
013:
014: import org.eclipse.jface.dialogs.Dialog;
015: import org.eclipse.jface.dialogs.IDialogConstants;
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.graphics.Point;
018: import org.eclipse.swt.widgets.Button;
019: import org.eclipse.swt.widgets.Composite;
020: import org.eclipse.swt.widgets.Control;
021: import org.eclipse.swt.widgets.Label;
022: import org.eclipse.swt.widgets.Shell;
023: import org.eclipse.swt.widgets.TabFolder;
024: import org.eclipse.ui.internal.WorkbenchPlugin;
025: import org.eclipse.ui.tests.internal.util.VerifyDialog;
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: * @param assert this is the test case object, assertions will be
056: * executed on this object.
057: */
058: public static void assertDialog(Dialog dialog, Assert assertion) {
059: Assert.assertNotNull(dialog);
060: if (_verifyDialog.getShell() == null) {
061: //force the creation of the verify dialog
062: getShell();
063: }
064: if (_verifyDialog.open(dialog) == IDialogConstants.NO_ID) {
065: Assert.assertTrue(_verifyDialog.getFailureText(), false);
066: }
067: }
068:
069: /**
070: * Automated test that checks all the labels and buttons of a dialog
071: * to make sure there is enough room to display all the text. Any
072: * text that wraps is only approximated and is currently not accurate.
073: *
074: * @param dialog the test dialog to be verified.
075: * @param assert this is the test case object, assertions will be
076: * executed on this object.
077: */
078: public static void assertDialogTexts(Dialog dialog, Assert assertion) {
079: Assert.assertNotNull(dialog);
080: dialog.setBlockOnOpen(false);
081: dialog.open();
082: Shell shell = dialog.getShell();
083: verifyCompositeText(shell, assertion);
084: dialog.close();
085: }
086:
087: /**
088: * This method should be called when creating dialogs to test. This
089: * ensures that the dialog's parent shell will be that of the
090: * verification dialog.
091: *
092: * @return Shell The shell of the verification dialog to be used as
093: * the parent shell of the test dialog.
094: */
095: public static Shell getShell() {
096: Shell shell = WorkbenchPlugin.getDefault().getWorkbench()
097: .getActiveWorkbenchWindow().getShell();
098: _verifyDialog = new VerifyDialog(shell);
099: _verifyDialog.create();
100: return _verifyDialog.getShell();
101: }
102:
103: /*
104: * Looks at all the child widgets of a given composite and
105: * verifies the text on all labels and widgets.
106: * @param composite The composite to look through
107: * @param assert The object to invoke assertions on.
108: */
109: private static void verifyCompositeText(Composite composite,
110: Assert assertion) {
111: Control children[] = composite.getChildren();
112: for (int i = 0; i < children.length; i++) {
113: Control child = children[i];
114: if (child instanceof TabFolder) {
115: TabFolder folder = (TabFolder) child;
116: int numPages = folder.getItemCount();
117: for (int j = 0; j < numPages; j++) {
118: folder.setSelection(j);
119: }
120: } else if (child instanceof Button) {
121: //verify the text if the child is a button
122: verifyButtonText((Button) child, assertion);
123: } else if (child instanceof Label) {
124: //child is not a button, maybe a label
125: verifyLabelText((Label) child, assertion);
126: } else if (child instanceof Composite) {
127: //child is not a label, make a recursive call if it is a composite
128: verifyCompositeText((Composite) child, assertion);
129: }
130: }
131: }
132:
133: /*
134: * Verifies that a given button is large enough to display its text.
135: * @param button The button to verify,
136: * @param assert The object to invoke assertions on.
137: */
138: private static void verifyButtonText(Button button, Assert assertion) {
139: String widget = button.toString();
140: Point size = button.getSize();
141:
142: //compute the size with no line wrapping
143: Point preferred = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
144: //if (size.y/preferred.y) == X, then label spans X lines, so divide
145: //the calculated value of preferred.x by X
146: if (preferred.y * size.y > 0) {
147: preferred.y /= countLines(button.getText()); //check for '\n\'
148: if (size.y / preferred.y > 1) {
149: preferred.x /= (size.y / preferred.y);
150: }
151: }
152:
153: String message = new StringBuffer("Warning: ").append(widget)
154: .append("\n\tActual Width -> ").append(size.x).append(
155: "\n\tRecommended Width -> ")
156: .append(preferred.x).toString();
157: if (preferred.x > size.x) {
158: //close the dialog
159: button.getShell().dispose();
160: Assert.assertTrue(message.toString(), false);
161: }
162: }
163:
164: /*
165: * Verifies that a given label is large enough to display its text.
166: * @param label The label to verify,
167: * @param assert The object to invoke assertions on.
168: */
169: private static void verifyLabelText(Label label, Assert assertion) {
170: String widget = label.toString();
171: Point size = label.getSize();
172:
173: //compute the size with no line wrapping
174: Point preferred = label.computeSize(SWT.DEFAULT, SWT.DEFAULT);
175: //if (size.y/preferred.y) == X, then label spans X lines, so divide
176: //the calculated value of preferred.x by X
177: if (preferred.y * size.y > 0) {
178: preferred.y /= countLines(label.getText());
179: if (size.y / preferred.y > 1) {
180: preferred.x /= (size.y / preferred.y);
181: }
182: }
183: String message = new StringBuffer("Warning: ").append(widget)
184: .append("\n\tActual Width -> ").append(size.x).append(
185: "\n\tRecommended Width -> ")
186: .append(preferred.x).toString();
187: if (preferred.x > size.x) {
188: //close the dialog
189: label.getShell().dispose();
190: Assert.assertTrue(message.toString(), false);
191: }
192: }
193:
194: /*
195: * Counts the number of lines in a given String.
196: * For example, if a string contains one (1) newline character,
197: * a value of two (2) would be returned.
198: * @param text The string to look through.
199: * @return int the number of lines in text.
200: */
201: private static int countLines(String text) {
202: int newLines = 1;
203: for (int i = 0; i < text.length(); i++) {
204: if (text.charAt(i) == '\n') {
205: newLines++;
206: }
207: }
208: return newLines;
209: }
210: }
|