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.internal.util;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.jface.dialogs.Dialog;
015: import org.eclipse.jface.dialogs.IDialogConstants;
016: import org.eclipse.jface.dialogs.TitleAreaDialog;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.events.SelectionAdapter;
019: import org.eclipse.swt.events.SelectionEvent;
020: import org.eclipse.swt.events.ShellAdapter;
021: import org.eclipse.swt.events.ShellEvent;
022: import org.eclipse.swt.graphics.Point;
023: import org.eclipse.swt.layout.GridData;
024: import org.eclipse.swt.layout.GridLayout;
025: import org.eclipse.swt.widgets.Button;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.swt.widgets.Control;
028: import org.eclipse.swt.widgets.Group;
029: import org.eclipse.swt.widgets.Label;
030: import org.eclipse.swt.widgets.Shell;
031:
032: /*
033: * This dialog is intended to verify a dialogs in a testing
034: * environment. The tester can test for sizing, initial focus,
035: * or accessibility.
036: */
037: public class VerifyDialog extends TitleAreaDialog {
038: private int SIZING_WIDTH = 400;
039:
040: private static int TEST_TYPE;
041:
042: public static final int TEST_SIZING = 0;
043:
044: public static final int TEST_FOCUS = 1;
045:
046: public static final int TEST_ACCESS = 2;
047:
048: private IDialogTestPass _dialogTests[] = new IDialogTestPass[3];
049:
050: private Dialog _testDialog; //the dialog to test
051:
052: private Point _testDialogSize;
053:
054: private Label _queryLabel;
055:
056: private Button _yesButton;
057:
058: private Button _checkList[];
059:
060: private String _failureText;
061:
062: /*
063: * Create an instance of the verification dialog.
064: */
065: public VerifyDialog(Shell parent) {
066: super (parent);
067: if (!(TEST_TYPE <= 2) && !(TEST_TYPE >= 0)) {
068: TEST_TYPE = TEST_SIZING;
069: }
070: _failureText = "";
071: _dialogTests[0] = new SizingTestPass();
072: _dialogTests[1] = new FocusTestPass();
073: _dialogTests[2] = new AccessibilityTestPass();
074: }
075:
076: /* (non-Javadoc)
077: * Method declared on Window.
078: */
079: protected void configureShell(Shell newShell) {
080: super .configureShell(newShell);
081: newShell.setText("Dialog Verification");
082: setShellStyle(SWT.NONE);
083: }
084:
085: /* (non-Javadoc)
086: * Method declared on Dialog.
087: */
088: protected void createButtonsForButtonBar(Composite parent) {
089: _yesButton = createButton(parent, IDialogConstants.YES_ID,
090: IDialogConstants.YES_LABEL, true);
091: createButton(parent, IDialogConstants.NO_ID,
092: IDialogConstants.NO_LABEL, false);
093: }
094:
095: /* (non-Javadoc)
096: * Method declared on Dialog.
097: */
098: protected void buttonPressed(int buttonId) {
099: if (IDialogConstants.YES_ID == buttonId) {
100: setReturnCode(IDialogConstants.YES_ID);
101: if (_testDialog.getShell() != null) {
102: _testDialog.close();
103: }
104: close();
105: } else if (IDialogConstants.NO_ID == buttonId) {
106: handleFailure();
107: }
108: }
109:
110: /* (non-Javadoc)
111: * Method declared on Dialog.
112: */
113: protected Control createDialogArea(Composite parent) {
114: // top level composite
115: Composite parentComposite = (Composite) super
116: .createDialogArea(parent);
117:
118: // create a composite with standard margins and spacing
119: Composite composite = new Composite(parentComposite, SWT.NONE);
120: composite.setSize(SIZING_WIDTH, SWT.DEFAULT);
121: GridLayout layout = new GridLayout();
122: layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
123: layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
124: layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
125: layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
126: composite.setLayout(layout);
127: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
128:
129: createTestSelectionGroup(composite);
130: createCheckListGroup(composite);
131:
132: _queryLabel = new Label(composite, SWT.NONE);
133: _queryLabel
134: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
135:
136: initializeTest();
137: return composite;
138: }
139:
140: /*
141: * Group for selecting type of test.
142: */
143: private void createTestSelectionGroup(Composite parent) {
144: Group group = new Group(parent, SWT.SHADOW_NONE);
145: group.setText("Testing:");
146: group.setLayout(new GridLayout());
147: GridData data = new GridData(GridData.FILL_HORIZONTAL);
148: group.setLayoutData(data);
149:
150: for (int i = 0; i < _dialogTests.length; i++) {
151: Button radio = new Button(group, SWT.RADIO);
152: radio.setText(_dialogTests[i].label());
153: final int testID = _dialogTests[i].getID();
154: radio.addSelectionListener(new SelectionAdapter() {
155: public void widgetSelected(SelectionEvent e) {
156: TEST_TYPE = testID;
157: initializeTest();
158: _yesButton.setEnabled(true);
159: }
160: });
161: if (TEST_TYPE == _dialogTests[i].getID()) {
162: radio.setSelection(true);
163: }
164: }
165: }
166:
167: /*
168: * Initializes the checklist with empty checks.
169: */
170: private void createCheckListGroup(Composite parent) {
171: Group group = new Group(parent, SWT.SHADOW_NONE);
172: group.setText("Verify that:");
173: group.setLayout(new GridLayout());
174: GridData data = new GridData(GridData.FILL_HORIZONTAL);
175: group.setLayoutData(data);
176:
177: int checkListSize = 0;
178: for (int i = 0; i < _dialogTests.length; i++) {
179: int size = _dialogTests[i].checkListTexts().size();
180: if (size > checkListSize) {
181: checkListSize = size;
182: }
183: }
184: _checkList = new Button[checkListSize];
185: SelectionAdapter selectionAdapter = new SelectionAdapter() {
186: public void widgetSelected(SelectionEvent e) {
187: checkYesEnable();
188: }
189: };
190: for (int i = 0; i < checkListSize; i++) {
191: _checkList[i] = new Button(group, SWT.CHECK);
192: _checkList[i].addSelectionListener(selectionAdapter);
193: data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
194: data.grabExcessHorizontalSpace = true;
195: _checkList[i].setLayoutData(data);
196: }
197: }
198:
199: /*
200: * Disables the yes button if any of the items in the checklist
201: * are unchecked. Enables the yes button otherwise.
202: */
203: private void checkYesEnable() {
204: boolean enable = true;
205: for (int i = 0; i < _checkList.length; i++) {
206: if (!_checkList[i].getSelection()) {
207: enable = false;
208: }
209: }
210: _yesButton.setEnabled(enable);
211: }
212:
213: /*
214: * Initializes the checklist, banner texts, and query label
215: */
216: private void initializeTest() {
217: IDialogTestPass test = _dialogTests[TEST_TYPE];
218: setTitle(test.title());
219: setMessage(test.description());
220: Iterator iterator = test.checkListTexts().iterator();
221: for (int i = 0; i < _checkList.length; i++) {
222: if (iterator.hasNext()) {
223: _checkList[i].setText(iterator.next().toString());
224: _checkList[i].setVisible(true);
225: _checkList[i].update();
226: } else {
227: _checkList[i].setVisible(false);
228: _checkList[i].update();
229: }
230: _checkList[i].setSelection(true);
231: }
232: _queryLabel.setText(test.queryText());
233: }
234:
235: public String getFailureText() {
236: return _failureText;
237: }
238:
239: /*
240: * Can't open the verification dialog without a specified
241: * test dialog, this simply returns a failure and prevents
242: * opening. Should use open(Dialog) instead.
243: *
244: */
245: public int open() {
246: _failureText = "Testing dialog is required, use VerifyDialog::open(Dialog)";
247: return IDialogConstants.NO_ID;
248: }
249:
250: /*
251: * Opens the verification dialog to test the specified dialog.
252: */
253: public int open(Dialog testDialog) {
254: if (getShell() == null) {
255: create();
256: }
257: getShell().setLocation(0, 0);
258: getShell().setSize(
259: Math.max(SIZING_WIDTH, getShell().getSize().x),
260: getShell().getSize().y);
261: _testDialog = testDialog;
262: if (_testDialog.getShell() == null) {
263: _testDialog.create();
264: }
265: _testDialogSize = _testDialog.getShell().getSize();
266: openNewTestDialog();
267:
268: return super .open();
269: }
270:
271: /*
272: * Opens the dialog to be verified.
273: */
274: private void openNewTestDialog() {
275: if (_testDialog.getShell() == null) {
276: _testDialog.create();
277: }
278: _testDialog.setBlockOnOpen(false);
279: _testDialog.getShell().setLocation(getShell().getSize().x + 1,
280: 0);
281: _testDialog.getShell().setSize(_testDialogSize);
282: _testDialog.getShell().addShellListener(new ShellAdapter() {
283: public void shellClosed(ShellEvent e) {
284: e.doit = false;
285: }
286:
287: });
288: _testDialog.open();
289: }
290:
291: /*
292: * The test dialog failed, open the failure dialog.
293: */
294: private void handleFailure() {
295: IDialogTestPass test = _dialogTests[TEST_TYPE];
296: StringBuffer text = new StringBuffer();
297: String label = test.label();
298: label = label.substring(0, label.indexOf("&"))
299: + label.substring(label.indexOf("&") + 1);
300: text.append(label).append(" failed on the ").append(
301: SWT.getPlatform()).append(" platform:\n");
302:
303: String failureMessages[] = test.failureTexts();
304: for (int i = 0; i < test.checkListTexts().size(); i++) {
305: if (!_checkList[i].getSelection()) {
306: text.append("- ").append(failureMessages[i]).append(
307: "\n");
308: }
309: }
310: FailureDialog dialog = new FailureDialog(getShell());
311: dialog.create();
312: dialog.setText(text.toString());
313: if (dialog.open() == IDialogConstants.OK_ID) {
314: _failureText = dialog.toString();
315: setReturnCode(IDialogConstants.NO_ID);
316: if (_testDialog.getShell() != null) {
317: _testDialog.close();
318: }
319: close();
320: }
321: }
322:
323: /*
324: * In case the shell was closed by a means other than
325: * the NO button.
326: */
327: protected void handleShellCloseEvent() {
328: handleFailure();
329: }
330: }
|