Coverage Report - org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTWorkbenchBot
61%
49/80
28%
4/14
1.323
SWTWorkbenchBot$1
100%
4/4
N/A
1.323
 
 1  2
 /*******************************************************************************
 2  
  * Copyright (c) 2009,2010 SWTBot Committers 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  
  *     Ralf Ebert www.ralfebert.de - (bug 271630) SWTBot Improved RCP / Workbench support
 10  
  *     Ketan Padegaonkar - (bug 260088) Support for MultiPageEditorPart
 11  
  *******************************************************************************/
 12  
 package org.eclipse.swtbot.eclipse.finder;
 13  
 
 14  
 import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartId;
 15  
 import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartName;
 16  
 import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPerspectiveId;
 17  
 import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPerspectiveLabel;
 18  
 import static org.eclipse.swtbot.eclipse.finder.waits.Conditions.waitForEditor;
 19  
 import static org.eclipse.swtbot.eclipse.finder.waits.Conditions.waitForView;
 20  
 import static org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec;
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.List;
 24  
 
 25  
 import org.eclipse.swtbot.eclipse.finder.finders.WorkbenchContentsFinder;
 26  
 import org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory;
 27  
 import org.eclipse.swtbot.eclipse.finder.waits.WaitForEditor;
 28  
 import org.eclipse.swtbot.eclipse.finder.waits.WaitForView;
 29  
 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
 30  
 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotMultiPageEditor;
 31  
 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotPerspective;
 32  
 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
 33  
 import org.eclipse.swtbot.swt.finder.SWTBot;
 34  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 35  
 import org.eclipse.swtbot.swt.finder.results.Result;
 36  
 import org.eclipse.ui.IEditorReference;
 37  
 import org.eclipse.ui.IPerspectiveDescriptor;
 38  
 import org.eclipse.ui.IViewReference;
 39  
 import org.eclipse.ui.IWorkbench;
 40  
 import org.eclipse.ui.PlatformUI;
 41  
 import org.hamcrest.Matcher;
 42  
 import org.hamcrest.Matchers;
 43  
 
 44  
 /**
 45  
  * SWTWorkbenchBot is a {@link SWTBot} with capabilities for testing Eclipse workbench items like views, editors and
 46  
  * perspectives
 47  
  * 
 48  
  * @author Ralf Ebert www.ralfebert.de (bug 271630)
 49  
  * @version $Id$
 50  
  */
 51  
 public class SWTWorkbenchBot extends SWTBot {
 52  
 
 53  
         private final WorkbenchContentsFinder        workbenchContentsFinder;
 54  
 
 55  
         /**
 56  
          * Constructs a workbench bot
 57  
          */
 58  25
         public SWTWorkbenchBot() {
 59  25
                 workbenchContentsFinder = new WorkbenchContentsFinder();
 60  25
         }
 61  
 
 62  
         /**
 63  
          * Returns the perspective matching the given matcher
 64  
          * 
 65  
          * @param matcher the matcher used to find the perspective
 66  
          * @return a perspective matching the matcher
 67  
          * @throws WidgetNotFoundException if the perspective is not found
 68  
          */
 69  
         public SWTBotPerspective perspective(Matcher<?> matcher) {
 70  2
                 List<IPerspectiveDescriptor> perspectives = workbenchContentsFinder.findPerspectives(matcher);
 71  2
                 return new SWTBotPerspective(perspectives.get(0), this);
 72  
         }
 73  
 
 74  
         /**
 75  
          * Shortcut for perspective(withPerspectiveLabel(label))
 76  
          * 
 77  
          * @param label the "human readable" label for the perspective
 78  
          * @return a perspective with the specified <code>label</code>
 79  
          * @see #perspective(Matcher)
 80  
          * @see WidgetMatcherFactory#withPerspectiveLabel(Matcher)
 81  
          */
 82  
         public SWTBotPerspective perspectiveByLabel(String label) {
 83  0
                 return perspective(withPerspectiveLabel(label));
 84  
         }
 85  
 
 86  
         /**
 87  
          * Shortcut for perspective(perspectiveById(label))
 88  
          * 
 89  
          * @param id the perspective id
 90  
          * @return a perspective with the specified <code>label</code>
 91  
          * @see #perspective(Matcher)
 92  
          * @see WidgetMatcherFactory#withPerspectiveId(Matcher)
 93  
          */
 94  
         public SWTBotPerspective perspectiveById(String id) {
 95  2
                 return perspective(withPerspectiveId(id));
 96  
         }
 97  
 
 98  
         /**
 99  
          * @param matcher Matcher for IPerspectiveDescriptor
 100  
          * @return all available matching perspectives
 101  
          */
 102  
         public List<SWTBotPerspective> perspectives(Matcher<?> matcher) {
 103  0
                 List<IPerspectiveDescriptor> perspectives = workbenchContentsFinder.findPerspectives(matcher);
 104  
 
 105  0
                 List<SWTBotPerspective> perspectiveBots = new ArrayList<SWTBotPerspective>();
 106  0
                 for (IPerspectiveDescriptor perspectiveDescriptor : perspectives)
 107  0
                         perspectiveBots.add(new SWTBotPerspective(perspectiveDescriptor, this));
 108  
 
 109  0
                 return perspectiveBots;
 110  
         }
 111  
 
 112  
         /**
 113  
          * @return all available perspectives
 114  
          */
 115  
         public List<SWTBotPerspective> perspectives() {
 116  0
                 return perspectives(Matchers.anything());
 117  
         }
 118  
 
 119  
         /**
 120  
          * Waits for a view matching the given matcher to appear in the active workbench page and returns it
 121  
          * 
 122  
          * @param matcher the matcher used to match views
 123  
          * @return views that match the matcher
 124  
          * @throws WidgetNotFoundException if the view is not found
 125  
          */
 126  
         public SWTBotView view(Matcher<IViewReference> matcher) {
 127  22
                 WaitForView waitForView = waitForView(matcher);
 128  22
                 waitUntilWidgetAppears(waitForView);
 129  18
                 return new SWTBotView(waitForView.get(0), this);
 130  
         }
 131  
 
 132  
         /**
 133  
          * Shortcut for view(withPartName(title))
 134  
          * 
 135  
          * @param title the "human readable" title
 136  
          * @return the view with the specified title
 137  
          * @see WidgetMatcherFactory#withPartName(Matcher)
 138  
          */
 139  
         public SWTBotView viewByTitle(String title) {
 140  22
                 Matcher<IViewReference> withPartName = withPartName(title);
 141  22
                 return view(withPartName);
 142  
         }
 143  
 
 144  
         /**
 145  
          * Shortcut for view(withPartId(id))
 146  
          * 
 147  
          * @param id the view id
 148  
          * @return the view with the specified id
 149  
          * @see WidgetMatcherFactory#withPartId(String)
 150  
          */
 151  
         public SWTBotView viewById(String id) {
 152  0
                 Matcher<IViewReference> withPartId = withPartId(id);
 153  0
                 return view(withPartId);
 154  
         }
 155  
 
 156  
         /**
 157  
          * Returns all views which are opened currently (no waiting!) which match the given matcher
 158  
          * 
 159  
          * @param matcher the matcher used to find views
 160  
          * @return the list of all matching views
 161  
          */
 162  
         public List<SWTBotView> views(Matcher<?> matcher) {
 163  0
                 List<IViewReference> views = workbenchContentsFinder.findViews(matcher);
 164  
 
 165  0
                 List<SWTBotView> viewBots = new ArrayList<SWTBotView>();
 166  0
                 for (IViewReference viewReference : views)
 167  0
                         viewBots.add(new SWTBotView(viewReference, this));
 168  0
                 return viewBots;
 169  
         }
 170  
 
 171  
         /**
 172  
          * @return all views which are opened currently
 173  
          */
 174  
         public List<SWTBotView> views() {
 175  0
                 return views(Matchers.anything());
 176  
         }
 177  
 
 178  
         /**
 179  
          * Returns the active workbench view part
 180  
          * 
 181  
          * @return the active view, if any
 182  
          * @throws WidgetNotFoundException if there is no active view
 183  
          */
 184  
         public SWTBotView activeView() {
 185  0
                 IViewReference view = workbenchContentsFinder.findActiveView();
 186  0
                 if (view == null)
 187  0
                         throw new WidgetNotFoundException("There is no active view"); //$NON-NLS-1$
 188  0
                 return new SWTBotView(view, this);
 189  
         }
 190  
 
 191  
         /**
 192  
          * Waits for a editor matching the given matcher to appear in the active workbench page and returns it
 193  
          * 
 194  
          * @param matcher the matcher used to find the editor
 195  
          * @return an editor that matches the matcher
 196  
          * @throws WidgetNotFoundException if the editor is not found
 197  
          */
 198  
         public SWTBotEditor editor(Matcher<IEditorReference> matcher) {
 199  6
                 WaitForEditor waitForEditor = waitForEditor(matcher);
 200  6
                 waitUntilWidgetAppears(waitForEditor);
 201  6
                 return new SWTBotEditor(waitForEditor.get(0), this);
 202  
         }
 203  
 
 204  
         /**
 205  
          * @return all editors which are opened currently (no waiting!) which match the given matcher
 206  
          * @param matcher the matcher used to find all editors
 207  
          */
 208  
         public List<SWTBotEditor> editors(Matcher<?> matcher) {
 209  12
                 List<IEditorReference> editors = workbenchContentsFinder.findEditors(matcher);
 210  
 
 211  12
                 List<SWTBotEditor> editorBots = new ArrayList<SWTBotEditor>();
 212  37
                 for (IEditorReference editorReference : editors)
 213  13
                         editorBots.add(new SWTBotEditor(editorReference, this));
 214  
 
 215  12
                 return editorBots;
 216  
         }
 217  
 
 218  
         /**
 219  
          * @return all editors which are opened currently
 220  
          */
 221  
         public List<? extends SWTBotEditor> editors() {
 222  12
                 return editors(Matchers.anything());
 223  
         }
 224  
 
 225  
         /**
 226  
          * Shortcut for editor(withPartName(title))
 227  
          * 
 228  
          * @param fileName the the filename on the editor tab
 229  
          * @return the editor with the specified title
 230  
          * @see #editor(Matcher)
 231  
          */
 232  
         public SWTBotEditor editorByTitle(String fileName) {
 233  6
                 Matcher<IEditorReference> withPartName = withPartName(fileName);
 234  6
                 return editor(withPartName);
 235  
         }
 236  
 
 237  
         /**
 238  
          * Shortcut for editor(withPartId(id))
 239  
          * 
 240  
          * @param id the the id on the editor tab
 241  
          * @return the editor with the specified title
 242  
          * @see #editor(Matcher)
 243  
          */
 244  
         public SWTBotEditor editorById(String id) {
 245  0
                 Matcher<IEditorReference> withPartId = withPartId(id);
 246  0
                 return editor(withPartId);
 247  
         }
 248  
 
 249  
         /**
 250  
          * Returns the active workbench editor part
 251  
          * 
 252  
          * @return the active editor, if any
 253  
          * @throws WidgetNotFoundException if there is no active view
 254  
          */
 255  
         public SWTBotEditor activeEditor() {
 256  6
                 IEditorReference editor = workbenchContentsFinder.findActiveEditor();
 257  6
                 if (editor == null)
 258  0
                         throw new WidgetNotFoundException("There is no active editor"); //$NON-NLS-1$
 259  6
                 return new SWTBotEditor(editor, this);
 260  
         }
 261  
         
 262  
         /**
 263  
          * Waits for a multipage editor matching the given matcher to appear in the active workbench page and returns it
 264  
          * 
 265  
          * @param matcher the matcher used to find the editor
 266  
          * @return an editor that matches the matcher
 267  
          * @throws WidgetNotFoundException if the editor is not found
 268  
          */
 269  
         public SWTBotMultiPageEditor multipageEditor(Matcher<IEditorReference> matcher) {
 270  7
                 WaitForEditor waitForEditor = waitForEditor(matcher);
 271  7
                 waitUntilWidgetAppears(waitForEditor);
 272  7
                 return new SWTBotMultiPageEditor(waitForEditor.get(0), this);
 273  
         }
 274  
 
 275  
         /**
 276  
          * @return all multipage editors which are opened currently (no waiting!) which match the given matcher
 277  
          * @param matcher the matcher used to find all editors
 278  
          */
 279  
         public List<SWTBotMultiPageEditor> multipageEditors(Matcher<?> matcher) {
 280  0
                 List<IEditorReference> editors = workbenchContentsFinder.findEditors(matcher);
 281  
 
 282  0
                 List<SWTBotMultiPageEditor> editorBots = new ArrayList<SWTBotMultiPageEditor>();
 283  0
                 for (IEditorReference editorReference : editors)
 284  0
                         editorBots.add(new SWTBotMultiPageEditor(editorReference, this));
 285  
 
 286  0
                 return editorBots;
 287  
         }
 288  
 
 289  
         /**
 290  
          * @return all editors which are opened currently
 291  
          */
 292  
         public List<? extends SWTBotMultiPageEditor> multipageEditors() {
 293  0
                 return multipageEditors(Matchers.anything());
 294  
         }
 295  
 
 296  
         /**
 297  
          * Shortcut for multipageEditor(withPartName(title))
 298  
          * 
 299  
          * @param fileName the the filename on the editor tab
 300  
          * @return the editor with the specified title
 301  
          * @see #editor(Matcher)
 302  
          */
 303  
         public SWTBotMultiPageEditor multipageEditorByTitle(String fileName) {
 304  7
                 Matcher<IEditorReference> withPartName = withPartName(fileName);
 305  7
                 return multipageEditor(withPartName);
 306  
         }
 307  
 
 308  
         /**
 309  
          * Shortcut for multipageEditor(withPartId(id))
 310  
          * 
 311  
          * @param id the the id on the editor tab
 312  
          * @return the editor with the specified title
 313  
          * @see #editor(Matcher)
 314  
          */
 315  
         public SWTBotMultiPageEditor multipageEditorById(String id) {
 316  0
                 Matcher<IEditorReference> withPartId = withPartId(id);
 317  0
                 return multipageEditor(withPartId);
 318  
         }
 319  
         
 320  
         /**
 321  
          * @return the active perspective in the active workbench page
 322  
          */
 323  
         public SWTBotPerspective activePerspective() {
 324  2
                 IPerspectiveDescriptor perspective = workbenchContentsFinder.findActivePerspective();
 325  2
                 if (perspective == null)
 326  0
                         throw new WidgetNotFoundException("There is no active perspective"); //$NON-NLS-1$
 327  2
                 return new SWTBotPerspective(perspective, this);
 328  
         }
 329  
 
 330  
         /**
 331  
          * Does a <em>best effort</em> to reset the workbench. This method attempts to:
 332  
          * <ul>
 333  
          * <li>close all non-workbench windows</li>
 334  
          * <li>save and close all open editors</li>
 335  
          * <li>reset the <em>active</em> perspective</li>
 336  
          * <li>switch to the default perspective for the workbench</li>
 337  
          * <li>reset the <em>default</em> perspective for the workbench</li>
 338  
          * <ul>
 339  
          */
 340  
         public void resetWorkbench() {
 341  1
                 closeAllShells();
 342  1
                 saveAllEditors();
 343  1
                 closeAllEditors();
 344  1
                 resetActivePerspective();
 345  
 
 346  1
                 defaultPerspective().activate();
 347  1
                 resetActivePerspective();
 348  1
         }
 349  
 
 350  
         /**
 351  
          * Returns the default perspective as defined in the WorkbenchAdvisor of the application.
 352  
          */
 353  
         public SWTBotPerspective defaultPerspective() {
 354  1
                 return syncExec(new Result<SWTBotPerspective>() {
 355  
 
 356  
                         public SWTBotPerspective run() {
 357  1
                                 IWorkbench workbench = PlatformUI.getWorkbench();
 358  1
                                 return perspectiveById(workbench.getPerspectiveRegistry().getDefaultPerspective());
 359  
                         }
 360  
                 });
 361  
         }
 362  
 
 363  
         public void closeAllEditors() {
 364  1
                 new DefaultWorkbench(this).closeAllEditors();
 365  1
         }
 366  
 
 367  
         public void saveAllEditors() {
 368  1
                 new DefaultWorkbench(this).saveAllEditors();
 369  1
         }
 370  
 
 371  
         public SWTBotPerspective resetActivePerspective() {
 372  2
                 new DefaultWorkbench(this).resetActivePerspective();
 373  2
                 return activePerspective();
 374  
         }
 375  
 
 376  
         public void closeAllShells() {
 377  1
                 new DefaultWorkbench(this).closeAllShells();
 378  1
         }
 379  
 
 380  
 }