Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAparna Argade2018-03-13 09:22:03 +0000
committerAparna Argade2018-03-23 14:13:50 +0000
commitbed1282a852fb506fe81eb3db89b0f4d02e2e95e (patch)
treedf23a4077fd24ecd8e34c8fe7a1bb8dc540d3868
parent15fbf0d210c39fefe3e98871f5f34a02a6259a03 (diff)
downloadorg.eclipse.swtbot-bed1282a852fb506fe81eb3db89b0f4d02e2e95e.tar.gz
org.eclipse.swtbot-bed1282a852fb506fe81eb3db89b0f4d02e2e95e.tar.xz
org.eclipse.swtbot-bed1282a852fb506fe81eb3db89b0f4d02e2e95e.zip
Bug 532391: New API to maximize SWTBotShell
Change-Id: I5bd3a78d608e53385f3c538d0f8d46e7f764bca8 Signed-off-by: Aparna Argade <aprsac@yahoo.com>
-rw-r--r--org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotShellTest.java50
-rw-r--r--org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotShell.java73
2 files changed, 120 insertions, 3 deletions
diff --git a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotShellTest.java b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotShellTest.java
index b8b0820f..45df14ee 100644
--- a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotShellTest.java
+++ b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotShellTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2008 Ketan Padegaonkar and others.
+ * Copyright (c) 2008, 2018 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
@@ -7,13 +7,17 @@
*
* Contributors:
* Ketan Padegaonkar - initial API and implementation
+ * Aparna Argade - maximize API (Bug 532391)
*******************************************************************************/
package org.eclipse.swtbot.swt.finder.widgets;
import static org.eclipse.swtbot.swt.finder.SWTBotTestCase.assertSameWidget;
import static org.eclipse.swtbot.swt.finder.SWTBotTestCase.pass;
+import static org.hamcrest.Matchers.greaterThan;
+import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import org.eclipse.swt.widgets.Composite;
@@ -22,6 +26,7 @@ import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.finders.ControlFinder;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
+import org.eclipse.swtbot.swt.finder.results.IntResult;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.test.AbstractSWTShellTest;
import org.junit.Test;
@@ -123,4 +128,47 @@ public class SWTBotShellTest extends AbstractSWTShellTest {
assertSame(shell, bot.shellWithId("foo-shell", "bar").widget);
}
+ private int getWidth(final SWTBotShell shell) {
+ return UIThreadRunnable.syncExec(display, new IntResult() {
+ @Override
+ public Integer run() {
+ return shell.widget.getBounds().width;
+ }
+ });
+ }
+
+ private int getHeight(final SWTBotShell shell) {
+ return UIThreadRunnable.syncExec(display, new IntResult() {
+ @Override
+ public Integer run() {
+ return shell.widget.getBounds().height;
+ }
+ });
+ }
+
+ @Test
+ public void testMaximize() throws Exception {
+ final SWTBotShell shell = bot.shell("shell4");
+ final int originalwidth = 400, originalheight = 400;
+ UIThreadRunnable.syncExec(display, new VoidResult() {
+ @Override
+ public void run() {
+ shell.widget.setSize(originalwidth, originalheight);
+ }
+ });
+ assertEquals("Initial width", originalwidth, getWidth(shell));
+ assertEquals("Initial height", originalheight, getHeight(shell));
+
+ //after maximize, bounds are greater than original
+ shell.maximize(true);
+ assertThat(getWidth(shell), greaterThan(originalwidth));
+ assertThat(getHeight(shell), greaterThan(originalheight));
+
+ // after un-maximize, bounds are either equal (normal size) or less than
+ // (minimized) the original
+ shell.maximize(false);
+ assertThat(getWidth(shell), lessThanOrEqualTo(originalwidth));
+ assertThat(getHeight(shell), lessThanOrEqualTo(originalheight));
+ }
+
}
diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotShell.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotShell.java
index 91288337..d68f5ff1 100644
--- a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotShell.java
+++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotShell.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2008, 2015 Ketan Padegaonkar and others.
+ * Copyright (c) 2008, 2018 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
@@ -7,11 +7,13 @@
*
* Contributors:
* Ketan Padegaonkar - initial API and implementation
- * Patrick Tasse - Improve SWTBot menu API and implementation (Bug 479091)
+ * Patrick Tasse - Improve SWTBot menu API and implementation (Bug 479091)
+ * Aparna Argade - maximize API (Bug 532391)
*******************************************************************************/
package org.eclipse.swtbot.swt.finder.widgets;
import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.swt.finder.SWTBot;
@@ -19,6 +21,7 @@ import org.eclipse.swtbot.swt.finder.SWTBotWidget;
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.utils.MessageFormat;
import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
import org.eclipse.swtbot.swt.finder.waits.Conditions;
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
@@ -171,4 +174,70 @@ public class SWTBotShell extends AbstractSWTBotControl<Shell> {
bot().waitUntil(waitForMenu);
return new SWTBotRootMenu(waitForMenu.get(0));
}
+
+ /**
+ * Sets the maximized state of the shell.
+ * <p>
+ * If the argument is <code>true</code> causes the shell to switch to the
+ * maximized state, and if the argument is <code>false</code> and the shell was
+ * previously maximized, causes the shell to switch back to either the minimized
+ * or normal state.
+ *
+ * @param maximize
+ * the new maximized state
+ * @return itself
+ * @throws TimeoutException if the shell does not resize as per the expectation.
+ * @since 2.7
+ */
+ public SWTBotShell maximize(final boolean maximize) throws TimeoutException {
+ if (maximize == isMaximizedState()) {
+ log.debug(MessageFormat.format("{0} is already in expected state, not resizing again.", this)); //$NON-NLS-1$
+ return this;
+ }
+ final Rectangle initialRectangle = getBounds();
+ activate();
+ syncExec(new VoidResult() {
+ @Override
+ public void run() {
+ widget.setMaximized(maximize);
+ }
+ });
+
+ new SWTBot().waitUntil(new DefaultCondition() {
+ @Override
+ public String getFailureMessage() {
+ return "Timed out waiting for " + SWTUtils.toString(widget) + " to get maximized/unmaximized"; //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ @Override
+ public boolean test() throws Exception {
+ /* Wait for maximized state and also for resize to complete. */
+ if (maximize) {
+ return isMaximizedState() && isResized(initialRectangle);
+ } else {
+ return !isMaximizedState() && isResized(initialRectangle);
+ }
+ }
+ });
+ return this;
+ }
+
+ private boolean isMaximizedState() {
+ return syncExec(new BoolResult() {
+ @Override
+ public Boolean run() {
+ return widget.getMaximized();
+ }
+ });
+ }
+
+ private boolean isResized(final Rectangle initialRectangle) {
+ Rectangle newRectangle = getBounds();
+ if ((initialRectangle.width != newRectangle.width) || (initialRectangle.height != newRectangle.height)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
}

Back to the top