Author: jpeterka
Date: 2010-10-22 01:37:17 -0400 (Fri, 22 Oct 2010)
New Revision: 25986
Added:
trunk/tests/plugins/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/matcher/
trunk/tests/plugins/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/matcher/WithItem.java
trunk/tests/plugins/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/widgets/SWTBotMultiPageEditor.java
Log:
SWTBotMultiPageEditor and required matcher added
Added:
trunk/tests/plugins/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/matcher/WithItem.java
===================================================================
---
trunk/tests/plugins/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/matcher/WithItem.java
(rev 0)
+++
trunk/tests/plugins/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/matcher/WithItem.java 2010-10-22
05:37:17 UTC (rev 25986)
@@ -0,0 +1,59 @@
+package org.jboss.tools.ui.bot.ext.matcher;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+
+import org.eclipse.swt.widgets.Item;
+import org.eclipse.swtbot.swt.finder.matchers.AbstractMatcher;
+import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+
+public class WithItem<T extends Item> extends AbstractMatcher<T> {
+ protected final Matcher<?> itemMatcher;
+ protected final ArrayList<T> matches;
+
+ /**
+ * Matches widgets which contains <code>item(s)</code>, as returned by
<code>getItems</code> method, that match
+ * given matcher...i.e CTabFolder with Item with text "xyz"
+ * Note: Code taken from unreleased SWTBot, after release will be deprecated and
removed
+ *
+ * @param itemMatcher the item matcher
+ */
+ public WithItem(Matcher<?> itemMatcher) {
+ this.itemMatcher = itemMatcher;
+ this.matches = new ArrayList<T>();
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("with item");
+ this.itemMatcher.describeTo(description);
+ }
+
+ protected boolean doMatch(Object obj) {
+ matches.clear();
+ try {
+ for (T item : getItems(obj)) {
+ if (itemMatcher.matches(item))
+ matches.add(item);
+ }
+ } catch (Exception e) {
+ // do nothing
+ }
+ return !matches.isEmpty();
+ }
+
+ @SuppressWarnings("unchecked")
+ private T[] getItems(Object obj) throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
+ return (T[]) SWTUtils.invokeMethod(obj, "getItems");
+ }
+
+ public ArrayList<T> getAllMatches() {
+ return matches;
+ }
+
+ public T get(int index) {
+ return matches.get(index);
+ }
+
+}
Property changes on:
trunk/tests/plugins/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/matcher/WithItem.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/tests/plugins/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/widgets/SWTBotMultiPageEditor.java
===================================================================
---
trunk/tests/plugins/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/widgets/SWTBotMultiPageEditor.java
(rev 0)
+++
trunk/tests/plugins/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/widgets/SWTBotMultiPageEditor.java 2010-10-22
05:37:17 UTC (rev 25986)
@@ -0,0 +1,145 @@
+package org.jboss.tools.ui.bot.ext.widgets;
+
+import static org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec;
+import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.allOf;
+import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.widgetOfType;
+import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;
+import static org.hamcrest.Matchers.equalTo;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.swt.custom.CTabFolder;
+import org.eclipse.swt.custom.CTabItem;
+import org.eclipse.swt.widgets.Widget;
+import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
+import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
+import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
+import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
+import org.eclipse.swtbot.swt.finder.results.IntResult;
+import org.eclipse.swtbot.swt.finder.results.Result;
+import org.eclipse.swtbot.swt.finder.widgets.SWTBotCTabItem;
+import org.eclipse.ui.IEditorReference;
+import org.hamcrest.Matcher;
+import org.jboss.tools.ui.bot.ext.matcher.WithItem;
+
+/**
+ * SWTBotMultiPageEditor taken from SWTBot. Will be deprecated after
+ * it's released in SWTBot
+ * @author ketan
+ *
+ */
+public class SWTBotMultiPageEditor extends SWTBotEditor {
+ /**
+ * The tabFolder widget.
+ */
+ protected final CTabFolder tabFolder;
+
+ /**
+ * Constructs an instance of the given object.
+ *
+ * @param editorReference the editor reference.
+ * @param bot the instance of {@link SWTWorkbenchBot} which will be used to drive
operations on behalf of this object.
+ * @throws WidgetNotFoundException if the widget is <code>null</code> or
widget has been disposed.
+ */
+ public SWTBotMultiPageEditor(IEditorReference editorReference, SWTWorkbenchBot bot) {
+ super(editorReference, bot);
+ tabFolder = (CTabFolder) findWidget(widgetOfType(CTabFolder.class));
+ }
+
+ /**
+ * Find the CTabItem whose text matches the given matcher.
+ *
+ * @param titleMatcher the text matcher
+ * @return a {@link SWTBotCTabItem} with the specified tab name.
+ */
+ private SWTBotCTabItem findPage(Matcher<? extends Widget> titleMatcher) {
+ WithItem<CTabItem> itemMatcher = new
WithItem<CTabItem>(allOf(widgetOfType(CTabItem.class), titleMatcher));
+ if (itemMatcher.matches(tabFolder))
+ return new SWTBotCTabItem(itemMatcher.get(0));
+ throw new WidgetNotFoundException("Could not find page with title " +
titleMatcher);
+ }
+
+ /**
+ * Returns the number of pages in this multi-page editor.
+ *
+ * @return the number of pages
+ */
+ public int getPageCount() {
+ return syncExec(new IntResult() {
+ public Integer run() {
+ return tabFolder.getItemCount();
+ }
+ });
+ }
+
+ /**
+ * Sets the currently active page.
+ *
+ * @param pageText the text label for the page to be activated
+ * @return the {@link CTabItem} that was activated.
+ */
+ public SWTBotCTabItem activatePage(String pageText) {
+ return activatePage(withMnemonic(pageText));
+ }
+
+ /**
+ * Sets the currently active page.
+ *
+ * @param titleMatcher the title matcher for the page to be activated.
+ * @return the {@link CTabItem} that was activated.
+ */
+ public SWTBotCTabItem activatePage(Matcher<? extends Widget> titleMatcher) {
+ return findPage(titleMatcher).show().activate();
+ }
+
+ /**
+ * Returns the title of the currently active page or <code>null</code> if
there is no active page
+ *
+ * @return the title of the currently active page or <code>null</code> if
there is no active page
+ */
+ public String getActivePage() {
+ CTabItem tab = tabFolder.getSelection();
+ if (tab != null) {
+ return new SWTBotCTabItem(tab).getText();
+ }
+ return null;
+ }
+
+ /**
+ * Returns a list of title of all the pages in this multi-page editor.
+ *
+ * @return List of title of all pages; empty list if no pages.
+ */
+ public List<String> getPages() {
+ List<String> pages = null;
+ if (getPageCount() > 0) {
+ pages = UIThreadRunnable.syncExec(new Result<List<String>>() {
+ public List<String> run() {
+ ArrayList<String> titles = new ArrayList<String>();
+ for (CTabItem item : tabFolder.getItems()) {
+ titles.add(item.getText());
+ }
+ return titles;
+ }
+ });
+ }
+ return pages == null ? new ArrayList<String>() : pages;
+ }
+
+ /**
+ * @param pageText the page title to test
+ * @return <code>true</code> if the currently active page has given title,
<code>false</code> otherwise.
+ */
+ public boolean isActivePage(String pageText) {
+ return isActivePage(equalTo(pageText));
+ }
+
+ /**
+ * @param titleMatcher the title matcher for the active page
+ * @return <code>true</code> if the currently active page title matches,
<code>false</code> otherwise.
+ */
+ public boolean isActivePage(Matcher<String> titleMatcher) {
+ return titleMatcher.matches(getActivePage());
+ }
+}
Property changes on:
trunk/tests/plugins/org.jboss.tools.ui.bot.ext/src/org/jboss/tools/ui/bot/ext/widgets/SWTBotMultiPageEditor.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Show replies by date