001: /*******************************************************************************
002: * Copyright (c) 2004, 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.rcp.util;
011:
012: import java.util.Iterator;
013: import java.util.LinkedList;
014: import java.util.List;
015:
016: import junit.framework.Assert;
017:
018: import org.eclipse.ui.IWorkbenchWindow;
019: import org.eclipse.ui.WorkbenchException;
020: import org.eclipse.ui.application.IActionBarConfigurer;
021: import org.eclipse.ui.application.IWorkbenchConfigurer;
022: import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
023:
024: /**
025: * This utility class is used to record the order in which the hooks are called.
026: * It should be subclassed to provide the behaviour needed for a particular test
027: * case. After the case has run, its list can be examined to check for the
028: * expected order.
029: * <p>
030: * NOTE: This differs from <code>org.eclipse.ui.tests.CallHistory</code> since
031: * this class allows assertions on individual method names. Which means that the
032: * junit error will identify the exact error, not just the existence of an
033: * error.
034: */
035: public class WorkbenchAdvisorObserver extends RCPTestWorkbenchAdvisor {
036:
037: private List operations = new LinkedList();
038:
039: private Iterator iterator;
040:
041: public final static String INITIALIZE = "initialize"; //$NON-NLS-1$
042:
043: public final static String PRE_STARTUP = "preStartup"; //$NON-NLS-1$
044:
045: public final static String POST_STARTUP = "postStartup"; //$NON-NLS-1$
046:
047: public final static String PRE_WINDOW_OPEN = "preWindowOpen"; //$NON-NLS-1$
048:
049: public final static String FILL_ACTION_BARS = "fillActionBars"; //$NON-NLS-1$
050:
051: public final static String POST_WINDOW_RESTORE = "postWindowRestore"; //$NON-NLS-1$
052:
053: public final static String POST_WINDOW_OPEN = "postWindowOpen"; //$NON-NLS-1$
054:
055: public final static String PRE_WINDOW_SHELL_CLOSE = "preWindowShellClose"; //$NON-NLS-1$
056:
057: public final static String EVENT_LOOP_EXCEPTION = "eventLoopException"; //$NON-NLS-1$
058:
059: public final static String PRE_SHUTDOWN = "preShutdown"; //$NON-NLS-1$
060:
061: public final static String POST_SHUTDOWN = "postShutdown"; //$NON-NLS-1$
062:
063: public IWorkbenchConfigurer workbenchConfig;
064:
065: public WorkbenchAdvisorObserver() {
066: super ();
067: }
068:
069: public WorkbenchAdvisorObserver(int idleBeforeExit) {
070: super (idleBeforeExit);
071: }
072:
073: public void resetOperationIterator() {
074: iterator = operations.iterator();
075: }
076:
077: public void assertNextOperation(String expected) {
078: Assert.assertTrue(iterator.hasNext());
079: Assert.assertEquals(expected, (String) iterator.next());
080: }
081:
082: public void assertAllOperationsExamined() {
083: Assert.assertNotNull(iterator);
084: Assert.assertFalse(iterator.hasNext());
085: }
086:
087: private void addOperation(String operation) {
088: operations.add(operation);
089: }
090:
091: public void initialize(IWorkbenchConfigurer configurer) {
092: super .initialize(configurer);
093: workbenchConfig = configurer;
094: addOperation(INITIALIZE);
095: }
096:
097: public void preStartup() {
098: super .preStartup();
099: addOperation(PRE_STARTUP);
100: }
101:
102: public void preWindowOpen(IWorkbenchWindowConfigurer configurer) {
103: super .preWindowOpen(configurer);
104: addOperation(PRE_WINDOW_OPEN);
105: }
106:
107: public void fillActionBars(IWorkbenchWindow window,
108: IActionBarConfigurer configurer, int flags) {
109: super .fillActionBars(window, configurer, flags);
110: addOperation(FILL_ACTION_BARS);
111: }
112:
113: public void postWindowRestore(IWorkbenchWindowConfigurer configurer)
114: throws WorkbenchException {
115: super .postWindowRestore(configurer);
116: addOperation(POST_WINDOW_RESTORE);
117: }
118:
119: public void postWindowOpen(IWorkbenchWindowConfigurer configurer) {
120: super .postWindowOpen(configurer);
121: addOperation(POST_WINDOW_OPEN);
122: }
123:
124: public void postStartup() {
125: super .postStartup();
126: addOperation(POST_STARTUP);
127: }
128:
129: public boolean preWindowShellClose(
130: IWorkbenchWindowConfigurer configurer) {
131: if (!super .preWindowShellClose(configurer))
132: return false;
133: addOperation(PRE_WINDOW_SHELL_CLOSE);
134: return true;
135: }
136:
137: public boolean preShutdown() {
138: boolean result = super .preShutdown();
139: addOperation(PRE_SHUTDOWN);
140: return result;
141: }
142:
143: public void postShutdown() {
144: super .postShutdown();
145: addOperation(POST_SHUTDOWN);
146: }
147:
148: public void eventLoopException(Throwable exception) {
149: super.eventLoopException(exception);
150: addOperation(EVENT_LOOP_EXCEPTION);
151: }
152: }
|