From fc79792301c95d10207309d4f495d2cf11b678cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Valach?= Date: Tue, 13 Mar 2018 14:06:04 +0100 Subject: [PATCH] REDDEER-1838 Implement a simple shell with Ok and Cancel buttons MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lukáš Valach --- .../reddeer/swt/impl/shell/OkCancelShell.java | 97 ++++++++++++++ .../test/impl/shell/OkCancelShellTest.java | 126 ++++++++++++++++++ 2 files changed, 223 insertions(+) create mode 100644 plugins/org.eclipse.reddeer.swt/src/org/eclipse/reddeer/swt/impl/shell/OkCancelShell.java create mode 100644 tests/org.eclipse.reddeer.swt.test/src/org/eclipse/reddeer/swt/test/impl/shell/OkCancelShellTest.java diff --git a/plugins/org.eclipse.reddeer.swt/src/org/eclipse/reddeer/swt/impl/shell/OkCancelShell.java b/plugins/org.eclipse.reddeer.swt/src/org/eclipse/reddeer/swt/impl/shell/OkCancelShell.java new file mode 100644 index 0000000000..cc464b82eb --- /dev/null +++ b/plugins/org.eclipse.reddeer.swt/src/org/eclipse/reddeer/swt/impl/shell/OkCancelShell.java @@ -0,0 +1,97 @@ +/******************************************************************************* + * Copyright (c) 2018 Red Hat, Inc 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: + * Red Hat Inc. - initial API and implementation + *******************************************************************************/ +package org.eclipse.reddeer.swt.impl.shell; + +import org.eclipse.reddeer.swt.impl.button.CancelButton; +import org.eclipse.reddeer.swt.impl.button.OkButton; +import org.eclipse.swt.widgets.Shell; +import org.hamcrest.Matcher; + +/** + * Represents a simple shell with "OK" and "Cancel" buttons. + * + * @author lvalach + * + */ +public class OkCancelShell extends DefaultShell { + + /** + * Instantiates a new OkCancelShell. + * + * @param text + * shell text + */ + public OkCancelShell(String text) { + super(text); + } + + /** + * Instantiates a new OkCancelShell. + * + * @param shell + * shell with both "OK" and "Cancel" buttons + */ + public OkCancelShell(Shell shell) { + super(shell); + } + + /** + * + * Creates a new OkCancelShell matching specified matcher. First found shell + * with specified matcher is created. Beware, there is no strict (deterministic) + * order of shells. + * + * @param matchers + * matchers to match title of a shell + */ + public OkCancelShell(Matcher... matchers) { + super(matchers); + } + + /** + * Instantiates a new OkCancelShell. + */ + public OkCancelShell() { + super(); + } + + /** + * Click "OK" button. + */ + public void ok() { + new OkButton(this).click(); + } + + /** + * Click "Cancel" button. + */ + public void cancel() { + new CancelButton(this).click(); + } + + /** + * Finds out whether a "OK" button is enabled. + * + * @return true if button is enabled, false otherwise + */ + public boolean isOkEnabled() { + return new OkButton(this).isEnabled(); + } + + /** + * Finds out whether a "Cancel" button is enabled. + * + * @return true if button is enabled, false otherwise + */ + public boolean isCancelEnabled() { + return new CancelButton(this).isEnabled(); + } +} diff --git a/tests/org.eclipse.reddeer.swt.test/src/org/eclipse/reddeer/swt/test/impl/shell/OkCancelShellTest.java b/tests/org.eclipse.reddeer.swt.test/src/org/eclipse/reddeer/swt/test/impl/shell/OkCancelShellTest.java new file mode 100644 index 0000000000..98fea0139d --- /dev/null +++ b/tests/org.eclipse.reddeer.swt.test/src/org/eclipse/reddeer/swt/test/impl/shell/OkCancelShellTest.java @@ -0,0 +1,126 @@ +/******************************************************************************* + * Copyright (c) 2018 Red Hat, Inc 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: + * Red Hat Inc. - initial API and implementation + *******************************************************************************/ +package org.eclipse.reddeer.swt.test.impl.shell; + +import static org.junit.Assert.fail; + +import org.eclipse.reddeer.common.condition.AbstractWaitCondition; +import org.eclipse.reddeer.common.exception.WaitTimeoutExpiredException; +import org.eclipse.reddeer.common.util.Display; +import org.eclipse.reddeer.common.wait.WaitUntil; +import org.eclipse.reddeer.junit.runner.RedDeerSuite; +import org.eclipse.reddeer.swt.impl.shell.OkCancelShell; +import org.eclipse.reddeer.swt.test.utils.ShellTestUtils; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Listener; +import org.eclipse.swt.widgets.Shell; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(RedDeerSuite.class) +public class OkCancelShellTest { + + protected static final String SHELL_TITLE = "Testing shell"; + + private Button okButton; + private Button cancelButton; + private ButtonClickListener okListener; + private ButtonClickListener cancelListener; + + @Before + public void setUp() { + Display.syncExec(new Runnable() { + @Override + public void run() { + Shell shell = ShellTestUtils.createShell(SHELL_TITLE); + + okButton = new org.eclipse.swt.widgets.Button(shell, SWT.PUSH); + okButton.setText("OK"); + okListener = new ButtonClickListener(); + okButton.addListener(SWT.Selection, okListener); + + cancelButton = new org.eclipse.swt.widgets.Button(shell, SWT.PUSH); + cancelButton.setText("Cancel"); + cancelListener = new ButtonClickListener(); + cancelButton.addListener(SWT.Selection, cancelListener); + } + }); + } + + @Test + public void okAndCancelButtonsTest() { + OkCancelShell shell = new OkCancelShell(SHELL_TITLE); + + shell.ok(); + try { + new WaitUntil(new ButtonHeardClickNotification(okListener)); + } catch (WaitTimeoutExpiredException e) { + fail("OK button didn't registered click event."); + } + + shell.cancel(); + try { + new WaitUntil(new ButtonHeardClickNotification(cancelListener)); + } catch (WaitTimeoutExpiredException e) { + fail("Cancel button didn't registered click event."); + } + } + + @After + public void cleanup() { + Display.syncExec(new Runnable() { + @Override + public void run() { + ShellTestUtils.closeShell(SHELL_TITLE); + } + }); + } + + public class ButtonClickListener implements Listener { + + private boolean heard = false; + + public void handleEvent(Event e) { + switch (e.type) { + case SWT.Selection: + heard = true; + break; + } + } + + public boolean isHeard() { + return heard; + } + } + + private class ButtonHeardClickNotification extends AbstractWaitCondition { + + private ButtonClickListener listener; + + public ButtonHeardClickNotification(ButtonClickListener listener) { + this.listener = listener; + } + + @Override + public boolean test() { + return listener.isHeard(); + } + + @Override + public String description() { + return "button heard click notification"; + } + } +}