Coverage Report - org.eclipse.swtbot.eclipse.finder.widgets.SWTBotMultiPageEditor
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotMultiPageEditor
90%
18/20
50%
4/8
1.727
SWTBotMultiPageEditor$1
100%
3/3
N/A
1.727
SWTBotMultiPageEditor$2
100%
6/6
100%
2/2
1.727
 
 1  4
 /*******************************************************************************
 2  
  * Copyright (c) 2010 Ketan Padegaonkar 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  
  *     Ketan Padegaonkar - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.eclipse.finder.widgets;
 12  
 
 13  
 import static org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec;
 14  
 import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.allOf;
 15  
 import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.widgetOfType;
 16  
 import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;
 17  
 import static org.hamcrest.Matchers.equalTo;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.eclipse.swt.custom.CTabFolder;
 23  
 import org.eclipse.swt.custom.CTabItem;
 24  
 import org.eclipse.swt.widgets.Widget;
 25  
 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
 26  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 27  
 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
 28  
 import org.eclipse.swtbot.swt.finder.matchers.WithItem;
 29  
 import org.eclipse.swtbot.swt.finder.results.IntResult;
 30  
 import org.eclipse.swtbot.swt.finder.results.Result;
 31  
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotCTabItem;
 32  
 import org.eclipse.ui.IEditorReference;
 33  
 import org.hamcrest.Matcher;
 34  
 
 35  
 /**
 36  
  * Base class for all multi-page editors in Eclipse.
 37  
  * <p>
 38  
  * <b>Note:</b> It is recommend that clients decorate this class with their own custom wrappers.
 39  
  * </p>
 40  
  * <p>
 41  
  * <b>Note:</b> This API is very new, experimental and subject to change.
 42  
  * </p>
 43  
  * 
 44  
  * <pre>
 45  
  * public class PDEManifestEditor extends SWTBotMultiPageEditor {
 46  
  *         public PDEManifestEditor(SWTBotMultiPageEditor editor, SWTWorkbenchBot bot) {
 47  
  *                 super(editor.getReference(), bot);
 48  
  *                 this.editor = editor;
 49  
  *         }
 50  
  * 
 51  
  *         // any custom behavior you need to test
 52  
  *         public activateBuildPropertiesTab() {
 53  
  *                 activatePage(&quot;build.properties&quot;);
 54  
  *         }
 55  
  * 
 56  
  *         public SWTBotStyledText buildProperties() {
 57  
  *                 activateBuildPropertiesTab();
 58  
  *                 return SWTBotStyledText((StyledText) findWidget(widgetOfType(StyledText.class)));
 59  
  *         }
 60  
  * }
 61  
  * </pre>
 62  
  * 
 63  
  * @author Ketan Patel
 64  
  * @author Ketan Padegaonkar
 65  
  * @since 2.0
 66  
  */
 67  
 public class SWTBotMultiPageEditor extends SWTBotEditor {
 68  
         /**
 69  
          * The tabFolder widget.
 70  
          */
 71  
         protected final CTabFolder        tabFolder;
 72  
 
 73  
         /**
 74  
          * Constructs an instance of the given object.
 75  
          * 
 76  
          * @param editorReference the editor reference.
 77  
          * @param bot the instance of {@link SWTWorkbenchBot} which will be used to drive operations on behalf of this
 78  
          *            object.
 79  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 80  
          */
 81  
         public SWTBotMultiPageEditor(IEditorReference editorReference, SWTWorkbenchBot bot) {
 82  14
                 super(editorReference, bot);
 83  14
                 tabFolder = findWidget(widgetOfType(CTabFolder.class));
 84  14
         }
 85  
 
 86  
         /**
 87  
          * Find the CTabItem whose text matches the given matcher.
 88  
          * 
 89  
          * @param titleMatcher the text matcher
 90  
          * @return a {@link SWTBotCTabItem} with the specified tab name.
 91  
          */
 92  
         private SWTBotCTabItem findPage(Matcher<? extends Widget> titleMatcher) {
 93  2
                 WithItem<CTabItem> itemMatcher = WithItem.withItem(allOf(widgetOfType(CTabItem.class), titleMatcher));
 94  2
                 if (itemMatcher.matches(tabFolder))
 95  2
                         return new SWTBotCTabItem(itemMatcher.get(0));
 96  0
                 throw new WidgetNotFoundException("Could not find page with title " + titleMatcher);
 97  
         }
 98  
 
 99  
         /**
 100  
          * Returns the number of pages in this multi-page editor.
 101  
          * 
 102  
          * @return the number of pages
 103  
          */
 104  
         public int getPageCount() {
 105  2
                 return syncExec(new IntResult() {
 106  
                         public Integer run() {
 107  2
                                 return tabFolder.getItemCount();
 108  
                         }
 109  
                 });
 110  
         }
 111  
 
 112  
         /**
 113  
          * Sets the currently active page.
 114  
          * 
 115  
          * @param pageText the text label for the page to be activated
 116  
          * @return the {@link CTabItem} that was activated.
 117  
          */
 118  
         public SWTBotCTabItem activatePage(String pageText) {
 119  2
                 return activatePage(withMnemonic(pageText));
 120  
         }
 121  
 
 122  
         /**
 123  
          * Sets the currently active page.
 124  
          * 
 125  
          * @param titleMatcher the title matcher for the page to be activated.
 126  
          * @return the {@link CTabItem} that was activated.
 127  
          */
 128  
         public SWTBotCTabItem activatePage(Matcher<? extends Widget> titleMatcher) {
 129  2
                 return findPage(titleMatcher).show().activate();
 130  
         }
 131  
 
 132  
         /**
 133  
          * Returns the title of the currently active page or <code>null</code> if there is no active page
 134  
          * 
 135  
          * @return the title of the currently active page or <code>null</code> if there is no active page
 136  
          */
 137  
         public String getActivePageTitle() {
 138  7
                 CTabItem tab = tabFolder.getSelection();
 139  7
                 if (tab != null) {
 140  7
                         return new SWTBotCTabItem(tab).getText();
 141  
                 }
 142  0
                 return null;
 143  
         }
 144  
 
 145  
         /**
 146  
          * Returns a list of title of all the pages in this multi-page editor.
 147  
          * 
 148  
          * @return List of title of all pages; empty list if no pages.
 149  
          */
 150  
         public List<String> getPagesTitles() {
 151  1
                 List<String> pages = null;
 152  1
                 if (getPageCount() > 0) {
 153  1
                         pages = UIThreadRunnable.syncExec(new Result<List<String>>() {
 154  
                                 public List<String> run() {
 155  1
                                         ArrayList<String> titles = new ArrayList<String>();
 156  4
                                         for (CTabItem item : tabFolder.getItems()) {
 157  3
                                                 titles.add(item.getText());
 158  
                                         }
 159  1
                                         return titles;
 160  
                                 }
 161  
                         });
 162  
                 }
 163  1
                 return pages == null ? new ArrayList<String>() : pages;
 164  
         }
 165  
 
 166  
         /**
 167  
          * @param pageText the page title to test
 168  
          * @return <code>true</code> if the currently active page has given title, <code>false</code> otherwise.
 169  
          */
 170  
         public boolean isActivePage(String pageText) {
 171  5
                 return isActivePage(equalTo(pageText));
 172  
         }
 173  
 
 174  
         /**
 175  
          * @param titleMatcher the title matcher for the active page
 176  
          * @return <code>true</code> if the currently active page title matches, <code>false</code> otherwise.
 177  
          */
 178  
         public boolean isActivePage(Matcher<String> titleMatcher) {
 179  6
                 return titleMatcher.matches(getActivePageTitle());
 180  
         }
 181  
 }