001: /*******************************************************************************
002: * Copyright (c) 2004, 2007 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.rcp.util;
011:
012: import org.eclipse.jface.preference.IPreferenceStore;
013: import org.eclipse.swt.widgets.Display;
014: import org.eclipse.ui.IWorkbenchPreferenceConstants;
015: import org.eclipse.ui.PlatformUI;
016: import org.eclipse.ui.application.IWorkbenchConfigurer;
017: import org.eclipse.ui.application.WorkbenchAdvisor;
018:
019: /**
020: * This advisor adds the ability to exit the workbench after it has started up.
021: * This is done with a call to close from within the advisor's event idle loop.
022: * The number of times the idle is called before exiting can be configured. Test
023: * cases should subclass this advisor and add their own callback methods if
024: * needed.
025: *
026: * @since 3.1
027: */
028: public class RCPTestWorkbenchAdvisor extends WorkbenchAdvisor {
029:
030: public static Boolean asyncDuringStartup = null;
031:
032: private static boolean started = false;
033:
034: public static boolean isSTARTED() {
035: synchronized (RCPTestWorkbenchAdvisor.class) {
036: return started;
037: }
038: }
039:
040: /** Default value of -1 causes the option to be ignored. */
041: private int idleBeforeExit = -1;
042:
043: public RCPTestWorkbenchAdvisor() {
044: // default value means the advisor will not trigger the workbench to
045: // close
046: this .idleBeforeExit = -1;
047: }
048:
049: public RCPTestWorkbenchAdvisor(int idleBeforeExit) {
050: this .idleBeforeExit = idleBeforeExit;
051: }
052:
053: /*
054: * (non-Javadoc)
055: *
056: * @see org.eclipse.ui.application.WorkbenchAdvisor#initialize(org.eclipse.ui.application.IWorkbenchConfigurer)
057: */
058: public void initialize(IWorkbenchConfigurer configurer) {
059: super .initialize(configurer);
060:
061: // The RCP tests are currently run in the context of the Platform
062: // product, which specifies
063: // the Resource perspective as the default, and also reports progress on
064: // startup.
065: // We don't want either in effect when running the RCP tests.
066: // Also disable intro.
067: IPreferenceStore prefs = PlatformUI.getPreferenceStore();
068: prefs.setValue(
069: IWorkbenchPreferenceConstants.DEFAULT_PERSPECTIVE_ID,
070: "");
071: prefs.setValue(
072: IWorkbenchPreferenceConstants.SHOW_PROGRESS_ON_STARTUP,
073: false);
074: prefs.setValue(IWorkbenchPreferenceConstants.SHOW_INTRO, false);
075: }
076:
077: /*
078: * (non-Javadoc)
079: *
080: * @see org.eclipse.ui.application.WorkbenchAdvisor#getInitialWindowPerspectiveId()
081: */
082: public String getInitialWindowPerspectiveId() {
083: return EmptyPerspective.PERSP_ID;
084: }
085:
086: public void eventLoopIdle(final Display display) {
087: // Bug 107369: RCP test suite hangs on GTK
088: if (idleBeforeExit != -1 && --idleBeforeExit <= 0)
089: PlatformUI.getWorkbench().close();
090:
091: // bug 73184: On the mac the parent eventLoopIdle will put the display
092: // to sleep
093: // until there are events (e.g., mouse jiggled).
094: // if (!"carbon".equals(SWT.getPlatform()))
095: // super.eventLoopIdle(display);
096:
097: if (idleBeforeExit == -1)
098: return;
099: }
100:
101: /* (non-Javadoc)
102: * @see org.eclipse.ui.application.WorkbenchAdvisor#preStartup()
103: */
104: public void preStartup() {
105: super .preStartup();
106: Display display = Display.getCurrent();
107: if (display != null) {
108: display.asyncExec(new Runnable() {
109:
110: public void run() {
111: if (isSTARTED())
112: asyncDuringStartup = Boolean.FALSE;
113: else
114: asyncDuringStartup = Boolean.TRUE;
115: }
116: });
117: }
118: }
119:
120: /*
121: * (non-Javadoc)
122: *
123: * @see org.eclipse.ui.application.WorkbenchAdvisor#postStartup()
124: */
125: public void postStartup() {
126: super .postStartup();
127: synchronized (RCPTestWorkbenchAdvisor.class) {
128: started = true;
129: }
130: }
131: }
|