public class ButtonTest extends TestCase {
/**
* Test Button onProcess behavior.
*/
public void testOnProcess() {
MockContext context = MockContext.initContext();
MockRequest request = context.getMockRequest();
Button button = new Button("button");
assertEquals("button", button.getName());
assertTrue(button.onProcess());
request.setParameter("button", "true");
assertTrue(button.onProcess());
final boolean check[] = new boolean[1];
button.setActionListener(new ActionListener() {
private static final long serialVersionUID = 1L;
public boolean onAction(Control source) {
check[0] = true;
return false;
}
});
// No request param -> no action listener executed
request.removeParameter("button");
assertTrue(button.onProcess());
context.executeActionListeners();
assertFalse(check[0]);
request.setParameter("button", "true");
// Not an ajax request -> no action listener executed
assertTrue(button.onProcess());
context.executeActionListeners();
assertFalse(check[0]);
// ajax request, but no request param -> no action listener executed
request.removeParameter("button");
request.setParameter("X-Requested-With", "true");
assertTrue(button.onProcess());
context.executeActionListeners();
assertFalse(check[0]);
// Ajax request & request param -> call the onAction.
request.setParameter("button", "true");
request.setParameter("X-Requested-With", "true");
assertTrue(button.onProcess());
context.executeActionListeners();
assertTrue(check[0]);
button.setDisabled(true);
assertTrue(button.onProcess());