001: /*******************************************************************************
002: * Copyright (c) 2005, 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.internal;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.ui.IEditorInput;
014: import org.eclipse.ui.IEditorPart;
015: import org.eclipse.ui.IPersistableElement;
016: import org.eclipse.ui.IPropertyListener;
017: import org.eclipse.ui.IViewPart;
018: import org.eclipse.ui.IWorkbenchPart;
019: import org.eclipse.ui.IWorkbenchPart2;
020:
021: public class PartTester {
022: private PartTester() {
023: }
024:
025: /**
026: * Sanity-check the public interface of the editor. This is called on every editor after it
027: * is fully initiallized, but before it is actually connected to the editor reference or the
028: * layout. Calls as much of the editor's public interface as possible to test for exceptions,
029: * and tests the return values for glaring faults. This does not need to be an exhaustive conformance
030: * test, as it is called every time an editor is opened and it needs to be efficient.
031: * The part should be unmodified when the method exits.
032: *
033: * @param part
034: */
035: public static void testEditor(IEditorPart part) throws Exception {
036: testWorkbenchPart(part);
037:
038: Assert
039: .isTrue(part.getEditorSite() == part.getSite(),
040: "The part's editor site must be the same as the part's site"); //$NON-NLS-1$
041: IEditorInput input = part.getEditorInput();
042: Assert.isNotNull(input, "The editor input must be non-null"); //$NON-NLS-1$
043: testEditorInput(input);
044:
045: part.isDirty();
046: part.isSaveAsAllowed();
047: part.isSaveOnCloseNeeded();
048: }
049:
050: public static void testEditorInput(IEditorInput input)
051: throws Exception {
052: input.getAdapter(Object.class);
053:
054: // Don't test input.getImageDescriptor() -- the workbench never uses that
055: // method and most editor inputs would fail the test. It should really be
056: // deprecated.
057:
058: Assert.isNotNull(input.getName(),
059: "The editor input must have a non-null name"); //$NON-NLS-1$
060: Assert.isNotNull(input.getToolTipText(),
061: "The editor input must have a non-null tool tip"); //$NON-NLS-1$
062:
063: // Persistable element may be null
064: IPersistableElement persistableElement = input.getPersistable();
065: if (persistableElement != null) {
066: Assert
067: .isNotNull(persistableElement.getFactoryId(),
068: "The persistable element for the editor input must have a non-null factory id"); //$NON-NLS-1$
069: }
070: }
071:
072: /**
073: * Sanity-checks a workbench part. Excercises the public interface and tests for any
074: * obviously bogus return values. The part should be unmodified when the method exits.
075: *
076: * @param part
077: * @throws Exception
078: */
079: private static void testWorkbenchPart(IWorkbenchPart part)
080: throws Exception {
081: IPropertyListener testListener = new IPropertyListener() {
082: public void propertyChanged(Object source, int propId) {
083:
084: }
085: };
086:
087: // Test addPropertyListener
088: part.addPropertyListener(testListener);
089:
090: // Test removePropertyListener
091: part.removePropertyListener(testListener);
092:
093: // Test equals
094: Assert.isTrue(part.equals(part),
095: "A part must be equal to itself"); //$NON-NLS-1$
096: Assert.isTrue(!part.equals(new Integer(32)),
097: "A part must have a meaningful equals method"); //$NON-NLS-1$
098:
099: // Test getAdapter
100: Object partAdapter = part.getAdapter(part.getClass());
101: Assert.isTrue(partAdapter == null || partAdapter == part,
102: "A part must adapter to itself or return null"); //$NON-NLS-1$
103:
104: // Test getTitle
105: Assert.isNotNull(part.getTitle(),
106: "A part's title must be non-null"); //$NON-NLS-1$
107:
108: // Test getTitleImage
109: Assert.isNotNull(part.getTitleImage(),
110: "A part's title image must be non-null"); //$NON-NLS-1$
111:
112: // Test getTitleToolTip
113: Assert.isNotNull(part.getTitleToolTip(),
114: "A part's title tool tip must be non-null"); //$NON-NLS-1$
115:
116: // Test toString
117: Assert
118: .isNotNull(part.toString(),
119: "A part's toString method must return a non-null value"); //$NON-NLS-1$
120:
121: // Compute hashCode
122: part.hashCode();
123:
124: if (part instanceof IWorkbenchPart2) {
125: testWorkbenchPart2((IWorkbenchPart2) part);
126: }
127: }
128:
129: private static void testWorkbenchPart2(IWorkbenchPart2 part)
130: throws Exception {
131: Assert.isNotNull(part.getContentDescription(),
132: "A part must return a non-null content description"); //$NON-NLS-1$
133: Assert.isNotNull(part.getPartName(),
134: "A part must return a non-null part name"); //$NON-NLS-1$
135: }
136:
137: /**
138: * Sanity-check the public interface of a view. This is called on every view after it
139: * is fully initiallized, but before it is actually connected to the part reference or the
140: * layout. Calls as much of the part's public interface as possible without modifying the part
141: * to test for exceptions and check the return values for glaring faults. This does not need
142: * to be an exhaustive conformance test, as it is called every time an editor is opened and
143: * it needs to be efficient.
144: *
145: * @param part
146: */
147: public static void testView(IViewPart part) throws Exception {
148: Assert.isTrue(part.getSite() == part.getViewSite(),
149: "A part's site must be the same as a part's view site"); //$NON-NLS-1$
150: testWorkbenchPart(part);
151: }
152: }
|