Created
July 19, 2014 14:08
-
-
Save nebulorum/b841ed14a2958cf8edce to your computer and use it in GitHub Desktop.
Example of how test window closing logic in UISpec4J
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.uispec4j.demo; | |
import org.uispec4j.Trigger; | |
import org.uispec4j.UISpecAdapter; | |
import org.uispec4j.UISpecTestCase; | |
import org.uispec4j.Window; | |
import org.uispec4j.interception.WindowInterceptor; | |
import javax.swing.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.WindowAdapter; | |
import java.awt.event.WindowEvent; | |
import static javax.swing.JOptionPane.WARNING_MESSAGE; | |
import static javax.swing.JOptionPane.showConfirmDialog; | |
public class WindowClosingConfirmationTest extends UISpecTestCase { | |
public static interface TerminationHandler { | |
void terminateApplication(); | |
} | |
public static class TestFrame extends JFrame { | |
public TestFrame(final TerminationHandler handler) { | |
final JFrame frame = this; | |
setTitle("Test"); | |
JButton close = new JButton("Close"); | |
add(close); | |
close.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent actionEvent) { | |
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); | |
} | |
}); | |
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); | |
addWindowListener(new WindowAdapter() { | |
@Override | |
public void windowClosing(WindowEvent windowEvent) { | |
int result = showConfirmDialog(frame, "You are leaving", "Closing...", JOptionPane.YES_NO_OPTION); | |
System.out.println("Closing was called"); | |
if (result == JOptionPane.YES_OPTION) | |
handler.terminateApplication(); | |
} | |
}); | |
} | |
} | |
static class MockHandler implements TerminationHandler { | |
private boolean terminated = false; | |
@Override | |
public void terminateApplication() { | |
terminated = true; | |
} | |
public boolean isTerminated() { | |
return terminated; | |
} | |
} | |
private JFrame frame = null; | |
private MockHandler handler = new MockHandler(); | |
@Override | |
protected void setUp() throws Exception { | |
super.setUp(); | |
frame = new TestFrame(handler); | |
setAdapter(new UISpecAdapter() { | |
@Override | |
public Window getMainWindow() { | |
return new Window(frame); | |
} | |
}); | |
} | |
public void testHasButton() { | |
assertTrue(getMainWindow().getButton().isEnabled()); | |
} | |
public void testHandleClosing() { | |
WindowInterceptor. | |
init(createCloseWindowTrigger()). | |
processWithButtonClick("Closing...", "Yes"). | |
run(); | |
assertTrue("Handler not called", handler.isTerminated()); | |
} | |
public void testHandleClosing_butCancel() { | |
WindowInterceptor. | |
init(createCloseWindowTrigger()). | |
processWithButtonClick("Closing...", "No"). | |
run(); | |
assertFalse("Handler called", handler.isTerminated()); | |
} | |
private Trigger createCloseWindowTrigger() { | |
return new Trigger() { | |
@Override | |
public void run() throws Exception { | |
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment