Coverage Report - org.eclipse.swtbot.eclipse.finder.DefaultWorkbench
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultWorkbench
80%
20/25
60%
6/10
1.308
DefaultWorkbench$1
100%
4/4
N/A
1.308
DefaultWorkbench$2
100%
3/3
N/A
1.308
 
 1  6
 /*******************************************************************************
 2  
  * Copyright (c) 2009 - 2010 David Green and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  * 
 8  
  * Contributors:
 9  
  *     David Green - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.eclipse.finder;
 12  
 
 13  
 import java.util.List;
 14  
 
 15  
 import org.eclipse.swt.widgets.Widget;
 16  
 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
 17  
 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
 18  
 import org.eclipse.swtbot.swt.finder.results.Result;
 19  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 20  
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
 21  
 import org.eclipse.ui.IPerspectiveDescriptor;
 22  
 import org.eclipse.ui.IWorkbenchPage;
 23  
 import org.eclipse.ui.IWorkbenchWindow;
 24  
 import org.eclipse.ui.PlatformUI;
 25  
 
 26  
 /**
 27  
  * A default implementation of the workbench. May be subclassed to provide alternate implementations for different
 28  
  * versions of Eclipse.
 29  
  * <p>
 30  
  * Do not access directly. Instead use <code>Eclipse.workbench()</code>
 31  
  * </p>
 32  
  * 
 33  
  * @author David Green
 34  
  * @see Eclipse
 35  
  */
 36  
 class DefaultWorkbench {
 37  
 
 38  
         /** The bot that may be used to drive the workbench. */
 39  
         private final SWTWorkbenchBot        bot;
 40  
 
 41  
         /**
 42  
          * Creates an instance of the default workbench.
 43  
          * 
 44  
          * @param bot the bot that can drive the workbench.
 45  
          */
 46  5
         DefaultWorkbench(SWTWorkbenchBot bot) {
 47  5
                 this.bot = bot;
 48  5
         }
 49  
 
 50  
         DefaultWorkbench resetActivePerspective() {
 51  2
                 UIThreadRunnable.syncExec(new VoidResult() {
 52  
                         public void run() {
 53  2
                                 activePage().resetPerspective();
 54  2
                         }
 55  
                 });
 56  2
                 return this;
 57  
         }
 58  
 
 59  
         DefaultWorkbench resetWorkbench() {
 60  0
                 return closeAllShells().saveAllEditors().closeAllEditors();
 61  
         }
 62  
 
 63  
         DefaultWorkbench closeAllShells() {
 64  1
                 SWTBotShell[] shells = bot.shells();
 65  2
                 for (SWTBotShell shell : shells) {
 66  1
                         if (!isEclipseShell(shell)) {
 67  0
                                 shell.close();
 68  
                         }
 69  
                 }
 70  1
                 return this;
 71  
         }
 72  
 
 73  
         DefaultWorkbench saveAllEditors() {
 74  1
                 List<? extends SWTBotEditor> editors = bot.editors();
 75  2
                 for (SWTBotEditor editor : editors) {
 76  0
                         editor.save();
 77  
                 }
 78  1
                 return this;
 79  
         }
 80  
 
 81  
         DefaultWorkbench closeAllEditors() {
 82  1
                 List<? extends SWTBotEditor> editors = bot.editors();
 83  2
                 for (SWTBotEditor editor : editors) {
 84  0
                         editor.close();
 85  
                 }
 86  1
                 return this;
 87  
         }
 88  
 
 89  
         private boolean isEclipseShell(final SWTBotShell shell) {
 90  1
                 return getActiveWorkbenchWindowShell() == shell.widget;
 91  
         }
 92  
 
 93  
         private IWorkbenchWindow getActiveWorkbenchWindow() {
 94  3
                 return UIThreadRunnable.syncExec(bot.getDisplay(), new Result<IWorkbenchWindow>() {
 95  
                         public IWorkbenchWindow run() {
 96  3
                                 return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
 97  
                         }
 98  
                 });
 99  
         }
 100  
         
 101  
         private Widget getActiveWorkbenchWindowShell() {
 102  1
                 return getActiveWorkbenchWindow().getShell();
 103  
         }
 104  
 
 105  2
         private IWorkbenchPage activePage() {
 106  2
                 return getActiveWorkbenchWindow().getActivePage();
 107  
         }
 108  
 
 109  
         private IPerspectiveDescriptor[] perspectives() {
 110  0
                 return PlatformUI.getWorkbench().getPerspectiveRegistry().getPerspectives();
 111  
         }
 112  
 
 113  
 }