Skip to content

Commit

Permalink
REDDEER-1838 Implement a simple shell with Ok and Cancel buttons
Browse files Browse the repository at this point in the history
Signed-off-by: Lukáš Valach <lvalach@redhat.com>
  • Loading branch information
luvalach authored and rawagner committed Mar 16, 2018
1 parent b4adcfc commit fc79792
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 0 deletions.
@@ -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();
}
}
@@ -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";
}
}
}

0 comments on commit fc79792

Please sign in to comment.