diff options
author | Ketan Padegaonkar | 2011-01-30 01:58:41 +0000 |
---|---|---|
committer | Ketan Padegaonkar | 2011-01-30 01:59:18 +0000 |
commit | c094846872e8cb2426b33658cbd379d572cf242b (patch) | |
tree | 7c371f8f75f25122587a2560b905ee7900d7ae4e | |
parent | 3146430cb2cdd38e9577e6bee1afd81b72248018 (diff) | |
download | org.eclipse.swtbot-c094846872e8cb2426b33658cbd379d572cf242b.tar.gz org.eclipse.swtbot-c094846872e8cb2426b33658cbd379d572cf242b.tar.xz org.eclipse.swtbot-c094846872e8cb2426b33658cbd379d572cf242b.zip |
RESOLVED - 259799: Add support for ExpandBar
https://bugs.eclipse.org/bugs/show_bug.cgi?id=259799
8 files changed, 644 insertions, 1 deletions
diff --git a/org.eclipse.swtbot.generator/widgets.xml b/org.eclipse.swtbot.generator/widgets.xml index cac0276f..61fb5d2d 100644 --- a/org.eclipse.swtbot.generator/widgets.xml +++ b/org.eclipse.swtbot.generator/widgets.xml @@ -25,6 +25,7 @@ <widget class="org.eclipse.swtbot.swt.finder.widgets.SWTBotSlider" /> <widget class="org.eclipse.swtbot.swt.finder.widgets.SWTBotBrowser" /> <widget class="org.eclipse.swtbot.swt.finder.widgets.SWTBotScale" /> + <widget class="org.eclipse.swtbot.swt.finder.widgets.SWTBotExpandBar" /> <!-- <widget class="org.eclipse.swtbot.swt.finder.widgets.SWTBotExpandBar" /> <widget class="org.eclipse.swtbot.swt.finder.widgets.SWTBotShell" /> diff --git a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotExpandBarTest.java b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotExpandBarTest.java new file mode 100644 index 00000000..5d760857 --- /dev/null +++ b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotExpandBarTest.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2011 Ketan Padegaonkar and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Ketan Padegaonkar - initial API and implementation + *******************************************************************************/ +package org.eclipse.swtbot.swt.finder.widgets; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.eclipse.swtbot.swt.finder.finders.AbstractSWTTestCase; +import org.junit.Before; +import org.junit.Test; + +/** + * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> + */ +public class SWTBotExpandBarTest extends AbstractSWTTestCase { + + @Test + public void shouldGetExpandItemCount() throws Exception { + assertEquals(2, bot.expandBar().itemCount()); + } + + @Test + public void shouldGetExpandedAndCollapsedItemCount() throws Exception { + SWTBotExpandBar expandBar = bot.expandBar(); + assertEquals(1, expandBar.expandedItemCount()); + assertEquals(1, expandBar.collapsedItemCount()); + } + + @Test + public void shouldExpandAnItem() throws Exception { + SWTBotExpandBar expandBar = bot.expandBar(); + SWTBotExpandItem item1 = expandBar.expandItem("What is your favorite button?"); + SWTBotExpandItem item2 = expandBar.expandItem("What is your favorite icon?"); + assertEquals(2, expandBar.expandedItemCount()); + assertEquals(0, expandBar.collapsedItemCount()); + + assertTrue(item1.isExpanded()); + assertTrue(item2.isExpanded()); + + assertFalse(item1.isCollapsed()); + assertFalse(item2.isCollapsed()); + } + + @Test + public void shouldCollapseAnItem() throws Exception { + SWTBotExpandBar expandBar = bot.expandBar(); + SWTBotExpandItem item1 = expandBar.collapseItem("What is your favorite button?"); + SWTBotExpandItem item2 = expandBar.collapseItem("What is your favorite icon?"); + assertEquals(0, expandBar.expandedItemCount()); + assertEquals(2, expandBar.collapsedItemCount()); + + assertFalse(item1.isExpanded()); + assertFalse(item2.isExpanded()); + + assertTrue(item1.isCollapsed()); + assertTrue(item2.isCollapsed()); + } + + @Before + public void setUp() throws Exception { + super.setUp(); + bot.tabItem("ExpandBar").activate(); + } + +} diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/SWTBot.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/SWTBot.java index 37af160f..6c528bba 100644 --- a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/SWTBot.java +++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/SWTBot.java @@ -12,6 +12,7 @@ import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.DateTime; +import org.eclipse.swt.widgets.ExpandBar; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Link; import org.eclipse.swt.widgets.List; @@ -38,6 +39,7 @@ import org.eclipse.swtbot.swt.finder.widgets.SWTBotCTabItem; import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox; import org.eclipse.swtbot.swt.finder.widgets.SWTBotCombo; import org.eclipse.swtbot.swt.finder.widgets.SWTBotDateTime; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotExpandBar; import org.eclipse.swtbot.swt.finder.widgets.SWTBotLabel; import org.eclipse.swtbot.swt.finder.widgets.SWTBotLink; import org.eclipse.swtbot.swt.finder.widgets.SWTBotList; @@ -4372,6 +4374,134 @@ public class SWTBot extends SWTBotFactory { return new SWTBotScale((Scale) widget(matcher, index), matcher); } + /** + * @param label the label on the widget. + * @return a {@link SWTBotExpandBar} with the specified <code>label</code>. + * @throws WidgetNotFoundException if the widget is not found or is disposed. + */ + public SWTBotExpandBar expandBarWithLabel(String label) { + return expandBarWithLabel(label, 0); + } + + /** + * @param label the label on the widget. + * @param index the index of the widget. + * @return a {@link SWTBotExpandBar} with the specified <code>label</code>. + * @throws WidgetNotFoundException if the widget is not found or is disposed. + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + public SWTBotExpandBar expandBarWithLabel(String label, int index) { + Matcher matcher = allOf(widgetOfType(ExpandBar.class), withLabel(label)); + return new SWTBotExpandBar((ExpandBar) widget(matcher, index), matcher); + } + + /** + * @param key the key set on the widget. + * @param value the value for the key. + * @return a {@link SWTBotExpandBar} with the specified <code>key/value</code>. + * @throws WidgetNotFoundException if the widget is not found or is disposed. + */ + public SWTBotExpandBar expandBarWithId(String key, String value) { + return expandBarWithId(key, value, 0); + } + + /** + * @param key the key set on the widget. + * @param value the value for the key. + * @param index the index of the widget. + * @return a {@link SWTBotExpandBar} with the specified <code>key/value</code>. + * @throws WidgetNotFoundException if the widget is not found or is disposed. + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + public SWTBotExpandBar expandBarWithId(String key, String value, int index) { + Matcher matcher = allOf(widgetOfType(ExpandBar.class), withId(key, value)); + return new SWTBotExpandBar((ExpandBar) widget(matcher, index), matcher); + } + + /** + * @param value the value for the key {@link org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences#DEFAULT_KEY}. + * @return a {@link SWTBotExpandBar} with the specified <code>value</code>. + * @throws WidgetNotFoundException if the widget is not found or is disposed. + */ + public SWTBotExpandBar expandBarWithId(String value) { + return expandBarWithId(value, 0); + } + + /** + * @param value the value for the key {@link org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences#DEFAULT_KEY}. + * @param index the index of the widget. + * @return a {@link SWTBotExpandBar} with the specified <code>value</code>. + * @throws WidgetNotFoundException if the widget is not found or is disposed. + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + public SWTBotExpandBar expandBarWithId(String value, int index) { + Matcher matcher = allOf(widgetOfType(ExpandBar.class), withId(value)); + return new SWTBotExpandBar((ExpandBar) widget(matcher, index), matcher); + } + + /** + * @param inGroup the inGroup on the widget. + * @return a {@link SWTBotExpandBar} with the specified <code>inGroup</code>. + * @throws WidgetNotFoundException if the widget is not found or is disposed. + */ + public SWTBotExpandBar expandBarInGroup(String inGroup) { + return expandBarInGroup(inGroup, 0); + } + + /** + * @param inGroup the inGroup on the widget. + * @param index the index of the widget. + * @return a {@link SWTBotExpandBar} with the specified <code>inGroup</code>. + * @throws WidgetNotFoundException if the widget is not found or is disposed. + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + public SWTBotExpandBar expandBarInGroup(String inGroup, int index) { + Matcher matcher = allOf(widgetOfType(ExpandBar.class), inGroup(inGroup)); + return new SWTBotExpandBar((ExpandBar) widget(matcher, index), matcher); + } + + /** + * @return a {@link SWTBotExpandBar} with the specified <code>none</code>. + * @throws WidgetNotFoundException if the widget is not found or is disposed. + */ + public SWTBotExpandBar expandBar() { + return expandBar(0); + } + + /** + * @param index the index of the widget. + * @return a {@link SWTBotExpandBar} with the specified <code>none</code>. + * @throws WidgetNotFoundException if the widget is not found or is disposed. + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + public SWTBotExpandBar expandBar(int index) { + Matcher matcher = allOf(widgetOfType(ExpandBar.class)); + return new SWTBotExpandBar((ExpandBar) widget(matcher, index), matcher); + } + + /** + * @param label the label on the widget. + * @param inGroup the inGroup on the widget. + * @return a {@link SWTBotExpandBar} with the specified <code>label</code> with the specified <code>inGroup</code>. + * @throws WidgetNotFoundException if the widget is not found or is disposed. + */ + public SWTBotExpandBar expandBarWithLabelInGroup(String label, String inGroup) { + return expandBarWithLabelInGroup(label, inGroup, 0); + } + + /** + * @param label the label on the widget. + * @param inGroup the inGroup on the widget. + * @param index the index of the widget. + * @return a {@link SWTBotExpandBar} with the specified <code>label</code> with the specified <code>inGroup</code>. + * @throws WidgetNotFoundException if the widget is not found or is disposed. + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + public SWTBotExpandBar expandBarWithLabelInGroup(String label, String inGroup, int index) { + Matcher matcher = allOf(widgetOfType(ExpandBar.class), withLabel(label), inGroup(inGroup)); + return new SWTBotExpandBar((ExpandBar) widget(matcher, index), matcher); + } + private Matcher<? extends List> withLabel(String label) { return WidgetMatcherFactory.withLabel(label, finder); } diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/resolvers/ExpandBarResolver.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/resolvers/ExpandBarResolver.java new file mode 100644 index 00000000..c125a99a --- /dev/null +++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/resolvers/ExpandBarResolver.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2011 Ketan Padegaonkar and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Ketan Padegaonkar - initial API and implementation + *******************************************************************************/ +package org.eclipse.swtbot.swt.finder.resolvers; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.eclipse.swt.widgets.ExpandBar; +import org.eclipse.swt.widgets.Widget; + +/** + * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> + */ +public class ExpandBarResolver implements IChildrenResolver, IParentResolver { + + public boolean canResolve(Widget w) { + return w instanceof ExpandBar; + } + + public List getChildren(Widget w) { + return hasChildren(w) ? Arrays.asList(((ExpandBar) w).getItems()) : new ArrayList(); + } + + public Widget getParent(Widget w) { + return canResolve(w) ? ((ExpandBar) w).getParent() : null; + } + + public Class[] getResolvableClasses() { + return new Class[] { ExpandBar.class }; + } + + public boolean hasChildren(Widget w) { + return canResolve(w) && ((ExpandBar) w).getItems().length > 0; + } + + public boolean hasParent(Widget w) { + return getParent(w) != null; + } + +} diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/resolvers/ExpandItemResolver.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/resolvers/ExpandItemResolver.java new file mode 100644 index 00000000..6960242a --- /dev/null +++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/resolvers/ExpandItemResolver.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2011 Ketan Padegaonkar and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Ketan Padegaonkar - initial API and implementation + *******************************************************************************/ +package org.eclipse.swtbot.swt.finder.resolvers; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.ExpandItem; +import org.eclipse.swt.widgets.Widget; + +/** + * Resolves {@link ExpandItem}s. + * + * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> + */ +public class ExpandItemResolver implements IChildrenResolver, IParentResolver { + + public List getChildren(Widget w) { + ArrayList children = new ArrayList(); + Control control = ((ExpandItem) w).getControl(); + if (control != null) + children.add(control); + return children; + } + + public boolean hasChildren(Widget w) { + return canResolve(w) && ((ExpandItem) w).getControl() != null; + } + + public boolean canResolve(Widget w) { + return w instanceof ExpandItem; + } + + public Class[] getResolvableClasses() { + return new Class[] { ExpandItem.class }; + } + + public Widget getParent(Widget w) { + return canResolve(w) ? ((ExpandItem) w).getParent() : null; + } + + public boolean hasParent(Widget w) { + return getParent(w) != null; + } + +} diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/resolvers/Resolvable.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/resolvers/Resolvable.java index 25b990f5..97494ef8 100644 --- a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/resolvers/Resolvable.java +++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/resolvers/Resolvable.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008 Ketan Padegaonkar and others. + * Copyright (c) 2008,2011 Ketan Padegaonkar and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -28,6 +28,8 @@ public class Resolvable implements IResolvable { */ public Resolvable() { this(new Resolver()); + resolver.addResolver(new ExpandBarResolver()); + resolver.addResolver(new ExpandItemResolver()); resolver.addResolver(new CTabFolderResolver()); resolver.addResolver(new TabFolderResolver()); resolver.addResolver(new CTabItemResolver()); diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotExpandBar.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotExpandBar.java new file mode 100644 index 00000000..6d51b96a --- /dev/null +++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotExpandBar.java @@ -0,0 +1,187 @@ +/******************************************************************************* + * Copyright (c) 2011 SWTBot Committers and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Toby Weston - initial API and implementation (Bug 259799) + *******************************************************************************/ +package org.eclipse.swtbot.swt.finder.widgets; + +import static java.util.Collections.emptyList; +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.withText; +import static org.eclipse.swtbot.swt.finder.waits.Conditions.waitForWidget; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.swt.widgets.ExpandBar; +import org.eclipse.swt.widgets.ExpandItem; +import org.eclipse.swt.widgets.Widget; +import org.eclipse.swtbot.swt.finder.ReferenceBy; +import org.eclipse.swtbot.swt.finder.SWTBot; +import org.eclipse.swtbot.swt.finder.SWTBotWidget; +import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; +import org.eclipse.swtbot.swt.finder.results.IntResult; +import org.eclipse.swtbot.swt.finder.results.ListResult; +import org.eclipse.swtbot.swt.finder.waits.WaitForObjectCondition; +import org.hamcrest.Matcher; +import org.hamcrest.SelfDescribing; + +/** + * Represents an {@link ExpandBar}. + * + * @author Toby Weston + */ +@SWTBotWidget(clasz = ExpandBar.class, preferredName = "expandBar", referenceBy = { ReferenceBy.LABEL }) +public class SWTBotExpandBar extends AbstractSWTBot<ExpandBar> { + + /** + * Constructs a new instance with the given widget. + * + * @param w the widget. + * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed. + */ + public SWTBotExpandBar(ExpandBar w) { + super(w); + } + + /** + * Constructs a new instance with the given widget. + * + * @param w the widget. + * @param description the description of the widget, this will be reported by {@link #toString()} + * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed. + */ + public SWTBotExpandBar(ExpandBar w, SelfDescribing description) { + super(w, description); + } + + /** + * @return the number of {@link ExpandItem}s in the widget. + */ + public int itemCount() { + return syncExec(new IntResult() { + public Integer run() { + return widget.getItemCount(); + } + }); + } + + /** + * @return the number of items that are expanded. + * @see #itemCount() + * @see #collapsedItemCount() + */ + public int expandedItemCount() { + return syncExec(new IntResult() { + public Integer run() { + int count = 0; + for (ExpandItem item : widget.getItems()) { + if (item.getExpanded()) + count++; + } + return count; + } + }); + } + + /** + * @return the number of items that are collapsed. + * @see #itemCount() + * @see #expandedItemCount() + */ + public int collapsedItemCount() { + return itemCount() - expandedItemCount(); + } + + /** + * Expands the {@link ExpandItem} and returns it. + * + * @param itemText the text on the item. + * @return the {@link SWTBotExpandItem} with the specified text. + */ + @SuppressWarnings("unchecked") + public SWTBotExpandItem expandItem(final String itemText) { + return expandItem(allOf(widgetOfType(ExpandItem.class), withText(itemText))); + } + + /** + * Expands the {@link ExpandItem} and returns it. + * + * @param matcher the matcher. + * @return the {@link SWTBotExpandItem} that matches the matcher + */ + public SWTBotExpandItem expandItem(Matcher<Widget> matcher) { + waitForEnabled(); + return getExpandItem(matcher).expand(); + } + + /** + * Collapses the {@link ExpandItem} and returns it. + * + * @param itemText the text on the item. + * @return the {@link SWTBotExpandItem} with the specified text. + */ + @SuppressWarnings("unchecked") + public SWTBotExpandItem collapseItem(final String itemText) { + return collapseItem(allOf(widgetOfType(ExpandItem.class), withText(itemText))); + } + + /** + * Collapses the {@link ExpandItem} and returns it. + * + * @param matcher the matcher. + * @return the {@link SWTBotExpandItem} that matches the matcher. + */ + public SWTBotExpandItem collapseItem(Matcher<Widget> matcher) { + waitForEnabled(); + return getExpandItem(matcher).collapse(); + } + + /** + * Return the {@link ExpandItem} that matches the specified matcher. + * + * @param matcher the matcher. + * @return the {@link SWTBotExpandItem} with the specified text. + */ + public SWTBotExpandItem getExpandItem(final Matcher<Widget> matcher) { + try { + WaitForObjectCondition<? extends Widget> waitForWidget = waitForWidget(matcher, widget); + new SWTBot().waitUntilWidgetAppears(waitForWidget); + return new SWTBotExpandItem((ExpandItem) waitForWidget.get(0), matcher); + } catch (TimeoutException e) { + throw new WidgetNotFoundException("Timed out waiting for expandBar item " + matcher, e); + } + } + + /** + * @return a list of {@link SWTBotExpandItem} or the empty list if there was a problem with any of the items. + */ + public List<SWTBotExpandItem> getAllItems() { + return syncExec(new ListResult<SWTBotExpandItem>() { + public List<SWTBotExpandItem> run() { + List<SWTBotExpandItem> result = new ArrayList<SWTBotExpandItem>(); + for (ExpandItem item : widget.getItems()) { + try { + result.add(new SWTBotExpandItem(item)); + } catch (WidgetNotFoundException e) { + return emptyList(); + } + } + return result; + } + }); + } + + /** + * @return <code>true</code> if the expandBar has any items, <code>false</code> otherwise. + */ + public boolean hasItems() { + return itemCount() > 0; + } +} diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotExpandItem.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotExpandItem.java new file mode 100644 index 00000000..ca7cadb6 --- /dev/null +++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotExpandItem.java @@ -0,0 +1,145 @@ +/*******************************************************************************
+ * Copyright (c) 2011 SWTBot Committers and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Toby Weston - initial API and implementation (Bug 259799)
+ *******************************************************************************/
+package org.eclipse.swtbot.swt.finder.widgets;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.ExpandBar;
+import org.eclipse.swt.widgets.ExpandItem;
+import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
+import org.eclipse.swtbot.swt.finder.results.BoolResult;
+import org.eclipse.swtbot.swt.finder.results.VoidResult;
+import org.eclipse.swtbot.swt.finder.results.WidgetResult;
+import org.hamcrest.SelfDescribing;
+
+/**
+ * Represents an {@link ExpandItem}.
+ *
+ * @author Toby Weston (Bug 259799)
+ * @version $Id$
+ */
+public class SWTBotExpandItem extends AbstractSWTBot<ExpandItem> {
+ private ExpandBar expandBar;
+
+ /**
+ * Constructs a new instance with the given widget.
+ *
+ * @param w the widget.
+ * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
+ */
+ public SWTBotExpandItem(ExpandItem w) throws WidgetNotFoundException {
+ this(w, null);
+ }
+
+ /**
+ * Constructs a new instance with the given widget.
+ *
+ * @param w the widget.
+ * @param description the description of the widget, this will be reported by {@link #toString()}
+ * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
+ */
+ public SWTBotExpandItem(final ExpandItem w, SelfDescribing description) throws WidgetNotFoundException {
+ super(w, description);
+ this.expandBar = syncExec(new WidgetResult<ExpandBar>() {
+ public ExpandBar run() {
+ return w.getParent();
+ }
+ });
+ }
+
+ /**
+ * Expand this item and return itself.
+ *
+ * @return itself.
+ */
+ public SWTBotExpandItem expand() {
+ asyncExec(new VoidResult() {
+ public void run() {
+ if (isExpanded())
+ return;
+ widget.setExpanded(true);
+ expandNotify();
+ }
+ });
+ return this;
+ }
+
+ /**
+ * Collapse this item and return itself.
+ *
+ * @return itself.
+ */
+ public SWTBotExpandItem collapse() {
+ asyncExec(new VoidResult() {
+ public void run() {
+ if (isCollapsed())
+ return;
+ widget.setExpanded(false);
+ collapseNotify();
+ }
+ });
+ return this;
+ }
+
+ /**
+ * @return <code>true</code> if the item is expanded, <code>false</code> otherwise.
+ * @see #isCollapsed()
+ */
+ public boolean isExpanded() {
+ return syncExec(new BoolResult() {
+ public Boolean run() {
+ return widget.getExpanded();
+ }
+ });
+ }
+
+ /**
+ * @return <code>true</code> if the item is collapsed, <code>false</code> otherwise.
+ * @see SWTBotExpandItem#isExpanded()
+ */
+ public boolean isCollapsed() {
+ return !isExpanded();
+ }
+
+ private void expandNotify() {
+ notifyExpandBar(SWT.MouseMove);
+ notifyExpandBar(SWT.Activate);
+ notifyExpandBar(SWT.FocusIn);
+ notifyExpandBar(SWT.MouseDown);
+ notifyExpandBar(SWT.Expand);
+ notifyExpandBar(SWT.MeasureItem);
+ notifyExpandBar(SWT.Deactivate);
+ notifyExpandBar(SWT.FocusOut);
+ }
+
+ private void collapseNotify() {
+ notifyExpandBar(SWT.MouseMove);
+ notifyExpandBar(SWT.Activate);
+ notifyExpandBar(SWT.FocusIn);
+ notifyExpandBar(SWT.MouseDown);
+ notifyExpandBar(SWT.Collapse);
+ notifyExpandBar(SWT.MeasureItem);
+ notifyExpandBar(SWT.Deactivate);
+ notifyExpandBar(SWT.FocusOut);
+ }
+
+ private void notifyExpandBar(int eventType) {
+ notify(eventType, createEvent(), expandBar);
+ }
+
+ protected Event createEvent() {
+ Event e = super.createEvent();
+ e.widget = expandBar;
+ e.item = widget;
+ return e;
+ }
+
+}
|